Coverage report: /home/ellis/comp/ext/ironclad/src/ciphers/salsa20.lisp

KindCoveredAll%
expression0206 0.0
branch014 0.0
Key
Not instrumented
Conditionalized out
Executed
Not executed
 
Both branches taken
One branch taken
Neither branch taken
1
 ;;; salsa20.lisp --implementation of the Salsa20 stream cipher
2
 (in-package :crypto)
3
 
4
 (declaim (type (simple-octet-vector 16) salsa20-sigma salsa20-tau))
5
 (defconst salsa20-sigma
6
   #.(coerce (map 'vector #'char-code "expand 32-byte k") 'simple-octet-vector))
7
 
8
 (defconst salsa20-tau
9
   #.(coerce (map 'vector #'char-code "expand 16-byte k") 'simple-octet-vector))
10
 
11
 (deftype salsa20-state () '(simple-array (unsigned-byte 32) (16)))
12
 (deftype salsa20-keystream-buffer () '(simple-octet-vector 64))
13
 
14
 (declaim (inline salsa-core))
15
 (defun salsa-core (n-rounds buffer state)
16
   (declare (type salsa20-keystream-buffer buffer))
17
   (declare (type salsa20-state state))
18
   (declare (optimize speed))
19
   #+(and x86-64 ironclad-assembly)
20
   (x-salsa-core n-rounds buffer state)
21
   #-(and x86-64 ironclad-assembly)
22
   (let ((x (make-array 16 :element-type '(unsigned-byte 32))))
23
     (declare (dynamic-extent x))
24
     (replace x state)
25
     (macrolet ((combine (x y z shift)
26
                  `(logxor ,x (rol32 (mod32+ ,y ,z) ,shift)))
27
                (ref (i)
28
                  `(aref x ,i))
29
                (quarter-round (y0 y1 y2 y3)
30
                  `(setf (ref ,y0) (combine (ref ,y0) (ref ,y3) (ref ,y2) 7)
31
                         (ref ,y1) (combine (ref ,y1) (ref ,y0) (ref ,y3) 9)
32
                         (ref ,y2) (combine (ref ,y2) (ref ,y1) (ref ,y0) 13)
33
                         (ref ,y3) (combine (ref ,y3) (ref ,y2) (ref ,y1) 18))))
34
       (dotimes (i n-rounds)
35
         (quarter-round 4 8 12 0)
36
         (quarter-round 9 13 1 5)
37
         (quarter-round 14 2 6 10)
38
         (quarter-round 3 7 11 15)
39
 
40
         (quarter-round 1 2 3 0)
41
         (quarter-round 6 7 4 5)
42
         (quarter-round 11 8 9 10)
43
         (quarter-round 12 13 14 15))
44
       (dotimes (i 16)
45
         (setf (ub32ref/le buffer (* i 4))
46
               (mod32+ (aref x i) (aref state i))))))
47
   (values))
48
 
49
 (defun salsa20/8-core (buffer state)
50
   (declare (type salsa20-keystream-buffer buffer))
51
   (declare (type salsa20-state state))
52
   (salsa-core 4 buffer state))
53
 
54
 (defun salsa20/12-core (buffer state)
55
   (declare (type salsa20-keystream-buffer buffer))
56
   (declare (type salsa20-state state))
57
   (salsa-core 6 buffer state))
58
 
59
 (defun salsa20/20-core (buffer state)
60
   (declare (type salsa20-keystream-buffer buffer))
61
   (declare (type salsa20-state state))
62
   (salsa-core 10 buffer state))
63
 
64
 (defclass salsa20 (stream-cipher)
65
   ((state :reader salsa20-state
66
           :initform (make-array 16 :element-type '(unsigned-byte 32)
67
                                 :initial-element 0)
68
           :type salsa20-state)
69
    (keystream-buffer :reader salsa20-keystream-buffer
70
                      :initform (make-array 64 :element-type '(unsigned-byte 8))
71
                      :type salsa20-keystream-buffer)
72
    (keystream-buffer-remaining :accessor salsa20-keystream-buffer-remaining
73
                                :initform 0
74
                                :type (integer 0 64))
75
    (core-function :reader salsa20-core-function
76
                   :initarg :core-function
77
                   :type function))
78
   (:default-initargs :core-function #'salsa20/20-core))
79
 
80
 (defclass salsa20/12 (salsa20)
81
   ()
82
   (:default-initargs :core-function #'salsa20/12-core))
83
 
84
 (defclass salsa20/8 (salsa20)
85
   ()
86
   (:default-initargs :core-function #'salsa20/8-core))
87
 
88
 (defun salsa20-keyify (cipher key)
89
   (declare (type salsa20 cipher))
90
   (let ((state (salsa20-state cipher)))
91
     (declare (type salsa20-state state))
92
     (multiple-value-bind (constants offset)
93
         (if (= (length key) 16)
94
             (values salsa20-tau 0)
95
             (values salsa20-sigma 16))
96
       (setf (aref state 1) (ub32ref/le key 0)
97
             (aref state 2) (ub32ref/le key 4)
98
             (aref state 3) (ub32ref/le key 8)
99
             (aref state 4) (ub32ref/le key 12))
100
       (setf (aref state 11) (ub32ref/le key (+ offset 0))
101
             (aref state 12) (ub32ref/le key (+ offset 4))
102
             (aref state 13) (ub32ref/le key (+ offset 8))
103
             (aref state 14) (ub32ref/le key (+ offset 12)))
104
       (setf (aref state 0) (ub32ref/le constants 0)
105
             (aref state 5) (ub32ref/le constants 4)
106
             (aref state 10) (ub32ref/le constants 8)
107
             (aref state 15) (ub32ref/le constants 12))
108
       (values))))
109
 
110
 (defmethod shared-initialize :after ((cipher salsa20) slot-names
111
                                      &rest initargs
112
                                      &key (key nil key-p)
113
                                      (initialization-vector nil iv-p)
114
                                      &allow-other-keys)
115
   (declare (ignore initargs key key-p iv-p))
116
   (setf (salsa20-keystream-buffer-remaining cipher) 0)
117
   (when initialization-vector
118
     (when (< (length initialization-vector) 8)
119
       (error 'invalid-initialization-vector
120
              :cipher (class-name (class-of cipher))
121
              :block-length 8))
122
     (let ((state (salsa20-state cipher)))
123
       (declare (type salsa20-state state))
124
       (setf (aref state 6) (ub32ref/le initialization-vector 0)
125
             (aref state 7) (ub32ref/le initialization-vector 4)
126
             (aref state 8) 0
127
             (aref state 9) 0)))
128
   cipher)
129
 
130
 (defmethod schedule-key ((cipher salsa20) key)
131
   (salsa20-keyify cipher key)
132
   cipher)
133
 
134
 (define-stream-cryptor salsa20
135
   (let ((state (salsa20-state context))
136
         (keystream-buffer (salsa20-keystream-buffer context))
137
         (keystream-buffer-remaining (salsa20-keystream-buffer-remaining context))
138
         (core-function (salsa20-core-function context)))
139
     (declare (type salsa20-state state)
140
              (type salsa20-keystream-buffer keystream-buffer)
141
              (type (integer 0 64) keystream-buffer-remaining)
142
              (type function core-function))
143
     (unless (zerop length)
144
       (unless (zerop keystream-buffer-remaining)
145
         (let ((size (min length keystream-buffer-remaining)))
146
           (declare (type (integer 0 64) size))
147
           (xor-block size keystream-buffer (- 64 keystream-buffer-remaining)
148
                      plaintext plaintext-start
149
                      ciphertext ciphertext-start)
150
           (decf keystream-buffer-remaining size)
151
           (decf length size)
152
           (incf ciphertext-start size)
153
           (incf plaintext-start size)))
154
       (unless (zerop length)
155
         (loop
156
           (funcall core-function keystream-buffer state)
157
           (when (zerop (setf (aref state 8)
158
                              (mod32+ (aref state 8) 1)))
159
             (setf (aref state 9) (mod32+ (aref state 9) 1)))
160
           (when (<= length 64)
161
             (xor-block length keystream-buffer 0 plaintext plaintext-start
162
                        ciphertext ciphertext-start)
163
             (setf (salsa20-keystream-buffer-remaining context) (- 64 length))
164
             (return-from salsa20-crypt (values)))
165
           (xor-block 64 keystream-buffer 0 plaintext plaintext-start
166
                      ciphertext ciphertext-start)
167
           (decf length 64)
168
           (incf ciphertext-start 64)
169
           (incf plaintext-start 64)))
170
       (setf (salsa20-keystream-buffer-remaining context) keystream-buffer-remaining))
171
     (values)))
172
 
173
 (defcipher salsa20
174
   (:mode :stream)
175
   (:crypt-function salsa20-crypt)
176
   (:key-length (:fixed 16 32)))
177
 
178
 (defcipher salsa20/12
179
   (:mode :stream)
180
   (:crypt-function salsa20-crypt)
181
   (:key-length (:fixed 16 32)))
182
 
183
 (defcipher salsa20/8
184
   (:mode :stream)
185
   (:crypt-function salsa20-crypt)
186
   (:key-length (:fixed 16 32)))