Coverage report: /home/ellis/comp/ext/ironclad/src/digests/skein.lisp

KindCoveredAll%
expression01007 0.0
branch030 0.0
Key
Not instrumented
Conditionalized out
Executed
Not executed
 
Both branches taken
One branch taken
Neither branch taken
1
 ;;;; skein.lisp -- implementation of the Skein hash functions
2
 
3
 (in-package :crypto)
4
 (in-ironclad-readtable)
5
 
6
 ;;; Parameter identifiers
7
 (defconstant +skein-key+ 0)
8
 (defconstant +skein-cfg+ 4)
9
 (defconstant +skein-prs+ 8)
10
 (defconstant +skein-pk+ 12)
11
 (defconstant +skein-kdf+ 16)
12
 (defconstant +skein-non+ 20)
13
 (defconstant +skein-msg+ 48)
14
 (defconstant +skein-out+ 63)
15
 
16
 ;;; Initial values
17
 (declaim (type (simple-array (unsigned-byte 8) (32))
18
                +skein256-iv-128+ +skein256-iv-160+ +skein256-iv-224+
19
                +skein256-iv-256+))
20
 (declaim (type (simple-array (unsigned-byte 8) (64))
21
                +skein512-iv-128+ +skein512-iv-160+ +skein512-iv-224+
22
                +skein512-iv-256+ +skein512-iv-384+ +skein512-iv-512+))
23
 (declaim (type (simple-array (unsigned-byte 8) (128))
24
                +skein1024-iv-384+ +skein1024-iv-512+ +skein1024-iv-1024+))
25
 
26
 (defconst +skein256-iv-128+ #8@(96 114 77 150 6 25 17 225
27
                                 28 129 141 124 167 170 61 136
28
                                 122 15 150 145 244 13 8 16
29
                                 194 193 91 180 229 221 247 204))
30
 (defconst +skein256-iv-160+ #8@(152 94 130 114 20 35 32 20
31
                                 144 229 119 90 162 233 196 42
32
                                 62 214 56 136 86 88 122 212
33
                                 125 171 134 133 150 228 210 45))
34
 (defconst +skein256-iv-224+ #8@(11 234 229 154 140 138 9 198
35
                                 28 25 197 8 134 86 109 135
36
                                 132 56 245 215 215 136 203 153
37
                                 222 181 221 174 177 221 75 56))
38
 (defconst +skein256-iv-256+ #8@(73 180 72 208 96 168 157 252
39
                                 51 216 167 159 71 102 202 47
40
                                 15 132 86 102 137 195 59 179
41
                                 105 218 232 253 32 233 84 106))
42
 
43
 (defconst +skein512-iv-128+ #8@(82 159 191 111 243 123 188 168
44
                                 170 240 26 189 206 114 152 30
45
                                 211 144 33 179 144 23 155 48
46
                                 92 128 148 63 84 184 251 188
47
                                 27 177 49 110 205 27 166 13
48
                                 227 50 106 212 234 235 24 26
49
                                 130 170 132 206 24 91 204 162
50
                                 45 152 70 157 40 171 130 105))
51
 (defconst +skein512-iv-160+ #8@(145 189 19 224 42 26 184 40
52
                                 143 247 189 181 104 22 241 194
53
                                 18 111 165 246 243 216 96 23
54
                                 79 144 57 130 88 71 183 79
55
                                 86 80 175 126 127 224 237 33
56
                                 184 112 237 99 46 146 8 217
57
                                 250 82 203 236 255 118 236 184
58
                                 110 122 242 163 184 123 164 1))
59
 (defconst +skein512-iv-224+ #8@(36 114 103 72 98 97 208 204
60
                                 239 57 35 169 243 92 166 203
61
                                 100 75 255 82 214 105 205 140
62
                                 180 144 184 58 123 237 138 57
63
                                 208 43 125 69 177 209 89 15
64
                                 61 235 212 117 101 254 118 103
65
                                 233 19 116 153 14 199 251 153
66
                                 247 30 196 225 207 252 44 158))
67
 (defconst +skein512-iv-256+ #8@(19 62 219 47 161 68 208 204
68
                                 235 169 121 26 48 144 53 232
69
                                 111 110 129 79 97 160 174 85
70
                                 219 148 155 174 164 103 39 42
71
                                 131 118 221 116 94 2 6 236
72
                                 81 98 116 196 205 54 164 231
73
                                 133 209 58 57 249 186 111 195
74
                                 19 252 237 51 24 186 237 62))
75
 (defconst +skein512-iv-384+ #8@(95 239 117 58 191 198 246 163
76
                                 164 250 132 253 204 249 254 176
77
                                 254 12 119 61 102 221 119 157
78
                                 218 253 104 180 243 203 152 215
79
                                 101 68 14 138 102 166 196 27
80
                                 7 116 128 229 52 212 215 126
81
                                 214 68 236 212 172 193 143 84
82
                                 248 143 161 106 84 23 110 38))
83
 (defconst +skein512-iv-512+ #8@(206 81 156 116 255 173 3 73
84
                                 3 223 70 151 57 222 149 13
85
                                 206 155 199 39 65 147 209 143
86
                                 177 44 53 255 41 86 37 154
87
                                 176 167 108 223 153 37 182 93
88
                                 244 195 213 169 76 57 190 234
89
                                 35 181 117 26 199 18 17 153
90
                                 51 204 15 102 11 164 24 174))
91
 
92
 (defconst +skein1024-iv-384+ #8@(53 74 137 193 184 182 2 81
93
                                  26 241 138 254 227 201 235 254
94
                                  113 237 43 227 6 127 128 12
95
                                  246 145 26 180 82 58 193 96
96
                                  56 124 145 212 93 211 22 151
97
                                  58 29 211 111 18 223 128 231
98
                                  58 48 152 200 182 70 120 121
99
                                  59 42 87 179 168 194 114 177
100
                                  108 74 16 166 3 130 188 201
101
                                  244 36 86 215 56 147 144 101
102
                                  160 129 63 75 104 197 188 148
103
                                  70 253 236 16 30 245 187 62
104
                                  66 133 176 238 11 15 245 45
105
                                  22 101 188 13 48 101 90 59
106
                                  225 188 123 22 210 156 75 72
107
                                  234 175 203 212 71 105 19 45))
108
 (defconst +skein1024-iv-512+ #8@(24 27 27 124 93 14 236 202
109
                                  2 232 3 95 4 14 27 160
110
                                  133 40 145 237 81 4 132 51
111
                                  28 46 236 234 4 251 74 55
112
                                  247 129 53 129 226 160 37 223
113
                                  210 249 18 139 147 64 0 228
114
                                  182 57 237 194 57 213 98 166
115
                                  90 199 216 69 207 133 139 250
116
                                  150 231 237 41 142 237 22 131
117
                                  184 145 159 46 192 137 50 5
118
                                  115 139 81 109 29 239 248 195
119
                                  46 51 239 213 196 195 206 189
120
                                  135 68 151 34 82 126 154 84
121
                                  22 152 116 91 114 8 7 103
122
                                  209 27 88 240 251 40 205 185
123
                                  116 73 128 21 184 64 41 14))
124
 (defconst +skein1024-iv-1024+ #8@(85 35 231 65 7 218 147 213
125
                                   12 224 115 172 17 229 181 21
126
                                   240 196 242 186 174 229 128 81
127
                                   175 175 188 252 211 65 189 3
128
                                   152 168 131 25 253 198 174 28
129
                                   159 88 208 205 139 11 81 110
130
                                   218 74 57 198 253 189 226 119
131
                                   163 176 220 36 181 29 30 193
132
                                   181 154 50 198 249 74 209 214
133
                                   13 126 182 110 252 11 155 106
134
                                   50 19 255 204 13 198 67 146
135
                                   212 2 63 116 222 29 31 26
136
                                   184 11 237 16 60 117 150 9
137
                                   154 150 180 242 34 221 114 101
138
                                   154 87 10 208 98 48 253 97
139
                                   57 229 130 134 110 83 224 29))
140
 
141
 (defun skein-get-iv (block-bits digest-bits)
142
   (ecase block-bits
143
     (256 (ecase digest-bits
144
            (128 +skein256-iv-128+)
145
            (160 +skein256-iv-160+)
146
            (224 +skein256-iv-224+)
147
            (256 +skein256-iv-256+)))
148
     (512 (ecase digest-bits
149
            (128 +skein512-iv-128+)
150
            (160 +skein512-iv-160+)
151
            (224 +skein512-iv-224+)
152
            (256 +skein512-iv-256+)
153
            (384 +skein512-iv-384+)
154
            (512 +skein512-iv-512+)))
155
     (1024 (ecase digest-bits
156
             (384 +skein1024-iv-384+)
157
             (512 +skein1024-iv-512+)
158
             (1024 +skein1024-iv-1024+)))))
159
 
160
 ;;; Functions to generate and update the tweak
161
 
162
 ;;; This function is called a lot by skein-ubi,
163
 ;;; so we try to optimize it for speed.
164
 (declaim (inline skein-increment-counter))
165
 (defun skein-increment-counter (tweak n)
166
   (declare (type (simple-array (unsigned-byte 64) (2)) tweak)
167
            (type (unsigned-byte 32) n)
168
            #.(burn-baby-burn))
169
   (let* ((x (mod64+ (aref tweak 0) n))
170
          (y (mod32+ (logand (aref tweak 1) #xffffffff) (if (< x n) 1 0))))
171
     (declare (type (unsigned-byte 64) x)
172
              (type (unsigned-byte 32) y))
173
     (setf (aref tweak 0) x
174
           (ldb (byte 32 0) (aref tweak 1)) y)
175
     (values)))
176
 
177
 (defun skein-update-tweak (tweak &key
178
                                    (first nil first-p)
179
                                    (final nil final-p)
180
                                    (type nil type-p)
181
                                    (position nil position-p)
182
                                    (position-increment nil position-increment-p))
183
   (when first-p
184
     (setf (ldb (byte 1 62) (aref tweak 1)) (if first 1 0)))
185
   (when final-p
186
     (setf (ldb (byte 1 63) (aref tweak 1)) (if final 1 0)))
187
   (when type-p
188
     (setf (ldb (byte 6 56) (aref tweak 1)) type))
189
   (when position-p
190
     (setf (aref tweak 0) (ldb (byte 64 0) position))
191
     (setf (ldb (byte 32 0) (aref tweak 1)) (ldb (byte 32 64) position)))
192
   (when position-increment-p
193
     (skein-increment-counter tweak position-increment))
194
   (values))
195
 
196
 (defun skein-make-tweak (first final type position)
197
   (let ((tweak (make-array 2
198
                            :element-type '(unsigned-byte 64)
199
                            :initial-element 0)))
200
     (skein-update-tweak tweak
201
                         :first first
202
                         :final final
203
                         :type type
204
                         :position position)
205
     tweak))
206
 
207
 (defun skein-make-configuration-string (output-length)
208
   (let ((cfg (make-array 32
209
                          :element-type '(unsigned-byte 8)
210
                          :initial-element 0)))
211
     (setf (subseq cfg 0 4) #(83 72 65 51))
212
     (setf (subseq cfg 4 6)
213
           (integer-to-octets 1 :n-bits 16 :big-endian nil))
214
     (setf (subseq cfg 8 16)
215
           (integer-to-octets output-length :n-bits 64 :big-endian nil))
216
     cfg))
217
 
218
 ;;; UBI (unique block iteration chaining)
219
 (defgeneric skein-value (state))
220
 (defgeneric skein-tweak (state))
221
 (defgeneric skein-cfg (state))
222
 (defgeneric skein-buffer (state))
223
 (defgeneric skein-buffer-length (state))
224
 (defgeneric skein-cipher (state))
225
 
226
 ;;; This function is called a lot by skein-ubi,
227
 ;;; so we try to optimize it for speed.
228
 (declaim (inline skein-update-cipher))
229
 (defun skein-update-cipher (block-length cipher-key cipher-tweak key tweak)
230
   (declare (type fixnum block-length)
231
            (type (simple-array (unsigned-byte 64) (*)) cipher-key)
232
            (type (simple-array (unsigned-byte 64) (3)) cipher-tweak)
233
            (type (simple-array (unsigned-byte 8) (*)) key)
234
            (type (simple-array (unsigned-byte 64) (2)) tweak)
235
            #.(burn-baby-burn))
236
   (let ((key-words (ash block-length -3))
237
         (parity +threefish-key-schedule-constant+)
238
         (n 0))
239
     (declare (type (unsigned-byte 64) parity n key-words))
240
 
241
     ;; Update key
242
     (loop for i of-type fixnum from 0 below key-words do
243
       (setf n (ub64ref/le key (ash i 3))
244
             (aref cipher-key i) n
245
             parity (logxor parity n)))
246
     (setf (aref cipher-key key-words) parity)
247
 
248
     ;; Update tweak
249
     (setf (aref cipher-tweak 0) (aref tweak 0)
250
           (aref cipher-tweak 1) (aref tweak 1)
251
           (aref cipher-tweak 2) (logxor (aref tweak 0) (aref tweak 1)))
252
     (values)))
253
 
254
 (defun skein-ubi (state message start end &optional final)
255
   (declare (type (simple-array (unsigned-byte 8) (*)) message)
256
            (type index start end)
257
            #.(burn-baby-burn))
258
   (let* ((cipher (skein-cipher state))
259
          (encryption-function (encrypt-function cipher))
260
          (cipher-key (threefish-key cipher))
261
          (cipher-tweak (threefish-tweak cipher))
262
          (block-length (block-length state))
263
          (value (skein-value state))
264
          (tweak (skein-tweak state))
265
          (buffer (skein-buffer state))
266
          (buffer-length (skein-buffer-length state))
267
          (message-start start)
268
          (message-length (- end start))
269
          (ciphertext (make-array 128
270
                                  :element-type '(unsigned-byte 8)
271
                                  :initial-element 0))
272
          (n 0))
273
     (declare (type (simple-array (unsigned-byte 64) (*)) cipher-key)
274
              (type (simple-array (unsigned-byte 64) (3)) cipher-tweak)
275
              (type (simple-array (unsigned-byte 8) (*)) value buffer)
276
              (type (simple-array (unsigned-byte 8) (128)) ciphertext)
277
              (dynamic-extent ciphertext)
278
              (type (simple-array (unsigned-byte 64) (2)) tweak)
279
              (type (integer 0 128) block-length buffer-length n)
280
              (type index message-start message-length))
281
 
282
     ;; Try to fill the buffer with the new data
283
     (setf n (min message-length (- block-length buffer-length)))
284
     (replace buffer message
285
              :start1 buffer-length
286
              :start2 message-start
287
              :end2 (+ message-start n))
288
     (incf buffer-length n)
289
     (incf message-start n)
290
     (decf message-length n)
291
 
292
     ;; Process as many blocks as we can, but unless we are in the
293
     ;; final call, keep some data in the buffer (so that it can be
294
     ;; processed with the 'final' tweak flag in the final call)
295
 
296
     ;; Process data in buffer
297
     (when (and (= buffer-length block-length)
298
                (or final (plusp message-length)))
299
       (unless final
300
         (skein-increment-counter tweak block-length))
301
       (skein-update-cipher block-length cipher-key cipher-tweak value tweak)
302
       (funcall encryption-function cipher buffer 0 ciphertext 0)
303
       (skein-update-tweak tweak :first nil)
304
       (xor-block block-length ciphertext 0 buffer 0 value 0)
305
       (setf buffer-length 0))
306
 
307
     ;; Process data in message
308
     (unless final
309
       (loop until (<= message-length block-length) do
310
         (skein-increment-counter tweak block-length)
311
         (skein-update-cipher block-length cipher-key cipher-tweak value tweak)
312
         (funcall encryption-function cipher message message-start ciphertext 0)
313
         (xor-block block-length ciphertext 0 message message-start value 0)
314
         (incf message-start block-length)
315
         (decf message-length block-length)))
316
 
317
     ;; Put remaining message data in buffer
318
     (when (plusp message-length)
319
       (replace buffer message :end1 message-length :start2 message-start)
320
       (incf buffer-length message-length))
321
 
322
     ;; Save the new state
323
     (setf (skein-buffer-length state) buffer-length)
324
     (values)))
325
 
326
 (defun skein-finalize (state digest digest-start)
327
   (let* ((block-length (block-length state))
328
          (digest-length (digest-length state))
329
          (tweak (skein-tweak state))
330
          (buffer-length (skein-buffer-length state))
331
          (padding-length (- block-length buffer-length))
332
          (padding (make-array padding-length
333
                               :element-type '(unsigned-byte 8)
334
                               :initial-element 0)))
335
     ;; Process remaining data after padding it
336
     (skein-update-tweak tweak :final t :position-increment buffer-length)
337
     (skein-ubi state padding 0 padding-length t)
338
 
339
     ;; Generate output
340
     (do* ((value (copy-seq (skein-value state)))
341
           (noutputs (ceiling digest-length block-length))
342
           (output (make-array (* noutputs block-length)
343
                               :element-type '(unsigned-byte 8)))
344
           (i 0 (1+ i))
345
           (msg (make-array block-length
346
                            :element-type '(unsigned-byte 8)
347
                            :initial-element 0)))
348
          ((= i noutputs)
349
           (progn
350
             (replace digest output :start1 digest-start :end2 digest-length)
351
             digest))
352
       (replace msg (integer-to-octets i :n-bits 64 :big-endian nil) :end2 8)
353
       (replace (skein-value state) value)
354
       (skein-update-tweak tweak :first t :final t :type +skein-out+ :position 8)
355
       (skein-ubi state msg 0 block-length t)
356
       (replace output (skein-value state) :start1 (* i block-length) :end2 block-length))))
357
 
358
 (defun skein-copy-cipher (cipher &optional copy)
359
   (let* ((tmp-key (make-array (block-length cipher)
360
                               :element-type '(unsigned-byte 8)))
361
          (cipher-name (ecase (block-length cipher)
362
                         (32 :threefish256)
363
                         (64 :threefish512)
364
                         (128 :threefish1024)))
365
          (copy (if copy copy (make-cipher cipher-name
366
                                           :key tmp-key
367
                                           :mode :ecb))))
368
     (setf (threefish-key copy) (copy-seq (threefish-key cipher)))
369
     (setf (threefish-tweak copy) (copy-seq (threefish-tweak cipher)))
370
     copy))
371
 
372
 ;;; Implementation for blocks of 256 bits
373
 (defstruct (skein256
374
              (:constructor %make-skein256-digest nil)
375
              (:copier nil))
376
   (value (copy-seq (skein-get-iv 256 256))
377
          :type (simple-array (unsigned-byte 8) (32)))
378
   (tweak (skein-make-tweak t nil +skein-msg+ 0)
379
          :type (simple-array (unsigned-byte 64) (2)))
380
   (cfg (skein-make-configuration-string 256)
381
        :type (simple-array (unsigned-byte 8) (32)))
382
   (buffer (make-array 32 :element-type '(unsigned-byte 8))
383
           :type (simple-array (unsigned-byte 8) (32)))
384
   (buffer-length 0 :type integer)
385
   (cipher (make-cipher :threefish256
386
                        :key (skein-get-iv 256 256)
387
                        :mode :ecb)))
388
 
389
 (defstruct (skein256/128
390
              (:include skein256)
391
              (:constructor %make-skein256/128-digest
392
                            (&aux (value (copy-seq (skein-get-iv 256 128)))
393
                                  (cfg (skein-make-configuration-string 128))
394
                                  (cipher (make-cipher :threefish256
395
                                                       :key (skein-get-iv 256 128)
396
                                                       :mode :ecb))))
397
              (:copier nil)))
398
 
399
 (defstruct (skein256/160
400
              (:include skein256)
401
              (:constructor %make-skein256/160-digest
402
                            (&aux (value (copy-seq (skein-get-iv 256 160)))
403
                                  (cfg (skein-make-configuration-string 160))
404
                                  (cipher (make-cipher :threefish256
405
                                                       :key (skein-get-iv 256 160)
406
                                                       :mode :ecb))))
407
              (:copier nil)))
408
 
409
 (defstruct (skein256/224
410
              (:include skein256)
411
              (:constructor %make-skein256/224-digest
412
                            (&aux (value (copy-seq (skein-get-iv 256 224)))
413
                                  (cfg (skein-make-configuration-string 224))
414
                                  (cipher (make-cipher :threefish256
415
                                                       :key (skein-get-iv 256 224)
416
                                                       :mode :ecb))))
417
              (:copier nil)))
418
 
419
 (defmethod skein-value ((state skein256))
420
   (skein256-value state))
421
 
422
 (defmethod skein-tweak ((state skein256))
423
   (skein256-tweak state))
424
 
425
 (defmethod skein-cfg ((state skein256))
426
   (skein256-cfg state))
427
 
428
 (defmethod skein-buffer ((state skein256))
429
   (skein256-buffer state))
430
 
431
 (defmethod skein-buffer-length ((state skein256))
432
   (skein256-buffer-length state))
433
 
434
 (defmethod (setf skein-buffer-length) (n (state skein256))
435
   (setf (skein256-buffer-length state) n))
436
 
437
 (defmethod skein-cipher ((state skein256))
438
   (skein256-cipher state))
439
 
440
 (defmethod (setf skein-cipher) (cipher (state skein256))
441
   (setf (skein256-cipher state) cipher))
442
 
443
 (defun %reinitialize-skein256 (state digest-bits)
444
   (declare (type skein256 state))
445
   (replace (skein256-value state) (skein-get-iv 256 digest-bits))
446
   (replace (skein256-tweak state) (skein-make-tweak t nil +skein-msg+ 0))
447
   (replace (skein256-cfg state) (skein-make-configuration-string digest-bits))
448
   (setf (skein256-buffer-length state) 0)
449
   (setf (skein256-cipher state) (make-cipher :threefish256
450
                                              :key (skein-get-iv 256 digest-bits)
451
                                              :mode :ecb))
452
   state)
453
   
454
 (defmethod reinitialize-instance ((state skein256) &rest initargs)
455
   (declare (ignore initargs))
456
   (%reinitialize-skein256 state 256))
457
 
458
 (defmethod reinitialize-instance ((state skein256/128) &rest initargs)
459
   (declare (ignore initargs))
460
   (%reinitialize-skein256 state 128))
461
 
462
 (defmethod reinitialize-instance ((state skein256/160) &rest initargs)
463
   (declare (ignore initargs))
464
   (%reinitialize-skein256 state 160))
465
 
466
 (defmethod reinitialize-instance ((state skein256/224) &rest initargs)
467
   (declare (ignore initargs))
468
   (%reinitialize-skein256 state 224))
469
 
470
 (defmethod copy-digest ((state skein256) &optional copy)
471
   (check-type copy (or null skein256))
472
   (let ((copy (if copy
473
                   copy
474
                   (etypecase state
475
                     (skein256/128 (%make-skein256/128-digest))
476
                     (skein256/160 (%make-skein256/160-digest))
477
                     (skein256/224 (%make-skein256/224-digest))
478
                     (skein256 (%make-skein256-digest))))))
479
     (declare (type skein256 copy))
480
     (replace (skein256-value copy) (skein256-value state))
481
     (replace (skein256-tweak copy) (skein256-tweak state))
482
     (replace (skein256-cfg copy) (skein256-cfg state))
483
     (replace (skein256-buffer copy) (skein256-buffer state))
484
     (setf (skein256-buffer-length copy) (skein256-buffer-length state))
485
     (setf (skein256-cipher copy) (skein-copy-cipher (skein256-cipher state)))
486
     copy))
487
 
488
 (define-digest-updater skein256
489
   (skein-ubi state sequence start end))
490
 
491
 (define-digest-finalizer ((skein256 32)
492
                           (skein256/224 28)
493
                           (skein256/160 20)
494
                           (skein256/128 16))
495
   (skein-finalize state digest digest-start))
496
 
497
 (defdigest skein256 :digest-length 32 :block-length 32)
498
 (defdigest skein256/128 :digest-length 16 :block-length 32)
499
 (defdigest skein256/160 :digest-length 20 :block-length 32)
500
 (defdigest skein256/224 :digest-length 28 :block-length 32)
501
 
502
 ;;; Implementation for blocks of 512 bits
503
 (defstruct (skein512
504
              (:constructor %make-skein512-digest nil)
505
              (:copier nil))
506
   (value (copy-seq (skein-get-iv 512 512))
507
          :type (simple-array (unsigned-byte 8) (64)))
508
   (tweak (skein-make-tweak t nil +skein-msg+ 0)
509
          :type (simple-array (unsigned-byte 64) (2)))
510
   (cfg (skein-make-configuration-string 512)
511
        :type (simple-array (unsigned-byte 8) (32)))
512
   (buffer (make-array 64 :element-type '(unsigned-byte 8))
513
           :type (simple-array (unsigned-byte 8) (64)))
514
   (buffer-length 0 :type integer)
515
   (cipher (make-cipher :threefish512
516
                        :key (skein-get-iv 512 512)
517
                        :mode :ecb)))
518
 
519
 (defstruct (skein512/128
520
              (:include skein512)
521
              (:constructor %make-skein512/128-digest
522
                            (&aux (value (copy-seq (skein-get-iv 512 128)))
523
                                  (cfg (skein-make-configuration-string 128))
524
                                  (cipher (make-cipher :threefish512
525
                                                       :key (skein-get-iv 512 128)
526
                                                       :mode :ecb))))
527
              (:copier nil)))
528
 
529
 (defstruct (skein512/160
530
              (:include skein512)
531
              (:constructor %make-skein512/160-digest
532
                            (&aux (value (copy-seq (skein-get-iv 512 160)))
533
                                  (cfg (skein-make-configuration-string 160))
534
                                  (cipher (make-cipher :threefish512
535
                                                       :key (skein-get-iv 512 160)
536
                                                       :mode :ecb))))
537
              (:copier nil)))
538
 
539
 (defstruct (skein512/224
540
              (:include skein512)
541
              (:constructor %make-skein512/224-digest
542
                            (&aux (value (copy-seq (skein-get-iv 512 224)))
543
                                  (cfg (skein-make-configuration-string 224))
544
                                  (cipher (make-cipher :threefish512
545
                                                       :key (skein-get-iv 512 224)
546
                                                       :mode :ecb))))
547
              (:copier nil)))
548
 
549
 (defstruct (skein512/256
550
              (:include skein512)
551
              (:constructor %make-skein512/256-digest
552
                            (&aux (value (copy-seq (skein-get-iv 512 256)))
553
                                  (cfg (skein-make-configuration-string 256))
554
                                  (cipher (make-cipher :threefish512
555
                                                       :key (skein-get-iv 512 256)
556
                                                       :mode :ecb))))
557
              (:copier nil)))
558
 
559
 (defstruct (skein512/384
560
              (:include skein512)
561
              (:constructor %make-skein512/384-digest
562
                            (&aux (value (copy-seq (skein-get-iv 512 384)))
563
                                  (cfg (skein-make-configuration-string 384))
564
                                  (cipher (make-cipher :threefish512
565
                                                       :key (skein-get-iv 512 384)
566
                                                       :mode :ecb))))
567
              (:copier nil)))
568
 
569
 (defmethod skein-value ((state skein512))
570
   (skein512-value state))
571
 
572
 (defmethod skein-tweak ((state skein512))
573
   (skein512-tweak state))
574
 
575
 (defmethod skein-cfg ((state skein512))
576
   (skein512-cfg state))
577
 
578
 (defmethod skein-buffer ((state skein512))
579
   (skein512-buffer state))
580
 
581
 (defmethod skein-buffer-length ((state skein512))
582
   (skein512-buffer-length state))
583
 
584
 (defmethod (setf skein-buffer-length) (n (state skein512))
585
   (setf (skein512-buffer-length state) n))
586
 
587
 (defmethod skein-cipher ((state skein512))
588
   (skein512-cipher state))
589
 
590
 (defmethod (setf skein-cipher) (cipher (state skein512))
591
   (setf (skein512-cipher state) cipher))
592
 
593
 (defun %reinitialize-skein512 (state digest-bits)
594
   (declare (type skein512 state))
595
   (replace (skein512-value state) (skein-get-iv 512 digest-bits))
596
   (replace (skein512-tweak state) (skein-make-tweak t nil +skein-msg+ 0))
597
   (replace (skein512-cfg state) (skein-make-configuration-string digest-bits))
598
   (setf (skein512-buffer-length state) 0)
599
   (setf (skein512-cipher state) (make-cipher :threefish512
600
                                              :key (skein-get-iv 512 digest-bits)
601
                                              :mode :ecb))
602
   state)
603
 
604
 (defmethod reinitialize-instance ((state skein512) &rest initargs)
605
   (declare (ignore initargs))
606
   (%reinitialize-skein512 state 512))
607
 
608
 (defmethod reinitialize-instance ((state skein512/128) &rest initargs)
609
   (declare (ignore initargs))
610
   (%reinitialize-skein512 state 128))
611
 
612
 (defmethod reinitialize-instance ((state skein512/160) &rest initargs)
613
   (declare (ignore initargs))
614
   (%reinitialize-skein512 state 160))
615
 
616
 (defmethod reinitialize-instance ((state skein512/224) &rest initargs)
617
   (declare (ignore initargs))
618
   (%reinitialize-skein512 state 224))
619
 
620
 (defmethod reinitialize-instance ((state skein512/256) &rest initargs)
621
   (declare (ignore initargs))
622
   (%reinitialize-skein512 state 256))
623
 
624
 (defmethod reinitialize-instance ((state skein512/384) &rest initargs)
625
   (declare (ignore initargs))
626
   (%reinitialize-skein512 state 384))
627
 
628
 (defmethod copy-digest ((state skein512) &optional copy)
629
   (declare (type (or null skein512) copy))
630
   (let ((copy (if copy
631
                   copy
632
                   (etypecase state
633
                     (skein512/128 (%make-skein512/128-digest))
634
                     (skein512/160 (%make-skein512/160-digest))
635
                     (skein512/224 (%make-skein512/224-digest))
636
                     (skein512/256 (%make-skein512/256-digest))
637
                     (skein512/384 (%make-skein512/384-digest))
638
                     (skein512 (%make-skein512-digest))))))
639
     (declare (type skein512 copy))
640
     (replace (skein512-value copy) (skein512-value state))
641
     (replace (skein512-tweak copy) (skein512-tweak state))
642
     (replace (skein512-cfg copy) (skein512-cfg state))
643
     (replace (skein512-buffer copy) (skein512-buffer state))
644
     (setf (skein512-buffer-length copy) (skein512-buffer-length state))
645
     (setf (skein512-cipher copy) (skein-copy-cipher (skein512-cipher state)))
646
     copy))
647
 
648
 (define-digest-updater skein512
649
   (skein-ubi state sequence start end))
650
 
651
 (define-digest-finalizer ((skein512 64)
652
                           (skein512/128 16)
653
                           (skein512/160 20)
654
                           (skein512/224 28)
655
                           (skein512/256 32)
656
                           (skein512/384 48))
657
   (skein-finalize state digest digest-start))
658
 
659
 (defdigest skein512 :digest-length 64 :block-length 64)
660
 (defdigest skein512/128 :digest-length 16 :block-length 64)
661
 (defdigest skein512/160 :digest-length 20 :block-length 64)
662
 (defdigest skein512/224 :digest-length 28 :block-length 64)
663
 (defdigest skein512/256 :digest-length 32 :block-length 64)
664
 (defdigest skein512/384 :digest-length 48 :block-length 64)
665
 
666
 ;;; Implementation for blocks of 1024 bits
667
 (defstruct (skein1024
668
              (:constructor %make-skein1024-digest nil)
669
              (:copier nil))
670
   (value (copy-seq (skein-get-iv 1024 1024))
671
          :type (simple-array (unsigned-byte 8) (128)))
672
   (tweak (skein-make-tweak t nil +skein-msg+ 0)
673
          :type (simple-array (unsigned-byte 64) (2)))
674
   (cfg (skein-make-configuration-string 1024)
675
        :type (simple-array (unsigned-byte 8) (32)))
676
   (buffer (make-array 128 :element-type '(unsigned-byte 8))
677
           :type (simple-array (unsigned-byte 8) (128)))
678
   (buffer-length 0 :type integer)
679
   (cipher (make-cipher :threefish1024
680
                        :key (skein-get-iv 1024 1024)
681
                        :mode :ecb)))
682
 
683
 (defstruct (skein1024/384
684
              (:include skein1024)
685
              (:constructor %make-skein1024/384-digest
686
                            (&aux (value (copy-seq (skein-get-iv 1024 384)))
687
                                  (cfg (skein-make-configuration-string 384))
688
                                  (cipher (make-cipher :threefish1024
689
                                                       :key (skein-get-iv 1024 384)
690
                                                       :mode :ecb))))
691
              (:copier nil)))
692
 
693
 (defstruct (skein1024/512
694
              (:include skein1024)
695
              (:constructor %make-skein1024/512-digest
696
                            (&aux (value (copy-seq (skein-get-iv 1024 512)))
697
                                  (cfg (skein-make-configuration-string 512))
698
                                  (cipher (make-cipher :threefish1024
699
                                                       :key (skein-get-iv 1024 512)
700
                                                       :mode :ecb))))
701
              (:copier nil)))
702
 
703
 (defmethod skein-value ((state skein1024))
704
   (skein1024-value state))
705
 
706
 (defmethod skein-tweak ((state skein1024))
707
   (skein1024-tweak state))
708
 
709
 (defmethod skein-cfg ((state skein1024))
710
   (skein1024-cfg state))
711
 
712
 (defmethod skein-buffer ((state skein1024))
713
   (skein1024-buffer state))
714
 
715
 (defmethod skein-buffer-length ((state skein1024))
716
   (skein1024-buffer-length state))
717
 
718
 (defmethod (setf skein-buffer-length) (n (state skein1024))
719
   (setf (skein1024-buffer-length state) n))
720
 
721
 (defmethod skein-cipher ((state skein1024))
722
   (skein1024-cipher state))
723
 
724
 (defmethod (setf skein-cipher) (cipher (state skein1024))
725
   (setf (skein1024-cipher state) cipher))
726
 
727
 (defun %reinitialize-skein1024 (state digest-bits)
728
   (declare (type skein1024 state))
729
   (replace (skein1024-value state) (skein-get-iv 1024 digest-bits))
730
   (replace (skein1024-tweak state) (skein-make-tweak t nil +skein-msg+ 0))
731
   (replace (skein1024-cfg state) (skein-make-configuration-string digest-bits))
732
   (setf (skein1024-buffer-length state) 0)
733
   (setf (skein1024-cipher state) (make-cipher :threefish1024
734
                                               :key (skein-get-iv 1024 digest-bits)
735
                                               :mode :ecb))
736
   state)
737
 
738
 (defmethod reinitialize-instance ((state skein1024) &rest initargs)
739
   (declare (ignore initargs))
740
   (%reinitialize-skein1024 state 1024))
741
 
742
 (defmethod reinitialize-instance ((state skein1024/384) &rest initargs)
743
   (declare (ignore initargs))
744
   (%reinitialize-skein1024 state 384))
745
 
746
 (defmethod reinitialize-instance ((state skein1024/512) &rest initargs)
747
   (declare (ignore initargs))
748
   (%reinitialize-skein1024 state 512))
749
 
750
 (defmethod copy-digest ((state skein1024) &optional copy)
751
   (declare (type (or null skein1024) copy))
752
   (let ((copy (if copy
753
                   copy
754
                   (etypecase state
755
                     (skein1024/384 (%make-skein1024/384-digest))
756
                     (skein1024/512 (%make-skein1024/512-digest))
757
                     (skein1024 (%make-skein1024-digest))))))
758
     (declare (type skein1024 copy))
759
     (replace (skein1024-value copy) (skein1024-value state))
760
     (replace (skein1024-tweak copy) (skein1024-tweak state))
761
     (replace (skein1024-cfg copy) (skein1024-cfg state))
762
     (replace (skein1024-buffer copy) (skein1024-buffer state))
763
     (setf (skein1024-buffer-length copy) (skein1024-buffer-length state))
764
     (setf (skein1024-cipher copy) (skein-copy-cipher (skein1024-cipher state)))
765
     copy))
766
 
767
 (define-digest-updater skein1024
768
   (skein-ubi state sequence start end))
769
 
770
 (define-digest-finalizer ((skein1024 128)
771
                           (skein1024/384 48)
772
                           (skein1024/512 64))
773
   (skein-finalize state digest digest-start))
774
 
775
 (defdigest skein1024 :digest-length 128 :block-length 128)
776
 (defdigest skein1024/384 :digest-length 48 :block-length 128)
777
 (defdigest skein1024/512 :digest-length 64 :block-length 128)