Skip to content

Commit 033cec5

Browse files
Tom St Denissjaeckel
authored andcommitted
added libtomcrypt-0.92
1 parent 55d745a commit 033cec5

66 files changed

Lines changed: 9123 additions & 1253 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

aes.c

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const struct _cipher_descriptor aes_desc =
4747

4848
int rijndael_setup(const unsigned char *key, int keylen, int rounds, symmetric_key *skey)
4949
{
50-
int i = 0, j;
50+
int i, j;
5151
ulong32 temp, *rk, *rrk;
5252

5353
_ARGCHK(key != NULL);
@@ -64,6 +64,7 @@ int rijndael_setup(const unsigned char *key, int keylen, int rounds, symmetric_k
6464
skey->rijndael.Nr = 10 + ((keylen/8)-2)*2;
6565

6666
/* setup the forward key */
67+
i = 0;
6768
rk = skey->rijndael.eK;
6869
LOAD32H(rk[0], key );
6970
LOAD32H(rk[1], key + 4);
@@ -83,7 +84,7 @@ int rijndael_setup(const unsigned char *key, int keylen, int rounds, symmetric_k
8384
rk[6] = rk[2] ^ rk[5];
8485
rk[7] = rk[3] ^ rk[6];
8586
if (++i == 10) {
86-
break;
87+
break;
8788
}
8889
rk += 4;
8990
}
@@ -233,7 +234,11 @@ int rijndael_setup(const unsigned char *key, int keylen, int rounds, symmetric_k
233234
return CRYPT_OK;
234235
}
235236

236-
void rijndael_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
237+
#ifdef CLEAN_STACK
238+
static void _rijndael_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
239+
#else
240+
void rijndael_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
241+
#endif
237242
{
238243
ulong32 s0, s1, s2, s3, t0, t1, t2, t3, *rk;
239244
int Nr, r;
@@ -259,8 +264,6 @@ void rijndael_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_
259264
*/
260265
r = Nr >> 1;
261266
for (;;) {
262-
263-
/* Both of these blocks are equivalent except the top is more friendlier for x86 processors */
264267
t0 =
265268
Te0[byte(s0, 3)] ^
266269
Te1[byte(s1, 2)] ^
@@ -350,7 +353,20 @@ void rijndael_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_
350353
STORE32H(s3, ct+12);
351354
}
352355

353-
void rijndael_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) {
356+
#ifdef CLEAN_STACK
357+
void rijndael_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
358+
{
359+
_rijndael_ecb_encrypt(pt, ct, skey);
360+
burn_stack(sizeof(unsigned long)*8 + sizeof(unsigned long*) + sizeof(int)*2);
361+
}
362+
#endif
363+
364+
#ifdef CLEAN_STACK
365+
static void _rijndael_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
366+
#else
367+
void rijndael_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
368+
#endif
369+
{
354370
ulong32 s0, s1, s2, s3, t0, t1, t2, t3, *rk;
355371
int Nr, r;
356372

@@ -467,6 +483,15 @@ void rijndael_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_
467483
STORE32H(s3, pt+12);
468484
}
469485

486+
487+
#ifdef CLEAN_STACK
488+
void rijndael_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
489+
{
490+
_rijndael_ecb_decrypt(ct, pt, skey);
491+
burn_stack(sizeof(unsigned long)*8 + sizeof(unsigned long*) + sizeof(int)*2);
492+
}
493+
#endif
494+
470495
int rijndael_test(void)
471496
{
472497
#ifndef LTC_TEST
@@ -508,7 +533,7 @@ int rijndael_test(void)
508533

509534
symmetric_key key;
510535
unsigned char tmp[2][16];
511-
int i;
536+
int i, y;
512537

513538
for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
514539
zeromem(&key, sizeof(key));
@@ -537,6 +562,12 @@ int rijndael_test(void)
537562
#endif
538563
return CRYPT_FAIL_TESTVECTOR;
539564
}
565+
566+
/* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
567+
for (y = 0; y < 16; y++) tmp[0][y] = 0;
568+
for (y = 0; y < 1000; y++) rijndael_ecb_encrypt(tmp[0], tmp[0], &key);
569+
for (y = 0; y < 1000; y++) rijndael_ecb_decrypt(tmp[0], tmp[0], &key);
570+
for (y = 0; y < 16; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR;
540571
}
541572
return CRYPT_OK;
542573
#endif

blowfish.c

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,10 @@ int blowfish_setup(const unsigned char *key, int keylen, int num_rounds,
304304
for (x = y = 0; x < 18; x++) {
305305
A = 0;
306306
for (z = 0; z < 4; z++) {
307-
A = (A << 8) | ((ulong32)key[y++ % keylen]);
307+
A = (A << 8) | ((ulong32)key[y++] & 255);
308+
if (y == (ulong32)keylen) {
309+
y = 0;
310+
}
308311
}
309312
skey->blowfish.K[x] = ORIG_P[x] ^ A;
310313
}
@@ -347,13 +350,6 @@ int blowfish_setup(const unsigned char *key, int keylen, int num_rounds,
347350
return CRYPT_OK;
348351
}
349352

350-
#if defined(__GNUC__)
351-
#define S1 key->blowfish.S[0]
352-
#define S2 key->blowfish.S[1]
353-
#define S3 key->blowfish.S[2]
354-
#define S4 key->blowfish.S[3]
355-
#endif
356-
357353
#define F(x) ((S1[byte(x,3)] + S2[byte(x,2)]) ^ S3[byte(x,1)]) + S4[byte(x,0)]
358354

359355
#ifdef CLEAN_STACK
@@ -364,20 +360,16 @@ void blowfish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_
364360
{
365361
ulong32 L, R;
366362
int r;
367-
#if !defined(TWOFISH_SMALL) && !defined(__GNUC__)
368-
ulong32 *S1, *S2, *S3, *S4;
369-
#endif
363+
ulong32 *S1, *S2, *S3, *S4;
370364

371365
_ARGCHK(pt != NULL);
372366
_ARGCHK(ct != NULL);
373367
_ARGCHK(key != NULL);
374-
375-
#if !defined(TWOFISH_SMALL) && !defined(__GNUC__)
368+
376369
S1 = key->blowfish.S[0];
377370
S2 = key->blowfish.S[1];
378371
S3 = key->blowfish.S[2];
379372
S4 = key->blowfish.S[3];
380-
#endif
381373

382374
/* load it */
383375
LOAD32H(L, &pt[0]);
@@ -416,20 +408,16 @@ void blowfish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_
416408
{
417409
ulong32 L, R;
418410
int r;
419-
#if !defined(TWOFISH_SMALL) && !defined(__GNUC__)
420-
ulong32 *S1, *S2, *S3, *S4;
421-
#endif
411+
ulong32 *S1, *S2, *S3, *S4;
422412

423413
_ARGCHK(pt != NULL);
424414
_ARGCHK(ct != NULL);
425415
_ARGCHK(key != NULL);
426416

427-
#if !defined(TWOFISH_SMALL) && !defined(__GNUC__)
428417
S1 = key->blowfish.S[0];
429418
S2 = key->blowfish.S[1];
430419
S3 = key->blowfish.S[2];
431420
S4 = key->blowfish.S[3];
432-
#endif
433421

434422
/* load it */
435423
LOAD32H(R, &ct[0]);
@@ -487,8 +475,8 @@ int blowfish_test(void)
487475
{ 0x7D, 0x85, 0x6F, 0x9A, 0x61, 0x30, 0x63, 0xF2}
488476
}
489477
};
490-
unsigned char buf[2][8];
491-
int x;
478+
unsigned char tmp[2][8];
479+
int x, y;
492480

493481
for (x = 0; x < (int)(sizeof(tests) / sizeof(tests[0])); x++) {
494482
/* setup key */
@@ -497,13 +485,19 @@ int blowfish_test(void)
497485
}
498486

499487
/* encrypt and decrypt */
500-
blowfish_ecb_encrypt(tests[x].pt, buf[0], &key);
501-
blowfish_ecb_decrypt(buf[0], buf[1], &key);
488+
blowfish_ecb_encrypt(tests[x].pt, tmp[0], &key);
489+
blowfish_ecb_decrypt(tmp[0], tmp[1], &key);
502490

503491
/* compare */
504-
if ((memcmp(buf[0], tests[x].ct, 8) != 0) || (memcmp(buf[1], tests[x].pt, 8) != 0)) {
492+
if ((memcmp(tmp[0], tests[x].ct, 8) != 0) || (memcmp(tmp[1], tests[x].pt, 8) != 0)) {
505493
return CRYPT_FAIL_TESTVECTOR;
506494
}
495+
496+
/* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
497+
for (y = 0; y < 8; y++) tmp[0][y] = 0;
498+
for (y = 0; y < 1000; y++) blowfish_ecb_encrypt(tmp[0], tmp[0], &key);
499+
for (y = 0; y < 1000; y++) blowfish_ecb_decrypt(tmp[0], tmp[0], &key);
500+
for (y = 0; y < 8; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR;
507501
}
508502
return CRYPT_OK;
509503
#endif

cast5.c

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,11 @@ static const ulong32 S8[256] = {
381381
#define GB(x, i) (((x[(15-i)>>2])>>(unsigned)(8*((15-i)&3)))&255)
382382
#endif
383383

384+
#ifdef CLEAN_STACK
385+
static int _cast5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
386+
#else
384387
int cast5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
388+
#endif
385389
{
386390
ulong32 x[4], z[4];
387391
unsigned char buf[16];
@@ -431,7 +435,6 @@ int cast5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_
431435
skey->cast5.K[i++] = S5[GB(x, 0x5)] ^ S6[GB(x, 0x4)] ^ S7[GB(x, 0xa)] ^ S8[GB(x, 0xb)] ^ S8[GB(x, 0x7)];
432436

433437
/* second half */
434-
435438
z[3] = x[3] ^ S5[GB(x, 0xD)] ^ S6[GB(x, 0xF)] ^ S7[GB(x, 0xC)] ^ S8[GB(x, 0xE)] ^ S7[GB(x, 0x8)];
436439
z[2] = x[1] ^ S5[GB(z, 0x0)] ^ S6[GB(z, 0x2)] ^ S7[GB(z, 0x1)] ^ S8[GB(z, 0x3)] ^ S8[GB(x, 0xA)];
437440
z[1] = x[0] ^ S5[GB(z, 0x7)] ^ S6[GB(z, 0x6)] ^ S7[GB(z, 0x5)] ^ S8[GB(z, 0x4)] ^ S5[GB(x, 0x9)];
@@ -462,6 +465,16 @@ int cast5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_
462465
return CRYPT_OK;
463466
}
464467

468+
#ifdef CLEAN_STACK
469+
int cast5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
470+
{
471+
int z;
472+
z = _cast5_setup(key, keylen, num_rounds, skey);
473+
burn_stack(sizeof(ulong32)*8 + 16 + sizeof(int)*2);
474+
return z;
475+
}
476+
#endif
477+
465478
#ifdef _MSC_VER
466479
#define INLINE __inline
467480
#else
@@ -492,7 +505,11 @@ INLINE static ulong32 FIII(ulong32 R, ulong32 Km, ulong32 Kr)
492505
return ((S1[byte(I, 3)] + S2[byte(I,2)]) ^ S3[byte(I,1)]) - S4[byte(I,0)];
493506
}
494507

508+
#ifdef CLEAN_STACK
509+
static void _cast5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
510+
#else
495511
void cast5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
512+
#endif
496513
{
497514
ulong32 R, L;
498515

@@ -502,7 +519,6 @@ void cast5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key
502519

503520
LOAD32H(L,&pt[0]);
504521
LOAD32H(R,&pt[4]);
505-
506522
L ^= FI(R, key->cast5.K[0], key->cast5.K[16]);
507523
R ^= FII(L, key->cast5.K[1], key->cast5.K[17]);
508524
L ^= FIII(R, key->cast5.K[2], key->cast5.K[18]);
@@ -525,7 +541,20 @@ void cast5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key
525541
STORE32H(L,&ct[4]);
526542
}
527543

544+
545+
#ifdef CLEAN_STACK
546+
void cast5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key)
547+
{
548+
_cast5_ecb_encrypt(pt,ct,key);
549+
burn_stack(sizeof(ulong32)*3);
550+
}
551+
#endif
552+
553+
#ifdef CLEAN_STACK
554+
static void _cast5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
555+
#else
528556
void cast5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
557+
#endif
529558
{
530559
ulong32 R, L;
531560

@@ -535,14 +564,12 @@ void cast5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key
535564

536565
LOAD32H(R,&ct[0]);
537566
LOAD32H(L,&ct[4]);
538-
539567
if (key->cast5.keylen > 10) {
540568
R ^= FI(L, key->cast5.K[15], key->cast5.K[31]);
541569
L ^= FIII(R, key->cast5.K[14], key->cast5.K[30]);
542570
R ^= FII(L, key->cast5.K[13], key->cast5.K[29]);
543571
L ^= FI(R, key->cast5.K[12], key->cast5.K[28]);
544572
}
545-
546573
R ^= FIII(L, key->cast5.K[11], key->cast5.K[27]);
547574
L ^= FII(R, key->cast5.K[10], key->cast5.K[26]);
548575
R ^= FI(L, key->cast5.K[9], key->cast5.K[25]);
@@ -555,11 +582,18 @@ void cast5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key
555582
L ^= FIII(R, key->cast5.K[2], key->cast5.K[18]);
556583
R ^= FII(L, key->cast5.K[1], key->cast5.K[17]);
557584
L ^= FI(R, key->cast5.K[0], key->cast5.K[16]);
558-
559585
STORE32H(L,&pt[0]);
560586
STORE32H(R,&pt[4]);
561587
}
562588

589+
#ifdef CLEAN_STACK
590+
void cast5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key)
591+
{
592+
_cast5_ecb_decrypt(ct,pt,key);
593+
burn_stack(sizeof(ulong32)*3);
594+
}
595+
#endif
596+
563597
int cast5_test(void)
564598
{
565599
#ifndef LTC_TEST
@@ -587,19 +621,24 @@ int cast5_test(void)
587621
{0x7A, 0xC8, 0x16, 0xD1, 0x6E, 0x9B, 0x30, 0x2E}
588622
}
589623
};
590-
int i, err;
624+
int i, y, err;
591625
symmetric_key key;
592-
unsigned char buf[8], buf2[8];
626+
unsigned char tmp[2][8];
593627

594628
for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
595629
if ((err = cast5_setup(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) {
596630
return err;
597631
}
598-
cast5_ecb_encrypt(tests[i].pt, buf, &key);
599-
cast5_ecb_decrypt(buf, buf2, &key);
600-
if ((memcmp(buf, tests[i].ct, 8) != 0) || (memcmp(buf2, tests[i].pt, 8) != 0)) {
632+
cast5_ecb_encrypt(tests[i].pt, tmp[0], &key);
633+
cast5_ecb_decrypt(tmp[0], tmp[1], &key);
634+
if ((memcmp(tmp[0], tests[i].ct, 8) != 0) || (memcmp(tmp[1], tests[i].pt, 8) != 0)) {
601635
return CRYPT_FAIL_TESTVECTOR;
602636
}
637+
/* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
638+
for (y = 0; y < 8; y++) tmp[0][y] = 0;
639+
for (y = 0; y < 1000; y++) cast5_ecb_encrypt(tmp[0], tmp[0], &key);
640+
for (y = 0; y < 1000; y++) cast5_ecb_decrypt(tmp[0], tmp[0], &key);
641+
for (y = 0; y < 8; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR;
603642

604643
}
605644
return CRYPT_OK;
@@ -618,4 +657,3 @@ int cast5_keysize(int *desired_keysize)
618657
}
619658

620659
#endif
621-

0 commit comments

Comments
 (0)