diff options
author | Ben Lindstrom <mouring@eviladmin.org> | 2000-12-07 05:57:27 +0000 |
---|---|---|
committer | Ben Lindstrom <mouring@eviladmin.org> | 2000-12-07 05:57:27 +0000 |
commit | 01f8463b15ead597f8ecf0052fd7569240dcaab9 (patch) | |
tree | 73ab1f33ee4c21041c160a93ff30e47c8179512b /cipher.c | |
parent | a14ee47f2eee3030cd784b93985a4de417a4b14c (diff) |
- markus@cvs.openbsd.org 2000/12/06 23:10:39
[rijndael.c]
unexpand(1)
- markus@cvs.openbsd.org 2000/12/06 23:05:43
[cipher.c cipher.h rijndael.c rijndael.h rijndael_boxes.h]
new rijndael implementation. fixes endian bugs
Diffstat (limited to 'cipher.c')
-rw-r--r-- | cipher.c | 67 |
1 files changed, 36 insertions, 31 deletions
@@ -35,7 +35,7 @@ | |||
35 | */ | 35 | */ |
36 | 36 | ||
37 | #include "includes.h" | 37 | #include "includes.h" |
38 | RCSID("$OpenBSD: cipher.c,v 1.38 2000/11/29 20:39:17 markus Exp $"); | 38 | RCSID("$OpenBSD: cipher.c,v 1.39 2000/12/06 23:05:42 markus Exp $"); |
39 | 39 | ||
40 | #include "ssh.h" | 40 | #include "ssh.h" |
41 | #include "xmalloc.h" | 41 | #include "xmalloc.h" |
@@ -285,40 +285,45 @@ cast_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len) | |||
285 | /* RIJNDAEL */ | 285 | /* RIJNDAEL */ |
286 | 286 | ||
287 | #define RIJNDAEL_BLOCKSIZE 16 | 287 | #define RIJNDAEL_BLOCKSIZE 16 |
288 | |||
288 | void | 289 | void |
289 | rijndael_setkey(CipherContext *cc, const u_char *key, u_int keylen) | 290 | rijndael_setkey(CipherContext *cc, const u_char *key, u_int keylen) |
290 | { | 291 | { |
291 | rijndael_set_key(&cc->u.rijndael.enc, (u4byte *)key, 8*keylen, 1); | 292 | if (rijndael_makekey(&cc->u.rijndael.enc, RIJNDAEL_ENCRYPT, |
292 | rijndael_set_key(&cc->u.rijndael.dec, (u4byte *)key, 8*keylen, 0); | 293 | 8*keylen, (char *)key) == -1) |
294 | fatal("rijndael_setkey: RIJNDAEL_ENCRYPT"); | ||
295 | if (rijndael_makekey(&cc->u.rijndael.dec, RIJNDAEL_DECRYPT, | ||
296 | 8*keylen, (char *)key) == -1) | ||
297 | fatal("rijndael_setkey: RIJNDAEL_DECRYPT"); | ||
293 | } | 298 | } |
294 | void | 299 | void |
295 | rijndael_setiv(CipherContext *cc, const u_char *iv, u_int ivlen) | 300 | rijndael_setiv(CipherContext *cc, const u_char *iv, u_int ivlen) |
296 | { | 301 | { |
297 | if (iv == NULL) | 302 | if (iv == NULL || ivlen != RIJNDAEL_BLOCKSIZE) |
298 | fatal("no IV for %s.", cc->cipher->name); | 303 | fatal("bad/no IV for %s.", cc->cipher->name); |
299 | memcpy((u_char *)cc->u.rijndael.iv, iv, RIJNDAEL_BLOCKSIZE); | 304 | memcpy(cc->u.rijndael.iv, iv, RIJNDAEL_BLOCKSIZE); |
300 | } | 305 | } |
306 | |||
301 | void | 307 | void |
302 | rijndael_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src, | 308 | rijndael_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src, |
303 | u_int len) | 309 | u_int len) |
304 | { | 310 | { |
305 | rijndael_ctx *ctx = &cc->u.rijndael.enc; | 311 | rijndael_key *ctx = &cc->u.rijndael.enc; |
306 | u4byte *iv = cc->u.rijndael.iv; | 312 | u_char *iv = cc->u.rijndael.iv; |
307 | u4byte in[4]; | 313 | u_char in[RIJNDAEL_BLOCKSIZE]; |
308 | u4byte *cprev, *cnow, *plain; | 314 | u_char *cprev, *cnow, *plain; |
309 | int i, blocks = len / RIJNDAEL_BLOCKSIZE; | 315 | int i, j, blocks = len / RIJNDAEL_BLOCKSIZE; |
310 | if (len == 0) | 316 | if (len == 0) |
311 | return; | 317 | return; |
312 | if (len % RIJNDAEL_BLOCKSIZE) | 318 | if (len % RIJNDAEL_BLOCKSIZE) |
313 | fatal("rijndael_cbc_encrypt: bad len %d", len); | 319 | fatal("rijndael_cbc_encrypt: bad len %d", len); |
314 | cnow = (u4byte*) dest; | 320 | cnow = dest; |
315 | plain = (u4byte*) src; | 321 | plain = (u_char *) src; |
316 | cprev = iv; | 322 | cprev = iv; |
317 | for(i = 0; i < blocks; i++, plain+=4, cnow+=4) { | 323 | for(i = 0; i < blocks; i++, plain+=RIJNDAEL_BLOCKSIZE, |
318 | in[0] = plain[0] ^ cprev[0]; | 324 | cnow+=RIJNDAEL_BLOCKSIZE) { |
319 | in[1] = plain[1] ^ cprev[1]; | 325 | for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++) |
320 | in[2] = plain[2] ^ cprev[2]; | 326 | in[j] = plain[j] ^ cprev[j]; |
321 | in[3] = plain[3] ^ cprev[3]; | ||
322 | rijndael_encrypt(ctx, in, cnow); | 327 | rijndael_encrypt(ctx, in, cnow); |
323 | cprev = cnow; | 328 | cprev = cnow; |
324 | } | 329 | } |
@@ -329,25 +334,25 @@ void | |||
329 | rijndael_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src, | 334 | rijndael_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src, |
330 | u_int len) | 335 | u_int len) |
331 | { | 336 | { |
332 | rijndael_ctx *ctx = &cc->u.rijndael.dec; | 337 | rijndael_key *ctx = &cc->u.rijndael.dec; |
333 | u4byte *iv = cc->u.rijndael.iv; | 338 | u_char *iv = cc->u.rijndael.iv; |
334 | u4byte ivsaved[4]; | 339 | u_char ivsaved[RIJNDAEL_BLOCKSIZE]; |
335 | u4byte *cnow = (u4byte*) (src+len-RIJNDAEL_BLOCKSIZE); | 340 | u_char *cnow = (u_char *) (src+len-RIJNDAEL_BLOCKSIZE); |
336 | u4byte *plain = (u4byte*) (dest+len-RIJNDAEL_BLOCKSIZE); | 341 | u_char *plain = dest+len-RIJNDAEL_BLOCKSIZE; |
337 | u4byte *ivp; | 342 | u_char *ivp; |
338 | int i, blocks = len / RIJNDAEL_BLOCKSIZE; | 343 | int i, j, blocks = len / RIJNDAEL_BLOCKSIZE; |
339 | if (len == 0) | 344 | if (len == 0) |
340 | return; | 345 | return; |
341 | if (len % RIJNDAEL_BLOCKSIZE) | 346 | if (len % RIJNDAEL_BLOCKSIZE) |
342 | fatal("rijndael_cbc_decrypt: bad len %d", len); | 347 | fatal("rijndael_cbc_decrypt: bad len %d", len); |
343 | memcpy(ivsaved, cnow, RIJNDAEL_BLOCKSIZE); | 348 | memcpy(ivsaved, cnow, RIJNDAEL_BLOCKSIZE); |
344 | for(i = blocks; i > 0; i--, cnow-=4, plain-=4) { | 349 | for(i = blocks; i > 0; i--, cnow-=RIJNDAEL_BLOCKSIZE, |
350 | plain-=RIJNDAEL_BLOCKSIZE) { | ||
345 | rijndael_decrypt(ctx, cnow, plain); | 351 | rijndael_decrypt(ctx, cnow, plain); |
346 | ivp = (i == 1) ? iv : cnow-4; | 352 | //rijndael_decrypt(cnow, plain, ctx->keySched, ctx->ROUNDS); |
347 | plain[0] ^= ivp[0]; | 353 | ivp = (i == 1) ? iv : cnow-RIJNDAEL_BLOCKSIZE; |
348 | plain[1] ^= ivp[1]; | 354 | for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++) |
349 | plain[2] ^= ivp[2]; | 355 | plain[j] ^= ivp[j]; |
350 | plain[3] ^= ivp[3]; | ||
351 | } | 356 | } |
352 | memcpy(iv, ivsaved, RIJNDAEL_BLOCKSIZE); | 357 | memcpy(iv, ivsaved, RIJNDAEL_BLOCKSIZE); |
353 | } | 358 | } |