Codebase list libcryptx-perl / 1644699
libtomcrypt upgrade Karel Miko 3 years ago
93 changed file(s) with 937 addition(s) and 927 deletion(s). Raw diff Collapse all Expand all
22 0.070 2020-12-XX
33 - fix #66 remove -flto=auto (Makefile.PL)
44 - fix #65 typo in docs (lib/Crypt/Cipher.pm)
5 - bundled libtomcrypt update branch:develop (commit:cfbd7f8d 2020-08-29)
56
67 0.069 2020-08-25
78 - fix #64 ECC: segfault on invalid input
7979
8080 #endif
8181
82 #define __LTC_AES_TAB_C__
82 #define LTC_AES_TAB_C
8383 #include "aes_tab.c"
8484
8585 static ulong32 setup_mix(ulong32 temp)
274274 @return CRYPT_OK if successful
275275 */
276276 #ifdef LTC_CLEAN_STACK
277 static int _rijndael_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
277 static int s_rijndael_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
278278 #else
279279 int ECB_ENC(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
280280 #endif
442442 #ifdef LTC_CLEAN_STACK
443443 int ECB_ENC(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
444444 {
445 int err = _rijndael_ecb_encrypt(pt, ct, skey);
445 int err = s_rijndael_ecb_encrypt(pt, ct, skey);
446446 burn_stack(sizeof(unsigned long)*8 + sizeof(unsigned long*) + sizeof(int)*2);
447447 return err;
448448 }
458458 @return CRYPT_OK if successful
459459 */
460460 #ifdef LTC_CLEAN_STACK
461 static int _rijndael_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
461 static int s_rijndael_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
462462 #else
463463 int ECB_DEC(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
464464 #endif
627627 #ifdef LTC_CLEAN_STACK
628628 int ECB_DEC(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
629629 {
630 int err = _rijndael_ecb_decrypt(ct, pt, skey);
630 int err = s_rijndael_ecb_decrypt(ct, pt, skey);
631631 burn_stack(sizeof(unsigned long)*8 + sizeof(unsigned long*) + sizeof(int)*2);
632632 return err;
633633 }
1414 Td4[x] = Si[x].[01, 01, 01, 01];
1515 */
1616
17 #ifdef __LTC_AES_TAB_C__
17 #ifdef LTC_AES_TAB_C
1818
1919 /**
2020 @file aes_tab.c
10181018 };
10191019 #endif
10201020
1021 #endif /* __LTC_AES_TAB_C__ */
1021 #endif /* LTC_AES_TAB_C */
875875 @return CRYPT_OK if successful
876876 */
877877 #ifdef LTC_CLEAN_STACK
878 static int _anubis_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
878 static int s_anubis_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
879879 #else
880880 int anubis_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
881881 #endif
10121012 int anubis_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
10131013 {
10141014 int err;
1015 err = _anubis_setup(key, keylen, num_rounds, skey);
1015 err = s_anubis_setup(key, keylen, num_rounds, skey);
10161016 burn_stack(sizeof(int) * 5 + sizeof(ulong32) * (MAX_N + MAX_N + 5));
10171017 return err;
10181018 }
471471 @return CRYPT_OK if successful
472472 */
473473 #ifdef LTC_CLEAN_STACK
474 static int _blowfish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
474 static int s_blowfish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
475475 #else
476476 int blowfish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
477477 #endif
498498 #ifdef LTC_CLEAN_STACK
499499 int blowfish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
500500 {
501 int err = _blowfish_ecb_encrypt(pt, ct, skey);
501 int err = s_blowfish_ecb_encrypt(pt, ct, skey);
502502 burn_stack(sizeof(ulong32) * 2 + sizeof(int));
503503 return err;
504504 }
512512 @return CRYPT_OK if successful
513513 */
514514 #ifdef LTC_CLEAN_STACK
515 static int _blowfish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
515 static int s_blowfish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
516516 #else
517517 int blowfish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
518518 #endif
559559 #ifdef LTC_CLEAN_STACK
560560 int blowfish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
561561 {
562 int err = _blowfish_ecb_decrypt(ct, pt, skey);
562 int err = s_blowfish_ecb_decrypt(ct, pt, skey);
563563 burn_stack(sizeof(ulong32) * 2 + sizeof(int));
564564 return err;
565565 }
397397 @return CRYPT_OK if successful
398398 */
399399 #ifdef LTC_CLEAN_STACK
400 static int _cast5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
400 static int s_cast5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
401401 #else
402402 int cast5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
403403 #endif
484484 int cast5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
485485 {
486486 int z;
487 z = _cast5_setup(key, keylen, num_rounds, skey);
487 z = s_cast5_setup(key, keylen, num_rounds, skey);
488488 burn_stack(sizeof(ulong32)*8 + 16 + sizeof(int)*2);
489489 return z;
490490 }
491491 #endif
492492
493 #ifdef _MSC_VER
494 #define INLINE __inline
495 #else
496 #define INLINE
497 #endif
498
499 INLINE static ulong32 FI(ulong32 R, ulong32 Km, ulong32 Kr)
493 LTC_INLINE static ulong32 FI(ulong32 R, ulong32 Km, ulong32 Kr)
500494 {
501495 ulong32 I;
502496 I = (Km + R);
504498 return ((S1[LTC_BYTE(I, 3)] ^ S2[LTC_BYTE(I,2)]) - S3[LTC_BYTE(I,1)]) + S4[LTC_BYTE(I,0)];
505499 }
506500
507 INLINE static ulong32 FII(ulong32 R, ulong32 Km, ulong32 Kr)
501 LTC_INLINE static ulong32 FII(ulong32 R, ulong32 Km, ulong32 Kr)
508502 {
509503 ulong32 I;
510504 I = (Km ^ R);
512506 return ((S1[LTC_BYTE(I, 3)] - S2[LTC_BYTE(I,2)]) + S3[LTC_BYTE(I,1)]) ^ S4[LTC_BYTE(I,0)];
513507 }
514508
515 INLINE static ulong32 FIII(ulong32 R, ulong32 Km, ulong32 Kr)
509 LTC_INLINE static ulong32 FIII(ulong32 R, ulong32 Km, ulong32 Kr)
516510 {
517511 ulong32 I;
518512 I = (Km - R);
527521 @param skey The key as scheduled
528522 */
529523 #ifdef LTC_CLEAN_STACK
530 static int _cast5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
524 static int s_cast5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
531525 #else
532526 int cast5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
533527 #endif
567561 #ifdef LTC_CLEAN_STACK
568562 int cast5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
569563 {
570 int err =_cast5_ecb_encrypt(pt,ct,skey);
564 int err = s_cast5_ecb_encrypt(pt,ct,skey);
571565 burn_stack(sizeof(ulong32)*3);
572566 return err;
573567 }
580574 @param skey The key as scheduled
581575 */
582576 #ifdef LTC_CLEAN_STACK
583 static int _cast5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
577 static int s_cast5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
584578 #else
585579 int cast5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
586580 #endif
620614 #ifdef LTC_CLEAN_STACK
621615 int cast5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
622616 {
623 int err = _cast5_ecb_decrypt(ct,pt,skey);
617 int err = s_cast5_ecb_decrypt(ct,pt,skey);
624618 burn_stack(sizeof(ulong32)*3);
625619 return err;
626620 }
12921292 static void cookey(const ulong32 *raw1, ulong32 *keyout);
12931293
12941294 #ifdef LTC_CLEAN_STACK
1295 static void _deskey(const unsigned char *key, short edf, ulong32 *keyout)
1295 static void s_deskey(const unsigned char *key, short edf, ulong32 *keyout)
12961296 #else
12971297 static void deskey(const unsigned char *key, short edf, ulong32 *keyout)
12981298 #endif
13461346 #ifdef LTC_CLEAN_STACK
13471347 static void deskey(const unsigned char *key, short edf, ulong32 *keyout)
13481348 {
1349 _deskey(key, edf, keyout);
1349 s_deskey(key, edf, keyout);
13501350 burn_stack(sizeof(int)*5 + sizeof(ulong32)*32 + sizeof(unsigned char)*112);
13511351 }
13521352 #endif
13531353
13541354 #ifdef LTC_CLEAN_STACK
1355 static void _cookey(const ulong32 *raw1, ulong32 *keyout)
1355 static void s_cookey(const ulong32 *raw1, ulong32 *keyout)
13561356 #else
13571357 static void cookey(const ulong32 *raw1, ulong32 *keyout)
13581358 #endif
13821382 #ifdef LTC_CLEAN_STACK
13831383 static void cookey(const ulong32 *raw1, ulong32 *keyout)
13841384 {
1385 _cookey(raw1, keyout);
1385 s_cookey(raw1, keyout);
13861386 burn_stack(sizeof(ulong32 *) * 2 + sizeof(ulong32)*32 + sizeof(int));
13871387 }
13881388 #endif
13901390 #ifndef LTC_CLEAN_STACK
13911391 static void desfunc(ulong32 *block, const ulong32 *keys)
13921392 #else
1393 static void _desfunc(ulong32 *block, const ulong32 *keys)
1393 static void s_desfunc(ulong32 *block, const ulong32 *keys)
13941394 #endif
13951395 {
13961396 ulong32 work, right, leftt;
15041504 #ifdef LTC_CLEAN_STACK
15051505 static void desfunc(ulong32 *block, const ulong32 *keys)
15061506 {
1507 _desfunc(block, keys);
1507 s_desfunc(block, keys);
15081508 burn_stack(sizeof(ulong32) * 4 + sizeof(int));
15091509 }
15101510 #endif
3434
3535 typedef unsigned short int ushort16;
3636
37 #define _LOW16(x) ((x)&0xffff) /* compiler should be able to optimize this away if x is 16 bits */
38 #define _HIGH16(x) ((x)>>16)
39 #define _MUL(a,b) { \
40 ulong32 p = (ulong32)_LOW16(a) * b; \
37 #define LOW16(x) ((x)&0xffff) /* compiler should be able to optimize this away if x is 16 bits */
38 #define HIGH16(x) ((x)>>16)
39 #define MUL(a,b) { \
40 ulong32 p = (ulong32)LOW16(a) * b; \
4141 if (p) { \
42 p = _LOW16(p) - _HIGH16(p); \
43 a = (ushort16)p - (ushort16)_HIGH16(p); \
42 p = LOW16(p) - HIGH16(p); \
43 a = (ushort16)p - (ushort16)HIGH16(p); \
4444 } \
4545 else \
4646 a = 1 - a - b; \
4747 }
48 #define _STORE16(x,y) { (y)[0] = (unsigned char)(((x)>>8)&255); (y)[1] = (unsigned char)((x)&255); }
49 #define _LOAD16(x,y) { x = ((ushort16)((y)[0] & 255)<<8) | ((ushort16)((y)[1] & 255)); }
50
51 static ushort16 _mul_inv(ushort16 x)
48 #define STORE16(x,y) { (y)[0] = (unsigned char)(((x)>>8)&255); (y)[1] = (unsigned char)((x)&255); }
49 #define LOAD16(x,y) { x = ((ushort16)((y)[0] & 255)<<8) | ((ushort16)((y)[1] & 255)); }
50
51 static ushort16 s_mul_inv(ushort16 x)
5252 {
5353 ushort16 y = x;
5454 unsigned i;
5555
5656 for (i = 0; i < 15; i++) {
57 _MUL(y, _LOW16(y));
58 _MUL(y, x);
59 }
60 return _LOW16(y);
61 }
62
63 static ushort16 _add_inv(ushort16 x)
64 {
65 return _LOW16(0 - x);
66 }
67
68 static int _setup_key(const unsigned char *key, symmetric_key *skey)
57 MUL(y, LOW16(y));
58 MUL(y, x);
59 }
60 return LOW16(y);
61 }
62
63 static ushort16 s_add_inv(ushort16 x)
64 {
65 return LOW16(0 - x);
66 }
67
68 static int s_setup_key(const unsigned char *key, symmetric_key *skey)
6969 {
7070 int i, j;
7171 ushort16 *e_key = skey->idea.ek;
7373
7474 /* prepare enc key */
7575 for (i = 0; i < 8; i++) {
76 _LOAD16(e_key[i], key + 2 * i);
76 LOAD16(e_key[i], key + 2 * i);
7777 }
7878 for (; i < LTC_IDEA_KEYLEN; i++) {
7979 j = (i - i % 8) - 8;
80 e_key[i] = _LOW16((e_key[j+(i+1)%8] << 9) | (e_key[j+(i+2)%8] >> 7));
80 e_key[i] = LOW16((e_key[j+(i+1)%8] << 9) | (e_key[j+(i+2)%8] >> 7));
8181 }
8282
8383 /* prepare dec key */
8484 for (i = 0; i < LTC_IDEA_ROUNDS; i++) {
85 d_key[i*6+0] = _mul_inv(e_key[(LTC_IDEA_ROUNDS-i)*6+0]);
86 d_key[i*6+1] = _add_inv(e_key[(LTC_IDEA_ROUNDS-i)*6+1+(i>0 ? 1 : 0)]);
87 d_key[i*6+2] = _add_inv(e_key[(LTC_IDEA_ROUNDS-i)*6+2-(i>0 ? 1 : 0)]);
88 d_key[i*6+3] = _mul_inv(e_key[(LTC_IDEA_ROUNDS-i)*6+3]);
89 d_key[i*6+4] = e_key[(LTC_IDEA_ROUNDS-1-i)*6+4];
90 d_key[i*6+5] = e_key[(LTC_IDEA_ROUNDS-1-i)*6+5];
91 }
92 d_key[i*6+0] = _mul_inv(e_key[(LTC_IDEA_ROUNDS-i)*6+0]);
93 d_key[i*6+1] = _add_inv(e_key[(LTC_IDEA_ROUNDS-i)*6+1]);
94 d_key[i*6+2] = _add_inv(e_key[(LTC_IDEA_ROUNDS-i)*6+2]);
95 d_key[i*6+3] = _mul_inv(e_key[(LTC_IDEA_ROUNDS-i)*6+3]);
96
97 return CRYPT_OK;
98 }
99
100 static int _process_block(const unsigned char *in, unsigned char *out, const ushort16 *m_key)
85 d_key[i*6+0] = s_mul_inv(e_key[(LTC_IDEA_ROUNDS-i)*6+0]);
86 d_key[i*6+1] = s_add_inv(e_key[(LTC_IDEA_ROUNDS-i)*6+1+(i>0 ? 1 : 0)]);
87 d_key[i*6+2] = s_add_inv(e_key[(LTC_IDEA_ROUNDS-i)*6+2-(i>0 ? 1 : 0)]);
88 d_key[i*6+3] = s_mul_inv(e_key[(LTC_IDEA_ROUNDS-i)*6+3]);
89 d_key[i*6+4] = e_key[(LTC_IDEA_ROUNDS-1-i)*6+4];
90 d_key[i*6+5] = e_key[(LTC_IDEA_ROUNDS-1-i)*6+5];
91 }
92 d_key[i*6+0] = s_mul_inv(e_key[(LTC_IDEA_ROUNDS-i)*6+0]);
93 d_key[i*6+1] = s_add_inv(e_key[(LTC_IDEA_ROUNDS-i)*6+1]);
94 d_key[i*6+2] = s_add_inv(e_key[(LTC_IDEA_ROUNDS-i)*6+2]);
95 d_key[i*6+3] = s_mul_inv(e_key[(LTC_IDEA_ROUNDS-i)*6+3]);
96
97 return CRYPT_OK;
98 }
99
100 static int s_process_block(const unsigned char *in, unsigned char *out, const ushort16 *m_key)
101101 {
102102 int i;
103103 ushort16 x0, x1, x2, x3, t0, t1;
104104
105 _LOAD16(x0, in + 0);
106 _LOAD16(x1, in + 2);
107 _LOAD16(x2, in + 4);
108 _LOAD16(x3, in + 6);
105 LOAD16(x0, in + 0);
106 LOAD16(x1, in + 2);
107 LOAD16(x2, in + 4);
108 LOAD16(x3, in + 6);
109109
110110 for (i = 0; i < LTC_IDEA_ROUNDS; i++) {
111 _MUL(x0, m_key[i*6+0]);
111 MUL(x0, m_key[i*6+0]);
112112 x1 += m_key[i*6+1];
113113 x2 += m_key[i*6+2];
114 _MUL(x3, m_key[i*6+3]);
114 MUL(x3, m_key[i*6+3]);
115115 t0 = x0^x2;
116 _MUL(t0, m_key[i*6+4]);
116 MUL(t0, m_key[i*6+4]);
117117 t1 = t0 + (x1^x3);
118 _MUL(t1, m_key[i*6+5]);
118 MUL(t1, m_key[i*6+5]);
119119 t0 += t1;
120120 x0 ^= t1;
121121 x3 ^= t0;
124124 x2 = t0;
125125 }
126126
127 _MUL(x0, m_key[LTC_IDEA_ROUNDS*6+0]);
127 MUL(x0, m_key[LTC_IDEA_ROUNDS*6+0]);
128128 x2 += m_key[LTC_IDEA_ROUNDS*6+1];
129129 x1 += m_key[LTC_IDEA_ROUNDS*6+2];
130 _MUL(x3, m_key[LTC_IDEA_ROUNDS*6+3]);
131
132 _STORE16(x0, out + 0);
133 _STORE16(x2, out + 2);
134 _STORE16(x1, out + 4);
135 _STORE16(x3, out + 6);
130 MUL(x3, m_key[LTC_IDEA_ROUNDS*6+3]);
131
132 STORE16(x0, out + 0);
133 STORE16(x2, out + 2);
134 STORE16(x1, out + 4);
135 STORE16(x3, out + 6);
136136
137137 return CRYPT_OK;
138138 }
145145 if (num_rounds != 0 && num_rounds != 8) return CRYPT_INVALID_ROUNDS;
146146 if (keylen != 16) return CRYPT_INVALID_KEYSIZE;
147147
148 return _setup_key(key, skey);
148 return s_setup_key(key, skey);
149149 }
150150
151151 int idea_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
152152 {
153 int err = _process_block(pt, ct, skey->idea.ek);
153 int err = s_process_block(pt, ct, skey->idea.ek);
154154 #ifdef LTC_CLEAN_STACK
155155 burn_stack(sizeof(ushort16) * 6 + sizeof(int));
156156 #endif
159159
160160 int idea_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
161161 {
162 int err = _process_block(ct, pt, skey->idea.dk);
162 int err = s_process_block(ct, pt, skey->idea.dk);
163163 #ifdef LTC_CLEAN_STACK
164164 burn_stack(sizeof(ushort16) * 6 + sizeof(int));
165165 #endif
88
99 #ifdef LTC_MULTI2
1010
11 static void pi1(ulong32 *p)
11 static void s_pi1(ulong32 *p)
1212 {
1313 p[1] ^= p[0];
1414 }
1515
16 static void pi2(ulong32 *p, const ulong32 *k)
16 static void s_pi2(ulong32 *p, const ulong32 *k)
1717 {
1818 ulong32 t;
1919 t = (p[1] + k[0]) & 0xFFFFFFFFUL;
2222 p[0] ^= t;
2323 }
2424
25 static void pi3(ulong32 *p, const ulong32 *k)
25 static void s_pi3(ulong32 *p, const ulong32 *k)
2626 {
2727 ulong32 t;
2828 t = p[0] + k[1];
3434 p[1] ^= t;
3535 }
3636
37 static void pi4(ulong32 *p, const ulong32 *k)
37 static void s_pi4(ulong32 *p, const ulong32 *k)
3838 {
3939 ulong32 t;
4040 t = (p[1] + k[3]) & 0xFFFFFFFFUL;
4242 p[0] ^= t;
4343 }
4444
45 static void setup(const ulong32 *dk, const ulong32 *k, ulong32 *uk)
45 static void s_setup(const ulong32 *dk, const ulong32 *k, ulong32 *uk)
4646 {
4747 int n, t;
4848 ulong32 p[2];
5151
5252 t = 4;
5353 n = 0;
54 pi1(p);
55 pi2(p, k);
54 s_pi1(p);
55 s_pi2(p, k);
5656 uk[n++] = p[0];
57 pi3(p, k);
57 s_pi3(p, k);
5858 uk[n++] = p[1];
59 pi4(p, k);
59 s_pi4(p, k);
6060 uk[n++] = p[0];
61 pi1(p);
61 s_pi1(p);
6262 uk[n++] = p[1];
63 pi2(p, k+t);
63 s_pi2(p, k+t);
6464 uk[n++] = p[0];
65 pi3(p, k+t);
65 s_pi3(p, k+t);
6666 uk[n++] = p[1];
67 pi4(p, k+t);
67 s_pi4(p, k+t);
6868 uk[n++] = p[0];
69 pi1(p);
69 s_pi1(p);
7070 uk[n++] = p[1];
7171 }
7272
73 static void encrypt(ulong32 *p, int N, const ulong32 *uk)
73 static void s_encrypt(ulong32 *p, int N, const ulong32 *uk)
7474 {
7575 int n, t;
7676 for (t = n = 0; ; ) {
77 pi1(p); if (++n == N) break;
78 pi2(p, uk+t); if (++n == N) break;
79 pi3(p, uk+t); if (++n == N) break;
80 pi4(p, uk+t); if (++n == N) break;
77 s_pi1(p); if (++n == N) break;
78 s_pi2(p, uk+t); if (++n == N) break;
79 s_pi3(p, uk+t); if (++n == N) break;
80 s_pi4(p, uk+t); if (++n == N) break;
8181 t ^= 4;
8282 }
8383 }
8484
85 static void decrypt(ulong32 *p, int N, const ulong32 *uk)
85 static void s_decrypt(ulong32 *p, int N, const ulong32 *uk)
8686 {
8787 int n, t;
8888 for (t = 4*(((N-1)>>2)&1), n = N; ; ) {
8989 switch (n<=4 ? n : ((n-1)%4)+1) {
90 case 4: pi4(p, uk+t); --n; /* FALLTHROUGH */
91 case 3: pi3(p, uk+t); --n; /* FALLTHROUGH */
92 case 2: pi2(p, uk+t); --n; /* FALLTHROUGH */
93 case 1: pi1(p); --n; break;
90 case 4: s_pi4(p, uk+t); --n; /* FALLTHROUGH */
91 case 3: s_pi3(p, uk+t); --n; /* FALLTHROUGH */
92 case 2: s_pi2(p, uk+t); --n; /* FALLTHROUGH */
93 case 1: s_pi1(p); --n; break;
9494 case 0: return;
9595 }
9696 t ^= 4;
127127 }
128128 LOAD32H(dk[0], key + 32);
129129 LOAD32H(dk[1], key + 36);
130 setup(dk, sk, skey->multi2.uk);
130 s_setup(dk, sk, skey->multi2.uk);
131131
132132 zeromem(sk, sizeof(sk));
133133 zeromem(dk, sizeof(dk));
149149 LTC_ARGCHK(skey != NULL);
150150 LOAD32H(p[0], pt);
151151 LOAD32H(p[1], pt+4);
152 encrypt(p, skey->multi2.N, skey->multi2.uk);
152 s_encrypt(p, skey->multi2.N, skey->multi2.uk);
153153 STORE32H(p[0], ct);
154154 STORE32H(p[1], ct+4);
155155 return CRYPT_OK;
170170 LTC_ARGCHK(skey != NULL);
171171 LOAD32H(p[0], ct);
172172 LOAD32H(p[1], ct+4);
173 decrypt(p, skey->multi2.N, skey->multi2.uk);
173 s_decrypt(p, skey->multi2.N, skey->multi2.uk);
174174 STORE32H(p[0], pt);
175175 STORE32H(p[1], pt+4);
176176 return CRYPT_OK;
101101 @return CRYPT_OK if successful
102102 */
103103 #ifdef LTC_CLEAN_STACK
104 static int _noekeon_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
104 static int s_noekeon_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
105105 #else
106106 int noekeon_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
107107 #endif
141141 #ifdef LTC_CLEAN_STACK
142142 int noekeon_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
143143 {
144 int err = _noekeon_ecb_encrypt(pt, ct, skey);
144 int err = s_noekeon_ecb_encrypt(pt, ct, skey);
145145 burn_stack(sizeof(ulong32) * 5 + sizeof(int));
146146 return err;
147147 }
155155 @return CRYPT_OK if successful
156156 */
157157 #ifdef LTC_CLEAN_STACK
158 static int _noekeon_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
158 static int s_noekeon_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
159159 #else
160160 int noekeon_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
161161 #endif
194194 #ifdef LTC_CLEAN_STACK
195195 int noekeon_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
196196 {
197 int err = _noekeon_ecb_decrypt(ct, pt, skey);
197 int err = s_noekeon_ecb_decrypt(ct, pt, skey);
198198 burn_stack(sizeof(ulong32) * 5 + sizeof(int));
199199 return err;
200200 }
138138 @return CRYPT_OK if successful
139139 */
140140 #ifdef LTC_CLEAN_STACK
141 static int _rc2_ecb_encrypt( const unsigned char *pt,
141 static int s_rc2_ecb_encrypt( const unsigned char *pt,
142142 unsigned char *ct,
143143 const symmetric_key *skey)
144144 #else
199199 unsigned char *ct,
200200 const symmetric_key *skey)
201201 {
202 int err = _rc2_ecb_encrypt(pt, ct, skey);
202 int err = s_rc2_ecb_encrypt(pt, ct, skey);
203203 burn_stack(sizeof(unsigned *) + sizeof(unsigned) * 5);
204204 return err;
205205 }
216216 @return CRYPT_OK if successful
217217 */
218218 #ifdef LTC_CLEAN_STACK
219 static int _rc2_ecb_decrypt( const unsigned char *ct,
219 static int s_rc2_ecb_decrypt( const unsigned char *ct,
220220 unsigned char *pt,
221221 const symmetric_key *skey)
222222 #else
278278 unsigned char *pt,
279279 const symmetric_key *skey)
280280 {
281 int err = _rc2_ecb_decrypt(ct, pt, skey);
281 int err = s_rc2_ecb_decrypt(ct, pt, skey);
282282 burn_stack(sizeof(unsigned *) + sizeof(unsigned) * 4 + sizeof(int));
283283 return err;
284284 }
4242 @return CRYPT_OK if successful
4343 */
4444 #ifdef LTC_CLEAN_STACK
45 static int _rc5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
45 static int s_rc5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
4646 #else
4747 int rc5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
4848 #endif
103103 int rc5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
104104 {
105105 int x;
106 x = _rc5_setup(key, keylen, num_rounds, skey);
106 x = s_rc5_setup(key, keylen, num_rounds, skey);
107107 burn_stack(sizeof(ulong32) * 122 + sizeof(int));
108108 return x;
109109 }
117117 @return CRYPT_OK if successful
118118 */
119119 #ifdef LTC_CLEAN_STACK
120 static int _rc5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
120 static int s_rc5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
121121 #else
122122 int rc5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
123123 #endif
163163 #ifdef LTC_CLEAN_STACK
164164 int rc5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
165165 {
166 int err = _rc5_ecb_encrypt(pt, ct, skey);
166 int err = s_rc5_ecb_encrypt(pt, ct, skey);
167167 burn_stack(sizeof(ulong32) * 2 + sizeof(int));
168168 return err;
169169 }
177177 @return CRYPT_OK if successful
178178 */
179179 #ifdef LTC_CLEAN_STACK
180 static int _rc5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
180 static int s_rc5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
181181 #else
182182 int rc5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
183183 #endif
224224 #ifdef LTC_CLEAN_STACK
225225 int rc5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
226226 {
227 int err = _rc5_ecb_decrypt(ct, pt, skey);
227 int err = s_rc5_ecb_decrypt(ct, pt, skey);
228228 burn_stack(sizeof(ulong32) * 2 + sizeof(int));
229229 return err;
230230 }
3939 @return CRYPT_OK if successful
4040 */
4141 #ifdef LTC_CLEAN_STACK
42 static int _rc6_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
42 static int s_rc6_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
4343 #else
4444 int rc6_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
4545 #endif
9898 int rc6_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
9999 {
100100 int x;
101 x = _rc6_setup(key, keylen, num_rounds, skey);
101 x = s_rc6_setup(key, keylen, num_rounds, skey);
102102 burn_stack(sizeof(ulong32) * 122);
103103 return x;
104104 }
111111 @param skey The key as scheduled
112112 */
113113 #ifdef LTC_CLEAN_STACK
114 static int _rc6_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
114 static int s_rc6_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
115115 #else
116116 int rc6_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
117117 #endif
153153 #ifdef LTC_CLEAN_STACK
154154 int rc6_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
155155 {
156 int err = _rc6_ecb_encrypt(pt, ct, skey);
156 int err = s_rc6_ecb_encrypt(pt, ct, skey);
157157 burn_stack(sizeof(ulong32) * 6 + sizeof(int));
158158 return err;
159159 }
166166 @param skey The key as scheduled
167167 */
168168 #ifdef LTC_CLEAN_STACK
169 static int _rc6_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
169 static int s_rc6_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
170170 #else
171171 int rc6_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
172172 #endif
210210 #ifdef LTC_CLEAN_STACK
211211 int rc6_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
212212 {
213 int err = _rc6_ecb_decrypt(ct, pt, skey);
213 int err = s_rc6_ecb_decrypt(ct, pt, skey);
214214 burn_stack(sizeof(ulong32) * 6 + sizeof(int));
215215 return err;
216216 }
2323
2424 #ifdef LTC_SAFER
2525
26 #define __LTC_SAFER_TAB_C__
26 #define LTC_SAFER_TAB_C
2727 #include "safer_tab.c"
2828
2929 const struct ltc_cipher_descriptor safer_k64_desc = {
9090 /******************* Types ****************************************************/
9191
9292 #ifdef LTC_CLEAN_STACK
93 static void _Safer_Expand_Userkey(const unsigned char *userkey_1,
93 static void s_safer_expand_userkey(const unsigned char *userkey_1,
9494 const unsigned char *userkey_2,
9595 unsigned int nof_rounds,
9696 int strengthened,
9797 safer_key_t key)
9898 #else
99 static void Safer_Expand_Userkey(const unsigned char *userkey_1,
99 static void safer_expand_userkey(const unsigned char *userkey_1,
100100 const unsigned char *userkey_2,
101101 unsigned int nof_rounds,
102102 int strengthened,
159159 }
160160
161161 #ifdef LTC_CLEAN_STACK
162 static void Safer_Expand_Userkey(const unsigned char *userkey_1,
162 static void safer_expand_userkey(const unsigned char *userkey_1,
163163 const unsigned char *userkey_2,
164164 unsigned int nof_rounds,
165165 int strengthened,
166166 safer_key_t key)
167167 {
168 _Safer_Expand_Userkey(userkey_1, userkey_2, nof_rounds, strengthened, key);
168 s_safer_expand_userkey(userkey_1, userkey_2, nof_rounds, strengthened, key);
169169 burn_stack(sizeof(unsigned char) * (2 * (LTC_SAFER_BLOCK_LEN + 1)) + sizeof(unsigned int)*2);
170170 }
171171 #endif
183183 return CRYPT_INVALID_KEYSIZE;
184184 }
185185
186 Safer_Expand_Userkey(key, key, (unsigned int)(num_rounds != 0 ?num_rounds:LTC_SAFER_K64_DEFAULT_NOF_ROUNDS), 0, skey->safer.key);
186 safer_expand_userkey(key, key, (unsigned int)(num_rounds != 0 ?num_rounds:LTC_SAFER_K64_DEFAULT_NOF_ROUNDS), 0, skey->safer.key);
187187 return CRYPT_OK;
188188 }
189189
200200 return CRYPT_INVALID_KEYSIZE;
201201 }
202202
203 Safer_Expand_Userkey(key, key, (unsigned int)(num_rounds != 0 ?num_rounds:LTC_SAFER_SK64_DEFAULT_NOF_ROUNDS), 1, skey->safer.key);
203 safer_expand_userkey(key, key, (unsigned int)(num_rounds != 0 ?num_rounds:LTC_SAFER_SK64_DEFAULT_NOF_ROUNDS), 1, skey->safer.key);
204204 return CRYPT_OK;
205205 }
206206
217217 return CRYPT_INVALID_KEYSIZE;
218218 }
219219
220 Safer_Expand_Userkey(key, key+8, (unsigned int)(num_rounds != 0 ?num_rounds:LTC_SAFER_K128_DEFAULT_NOF_ROUNDS), 0, skey->safer.key);
220 safer_expand_userkey(key, key+8, (unsigned int)(num_rounds != 0 ?num_rounds:LTC_SAFER_K128_DEFAULT_NOF_ROUNDS), 0, skey->safer.key);
221221 return CRYPT_OK;
222222 }
223223
234234 return CRYPT_INVALID_KEYSIZE;
235235 }
236236
237 Safer_Expand_Userkey(key, key+8, (unsigned int)(num_rounds != 0?num_rounds:LTC_SAFER_SK128_DEFAULT_NOF_ROUNDS), 1, skey->safer.key);
238 return CRYPT_OK;
239 }
240
241 #ifdef LTC_CLEAN_STACK
242 static int _safer_ecb_encrypt(const unsigned char *pt,
237 safer_expand_userkey(key, key+8, (unsigned int)(num_rounds != 0?num_rounds:LTC_SAFER_SK128_DEFAULT_NOF_ROUNDS), 1, skey->safer.key);
238 return CRYPT_OK;
239 }
240
241 #ifdef LTC_CLEAN_STACK
242 static int s_safer_ecb_encrypt(const unsigned char *pt,
243243 unsigned char *ct,
244244 const symmetric_key *skey)
245245 #else
286286 unsigned char *ct,
287287 const symmetric_key *skey)
288288 {
289 int err = _safer_ecb_encrypt(pt, ct, skey);
289 int err = s_safer_ecb_encrypt(pt, ct, skey);
290290 burn_stack(sizeof(unsigned char) * 9 + sizeof(unsigned int) + sizeof(unsigned char *));
291291 return err;
292292 }
293293 #endif
294294
295295 #ifdef LTC_CLEAN_STACK
296 static int _safer_ecb_decrypt(const unsigned char *ct,
296 static int s_safer_ecb_decrypt(const unsigned char *ct,
297297 unsigned char *pt,
298298 const symmetric_key *skey)
299299 #else
341341 unsigned char *pt,
342342 const symmetric_key *skey)
343343 {
344 int err = _safer_ecb_decrypt(ct, pt, skey);
344 int err = s_safer_ecb_decrypt(ct, pt, skey);
345345 burn_stack(sizeof(unsigned char) * 9 + sizeof(unsigned int) + sizeof(unsigned char *));
346346 return err;
347347 }
55 Tables for LTC_SAFER block ciphers
66 */
77
8 #ifdef __LTC_SAFER_TAB_C__
8 #ifdef LTC_SAFER_TAB_C
99
1010 /* This is the box defined by ebox[x] = 45^x mod 257.
1111 * Its assumed that the value "256" corresponds to zero. */
4848 184, 64, 120, 45, 58, 233, 100, 31, 146, 144, 125, 57, 111, 224, 137, 48
4949 };
5050
51 #endif /* __LTC_SAFER_TAB_C__ */
51 #endif /* LTC_SAFER_TAB_C */
5252
5353
88
99 #ifdef LTC_SAFERP
1010
11 #define __LTC_SAFER_TAB_C__
11 #define LTC_SAFER_TAB_C
1212 #include "safer_tab.c"
1313
1414 const struct ltc_cipher_descriptor saferp_desc =
136136
137137 #ifdef LTC_SMALL_CODE
138138
139 static void _round(unsigned char *b, int i, const symmetric_key *skey)
139 static void s_round(unsigned char *b, int i, const symmetric_key *skey)
140140 {
141141 ROUND(b, i);
142142 }
143143
144 static void _iround(unsigned char *b, int i, const symmetric_key *skey)
144 static void s_iround(unsigned char *b, int i, const symmetric_key *skey)
145145 {
146146 iROUND(b, i);
147147 }
148148
149 static void _lt(unsigned char *b, unsigned char *b2)
149 static void s_lt(unsigned char *b, unsigned char *b2)
150150 {
151151 LT(b, b2);
152152 }
153153
154 static void _ilt(unsigned char *b, unsigned char *b2)
154 static void s_ilt(unsigned char *b, unsigned char *b2)
155155 {
156156 iLT(b, b2);
157157 }
158158
159159 #undef ROUND
160 #define ROUND(b, i) _round(b, i, skey)
160 #define ROUND(b, i) s_round(b, i, skey)
161161
162162 #undef iROUND
163 #define iROUND(b, i) _iround(b, i, skey)
163 #define iROUND(b, i) s_iround(b, i, skey)
164164
165165 #undef LT
166 #define LT(b, b2) _lt(b, b2)
166 #define LT(b, b2) s_lt(b, b2)
167167
168168 #undef iLT
169 #define iLT(b, b2) _ilt(b, b2)
169 #define iLT(b, b2) s_ilt(b, b2)
170170
171171 #endif
172172
2626 };
2727
2828 /* linear transformation */
29 #define _LT(i,a,b,c,d,e) { \
29 #define s_lt(i,a,b,c,d,e) { \
3030 a = ROLc(a, 13); \
3131 c = ROLc(c, 3); \
3232 d = ROLc(d ^ c ^ (a << 3), 7); \
3636 }
3737
3838 /* inverse linear transformation */
39 #define _ILT(i,a,b,c,d,e) { \
39 #define s_ilt(i,a,b,c,d,e) { \
4040 c = RORc(c, 22); \
4141 a = RORc(a, 5); \
4242 c ^= d ^ (b << 7); \
4949 }
5050
5151 /* order of output from S-box functions */
52 #define _beforeS0(f) f(0,a,b,c,d,e)
53 #define _afterS0(f) f(1,b,e,c,a,d)
54 #define _afterS1(f) f(2,c,b,a,e,d)
55 #define _afterS2(f) f(3,a,e,b,d,c)
56 #define _afterS3(f) f(4,e,b,d,c,a)
57 #define _afterS4(f) f(5,b,a,e,c,d)
58 #define _afterS5(f) f(6,a,c,b,e,d)
59 #define _afterS6(f) f(7,a,c,d,b,e)
60 #define _afterS7(f) f(8,d,e,b,a,c)
52 #define s_beforeS0(f) f(0,a,b,c,d,e)
53 #define s_afterS0(f) f(1,b,e,c,a,d)
54 #define s_afterS1(f) f(2,c,b,a,e,d)
55 #define s_afterS2(f) f(3,a,e,b,d,c)
56 #define s_afterS3(f) f(4,e,b,d,c,a)
57 #define s_afterS4(f) f(5,b,a,e,c,d)
58 #define s_afterS5(f) f(6,a,c,b,e,d)
59 #define s_afterS6(f) f(7,a,c,d,b,e)
60 #define s_afterS7(f) f(8,d,e,b,a,c)
6161
6262 /* order of output from inverse S-box functions */
63 #define _beforeI7(f) f(8,a,b,c,d,e)
64 #define _afterI7(f) f(7,d,a,b,e,c)
65 #define _afterI6(f) f(6,a,b,c,e,d)
66 #define _afterI5(f) f(5,b,d,e,c,a)
67 #define _afterI4(f) f(4,b,c,e,a,d)
68 #define _afterI3(f) f(3,a,b,e,c,d)
69 #define _afterI2(f) f(2,b,d,e,c,a)
70 #define _afterI1(f) f(1,a,b,c,e,d)
71 #define _afterI0(f) f(0,a,d,b,e,c)
63 #define s_beforeI7(f) f(8,a,b,c,d,e)
64 #define s_afterI7(f) f(7,d,a,b,e,c)
65 #define s_afterI6(f) f(6,a,b,c,e,d)
66 #define s_afterI5(f) f(5,b,d,e,c,a)
67 #define s_afterI4(f) f(4,b,c,e,a,d)
68 #define s_afterI3(f) f(3,a,b,e,c,d)
69 #define s_afterI2(f) f(2,b,d,e,c,a)
70 #define s_afterI1(f) f(1,a,b,c,e,d)
71 #define s_afterI0(f) f(0,a,d,b,e,c)
7272
7373 /* The instruction sequences for the S-box functions
7474 * come from Dag Arne Osvik's paper "Speeding up Serpent".
7575 */
7676
77 #define _S0(i, r0, r1, r2, r3, r4) { \
77 #define s_s0(i, r0, r1, r2, r3, r4) { \
7878 r3 ^= r0; \
7979 r4 = r1; \
8080 r1 &= r3; \
9595 r4 ^= r3; \
9696 }
9797
98 #define _I0(i, r0, r1, r2, r3, r4) { \
98 #define s_i0(i, r0, r1, r2, r3, r4) { \
9999 r2 = ~r2; \
100100 r4 = r1; \
101101 r1 |= r0; \
117117 r4 ^= r2; \
118118 }
119119
120 #define _S1(i, r0, r1, r2, r3, r4) { \
120 #define s_s1(i, r0, r1, r2, r3, r4) { \
121121 r0 = ~r0; \
122122 r2 = ~r2; \
123123 r4 = r0; \
138138 r0 ^= r4; \
139139 }
140140
141 #define _I1(i, r0, r1, r2, r3, r4) { \
141 #define s_i1(i, r0, r1, r2, r3, r4) { \
142142 r4 = r1; \
143143 r1 ^= r3; \
144144 r3 &= r1; \
160160 r3 ^= r1; \
161161 }
162162
163 #define _S2(i, r0, r1, r2, r3, r4) { \
163 #define s_s2(i, r0, r1, r2, r3, r4) { \
164164 r4 = r0; \
165165 r0 &= r2; \
166166 r0 ^= r3; \
179179 r4 = ~r4; \
180180 }
181181
182 #define _I2(i, r0, r1, r2, r3, r4) { \
182 #define s_i2(i, r0, r1, r2, r3, r4) { \
183183 r2 ^= r3; \
184184 r3 ^= r0; \
185185 r4 = r3; \
201201 r3 ^= r0; \
202202 }
203203
204 #define _S3(i, r0, r1, r2, r3, r4) { \
204 #define s_s3(i, r0, r1, r2, r3, r4) { \
205205 r4 = r0; \
206206 r0 |= r3; \
207207 r3 ^= r1; \
223223 r1 ^= r0; \
224224 }
225225
226 #define _I3(i, r0, r1, r2, r3, r4) { \
226 #define s_i3(i, r0, r1, r2, r3, r4) { \
227227 r4 = r2; \
228228 r2 ^= r1; \
229229 r1 &= r2; \
244244 r2 ^= r4; \
245245 }
246246
247 #define _S4(i, r0, r1, r2, r3, r4) { \
247 #define s_s4(i, r0, r1, r2, r3, r4) { \
248248 r1 ^= r3; \
249249 r3 = ~r3; \
250250 r2 ^= r3; \
267267 r4 ^= r2; \
268268 }
269269
270 #define _I4(i, r0, r1, r2, r3, r4) { \
270 #define s_i4(i, r0, r1, r2, r3, r4) { \
271271 r4 = r2; \
272272 r2 &= r3; \
273273 r2 ^= r1; \
290290 r2 ^= r1; \
291291 }
292292
293 #define _S5(i, r0, r1, r2, r3, r4) { \
293 #define s_s5(i, r0, r1, r2, r3, r4) { \
294294 r0 ^= r1; \
295295 r1 ^= r3; \
296296 r3 = ~r3; \
312312 r2 ^= r4; \
313313 }
314314
315 #define _I5(i, r0, r1, r2, r3, r4) { \
315 #define s_i5(i, r0, r1, r2, r3, r4) { \
316316 r1 = ~r1; \
317317 r4 = r3; \
318318 r2 ^= r1; \
334334 r4 = ~r4; \
335335 }
336336
337 #define _S6(i, r0, r1, r2, r3, r4) { \
337 #define s_s6(i, r0, r1, r2, r3, r4) { \
338338 r2 = ~r2; \
339339 r4 = r3; \
340340 r3 &= r0; \
355355 r2 ^= r3; \
356356 }
357357
358 #define _I6(i, r0, r1, r2, r3, r4) { \
358 #define s_i6(i, r0, r1, r2, r3, r4) { \
359359 r0 ^= r2; \
360360 r4 = r2; \
361361 r2 &= r0; \
375375 r4 ^= r0; \
376376 }
377377
378 #define _S7(i, r0, r1, r2, r3, r4) { \
378 #define s_s7(i, r0, r1, r2, r3, r4) { \
379379 r4 = r2; \
380380 r2 &= r1; \
381381 r2 ^= r3; \
398398 r4 ^= r1; \
399399 }
400400
401 #define _I7(i, r0, r1, r2, r3, r4) { \
401 #define s_i7(i, r0, r1, r2, r3, r4) { \
402402 r4 = r2; \
403403 r2 ^= r0; \
404404 r0 &= r3; \
421421 }
422422
423423 /* key xor */
424 #define _KX(r, a, b, c, d, e) { \
424 #define s_kx(r, a, b, c, d, e) { \
425425 a ^= k[4 * r + 0]; \
426426 b ^= k[4 * r + 1]; \
427427 c ^= k[4 * r + 2]; \
428428 d ^= k[4 * r + 3]; \
429429 }
430430
431 #define _LK(r, a, b, c, d, e) { \
431 #define s_lk(r, a, b, c, d, e) { \
432432 a = k[(8-r)*4 + 0]; \
433433 b = k[(8-r)*4 + 1]; \
434434 c = k[(8-r)*4 + 2]; \
435435 d = k[(8-r)*4 + 3]; \
436436 }
437437
438 #define _SK(r, a, b, c, d, e) { \
438 #define s_sk(r, a, b, c, d, e) { \
439439 k[(8-r)*4 + 4] = a; \
440440 k[(8-r)*4 + 5] = b; \
441441 k[(8-r)*4 + 6] = c; \
442442 k[(8-r)*4 + 7] = d; \
443443 }
444444
445 static int _setup_key(const unsigned char *key, int keylen, int rounds, ulong32 *k)
445 static int s_setup_key(const unsigned char *key, int keylen, int rounds, ulong32 *k)
446446 {
447447 int i;
448448 ulong32 t;
466466 k -= 20;
467467
468468 for (i = 0; i < rounds/8; i++) {
469 _afterS2(_LK); _afterS2(_S3); _afterS3(_SK);
470 _afterS1(_LK); _afterS1(_S2); _afterS2(_SK);
471 _afterS0(_LK); _afterS0(_S1); _afterS1(_SK);
472 _beforeS0(_LK); _beforeS0(_S0); _afterS0(_SK);
469 s_afterS2(s_lk); s_afterS2(s_s3); s_afterS3(s_sk);
470 s_afterS1(s_lk); s_afterS1(s_s2); s_afterS2(s_sk);
471 s_afterS0(s_lk); s_afterS0(s_s1); s_afterS1(s_sk);
472 s_beforeS0(s_lk); s_beforeS0(s_s0); s_afterS0(s_sk);
473473 k += 8*4;
474 _afterS6(_LK); _afterS6(_S7); _afterS7(_SK);
475 _afterS5(_LK); _afterS5(_S6); _afterS6(_SK);
476 _afterS4(_LK); _afterS4(_S5); _afterS5(_SK);
477 _afterS3(_LK); _afterS3(_S4); _afterS4(_SK);
474 s_afterS6(s_lk); s_afterS6(s_s7); s_afterS7(s_sk);
475 s_afterS5(s_lk); s_afterS5(s_s6); s_afterS6(s_sk);
476 s_afterS4(s_lk); s_afterS4(s_s5); s_afterS5(s_sk);
477 s_afterS3(s_lk); s_afterS3(s_s4); s_afterS4(s_sk);
478478 }
479 _afterS2(_LK); _afterS2(_S3); _afterS3(_SK);
479 s_afterS2(s_lk); s_afterS2(s_s3); s_afterS3(s_sk);
480480
481481 return CRYPT_OK;
482482 }
483483
484 static int _enc_block(const unsigned char *in, unsigned char *out, const ulong32 *k)
484 static int s_enc_block(const unsigned char *in, unsigned char *out, const ulong32 *k)
485485 {
486486 ulong32 a, b, c, d, e;
487487 unsigned int i = 1;
492492 LOAD32L(d, in + 12);
493493
494494 do {
495 _beforeS0(_KX); _beforeS0(_S0); _afterS0(_LT);
496 _afterS0(_KX); _afterS0(_S1); _afterS1(_LT);
497 _afterS1(_KX); _afterS1(_S2); _afterS2(_LT);
498 _afterS2(_KX); _afterS2(_S3); _afterS3(_LT);
499 _afterS3(_KX); _afterS3(_S4); _afterS4(_LT);
500 _afterS4(_KX); _afterS4(_S5); _afterS5(_LT);
501 _afterS5(_KX); _afterS5(_S6); _afterS6(_LT);
502 _afterS6(_KX); _afterS6(_S7);
495 s_beforeS0(s_kx); s_beforeS0(s_s0); s_afterS0(s_lt);
496 s_afterS0(s_kx); s_afterS0(s_s1); s_afterS1(s_lt);
497 s_afterS1(s_kx); s_afterS1(s_s2); s_afterS2(s_lt);
498 s_afterS2(s_kx); s_afterS2(s_s3); s_afterS3(s_lt);
499 s_afterS3(s_kx); s_afterS3(s_s4); s_afterS4(s_lt);
500 s_afterS4(s_kx); s_afterS4(s_s5); s_afterS5(s_lt);
501 s_afterS5(s_kx); s_afterS5(s_s6); s_afterS6(s_lt);
502 s_afterS6(s_kx); s_afterS6(s_s7);
503503
504504 if (i == 4) break;
505505
510510 d = a;
511511 a = e;
512512 k += 32;
513 _beforeS0(_LT);
513 s_beforeS0(s_lt);
514514 } while (1);
515515
516 _afterS7(_KX);
516 s_afterS7(s_kx);
517517
518518 STORE32L(d, out + 0);
519519 STORE32L(e, out + 4);
523523 return CRYPT_OK;
524524 }
525525
526 static int _dec_block(const unsigned char *in, unsigned char *out, const ulong32 *k)
526 static int s_dec_block(const unsigned char *in, unsigned char *out, const ulong32 *k)
527527 {
528528 ulong32 a, b, c, d, e;
529529 unsigned int i;
536536 i = 4;
537537 k += 96;
538538
539 _beforeI7(_KX);
539 s_beforeI7(s_kx);
540540 goto start;
541541
542542 do {
544544 b = d;
545545 d = e;
546546 k -= 32;
547 _beforeI7(_ILT);
547 s_beforeI7(s_ilt);
548548 start:
549 _beforeI7(_I7); _afterI7(_KX);
550 _afterI7(_ILT); _afterI7(_I6); _afterI6(_KX);
551 _afterI6(_ILT); _afterI6(_I5); _afterI5(_KX);
552 _afterI5(_ILT); _afterI5(_I4); _afterI4(_KX);
553 _afterI4(_ILT); _afterI4(_I3); _afterI3(_KX);
554 _afterI3(_ILT); _afterI3(_I2); _afterI2(_KX);
555 _afterI2(_ILT); _afterI2(_I1); _afterI1(_KX);
556 _afterI1(_ILT); _afterI1(_I0); _afterI0(_KX);
549 s_beforeI7(s_i7); s_afterI7(s_kx);
550 s_afterI7(s_ilt); s_afterI7(s_i6); s_afterI6(s_kx);
551 s_afterI6(s_ilt); s_afterI6(s_i5); s_afterI5(s_kx);
552 s_afterI5(s_ilt); s_afterI5(s_i4); s_afterI4(s_kx);
553 s_afterI4(s_ilt); s_afterI4(s_i3); s_afterI3(s_kx);
554 s_afterI3(s_ilt); s_afterI3(s_i2); s_afterI2(s_kx);
555 s_afterI2(s_ilt); s_afterI2(s_i1); s_afterI1(s_kx);
556 s_afterI1(s_ilt); s_afterI1(s_i0); s_afterI0(s_kx);
557557 } while (--i != 0);
558558
559559 STORE32L(a, out + 0);
574574 if (num_rounds != 0 && num_rounds != 32) return CRYPT_INVALID_ROUNDS;
575575 if (keylen != 16 && keylen != 24 && keylen != 32) return CRYPT_INVALID_KEYSIZE;
576576
577 err = _setup_key(key, keylen, 32, skey->serpent.k);
577 err = s_setup_key(key, keylen, 32, skey->serpent.k);
578578 #ifdef LTC_CLEAN_STACK
579579 burn_stack(sizeof(ulong32) * 14 + sizeof(int));
580580 #endif
583583
584584 int serpent_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
585585 {
586 int err = _enc_block(pt, ct, skey->serpent.k);
586 int err = s_enc_block(pt, ct, skey->serpent.k);
587587 #ifdef LTC_CLEAN_STACK
588588 burn_stack(sizeof(ulong32) * 5 + sizeof(int));
589589 #endif
592592
593593 int serpent_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
594594 {
595 int err = _dec_block(ct, pt, skey->serpent.k);
595 int err = s_dec_block(ct, pt, skey->serpent.k);
596596 #ifdef LTC_CLEAN_STACK
597597 burn_stack(sizeof(ulong32) * 5 + sizeof(int));
598598 #endif
132132 @return CRYPT_OK if successful
133133 */
134134 #ifdef LTC_CLEAN_STACK
135 static int _skipjack_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
135 static int s_skipjack_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
136136 #else
137137 int skipjack_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
138138 #endif
182182 #ifdef LTC_CLEAN_STACK
183183 int skipjack_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
184184 {
185 int err = _skipjack_ecb_encrypt(pt, ct, skey);
185 int err = s_skipjack_ecb_encrypt(pt, ct, skey);
186186 burn_stack(sizeof(unsigned) * 8 + sizeof(int) * 2);
187187 return err;
188188 }
196196 @return CRYPT_OK if successful
197197 */
198198 #ifdef LTC_CLEAN_STACK
199 static int _skipjack_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
199 static int s_skipjack_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
200200 #else
201201 int skipjack_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
202202 #endif
250250 #ifdef LTC_CLEAN_STACK
251251 int skipjack_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
252252 {
253 int err = _skipjack_ecb_decrypt(ct, pt, skey);
253 int err = s_skipjack_ecb_decrypt(ct, pt, skey);
254254 burn_stack(sizeof(unsigned) * 7 + sizeof(int) * 2);
255255 return err;
256256 }
5757
5858 #ifdef LTC_TWOFISH_TABLES
5959
60 #define __LTC_TWOFISH_TAB_C__
60 #define LTC_TWOFISH_TAB_C
6161 #include "twofish_tab.c"
6262
6363 #define sbox(i, x) ((ulong32)SBOX[i][(x)&255])
8282
8383 /* computes S_i[x] */
8484 #ifdef LTC_CLEAN_STACK
85 static ulong32 _sbox(int i, ulong32 x)
85 static ulong32 s_sbox(int i, ulong32 x)
8686 #else
8787 static ulong32 sbox(int i, ulong32 x)
8888 #endif
124124 static ulong32 sbox(int i, ulong32 x)
125125 {
126126 ulong32 y;
127 y = _sbox(i, x);
127 y = s_sbox(i, x);
128128 burn_stack(sizeof(unsigned char) * 11);
129129 return y;
130130 }
281281 #else
282282
283283 #ifdef LTC_CLEAN_STACK
284 static ulong32 _g_func(ulong32 x, const symmetric_key *key)
284 static ulong32 s_g_func(ulong32 x, const symmetric_key *key)
285285 #else
286286 static ulong32 g_func(ulong32 x, const symmetric_key *key)
287287 #endif
317317 static ulong32 g_func(ulong32 x, const symmetric_key *key)
318318 {
319319 ulong32 y;
320 y = _g_func(x, key);
320 y = s_g_func(x, key);
321321 burn_stack(sizeof(unsigned char) * 4 + sizeof(ulong32));
322322 return y;
323323 }
334334 @return CRYPT_OK if successful
335335 */
336336 #ifdef LTC_CLEAN_STACK
337 static int _twofish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
337 static int s_twofish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
338338 #else
339339 int twofish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
340340 #endif
447447 int twofish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
448448 {
449449 int x;
450 x = _twofish_setup(key, keylen, num_rounds, skey);
450 x = s_twofish_setup(key, keylen, num_rounds, skey);
451451 burn_stack(sizeof(int) * 7 + sizeof(unsigned char) * 56 + sizeof(ulong32) * 2);
452452 return x;
453453 }
461461 @return CRYPT_OK if successful
462462 */
463463 #ifdef LTC_CLEAN_STACK
464 static int _twofish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
464 static int s_twofish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
465465 #else
466466 int twofish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
467467 #endif
521521 #ifdef LTC_CLEAN_STACK
522522 int twofish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
523523 {
524 int err = _twofish_ecb_encrypt(pt, ct, skey);
524 int err = s_twofish_ecb_encrypt(pt, ct, skey);
525525 burn_stack(sizeof(ulong32) * 10 + sizeof(int));
526526 return err;
527527 }
535535 @return CRYPT_OK if successful
536536 */
537537 #ifdef LTC_CLEAN_STACK
538 static int _twofish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
538 static int s_twofish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
539539 #else
540540 int twofish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
541541 #endif
597597 #ifdef LTC_CLEAN_STACK
598598 int twofish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
599599 {
600 int err =_twofish_ecb_decrypt(ct, pt, skey);
600 int err = s_twofish_ecb_decrypt(ct, pt, skey);
601601 burn_stack(sizeof(ulong32) * 10 + sizeof(int));
602602 return err;
603603 }
55 Twofish tables, Tom St Denis
66 */
77 #ifdef LTC_TWOFISH_TABLES
8 #ifdef __LTC_TWOFISH_TAB_C__
8 #ifdef LTC_TWOFISH_TAB_C
99
1010 /* pre generated 8x8 tables from the four 4x4s */
1111 static const unsigned char SBOX[2][256] = {
481481
482482 #endif /* LTC_TWOFISH_ALL_TABLES */
483483
484 #endif /* __LTC_TWOFISH_TAB_C__ */
484 #endif /* LTC_TWOFISH_TAB_C */
485485 #endif
5151
5252 #ifndef LTC_FAST
5353 /* right shift */
54 static void _gcm_rightshift(unsigned char *a)
54 static void s_gcm_rightshift(unsigned char *a)
5555 {
5656 int x;
5757 for (x = 15; x > 0; x--) {
8585 }
8686 }
8787 z = V[15] & 0x01;
88 _gcm_rightshift(V);
88 s_gcm_rightshift(V);
8989 V[0] ^= poly[z];
9090 }
9191 XMEMCPY(c, Z, 16);
1414 @param aad_block [in] AAD data (block_len size)
1515 @return CRYPT_OK if successful
1616 */
17 static int _ocb3_int_aad_add_block(ocb3_state *ocb, const unsigned char *aad_block)
17 static int s_ocb3_int_aad_add_block(ocb3_state *ocb, const unsigned char *aad_block)
1818 {
1919 unsigned char tmp[MAXBLOCKSIZE];
2020 int err;
5858 ocb->adata_buffer_bytes += l;
5959
6060 if (ocb->adata_buffer_bytes == ocb->block_len) {
61 if ((err = _ocb3_int_aad_add_block(ocb, ocb->adata_buffer)) != CRYPT_OK) {
61 if ((err = s_ocb3_int_aad_add_block(ocb, ocb->adata_buffer)) != CRYPT_OK) {
6262 return err;
6363 }
6464 ocb->adata_buffer_bytes = 0;
7979 last_block_len = datalen - full_blocks_len;
8080
8181 for (x=0; x<full_blocks; x++) {
82 if ((err = _ocb3_int_aad_add_block(ocb, data+x*ocb->block_len)) != CRYPT_OK) {
82 if ((err = s_ocb3_int_aad_add_block(ocb, data+x*ocb->block_len)) != CRYPT_OK) {
8383 return err;
8484 }
8585 }
88
99 #ifdef LTC_OCB3_MODE
1010
11 static void _ocb3_int_calc_offset_zero(ocb3_state *ocb, const unsigned char *nonce, unsigned long noncelen, unsigned long taglen)
11 static void s_ocb3_int_calc_offset_zero(ocb3_state *ocb, const unsigned char *nonce, unsigned long noncelen, unsigned long taglen)
1212 {
1313 int x, y, bottom;
1414 int idx, shift;
165165 }
166166
167167 /* initialize ocb->Offset_current = Offset_0 */
168 _ocb3_int_calc_offset_zero(ocb, nonce, noncelen, taglen);
168 s_ocb3_int_calc_offset_zero(ocb, nonce, noncelen, taglen);
169169
170170 /* initialize checksum to all zeros */
171171 zeromem(ocb->checksum, ocb->block_len);
146146 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }
147147 };
148148
149 static void blake2b_set_lastnode(hash_state *md) { md->blake2b.f[1] = CONST64(0xffffffffffffffff); }
149 static void s_blake2b_set_lastnode(hash_state *md) { md->blake2b.f[1] = CONST64(0xffffffffffffffff); }
150150
151151 /* Some helper functions, not necessarily useful */
152 static int blake2b_is_lastblock(const hash_state *md) { return md->blake2b.f[0] != 0; }
153
154 static void blake2b_set_lastblock(hash_state *md)
152 static int s_blake2b_is_lastblock(const hash_state *md) { return md->blake2b.f[0] != 0; }
153
154 static void s_blake2b_set_lastblock(hash_state *md)
155155 {
156156 if (md->blake2b.last_node) {
157 blake2b_set_lastnode(md);
157 s_blake2b_set_lastnode(md);
158158 }
159159 md->blake2b.f[0] = CONST64(0xffffffffffffffff);
160160 }
161161
162 static void blake2b_increment_counter(hash_state *md, ulong64 inc)
162 static void s_blake2b_increment_counter(hash_state *md, ulong64 inc)
163163 {
164164 md->blake2b.t[0] += inc;
165165 if (md->blake2b.t[0] < inc) md->blake2b.t[1]++;
166166 }
167167
168 static void blake2b_init0(hash_state *md)
168 static void s_blake2b_init0(hash_state *md)
169169 {
170170 unsigned long i;
171171 XMEMSET(&md->blake2b, 0, sizeof(md->blake2b));
176176 }
177177
178178 /* init xors IV with input parameter block */
179 static int blake2b_init_param(hash_state *md, const unsigned char *P)
179 static int s_blake2b_init_param(hash_state *md, const unsigned char *P)
180180 {
181181 unsigned long i;
182182
183 blake2b_init0(md);
183 s_blake2b_init0(md);
184184
185185 /* IV XOR ParamBlock */
186186 for (i = 0; i < 8; ++i) {
227227 P[O_FANOUT] = 1;
228228 P[O_DEPTH] = 1;
229229
230 err = blake2b_init_param(md, P);
230 err = s_blake2b_init_param(md, P);
231231 if (err != CRYPT_OK) return err;
232232
233233 if (key) {
298298 } while (0)
299299
300300 #ifdef LTC_CLEAN_STACK
301 static int _blake2b_compress(hash_state *md, const unsigned char *buf)
301 static int ss_blake2b_compress(hash_state *md, const unsigned char *buf)
302302 #else
303 static int blake2b_compress(hash_state *md, const unsigned char *buf)
303 static int s_blake2b_compress(hash_state *md, const unsigned char *buf)
304304 #endif
305305 {
306306 ulong64 m[16];
347347 #undef ROUND
348348
349349 #ifdef LTC_CLEAN_STACK
350 static int blake2b_compress(hash_state *md, const unsigned char *buf)
350 static int s_blake2b_compress(hash_state *md, const unsigned char *buf)
351351 {
352352 int err;
353 err = _blake2b_compress(md, buf);
353 err = ss_blake2b_compress(md, buf);
354354 burn_stack(sizeof(ulong64) * 32 + sizeof(unsigned long));
355355 return err;
356356 }
378378 if (inlen > fill) {
379379 md->blake2b.curlen = 0;
380380 XMEMCPY(md->blake2b.buf + (left % sizeof(md->blake2b.buf)), in, fill); /* Fill buffer */
381 blake2b_increment_counter(md, BLAKE2B_BLOCKBYTES);
382 blake2b_compress(md, md->blake2b.buf); /* Compress */
381 s_blake2b_increment_counter(md, BLAKE2B_BLOCKBYTES);
382 s_blake2b_compress(md, md->blake2b.buf); /* Compress */
383383 in += fill;
384384 inlen -= fill;
385385 while (inlen > BLAKE2B_BLOCKBYTES) {
386 blake2b_increment_counter(md, BLAKE2B_BLOCKBYTES);
387 blake2b_compress(md, in);
386 s_blake2b_increment_counter(md, BLAKE2B_BLOCKBYTES);
387 s_blake2b_compress(md, in);
388388 in += BLAKE2B_BLOCKBYTES;
389389 inlen -= BLAKE2B_BLOCKBYTES;
390390 }
411411
412412 /* if(md->blakebs.outlen != outlen) return CRYPT_INVALID_ARG; */
413413
414 if (blake2b_is_lastblock(md)) {
414 if (s_blake2b_is_lastblock(md)) {
415415 return CRYPT_ERROR;
416416 }
417417
418 blake2b_increment_counter(md, md->blake2b.curlen);
419 blake2b_set_lastblock(md);
418 s_blake2b_increment_counter(md, md->blake2b.curlen);
419 s_blake2b_set_lastblock(md);
420420 XMEMSET(md->blake2b.buf + md->blake2b.curlen, 0, BLAKE2B_BLOCKBYTES - md->blake2b.curlen); /* Padding */
421 blake2b_compress(md, md->blake2b.buf);
421 s_blake2b_compress(md, md->blake2b.buf);
422422
423423 for (i = 0; i < 8; ++i) { /* Output full hash to temp buffer */
424424 STORE64L(md->blake2b.h[i], buffer + i * 8);
138138 { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0 },
139139 };
140140
141 static void blake2s_set_lastnode(hash_state *md) { md->blake2s.f[1] = 0xffffffffUL; }
141 static void s_blake2s_set_lastnode(hash_state *md) { md->blake2s.f[1] = 0xffffffffUL; }
142142
143143 /* Some helper functions, not necessarily useful */
144 static int blake2s_is_lastblock(const hash_state *md) { return md->blake2s.f[0] != 0; }
145
146 static void blake2s_set_lastblock(hash_state *md)
144 static int s_blake2s_is_lastblock(const hash_state *md) { return md->blake2s.f[0] != 0; }
145
146 static void s_blake2s_set_lastblock(hash_state *md)
147147 {
148148 if (md->blake2s.last_node) {
149 blake2s_set_lastnode(md);
149 s_blake2s_set_lastnode(md);
150150 }
151151 md->blake2s.f[0] = 0xffffffffUL;
152152 }
153153
154 static void blake2s_increment_counter(hash_state *md, const ulong32 inc)
154 static void s_blake2s_increment_counter(hash_state *md, const ulong32 inc)
155155 {
156156 md->blake2s.t[0] += inc;
157157 if (md->blake2s.t[0] < inc) md->blake2s.t[1]++;
158158 }
159159
160 static int blake2s_init0(hash_state *md)
160 static int s_blake2s_init0(hash_state *md)
161161 {
162162 int i;
163163 XMEMSET(&md->blake2s, 0, sizeof(struct blake2s_state));
170170 }
171171
172172 /* init2 xors IV with input parameter block */
173 static int blake2s_init_param(hash_state *md, const unsigned char *P)
173 static int s_blake2s_init_param(hash_state *md, const unsigned char *P)
174174 {
175175 unsigned long i;
176176
177 blake2s_init0(md);
177 s_blake2s_init0(md);
178178
179179 /* IV XOR ParamBlock */
180180 for (i = 0; i < 8; ++i) {
221221 P[O_FANOUT] = 1;
222222 P[O_DEPTH] = 1;
223223
224 err = blake2s_init_param(md, P);
224 err = s_blake2s_init_param(md, P);
225225 if (err != CRYPT_OK) return err;
226226
227227 if (key) {
290290 } while (0)
291291
292292 #ifdef LTC_CLEAN_STACK
293 static int _blake2s_compress(hash_state *md, const unsigned char *buf)
293 static int ss_blake2s_compress(hash_state *md, const unsigned char *buf)
294294 #else
295 static int blake2s_compress(hash_state *md, const unsigned char *buf)
295 static int s_blake2s_compress(hash_state *md, const unsigned char *buf)
296296 #endif
297297 {
298298 unsigned long i;
336336 #undef ROUND
337337
338338 #ifdef LTC_CLEAN_STACK
339 static int blake2s_compress(hash_state *md, const unsigned char *buf)
339 static int s_blake2s_compress(hash_state *md, const unsigned char *buf)
340340 {
341341 int err;
342 err = _blake2s_compress(md, buf);
342 err = ss_blake2s_compress(md, buf);
343343 burn_stack(sizeof(ulong32) * (32) + sizeof(unsigned long));
344344 return err;
345345 }
367367 if (inlen > fill) {
368368 md->blake2s.curlen = 0;
369369 XMEMCPY(md->blake2s.buf + (left % sizeof(md->blake2s.buf)), in, fill); /* Fill buffer */
370 blake2s_increment_counter(md, BLAKE2S_BLOCKBYTES);
371 blake2s_compress(md, md->blake2s.buf); /* Compress */
370 s_blake2s_increment_counter(md, BLAKE2S_BLOCKBYTES);
371 s_blake2s_compress(md, md->blake2s.buf); /* Compress */
372372 in += fill;
373373 inlen -= fill;
374374 while (inlen > BLAKE2S_BLOCKBYTES) {
375 blake2s_increment_counter(md, BLAKE2S_BLOCKBYTES);
376 blake2s_compress(md, in);
375 s_blake2s_increment_counter(md, BLAKE2S_BLOCKBYTES);
376 s_blake2s_compress(md, in);
377377 in += BLAKE2S_BLOCKBYTES;
378378 inlen -= BLAKE2S_BLOCKBYTES;
379379 }
400400
401401 /* if(md->blake2s.outlen != outlen) return CRYPT_INVALID_ARG; */
402402
403 if (blake2s_is_lastblock(md)) {
403 if (s_blake2s_is_lastblock(md)) {
404404 return CRYPT_ERROR;
405405 }
406 blake2s_increment_counter(md, md->blake2s.curlen);
407 blake2s_set_lastblock(md);
406 s_blake2s_increment_counter(md, md->blake2s.curlen);
407 s_blake2s_set_lastblock(md);
408408 XMEMSET(md->blake2s.buf + md->blake2s.curlen, 0, BLAKE2S_BLOCKBYTES - md->blake2s.curlen); /* Padding */
409 blake2s_compress(md, md->blake2s.buf);
409 s_blake2s_compress(md, md->blake2s.buf);
410410
411411 for (i = 0; i < 8; ++i) { /* Output full hash to temp buffer */
412412 STORE32L(md->blake2s.h[i], buffer + i * 4);
120120 T0 <= encrypt T0
121121 state <= state xor T0 xor T1
122122 */
123 static int chc_compress(hash_state *md, const unsigned char *buf)
123 static int s_chc_compress(hash_state *md, const unsigned char *buf)
124124 {
125125 unsigned char T[2][MAXBLOCKSIZE];
126126 symmetric_key *key;
153153 @param len The length of the data (octets)
154154 @return CRYPT_OK if successful
155155 */
156 static int _chc_process(hash_state * md, const unsigned char *in, unsigned long inlen);
157 static HASH_PROCESS(_chc_process, chc_compress, chc, (unsigned long)cipher_blocksize)
156 static int ss_chc_process(hash_state * md, const unsigned char *in, unsigned long inlen);
157 static HASH_PROCESS(ss_chc_process, s_chc_compress, chc, (unsigned long)cipher_blocksize)
158158
159159 /**
160160 Process a block of memory though the hash
178178 return CRYPT_INVALID_CIPHER;
179179 }
180180
181 return _chc_process(md, in, inlen);
181 return ss_chc_process(md, in, inlen);
182182 }
183183
184184 /**
220220 while (md->chc.curlen < (unsigned long)cipher_blocksize) {
221221 md->chc.buf[md->chc.curlen++] = (unsigned char)0;
222222 }
223 chc_compress(md, md->chc.buf);
223 s_chc_compress(md, md->chc.buf);
224224 md->chc.curlen = 0;
225225 }
226226
231231
232232 /* store length */
233233 STORE64L(md->chc.length, md->chc.buf+(cipher_blocksize-8));
234 chc_compress(md, md->chc.buf);
234 s_chc_compress(md, md->chc.buf);
235235
236236 /* copy output */
237237 XMEMCPY(out, md->chc.state, cipher_blocksize);
4848 };
4949
5050 /* adds 16 bytes to the checksum */
51 static void md2_update_chksum(hash_state *md)
51 static void s_md2_update_chksum(hash_state *md)
5252 {
5353 int j;
5454 unsigned char L;
6262 }
6363 }
6464
65 static void md2_compress(hash_state *md)
65 static void s_md2_compress(hash_state *md)
6666 {
6767 int j, k;
6868 unsigned char t;
125125
126126 /* is 16 bytes full? */
127127 if (md->md2.curlen == 16) {
128 md2_compress(md);
129 md2_update_chksum(md);
128 s_md2_compress(md);
129 s_md2_update_chksum(md);
130130 md->md2.curlen = 0;
131131 }
132132 }
158158 }
159159
160160 /* hash and update */
161 md2_compress(md);
162 md2_update_chksum(md);
161 s_md2_compress(md);
162 s_md2_update_chksum(md);
163163
164164 /* hash checksum */
165165 XMEMCPY(md->md2.buf, md->md2.chksum, 16);
166 md2_compress(md);
166 s_md2_compress(md);
167167
168168 /* output is lower 16 bytes of X */
169169 XMEMCPY(out, md->md2.X, 16);
6464 }
6565
6666 #ifdef LTC_CLEAN_STACK
67 static int _md4_compress(hash_state *md, const unsigned char *buf)
67 static int ss_md4_compress(hash_state *md, const unsigned char *buf)
6868 #else
69 static int md4_compress(hash_state *md, const unsigned char *buf)
69 static int s_md4_compress(hash_state *md, const unsigned char *buf)
7070 #endif
7171 {
7272 ulong32 x[16], a, b, c, d;
148148 }
149149
150150 #ifdef LTC_CLEAN_STACK
151 static int md4_compress(hash_state *md, const unsigned char *buf)
151 static int s_md4_compress(hash_state *md, const unsigned char *buf)
152152 {
153153 int err;
154 err = _md4_compress(md, buf);
154 err = ss_md4_compress(md, buf);
155155 burn_stack(sizeof(ulong32) * 20 + sizeof(int));
156156 return err;
157157 }
181181 @param inlen The length of the data (octets)
182182 @return CRYPT_OK if successful
183183 */
184 HASH_PROCESS(md4_process, md4_compress, md4, 64)
184 HASH_PROCESS(md4_process, s_md4_compress, md4, 64)
185185
186186 /**
187187 Terminate the hash to get the digest
214214 while (md->md4.curlen < 64) {
215215 md->md4.buf[md->md4.curlen++] = (unsigned char)0;
216216 }
217 md4_compress(md, md->md4.buf);
217 s_md4_compress(md, md->md4.buf);
218218 md->md4.curlen = 0;
219219 }
220220
225225
226226 /* store length */
227227 STORE64L(md->md4.length, md->md4.buf+56);
228 md4_compress(md, md->md4.buf);
228 s_md4_compress(md, md->md4.buf);
229229
230230 /* copy output */
231231 for (i = 0; i < 4; i++) {
8989 #endif
9090
9191 #ifdef LTC_CLEAN_STACK
92 static int _md5_compress(hash_state *md, const unsigned char *buf)
92 static int ss_md5_compress(hash_state *md, const unsigned char *buf)
9393 #else
94 static int md5_compress(hash_state *md, const unsigned char *buf)
94 static int s_md5_compress(hash_state *md, const unsigned char *buf)
9595 #endif
9696 {
9797 ulong32 i, W[16], a, b, c, d;
207207 }
208208
209209 #ifdef LTC_CLEAN_STACK
210 static int md5_compress(hash_state *md, const unsigned char *buf)
210 static int s_md5_compress(hash_state *md, const unsigned char *buf)
211211 {
212212 int err;
213 err = _md5_compress(md, buf);
213 err = ss_md5_compress(md, buf);
214214 burn_stack(sizeof(ulong32) * 21);
215215 return err;
216216 }
240240 @param inlen The length of the data (octets)
241241 @return CRYPT_OK if successful
242242 */
243 HASH_PROCESS(md5_process, md5_compress, md5, 64)
243 HASH_PROCESS(md5_process, s_md5_compress, md5, 64)
244244
245245 /**
246246 Terminate the hash to get the digest
274274 while (md->md5.curlen < 64) {
275275 md->md5.buf[md->md5.curlen++] = (unsigned char)0;
276276 }
277 md5_compress(md, md->md5.buf);
277 s_md5_compress(md, md->md5.buf);
278278 md->md5.curlen = 0;
279279 }
280280
285285
286286 /* store length */
287287 STORE64L(md->md5.length, md->md5.buf+56);
288 md5_compress(md, md->md5.buf);
288 s_md5_compress(md, md->md5.buf);
289289
290290 /* copy output */
291291 for (i = 0; i < 4; i++) {
7272 (a) = ROLc((a), (s));
7373
7474 #ifdef LTC_CLEAN_STACK
75 static int _rmd128_compress(hash_state *md, const unsigned char *buf)
75 static int ss_rmd128_compress(hash_state *md, const unsigned char *buf)
7676 #else
77 static int rmd128_compress(hash_state *md, const unsigned char *buf)
77 static int s_rmd128_compress(hash_state *md, const unsigned char *buf)
7878 #endif
7979 {
8080 ulong32 aa,bb,cc,dd,aaa,bbb,ccc,ddd,X[16];
246246 }
247247
248248 #ifdef LTC_CLEAN_STACK
249 static int rmd128_compress(hash_state *md, const unsigned char *buf)
249 static int s_rmd128_compress(hash_state *md, const unsigned char *buf)
250250 {
251251 int err;
252 err = _rmd128_compress(md, buf);
252 err = ss_rmd128_compress(md, buf);
253253 burn_stack(sizeof(ulong32) * 24 + sizeof(int));
254254 return err;
255255 }
279279 @param inlen The length of the data (octets)
280280 @return CRYPT_OK if successful
281281 */
282 HASH_PROCESS(rmd128_process, rmd128_compress, rmd128, 64)
282 HASH_PROCESS(rmd128_process, s_rmd128_compress, rmd128, 64)
283283
284284 /**
285285 Terminate the hash to get the digest
313313 while (md->rmd128.curlen < 64) {
314314 md->rmd128.buf[md->rmd128.curlen++] = (unsigned char)0;
315315 }
316 rmd128_compress(md, md->rmd128.buf);
316 s_rmd128_compress(md, md->rmd128.buf);
317317 md->rmd128.curlen = 0;
318318 }
319319
324324
325325 /* store length */
326326 STORE64L(md->rmd128.length, md->rmd128.buf+56);
327 rmd128_compress(md, md->rmd128.buf);
327 s_rmd128_compress(md, md->rmd128.buf);
328328
329329 /* copy output */
330330 for (i = 0; i < 4; i++) {
9292
9393
9494 #ifdef LTC_CLEAN_STACK
95 static int _rmd160_compress(hash_state *md, const unsigned char *buf)
95 static int ss_rmd160_compress(hash_state *md, const unsigned char *buf)
9696 #else
97 static int rmd160_compress(hash_state *md, const unsigned char *buf)
97 static int s_rmd160_compress(hash_state *md, const unsigned char *buf)
9898 #endif
9999 {
100100 ulong32 aa,bb,cc,dd,ee,aaa,bbb,ccc,ddd,eee,X[16];
304304 }
305305
306306 #ifdef LTC_CLEAN_STACK
307 static int rmd160_compress(hash_state *md, const unsigned char *buf)
307 static int s_rmd160_compress(hash_state *md, const unsigned char *buf)
308308 {
309309 int err;
310 err = _rmd160_compress(md, buf);
310 err = ss_rmd160_compress(md, buf);
311311 burn_stack(sizeof(ulong32) * 26 + sizeof(int));
312312 return err;
313313 }
338338 @param inlen The length of the data (octets)
339339 @return CRYPT_OK if successful
340340 */
341 HASH_PROCESS(rmd160_process, rmd160_compress, rmd160, 64)
341 HASH_PROCESS(rmd160_process, s_rmd160_compress, rmd160, 64)
342342
343343 /**
344344 Terminate the hash to get the digest
372372 while (md->rmd160.curlen < 64) {
373373 md->rmd160.buf[md->rmd160.curlen++] = (unsigned char)0;
374374 }
375 rmd160_compress(md, md->rmd160.buf);
375 s_rmd160_compress(md, md->rmd160.buf);
376376 md->rmd160.curlen = 0;
377377 }
378378
383383
384384 /* store length */
385385 STORE64L(md->rmd160.length, md->rmd160.buf+56);
386 rmd160_compress(md, md->rmd160.buf);
386 s_rmd160_compress(md, md->rmd160.buf);
387387
388388 /* copy output */
389389 for (i = 0; i < 5; i++) {
6666 (a) = ROLc((a), (s));
6767
6868 #ifdef LTC_CLEAN_STACK
69 static int _rmd256_compress(hash_state *md, const unsigned char *buf)
69 static int ss_rmd256_compress(hash_state *md, const unsigned char *buf)
7070 #else
71 static int rmd256_compress(hash_state *md, const unsigned char *buf)
71 static int s_rmd256_compress(hash_state *md, const unsigned char *buf)
7272 #endif
7373 {
7474 ulong32 aa,bb,cc,dd,aaa,bbb,ccc,ddd,tmp,X[16];
255255 }
256256
257257 #ifdef LTC_CLEAN_STACK
258 static int rmd256_compress(hash_state *md, const unsigned char *buf)
258 static int s_rmd256_compress(hash_state *md, const unsigned char *buf)
259259 {
260260 int err;
261 err = _rmd256_compress(md, buf);
261 err = ss_rmd256_compress(md, buf);
262262 burn_stack(sizeof(ulong32) * 25 + sizeof(int));
263263 return err;
264264 }
292292 @param inlen The length of the data (octets)
293293 @return CRYPT_OK if successful
294294 */
295 HASH_PROCESS(rmd256_process, rmd256_compress, rmd256, 64)
295 HASH_PROCESS(rmd256_process, s_rmd256_compress, rmd256, 64)
296296
297297 /**
298298 Terminate the hash to get the digest
326326 while (md->rmd256.curlen < 64) {
327327 md->rmd256.buf[md->rmd256.curlen++] = (unsigned char)0;
328328 }
329 rmd256_compress(md, md->rmd256.buf);
329 s_rmd256_compress(md, md->rmd256.buf);
330330 md->rmd256.curlen = 0;
331331 }
332332
337337
338338 /* store length */
339339 STORE64L(md->rmd256.length, md->rmd256.buf+56);
340 rmd256_compress(md, md->rmd256.buf);
340 s_rmd256_compress(md, md->rmd256.buf);
341341
342342 /* copy output */
343343 for (i = 0; i < 8; i++) {
8787
8888
8989 #ifdef LTC_CLEAN_STACK
90 static int _rmd320_compress(hash_state *md, const unsigned char *buf)
90 static int ss_rmd320_compress(hash_state *md, const unsigned char *buf)
9191 #else
92 static int rmd320_compress(hash_state *md, const unsigned char *buf)
92 static int s_rmd320_compress(hash_state *md, const unsigned char *buf)
9393 #endif
9494 {
9595 ulong32 aa,bb,cc,dd,ee,aaa,bbb,ccc,ddd,eee,tmp,X[16];
318318 }
319319
320320 #ifdef LTC_CLEAN_STACK
321 static int rmd320_compress(hash_state *md, const unsigned char *buf)
321 static int s_rmd320_compress(hash_state *md, const unsigned char *buf)
322322 {
323323 int err;
324 err = _rmd320_compress(md, buf);
324 err = ss_rmd320_compress(md, buf);
325325 burn_stack(sizeof(ulong32) * 27 + sizeof(int));
326326 return err;
327327 }
357357 @param inlen The length of the data (octets)
358358 @return CRYPT_OK if successful
359359 */
360 HASH_PROCESS(rmd320_process, rmd320_compress, rmd320, 64)
360 HASH_PROCESS(rmd320_process, s_rmd320_compress, rmd320, 64)
361361
362362 /**
363363 Terminate the hash to get the digest
391391 while (md->rmd320.curlen < 64) {
392392 md->rmd320.buf[md->rmd320.curlen++] = (unsigned char)0;
393393 }
394 rmd320_compress(md, md->rmd320.buf);
394 s_rmd320_compress(md, md->rmd320.buf);
395395 md->rmd320.curlen = 0;
396396 }
397397
402402
403403 /* store length */
404404 STORE64L(md->rmd320.length, md->rmd320.buf+56);
405 rmd320_compress(md, md->rmd320.buf);
405 s_rmd320_compress(md, md->rmd320.buf);
406406
407407 /* copy output */
408408 for (i = 0; i < 10; i++) {
3333 #define F3(x,y,z) (x ^ y ^ z)
3434
3535 #ifdef LTC_CLEAN_STACK
36 static int _sha1_compress(hash_state *md, const unsigned char *buf)
36 static int ss_sha1_compress(hash_state *md, const unsigned char *buf)
3737 #else
38 static int sha1_compress(hash_state *md, const unsigned char *buf)
38 static int s_sha1_compress(hash_state *md, const unsigned char *buf)
3939 #endif
4040 {
4141 ulong32 a,b,c,d,e,W[80],i;
139139 }
140140
141141 #ifdef LTC_CLEAN_STACK
142 static int sha1_compress(hash_state *md, const unsigned char *buf)
142 static int s_sha1_compress(hash_state *md, const unsigned char *buf)
143143 {
144144 int err;
145 err = _sha1_compress(md, buf);
145 err = ss_sha1_compress(md, buf);
146146 burn_stack(sizeof(ulong32) * 87);
147147 return err;
148148 }
173173 @param inlen The length of the data (octets)
174174 @return CRYPT_OK if successful
175175 */
176 HASH_PROCESS(sha1_process, sha1_compress, sha1, 64)
176 HASH_PROCESS(sha1_process, s_sha1_compress, sha1, 64)
177177
178178 /**
179179 Terminate the hash to get the digest
206206 while (md->sha1.curlen < 64) {
207207 md->sha1.buf[md->sha1.curlen++] = (unsigned char)0;
208208 }
209 sha1_compress(md, md->sha1.buf);
209 s_sha1_compress(md, md->sha1.buf);
210210 md->sha1.curlen = 0;
211211 }
212212
217217
218218 /* store length */
219219 STORE64H(md->sha1.length, md->sha1.buf+56);
220 sha1_compress(md, md->sha1.buf);
220 s_sha1_compress(md, md->sha1.buf);
221221
222222 /* copy output */
223223 for (i = 0; i < 5; i++) {
5757
5858 /* compress 512-bits */
5959 #ifdef LTC_CLEAN_STACK
60 static int _sha256_compress(hash_state * md, const unsigned char *buf)
60 static int ss_sha256_compress(hash_state * md, const unsigned char *buf)
6161 #else
62 static int sha256_compress(hash_state * md, const unsigned char *buf)
62 static int s_sha256_compress(hash_state * md, const unsigned char *buf)
6363 #endif
6464 {
6565 ulong32 S[8], W[64], t0, t1;
167167 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506ceb);
168168 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7);
169169 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2);
170
170 #endif
171171 #undef RND
172
173 #endif
174172
175173 /* feedback */
176174 for (i = 0; i < 8; i++) {
180178 }
181179
182180 #ifdef LTC_CLEAN_STACK
183 static int sha256_compress(hash_state * md, const unsigned char *buf)
181 static int s_sha256_compress(hash_state * md, const unsigned char *buf)
184182 {
185183 int err;
186 err = _sha256_compress(md, buf);
184 err = ss_sha256_compress(md, buf);
187185 burn_stack(sizeof(ulong32) * 74);
188186 return err;
189187 }
218216 @param inlen The length of the data (octets)
219217 @return CRYPT_OK if successful
220218 */
221 HASH_PROCESS(sha256_process, sha256_compress, sha256, 64)
219 HASH_PROCESS(sha256_process,s_sha256_compress, sha256, 64)
222220
223221 /**
224222 Terminate the hash to get the digest
252250 while (md->sha256.curlen < 64) {
253251 md->sha256.buf[md->sha256.curlen++] = (unsigned char)0;
254252 }
255 sha256_compress(md, md->sha256.buf);
253 s_sha256_compress(md, md->sha256.buf);
256254 md->sha256.curlen = 0;
257255 }
258256
263261
264262 /* store length */
265263 STORE64H(md->sha256.length, md->sha256.buf+56);
266 sha256_compress(md, md->sha256.buf);
264 s_sha256_compress(md, md->sha256.buf);
267265
268266 /* copy output */
269267 for (i = 0; i < 8; i++) {
8282
8383 /* compress 1024-bits */
8484 #ifdef LTC_CLEAN_STACK
85 static int _sha512_compress(hash_state * md, const unsigned char *buf)
85 static int ss_sha512_compress(hash_state * md, const unsigned char *buf)
8686 #else
87 static int sha512_compress(hash_state * md, const unsigned char *buf)
87 static int s_sha512_compress(hash_state * md, const unsigned char *buf)
8888 #endif
8989 {
9090 ulong64 S[8], W[80], t0, t1;
149149
150150 /* compress 1024-bits */
151151 #ifdef LTC_CLEAN_STACK
152 static int sha512_compress(hash_state * md, const unsigned char *buf)
152 static int s_sha512_compress(hash_state * md, const unsigned char *buf)
153153 {
154154 int err;
155 err = _sha512_compress(md, buf);
155 err = ss_sha512_compress(md, buf);
156156 burn_stack(sizeof(ulong64) * 90 + sizeof(int));
157157 return err;
158158 }
186186 @param inlen The length of the data (octets)
187187 @return CRYPT_OK if successful
188188 */
189 HASH_PROCESS(sha512_process, sha512_compress, sha512, 128)
189 HASH_PROCESS(sha512_process, s_sha512_compress, sha512, 128)
190190
191191 /**
192192 Terminate the hash to get the digest
219219 while (md->sha512.curlen < 128) {
220220 md->sha512.buf[md->sha512.curlen++] = (unsigned char)0;
221221 }
222 sha512_compress(md, md->sha512.buf);
222 s_sha512_compress(md, md->sha512.buf);
223223 md->sha512.curlen = 0;
224224 }
225225
233233
234234 /* store length */
235235 STORE64H(md->sha512.length, md->sha512.buf+120);
236 sha512_compress(md, md->sha512.buf);
236 s_sha512_compress(md, md->sha512.buf);
237237
238238 /* copy output */
239239 for (i = 0; i < 8; i++) {
130130 #define SHA3_KECCAK_SPONGE_WORDS 25 /* 1600 bits > 200 bytes > 25 x ulong64 */
131131 #define SHA3_KECCAK_ROUNDS 24
132132
133 static const ulong64 keccakf_rndc[24] = {
133 static const ulong64 s_keccakf_rndc[24] = {
134134 CONST64(0x0000000000000001), CONST64(0x0000000000008082),
135135 CONST64(0x800000000000808a), CONST64(0x8000000080008000),
136136 CONST64(0x000000000000808b), CONST64(0x0000000080000001),
145145 CONST64(0x0000000080000001), CONST64(0x8000000080008008)
146146 };
147147
148 static const unsigned keccakf_rotc[24] = {
148 static const unsigned s_keccakf_rotc[24] = {
149149 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 2, 14, 27, 41, 56, 8, 25, 43, 62, 18, 39, 61, 20, 44
150150 };
151151
152 static const unsigned keccakf_piln[24] = {
152 static const unsigned s_keccakf_piln[24] = {
153153 10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4, 15, 23, 19, 13, 12, 2, 20, 14, 22, 9, 6, 1
154154 };
155155
156 static void keccakf(ulong64 s[25])
156 static void s_keccakf(ulong64 s[25])
157157 {
158158 int i, j, round;
159159 ulong64 t, bc[5];
172172 /* Rho Pi */
173173 t = s[1];
174174 for(i = 0; i < 24; i++) {
175 j = keccakf_piln[i];
175 j = s_keccakf_piln[i];
176176 bc[0] = s[j];
177 s[j] = ROL64(t, keccakf_rotc[i]);
177 s[j] = ROL64(t, s_keccakf_rotc[i]);
178178 t = bc[0];
179179 }
180180 /* Chi */
187187 }
188188 }
189189 /* Iota */
190 s[0] ^= keccakf_rndc[round];
191 }
192 }
193
194 static LTC_INLINE int _done(hash_state *md, unsigned char *hash, ulong64 pad)
190 s[0] ^= s_keccakf_rndc[round];
191 }
192 }
193
194 static LTC_INLINE int ss_done(hash_state *md, unsigned char *hash, ulong64 pad)
195195 {
196196 unsigned i;
197197
200200
201201 md->sha3.s[md->sha3.word_index] ^= (md->sha3.saved ^ (pad << (md->sha3.byte_index * 8)));
202202 md->sha3.s[SHA3_KECCAK_SPONGE_WORDS - md->sha3.capacity_words - 1] ^= CONST64(0x8000000000000000);
203 keccakf(md->sha3.s);
203 s_keccakf(md->sha3.s);
204204
205205 /* store sha3.s[] as little-endian bytes into sha3.sb */
206206 for(i = 0; i < SHA3_KECCAK_SPONGE_WORDS; i++) {
282282 md->sha3.byte_index = 0;
283283 md->sha3.saved = 0;
284284 if(++md->sha3.word_index == (SHA3_KECCAK_SPONGE_WORDS - md->sha3.capacity_words)) {
285 keccakf(md->sha3.s);
285 s_keccakf(md->sha3.s);
286286 md->sha3.word_index = 0;
287287 }
288288 }
296296 LOAD64L(t, in);
297297 md->sha3.s[md->sha3.word_index] ^= t;
298298 if(++md->sha3.word_index == (SHA3_KECCAK_SPONGE_WORDS - md->sha3.capacity_words)) {
299 keccakf(md->sha3.s);
299 s_keccakf(md->sha3.s);
300300 md->sha3.word_index = 0;
301301 }
302302 }
311311 #ifdef LTC_SHA3
312312 int sha3_done(hash_state *md, unsigned char *out)
313313 {
314 return _done(md, out, CONST64(0x06));
314 return ss_done(md, out, CONST64(0x06));
315315 }
316316 #endif
317317
318318 #ifdef LTC_KECCAK
319319 int keccak_done(hash_state *md, unsigned char *out)
320320 {
321 return _done(md, out, CONST64(0x01));
321 return ss_done(md, out, CONST64(0x01));
322322 }
323323 #endif
324324
337337 /* shake_xof operation must be done only once */
338338 md->sha3.s[md->sha3.word_index] ^= (md->sha3.saved ^ (CONST64(0x1F) << (md->sha3.byte_index * 8)));
339339 md->sha3.s[SHA3_KECCAK_SPONGE_WORDS - md->sha3.capacity_words - 1] ^= CONST64(0x8000000000000000);
340 keccakf(md->sha3.s);
340 s_keccakf(md->sha3.s);
341341 /* store sha3.s[] as little-endian bytes into sha3.sb */
342342 for(i = 0; i < SHA3_KECCAK_SPONGE_WORDS; i++) {
343343 STORE64L(md->sha3.s[i], md->sha3.sb + i * 8);
348348
349349 for (idx = 0; idx < outlen; idx++) {
350350 if(md->sha3.byte_index >= (SHA3_KECCAK_SPONGE_WORDS - md->sha3.capacity_words) * 8) {
351 keccakf(md->sha3.s);
351 s_keccakf(md->sha3.s);
352352 /* store sha3.s[] as little-endian bytes into sha3.sb */
353353 for(i = 0; i < SHA3_KECCAK_SPONGE_WORDS; i++) {
354354 STORE64L(md->sha3.s[i], md->sha3.sb + i * 8);
546546 CONST64(0xCD56D9430EA8280E) /* 1020 */, CONST64(0xC12591D7535F5065) /* 1021 */,
547547 CONST64(0xC83223F1720AEF96) /* 1022 */, CONST64(0xC3A0396F7363A51F) /* 1023 */};
548548
549 #ifdef _MSC_VER
550 #define INLINE __inline
551 #else
552 #define INLINE
553 #endif
554
555549 /* one round of the hash function */
556 INLINE static void tiger_round(ulong64 *a, ulong64 *b, ulong64 *c, ulong64 x, int mul)
550 LTC_INLINE static void tiger_round(ulong64 *a, ulong64 *b, ulong64 *c, ulong64 x, int mul)
557551 {
558552 ulong64 tmp;
559553 tmp = (*c ^= x);
567561 }
568562
569563 /* one complete pass */
570 static void pass(ulong64 *a, ulong64 *b, ulong64 *c, const ulong64 *x, int mul)
564 static void s_pass(ulong64 *a, ulong64 *b, ulong64 *c, const ulong64 *x, int mul)
571565 {
572566 tiger_round(a,b,c,x[0],mul);
573567 tiger_round(b,c,a,x[1],mul);
580574 }
581575
582576 /* The key mixing schedule */
583 static void key_schedule(ulong64 *x)
577 static void s_key_schedule(ulong64 *x)
584578 {
585579 x[0] -= x[7] ^ CONST64(0xA5A5A5A5A5A5A5A5);
586580 x[1] ^= x[0];
601595 }
602596
603597 #ifdef LTC_CLEAN_STACK
604 static int _tiger_compress(hash_state *md, const unsigned char *buf)
598 static int ss_tiger_compress(hash_state *md, const unsigned char *buf)
605599 #else
606 static int tiger_compress(hash_state *md, const unsigned char *buf)
600 static int s_tiger_compress(hash_state *md, const unsigned char *buf)
607601 #endif
608602 {
609603 ulong64 a, b, c, x[8];
617611 b = md->tiger.state[1];
618612 c = md->tiger.state[2];
619613
620 pass(&a,&b,&c,x,5);
621 key_schedule(x);
622 pass(&c,&a,&b,x,7);
623 key_schedule(x);
624 pass(&b,&c,&a,x,9);
614 s_pass(&a,&b,&c,x,5);
615 s_key_schedule(x);
616 s_pass(&c,&a,&b,x,7);
617 s_key_schedule(x);
618 s_pass(&b,&c,&a,x,9);
625619
626620 /* store state */
627621 md->tiger.state[0] = a ^ md->tiger.state[0];
632626 }
633627
634628 #ifdef LTC_CLEAN_STACK
635 static int tiger_compress(hash_state *md, const unsigned char *buf)
629 static int s_tiger_compress(hash_state *md, const unsigned char *buf)
636630 {
637631 int err;
638 err = _tiger_compress(md, buf);
632 err = ss_tiger_compress(md, buf);
639633 burn_stack(sizeof(ulong64) * 11 + sizeof(unsigned long));
640634 return err;
641635 }
664658 @param inlen The length of the data (octets)
665659 @return CRYPT_OK if successful
666660 */
667 HASH_PROCESS(tiger_process, tiger_compress, tiger, 64)
661 HASH_PROCESS(tiger_process, s_tiger_compress, tiger, 64)
668662
669663 /**
670664 Terminate the hash to get the digest
694688 while (md->tiger.curlen < 64) {
695689 md->tiger.buf[md->tiger.curlen++] = (unsigned char)0;
696690 }
697 tiger_compress(md, md->tiger.buf);
691 s_tiger_compress(md, md->tiger.buf);
698692 md->tiger.curlen = 0;
699693 }
700694
705699
706700 /* store length */
707701 STORE64L(md->tiger.length, md->tiger.buf+56);
708 tiger_compress(md, md->tiger.buf);
702 s_tiger_compress(md, md->tiger.buf);
709703
710704 /* copy output */
711705 STORE64L(md->tiger.state[0], &out[0]);
2828 };
2929
3030 /* the sboxes */
31 #define __LTC_WHIRLTAB_C__
31 #define LTC_WHIRLTAB_C
3232 #include "whirltab.c"
3333
3434 /* get a_{i,j} */
4646 SB7(GB(a, i-7, 0)))
4747
4848 #ifdef LTC_CLEAN_STACK
49 static int _whirlpool_compress(hash_state *md, const unsigned char *buf)
49 static int ss_whirlpool_compress(hash_state *md, const unsigned char *buf)
5050 #else
51 static int whirlpool_compress(hash_state *md, const unsigned char *buf)
51 static int s_whirlpool_compress(hash_state *md, const unsigned char *buf)
5252 #endif
5353 {
5454 ulong64 K[2][8], T[3][8];
102102
103103
104104 #ifdef LTC_CLEAN_STACK
105 static int whirlpool_compress(hash_state *md, const unsigned char *buf)
105 static int s_whirlpool_compress(hash_state *md, const unsigned char *buf)
106106 {
107107 int err;
108 err = _whirlpool_compress(md, buf);
108 err = ss_whirlpool_compress(md, buf);
109109 burn_stack((5 * 8 * sizeof(ulong64)) + (2 * sizeof(int)));
110110 return err;
111111 }
131131 @param inlen The length of the data (octets)
132132 @return CRYPT_OK if successful
133133 */
134 HASH_PROCESS(whirlpool_process, whirlpool_compress, whirlpool, 64)
134 HASH_PROCESS(whirlpool_process, s_whirlpool_compress, whirlpool, 64)
135135
136136 /**
137137 Terminate the hash to get the digest
164164 while (md->whirlpool.curlen < 64) {
165165 md->whirlpool.buf[md->whirlpool.curlen++] = (unsigned char)0;
166166 }
167 whirlpool_compress(md, md->whirlpool.buf);
167 s_whirlpool_compress(md, md->whirlpool.buf);
168168 md->whirlpool.curlen = 0;
169169 }
170170
175175
176176 /* store length */
177177 STORE64H(md->whirlpool.length, md->whirlpool.buf+56);
178 whirlpool_compress(md, md->whirlpool.buf);
178 s_whirlpool_compress(md, md->whirlpool.buf);
179179
180180 /* copy output */
181181 for (i = 0; i < 8; i++) {
55 LTC_WHIRLPOOL tables, Tom St Denis
66 */
77
8 #ifdef __LTC_WHIRLTAB_C__
8 #ifdef LTC_WHIRLTAB_C
99
1010 static const ulong64 sbox0[] = {
1111 CONST64(0x18186018c07830d8), CONST64(0x23238c2305af4626), CONST64(0xc6c63fc67ef991b8), CONST64(0xe8e887e8136fcdfb),
582582 CONST64(0x6302aa71c81949d9),
583583 };
584584
585 #endif /* __LTC_WHIRLTAB_C__ */
585 #endif /* LTC_WHIRLTAB_C */
584584 #define LTC_PBES
585585 #endif
586586
587 #if defined(LTC_PBES) && !defined(LTC_PKCS_5)
588 #error LTC_PBES requires LTC_PKCS_5
589 #endif
590
591 #if defined(LTC_PBES) && !defined(LTC_PKCS_12)
592 #error LTC_PBES requires LTC_PKCS_12
593 #endif
594
595 #if defined(LTC_PKCS_5) && !defined(LTC_HMAC)
596 #error LTC_PKCS_5 requires LTC_HMAC
597 #endif
598
599 #if defined(LTC_PKCS_5) && !defined(LTC_HASH_HELPERS)
600 #error LTC_PKCS_5 requires LTC_HASH_HELPERS
601 #endif
602
587603 #if defined(LTC_PELICAN) && !defined(LTC_RIJNDAEL)
588604 #error Pelican-MAC requires LTC_RIJNDAEL
589605 #endif
5353 #ifdef LTC_HAVE_BSWAP_BUILTIN
5454
5555 #define STORE32H(x, y) \
56 do { ulong32 __t = __builtin_bswap32 ((x)); \
57 XMEMCPY ((y), &__t, 4); } while(0)
56 do { ulong32 ttt = __builtin_bswap32 ((x)); \
57 XMEMCPY ((y), &ttt, 4); } while(0)
5858
5959 #define LOAD32H(x, y) \
6060 do { XMEMCPY (&(x), (y), 4); \
9292 #ifdef LTC_HAVE_BSWAP_BUILTIN
9393
9494 #define STORE64H(x, y) \
95 do { ulong64 __t = __builtin_bswap64 ((x)); \
96 XMEMCPY ((y), &__t, 8); } while(0)
95 do { ulong64 ttt = __builtin_bswap64 ((x)); \
96 XMEMCPY ((y), &ttt, 8); } while(0)
9797
9898 #define LOAD64H(x, y) \
9999 do { XMEMCPY (&(x), (y), 8); \
134134 #ifdef ENDIAN_32BITWORD
135135
136136 #define STORE32L(x, y) \
137 do { ulong32 __t = (x); XMEMCPY(y, &__t, 4); } while(0)
137 do { ulong32 ttt = (x); XMEMCPY(y, &ttt, 4); } while(0)
138138
139139 #define LOAD32L(x, y) \
140140 do { XMEMCPY(&(x), y, 4); } while(0)
154154 #else /* 64-bit words then */
155155
156156 #define STORE32L(x, y) \
157 do { ulong32 __t = (x); XMEMCPY(y, &__t, 4); } while(0)
157 do { ulong32 ttt = (x); XMEMCPY(y, &ttt, 4); } while(0)
158158
159159 #define LOAD32L(x, y) \
160160 do { XMEMCPY(&(x), y, 4); x &= 0xFFFFFFFF; } while(0)
161161
162162 #define STORE64L(x, y) \
163 do { ulong64 __t = (x); XMEMCPY(y, &__t, 8); } while(0)
163 do { ulong64 ttt = (x); XMEMCPY(y, &ttt, 8); } while(0)
164164
165165 #define LOAD64L(x, y) \
166166 do { XMEMCPY(&(x), y, 8); } while(0)
194194 #ifdef ENDIAN_32BITWORD
195195
196196 #define STORE32H(x, y) \
197 do { ulong32 __t = (x); XMEMCPY(y, &__t, 4); } while(0)
197 do { ulong32 ttt = (x); XMEMCPY(y, &ttt, 4); } while(0)
198198
199199 #define LOAD32H(x, y) \
200200 do { XMEMCPY(&(x), y, 4); } while(0)
214214 #else /* 64-bit words then */
215215
216216 #define STORE32H(x, y) \
217 do { ulong32 __t = (x); XMEMCPY(y, &__t, 4); } while(0)
217 do { ulong32 ttt = (x); XMEMCPY(y, &ttt, 4); } while(0)
218218
219219 #define LOAD32H(x, y) \
220220 do { XMEMCPY(&(x), y, 4); x &= 0xFFFFFFFF; } while(0)
221221
222222 #define STORE64H(x, y) \
223 do { ulong64 __t = (x); XMEMCPY(y, &__t, 8); } while(0)
223 do { ulong64 ttt = (x); XMEMCPY(y, &ttt, 8); } while(0)
224224
225225 #define LOAD64H(x, y) \
226226 do { XMEMCPY(&(x), y, 8); } while(0)
274274 #ifndef LTC_NO_ROLC
275275
276276 #define ROLc(word,i) ({ \
277 ulong32 __ROLc_tmp = (word); \
277 ulong32 ROLc_tmp = (word); \
278278 __asm__ ("roll %2, %0" : \
279 "=r" (__ROLc_tmp) : \
280 "0" (__ROLc_tmp), \
279 "=r" (ROLc_tmp) : \
280 "0" (ROLc_tmp), \
281281 "I" (i)); \
282 __ROLc_tmp; \
282 ROLc_tmp; \
283283 })
284284 #define RORc(word,i) ({ \
285 ulong32 __RORc_tmp = (word); \
285 ulong32 RORc_tmp = (word); \
286286 __asm__ ("rorl %2, %0" : \
287 "=r" (__RORc_tmp) : \
288 "0" (__RORc_tmp), \
287 "=r" (RORc_tmp) : \
288 "0" (RORc_tmp), \
289289 "I" (i)); \
290 __RORc_tmp; \
290 RORc_tmp; \
291291 })
292292
293293 #else
392392 #ifndef LTC_NO_ROLC
393393
394394 #define ROL64c(word,i) ({ \
395 ulong64 __ROL64c_tmp = word; \
395 ulong64 ROL64c_tmp = word; \
396396 __asm__ ("rolq %2, %0" : \
397 "=r" (__ROL64c_tmp) : \
398 "0" (__ROL64c_tmp), \
397 "=r" (ROL64c_tmp) : \
398 "0" (ROL64c_tmp), \
399399 "J" (i)); \
400 __ROL64c_tmp; \
400 ROL64c_tmp; \
401401 })
402402 #define ROR64c(word,i) ({ \
403 ulong64 __ROR64c_tmp = word; \
403 ulong64 ROR64c_tmp = word; \
404404 __asm__ ("rorq %2, %0" : \
405 "=r" (__ROR64c_tmp) : \
406 "0" (__ROR64c_tmp), \
405 "=r" (ROR64c_tmp) : \
406 "0" (ROR64c_tmp), \
407407 "J" (i)); \
408 __ROR64c_tmp; \
408 ROR64c_tmp; \
409409 })
410410
411411 #else /* LTC_NO_ROLC */
5454 void rsa_free(rsa_key *key);
5555
5656 /* These use PKCS #1 v2.0 padding */
57 #define rsa_encrypt_key(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _prng, _prng_idx, _hash_idx, _key) \
58 rsa_encrypt_key_ex(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _prng, _prng_idx, _hash_idx, LTC_PKCS_1_OAEP, _key)
59
60 #define rsa_decrypt_key(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash_idx, _stat, _key) \
61 rsa_decrypt_key_ex(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash_idx, LTC_PKCS_1_OAEP, _stat, _key)
62
63 #define rsa_sign_hash(_in, _inlen, _out, _outlen, _prng, _prng_idx, _hash_idx, _saltlen, _key) \
64 rsa_sign_hash_ex(_in, _inlen, _out, _outlen, LTC_PKCS_1_PSS, _prng, _prng_idx, _hash_idx, _saltlen, _key)
65
66 #define rsa_verify_hash(_sig, _siglen, _hash, _hashlen, _hash_idx, _saltlen, _stat, _key) \
67 rsa_verify_hash_ex(_sig, _siglen, _hash, _hashlen, LTC_PKCS_1_PSS, _hash_idx, _saltlen, _stat, _key)
68
69 #define rsa_sign_saltlen_get_max(_hash_idx, _key) \
70 rsa_sign_saltlen_get_max_ex(LTC_PKCS_1_PSS, _hash_idx, _key)
57 #define rsa_encrypt_key(in, inlen, out, outlen, lparam, lparamlen, prng, prng_idx, hash_idx, key) \
58 rsa_encrypt_key_ex(in, inlen, out, outlen, lparam, lparamlen, prng, prng_idx, hash_idx, LTC_PKCS_1_OAEP, key)
59
60 #define rsa_decrypt_key(in, inlen, out, outlen, lparam, lparamlen, hash_idx, stat, key) \
61 rsa_decrypt_key_ex(in, inlen, out, outlen, lparam, lparamlen, hash_idx, LTC_PKCS_1_OAEP, stat, key)
62
63 #define rsa_sign_hash(in, inlen, out, outlen, prng, prng_idx, hash_idx, saltlen, key) \
64 rsa_sign_hash_ex(in, inlen, out, outlen, LTC_PKCS_1_PSS, prng, prng_idx, hash_idx, saltlen, key)
65
66 #define rsa_verify_hash(sig, siglen, hash, hashlen, hash_idx, saltlen, stat, key) \
67 rsa_verify_hash_ex(sig, siglen, hash, hashlen, LTC_PKCS_1_PSS, hash_idx, saltlen, stat, key)
68
69 #define rsa_sign_saltlen_get_max(hash_idx, key) \
70 rsa_sign_saltlen_get_max_ex(LTC_PKCS_1_PSS, hash_idx, key)
7171
7272 /* These can be switched between PKCS #1 v2.x and PKCS #1 v1.5 paddings */
7373 int rsa_encrypt_key_ex(const unsigned char *in, unsigned long inlen,
539539 LTC_MACRO_list[LTC_MACRO_temp].tag = 0; \
540540 } while (0)
541541
542 #define __LTC_SET_ASN1_IDENTIFIER(list, index, Class, Pc, Tag) \
542 #define LTC_SET_ASN1_IDENTIFIER(list, index, Class, Pc, Tag) \
543543 do { \
544544 int LTC_MACRO_temp = (index); \
545545 ltc_asn1_list *LTC_MACRO_list = (list); \
553553 do { \
554554 int LTC_MACRO_temp##__LINE__ = (index); \
555555 LTC_SET_ASN1(list, LTC_MACRO_temp##__LINE__, LTC_ASN1_CUSTOM_TYPE, Data, 1); \
556 __LTC_SET_ASN1_IDENTIFIER(list, LTC_MACRO_temp##__LINE__, Class, LTC_ASN1_PC_CONSTRUCTED, Tag); \
556 LTC_SET_ASN1_IDENTIFIER(list, LTC_MACRO_temp##__LINE__, Class, LTC_ASN1_PC_CONSTRUCTED, Tag); \
557557 } while (0)
558558
559559 #define LTC_SET_ASN1_CUSTOM_PRIMITIVE(list, index, Class, Tag, Type, Data, Size) \
560560 do { \
561561 int LTC_MACRO_temp##__LINE__ = (index); \
562562 LTC_SET_ASN1(list, LTC_MACRO_temp##__LINE__, LTC_ASN1_CUSTOM_TYPE, Data, Size); \
563 __LTC_SET_ASN1_IDENTIFIER(list, LTC_MACRO_temp##__LINE__, Class, LTC_ASN1_PC_PRIMITIVE, Tag); \
563 LTC_SET_ASN1_IDENTIFIER(list, LTC_MACRO_temp##__LINE__, Class, LTC_ASN1_PC_PRIMITIVE, Tag); \
564564 list[LTC_MACRO_temp##__LINE__].used = (int)(Type); \
565565 } while (0)
566566
8989 if (md-> state_var .curlen > sizeof(md-> state_var .buf)) { \
9090 return CRYPT_INVALID_ARG; \
9191 } \
92 if ((md-> state_var .length + inlen) < md-> state_var .length) { \
92 if ((md-> state_var .length + inlen * 8) < md-> state_var .length) { \
9393 return CRYPT_HASH_OVERFLOW; \
9494 } \
9595 while (inlen > 0) { \
416416
417417 /* tomcrypt_prng.h */
418418
419 #define _LTC_PRNG_EXPORT(which) \
419 #define LTC_PRNG_EXPORT(which) \
420420 int which ## _export(unsigned char *out, unsigned long *outlen, prng_state *prng) \
421421 { \
422422 unsigned long len = which ## _desc.export_size; \
88
99 #ifdef LTC_PELICAN
1010
11 #define __LTC_AES_TAB_C__
11 #define LTC_AES_TAB_C
1212 #define ENCRYPT_ONLY
1313 #define PELI_TAB
1414 #include "../../ciphers/aes/aes_tab.c"
4444 return CRYPT_OK;
4545 }
4646
47 static void _four_rounds(pelican_state *pelmac)
47 static void s_four_rounds(pelican_state *pelmac)
4848 {
4949 ulong32 s0, s1, s2, s3, t0, t1, t2, t3;
5050 int r;
107107 for (x = 0; x < 16; x += sizeof(LTC_FAST_TYPE)) {
108108 *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)pelmac->state + x)) ^= *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)in + x));
109109 }
110 _four_rounds(pelmac);
110 s_four_rounds(pelmac);
111111 in += 16;
112112 inlen -= 16;
113113 }
117117 while (inlen--) {
118118 pelmac->state[pelmac->buflen++] ^= *in++;
119119 if (pelmac->buflen == 16) {
120 _four_rounds(pelmac);
120 s_four_rounds(pelmac);
121121 pelmac->buflen = 0;
122122 }
123123 }
141141 }
142142
143143 if (pelmac->buflen == 16) {
144 _four_rounds(pelmac);
144 s_four_rounds(pelmac);
145145 pelmac->buflen = 0;
146146 }
147147 pelmac->state[pelmac->buflen++] ^= 0x80;
1010 #ifdef LTC_POLY1305
1111
1212 /* internal only */
13 static void _poly1305_block(poly1305_state *st, const unsigned char *in, unsigned long inlen)
13 static void s_poly1305_block(poly1305_state *st, const unsigned char *in, unsigned long inlen)
1414 {
1515 const unsigned long hibit = (st->final) ? 0 : (1UL << 24); /* 1 << 128 */
1616 ulong32 r0,r1,r2,r3,r4;
134134 in += want;
135135 st->leftover += want;
136136 if (st->leftover < 16) return CRYPT_OK;
137 _poly1305_block(st, st->buffer, 16);
137 s_poly1305_block(st, st->buffer, 16);
138138 st->leftover = 0;
139139 }
140140
141141 /* process full blocks */
142142 if (inlen >= 16) {
143143 unsigned long want = (inlen & ~(16 - 1));
144 _poly1305_block(st, in, want);
144 s_poly1305_block(st, in, want);
145145 in += want;
146146 inlen -= want;
147147 }
179179 st->buffer[i++] = 1;
180180 for (; i < 16; i++) st->buffer[i] = 0;
181181 st->final = 1;
182 _poly1305_block(st, st->buffer, 16);
182 s_poly1305_block(st, st->buffer, 16);
183183 }
184184
185185 /* fully carry h */
565565 };
566566
567567 /* find a hole and free as required, return -1 if no hole found */
568 static int _find_hole(void)
568 static int s_find_hole(void)
569569 {
570570 unsigned x;
571571 int y, z;
601601 }
602602
603603 /* determine if a base is already in the cache and if so, where */
604 static int _find_base(ecc_point *g)
604 static int s_find_base(ecc_point *g)
605605 {
606606 int x;
607607 for (x = 0; x < FP_ENTRIES; x++) {
619619 }
620620
621621 /* add a new base to the cache */
622 static int _add_entry(int idx, ecc_point *g)
622 static int s_add_entry(int idx, ecc_point *g)
623623 {
624624 unsigned x, y;
625625
661661 * The algorithm builds patterns in increasing bit order by first making all
662662 * single bit input patterns, then all two bit input patterns and so on
663663 */
664 static int _build_lut(int idx, void *a, void *modulus, void *mp, void *mu)
664 static int s_build_lut(int idx, void *a, void *modulus, void *mp, void *mu)
665665 {
666666 unsigned x, y, err, bitlen, lut_gap;
667667 void *tmp;
768768 }
769769
770770 /* perform a fixed point ECC mulmod */
771 static int _accel_fp_mul(int idx, void *k, ecc_point *R, void *a, void *modulus, void *mp, int map)
771 static int s_accel_fp_mul(int idx, void *k, ecc_point *R, void *a, void *modulus, void *mp, int map)
772772 {
773773 unsigned char kb[128];
774774 int x;
891891
892892 #ifdef LTC_ECC_SHAMIR
893893 /* perform a fixed point ECC mulmod */
894 static int _accel_fp_mul2add(int idx1, int idx2,
894 static int ss_accel_fp_mul2add(int idx1, int idx2,
895895 void *kA, void *kB,
896896 ecc_point *R, void *a, void *modulus, void *mp)
897897 {
11141114 mu = NULL;
11151115 LTC_MUTEX_LOCK(&ltc_ecc_fp_lock);
11161116 /* find point */
1117 idx1 = _find_base(A);
1117 idx1 = s_find_base(A);
11181118
11191119 /* no entry? */
11201120 if (idx1 == -1) {
11211121 /* find hole and add it */
1122 if ((idx1 = _find_hole()) >= 0) {
1123 if ((err = _add_entry(idx1, A)) != CRYPT_OK) {
1122 if ((idx1 = s_find_hole()) >= 0) {
1123 if ((err = s_add_entry(idx1, A)) != CRYPT_OK) {
11241124 goto LBL_ERR;
11251125 }
11261126 }
11311131 }
11321132
11331133 /* find point */
1134 idx2 = _find_base(B);
1134 idx2 = s_find_base(B);
11351135
11361136 /* no entry? */
11371137 if (idx2 == -1) {
11381138 /* find hole and add it */
1139 if ((idx2 = _find_hole()) >= 0) {
1140 if ((err = _add_entry(idx2, B)) != CRYPT_OK) {
1139 if ((idx2 = s_find_hole()) >= 0) {
1140 if ((err = s_add_entry(idx2, B)) != CRYPT_OK) {
11411141 goto LBL_ERR;
11421142 }
11431143 }
11611161 }
11621162
11631163 /* build the LUT */
1164 if ((err = _build_lut(idx1, a, modulus, mp, mu)) != CRYPT_OK) {
1164 if ((err = s_build_lut(idx1, a, modulus, mp, mu)) != CRYPT_OK) {
11651165 goto LBL_ERR;;
11661166 }
11671167 }
11821182 }
11831183
11841184 /* build the LUT */
1185 if ((err = _build_lut(idx2, a, modulus, mp, mu)) != CRYPT_OK) {
1185 if ((err = s_build_lut(idx2, a, modulus, mp, mu)) != CRYPT_OK) {
11861186 goto LBL_ERR;;
11871187 }
11881188 }
11931193 /* compute mp */
11941194 if ((err = mp_montgomery_setup(modulus, &mp)) != CRYPT_OK) { goto LBL_ERR; }
11951195 }
1196 err = _accel_fp_mul2add(idx1, idx2, kA, kB, C, a, modulus, mp);
1196 err = ss_accel_fp_mul2add(idx1, idx2, kA, kB, C, a, modulus, mp);
11971197 } else {
11981198 err = ltc_ecc_mul2add(A, kA, B, kB, C, a, modulus);
11991199 }
12271227 mu = NULL;
12281228 LTC_MUTEX_LOCK(&ltc_ecc_fp_lock);
12291229 /* find point */
1230 idx = _find_base(G);
1230 idx = s_find_base(G);
12311231
12321232 /* no entry? */
12331233 if (idx == -1) {
12341234 /* find hole and add it */
1235 idx = _find_hole();
1235 idx = s_find_hole();
12361236
12371237 if (idx >= 0) {
1238 if ((err = _add_entry(idx, G)) != CRYPT_OK) {
1238 if ((err = s_add_entry(idx, G)) != CRYPT_OK) {
12391239 goto LBL_ERR;
12401240 }
12411241 }
12601260 }
12611261
12621262 /* build the LUT */
1263 if ((err = _build_lut(idx, a, modulus, mp, mu)) != CRYPT_OK) {
1263 if ((err = s_build_lut(idx, a, modulus, mp, mu)) != CRYPT_OK) {
12641264 goto LBL_ERR;;
12651265 }
12661266 }
12701270 /* compute mp */
12711271 if ((err = mp_montgomery_setup(modulus, &mp)) != CRYPT_OK) { goto LBL_ERR; }
12721272 }
1273 err = _accel_fp_mul(idx, k, R, a, modulus, mp, map);
1273 err = s_accel_fp_mul(idx, k, R, a, modulus, mp, map);
12741274 } else {
12751275 err = ltc_ecc_mulmod(k, G, R, a, modulus, map);
12761276 }
12861286 }
12871287
12881288 /* helper function for freeing the cache ... must be called with the cache mutex locked */
1289 static void _ltc_ecc_fp_free_cache(void)
1289 static void s_ltc_ecc_fp_free_cache(void)
12901290 {
12911291 unsigned x, y;
12921292 for (x = 0; x < FP_ENTRIES; x++) {
13111311 void ltc_ecc_fp_free(void)
13121312 {
13131313 LTC_MUTEX_LOCK(&ltc_ecc_fp_lock);
1314 _ltc_ecc_fp_free_cache();
1314 s_ltc_ecc_fp_free_cache();
13151315 LTC_MUTEX_UNLOCK(&ltc_ecc_fp_lock);
13161316 }
13171317
13301330 void *mu = NULL;
13311331
13321332 LTC_MUTEX_LOCK(&ltc_ecc_fp_lock);
1333 if ((idx = _find_base(g)) >= 0) {
1333 if ((idx = s_find_base(g)) >= 0) {
13341334 /* it is already in the cache ... just check that the LUT is initialized */
13351335 if(fp_cache[idx].lru_count >= 2) {
13361336 LTC_MUTEX_UNLOCK(&ltc_ecc_fp_lock);
13381338 }
13391339 }
13401340
1341 if(idx == -1 && (idx = _find_hole()) == -1) {
1341 if(idx == -1 && (idx = s_find_hole()) == -1) {
13421342 err = CRYPT_BUFFER_OVERFLOW;
13431343 goto LBL_ERR;
13441344 }
1345 if ((err = _add_entry(idx, g)) != CRYPT_OK) {
1345 if ((err = s_add_entry(idx, g)) != CRYPT_OK) {
13461346 goto LBL_ERR;
13471347 }
13481348 /* compute mp */
13591359 }
13601360
13611361 /* build the LUT */
1362 if ((err = _build_lut(idx, a, modulus, mp, mu)) != CRYPT_OK) {
1362 if ((err = s_build_lut(idx, a, modulus, mp, mu)) != CRYPT_OK) {
13631363 goto LBL_ERR;
13641364 }
13651365 fp_cache[idx].lru_count = 2;
14971497 /*
14981498 * start with an empty cache
14991499 */
1500 _ltc_ecc_fp_free_cache();
1500 s_ltc_ecc_fp_free_cache();
15011501
15021502 /*
15031503 * decode the input packet: It consists of a sequence with a few
15671567 ERR_OUT:
15681568 if(asn1_list)
15691569 XFREE(asn1_list);
1570 _ltc_ecc_fp_free_cache();
1570 s_ltc_ecc_fp_free_cache();
15711571 LTC_MUTEX_UNLOCK(&ltc_ecc_fp_lock);
15721572 return err;
15731573 }
99 */
1010 #ifdef LTC_ADLER32
1111
12 static const unsigned long _adler32_base = 65521;
12 static const unsigned long s_adler32_base = 65521;
1313
1414 void adler32_init(adler32_state *ctx)
1515 {
3434 length--;
3535 } while (length % 8 != 0);
3636
37 if (s1 >= _adler32_base) {
38 s1 -= _adler32_base;
37 if (s1 >= s_adler32_base) {
38 s1 -= s_adler32_base;
3939 }
40 s2 %= _adler32_base;
40 s2 %= s_adler32_base;
4141 }
4242
4343 while (length > 0) {
6161 length -= 8;
6262 input += 8;
6363
64 if (s1 >= _adler32_base) {
65 s1 -= _adler32_base;
64 if (s1 >= s_adler32_base) {
65 s1 -= s_adler32_base;
6666 }
67 s2 %= _adler32_base;
67 s2 %= s_adler32_base;
6868 }
6969
70 LTC_ARGCHKVD(s1 < _adler32_base);
71 LTC_ARGCHKVD(s2 < _adler32_base);
70 LTC_ARGCHKVD(s1 < s_adler32_base);
71 LTC_ARGCHKVD(s2 < s_adler32_base);
7272
7373 ctx->s[0] = (unsigned short)s1;
7474 ctx->s[1] = (unsigned short)s2;
7474 relaxed = 2
7575 };
7676
77 static int _base64_decode_internal(const char *in, unsigned long inlen,
77 static int s_base64_decode_internal(const char *in, unsigned long inlen,
7878 unsigned char *out, unsigned long *outlen,
7979 const unsigned char *map, int mode)
8080 {
148148 int base64_decode(const char *in, unsigned long inlen,
149149 unsigned char *out, unsigned long *outlen)
150150 {
151 return _base64_decode_internal(in, inlen, out, outlen, map_base64, insane);
151 return s_base64_decode_internal(in, inlen, out, outlen, map_base64, insane);
152152 }
153153
154154 /**
162162 int base64_strict_decode(const char *in, unsigned long inlen,
163163 unsigned char *out, unsigned long *outlen)
164164 {
165 return _base64_decode_internal(in, inlen, out, outlen, map_base64, strict);
165 return s_base64_decode_internal(in, inlen, out, outlen, map_base64, strict);
166166 }
167167
168168 /**
176176 int base64_sane_decode(const char *in, unsigned long inlen,
177177 unsigned char *out, unsigned long *outlen)
178178 {
179 return _base64_decode_internal(in, inlen, out, outlen, map_base64, relaxed);
179 return s_base64_decode_internal(in, inlen, out, outlen, map_base64, relaxed);
180180 }
181181 #endif /* LTC_BASE64 */
182182
192192 int base64url_decode(const char *in, unsigned long inlen,
193193 unsigned char *out, unsigned long *outlen)
194194 {
195 return _base64_decode_internal(in, inlen, out, outlen, map_base64url, insane);
195 return s_base64_decode_internal(in, inlen, out, outlen, map_base64url, insane);
196196 }
197197
198198 /**
206206 int base64url_strict_decode(const char *in, unsigned long inlen,
207207 unsigned char *out, unsigned long *outlen)
208208 {
209 return _base64_decode_internal(in, inlen, out, outlen, map_base64url, strict);
209 return s_base64_decode_internal(in, inlen, out, outlen, map_base64url, strict);
210210 }
211211
212212 /**
220220 int base64url_sane_decode(const char *in, unsigned long inlen,
221221 unsigned char *out, unsigned long *outlen)
222222 {
223 return _base64_decode_internal(in, inlen, out, outlen, map_base64url, relaxed);
223 return s_base64_decode_internal(in, inlen, out, outlen, map_base64url, relaxed);
224224 }
225225 #endif /* LTC_BASE64_URL */
226226
2020 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
2121 #endif /* LTC_BASE64_URL */
2222
23 static int _base64_encode_internal(const unsigned char *in, unsigned long inlen,
23 static int s_base64_encode_internal(const unsigned char *in, unsigned long inlen,
2424 char *out, unsigned long *outlen,
2525 const char *codes, int pad)
2626 {
8282 int base64_encode(const unsigned char *in, unsigned long inlen,
8383 char *out, unsigned long *outlen)
8484 {
85 return _base64_encode_internal(in, inlen, out, outlen, codes_base64, 1);
85 return s_base64_encode_internal(in, inlen, out, outlen, codes_base64, 1);
8686 }
8787 #endif /* LTC_BASE64 */
8888
9999 int base64url_encode(const unsigned char *in, unsigned long inlen,
100100 char *out, unsigned long *outlen)
101101 {
102 return _base64_encode_internal(in, inlen, out, outlen, codes_base64url, 0);
102 return s_base64_encode_internal(in, inlen, out, outlen, codes_base64url, 0);
103103 }
104104
105105 int base64url_strict_encode(const unsigned char *in, unsigned long inlen,
106106 char *out, unsigned long *outlen)
107107 {
108 return _base64_encode_internal(in, inlen, out, outlen, codes_base64url, 1);
108 return s_base64_encode_internal(in, inlen, out, outlen, codes_base64url, 1);
109109 }
110110 #endif /* LTC_BASE64_URL */
111111
1010 #define BCRYPT_WORDS 8
1111 #define BCRYPT_HASHSIZE (BCRYPT_WORDS * 4)
1212
13 static int _bcrypt_hash(const unsigned char *pt,
13 static int s_bcrypt_hash(const unsigned char *pt,
1414 const unsigned char *pass, unsigned long passlen,
1515 const unsigned char *salt, unsigned long saltlen,
1616 unsigned char *out, unsigned long *outlen)
5151 return CRYPT_OK;
5252 }
5353
54 static int _bcrypt_pbkdf_hash(const unsigned char *pass, unsigned long passlen,
54 static int s_bcrypt_pbkdf_hash(const unsigned char *pass, unsigned long passlen,
5555 const unsigned char *salt, unsigned long saltlen,
5656 unsigned char *out, unsigned long *outlen)
5757 {
5858 const unsigned char pt[] = "OxychromaticBlowfishSwatDynamite";
59 return _bcrypt_hash(pt, pass, passlen, salt, saltlen, out, outlen);
59 return s_bcrypt_hash(pt, pass, passlen, salt, saltlen, out, outlen);
6060 }
6161
6262 /**
142142 goto LBL_ERR;
143143 }
144144 y = MAXBLOCKSIZE;
145 if ((err = _bcrypt_pbkdf_hash(hashed_pass, hashed_pass_len, buf[0], x, buf[1], &y)) != CRYPT_OK) {
145 if ((err = s_bcrypt_pbkdf_hash(hashed_pass, hashed_pass_len, buf[0], x, buf[1], &y)) != CRYPT_OK) {
146146 goto LBL_ERR;
147147 }
148148 XMEMCPY(buf[2], buf[1], y);
154154 goto LBL_ERR;
155155 }
156156 y = MAXBLOCKSIZE;
157 if ((err = _bcrypt_pbkdf_hash(hashed_pass, hashed_pass_len, buf[0], x, buf[1], &y)) != CRYPT_OK) {
157 if ((err = s_bcrypt_pbkdf_hash(hashed_pass, hashed_pass_len, buf[0], x, buf[1], &y)) != CRYPT_OK) {
158158 goto LBL_ERR;
159159 }
160160 for (x = 0; x < y; x++) {
88 */
99
1010 #if defined(LTC_TEST) && defined(LTC_TEST_DBG)
11 static void _print_hex(const char* what, const void* v, const unsigned long l)
11 static void s_print_hex(const char* what, const void* v, const unsigned long l)
1212 {
1313 const unsigned char* p = v;
1414 unsigned long x, y = 0, z;
6464 #if defined(LTC_TEST) && defined(LTC_TEST_DBG)
6565 if (res != 0) {
6666 fprintf(stderr, "Testvector #%i of %s failed:\n", which, what);
67 _print_hex("SHOULD", should, should_len);
68 _print_hex("IS ", is, is_len);
67 s_print_hex("SHOULD", should, should_len);
68 s_print_hex("IS ", is, is_len);
6969 #if LTC_TEST_DBG > 1
7070 } else {
7171 fprintf(stderr, "Testvector #%i of %s passed!\n", which, what);
99 */
1010 #ifdef LTC_CRC32
1111
12 static const ulong32 _CRC32_NEGL = 0xffffffffUL;
12 static const ulong32 CRC32_NEGL = 0xffffffffUL;
1313
1414 #if defined(ENDIAN_LITTLE)
1515 #define CRC32_INDEX(c) (c & 0xff)
136136 void crc32_init(crc32_state *ctx)
137137 {
138138 LTC_ARGCHKVD(ctx != NULL);
139 ctx->crc = _CRC32_NEGL;
139 ctx->crc = CRC32_NEGL;
140140 }
141141
142142 void crc32_update(crc32_state *ctx, const unsigned char *input, unsigned long length)
163163
164164 h = hash;
165165 crc = ctx->crc;
166 crc ^= _CRC32_NEGL;
166 crc ^= CRC32_NEGL;
167167
168168 if (size > 4) size = 4;
169169 for (i = 0; i < size; i++) {
1616 const int value;
1717 } crypt_constant;
1818
19 #define _C_STRINGIFY(s) { #s, s }
20
21 static const crypt_constant _crypt_constants[] = {
22
23 _C_STRINGIFY(CRYPT_OK),
24 _C_STRINGIFY(CRYPT_ERROR),
25 _C_STRINGIFY(CRYPT_NOP),
26 _C_STRINGIFY(CRYPT_INVALID_KEYSIZE),
27 _C_STRINGIFY(CRYPT_INVALID_ROUNDS),
28 _C_STRINGIFY(CRYPT_FAIL_TESTVECTOR),
29 _C_STRINGIFY(CRYPT_BUFFER_OVERFLOW),
30 _C_STRINGIFY(CRYPT_INVALID_PACKET),
31 _C_STRINGIFY(CRYPT_INVALID_PRNGSIZE),
32 _C_STRINGIFY(CRYPT_ERROR_READPRNG),
33 _C_STRINGIFY(CRYPT_INVALID_CIPHER),
34 _C_STRINGIFY(CRYPT_INVALID_HASH),
35 _C_STRINGIFY(CRYPT_INVALID_PRNG),
36 _C_STRINGIFY(CRYPT_MEM),
37 _C_STRINGIFY(CRYPT_PK_TYPE_MISMATCH),
38 _C_STRINGIFY(CRYPT_PK_NOT_PRIVATE),
39 _C_STRINGIFY(CRYPT_INVALID_ARG),
40 _C_STRINGIFY(CRYPT_FILE_NOTFOUND),
41 _C_STRINGIFY(CRYPT_PK_INVALID_TYPE),
42 _C_STRINGIFY(CRYPT_OVERFLOW),
43 _C_STRINGIFY(CRYPT_PK_ASN1_ERROR),
44 _C_STRINGIFY(CRYPT_INPUT_TOO_LONG),
45 _C_STRINGIFY(CRYPT_PK_INVALID_SIZE),
46 _C_STRINGIFY(CRYPT_INVALID_PRIME_SIZE),
47 _C_STRINGIFY(CRYPT_PK_INVALID_PADDING),
48 _C_STRINGIFY(CRYPT_HASH_OVERFLOW),
49
50 _C_STRINGIFY(PK_PUBLIC),
51 _C_STRINGIFY(PK_PRIVATE),
52
53 _C_STRINGIFY(LTC_ENCRYPT),
54 _C_STRINGIFY(LTC_DECRYPT),
19 #define C_STRINGIFY(s) { #s, s }
20
21 static const crypt_constant s_crypt_constants[] = {
22
23 C_STRINGIFY(CRYPT_OK),
24 C_STRINGIFY(CRYPT_ERROR),
25 C_STRINGIFY(CRYPT_NOP),
26 C_STRINGIFY(CRYPT_INVALID_KEYSIZE),
27 C_STRINGIFY(CRYPT_INVALID_ROUNDS),
28 C_STRINGIFY(CRYPT_FAIL_TESTVECTOR),
29 C_STRINGIFY(CRYPT_BUFFER_OVERFLOW),
30 C_STRINGIFY(CRYPT_INVALID_PACKET),
31 C_STRINGIFY(CRYPT_INVALID_PRNGSIZE),
32 C_STRINGIFY(CRYPT_ERROR_READPRNG),
33 C_STRINGIFY(CRYPT_INVALID_CIPHER),
34 C_STRINGIFY(CRYPT_INVALID_HASH),
35 C_STRINGIFY(CRYPT_INVALID_PRNG),
36 C_STRINGIFY(CRYPT_MEM),
37 C_STRINGIFY(CRYPT_PK_TYPE_MISMATCH),
38 C_STRINGIFY(CRYPT_PK_NOT_PRIVATE),
39 C_STRINGIFY(CRYPT_INVALID_ARG),
40 C_STRINGIFY(CRYPT_FILE_NOTFOUND),
41 C_STRINGIFY(CRYPT_PK_INVALID_TYPE),
42 C_STRINGIFY(CRYPT_OVERFLOW),
43 C_STRINGIFY(CRYPT_PK_ASN1_ERROR),
44 C_STRINGIFY(CRYPT_INPUT_TOO_LONG),
45 C_STRINGIFY(CRYPT_PK_INVALID_SIZE),
46 C_STRINGIFY(CRYPT_INVALID_PRIME_SIZE),
47 C_STRINGIFY(CRYPT_PK_INVALID_PADDING),
48 C_STRINGIFY(CRYPT_HASH_OVERFLOW),
49
50 C_STRINGIFY(PK_PUBLIC),
51 C_STRINGIFY(PK_PRIVATE),
52
53 C_STRINGIFY(LTC_ENCRYPT),
54 C_STRINGIFY(LTC_DECRYPT),
5555
5656 #ifdef LTC_PKCS_1
5757 {"LTC_PKCS_1", 1},
5858 /* Block types */
59 _C_STRINGIFY(LTC_PKCS_1_EMSA),
60 _C_STRINGIFY(LTC_PKCS_1_EME),
59 C_STRINGIFY(LTC_PKCS_1_EMSA),
60 C_STRINGIFY(LTC_PKCS_1_EME),
6161
6262 /* Padding types */
63 _C_STRINGIFY(LTC_PKCS_1_V1_5),
64 _C_STRINGIFY(LTC_PKCS_1_OAEP),
65 _C_STRINGIFY(LTC_PKCS_1_PSS),
66 _C_STRINGIFY(LTC_PKCS_1_V1_5_NA1),
63 C_STRINGIFY(LTC_PKCS_1_V1_5),
64 C_STRINGIFY(LTC_PKCS_1_OAEP),
65 C_STRINGIFY(LTC_PKCS_1_PSS),
66 C_STRINGIFY(LTC_PKCS_1_V1_5_NA1),
6767 #else
6868 {"LTC_PKCS_1", 0},
6969 #endif
7171 #ifdef LTC_PADDING
7272 {"LTC_PADDING", 1},
7373
74 _C_STRINGIFY(LTC_PAD_PKCS7),
74 C_STRINGIFY(LTC_PAD_PKCS7),
7575 #ifdef LTC_RNG_GET_BYTES
76 _C_STRINGIFY(LTC_PAD_ISO_10126),
77 #endif
78 _C_STRINGIFY(LTC_PAD_ANSI_X923),
79 _C_STRINGIFY(LTC_PAD_ONE_AND_ZERO),
80 _C_STRINGIFY(LTC_PAD_ZERO),
81 _C_STRINGIFY(LTC_PAD_ZERO_ALWAYS),
76 C_STRINGIFY(LTC_PAD_ISO_10126),
77 #endif
78 C_STRINGIFY(LTC_PAD_ANSI_X923),
79 C_STRINGIFY(LTC_PAD_ONE_AND_ZERO),
80 C_STRINGIFY(LTC_PAD_ZERO),
81 C_STRINGIFY(LTC_PAD_ZERO_ALWAYS),
8282 #else
8383 {"LTC_PADDING", 0},
8484 #endif
9191
9292 #ifdef LTC_MECC
9393 {"LTC_MECC", 1},
94 _C_STRINGIFY(ECC_BUF_SIZE),
95 _C_STRINGIFY(ECC_MAXSIZE),
94 C_STRINGIFY(ECC_BUF_SIZE),
95 C_STRINGIFY(ECC_MAXSIZE),
9696 #else
9797 {"LTC_MECC", 0},
9898 #endif
9999
100100 #ifdef LTC_MDSA
101101 {"LTC_MDSA", 1},
102 _C_STRINGIFY(LTC_MDSA_DELTA),
103 _C_STRINGIFY(LTC_MDSA_MAX_GROUP),
102 C_STRINGIFY(LTC_MDSA_DELTA),
103 C_STRINGIFY(LTC_MDSA_MAX_GROUP),
104104 #else
105105 {"LTC_MDSA", 0},
106106 #endif
107107
108108 #ifdef LTC_MILLER_RABIN_REPS
109 _C_STRINGIFY(LTC_MILLER_RABIN_REPS),
109 C_STRINGIFY(LTC_MILLER_RABIN_REPS),
110110 #endif
111111
112112 #ifdef LTC_DER
113113 /* DER handling */
114114 {"LTC_DER", 1},
115 _C_STRINGIFY(LTC_ASN1_EOL),
116 _C_STRINGIFY(LTC_ASN1_BOOLEAN),
117 _C_STRINGIFY(LTC_ASN1_INTEGER),
118 _C_STRINGIFY(LTC_ASN1_SHORT_INTEGER),
119 _C_STRINGIFY(LTC_ASN1_BIT_STRING),
120 _C_STRINGIFY(LTC_ASN1_OCTET_STRING),
121 _C_STRINGIFY(LTC_ASN1_NULL),
122 _C_STRINGIFY(LTC_ASN1_OBJECT_IDENTIFIER),
123 _C_STRINGIFY(LTC_ASN1_IA5_STRING),
124 _C_STRINGIFY(LTC_ASN1_PRINTABLE_STRING),
125 _C_STRINGIFY(LTC_ASN1_UTF8_STRING),
126 _C_STRINGIFY(LTC_ASN1_UTCTIME),
127 _C_STRINGIFY(LTC_ASN1_CHOICE),
128 _C_STRINGIFY(LTC_ASN1_SEQUENCE),
129 _C_STRINGIFY(LTC_ASN1_SET),
130 _C_STRINGIFY(LTC_ASN1_SETOF),
131 _C_STRINGIFY(LTC_ASN1_RAW_BIT_STRING),
132 _C_STRINGIFY(LTC_ASN1_TELETEX_STRING),
133 _C_STRINGIFY(LTC_ASN1_GENERALIZEDTIME),
134 _C_STRINGIFY(LTC_ASN1_CUSTOM_TYPE),
135 _C_STRINGIFY(LTC_DER_MAX_RECURSION),
115 C_STRINGIFY(LTC_ASN1_EOL),
116 C_STRINGIFY(LTC_ASN1_BOOLEAN),
117 C_STRINGIFY(LTC_ASN1_INTEGER),
118 C_STRINGIFY(LTC_ASN1_SHORT_INTEGER),
119 C_STRINGIFY(LTC_ASN1_BIT_STRING),
120 C_STRINGIFY(LTC_ASN1_OCTET_STRING),
121 C_STRINGIFY(LTC_ASN1_NULL),
122 C_STRINGIFY(LTC_ASN1_OBJECT_IDENTIFIER),
123 C_STRINGIFY(LTC_ASN1_IA5_STRING),
124 C_STRINGIFY(LTC_ASN1_PRINTABLE_STRING),
125 C_STRINGIFY(LTC_ASN1_UTF8_STRING),
126 C_STRINGIFY(LTC_ASN1_UTCTIME),
127 C_STRINGIFY(LTC_ASN1_CHOICE),
128 C_STRINGIFY(LTC_ASN1_SEQUENCE),
129 C_STRINGIFY(LTC_ASN1_SET),
130 C_STRINGIFY(LTC_ASN1_SETOF),
131 C_STRINGIFY(LTC_ASN1_RAW_BIT_STRING),
132 C_STRINGIFY(LTC_ASN1_TELETEX_STRING),
133 C_STRINGIFY(LTC_ASN1_GENERALIZEDTIME),
134 C_STRINGIFY(LTC_ASN1_CUSTOM_TYPE),
135 C_STRINGIFY(LTC_DER_MAX_RECURSION),
136136 #else
137137 {"LTC_DER", 0},
138138 #endif
139139
140140 #ifdef LTC_CTR_MODE
141141 {"LTC_CTR_MODE", 1},
142 _C_STRINGIFY(CTR_COUNTER_LITTLE_ENDIAN),
143 _C_STRINGIFY(CTR_COUNTER_BIG_ENDIAN),
144 _C_STRINGIFY(LTC_CTR_RFC3686),
142 C_STRINGIFY(CTR_COUNTER_LITTLE_ENDIAN),
143 C_STRINGIFY(CTR_COUNTER_BIG_ENDIAN),
144 C_STRINGIFY(LTC_CTR_RFC3686),
145145 #else
146146 {"LTC_CTR_MODE", 0},
147147 #endif
148148 #ifdef LTC_GCM_MODE
149 _C_STRINGIFY(LTC_GCM_MODE_IV),
150 _C_STRINGIFY(LTC_GCM_MODE_AAD),
151 _C_STRINGIFY(LTC_GCM_MODE_TEXT),
152 #endif
153
154 _C_STRINGIFY(LTC_MP_LT),
155 _C_STRINGIFY(LTC_MP_EQ),
156 _C_STRINGIFY(LTC_MP_GT),
157
158 _C_STRINGIFY(LTC_MP_NO),
159 _C_STRINGIFY(LTC_MP_YES),
160
161 _C_STRINGIFY(MAXBLOCKSIZE),
162 _C_STRINGIFY(TAB_SIZE),
163 _C_STRINGIFY(ARGTYPE),
149 C_STRINGIFY(LTC_GCM_MODE_IV),
150 C_STRINGIFY(LTC_GCM_MODE_AAD),
151 C_STRINGIFY(LTC_GCM_MODE_TEXT),
152 #endif
153
154 C_STRINGIFY(LTC_MP_LT),
155 C_STRINGIFY(LTC_MP_EQ),
156 C_STRINGIFY(LTC_MP_GT),
157
158 C_STRINGIFY(LTC_MP_NO),
159 C_STRINGIFY(LTC_MP_YES),
160
161 C_STRINGIFY(MAXBLOCKSIZE),
162 C_STRINGIFY(TAB_SIZE),
163 C_STRINGIFY(ARGTYPE),
164164
165165 #ifdef LTM_DESC
166166 {"LTM_DESC", 1},
228228 */
229229 int crypt_get_constant(const char* namein, int *valueout) {
230230 int i;
231 int _crypt_constants_len = sizeof(_crypt_constants) / sizeof(_crypt_constants[0]);
232 for (i=0; i<_crypt_constants_len; i++) {
233 if (XSTRCMP(_crypt_constants[i].name, namein) == 0) {
234 *valueout = _crypt_constants[i].value;
231 int count = sizeof(s_crypt_constants) / sizeof(s_crypt_constants[0]);
232 for (i=0; i<count; i++) {
233 if (XSTRCMP(s_crypt_constants[i].name, namein) == 0) {
234 *valueout = s_crypt_constants[i].value;
235235 return 0;
236236 }
237237 }
253253 unsigned int total_len = 0;
254254 char *ptr;
255255 int number_len;
256 int count = sizeof(_crypt_constants) / sizeof(_crypt_constants[0]);
256 int count = sizeof(s_crypt_constants) / sizeof(s_crypt_constants[0]);
257257
258258 /* calculate amount of memory required for the list */
259259 for (i=0; i<count; i++) {
260 number_len = snprintf(NULL, 0, "%s,%d\n", _crypt_constants[i].name, _crypt_constants[i].value);
260 number_len = snprintf(NULL, 0, "%s,%d\n", s_crypt_constants[i].name, s_crypt_constants[i].value);
261261 if (number_len < 0) {
262262 return -1;
263263 }
273273 /* build the names list */
274274 ptr = names_list;
275275 for (i=0; i<count; i++) {
276 number_len = snprintf(ptr, total_len, "%s,%d\n", _crypt_constants[i].name, _crypt_constants[i].value);
276 number_len = snprintf(ptr, total_len, "%s,%d\n", s_crypt_constants[i].name, s_crypt_constants[i].value);
277277 if (number_len < 0) return -1;
278278 if ((unsigned int)number_len > total_len) return -1;
279279 total_len -= number_len;
1616 const unsigned int size;
1717 } crypt_size;
1818
19 #define _SZ_STRINGIFY_S(s) { #s, sizeof(struct s) }
20 #define _SZ_STRINGIFY_T(s) { #s, sizeof(s) }
21
22 static const crypt_size _crypt_sizes[] = {
19 #define SZ_STRINGIFY_S(s) { #s, sizeof(struct s) }
20 #define SZ_STRINGIFY_T(s) { #s, sizeof(s) }
21
22 static const crypt_size s_crypt_sizes[] = {
2323 /* hash state sizes */
24 _SZ_STRINGIFY_S(ltc_hash_descriptor),
25 _SZ_STRINGIFY_T(hash_state),
24 SZ_STRINGIFY_S(ltc_hash_descriptor),
25 SZ_STRINGIFY_T(hash_state),
2626 #ifdef LTC_CHC_HASH
27 _SZ_STRINGIFY_S(chc_state),
27 SZ_STRINGIFY_S(chc_state),
2828 #endif
2929 #ifdef LTC_WHIRLPOOL
30 _SZ_STRINGIFY_S(whirlpool_state),
30 SZ_STRINGIFY_S(whirlpool_state),
3131 #endif
3232 #ifdef LTC_SHA3
33 _SZ_STRINGIFY_S(sha3_state),
33 SZ_STRINGIFY_S(sha3_state),
3434 #endif
3535 #ifdef LTC_SHA512
36 _SZ_STRINGIFY_S(sha512_state),
36 SZ_STRINGIFY_S(sha512_state),
3737 #endif
3838 #ifdef LTC_SHA256
39 _SZ_STRINGIFY_S(sha256_state),
39 SZ_STRINGIFY_S(sha256_state),
4040 #endif
4141 #ifdef LTC_SHA1
42 _SZ_STRINGIFY_S(sha1_state),
42 SZ_STRINGIFY_S(sha1_state),
4343 #endif
4444 #ifdef LTC_MD5
45 _SZ_STRINGIFY_S(md5_state),
45 SZ_STRINGIFY_S(md5_state),
4646 #endif
4747 #ifdef LTC_MD4
48 _SZ_STRINGIFY_S(md4_state),
48 SZ_STRINGIFY_S(md4_state),
4949 #endif
5050 #ifdef LTC_MD2
51 _SZ_STRINGIFY_S(md2_state),
51 SZ_STRINGIFY_S(md2_state),
5252 #endif
5353 #ifdef LTC_TIGER
54 _SZ_STRINGIFY_S(tiger_state),
54 SZ_STRINGIFY_S(tiger_state),
5555 #endif
5656 #ifdef LTC_RIPEMD128
57 _SZ_STRINGIFY_S(rmd128_state),
57 SZ_STRINGIFY_S(rmd128_state),
5858 #endif
5959 #ifdef LTC_RIPEMD160
60 _SZ_STRINGIFY_S(rmd160_state),
60 SZ_STRINGIFY_S(rmd160_state),
6161 #endif
6262 #ifdef LTC_RIPEMD256
63 _SZ_STRINGIFY_S(rmd256_state),
63 SZ_STRINGIFY_S(rmd256_state),
6464 #endif
6565 #ifdef LTC_RIPEMD320
66 _SZ_STRINGIFY_S(rmd320_state),
66 SZ_STRINGIFY_S(rmd320_state),
6767 #endif
6868 #ifdef LTC_BLAKE2S
69 _SZ_STRINGIFY_S(blake2s_state),
69 SZ_STRINGIFY_S(blake2s_state),
7070 #endif
7171 #ifdef LTC_BLAKE2B
72 _SZ_STRINGIFY_S(blake2b_state),
72 SZ_STRINGIFY_S(blake2b_state),
7373 #endif
7474
7575 /* block cipher key sizes */
76 _SZ_STRINGIFY_S(ltc_cipher_descriptor),
77 _SZ_STRINGIFY_T(symmetric_key),
76 SZ_STRINGIFY_S(ltc_cipher_descriptor),
77 SZ_STRINGIFY_T(symmetric_key),
7878 #ifdef LTC_ANUBIS
79 _SZ_STRINGIFY_S(anubis_key),
79 SZ_STRINGIFY_S(anubis_key),
8080 #endif
8181 #ifdef LTC_CAMELLIA
82 _SZ_STRINGIFY_S(camellia_key),
82 SZ_STRINGIFY_S(camellia_key),
8383 #endif
8484 #ifdef LTC_BLOWFISH
85 _SZ_STRINGIFY_S(blowfish_key),
85 SZ_STRINGIFY_S(blowfish_key),
8686 #endif
8787 #ifdef LTC_CAST5
88 _SZ_STRINGIFY_S(cast5_key),
88 SZ_STRINGIFY_S(cast5_key),
8989 #endif
9090 #ifdef LTC_DES
91 _SZ_STRINGIFY_S(des_key),
92 _SZ_STRINGIFY_S(des3_key),
91 SZ_STRINGIFY_S(des_key),
92 SZ_STRINGIFY_S(des3_key),
9393 #endif
9494 #ifdef LTC_IDEA
95 _SZ_STRINGIFY_S(idea_key),
95 SZ_STRINGIFY_S(idea_key),
9696 #endif
9797 #ifdef LTC_KASUMI
98 _SZ_STRINGIFY_S(kasumi_key),
98 SZ_STRINGIFY_S(kasumi_key),
9999 #endif
100100 #ifdef LTC_KHAZAD
101 _SZ_STRINGIFY_S(khazad_key),
101 SZ_STRINGIFY_S(khazad_key),
102102 #endif
103103 #ifdef LTC_KSEED
104 _SZ_STRINGIFY_S(kseed_key),
104 SZ_STRINGIFY_S(kseed_key),
105105 #endif
106106 #ifdef LTC_MULTI2
107 _SZ_STRINGIFY_S(multi2_key),
107 SZ_STRINGIFY_S(multi2_key),
108108 #endif
109109 #ifdef LTC_NOEKEON
110 _SZ_STRINGIFY_S(noekeon_key),
110 SZ_STRINGIFY_S(noekeon_key),
111111 #endif
112112 #ifdef LTC_RC2
113 _SZ_STRINGIFY_S(rc2_key),
113 SZ_STRINGIFY_S(rc2_key),
114114 #endif
115115 #ifdef LTC_RC5
116 _SZ_STRINGIFY_S(rc5_key),
116 SZ_STRINGIFY_S(rc5_key),
117117 #endif
118118 #ifdef LTC_RC6
119 _SZ_STRINGIFY_S(rc6_key),
119 SZ_STRINGIFY_S(rc6_key),
120120 #endif
121121 #ifdef LTC_SERPENT
122 _SZ_STRINGIFY_S(serpent_key),
122 SZ_STRINGIFY_S(serpent_key),
123123 #endif
124124 #ifdef LTC_SKIPJACK
125 _SZ_STRINGIFY_S(skipjack_key),
125 SZ_STRINGIFY_S(skipjack_key),
126126 #endif
127127 #ifdef LTC_XTEA
128 _SZ_STRINGIFY_S(xtea_key),
128 SZ_STRINGIFY_S(xtea_key),
129129 #endif
130130 #ifdef LTC_RIJNDAEL
131 _SZ_STRINGIFY_S(rijndael_key),
131 SZ_STRINGIFY_S(rijndael_key),
132132 #endif
133133 #ifdef LTC_SAFER
134 _SZ_STRINGIFY_S(safer_key),
134 SZ_STRINGIFY_S(safer_key),
135135 #endif
136136 #ifdef LTC_SAFERP
137 _SZ_STRINGIFY_S(saferp_key),
137 SZ_STRINGIFY_S(saferp_key),
138138 #endif
139139 #ifdef LTC_TWOFISH
140 _SZ_STRINGIFY_S(twofish_key),
140 SZ_STRINGIFY_S(twofish_key),
141141 #endif
142142
143143 /* mode sizes */
144144 #ifdef LTC_ECB_MODE
145 _SZ_STRINGIFY_T(symmetric_ECB),
145 SZ_STRINGIFY_T(symmetric_ECB),
146146 #endif
147147 #ifdef LTC_CFB_MODE
148 _SZ_STRINGIFY_T(symmetric_CFB),
148 SZ_STRINGIFY_T(symmetric_CFB),
149149 #endif
150150 #ifdef LTC_OFB_MODE
151 _SZ_STRINGIFY_T(symmetric_OFB),
151 SZ_STRINGIFY_T(symmetric_OFB),
152152 #endif
153153 #ifdef LTC_CBC_MODE
154 _SZ_STRINGIFY_T(symmetric_CBC),
154 SZ_STRINGIFY_T(symmetric_CBC),
155155 #endif
156156 #ifdef LTC_CTR_MODE
157 _SZ_STRINGIFY_T(symmetric_CTR),
157 SZ_STRINGIFY_T(symmetric_CTR),
158158 #endif
159159 #ifdef LTC_LRW_MODE
160 _SZ_STRINGIFY_T(symmetric_LRW),
160 SZ_STRINGIFY_T(symmetric_LRW),
161161 #endif
162162 #ifdef LTC_F8_MODE
163 _SZ_STRINGIFY_T(symmetric_F8),
163 SZ_STRINGIFY_T(symmetric_F8),
164164 #endif
165165 #ifdef LTC_XTS_MODE
166 _SZ_STRINGIFY_T(symmetric_xts),
166 SZ_STRINGIFY_T(symmetric_xts),
167167 #endif
168168
169169 /* stream cipher sizes */
170170 #ifdef LTC_CHACHA
171 _SZ_STRINGIFY_T(chacha_state),
171 SZ_STRINGIFY_T(chacha_state),
172172 #endif
173173 #ifdef LTC_SALSA20
174 _SZ_STRINGIFY_T(salsa20_state),
174 SZ_STRINGIFY_T(salsa20_state),
175175 #endif
176176 #ifdef LTC_SOSEMANUK
177 _SZ_STRINGIFY_T(sosemanuk_state),
177 SZ_STRINGIFY_T(sosemanuk_state),
178178 #endif
179179 #ifdef LTC_RABBIT
180 _SZ_STRINGIFY_T(rabbit_state),
180 SZ_STRINGIFY_T(rabbit_state),
181181 #endif
182182 #ifdef LTC_RC4_STREAM
183 _SZ_STRINGIFY_T(rc4_state),
183 SZ_STRINGIFY_T(rc4_state),
184184 #endif
185185 #ifdef LTC_SOBER128_STREAM
186 _SZ_STRINGIFY_T(sober128_state),
186 SZ_STRINGIFY_T(sober128_state),
187187 #endif
188188
189189 /* MAC sizes -- no states for ccm, lrw */
190190 #ifdef LTC_HMAC
191 _SZ_STRINGIFY_T(hmac_state),
191 SZ_STRINGIFY_T(hmac_state),
192192 #endif
193193 #ifdef LTC_OMAC
194 _SZ_STRINGIFY_T(omac_state),
194 SZ_STRINGIFY_T(omac_state),
195195 #endif
196196 #ifdef LTC_PMAC
197 _SZ_STRINGIFY_T(pmac_state),
197 SZ_STRINGIFY_T(pmac_state),
198198 #endif
199199 #ifdef LTC_POLY1305
200 _SZ_STRINGIFY_T(poly1305_state),
200 SZ_STRINGIFY_T(poly1305_state),
201201 #endif
202202 #ifdef LTC_EAX_MODE
203 _SZ_STRINGIFY_T(eax_state),
203 SZ_STRINGIFY_T(eax_state),
204204 #endif
205205 #ifdef LTC_OCB_MODE
206 _SZ_STRINGIFY_T(ocb_state),
206 SZ_STRINGIFY_T(ocb_state),
207207 #endif
208208 #ifdef LTC_OCB3_MODE
209 _SZ_STRINGIFY_T(ocb3_state),
209 SZ_STRINGIFY_T(ocb3_state),
210210 #endif
211211 #ifdef LTC_CCM_MODE
212 _SZ_STRINGIFY_T(ccm_state),
212 SZ_STRINGIFY_T(ccm_state),
213213 #endif
214214 #ifdef LTC_GCM_MODE
215 _SZ_STRINGIFY_T(gcm_state),
215 SZ_STRINGIFY_T(gcm_state),
216216 #endif
217217 #ifdef LTC_PELICAN
218 _SZ_STRINGIFY_T(pelican_state),
218 SZ_STRINGIFY_T(pelican_state),
219219 #endif
220220 #ifdef LTC_XCBC
221 _SZ_STRINGIFY_T(xcbc_state),
221 SZ_STRINGIFY_T(xcbc_state),
222222 #endif
223223 #ifdef LTC_F9_MODE
224 _SZ_STRINGIFY_T(f9_state),
224 SZ_STRINGIFY_T(f9_state),
225225 #endif
226226 #ifdef LTC_CHACHA20POLY1305_MODE
227 _SZ_STRINGIFY_T(chacha20poly1305_state),
227 SZ_STRINGIFY_T(chacha20poly1305_state),
228228 #endif
229229
230230 /* asymmetric keys */
231231 #ifdef LTC_MRSA
232 _SZ_STRINGIFY_T(rsa_key),
232 SZ_STRINGIFY_T(rsa_key),
233233 #endif
234234 #ifdef LTC_MDSA
235 _SZ_STRINGIFY_T(dsa_key),
235 SZ_STRINGIFY_T(dsa_key),
236236 #endif
237237 #ifdef LTC_MDH
238 _SZ_STRINGIFY_T(dh_key),
238 SZ_STRINGIFY_T(dh_key),
239239 #endif
240240 #ifdef LTC_MECC
241 _SZ_STRINGIFY_T(ltc_ecc_curve),
242 _SZ_STRINGIFY_T(ecc_point),
243 _SZ_STRINGIFY_T(ecc_key),
241 SZ_STRINGIFY_T(ltc_ecc_curve),
242 SZ_STRINGIFY_T(ecc_point),
243 SZ_STRINGIFY_T(ecc_key),
244244 #endif
245245
246246 /* DER handling */
247247 #ifdef LTC_DER
248 _SZ_STRINGIFY_T(ltc_asn1_list), /* a list entry */
249 _SZ_STRINGIFY_T(ltc_utctime),
250 _SZ_STRINGIFY_T(ltc_generalizedtime),
248 SZ_STRINGIFY_T(ltc_asn1_list), /* a list entry */
249 SZ_STRINGIFY_T(ltc_utctime),
250 SZ_STRINGIFY_T(ltc_generalizedtime),
251251 #endif
252252
253253 /* prng state sizes */
254 _SZ_STRINGIFY_S(ltc_prng_descriptor),
255 _SZ_STRINGIFY_T(prng_state),
254 SZ_STRINGIFY_S(ltc_prng_descriptor),
255 SZ_STRINGIFY_T(prng_state),
256256 #ifdef LTC_FORTUNA
257 _SZ_STRINGIFY_S(fortuna_prng),
257 SZ_STRINGIFY_S(fortuna_prng),
258258 #endif
259259 #ifdef LTC_CHACHA20_PRNG
260 _SZ_STRINGIFY_S(chacha20_prng),
260 SZ_STRINGIFY_S(chacha20_prng),
261261 #endif
262262 #ifdef LTC_RC4
263 _SZ_STRINGIFY_S(rc4_prng),
263 SZ_STRINGIFY_S(rc4_prng),
264264 #endif
265265 #ifdef LTC_SOBER128
266 _SZ_STRINGIFY_S(sober128_prng),
266 SZ_STRINGIFY_S(sober128_prng),
267267 #endif
268268 #ifdef LTC_YARROW
269 _SZ_STRINGIFY_S(yarrow_prng),
269 SZ_STRINGIFY_S(yarrow_prng),
270270 #endif
271271 /* sprng has no state as it uses other potentially available sources */
272272 /* like /dev/random. See Developers Guide for more info. */
273273
274274 #ifdef LTC_ADLER32
275 _SZ_STRINGIFY_T(adler32_state),
275 SZ_STRINGIFY_T(adler32_state),
276276 #endif
277277 #ifdef LTC_CRC32
278 _SZ_STRINGIFY_T(crc32_state),
279 #endif
280
281 _SZ_STRINGIFY_T(ltc_mp_digit),
282 _SZ_STRINGIFY_T(ltc_math_descriptor)
278 SZ_STRINGIFY_T(crc32_state),
279 #endif
280
281 SZ_STRINGIFY_T(ltc_mp_digit),
282 SZ_STRINGIFY_T(ltc_math_descriptor)
283283
284284 };
285285
289289 */
290290 int crypt_get_size(const char* namein, unsigned int *sizeout) {
291291 int i;
292 int count = sizeof(_crypt_sizes) / sizeof(_crypt_sizes[0]);
292 int count = sizeof(s_crypt_sizes) / sizeof(s_crypt_sizes[0]);
293293 for (i=0; i<count; i++) {
294 if (XSTRCMP(_crypt_sizes[i].name, namein) == 0) {
295 *sizeout = _crypt_sizes[i].size;
294 if (XSTRCMP(s_crypt_sizes[i].name, namein) == 0) {
295 *sizeout = s_crypt_sizes[i].size;
296296 return 0;
297297 }
298298 }
314314 unsigned int total_len = 0;
315315 char *ptr;
316316 int number_len;
317 int count = sizeof(_crypt_sizes) / sizeof(_crypt_sizes[0]);
317 int count = sizeof(s_crypt_sizes) / sizeof(s_crypt_sizes[0]);
318318
319319 /* calculate amount of memory required for the list */
320320 for (i=0; i<count; i++) {
321 number_len = snprintf(NULL, 0, "%s,%u\n", _crypt_sizes[i].name, _crypt_sizes[i].size);
321 number_len = snprintf(NULL, 0, "%s,%u\n", s_crypt_sizes[i].name, s_crypt_sizes[i].size);
322322 if (number_len < 0) {
323323 return -1;
324324 }
335335 /* build the names list */
336336 ptr = names_list;
337337 for (i=0; i<count; i++) {
338 number_len = snprintf(ptr, total_len, "%s,%u\n", _crypt_sizes[i].name, _crypt_sizes[i].size);
338 number_len = snprintf(ptr, total_len, "%s,%u\n", s_crypt_sizes[i].name, s_crypt_sizes[i].size);
339339 if (number_len < 0) return -1;
340340 if ((unsigned int)number_len > total_len) return -1;
341341 total_len -= number_len;
1010 @param mode Mask of (LTC_PAD_xxx | block_length)
1111 @return CRYPT_OK on success
1212 */
13 static int _padding_padded_length(unsigned long *length, unsigned long mode)
13 static int s_padding_padded_length(unsigned long *length, unsigned long mode)
1414 {
1515 enum padding_type padding;
1616 unsigned char pad, block_length, r, t;
8686 LTC_ARGCHK(padded_length != NULL);
8787
8888 l = length;
89 if ((err = _padding_padded_length(&l, mode)) != CRYPT_OK) {
89 if ((err = s_padding_padded_length(&l, mode)) != CRYPT_OK) {
9090 return err;
9191 }
9292
33
44 #ifdef LTC_PBES
55
6 static int _pkcs_5_alg1_wrap(const unsigned char *password, unsigned long password_len,
6 static int s_pkcs_5_alg1_wrap(const unsigned char *password, unsigned long password_len,
77 const unsigned char *salt, unsigned long salt_len,
88 int iteration_count, int hash_idx,
99 unsigned char *out, unsigned long *outlen)
1212 return pkcs_5_alg1(password, password_len, salt, iteration_count, hash_idx, out, outlen);
1313 }
1414
15 static int _pkcs_12_wrap(const unsigned char *password, unsigned long password_len,
15 static int s_pkcs_12_wrap(const unsigned char *password, unsigned long password_len,
1616 const unsigned char *salt, unsigned long salt_len,
1717 int iteration_count, int hash_idx,
1818 unsigned char *out, unsigned long *outlen)
3939 return err;
4040 }
4141
42 static const pbes_properties _pbes1_types[] = {
43 { _pkcs_5_alg1_wrap, "md2", "des", 8, 8 },
44 { _pkcs_5_alg1_wrap, "md2", "rc2", 8, 8 },
45 { _pkcs_5_alg1_wrap, "md5", "des", 8, 8 },
46 { _pkcs_5_alg1_wrap, "md5", "rc2", 8, 8 },
47 { _pkcs_5_alg1_wrap, "sha1", "des", 8, 8 },
48 { _pkcs_5_alg1_wrap, "sha1", "rc2", 8, 8 },
49 { _pkcs_12_wrap, "sha1", "3des", 24, 8 },
42 static const pbes_properties s_pbes1_types[] = {
43 { s_pkcs_5_alg1_wrap, "md2", "des", 8, 8 },
44 { s_pkcs_5_alg1_wrap, "md2", "rc2", 8, 8 },
45 { s_pkcs_5_alg1_wrap, "md5", "des", 8, 8 },
46 { s_pkcs_5_alg1_wrap, "md5", "rc2", 8, 8 },
47 { s_pkcs_5_alg1_wrap, "sha1", "des", 8, 8 },
48 { s_pkcs_5_alg1_wrap, "sha1", "rc2", 8, 8 },
49 { s_pkcs_12_wrap, "sha1", "3des", 24, 8 },
5050 };
5151
5252 typedef struct {
5454 const char *oid;
5555 } oid_to_pbes;
5656
57 static const oid_to_pbes _pbes1_list[] = {
58 { &_pbes1_types[0], "1.2.840.113549.1.5.1" }, /* http://www.oid-info.com/get/1.2.840.113549.1.5.1 pbeWithMD2AndDES-CBC */
59 { &_pbes1_types[1], "1.2.840.113549.1.5.4" }, /* http://www.oid-info.com/get/1.2.840.113549.1.5.4 pbeWithMD2AndRC2-CBC */
60 { &_pbes1_types[2], "1.2.840.113549.1.5.3" }, /* http://www.oid-info.com/get/1.2.840.113549.1.5.3 pbeWithMD5AndDES-CBC */
61 { &_pbes1_types[3], "1.2.840.113549.1.5.6" }, /* http://www.oid-info.com/get/1.2.840.113549.1.5.6 pbeWithMD5AndRC2-CBC */
62 { &_pbes1_types[4], "1.2.840.113549.1.5.10" }, /* http://www.oid-info.com/get/1.2.840.113549.1.5.10 pbeWithSHA1AndDES-CBC */
63 { &_pbes1_types[5], "1.2.840.113549.1.5.11" }, /* http://www.oid-info.com/get/1.2.840.113549.1.5.11 pbeWithSHA1AndRC2-CBC */
64 { &_pbes1_types[6], "1.2.840.113549.1.12.1.3" }, /* http://www.oid-info.com/get/1.2.840.113549.1.12.1.3 pbeWithSHAAnd3-KeyTripleDES-CBC */
57 static const oid_to_pbes s_pbes1_list[] = {
58 { &s_pbes1_types[0], "1.2.840.113549.1.5.1" }, /* http://www.oid-info.com/get/1.2.840.113549.1.5.1 pbeWithMD2AndDES-CBC */
59 { &s_pbes1_types[1], "1.2.840.113549.1.5.4" }, /* http://www.oid-info.com/get/1.2.840.113549.1.5.4 pbeWithMD2AndRC2-CBC */
60 { &s_pbes1_types[2], "1.2.840.113549.1.5.3" }, /* http://www.oid-info.com/get/1.2.840.113549.1.5.3 pbeWithMD5AndDES-CBC */
61 { &s_pbes1_types[3], "1.2.840.113549.1.5.6" }, /* http://www.oid-info.com/get/1.2.840.113549.1.5.6 pbeWithMD5AndRC2-CBC */
62 { &s_pbes1_types[4], "1.2.840.113549.1.5.10" }, /* http://www.oid-info.com/get/1.2.840.113549.1.5.10 pbeWithSHA1AndDES-CBC */
63 { &s_pbes1_types[5], "1.2.840.113549.1.5.11" }, /* http://www.oid-info.com/get/1.2.840.113549.1.5.11 pbeWithSHA1AndRC2-CBC */
64 { &s_pbes1_types[6], "1.2.840.113549.1.12.1.3" }, /* http://www.oid-info.com/get/1.2.840.113549.1.12.1.3 pbeWithSHAAnd3-KeyTripleDES-CBC */
6565 { 0 },
6666 };
6767
68 static int _pbes1_from_oid(const ltc_asn1_list *oid, pbes_properties *res)
68 static int s_pbes1_from_oid(const ltc_asn1_list *oid, pbes_properties *res)
6969 {
7070 unsigned int i;
71 for (i = 0; _pbes1_list[i].data != NULL; ++i) {
72 if (pk_oid_cmp_with_asn1(_pbes1_list[i].oid, oid) == CRYPT_OK) {
73 if (res != NULL) *res = *_pbes1_list[i].data;
71 for (i = 0; s_pbes1_list[i].data != NULL; ++i) {
72 if (pk_oid_cmp_with_asn1(s_pbes1_list[i].oid, oid) == CRYPT_OK) {
73 if (res != NULL) *res = *s_pbes1_list[i].data;
7474 return CRYPT_OK;
7575 }
7676 }
9191 LTC_ARGCHK(s != NULL);
9292 LTC_ARGCHK(res != NULL);
9393
94 if ((err = _pbes1_from_oid(s, &res->type)) != CRYPT_OK) return err;
94 if ((err = s_pbes1_from_oid(s, &res->type)) != CRYPT_OK) return err;
9595
9696 if (!LTC_ASN1_IS_TYPE(s->next, LTC_ASN1_SEQUENCE) ||
9797 !LTC_ASN1_IS_TYPE(s->next->child, LTC_ASN1_OCTET_STRING) ||
33
44 #ifdef LTC_PBES
55
6 static const char * const _oid_pbes2 = "1.2.840.113549.1.5.13";
7 static const char * const _oid_pbkdf2 = "1.2.840.113549.1.5.12";
6 static const char * const s_oid_pbes2 = "1.2.840.113549.1.5.13";
7 static const char * const s_oid_pbkdf2 = "1.2.840.113549.1.5.12";
88
99 typedef struct {
1010 const char *oid;
1111 const char *id;
1212 } oid_id_st;
1313
14 static const oid_id_st _hmac_oid_names[] = {
14 static const oid_id_st s_hmac_oid_names[] = {
1515 { "1.2.840.113549.2.7", "sha1" },
1616 { "1.2.840.113549.2.8", "sha224" },
1717 { "1.2.840.113549.2.9", "sha256" },
2121 { "1.2.840.113549.2.13", "sha512-256" },
2222 };
2323
24 static const pbes_properties _pbes2_default_types[] = {
24 static const pbes_properties s_pbes2_default_types[] = {
2525 { pkcs_5_alg2, "sha1", "des", 8, 0 },
2626 { pkcs_5_alg2, "sha1", "rc2", 4, 0 },
2727 { pkcs_5_alg2, "sha1", "3des", 24, 0 },
3535 const char* oid;
3636 } oid_to_pbes;
3737
38 static const oid_to_pbes _pbes2_list[] = {
39 { &_pbes2_default_types[0], "1.3.14.3.2.7" }, /* http://www.oid-info.com/get/1.3.14.3.2.7 desCBC */
40 { &_pbes2_default_types[1], "1.2.840.113549.3.2" }, /* http://www.oid-info.com/get/1.2.840.113549.3.2 rc2CBC */
41 { &_pbes2_default_types[2], "1.2.840.113549.3.7" }, /* http://www.oid-info.com/get/1.2.840.113549.3.7 des-EDE3-CBC */
42 { &_pbes2_default_types[3], "2.16.840.1.101.3.4.1.2" }, /* http://www.oid-info.com/get/2.16.840.1.101.3.4.1.2 aes128-CBC */
43 { &_pbes2_default_types[4], "2.16.840.1.101.3.4.1.22" }, /* http://www.oid-info.com/get/2.16.840.1.101.3.4.1.22 aes192-CBC */
44 { &_pbes2_default_types[5], "2.16.840.1.101.3.4.1.42" }, /* http://www.oid-info.com/get/2.16.840.1.101.3.4.1.42 aes256-CBC */
38 static const oid_to_pbes s_pbes2_list[] = {
39 { &s_pbes2_default_types[0], "1.3.14.3.2.7" }, /* http://www.oid-info.com/get/1.3.14.3.2.7 desCBC */
40 { &s_pbes2_default_types[1], "1.2.840.113549.3.2" }, /* http://www.oid-info.com/get/1.2.840.113549.3.2 rc2CBC */
41 { &s_pbes2_default_types[2], "1.2.840.113549.3.7" }, /* http://www.oid-info.com/get/1.2.840.113549.3.7 des-EDE3-CBC */
42 { &s_pbes2_default_types[3], "2.16.840.1.101.3.4.1.2" }, /* http://www.oid-info.com/get/2.16.840.1.101.3.4.1.2 aes128-CBC */
43 { &s_pbes2_default_types[4], "2.16.840.1.101.3.4.1.22" }, /* http://www.oid-info.com/get/2.16.840.1.101.3.4.1.22 aes192-CBC */
44 { &s_pbes2_default_types[5], "2.16.840.1.101.3.4.1.42" }, /* http://www.oid-info.com/get/2.16.840.1.101.3.4.1.42 aes256-CBC */
4545 };
4646
47 static int _pbes2_from_oid(const ltc_asn1_list *cipher_oid, const ltc_asn1_list *hmac_oid, pbes_properties *res)
47 static int s_pbes2_from_oid(const ltc_asn1_list *cipher_oid, const ltc_asn1_list *hmac_oid, pbes_properties *res)
4848 {
4949 unsigned int i;
50 for (i = 0; i < sizeof(_pbes2_list)/sizeof(_pbes2_list[0]); ++i) {
51 if (pk_oid_cmp_with_asn1(_pbes2_list[i].oid, cipher_oid) == CRYPT_OK) {
52 *res = *_pbes2_list[i].data;
50 for (i = 0; i < sizeof(s_pbes2_list)/sizeof(s_pbes2_list[0]); ++i) {
51 if (pk_oid_cmp_with_asn1(s_pbes2_list[i].oid, cipher_oid) == CRYPT_OK) {
52 *res = *s_pbes2_list[i].data;
5353 break;
5454 }
5555 }
5656 if (res->c == NULL) return CRYPT_INVALID_CIPHER;
5757 if (hmac_oid != NULL) {
58 for (i = 0; i < sizeof(_hmac_oid_names)/sizeof(_hmac_oid_names[0]); ++i) {
59 if (pk_oid_cmp_with_asn1(_hmac_oid_names[i].oid, hmac_oid) == CRYPT_OK) {
60 res->h = _hmac_oid_names[i].id;
58 for (i = 0; i < sizeof(s_hmac_oid_names)/sizeof(s_hmac_oid_names[0]); ++i) {
59 if (pk_oid_cmp_with_asn1(s_hmac_oid_names[i].oid, hmac_oid) == CRYPT_OK) {
60 res->h = s_hmac_oid_names[i].id;
6161 return CRYPT_OK;
6262 }
6363 }
8383 LTC_ARGCHK(s != NULL);
8484 LTC_ARGCHK(res != NULL);
8585
86 if ((err = pk_oid_cmp_with_asn1(_oid_pbes2, s)) != CRYPT_OK) return err;
86 if ((err = pk_oid_cmp_with_asn1(s_oid_pbes2, s)) != CRYPT_OK) return err;
8787
8888 if (!LTC_ASN1_IS_TYPE(s->next, LTC_ASN1_SEQUENCE) ||
8989 !LTC_ASN1_IS_TYPE(s->next->child, LTC_ASN1_SEQUENCE) ||
114114 lkdf = s->next->child->child;
115115 lenc = s->next->child->next->child;
116116
117 if ((err = pk_oid_cmp_with_asn1(_oid_pbkdf2, lkdf)) != CRYPT_OK) return err;
117 if ((err = pk_oid_cmp_with_asn1(s_oid_pbkdf2, lkdf)) != CRYPT_OK) return err;
118118
119119 if (!LTC_ASN1_IS_TYPE(lkdf->next, LTC_ASN1_SEQUENCE) ||
120120 !LTC_ASN1_IS_TYPE(lkdf->next->child, LTC_ASN1_OCTET_STRING) ||
139139 LTC_ASN1_IS_TYPE(loptseq->child, LTC_ASN1_OBJECT_IDENTIFIER)) {
140140 lhmac = loptseq->child;
141141 }
142 if ((err = _pbes2_from_oid(lenc, lhmac, &res->type)) != CRYPT_OK) return err;
142 if ((err = s_pbes2_from_oid(lenc, lhmac, &res->type)) != CRYPT_OK) return err;
143143
144144 if (LTC_ASN1_IS_TYPE(lenc->next, LTC_ASN1_OCTET_STRING)) {
145145 /* 'NON-RC2'-CBC */
2828 @param openssl_compat [in] Whether or not to grow the key to the buffer size ala OpenSSL
2929 @return CRYPT_OK if successful
3030 */
31 static int _pkcs_5_alg1_common(const unsigned char *password,
31 static int s_pkcs_5_alg1_common(const unsigned char *password,
3232 unsigned long password_len,
3333 const unsigned char *salt,
3434 int iteration_count, int hash_idx,
149149 int iteration_count, int hash_idx,
150150 unsigned char *out, unsigned long *outlen)
151151 {
152 return _pkcs_5_alg1_common(password, password_len, salt, iteration_count,
152 return s_pkcs_5_alg1_common(password, password_len, salt, iteration_count,
153153 hash_idx, out, outlen, 0);
154154 }
155155
173173 int iteration_count, int hash_idx,
174174 unsigned char *out, unsigned long *outlen)
175175 {
176 return _pkcs_5_alg1_common(password, password_len, salt, iteration_count,
176 return s_pkcs_5_alg1_common(password, password_len, salt, iteration_count,
177177 hash_idx, out, outlen, 1);
178178 }
179179
1717 @param ctr CTR state
1818 @return CRYPT_OK if successful
1919 */
20 static int _ctr_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_CTR *ctr)
20 static int s_ctr_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_CTR *ctr)
2121 {
2222 int x, err;
2323
104104 if ((cipher_descriptor[ctr->cipher].accel_ctr_encrypt != NULL) && (len >= (unsigned long)ctr->blocklen)) {
105105 if (ctr->padlen < ctr->blocklen) {
106106 fr = ctr->blocklen - ctr->padlen;
107 if ((err = _ctr_encrypt(pt, ct, fr, ctr)) != CRYPT_OK) {
107 if ((err = s_ctr_encrypt(pt, ct, fr, ctr)) != CRYPT_OK) {
108108 return err;
109109 }
110110 pt += fr;
122122 }
123123 }
124124
125 return _ctr_encrypt(pt, ct, len, ctr);
125 return s_ctr_encrypt(pt, ct, len, ctr);
126126 }
127127
128128 #endif
99
1010 #ifdef LTC_DER
1111
12 static int _char_to_int(unsigned char x)
12 static int s_char_to_int(unsigned char x)
1313 {
1414 switch (x) {
1515 case '0': return 0;
2727 }
2828
2929 #define DECODE_V(y, max) do {\
30 y = _char_to_int(buf[x])*10 + _char_to_int(buf[x+1]); \
30 y = s_char_to_int(buf[x])*10 + s_char_to_int(buf[x+1]); \
3131 if (y >= max) return CRYPT_INVALID_PACKET; \
3232 x += 2; \
3333 } while(0)
3434
3535 #define DECODE_V4(y, max) do {\
36 y = _char_to_int(buf[x])*1000 + _char_to_int(buf[x+1])*100 + _char_to_int(buf[x+2])*10 + _char_to_int(buf[x+3]); \
36 y = s_char_to_int(buf[x])*1000 + s_char_to_int(buf[x+1])*100 + s_char_to_int(buf[x+2])*10 + s_char_to_int(buf[x+3]); \
3737 if (y >= max) return CRYPT_INVALID_PACKET; \
3838 x += 4; \
3939 } while(0)
112112 unsigned fs = out->fs;
113113 if (x >= sizeof(buf)) return CRYPT_INVALID_PACKET;
114114 out->fs *= 10;
115 out->fs += _char_to_int(buf[x]);
115 out->fs += s_char_to_int(buf[x]);
116116 if (fs > out->fs) return CRYPT_OVERFLOW;
117117 x++;
118118 }
88
99 #ifdef LTC_DER
1010
11 static int _new_element(ltc_asn1_list **l)
11 static int s_new_element(ltc_asn1_list **l)
1212 {
1313 /* alloc new link */
1414 if (*l == NULL) {
3535 @param depth The depth/level of decoding recursion we've already reached
3636 @return CRYPT_OK on success.
3737 */
38 static int _der_decode_sequence_flexi(const unsigned char *in, unsigned long *inlen, ltc_asn1_list **out, unsigned long depth)
38 static int s_der_decode_sequence_flexi(const unsigned char *in, unsigned long *inlen, ltc_asn1_list **out, unsigned long depth)
3939 {
4040 ltc_asn1_list *l;
4141 unsigned long err, identifier, len, totlen, data_offset, id_len, len_len;
5050
5151 if (*inlen == 0) {
5252 /* alloc new link */
53 if ((err = _new_element(&l)) != CRYPT_OK) {
53 if ((err = s_new_element(&l)) != CRYPT_OK) {
5454 goto error;
5555 }
5656 }
5858 /* scan the input and and get lengths and what not */
5959 while (*inlen) {
6060 /* alloc new link */
61 if ((err = _new_element(&l)) != CRYPT_OK) {
61 if ((err = s_new_element(&l)) != CRYPT_OK) {
6262 goto error;
6363 }
6464
446446 len_len = len;
447447
448448 /* Sequence elements go as child */
449 if ((err = _der_decode_sequence_flexi(in, &len, &(l->child), depth+1)) != CRYPT_OK) {
449 if ((err = s_der_decode_sequence_flexi(in, &len, &(l->child), depth+1)) != CRYPT_OK) {
450450 goto error;
451451 }
452452 if (len_len != len) {
533533 */
534534 int der_decode_sequence_flexi(const unsigned char *in, unsigned long *inlen, ltc_asn1_list **out)
535535 {
536 return _der_decode_sequence_flexi(in, inlen, out, 0);
536 return s_der_decode_sequence_flexi(in, inlen, out, 0);
537537 }
538538
539539 #endif
1919 @param flags c.f. enum ltc_der_seq
2020 @return CRYPT_OK on success
2121 */
22 static int _der_decode_sequence_va(const unsigned char *in, unsigned long inlen, va_list a1, va_list a2, unsigned int flags)
22 static int s_der_decode_sequence_va(const unsigned char *in, unsigned long inlen, va_list a1, va_list a2, unsigned int flags)
2323 {
2424 int err;
2525 ltc_asn1_type type;
141141 va_start(a1, inlen);
142142 va_start(a2, inlen);
143143
144 err = _der_decode_sequence_va(in, inlen, a1, a2, LTC_DER_SEQ_SEQUENCE | LTC_DER_SEQ_RELAXED);
144 err = s_der_decode_sequence_va(in, inlen, a1, a2, LTC_DER_SEQ_SEQUENCE | LTC_DER_SEQ_RELAXED);
145145
146146 va_end(a2);
147147 va_end(a1);
167167 va_start(a1, flags);
168168 va_start(a2, flags);
169169
170 err = _der_decode_sequence_va(in, inlen, a1, a2, flags);
170 err = s_der_decode_sequence_va(in, inlen, a1, a2, flags);
171171
172172 va_end(a2);
173173 va_end(a1);
99 #ifdef LTC_DER
1010
1111 /* LTC define to ASN.1 TAG */
12 static int _ltc_to_asn1(ltc_asn1_type v)
12 static int s_ltc_to_asn1(ltc_asn1_type v)
1313 {
1414 return der_asn1_type_to_identifier_map[v];
1515 }
1616
1717
18 static int _qsort_helper(const void *a, const void *b)
18 static int s_qsort_helper(const void *a, const void *b)
1919 {
2020 ltc_asn1_list *A = (ltc_asn1_list *)a, *B = (ltc_asn1_list *)b;
2121 int r;
2222
23 r = _ltc_to_asn1(A->type) - _ltc_to_asn1(B->type);
23 r = s_ltc_to_asn1(A->type) - s_ltc_to_asn1(B->type);
2424
2525 /* for QSORT the order is UNDEFINED if they are "equal" which means it is NOT DETERMINISTIC. So we force it to be :-) */
2626 if (r == 0) {
5858 }
5959
6060 /* sort it by the "type" field */
61 XQSORT(copy, inlen, sizeof(*copy), &_qsort_helper);
61 XQSORT(copy, inlen, sizeof(*copy), &s_qsort_helper);
6262
6363 /* call der_encode_sequence_ex() */
6464 err = der_encode_sequence_ex(copy, inlen, out, outlen, LTC_ASN1_SET);
1313 unsigned long size;
1414 };
1515
16 static int _qsort_helper(const void *a, const void *b)
16 static int s_qsort_helper(const void *a, const void *b)
1717 {
1818 struct edge *A = (struct edge *)a, *B = (struct edge *)b;
1919 int r;
125125 }
126126
127127 /* sort based on contents (using edges) */
128 XQSORT(edges, inlen, sizeof(*edges), &_qsort_helper);
128 XQSORT(edges, inlen, sizeof(*edges), &s_qsort_helper);
129129
130130 /* copy static header */
131131 XMEMCPY(out, buf, hdrlen);
88
99 #ifdef LTC_DER
1010
11 static int _char_to_int(unsigned char x)
11 static int s_char_to_int(unsigned char x)
1212 {
1313 switch (x) {
1414 case '0': return 0;
2626 }
2727
2828 #define DECODE_V(y, max) \
29 y = _char_to_int(buf[x])*10 + _char_to_int(buf[x+1]); \
29 y = s_char_to_int(buf[x])*10 + s_char_to_int(buf[x+1]); \
3030 if (y >= max) return CRYPT_INVALID_PACKET; \
3131 x += 2;
3232
3838 unsigned long tmpbuf_len, tmp_inlen;
3939 ltc_asn1_list *decoded_list = NULL, *l;
4040
41 LTC_ARGCHK(in != NULL);
42 LTC_ARGCHK(inlen != 0);
41 LTC_ARGCHK(in != NULL);
42 LTC_ARGCHK(inlen != 0);
43 LTC_ARGCHK(callback != NULL);
4344
4445 tmpbuf_len = inlen;
4546 tmpbuf = XCALLOC(1, tmpbuf_len);
8081 && (l->data != NULL)
8182 && LOOKS_LIKE_SPKI(l->child)) {
8283 if (algorithm == PKA_EC) {
83 err = ecc_import_subject_public_key_info(l->data, l->size, ctx);
84 err = callback(l->data, l->size, ctx);
8485 } else {
8586 err = x509_decode_subject_public_key_info(l->data, l->size,
8687 algorithm, tmpbuf, &tmpbuf_len,
44
55 #ifdef LTC_MDH
66
7 static int _dh_groupsize_to_keysize(int groupsize)
7 static int s_dh_groupsize_to_keysize(int groupsize)
88 {
99 /* The strength estimates from https://tools.ietf.org/html/rfc3526#section-8
1010 * We use "Estimate 2" to get an appropriate private key (exponent) size.
4747 return err;
4848 }
4949
50 keysize = _dh_groupsize_to_keysize(mp_unsigned_bin_size(key->prime));
50 keysize = s_dh_groupsize_to_keysize(mp_unsigned_bin_size(key->prime));
5151 if (keysize == 0) {
5252 err = CRYPT_INVALID_KEYSIZE;
5353 goto freemp;
1919 @param g [out] bignum where generated 'g' is stored (must be initialized by caller)
2020 @return CRYPT_OK if successful, upon error this function will free all allocated memory
2121 */
22 static int _dsa_make_params(prng_state *prng, int wprng, int group_size, int modulus_size, void *p, void *q, void *g)
22 static int s_dsa_make_params(prng_state *prng, int wprng, int group_size, int modulus_size, void *p, void *q, void *g)
2323 {
2424 unsigned long L, N, n, outbytes, seedbytes, counter, j, i;
2525 int err, res, mr_tests_q, mr_tests_p, found_p, found_q, hash;
216216 return err;
217217 }
218218 /* generate params */
219 err = _dsa_make_params(prng, wprng, group_size, modulus_size, key->p, key->q, key->g);
219 err = s_dsa_make_params(prng, wprng, group_size, modulus_size, key->p, key->q, key->g);
220220 if (err != CRYPT_OK) {
221221 goto cleanup;
222222 }
1313 typedef i64 gf[16];
1414
1515 static const u8
16 _9[32] = {9};
16 nine[32] = {9};
1717 static const gf
1818 gf0,
1919 gf1 = {1},
20 _121665 = {0xDB41,1},
20 gf121665 = {0xDB41,1},
2121 D = {0x78a3, 0x1359, 0x4dca, 0x75eb, 0xd8ab, 0x4141, 0x0a4d, 0x0070, 0xe898, 0x7779, 0x4079, 0x8cc7, 0xfe73, 0x2b6f, 0x6cee, 0x5203},
2222 D2 = {0xf159, 0x26b2, 0x9b94, 0xebd6, 0xb156, 0x8283, 0x149a, 0x00e0, 0xd130, 0xeef3, 0x80f2, 0x198e, 0xfce7, 0x56df, 0xd9dc, 0x2406},
2323 X = {0xd51a, 0x8f25, 0x2d60, 0xc956, 0xa7b2, 0x9525, 0xc760, 0x692c, 0xdc5c, 0xfdd6, 0xe231, 0xc0a4, 0x53fe, 0xcd6e, 0x36d3, 0x2169},
194194 Z(a,a,c);
195195 S(b,a);
196196 Z(c,d,f);
197 M(a,c,_121665);
197 M(a,c,gf121665);
198198 A(a,a,d);
199199 M(c,c,a);
200200 M(a,d,f);
217217
218218 int tweetnacl_crypto_scalarmult_base(u8 *q,const u8 *n)
219219 {
220 return tweetnacl_crypto_scalarmult(q,n,_9);
220 return tweetnacl_crypto_scalarmult(q,n,nine);
221221 }
222222
223223 static int tweetnacl_crypto_hash(u8 *out,const u8 *m,u64 n)
77 static const struct {
88 const char *OID;
99 const char *names[6];
10 } _curve_names[] = {
10 } s_curve_names[] = {
1111 #ifdef LTC_ECC_SECP112R1
1212 {
1313 "1.3.132.0.6", { "SECP112R1", "ECC-112", NULL }
184184 };
185185
186186 /* case-insensitive match + ignore '-', '_', ' ' */
187 static int _name_match(const char *left, const char *right)
187 static int s_name_match(const char *left, const char *right)
188188 {
189189 char lc_r, lc_l;
190190
215215
216216 *cu = NULL;
217217
218 for (i = 0; _curve_names[i].OID != NULL && !OID; i++) {
219 if (XSTRCMP(_curve_names[i].OID, name_or_oid) == 0) {
220 OID = _curve_names[i].OID;
218 for (i = 0; s_curve_names[i].OID != NULL && !OID; i++) {
219 if (XSTRCMP(s_curve_names[i].OID, name_or_oid) == 0) {
220 OID = s_curve_names[i].OID;
221221 }
222 for (j = 0; _curve_names[i].names[j] != NULL && !OID; j++) {
223 if (_name_match(_curve_names[i].names[j], name_or_oid)) {
224 OID = _curve_names[i].OID;
222 for (j = 0; s_curve_names[i].names[j] != NULL && !OID; j++) {
223 if (s_name_match(s_curve_names[i].names[j], name_or_oid)) {
224 OID = s_curve_names[i].OID;
225225 }
226226 }
227227 }
44
55 #ifdef LTC_MECC
66
7 static int _ecc_import_private_with_oid(const unsigned char *in, unsigned long inlen, ecc_key *key)
7 static int s_ecc_import_private_with_oid(const unsigned char *in, unsigned long inlen, ecc_key *key)
88 {
99 ltc_asn1_list seq_priv[4], custom[2];
1010 unsigned char bin_xy[2*ECC_MAXSIZE+2], bin_k[ECC_MAXSIZE];
3838 return err;
3939 }
4040
41 static int _ecc_import_private_with_curve(const unsigned char *in, unsigned long inlen, ecc_key *key)
41 static int s_ecc_import_private_with_curve(const unsigned char *in, unsigned long inlen, ecc_key *key)
4242 {
4343 void *prime, *order, *a, *b, *gx, *gy;
4444 ltc_asn1_list seq_fieldid[2], seq_curve[3], seq_ecparams[6], seq_priv[4], custom[2];
106106 goto success;
107107 }
108108
109 if ((err = _ecc_import_private_with_oid(in, inlen, key)) == CRYPT_OK) {
109 if ((err = s_ecc_import_private_with_oid(in, inlen, key)) == CRYPT_OK) {
110110 goto success;
111111 }
112112
113 err = _ecc_import_private_with_curve(in, inlen, key);
113 err = s_ecc_import_private_with_curve(in, inlen, key);
114114
115115 success:
116116 return err;
1616 list[LTC_SDFC_temp##__LINE__].pp = P; \
1717 } while (0)
1818
19 static int _der_flexi_sequence_cmp(const ltc_asn1_list *flexi, der_flexi_check *check)
19 static int s_der_flexi_sequence_cmp(const ltc_asn1_list *flexi, der_flexi_check *check)
2020 {
2121 const ltc_asn1_list *cur;
2222 if (flexi->type != LTC_ASN1_SEQUENCE) {
3434 return CRYPT_OK;
3535 }
3636
37 /* NOTE: _der_decode_pkcs8_flexi & related stuff can be shared with rsa_import_pkcs8() */
37 /* NOTE: s_der_decode_pkcs8_flexi & related stuff can be shared with rsa_import_pkcs8() */
3838
3939 int ecc_import_pkcs8(const unsigned char *in, unsigned long inlen,
4040 const void *pwd, unsigned long pwdlen,
7272 LTC_SET_DER_FLEXI_CHECK(flexi_should, n++, LTC_ASN1_OCTET_STRING, &priv_key);
7373 LTC_SET_DER_FLEXI_CHECK(flexi_should, n, LTC_ASN1_EOL, NULL);
7474
75 if (((err = _der_flexi_sequence_cmp(l, flexi_should)) == CRYPT_OK) &&
75 if (((err = s_der_flexi_sequence_cmp(l, flexi_should)) == CRYPT_OK) &&
7676 (pk_oid_cmp_with_asn1(pka_ec_oid, seq->child) == CRYPT_OK)) {
7777 ltc_asn1_list *version, *field, *point, *point_g, *order, *p_cofactor;
7878
101101 if ((err = ecc_find_curve(OID, &curve)) != CRYPT_OK) { goto LBL_DONE; }
102102 if ((err = ecc_set_curve(curve, key)) != CRYPT_OK) { goto LBL_DONE; }
103103 }
104 else if ((err = _der_flexi_sequence_cmp(seq->child->next, flexi_should)) == CRYPT_OK) {
104 else if ((err = s_der_flexi_sequence_cmp(seq->child->next, flexi_should)) == CRYPT_OK) {
105105 /* CASE 2: explicit curve parameters (AKA long variant):
106106 * 0:d=0 hl=3 l= 227 cons: SEQUENCE
107107 * 3:d=1 hl=2 l= 1 prim: INTEGER :00
33
44 #ifdef LTC_MECC
55
6 static int _ecc_import_x509_with_oid(const unsigned char *in, unsigned long inlen, ecc_key *key)
6 static int s_ecc_import_x509_with_oid(const unsigned char *in, unsigned long inlen, ecc_key *key)
77 {
88 unsigned char bin_xy[2*ECC_MAXSIZE+2];
99 unsigned long curveoid[16];
2929 return err;
3030 }
3131
32 static int _ecc_import_x509_with_curve(const unsigned char *in, unsigned long inlen, ecc_key *key)
32 static int s_ecc_import_x509_with_curve(const unsigned char *in, unsigned long inlen, ecc_key *key)
3333 {
3434 void *prime, *order, *a, *b, *gx, *gy;
3535 ltc_asn1_list seq_fieldid[2], seq_curve[3], seq_ecparams[6];
8686 {
8787 int err;
8888
89 if ((err = _ecc_import_x509_with_oid(in, inlen, key)) == CRYPT_OK) {
89 if ((err = s_ecc_import_x509_with_oid(in, inlen, key)) == CRYPT_OK) {
9090 goto success;
9191 }
9292
93 err = _ecc_import_x509_with_curve(in, inlen, key);
93 err = s_ecc_import_x509_with_curve(in, inlen, key);
9494
9595 success:
9696 return err;
105105 */
106106 int ecc_import_x509(const unsigned char *in, unsigned long inlen, ecc_key *key)
107107 {
108 return x509_decode_public_key_from_certificate(in, inlen, PKA_EC, LTC_ASN1_EOL, NULL, NULL, NULL, key);
108 return x509_decode_public_key_from_certificate(in, inlen,
109 PKA_EC,
110 LTC_ASN1_EOL, NULL, NULL,
111 (public_key_decode_cb)ecc_import_subject_public_key_info, key);
109112 }
110113
111114 #endif /* LTC_MECC */
44
55 #ifdef LTC_MECC
66
7 static int _ecc_cmp_hex_bn(const char *left_hex, void *right_bn, void *tmp_bn)
7 static int s_ecc_cmp_hex_bn(const char *left_hex, void *right_bn, void *tmp_bn)
88 {
99 if (mp_read_radix(tmp_bn, left_hex, 16) != CRYPT_OK) return 0;
1010 if (mp_cmp(tmp_bn, right_bn) != LTC_MP_EQ) return 0;
1111 return 1;
1212 }
1313
14 static void _ecc_oid_lookup(ecc_key *key)
14 static void s_ecc_oid_lookup(ecc_key *key)
1515 {
1616 void *bn;
1717 const ltc_ecc_curve *curve;
1919 key->dp.oidlen = 0;
2020 if (mp_init(&bn) != CRYPT_OK) return;
2121 for (curve = ltc_ecc_curves; curve->prime != NULL; curve++) {
22 if (_ecc_cmp_hex_bn(curve->prime, key->dp.prime, bn) != 1) continue;
23 if (_ecc_cmp_hex_bn(curve->order, key->dp.order, bn) != 1) continue;
24 if (_ecc_cmp_hex_bn(curve->A, key->dp.A, bn) != 1) continue;
25 if (_ecc_cmp_hex_bn(curve->B, key->dp.B, bn) != 1) continue;
26 if (_ecc_cmp_hex_bn(curve->Gx, key->dp.base.x, bn) != 1) continue;
27 if (_ecc_cmp_hex_bn(curve->Gy, key->dp.base.y, bn) != 1) continue;
22 if (s_ecc_cmp_hex_bn(curve->prime, key->dp.prime, bn) != 1) continue;
23 if (s_ecc_cmp_hex_bn(curve->order, key->dp.order, bn) != 1) continue;
24 if (s_ecc_cmp_hex_bn(curve->A, key->dp.A, bn) != 1) continue;
25 if (s_ecc_cmp_hex_bn(curve->B, key->dp.B, bn) != 1) continue;
26 if (s_ecc_cmp_hex_bn(curve->Gx, key->dp.base.x, bn) != 1) continue;
27 if (s_ecc_cmp_hex_bn(curve->Gy, key->dp.base.y, bn) != 1) continue;
2828 if (key->dp.cofactor != curve->cofactor) continue;
2929 break; /* found */
3030 }
6565 for (i = 0; i < key->dp.oidlen; i++) key->dp.oid[i] = srckey->dp.oid[i];
6666 }
6767 else {
68 _ecc_oid_lookup(key); /* try to find OID in ltc_ecc_curves */
68 s_ecc_oid_lookup(key); /* try to find OID in ltc_ecc_curves */
6969 }
7070 /* success */
7171 return CRYPT_OK;
106106 key->dp.cofactor = cofactor;
107107 key->dp.size = mp_unsigned_bin_size(prime);
108108 /* try to find OID in ltc_ecc_curves */
109 _ecc_oid_lookup(key);
109 s_ecc_oid_lookup(key);
110110 /* success */
111111 return CRYPT_OK;
112112
66 Curve/OID to SSH+ECDSA name string mapping per RFC5656
77 Russ Williams
88 */
9
10 #ifdef LTC_SSH
911
1012 /**
1113 Curve/OID to SSH+ECDSA name string mapping
5961 return err;
6062 }
6163
64 #endif
88
99 #ifdef LTC_CURVE25519
1010
11 static int _ed25519_decode(const unsigned char *in, unsigned long inlen, curve25519_key *key)
11 static int s_ed25519_decode(const unsigned char *in, unsigned long inlen, curve25519_key *key)
1212 {
1313 if (inlen != sizeof(key->pub)) return CRYPT_PK_INVALID_SIZE;
1414 XMEMCPY(key->pub, in, sizeof(key->pub));
3232 if ((err = x509_decode_public_key_from_certificate(in, inlen,
3333 PKA_ED25519,
3434 LTC_ASN1_EOL, NULL, NULL,
35 (public_key_decode_cb)_ed25519_decode, key)) != CRYPT_OK) {
35 (public_key_decode_cb)s_ed25519_decode, key)) != CRYPT_OK) {
3636 return err;
3737 }
3838 key->type = PK_PUBLIC;
88
99 #ifdef LTC_MRSA
1010
11 static int _rsa_decode(const unsigned char *in, unsigned long inlen, rsa_key *key)
11 static int s_rsa_decode(const unsigned char *in, unsigned long inlen, rsa_key *key)
1212 {
1313 /* now it should be SEQUENCE { INTEGER, INTEGER } */
1414 return der_decode_sequence_multi(in, inlen,
4040 if ((err = x509_decode_public_key_from_certificate(in, inlen,
4141 PKA_RSA, LTC_ASN1_NULL,
4242 NULL, NULL,
43 (public_key_decode_cb)_rsa_decode, key)) != CRYPT_OK) {
43 (public_key_decode_cb)s_rsa_decode, key)) != CRYPT_OK) {
4444 rsa_free(key);
4545 } else {
4646 key->type = PK_PUBLIC;
1010 #ifdef LTC_MRSA
1111 #include <stdarg.h>
1212
13 static void _mpi_shrink_multi(void **a, ...)
13 static void s_mpi_shrink_multi(void **a, ...)
1414 {
1515 void **cur;
1616 unsigned n;
7575 void rsa_shrink_key(rsa_key *key)
7676 {
7777 LTC_ARGCHKVD(key != NULL);
78 _mpi_shrink_multi(&key->e, &key->d, &key->N, &key->dQ, &key->dP, &key->qP, &key->p, &key->q, NULL);
78 s_mpi_shrink_multi(&key->e, &key->d, &key->N, &key->dQ, &key->dP, &key->qP, &key->p, &key->q, NULL);
7979 }
8080
8181 /**
88
99 #ifdef LTC_CURVE25519
1010
11 static int _x25519_decode(const unsigned char *in, unsigned long inlen, curve25519_key *key)
11 static int s_x25519_decode(const unsigned char *in, unsigned long inlen, curve25519_key *key)
1212 {
1313 if (inlen != sizeof(key->pub)) return CRYPT_PK_INVALID_SIZE;
1414 XMEMCPY(key->pub, in, sizeof(key->pub));
3232 if ((err = x509_decode_public_key_from_certificate(in, inlen,
3333 PKA_X25519,
3434 LTC_ASN1_EOL, NULL, NULL,
35 (public_key_decode_cb)_x25519_decode, key)) != CRYPT_OK) {
35 (public_key_decode_cb)s_x25519_decode, key)) != CRYPT_OK) {
3636 return err;
3737 }
3838 key->type = PK_PUBLIC;
143143 @param prng The PRNG to export
144144 @return CRYPT_OK if successful
145145 */
146 _LTC_PRNG_EXPORT(chacha20_prng)
146 LTC_PRNG_EXPORT(chacha20_prng)
147147
148148 /**
149149 Import a PRNG state
5252 };
5353
5454 /* update the IV */
55 static void _fortuna_update_iv(prng_state *prng)
55 static void s_fortuna_update_iv(prng_state *prng)
5656 {
5757 int x;
5858 unsigned char *IV;
6666
6767 #ifdef LTC_FORTUNA_RESEED_RATELIMIT_TIMED
6868 /* get the current time in 100ms steps */
69 static ulong64 _fortuna_current_time(void)
69 static ulong64 s_fortuna_current_time(void)
7070 {
7171 ulong64 cur_time;
7272 #if defined(_WIN32)
9292 #endif
9393
9494 /* reseed the PRNG */
95 static int _fortuna_reseed(prng_state *prng)
95 static int s_fortuna_reseed(prng_state *prng)
9696 {
9797 unsigned char tmp[MAXBLOCKSIZE];
9898 hash_state md;
100100 int err, x;
101101
102102 #ifdef LTC_FORTUNA_RESEED_RATELIMIT_TIMED
103 ulong64 now = _fortuna_current_time();
103 ulong64 now = s_fortuna_current_time();
104104 if (now == prng->u.fortuna.wd) {
105105 return CRYPT_OK;
106106 }
148148 if ((err = rijndael_setup(prng->u.fortuna.K, 32, 0, &prng->u.fortuna.skey)) != CRYPT_OK) {
149149 return err;
150150 }
151 _fortuna_update_iv(prng);
151 s_fortuna_update_iv(prng);
152152
153153 /* reset/update internals */
154154 prng->u.fortuna.pool0_len = 0;
197197 if ((err = sha256_done(&md, prng->u.fortuna.K)) != CRYPT_OK) {
198198 goto LBL_UNLOCK;
199199 }
200 _fortuna_update_iv(prng);
200 s_fortuna_update_iv(prng);
201201
202202 LBL_UNLOCK:
203203 LTC_MUTEX_UNLOCK(&prng->lock);
248248 return CRYPT_OK;
249249 }
250250
251 static int _fortuna_add(unsigned long source, unsigned long pool, const unsigned char *in, unsigned long inlen, prng_state *prng)
251 static int s_fortuna_add(unsigned long source, unsigned long pool, const unsigned char *in, unsigned long inlen, prng_state *prng)
252252 {
253253 unsigned char tmp[2];
254254 int err;
295295
296296 LTC_MUTEX_LOCK(&prng->lock);
297297
298 err = _fortuna_add(source, pool, in, inlen, prng);
298 err = s_fortuna_add(source, pool, in, inlen, prng);
299299
300300 LTC_MUTEX_UNLOCK(&prng->lock);
301301
319319
320320 LTC_MUTEX_LOCK(&prng->lock);
321321
322 err = _fortuna_add(0, prng->u.fortuna.pool_idx, in, inlen, prng);
322 err = s_fortuna_add(0, prng->u.fortuna.pool_idx, in, inlen, prng);
323323
324324 if (err == CRYPT_OK) {
325325 ++(prng->u.fortuna.pool_idx);
345345 /* make sure the reseed doesn't fail because
346346 * of the chosen rate limit */
347347 #ifdef LTC_FORTUNA_RESEED_RATELIMIT_TIMED
348 prng->u.fortuna.wd = _fortuna_current_time() - 1;
348 prng->u.fortuna.wd = s_fortuna_current_time() - 1;
349349 #else
350350 prng->u.fortuna.wd = LTC_FORTUNA_WD;
351351 #endif
352 err = _fortuna_reseed(prng);
352 err = s_fortuna_reseed(prng);
353353 prng->ready = (err == CRYPT_OK) ? 1 : 0;
354354
355355 LTC_MUTEX_UNLOCK(&prng->lock);
378378
379379 /* do we have to reseed? */
380380 if (prng->u.fortuna.pool0_len >= 64) {
381 if (_fortuna_reseed(prng) != CRYPT_OK) {
381 if (s_fortuna_reseed(prng) != CRYPT_OK) {
382382 goto LBL_UNLOCK;
383383 }
384384 }
397397 rijndael_ecb_encrypt(prng->u.fortuna.IV, out, &prng->u.fortuna.skey);
398398 out += 16;
399399 outlen -= 16;
400 _fortuna_update_iv(prng);
400 s_fortuna_update_iv(prng);
401401 }
402402
403403 /* left over bytes? */
404404 if (outlen > 0) {
405405 rijndael_ecb_encrypt(prng->u.fortuna.IV, tmp, &prng->u.fortuna.skey);
406406 XMEMCPY(out, tmp, outlen);
407 _fortuna_update_iv(prng);
407 s_fortuna_update_iv(prng);
408408 }
409409
410410 /* generate new key */
411411 rijndael_ecb_encrypt(prng->u.fortuna.IV, prng->u.fortuna.K , &prng->u.fortuna.skey);
412 _fortuna_update_iv(prng);
412 s_fortuna_update_iv(prng);
413413
414414 rijndael_ecb_encrypt(prng->u.fortuna.IV, prng->u.fortuna.K+16, &prng->u.fortuna.skey);
415 _fortuna_update_iv(prng);
415 s_fortuna_update_iv(prng);
416416
417417 if (rijndael_setup(prng->u.fortuna.K, 32, 0, &prng->u.fortuna.skey) != CRYPT_OK) {
418418 tlen = 0;
466466 @param prng The PRNG to export
467467 @return CRYPT_OK if successful
468468 */
469 _LTC_PRNG_EXPORT(fortuna)
469 LTC_PRNG_EXPORT(fortuna)
470470
471471 /**
472472 Import a PRNG state
146146 @param prng The PRNG to export
147147 @return CRYPT_OK if successful
148148 */
149 _LTC_PRNG_EXPORT(rc4)
149 LTC_PRNG_EXPORT(rc4)
150150
151151 /**
152152 Import a PRNG state
99
1010 #if defined(LTC_DEVRANDOM) && !defined(_WIN32)
1111 /* on *NIX read /dev/random */
12 static unsigned long _rng_nix(unsigned char *buf, unsigned long len,
12 static unsigned long s_rng_nix(unsigned char *buf, unsigned long len,
1313 void (*callback)(void))
1414 {
1515 #ifdef LTC_NO_FILE
5252
5353 #define ANSI_RNG
5454
55 static unsigned long _rng_ansic(unsigned char *buf, unsigned long len,
55 static unsigned long s_rng_ansic(unsigned char *buf, unsigned long len,
5656 void (*callback)(void))
5757 {
5858 clock_t t1;
9393 #include <windows.h>
9494 #include <wincrypt.h>
9595
96 static unsigned long _rng_win32(unsigned char *buf, unsigned long len,
96 static unsigned long s_rng_win32(unsigned char *buf, unsigned long len,
9797 void (*callback)(void))
9898 {
9999 HCRYPTPROV hProv = 0;
139139 #endif
140140
141141 #if defined(_WIN32) || defined(_WIN32_WCE)
142 x = _rng_win32(out, outlen, callback); if (x != 0) { return x; }
142 x = s_rng_win32(out, outlen, callback); if (x != 0) { return x; }
143143 #elif defined(LTC_DEVRANDOM)
144 x = _rng_nix(out, outlen, callback); if (x != 0) { return x; }
144 x = s_rng_nix(out, outlen, callback); if (x != 0) { return x; }
145145 #endif
146146 #ifdef ANSI_RNG
147 x = _rng_ansic(out, outlen, callback); if (x != 0) { return x; }
147 x = s_rng_ansic(out, outlen, callback); if (x != 0) { return x; }
148148 #endif
149149 return 0;
150150 }
145145 @param prng The PRNG to export
146146 @return CRYPT_OK if successful
147147 */
148 _LTC_PRNG_EXPORT(sober128)
148 LTC_PRNG_EXPORT(sober128)
149149
150150 /**
151151 Import a PRNG state
266266 @param prng The PRNG to export
267267 @return CRYPT_OK if successful
268268 */
269 _LTC_PRNG_EXPORT(yarrow)
269 LTC_PRNG_EXPORT(yarrow)
270270
271271 /**
272272 Import a PRNG state
1515 x[a] += x[b]; x[d] = ROL(x[d] ^ x[a], 8); \
1616 x[c] += x[d]; x[b] = ROL(x[b] ^ x[c], 7);
1717
18 static void _chacha_block(unsigned char *output, const ulong32 *input, int rounds)
18 static void s_chacha_block(unsigned char *output, const ulong32 *input, int rounds)
1919 {
2020 ulong32 x[16];
2121 int i;
6565 in += j;
6666 }
6767 for (;;) {
68 _chacha_block(buf, st->input, st->rounds);
68 s_chacha_block(buf, st->input, st->rounds);
6969 if (st->ivlen == 8) {
7070 /* IV-64bit, increment 64bit counter */
7171 if (0 == ++st->input[12] && 0 == ++st->input[13]) return CRYPT_OVERFLOW;
6060 #ifdef LTC_RABBIT
6161
6262 /* local/private prototypes (NB: rabbit_ctx and rabbit_state are different) */
63 static LTC_INLINE ulong32 _rabbit_g_func(ulong32 x);
64 static LTC_INLINE void _rabbit_next_state(rabbit_ctx *p_instance);
65 static LTC_INLINE void _rabbit_gen_1_block(rabbit_state* st, unsigned char *out);
63 static LTC_INLINE ulong32 ss_rabbit_g_func(ulong32 x);
64 static LTC_INLINE void ss_rabbit_next_state(rabbit_ctx *p_instance);
65 static LTC_INLINE void ss_rabbit_gen_1_block(rabbit_state* st, unsigned char *out);
6666
6767 /* -------------------------------------------------------------------------- */
6868
6969 /* Square a 32-bit unsigned integer to obtain the 64-bit result and return */
7070 /* the upper 32 bits XOR the lower 32 bits */
71 static LTC_INLINE ulong32 _rabbit_g_func(ulong32 x)
71 static LTC_INLINE ulong32 ss_rabbit_g_func(ulong32 x)
7272 {
7373 ulong32 a, b, h, l;
7474
8787 /* -------------------------------------------------------------------------- */
8888
8989 /* Calculate the next internal state */
90 static LTC_INLINE void _rabbit_next_state(rabbit_ctx *p_instance)
90 static LTC_INLINE void ss_rabbit_next_state(rabbit_ctx *p_instance)
9191 {
9292 ulong32 g[8], c_old[8], i;
9393
109109
110110 /* Calculate the g-values */
111111 for (i=0;i<8;i++) {
112 g[i] = _rabbit_g_func((ulong32)(p_instance->x[i] + p_instance->c[i]));
112 g[i] = ss_rabbit_g_func((ulong32)(p_instance->x[i] + p_instance->c[i]));
113113 }
114114
115115 /* Calculate new state values */
125125
126126 /* ------------------------------------------------------------------------- */
127127
128 static LTC_INLINE void _rabbit_gen_1_block(rabbit_state* st, unsigned char *out)
128 static LTC_INLINE void ss_rabbit_gen_1_block(rabbit_state* st, unsigned char *out)
129129 {
130130 ulong32 *ptr;
131131
132132 /* Iterate the work context once */
133 _rabbit_next_state(&(st->work_ctx));
133 ss_rabbit_next_state(&(st->work_ctx));
134134
135135 /* Generate 16 bytes of pseudo-random data */
136136 ptr = (ulong32*)&(st->work_ctx.x);
194194
195195 /* Iterate the master context four times */
196196 for (i=0; i<4; i++) {
197 _rabbit_next_state(&(st->master_ctx));
197 ss_rabbit_next_state(&(st->master_ctx));
198198 }
199199
200200 /* Modify the counters */
254254
255255 /* Iterate the work context four times */
256256 for (i=0; i<4; i++) {
257 _rabbit_next_state(&(st->work_ctx));
257 ss_rabbit_next_state(&(st->work_ctx));
258258 }
259259
260260 /* reset keystream buffer and unused count */
288288 }
289289 for (;;) {
290290 /* gen a block for buf */
291 _rabbit_gen_1_block(st, buf);
291 ss_rabbit_gen_1_block(st, buf);
292292 if (inlen <= 16) {
293293 /* XOR and send to out */
294294 for (i = 0; i < inlen; ++i) out[i] = in[i] ^ buf[i];
1616 x[d] ^= (ROL((x[c] + x[b]), 13)); \
1717 x[a] ^= (ROL((x[d] + x[c]), 18));
1818
19 static void _salsa20_block(unsigned char *output, const ulong32 *input, int rounds)
19 static void s_salsa20_block(unsigned char *output, const ulong32 *input, int rounds)
2020 {
2121 ulong32 x[16];
2222 int i;
6666 in += j;
6767 }
6868 for (;;) {
69 _salsa20_block(buf, st->input, st->rounds);
69 s_salsa20_block(buf, st->input, st->rounds);
7070 /* Salsa20: 64-bit IV, increment 64-bit counter */
7171 if (0 == ++st->input[8] && 0 == ++st->input[9]) return CRYPT_OVERFLOW;
7272 if (inlen <= 64) {
2020 x[a] ^= (ROL((x[d] + x[c]), 18));
2121
2222 /* use modified salsa20 doubleround (no final addition as in salsa20) */
23 static void _xsalsa20_doubleround(ulong32 *x, int rounds)
23 static void s_xsalsa20_doubleround(ulong32 *x, int rounds)
2424 {
2525 int i;
2626
8686 LOAD32L(x[ 9], nonce + 12);
8787
8888 /* use modified salsa20 doubleround (no final addition) */
89 _xsalsa20_doubleround(x, rounds);
89 s_xsalsa20_doubleround(x, rounds);
9090
9191 /* extract the subkey */
9292 for (i = 0; i < 8; ++i) {
99
1010 #ifdef LTC_SOBER128
1111
12 #define __LTC_SOBER128TAB_C__
12 #define LTC_SOBER128TAB_C
1313 #include "sober128tab.c"
1414
1515 /* don't change these... */
55 SOBER-128 Tables
66 */
77
8 #ifdef __LTC_SOBER128TAB_C__
8 #ifdef LTC_SOBER128TAB_C
99
1010 /* $ID$ */
1111 /* @(#)TuringMultab.h 1.3 (QUALCOMM) 02/09/03 */
162162 0xf9e6053f, 0xa4b0d300, 0xd499cbcc, 0xb95e3d40,
163163 };
164164
165 #endif /* __LTC_SOBER128TAB_C__ */
165 #endif /* LTC_SOBER128TAB_C */
578578 * Compute the next block of bits of output stream. This is equivalent
579579 * to one full rotation of the shift register.
580580 */
581 static LTC_INLINE void _sosemanuk_internal(sosemanuk_state *st)
581 static LTC_INLINE void s_sosemanuk_internal(sosemanuk_state *st)
582582 {
583583 /*
584584 * MUL_A(x) computes alpha * x (in F_{2^32}).
716716 * or in2[] is not allowed. Total overlap (out == in1 and/or out == in2)
717717 * is allowed.
718718 */
719 static LTC_INLINE void _xorbuf(const unsigned char *in1, const unsigned char *in2,
719 static LTC_INLINE void s_xorbuf(const unsigned char *in1, const unsigned char *in2,
720720 unsigned char *out, unsigned long datalen)
721721 {
722722 while (datalen -- > 0) {
749749 if (rlen > inlen) {
750750 rlen = inlen;
751751 }
752 _xorbuf(st->buf + st->ptr, in, out, rlen);
752 s_xorbuf(st->buf + st->ptr, in, out, rlen);
753753 in += rlen;
754754 out += rlen;
755755 inlen -= rlen;
756756 st->ptr += rlen;
757757 }
758758 while (inlen > 0) {
759 _sosemanuk_internal(st);
759 s_sosemanuk_internal(st);
760760 if (inlen >= sizeof(st->buf)) {
761 _xorbuf(st->buf, in, out, sizeof(st->buf));
761 s_xorbuf(st->buf, in, out, sizeof(st->buf));
762762 in += sizeof(st->buf);
763763 out += sizeof(st->buf);
764764 inlen -= sizeof(st->buf);
765765 } else {
766 _xorbuf(st->buf, in, out, inlen);
766 s_xorbuf(st->buf, in, out, inlen);
767767 st->ptr = inlen;
768768 inlen = 0;
769769 }