summaryrefslogtreecommitdiff
path: root/cipher.c
diff options
context:
space:
mode:
authorBen Lindstrom <mouring@eviladmin.org>2001-09-14 02:47:33 +0000
committerBen Lindstrom <mouring@eviladmin.org>2001-09-14 02:47:33 +0000
commit319fc7353c647aa2703bb6c7f5288fb42f29e705 (patch)
tree6f18bbd9ecb425b951b549a3d01c59308d0bccaf /cipher.c
parent4213c559ef3d44670c8580cc552d23dce7528bda (diff)
I was promised that this does not need to have endness fix up by Markus.
So I will blindly trust him. =) - markus@cvs.openbsd.org 2001/08/23 11:31:59 [cipher.c cipher.h] switch to the optimised AES reference code from http://www.esat.kuleuven.ac.be/~rijmen/rijndael/rijndael-fst-3.0.zip
Diffstat (limited to 'cipher.c')
-rw-r--r--cipher.c59
1 files changed, 29 insertions, 30 deletions
diff --git a/cipher.c b/cipher.c
index 5f63cd4b2..de25ff096 100644
--- a/cipher.c
+++ b/cipher.c
@@ -35,7 +35,7 @@
35 */ 35 */
36 36
37#include "includes.h" 37#include "includes.h"
38RCSID("$OpenBSD: cipher.c,v 1.46 2001/06/25 08:25:36 markus Exp $"); 38RCSID("$OpenBSD: cipher.c,v 1.47 2001/08/23 11:31:59 markus Exp $");
39 39
40#include "xmalloc.h" 40#include "xmalloc.h"
41#include "log.h" 41#include "log.h"
@@ -283,66 +283,65 @@ cast_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
283static void 283static void
284rijndael_setkey(CipherContext *cc, const u_char *key, u_int keylen) 284rijndael_setkey(CipherContext *cc, const u_char *key, u_int keylen)
285{ 285{
286 rijndael_set_key(&cc->u.rijndael.enc, (u4byte *)key, 8*keylen, 1); 286 rijndael_set_key(&cc->u.rijndael.enc, (char *)key, 8*keylen, 1);
287 rijndael_set_key(&cc->u.rijndael.dec, (u4byte *)key, 8*keylen, 0); 287 rijndael_set_key(&cc->u.rijndael.dec, (char *)key, 8*keylen, 0);
288} 288}
289static void 289static void
290rijndael_setiv(CipherContext *cc, const u_char *iv, u_int ivlen) 290rijndael_setiv(CipherContext *cc, const u_char *iv, u_int ivlen)
291{ 291{
292 if (iv == NULL) 292 if (iv == NULL || ivlen != RIJNDAEL_BLOCKSIZE)
293 fatal("no IV for %s.", cc->cipher->name); 293 fatal("bad/no IV for %s.", cc->cipher->name);
294 memcpy((u_char *)cc->u.rijndael.iv, iv, RIJNDAEL_BLOCKSIZE); 294 memcpy(cc->u.rijndael.iv, iv, RIJNDAEL_BLOCKSIZE);
295} 295}
296static void 296static void
297rijndael_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src, 297rijndael_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src,
298 u_int len) 298 u_int len)
299{ 299{
300 rijndael_ctx *ctx = &cc->u.rijndael.enc; 300 rijndael_ctx *ctx = &cc->u.rijndael.enc;
301 u4byte *iv = cc->u.rijndael.iv; 301 u_char *iv = cc->u.rijndael.iv;
302 u4byte in[4]; 302 u_char in[RIJNDAEL_BLOCKSIZE];
303 u4byte *cprev, *cnow, *plain; 303 u_char *cprev, *cnow, *plain;
304 int i, blocks = len / RIJNDAEL_BLOCKSIZE; 304 int i, j, blocks = len / RIJNDAEL_BLOCKSIZE;
305
305 if (len == 0) 306 if (len == 0)
306 return; 307 return;
307 if (len % RIJNDAEL_BLOCKSIZE) 308 if (len % RIJNDAEL_BLOCKSIZE)
308 fatal("rijndael_cbc_encrypt: bad len %d", len); 309 fatal("rijndael_cbc_encrypt: bad len %d", len);
309 cnow = (u4byte*) dest; 310 cnow = dest;
310 plain = (u4byte*) src; 311 plain = (u_char *) src;
311 cprev = iv; 312 cprev = iv;
312 for(i = 0; i < blocks; i++, plain+=4, cnow+=4) { 313 for (i = 0; i < blocks; i++, plain+=RIJNDAEL_BLOCKSIZE,
313 in[0] = plain[0] ^ cprev[0]; 314 cnow+=RIJNDAEL_BLOCKSIZE) {
314 in[1] = plain[1] ^ cprev[1]; 315 for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++)
315 in[2] = plain[2] ^ cprev[2]; 316 in[j] = plain[j] ^ cprev[j];
316 in[3] = plain[3] ^ cprev[3];
317 rijndael_encrypt(ctx, in, cnow); 317 rijndael_encrypt(ctx, in, cnow);
318 cprev = cnow; 318 cprev = cnow;
319 } 319 }
320 memcpy(iv, cprev, RIJNDAEL_BLOCKSIZE); 320 memcpy(iv, cprev, RIJNDAEL_BLOCKSIZE);
321} 321}
322
323static void 322static void
324rijndael_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src, 323rijndael_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src,
325 u_int len) 324 u_int len)
326{ 325{
327 rijndael_ctx *ctx = &cc->u.rijndael.dec; 326 rijndael_ctx *ctx = &cc->u.rijndael.dec;
328 u4byte *iv = cc->u.rijndael.iv; 327 u_char *iv = cc->u.rijndael.iv;
329 u4byte ivsaved[4]; 328 u_char ivsaved[RIJNDAEL_BLOCKSIZE];
330 u4byte *cnow = (u4byte*) (src+len-RIJNDAEL_BLOCKSIZE); 329 u_char *cnow = (u_char *) (src+len-RIJNDAEL_BLOCKSIZE);
331 u4byte *plain = (u4byte*) (dest+len-RIJNDAEL_BLOCKSIZE); 330 u_char *plain = dest+len-RIJNDAEL_BLOCKSIZE;
332 u4byte *ivp; 331 u_char *ivp;
333 int i, blocks = len / RIJNDAEL_BLOCKSIZE; 332 int i, j, blocks = len / RIJNDAEL_BLOCKSIZE;
333
334 if (len == 0) 334 if (len == 0)
335 return; 335 return;
336 if (len % RIJNDAEL_BLOCKSIZE) 336 if (len % RIJNDAEL_BLOCKSIZE)
337 fatal("rijndael_cbc_decrypt: bad len %d", len); 337 fatal("rijndael_cbc_decrypt: bad len %d", len);
338 memcpy(ivsaved, cnow, RIJNDAEL_BLOCKSIZE); 338 memcpy(ivsaved, cnow, RIJNDAEL_BLOCKSIZE);
339 for(i = blocks; i > 0; i--, cnow-=4, plain-=4) { 339 for (i = blocks; i > 0; i--, cnow-=RIJNDAEL_BLOCKSIZE,
340 plain-=RIJNDAEL_BLOCKSIZE) {
340 rijndael_decrypt(ctx, cnow, plain); 341 rijndael_decrypt(ctx, cnow, plain);
341 ivp = (i == 1) ? iv : cnow-4; 342 ivp = (i == 1) ? iv : cnow-RIJNDAEL_BLOCKSIZE;
342 plain[0] ^= ivp[0]; 343 for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++)
343 plain[1] ^= ivp[1]; 344 plain[j] ^= ivp[j];
344 plain[2] ^= ivp[2];
345 plain[3] ^= ivp[3];
346 } 345 }
347 memcpy(iv, ivsaved, RIJNDAEL_BLOCKSIZE); 346 memcpy(iv, ivsaved, RIJNDAEL_BLOCKSIZE);
348} 347}