Coverage report: /home/ellis/comp/ext/ironclad/src/ciphers/salsa20.lisp
Kind | Covered | All | % |
expression | 0 | 206 | 0.0 |
branch | 0 | 14 | 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
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))
9
#.(coerce (map 'vector #'char-code "expand 16-byte k") 'simple-octet-vector))
11
(deftype salsa20-state () '(simple-array (unsigned-byte 32) (16)))
12
(deftype salsa20-keystream-buffer () '(simple-octet-vector 64))
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))
25
(macrolet ((combine (x y z shift)
26
`(logxor ,x (rol32 (mod32+ ,y ,z) ,shift)))
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))))
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)
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))
45
(setf (ub32ref/le buffer (* i 4))
46
(mod32+ (aref x i) (aref state i))))))
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))
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))
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))
64
(defclass salsa20 (stream-cipher)
65
((state :reader salsa20-state
66
:initform (make-array 16 :element-type '(unsigned-byte 32)
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
75
(core-function :reader salsa20-core-function
76
:initarg :core-function
78
(:default-initargs :core-function #'salsa20/20-core))
80
(defclass salsa20/12 (salsa20)
82
(:default-initargs :core-function #'salsa20/12-core))
84
(defclass salsa20/8 (salsa20)
86
(:default-initargs :core-function #'salsa20/8-core))
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))
110
(defmethod shared-initialize :after ((cipher salsa20) slot-names
113
(initialization-vector nil iv-p)
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))
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)
130
(defmethod schedule-key ((cipher salsa20) key)
131
(salsa20-keyify cipher key)
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)
152
(incf ciphertext-start size)
153
(incf plaintext-start size)))
154
(unless (zerop length)
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)))
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)
168
(incf ciphertext-start 64)
169
(incf plaintext-start 64)))
170
(setf (salsa20-keystream-buffer-remaining context) keystream-buffer-remaining))
175
(:crypt-function salsa20-crypt)
176
(:key-length (:fixed 16 32)))
178
(defcipher salsa20/12
180
(:crypt-function salsa20-crypt)
181
(:key-length (:fixed 16 32)))
185
(:crypt-function salsa20-crypt)
186
(:key-length (:fixed 16 32)))