3 * The SEAL pseudo-random function family
5 * (c) 2000 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
34 #include <mLib/bits.h>
43 /*----- Global variables --------------------------------------------------*/
45 const octet seal_keysz
[] = { KSZ_ANY
, SHA_HASHSZ
};
47 /*----- Main code ---------------------------------------------------------*/
51 * Arguments: @uint32 *p@ = output table
52 * @size_t sz@ = size of the output table
53 * @const void *k@ = pointer to key material
54 * @unsigned i@ = integer offset
58 * Use: Initializes a SEAL key table.
61 static void sealgamma(uint32
*p
, size_t sz
, const void *k
, unsigned i
)
63 uint32 buf
[80] = { 0 };
65 uint32 aa
= LOAD32(kk
);
66 uint32 bb
= LOAD32(kk
+ 4);
67 uint32 cc
= LOAD32(kk
+ 8);
68 uint32 dd
= LOAD32(kk
+ 12);
69 uint32 ee
= LOAD32(kk
+ 16);
71 unsigned skip
= i
% 5;
74 /* --- While there's hashing to do, do hashing --- */
77 uint32 a
= aa
, b
= bb
, c
= cc
, d
= dd
, e
= ee
;
80 /* --- Initialize and expand the buffer --- */
84 for (j
= 16; j
< 80; j
++) {
85 uint32 x
= buf
[j
- 3] ^ buf
[j
- 8] ^ buf
[j
- 14] ^ buf
[j
- 16];
89 /* --- Definitions for round functions --- */
91 #define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
92 #define G(x, y, z) ((x) ^ (y) ^ (z))
93 #define H(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
95 #define T(v, w, x, y, z, i, f, k) do { \
97 z = ROL32(v, 5) + f(w, x, y) + z + buf[i] + k; \
99 _x = v; v = z; z = y; y = x; x = w; w = _x; \
102 #define FF(v, w, x, y, z, i) T(v, w, x, y, z, i, F, 0x5a827999)
103 #define GG(v, w, x, y, z, i) T(v, w, x, y, z, i, G, 0x6ed9eba1)
104 #define HH(v, w, x, y, z, i) T(v, w, x, y, z, i, H, 0x8f1bbcdc)
105 #define II(v, w, x, y, z, i) T(v, w, x, y, z, i, G, 0xca62c1d6)
107 /* --- The main compression function --- *
109 * Since this isn't doing bulk hashing, do it the easy way.
112 for (j
= 0; j
< 20; j
++)
113 FF(a
, b
, c
, d
, e
, j
);
114 for (j
= 20; j
< 40; j
++)
115 GG(a
, b
, c
, d
, e
, j
);
116 for (j
= 40; j
< 60; j
++)
117 HH(a
, b
, c
, d
, e
, j
);
118 for (j
= 60; j
< 80; j
++)
119 II(a
, b
, c
, d
, e
, j
);
121 /* --- Do the chaining at the end --- */
123 a
+= aa
; b
+= bb
; c
+= cc
; d
+= dd
; e
+= ee
;
125 /* --- Write to the output buffer --- */
129 if (sz
) { *p
++ = a
; sz
--; }
131 if (sz
) { *p
++ = b
; sz
--; }
133 if (sz
) { *p
++ = c
; sz
--; }
135 if (sz
) { *p
++ = d
; sz
--; }
137 if (sz
) { *p
++ = e
; sz
--; }
143 /* --- @seal_initkey@ --- *
145 * Arguments: @seal_key *k@ = pointer to key block
146 * @const void *buf@ = pointer to key material
147 * @size_t sz@ = size of the key material
151 * Use: Initializes a SEAL key block. The key material may be any
152 * size, but if it's not 20 bytes long it's passed to SHA for
156 void seal_initkey(seal_key
*k
, const void *buf
, size_t sz
)
158 /* --- Hash the key if it's the wrong size --- */
160 if (sz
== SHA_HASHSZ
)
161 memcpy(k
->k
, buf
, sizeof(k
->k
));
165 sha_hash(&c
, buf
, sz
);
169 /* --- Expand the key to fit the various tables --- */
171 sealgamma(k
->t
, 512, k
->k
, 0);
172 sealgamma(k
->s
, 256, k
->k
, 0x1000);
173 sealgamma(k
->r
, SEAL_R
, k
->k
, 0x2000);
176 /* --- @seal_reset@ --- *
178 * Arguments: @seal_ctx *c@ = pointer to a SEAL context
182 * Use: Resets the context so that more data can be extracted from
186 static void seal_reset(seal_ctx
*c
)
193 /* --- Initialize the new chaining variables --- */
195 if (c
->l
>= SEAL_R
) {
196 sealgamma(c
->rbuf
, SEAL_R
, k
->k
, c
->ri
);
203 B
= ROR32(n
, 8) ^ c
->r
[1];
204 C
= ROR32(n
, 16) ^ c
->r
[2];
205 D
= ROR32(n
, 24) ^ c
->r
[3];
209 /* --- Ensure that everything is sufficiently diffused --- */
211 p
= A
& 0x7fc; B
+= k
->t
[p
>> 2]; A
= ROR32(A
, 9);
212 p
= B
& 0x7fc; C
+= k
->t
[p
>> 2]; B
= ROR32(B
, 9);
213 p
= C
& 0x7fc; D
+= k
->t
[p
>> 2]; C
= ROR32(C
, 9);
214 p
= D
& 0x7fc; A
+= k
->t
[p
>> 2]; D
= ROR32(D
, 9);
215 p
= A
& 0x7fc; B
+= k
->t
[p
>> 2]; A
= ROR32(A
, 9);
216 p
= B
& 0x7fc; C
+= k
->t
[p
>> 2]; B
= ROR32(B
, 9);
217 p
= C
& 0x7fc; D
+= k
->t
[p
>> 2]; C
= ROR32(C
, 9);
218 p
= D
& 0x7fc; A
+= k
->t
[p
>> 2]; D
= ROR32(D
, 9);
220 /* --- Write out some context --- */
222 c
->n1
= D
; c
->n2
= B
; c
->n3
= A
; c
->n4
= C
;
224 /* --- Diffuse some more --- */
226 p
= A
& 0x7fc; B
+= k
->t
[p
>> 2]; A
= ROR32(A
, 9);
227 p
= B
& 0x7fc; C
+= k
->t
[p
>> 2]; B
= ROR32(B
, 9);
228 p
= C
& 0x7fc; D
+= k
->t
[p
>> 2]; C
= ROR32(C
, 9);
229 p
= D
& 0x7fc; A
+= k
->t
[p
>> 2]; D
= ROR32(D
, 9);
231 /* --- Write out the magic numbers --- */
233 c
->a
= A
; c
->b
= B
; c
->c
= C
; c
->d
= D
;
237 /* --- @seal_initctx@ --- *
239 * Arguments: @seal_ctx *c@ = pointer to a SEAL context
240 * @seal_key *k@ = pointer to a SEAL key
241 * @uint32 n@ = integer sequence number
245 * Use: Initializes a SEAL context which can be used for random
246 * number generation or whatever.
249 void seal_initctx(seal_ctx
*c
, seal_key
*k
, uint32 n
)
255 c
->ri
= 0x2000 + SEAL_R
;
260 /* --- @seal_encrypt@ --- *
262 * Arguments: @seal_ctx *c@ = pointer to a SEAL context
263 * @const void *src@ = pointer to source data
264 * @void *dest@ = pointer to destination data
265 * @size_t sz@ = size of the data
269 * Use: Encrypts a block of data using SEAL. If @src@ is zero,
270 * @dest@ is filled with SEAL output. If @dest@ is zero, the
271 * SEAL generator is just spun around for a bit. This shouldn't
272 * be necessary, because SEAL isn't RC4.
275 void seal_encrypt(seal_ctx
*c
, const void *src
, void *dest
, size_t sz
)
277 const octet
*s
= src
;
280 /* --- Expect a big dollop of bytes --- */
284 uint32 A
= c
->a
, B
= c
->b
, C
= c
->c
, D
= c
->d
;
285 uint32 n1
= c
->n1
, n2
= c
->n2
, n3
= c
->n3
, n4
= c
->n4
;
286 uint32 aa
, bb
, cc
, dd
;
289 /* --- Empty the queue first --- */
294 octet
*p
= c
->q
+ sizeof(c
->q
) - c
->qsz
;
295 for (i
= 0; i
< c
->qsz
; i
++)
296 *d
++ = (s ?
*s
++ ^ *p
++ : *p
++);
301 /* --- Main sequence --- */
306 /* --- Reset if we've run out of steam on this iteration --- */
310 A
= c
->a
, B
= c
->b
, C
= c
->c
, D
= c
->d
;
311 n1
= c
->n1
, n2
= c
->n2
, n3
= c
->n3
, n4
= c
->n4
;
315 /* --- Make some new numbers --- */
317 P
= A
& 0x7fc; B
+= k
->t
[P
>> 2]; A
= ROR32(A
, 9); B
^= A
;
318 Q
= B
& 0x7fc; C
^= k
->t
[Q
>> 2]; B
= ROR32(B
, 9); C
+= B
;
319 P
= (P
+ C
) & 0x7fc; D
+= k
->t
[P
>> 2]; C
= ROR32(C
, 9); D
^= C
;
320 Q
= (Q
+ D
) & 0x7fc; A
^= k
->t
[Q
>> 2]; D
= ROR32(D
, 9); A
+= D
;
321 P
= (P
+ A
) & 0x7fc; B
^= k
->t
[P
>> 2]; A
= ROR32(A
, 9);
322 Q
= (Q
+ B
) & 0x7fc; C
+= k
->t
[Q
>> 2]; B
= ROR32(B
, 9);
323 P
= (P
+ C
) & 0x7fc; D
^= k
->t
[P
>> 2]; C
= ROR32(C
, 9);
324 Q
= (Q
+ D
) & 0x7fc; A
+= k
->t
[Q
>> 2]; D
= ROR32(D
, 9);
326 /* --- Remember the output and set up the next round --- */
328 aa
= B
+ k
->s
[j
+ 0];
329 bb
= C
^ k
->s
[j
+ 1];
330 cc
= D
+ k
->s
[j
+ 2];
331 dd
= A
^ k
->s
[j
+ 3];
335 A
+= n1
, B
+= n2
, C
^= n1
, D
^= n2
;
337 A
+= n3
, B
+= n4
, C
^= n3
, D
^= n4
;
339 /* --- Bail out here if we need to do buffering --- */
344 /* --- Write the next 16 bytes --- */
348 aa
^= LOAD32_L(s
+ 0);
349 bb
^= LOAD32_L(s
+ 4);
350 cc
^= LOAD32_L(s
+ 8);
351 dd
^= LOAD32_L(s
+ 12);
354 STORE32_L(d
+ 0, aa
);
355 STORE32_L(d
+ 4, bb
);
356 STORE32_L(d
+ 8, cc
);
357 STORE32_L(d
+ 12, dd
);
363 /* --- Write the new queue --- */
365 STORE32_L(c
->q
+ 0, aa
);
366 STORE32_L(c
->q
+ 4, bb
);
367 STORE32_L(c
->q
+ 8, cc
);
368 STORE32_L(c
->q
+ 12, dd
);
371 c
->a
= A
; c
->b
= B
; c
->c
= C
; c
->d
= D
;
375 /* --- Deal with the rest from the queue --- */
379 octet
*p
= c
->q
+ sizeof(c
->q
) - c
->qsz
;
381 for (i
= 0; i
< sz
; i
++)
382 *d
++ = (s ?
*s
++ ^ *p
++ : *p
++);
388 /*----- Generic cipher interface ------------------------------------------*/
390 typedef struct gctx
{
396 static const gcipher_ops gops
;
398 static gcipher
*ginit(const void *k
, size_t sz
)
400 gctx
*g
= S_CREATE(gctx
);
402 seal_initkey(&g
->k
, k
, sz
);
403 seal_initctx(&g
->cc
, &g
->k
, 0);
407 static void gencrypt(gcipher
*c
, const void *s
, void *t
, size_t sz
)
410 seal_encrypt(&g
->cc
, s
, t
, sz
);
413 static void gsetiv(gcipher
*c
, const void *iv
)
416 uint32 n
= *(const uint32
*)iv
;
417 seal_initctx(&g
->cc
, &g
->k
, n
);
420 static void gdestroy(gcipher
*c
)
427 static const gcipher_ops gops
= {
429 gencrypt
, gencrypt
, gdestroy
, gsetiv
, 0
432 const gccipher seal
= {
433 "seal", seal_keysz
, 0,
437 /*----- Generic random number generator interface -------------------------*/
439 typedef struct grctx
{
445 static void grdestroy(grand
*r
)
447 grctx
*g
= (grctx
*)r
;
452 static int grmisc(grand
*r
, unsigned op
, ...)
454 grctx
*g
= (grctx
*)r
;
461 switch (va_arg(ap
, unsigned)) {
464 case GRAND_SEEDUINT32
:
465 case GRAND_SEEDBLOCK
:
475 seal_initctx(&g
->cc
, &g
->k
, va_arg(ap
, int));
477 case GRAND_SEEDUINT32
:
478 seal_initctx(&g
->cc
, &g
->k
, va_arg(ap
, uint32
));
480 case GRAND_SEEDBLOCK
: {
481 const void *p
= va_arg(ap
, const void *);
482 size_t sz
= va_arg(ap
, size_t);
487 octet buf
[4] = { 0 };
491 seal_initctx(&g
->cc
, &g
->k
, n
);
493 case GRAND_SEEDRAND
: {
494 grand
*rr
= va_arg(ap
, grand
*);
495 seal_initctx(&g
->cc
, &g
->k
, rr
->ops
->word(rr
));
506 static octet
grbyte(grand
*r
)
508 grctx
*g
= (grctx
*)r
;
510 seal_encrypt(&g
->cc
, 0, &o
, 1);
514 static uint32
grword(grand
*r
)
516 grctx
*g
= (grctx
*)r
;
518 seal_encrypt(&g
->cc
, 0, b
, 4);
522 static void grfill(grand
*r
, void *p
, size_t sz
)
524 grctx
*g
= (grctx
*)r
;
525 seal_encrypt(&g
->cc
, 0, p
, sz
);
528 static const grand_ops grops
= {
532 grword
, grbyte
, grword
, grand_range
, grfill
535 /* --- @seal_rand@ --- *
537 * Arguments: @const void *k@ = pointer to key material
538 * @size_t sz@ = size of key material
539 * @uint32 n@ = sequence number
541 * Returns: Pointer to generic random number generator interface.
543 * Use: Creates a random number interface wrapper around a SEAL
544 * pseudorandom function.
547 grand
*seal_rand(const void *k
, size_t sz
, uint32 n
)
549 grctx
*g
= S_CREATE(grctx
);
551 seal_initkey(&g
->k
, k
, sz
);
552 seal_initctx(&g
->cc
, &g
->k
, n
);
556 /*----- Test rig ----------------------------------------------------------*/
562 #include <mLib/testrig.h>
564 static int verify(dstr
*v
)
568 uint32 n
= *(uint32
*)v
[1].buf
;
574 DENSURE(&d
, v
[2].len
);
575 DENSURE(&z
, v
[2].len
);
576 memset(z
.buf
, 0, v
[2].len
);
577 z
.len
= d
.len
= v
[2].len
;
578 seal_initkey(&k
, v
[0].buf
, v
[0].len
);
580 for (i
= 0; i
< v
[2].len
; i
++) {
581 seal_initctx(&c
, &k
, n
);
582 seal_encrypt(&c
, 0, d
.buf
, i
);
583 seal_encrypt(&c
, z
.buf
, d
.buf
+ i
, d
.len
- i
);
584 if (memcmp(d
.buf
, v
[2].buf
, d
.len
) != 0) {
586 printf("*** seal failure\n");
587 printf("*** k = "); type_hex
.dump(&v
[0], stdout
); putchar('\n');
588 printf("*** n = %08lx\n", (unsigned long)n
);
589 printf("*** i = %i\n", i
);
590 printf("*** expected = "); type_hex
.dump(&v
[2], stdout
); putchar('\n');
591 printf("*** computed = "); type_hex
.dump(&d
, stdout
); putchar('\n');
601 static test_chunk defs
[] = {
602 { "seal", verify
, { &type_hex
, &type_uint32
, &type_hex
, 0 } },
606 int main(int argc
, char *argv
[])
608 test_run(argc
, argv
, defs
, SRCDIR
"/t/seal");
614 /*----- That's all, folks -------------------------------------------------*/