Commit ba77ac1e2d88b72126fa0495b28fecd821ca118c
Standardized on stdint's uint_* types; cleanup.
Removed a fair bit of #if 0'd debug code which we don't need.Coda Hale committed on 9/16/2009, 7:39:54 PM
Parent: 525b5999142d3d8c731fd7524eb07780dcd03720
Files changed
ext/mri/bcrypt.c | changed |
ext/mri/bcrypt.h | changed |
ext/mri/bcrypt_ext.c | changed |
ext/mri/blf.h | changed |
ext/mri/blowfish.c | changed |
ext/mri/extconf.rb | changed |
ext/mri/bcrypt.c | ||
---|---|---|
@@ -1,7 +1,11 @@ | ||
1 | 1 | /* $OpenBSD: bcrypt.c,v 1.22 2007/02/20 01:44:16 ray Exp $ */ |
2 | 2 | |
3 | -/* | |
3 | +/* | |
4 | + * Modified by <coda.hale@gmail.com> on 2009-09-16: | |
5 | + * | |
6 | + * - Standardized on stdint.h's numerical types and removed some debug cruft. | |
7 | + * | |
4 | 8 | * Modified by <hongli@phusion.nl> on 2009-08-05: |
5 | 9 | * |
6 | 10 | * - Got rid of the global variables; they're not thread-safe. |
7 | 11 | * Modified the functions to accept local buffers instead. |
@@ -57,15 +61,10 @@ | ||
57 | 61 | * 6. RETURN Concatenate (salt, ctext); |
58 | 62 | * |
59 | 63 | */ |
60 | 64 | |
61 | - | |
62 | 65 | |
63 | - | |
64 | - | |
65 | - | |
66 | 66 | |
67 | - | |
68 | 67 | |
69 | 68 | |
70 | 69 | |
71 | 70 | |
@@ -73,16 +72,16 @@ | ||
73 | 72 | * You can have up to 2^31 rounds which should be enough for some |
74 | 73 | * time to come. |
75 | 74 | */ |
76 | 75 | |
77 | -static void encode_salt(char *, u_int8_t *, u_int16_t, u_int8_t); | |
78 | -static void encode_base64(u_int8_t *, u_int8_t *, u_int16_t); | |
79 | -static void decode_base64(u_int8_t *, u_int16_t, u_int8_t *); | |
76 | +static void encode_salt(char *, uint8_t *, uint16_t, uint8_t); | |
77 | +static void encode_base64(uint8_t *, uint8_t *, uint16_t); | |
78 | +static void decode_base64(uint8_t *, uint16_t, uint8_t *); | |
80 | 79 | |
81 | -const static u_int8_t Base64Code[] = | |
80 | +const static uint8_t Base64Code[] = | |
82 | 81 | "./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; |
83 | 82 | |
84 | -const static u_int8_t index_64[128] = { | |
83 | +const static uint8_t index_64[128] = { | |
85 | 84 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
86 | 85 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
87 | 86 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
88 | 87 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
@@ -98,13 +97,13 @@ | ||
98 | 97 | }; |
99 | 98 | |
100 | 99 | |
101 | 100 | static void |
102 | -decode_base64(u_int8_t *buffer, u_int16_t len, u_int8_t *data) | |
101 | +decode_base64(uint8_t *buffer, uint16_t len, uint8_t *data) | |
103 | 102 | { |
104 | - u_int8_t *bp = buffer; | |
105 | - u_int8_t *p = data; | |
106 | - u_int8_t c1, c2, c3, c4; | |
103 | + uint8_t *bp = buffer; | |
104 | + uint8_t *p = data; | |
105 | + uint8_t c1, c2, c3, c4; | |
107 | 106 | while (bp < buffer + len) { |
108 | 107 | c1 = CHAR64(*p); |
109 | 108 | c2 = CHAR64(*(p + 1)); |
110 | 109 | |
@@ -133,26 +132,26 @@ | ||
133 | 132 | } |
134 | 133 | } |
135 | 134 | |
136 | 135 | static void |
137 | -encode_salt(char *salt, u_int8_t *csalt, u_int16_t clen, u_int8_t logr) | |
136 | +encode_salt(char *salt, uint8_t *csalt, uint16_t clen, uint8_t logr) | |
138 | 137 | { |
139 | 138 | salt[0] = '$'; |
140 | 139 | salt[1] = BCRYPT_VERSION; |
141 | 140 | salt[2] = 'a'; |
142 | 141 | salt[3] = '$'; |
143 | 142 | |
144 | 143 | snprintf(salt + 4, 4, "%2.2u$", logr); |
145 | 144 | |
146 | - encode_base64((u_int8_t *) salt + 7, csalt, clen); | |
145 | + encode_base64((uint8_t *) salt + 7, csalt, clen); | |
147 | 146 | } |
148 | 147 | /* Generates a salt for this version of crypt. |
149 | 148 | Since versions may change. Keeping this here |
150 | 149 | seems sensible. |
151 | 150 | */ |
152 | 151 | |
153 | 152 | char * |
154 | -bcrypt_gensalt(char *output, u_int8_t log_rounds, u_int8_t *rseed) | |
153 | +bcrypt_gensalt(char *output, uint8_t log_rounds, uint8_t *rseed) | |
155 | 154 | { |
156 | 155 | if (log_rounds < 4) |
157 | 156 | log_rounds = 4; |
158 | 157 | else if (log_rounds > 31) |
@@ -167,14 +166,14 @@ | ||
167 | 166 | char * |
168 | 167 | bcrypt(char *output, const char *key, const char *salt) |
169 | 168 | { |
170 | 169 | blf_ctx state; |
171 | - u_int32_t rounds, i, k; | |
172 | - u_int16_t j; | |
173 | - u_int8_t key_len, salt_len, logr, minor; | |
174 | - u_int8_t ciphertext[4 * BCRYPT_BLOCKS] = "OrpheanBeholderScryDoubt"; | |
175 | - u_int8_t csalt[BCRYPT_MAXSALT]; | |
176 | - u_int32_t cdata[BCRYPT_BLOCKS]; | |
170 | + uint32_t rounds, i, k; | |
171 | + uint16_t j; | |
172 | + uint8_t key_len, salt_len, logr, minor; | |
173 | + uint8_t ciphertext[4 * BCRYPT_BLOCKS] = "OrpheanBeholderScryDoubt"; | |
174 | + uint8_t csalt[BCRYPT_MAXSALT]; | |
175 | + uint32_t cdata[BCRYPT_BLOCKS]; | |
177 | 176 | int n; |
178 | 177 | |
179 | 178 | /* Discard "$" identifier */ |
180 | 179 | salt++; |
@@ -207,10 +206,10 @@ | ||
207 | 206 | /* Computer power doesn't increase linear, 2^x should be fine */ |
208 | 207 | n = atoi(salt); |
209 | 208 | if (n > 31 || n < 0) |
210 | 209 | return NULL; |
211 | - logr = (u_int8_t)n; | |
212 | - if ((rounds = (u_int32_t) 1 << logr) < BCRYPT_MINROUNDS) | |
210 | + logr = (uint8_t)n; | |
211 | + if ((rounds = (uint32_t) 1 << logr) < BCRYPT_MINROUNDS) | |
213 | 212 | return NULL; |
214 | 213 | |
215 | 214 | /* Discard num rounds + "$" identifier */ |
216 | 215 | salt += 3; |
@@ -218,18 +217,18 @@ | ||
218 | 217 | if (strlen(salt) * 3 / 4 < BCRYPT_MAXSALT) |
219 | 218 | return NULL; |
220 | 219 | |
221 | 220 | /* We dont want the base64 salt but the raw data */ |
222 | - decode_base64(csalt, BCRYPT_MAXSALT, (u_int8_t *) salt); | |
221 | + decode_base64(csalt, BCRYPT_MAXSALT, (uint8_t *) salt); | |
223 | 222 | salt_len = BCRYPT_MAXSALT; |
224 | 223 | key_len = strlen(key) + (minor >= 'a' ? 1 : 0); |
225 | 224 | |
226 | 225 | /* Setting up S-Boxes and Subkeys */ |
227 | 226 | Blowfish_initstate(&state); |
228 | 227 | Blowfish_expandstate(&state, csalt, salt_len, |
229 | - (u_int8_t *) key, key_len); | |
228 | + (uint8_t *) key, key_len); | |
230 | 229 | for (k = 0; k < rounds; k++) { |
231 | - Blowfish_expand0state(&state, (u_int8_t *) key, key_len); | |
230 | + Blowfish_expand0state(&state, (uint8_t *) key, key_len); | |
232 | 231 | Blowfish_expand0state(&state, csalt, salt_len); |
233 | 232 | } |
234 | 233 | |
235 | 234 | /* This can be precomputed later */ |
@@ -260,20 +259,20 @@ | ||
260 | 259 | output[i++] = '$'; |
261 | 260 | |
262 | 261 | snprintf(output + i, 4, "%2.2u$", logr); |
263 | 262 | |
264 | - encode_base64((u_int8_t *) output + i + 3, csalt, BCRYPT_MAXSALT); | |
265 | - encode_base64((u_int8_t *) output + strlen(output), ciphertext, | |
263 | + encode_base64((uint8_t *) output + i + 3, csalt, BCRYPT_MAXSALT); | |
264 | + encode_base64((uint8_t *) output + strlen(output), ciphertext, | |
266 | 265 | 4 * BCRYPT_BLOCKS - 1); |
267 | 266 | return output; |
268 | 267 | } |
269 | 268 | |
270 | 269 | static void |
271 | -encode_base64(u_int8_t *buffer, u_int8_t *data, u_int16_t len) | |
270 | +encode_base64(uint8_t *buffer, uint8_t *data, uint16_t len) | |
272 | 271 | { |
273 | - u_int8_t *bp = buffer; | |
274 | - u_int8_t *p = data; | |
275 | - u_int8_t c1, c2; | |
272 | + uint8_t *bp = buffer; | |
273 | + uint8_t *p = data; | |
274 | + uint8_t c1, c2; | |
276 | 275 | while (p < data + len) { |
277 | 276 | c1 = *p++; |
278 | 277 | *bp++ = Base64Code[(c1 >> 2)]; |
279 | 278 | c1 = (c1 & 0x03) << 4; |
@@ -295,34 +294,4 @@ | ||
295 | 294 | *bp++ = Base64Code[c2 & 0x3f]; |
296 | 295 | } |
297 | 296 | *bp = '\0'; |
298 | 297 | } |
299 | - | |
300 | -void | |
301 | -main() | |
302 | -{ | |
303 | - char blubber[73]; | |
304 | - char salt[100]; | |
305 | - char *p; | |
306 | - salt[0] = '$'; | |
307 | - salt[1] = BCRYPT_VERSION; | |
308 | - salt[2] = '$'; | |
309 | - | |
310 | - snprintf(salt + 3, 4, "%2.2u$", 5); | |
311 | - | |
312 | - printf("24 bytes of salt: "); | |
313 | - fgets(salt + 6, sizeof(salt) - 6, stdin); | |
314 | - salt[99] = 0; | |
315 | - printf("72 bytes of password: "); | |
316 | - fpurge(stdin); | |
317 | - fgets(blubber, sizeof(blubber), stdin); | |
318 | - blubber[72] = 0; | |
319 | - | |
320 | - p = crypt(blubber, salt); | |
321 | - printf("Passwd entry: %s\n\n", p); | |
322 | - | |
323 | - p = bcrypt_gensalt(5); | |
324 | - printf("Generated salt: %s\n", p); | |
325 | - p = crypt(blubber, p); | |
326 | - printf("Passwd entry: %s\n", p); | |
327 | -} | |
328 | - |
ext/mri/bcrypt.h | ||
---|---|---|
@@ -30,8 +30,10 @@ | ||
30 | 30 | |
31 | 31 | |
32 | 32 | |
33 | 33 | |
34 | + | |
35 | + | |
34 | 36 | |
35 | 37 | |
36 | 38 | |
37 | 39 | |
@@ -48,9 +50,9 @@ | ||
48 | 50 | * rseed: a seed of BCRYPT_MAXSALT bytes. Should be obtained from a |
49 | 51 | * cryptographically secure random source. |
50 | 52 | * Returns: output |
51 | 53 | */ |
52 | -char *bcrypt_gensalt(char *output, u_int8_t log_rounds, u_int8_t *rseed); | |
54 | +char *bcrypt_gensalt(char *output, uint8_t log_rounds, uint8_t *rseed); | |
53 | 55 | |
54 | 56 | /* |
55 | 57 | * Given a secret and a salt, generates a salted hash (which you can then store safely). |
56 | 58 | * |
ext/mri/bcrypt_ext.c | ||
---|---|---|
@@ -40,9 +40,9 @@ | ||
40 | 40 | static VALUE bc_salt(VALUE self, VALUE cost, VALUE seed) { |
41 | 41 | int icost = NUM2INT(cost); |
42 | 42 | char salt[BCRYPT_SALT_OUTPUT_SIZE]; |
43 | 43 | |
44 | - bcrypt_gensalt(salt, icost, (u_int8_t *)RSTRING_PTR(seed)); | |
44 | + bcrypt_gensalt(salt, icost, (uint8_t *)RSTRING_PTR(seed)); | |
45 | 45 | return rb_str_new2(salt); |
46 | 46 | } |
47 | 47 | |
48 | 48 | /* Given a secret and a salt, generates a salted hash (which you can then store safely). |
ext/mri/blf.h | ||
---|---|---|
@@ -30,20 +30,13 @@ | ||
30 | 30 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
31 | 31 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
32 | 32 | */ |
33 | 33 | |
34 | -/* Add this type so we'll compile nicely on Solaris. | |
35 | - Thanks to Jeremy LaTrasse and the Twitter crew. */ | |
36 | - | |
37 | - | |
38 | - | |
39 | - | |
40 | - | |
41 | - | |
42 | - | |
43 | 34 | |
44 | 35 | |
45 | 36 | |
37 | + | |
38 | + | |
46 | 39 | // Imported from pwd.h. <coda.hale@gmail.com> |
47 | 40 | |
48 | 41 | |
49 | 42 | /* Schneier specifies a maximum key length of 56 bytes. |
@@ -57,37 +50,37 @@ | ||
57 | 50 | |
58 | 51 | |
59 | 52 | /* Blowfish context */ |
60 | 53 | typedef struct BlowfishContext { |
61 | - u_int32_t S[4][256]; /* S-Boxes */ | |
62 | - u_int32_t P[BLF_N + 2]; /* Subkeys */ | |
54 | + uint32_t S[4][256]; /* S-Boxes */ | |
55 | + uint32_t P[BLF_N + 2]; /* Subkeys */ | |
63 | 56 | } blf_ctx; |
64 | 57 | |
65 | 58 | /* Raw access to customized Blowfish |
66 | 59 | * blf_key is just: |
67 | 60 | * Blowfish_initstate( state ) |
68 | 61 | * Blowfish_expand0state( state, key, keylen ) |
69 | 62 | */ |
70 | 63 | |
71 | -void Blowfish_encipher(blf_ctx *, u_int32_t *, u_int32_t *); | |
72 | -void Blowfish_decipher(blf_ctx *, u_int32_t *, u_int32_t *); | |
64 | +void Blowfish_encipher(blf_ctx *, uint32_t *, uint32_t *); | |
65 | +void Blowfish_decipher(blf_ctx *, uint32_t *, uint32_t *); | |
73 | 66 | void Blowfish_initstate(blf_ctx *); |
74 | -void Blowfish_expand0state(blf_ctx *, const u_int8_t *, u_int16_t); | |
67 | +void Blowfish_expand0state(blf_ctx *, const uint8_t *, uint16_t); | |
75 | 68 | void Blowfish_expandstate |
76 | -(blf_ctx *, const u_int8_t *, u_int16_t, const u_int8_t *, u_int16_t); | |
69 | +(blf_ctx *, const uint8_t *, uint16_t, const uint8_t *, uint16_t); | |
77 | 70 | |
78 | 71 | /* Standard Blowfish */ |
79 | 72 | |
80 | -void blf_key(blf_ctx *, const u_int8_t *, u_int16_t); | |
81 | -void blf_enc(blf_ctx *, u_int32_t *, u_int16_t); | |
82 | -void blf_dec(blf_ctx *, u_int32_t *, u_int16_t); | |
73 | +void blf_key(blf_ctx *, const uint8_t *, uint16_t); | |
74 | +void blf_enc(blf_ctx *, uint32_t *, uint16_t); | |
75 | +void blf_dec(blf_ctx *, uint32_t *, uint16_t); | |
83 | 76 | |
84 | -void blf_ecb_encrypt(blf_ctx *, u_int8_t *, u_int32_t); | |
85 | -void blf_ecb_decrypt(blf_ctx *, u_int8_t *, u_int32_t); | |
77 | +void blf_ecb_encrypt(blf_ctx *, uint8_t *, uint32_t); | |
78 | +void blf_ecb_decrypt(blf_ctx *, uint8_t *, uint32_t); | |
86 | 79 | |
87 | -void blf_cbc_encrypt(blf_ctx *, u_int8_t *, u_int8_t *, u_int32_t); | |
88 | -void blf_cbc_decrypt(blf_ctx *, u_int8_t *, u_int8_t *, u_int32_t); | |
80 | +void blf_cbc_encrypt(blf_ctx *, uint8_t *, uint8_t *, uint32_t); | |
81 | +void blf_cbc_decrypt(blf_ctx *, uint8_t *, uint8_t *, uint32_t); | |
89 | 82 | |
90 | -/* Converts u_int8_t to u_int32_t */ | |
91 | -u_int32_t Blowfish_stream2word(const u_int8_t *, u_int16_t , u_int16_t *); | |
83 | +/* Converts uint8_t to uint32_t */ | |
84 | +uint32_t Blowfish_stream2word(const uint8_t *, uint16_t , uint16_t *); | |
92 | 85 | |
93 | 86 |
ext/mri/blowfish.c | ||
---|---|---|
@@ -38,14 +38,8 @@ | ||
38 | 38 | * Blowfish is an unpatented fast block cipher designed by |
39 | 39 | * Bruce Schneier. |
40 | 40 | */ |
41 | 41 | |
42 | - | |
43 | - | |
44 | - | |
45 | - | |
46 | - | |
47 | - | |
48 | 42 | |
49 | 43 | |
50 | 44 | |
51 | 45 | |
@@ -63,14 +57,14 @@ | ||
63 | 57 | |
64 | 58 | |
65 | 59 | |
66 | 60 | void |
67 | -Blowfish_encipher(blf_ctx *c, u_int32_t *xl, u_int32_t *xr) | |
61 | +Blowfish_encipher(blf_ctx *c, uint32_t *xl, uint32_t *xr) | |
68 | 62 | { |
69 | - u_int32_t Xl; | |
70 | - u_int32_t Xr; | |
71 | - u_int32_t *s = c->S[0]; | |
72 | - u_int32_t *p = c->P; | |
63 | + uint32_t Xl; | |
64 | + uint32_t Xr; | |
65 | + uint32_t *s = c->S[0]; | |
66 | + uint32_t *p = c->P; | |
73 | 67 | |
74 | 68 | Xl = *xl; |
75 | 69 | Xr = *xr; |
76 | 70 | |
@@ -88,14 +82,14 @@ | ||
88 | 82 | *xr = Xl; |
89 | 83 | } |
90 | 84 | |
91 | 85 | void |
92 | -Blowfish_decipher(blf_ctx *c, u_int32_t *xl, u_int32_t *xr) | |
86 | +Blowfish_decipher(blf_ctx *c, uint32_t *xl, uint32_t *xr) | |
93 | 87 | { |
94 | - u_int32_t Xl; | |
95 | - u_int32_t Xr; | |
96 | - u_int32_t *s = c->S[0]; | |
97 | - u_int32_t *p = c->P; | |
88 | + uint32_t Xl; | |
89 | + uint32_t Xr; | |
90 | + uint32_t *s = c->S[0]; | |
91 | + uint32_t *p = c->P; | |
98 | 92 | |
99 | 93 | Xl = *xl; |
100 | 94 | Xr = *xr; |
101 | 95 | |
@@ -391,15 +385,15 @@ | ||
391 | 385 | |
392 | 386 | *c = initstate; |
393 | 387 | } |
394 | 388 | |
395 | -u_int32_t | |
396 | -Blowfish_stream2word(const u_int8_t *data, u_int16_t databytes, | |
397 | - u_int16_t *current) | |
389 | +uint32_t | |
390 | +Blowfish_stream2word(const uint8_t *data, uint16_t databytes, | |
391 | + uint16_t *current) | |
398 | 392 | { |
399 | - u_int8_t i; | |
400 | - u_int16_t j; | |
401 | - u_int32_t temp; | |
393 | + uint8_t i; | |
394 | + uint16_t j; | |
395 | + uint32_t temp; | |
402 | 396 | |
403 | 397 | temp = 0x00000000; |
404 | 398 | j = *current; |
405 | 399 | |
@@ -413,16 +407,16 @@ | ||
413 | 407 | return temp; |
414 | 408 | } |
415 | 409 | |
416 | 410 | void |
417 | -Blowfish_expand0state(blf_ctx *c, const u_int8_t *key, u_int16_t keybytes) | |
411 | +Blowfish_expand0state(blf_ctx *c, const uint8_t *key, uint16_t keybytes) | |
418 | 412 | { |
419 | - u_int16_t i; | |
420 | - u_int16_t j; | |
421 | - u_int16_t k; | |
422 | - u_int32_t temp; | |
423 | - u_int32_t datal; | |
424 | - u_int32_t datar; | |
413 | + uint16_t i; | |
414 | + uint16_t j; | |
415 | + uint16_t k; | |
416 | + uint32_t temp; | |
417 | + uint32_t datal; | |
418 | + uint32_t datar; | |
425 | 419 | |
426 | 420 | j = 0; |
427 | 421 | for (i = 0; i < BLF_N + 2; i++) { |
428 | 422 | /* Extract 4 int8 to 1 int32 from keystream */ |
@@ -451,17 +445,17 @@ | ||
451 | 445 | } |
452 | 446 | |
453 | 447 | |
454 | 448 | void |
455 | -Blowfish_expandstate(blf_ctx *c, const u_int8_t *data, u_int16_t databytes, | |
456 | - const u_int8_t *key, u_int16_t keybytes) | |
449 | +Blowfish_expandstate(blf_ctx *c, const uint8_t *data, uint16_t databytes, | |
450 | + const uint8_t *key, uint16_t keybytes) | |
457 | 451 | { |
458 | - u_int16_t i; | |
459 | - u_int16_t j; | |
460 | - u_int16_t k; | |
461 | - u_int32_t temp; | |
462 | - u_int32_t datal; | |
463 | - u_int32_t datar; | |
452 | + uint16_t i; | |
453 | + uint16_t j; | |
454 | + uint16_t k; | |
455 | + uint32_t temp; | |
456 | + uint32_t datal; | |
457 | + uint32_t datar; | |
464 | 458 | |
465 | 459 | j = 0; |
466 | 460 | for (i = 0; i < BLF_N + 2; i++) { |
467 | 461 | /* Extract 4 int8 to 1 int32 from keystream */ |
@@ -494,9 +488,9 @@ | ||
494 | 488 | |
495 | 489 | } |
496 | 490 | |
497 | 491 | void |
498 | -blf_key(blf_ctx *c, const u_int8_t *k, u_int16_t len) | |
492 | +blf_key(blf_ctx *c, const uint8_t *k, uint16_t len) | |
499 | 493 | { |
500 | 494 | /* Initialize S-boxes and subkeys with Pi */ |
501 | 495 | Blowfish_initstate(c); |
502 | 496 | |
@@ -504,12 +498,12 @@ | ||
504 | 498 | Blowfish_expand0state(c, k, len); |
505 | 499 | } |
506 | 500 | |
507 | 501 | void |
508 | -blf_enc(blf_ctx *c, u_int32_t *data, u_int16_t blocks) | |
502 | +blf_enc(blf_ctx *c, uint32_t *data, uint16_t blocks) | |
509 | 503 | { |
510 | - u_int32_t *d; | |
511 | - u_int16_t i; | |
504 | + uint32_t *d; | |
505 | + uint16_t i; | |
512 | 506 | |
513 | 507 | d = data; |
514 | 508 | for (i = 0; i < blocks; i++) { |
515 | 509 | Blowfish_encipher(c, d, d + 1); |
@@ -517,12 +511,12 @@ | ||
517 | 511 | } |
518 | 512 | } |
519 | 513 | |
520 | 514 | void |
521 | -blf_dec(blf_ctx *c, u_int32_t *data, u_int16_t blocks) | |
515 | +blf_dec(blf_ctx *c, uint32_t *data, uint16_t blocks) | |
522 | 516 | { |
523 | - u_int32_t *d; | |
524 | - u_int16_t i; | |
517 | + uint32_t *d; | |
518 | + uint16_t i; | |
525 | 519 | |
526 | 520 | d = data; |
527 | 521 | for (i = 0; i < blocks; i++) { |
528 | 522 | Blowfish_decipher(c, d, d + 1); |
@@ -530,12 +524,12 @@ | ||
530 | 524 | } |
531 | 525 | } |
532 | 526 | |
533 | 527 | void |
534 | -blf_ecb_encrypt(blf_ctx *c, u_int8_t *data, u_int32_t len) | |
528 | +blf_ecb_encrypt(blf_ctx *c, uint8_t *data, uint32_t len) | |
535 | 529 | { |
536 | - u_int32_t l, r; | |
537 | - u_int32_t i; | |
530 | + uint32_t l, r; | |
531 | + uint32_t i; | |
538 | 532 | |
539 | 533 | for (i = 0; i < len; i += 8) { |
540 | 534 | l = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3]; |
541 | 535 | r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7]; |
@@ -552,12 +546,12 @@ | ||
552 | 546 | } |
553 | 547 | } |
554 | 548 | |
555 | 549 | void |
556 | -blf_ecb_decrypt(blf_ctx *c, u_int8_t *data, u_int32_t len) | |
550 | +blf_ecb_decrypt(blf_ctx *c, uint8_t *data, uint32_t len) | |
557 | 551 | { |
558 | - u_int32_t l, r; | |
559 | - u_int32_t i; | |
552 | + uint32_t l, r; | |
553 | + uint32_t i; | |
560 | 554 | |
561 | 555 | for (i = 0; i < len; i += 8) { |
562 | 556 | l = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3]; |
563 | 557 | r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7]; |
@@ -574,12 +568,12 @@ | ||
574 | 568 | } |
575 | 569 | } |
576 | 570 | |
577 | 571 | void |
578 | -blf_cbc_encrypt(blf_ctx *c, u_int8_t *iv, u_int8_t *data, u_int32_t len) | |
572 | +blf_cbc_encrypt(blf_ctx *c, uint8_t *iv, uint8_t *data, uint32_t len) | |
579 | 573 | { |
580 | - u_int32_t l, r; | |
581 | - u_int32_t i, j; | |
574 | + uint32_t l, r; | |
575 | + uint32_t i, j; | |
582 | 576 | |
583 | 577 | for (i = 0; i < len; i += 8) { |
584 | 578 | for (j = 0; j < 8; j++) |
585 | 579 | data[j] ^= iv[j]; |
@@ -599,13 +593,13 @@ | ||
599 | 593 | } |
600 | 594 | } |
601 | 595 | |
602 | 596 | void |
603 | -blf_cbc_decrypt(blf_ctx *c, u_int8_t *iva, u_int8_t *data, u_int32_t len) | |
597 | +blf_cbc_decrypt(blf_ctx *c, uint8_t *iva, uint8_t *data, uint32_t len) | |
604 | 598 | { |
605 | - u_int32_t l, r; | |
606 | - u_int8_t *iv; | |
607 | - u_int32_t i, j; | |
599 | + uint32_t l, r; | |
600 | + uint8_t *iv; | |
601 | + uint32_t i, j; | |
608 | 602 | |
609 | 603 | iv = data + len - 16; |
610 | 604 | data = data + len - 8; |
611 | 605 | for (i = len - 8; i >= 8; i -= 8) { |
@@ -638,48 +632,4 @@ | ||
638 | 632 | data[7] = r & 0xff; |
639 | 633 | for (j = 0; j < 8; j++) |
640 | 634 | data[j] ^= iva[j]; |
641 | 635 | } |
642 | - | |
643 | - | |
644 | -void | |
645 | -report(u_int32_t data[], u_int16_t len) | |
646 | -{ | |
647 | - u_int16_t i; | |
648 | - for (i = 0; i < len; i += 2) | |
649 | - printf("Block %0hd: %08lx %08lx.\n", | |
650 | - i / 2, data[i], data[i + 1]); | |
651 | -} | |
652 | -void | |
653 | -main(void) | |
654 | -{ | |
655 | - | |
656 | - blf_ctx c; | |
657 | - char key[] = "AAAAA"; | |
658 | - char key2[] = "abcdefghijklmnopqrstuvwxyz"; | |
659 | - | |
660 | - u_int32_t data[10]; | |
661 | - u_int32_t data2[] = | |
662 | - {0x424c4f57l, 0x46495348l}; | |
663 | - | |
664 | - u_int16_t i; | |
665 | - | |
666 | - /* First test */ | |
667 | - for (i = 0; i < 10; i++) | |
668 | - data[i] = i; | |
669 | - | |
670 | - blf_key(&c, (u_int8_t *) key, 5); | |
671 | - blf_enc(&c, data, 5); | |
672 | - blf_dec(&c, data, 1); | |
673 | - blf_dec(&c, data + 2, 4); | |
674 | - printf("Should read as 0 - 9.\n"); | |
675 | - report(data, 10); | |
676 | - | |
677 | - /* Second test */ | |
678 | - blf_key(&c, (u_int8_t *) key2, strlen(key2)); | |
679 | - blf_enc(&c, data2, 1); | |
680 | - printf("\nShould read as: 0x324ed0fe 0xf413a203.\n"); | |
681 | - report(data2, 2); | |
682 | - blf_dec(&c, data2, 1); | |
683 | - report(data2, 2); | |
684 | -} | |
685 | - |
Built with git-ssb-web