8dd8c294 |
1 | /* -*-c-*- |
8dd8c294 |
2 | * |
3 | * The SEAL pseudo-random function family |
4 | * |
5 | * (c) 2000 Straylight/Edgeware |
6 | */ |
7 | |
45c0fd36 |
8 | /*----- Licensing notice --------------------------------------------------* |
8dd8c294 |
9 | * |
10 | * This file is part of Catacomb. |
11 | * |
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. |
45c0fd36 |
16 | * |
8dd8c294 |
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. |
45c0fd36 |
21 | * |
8dd8c294 |
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, |
25 | * MA 02111-1307, USA. |
26 | */ |
27 | |
8dd8c294 |
28 | /*----- Notes on the SEAL pseudo-random function family -------------------* |
29 | * |
30 | * SEAL is a slightly odd cryptographic primitive. It was designed by Phil |
31 | * Rogaway and Don Coppersmith at IBM, basically as an exercise in producing |
32 | * a really fast symmetric cipher of some kind. They succeeded: SEAL is |
33 | * faster than the much simpler RC4. |
34 | * |
35 | * For each key, it gives you %$2^{32}$% different output streams. This |
36 | * implementation imposes no length limits on the size of output streams and |
37 | * performs careful buffer handling to allow arbitrary amounts of data to be |
38 | * extracted. In practice, extracting more than about 64K is possibly dodgy |
39 | * from a security point of view. |
40 | * |
41 | * SEAL is patented. |
42 | */ |
43 | |
44 | #ifndef CATACOMB_SEAL_H |
45 | #define CATACOMB_SEAL_H |
46 | |
47 | #ifdef __cplusplus |
48 | extern "C" { |
49 | #endif |
50 | |
51 | /*----- Header files ------------------------------------------------------*/ |
52 | |
53 | #ifndef CATACOMB_GCIPHER_H |
54 | # include "gcipher.h" |
55 | #endif |
56 | |
57 | #ifndef CATACOMB_GRAND_H |
58 | # include "grand.h" |
59 | #endif |
60 | |
61 | /*----- Data structures ---------------------------------------------------*/ |
62 | |
63 | #define SEAL_R 256 |
64 | |
65 | typedef struct seal_key { |
66 | octet k[20]; /* Copy of the 160-bit key */ |
67 | uint32 t[512]; /* Substitution table */ |
68 | uint32 s[256]; /* Magic for each iteration */ |
69 | uint32 r[SEAL_R]; /* Magic for the first 64K */ |
70 | } seal_key; |
71 | |
72 | typedef struct seal_ctx { |
73 | seal_key *k; /* Pointer to the key block */ |
74 | uint32 *r, ri; /* Pointer to current magic */ |
75 | uint32 n, l; /* Various indices into things */ |
76 | uint32 a, b, c, d; /* Current chaining variables */ |
77 | uint32 n1, n2, n3, n4; /* Increments for the variables */ |
78 | unsigned i; /* Index into current iteration */ |
79 | octet q[16]; /* Output buffer */ |
80 | unsigned qsz; /* Number of bytes in the buffer */ |
81 | uint32 rbuf[SEAL_R]; /* Buffer for later magic */ |
82 | } seal_ctx; |
83 | |
84 | /*----- Functions provided ------------------------------------------------*/ |
85 | |
86 | /* --- @seal_initkey@ --- * |
87 | * |
88 | * Arguments: @seal_key *k@ = pointer to key block |
89 | * @const void *buf@ = pointer to key material |
90 | * @size_t sz@ = size of the key material |
91 | * |
92 | * Returns: --- |
93 | * |
94 | * Use: Initializes a SEAL key block. The key material may be any |
95 | * size, but if it's not 20 bytes long it's passed to SHA for |
96 | * hashing first. |
97 | */ |
98 | |
99 | extern void seal_initkey(seal_key */*k*/, |
100 | const void */*buf*/, size_t /*sz*/); |
101 | |
102 | /* --- @seal_initctx@ --- * |
103 | * |
104 | * Arguments: @seal_ctx *c@ = pointer to a SEAL context |
105 | * @seal_key *k@ = pointer to a SEAL key |
106 | * @uint32 n@ = integer sequence number |
107 | * |
108 | * Returns: --- |
109 | * |
110 | * Use: Initializes a SEAL context which can be used for random |
111 | * number generation or whatever. |
112 | */ |
113 | |
114 | extern void seal_initctx(seal_ctx */*c*/, seal_key */*k*/, uint32 /*n*/); |
115 | |
116 | /* --- @seal_encrypt@ --- * |
117 | * |
118 | * Arguments: @seal_ctx *c@ = pointer to a SEAL context |
119 | * @const void *src@ = pointer to source data |
120 | * @void *dest@ = pointer to destination data |
121 | * @size_t sz@ = size of the data |
122 | * |
123 | * Returns: --- |
124 | * |
125 | * Use: Encrypts a block of data using SEAL. If @src@ is zero, |
126 | * @dest@ is filled with SEAL output. If @dest@ is zero, the |
127 | * SEAL generator is just spun around for a bit. This shouldn't |
128 | * be necessary, because SEAL isn't RC4. |
129 | */ |
130 | |
131 | extern void seal_encrypt(seal_ctx */*c*/, const void */*src*/, |
132 | void */*dest*/, size_t /*sz*/); |
133 | |
134 | /*----- Generic cipher interface ------------------------------------------*/ |
135 | |
136 | #define SEAL_KEYSZ 20 |
137 | extern const octet seal_keysz[]; |
138 | |
139 | extern const gccipher seal; |
140 | |
141 | /*----- Generic random number generator interface -------------------------*/ |
142 | |
143 | /* --- @seal_rand@ --- * |
144 | * |
145 | * Arguments: @const void *k@ = pointer to key material |
146 | * @size_t sz@ = size of key material |
147 | * @uint32 n@ = sequence number |
148 | * |
149 | * Returns: Pointer to generic random number generator interface. |
150 | * |
151 | * Use: Creates a random number interface wrapper around a SEAL |
152 | * pseudorandom function. |
153 | */ |
154 | |
155 | extern grand *seal_rand(const void */*k*/, size_t /*sz*/, uint32 /*n*/); |
156 | |
157 | /*----- That's all, folks -------------------------------------------------*/ |
158 | |
159 | #ifdef __cplusplus |
160 | } |
161 | #endif |
162 | |
163 | #endif |