Codebase list libcryptx-perl / f04674a
ltc sync whitespaces Karel Miko 7 years ago
105 changed file(s) with 952 addition(s) and 952 deletion(s). Raw diff Collapse all Expand all
674674 }
675675 };
676676
677 symmetric_key key;
678 unsigned char tmp[2][16];
679 int i, y;
680
681 for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
677 symmetric_key key;
678 unsigned char tmp[2][16];
679 int i, y;
680
681 for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
682682 zeromem(&key, sizeof(key));
683683 if ((err = rijndael_setup(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) {
684684 return err;
706706 return CRYPT_FAIL_TESTVECTOR;
707707 }
708708
709 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
710 for (y = 0; y < 16; y++) tmp[0][y] = 0;
711 for (y = 0; y < 1000; y++) rijndael_ecb_encrypt(tmp[0], tmp[0], &key);
712 for (y = 0; y < 1000; y++) rijndael_ecb_decrypt(tmp[0], tmp[0], &key);
713 for (y = 0; y < 16; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR;
714 }
715 return CRYPT_OK;
709 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
710 for (y = 0; y < 16; y++) tmp[0][y] = 0;
711 for (y = 0; y < 1000; y++) rijndael_ecb_encrypt(tmp[0], tmp[0], &key);
712 for (y = 0; y < 1000; y++) rijndael_ecb_decrypt(tmp[0], tmp[0], &key);
713 for (y = 0; y < 16; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR;
714 }
715 return CRYPT_OK;
716716 #endif
717717 }
718718
925925 return CRYPT_INVALID_ROUNDS;
926926 }
927927
928 /*
929 * map cipher key to initial key state (mu):
930 */
931 for (i = 0, pos = 0; i < N; i++, pos += 4) {
928 /*
929 * map cipher key to initial key state (mu):
930 */
931 for (i = 0, pos = 0; i < N; i++, pos += 4) {
932932 kappa[i] =
933933 (((ulong32)key[pos ]) << 24) ^
934934 (((ulong32)key[pos + 1]) << 16) ^
935935 (((ulong32)key[pos + 2]) << 8) ^
936936 (((ulong32)key[pos + 3]) );
937 }
937 }
938938
939939 /*
940940 * generate R + 1 round keys:
19821982 return CRYPT_FAIL_TESTVECTOR;
19831983 }
19841984
1985 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
1986 for (y = 0; y < 8; y++) tmp[y] = 0;
1987 for (y = 0; y < 1000; y++) des_ecb_encrypt(tmp, tmp, &des);
1988 for (y = 0; y < 1000; y++) des_ecb_decrypt(tmp, tmp, &des);
1989 for (y = 0; y < 8; y++) if (tmp[y] != 0) return CRYPT_FAIL_TESTVECTOR;
1990 }
1985 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
1986 for (y = 0; y < 8; y++) tmp[y] = 0;
1987 for (y = 0; y < 1000; y++) des_ecb_encrypt(tmp, tmp, &des);
1988 for (y = 0; y < 1000; y++) des_ecb_decrypt(tmp, tmp, &des);
1989 for (y = 0; y < 8; y++) if (tmp[y] != 0) return CRYPT_FAIL_TESTVECTOR;
1990 }
19911991
19921992 return CRYPT_OK;
19931993 #endif
200200 */
201201 int kseed_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
202202 {
203 int i;
204 ulong32 tmp, k1, k2, k3, k4;
205
206 if (keylen != 16) {
207 return CRYPT_INVALID_KEYSIZE;
208 }
209
210 if (num_rounds != 16 && num_rounds != 0) {
211 return CRYPT_INVALID_ROUNDS;
212 }
213
214 /* load key */
215 LOAD32H(k1, key);
216 LOAD32H(k2, key+4);
217 LOAD32H(k3, key+8);
218 LOAD32H(k4, key+12);
219
220 for (i = 0; i < 16; i++) {
221 skey->kseed.K[2*i+0] = G(k1 + k3 - KCi[i]);
222 skey->kseed.K[2*i+1] = G(k2 - k4 + KCi[i]);
223 if (i&1) {
224 tmp = k3;
225 k3 = ((k3 << 8) | (k4 >> 24)) & 0xFFFFFFFF;
226 k4 = ((k4 << 8) | (tmp >> 24)) & 0xFFFFFFFF;
227 } else {
228 tmp = k1;
229 k1 = ((k1 >> 8) | (k2 << 24)) & 0xFFFFFFFF;
230 k2 = ((k2 >> 8) | (tmp << 24)) & 0xFFFFFFFF;
203 int i;
204 ulong32 tmp, k1, k2, k3, k4;
205
206 if (keylen != 16) {
207 return CRYPT_INVALID_KEYSIZE;
208 }
209
210 if (num_rounds != 16 && num_rounds != 0) {
211 return CRYPT_INVALID_ROUNDS;
212 }
213
214 /* load key */
215 LOAD32H(k1, key);
216 LOAD32H(k2, key+4);
217 LOAD32H(k3, key+8);
218 LOAD32H(k4, key+12);
219
220 for (i = 0; i < 16; i++) {
221 skey->kseed.K[2*i+0] = G(k1 + k3 - KCi[i]);
222 skey->kseed.K[2*i+1] = G(k2 - k4 + KCi[i]);
223 if (i&1) {
224 tmp = k3;
225 k3 = ((k3 << 8) | (k4 >> 24)) & 0xFFFFFFFF;
226 k4 = ((k4 << 8) | (tmp >> 24)) & 0xFFFFFFFF;
227 } else {
228 tmp = k1;
229 k1 = ((k1 >> 8) | (k2 << 24)) & 0xFFFFFFFF;
230 k2 = ((k2 >> 8) | (tmp << 24)) & 0xFFFFFFFF;
231231 }
232232 /* reverse keys for decrypt */
233233 skey->kseed.dK[2*(15-i)+0] = skey->kseed.K[2*i+0];
234234 skey->kseed.dK[2*(15-i)+1] = skey->kseed.K[2*i+1];
235 }
236
237 return CRYPT_OK;
235 }
236
237 return CRYPT_OK;
238238 }
239239
240240 static void rounds(ulong32 *P, ulong32 *K)
302302 return CRYPT_FAIL_TESTVECTOR;
303303 }
304304
305 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
306 for (y = 0; y < 16; y++) tmp[0][y] = 0;
307 for (y = 0; y < 1000; y++) noekeon_ecb_encrypt(tmp[0], tmp[0], &key);
308 for (y = 0; y < 1000; y++) noekeon_ecb_decrypt(tmp[0], tmp[0], &key);
309 for (y = 0; y < 16; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR;
305 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
306 for (y = 0; y < 16; y++) tmp[0][y] = 0;
307 for (y = 0; y < 1000; y++) noekeon_ecb_encrypt(tmp[0], tmp[0], &key);
308 for (y = 0; y < 1000; y++) noekeon_ecb_decrypt(tmp[0], tmp[0], &key);
309 for (y = 0; y < 16; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR;
310310 }
311311 return CRYPT_OK;
312312 #endif
4141 /* form B_0 == flags | Nonce N | l(m) */
4242 x = 0;
4343 ccm->PAD[x++] = (unsigned char)(((ccm->aadlen > 0) ? (1<<6) : 0) |
44 (((ccm->taglen - 2)>>1)<<3) |
45 (ccm->L-1));
44 (((ccm->taglen - 2)>>1)<<3) |
45 (ccm->L-1));
4646
4747 /* nonce */
4848 for (y = 0; y < (16 - (ccm->L + 1)); y++) {
77 *
88 * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
99 */
10 /**
10 /**
1111 @file eax_addheader.c
12 EAX implementation, add meta-data, by Tom St Denis
12 EAX implementation, add meta-data, by Tom St Denis
1313 */
1414 #include "tomcrypt.h"
1515
1616 #ifdef LTC_EAX_MODE
1717
18 /**
19 add header (metadata) to the stream
18 /**
19 add header (metadata) to the stream
2020 @param eax The current EAX state
2121 @param header The header (meta-data) data you wish to add to the state
2222 @param length The length of the header data
2323 @return CRYPT_OK if successful
2424 */
25 int eax_addheader(eax_state *eax, const unsigned char *header,
25 int eax_addheader(eax_state *eax, const unsigned char *header,
2626 unsigned long length)
2727 {
2828 LTC_ARGCHK(eax != NULL);
88 * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
99 */
1010
11 /**
11 /**
1212 @file eax_decrypt.c
1313 EAX implementation, decrypt block, by Tom St Denis
1414 */
1616
1717 #ifdef LTC_EAX_MODE
1818
19 /**
19 /**
2020 Decrypt data with the EAX protocol
2121 @param eax The EAX state
2222 @param ct The ciphertext
2424 @param length The length (octets) of the ciphertext
2525 @return CRYPT_OK if successful
2626 */
27 int eax_decrypt(eax_state *eax, const unsigned char *ct, unsigned char *pt,
27 int eax_decrypt(eax_state *eax, const unsigned char *ct, unsigned char *pt,
2828 unsigned long length)
2929 {
3030 int err;
31
31
3232 LTC_ARGCHK(eax != NULL);
3333 LTC_ARGCHK(pt != NULL);
3434 LTC_ARGCHK(ct != NULL);
7676 if ((err = eax_decrypt(eax, ct, pt, ctlen)) != CRYPT_OK) {
7777 goto LBL_ERR;
7878 }
79
79
8080 buflen = taglen;
8181 if ((err = eax_done(eax, buf, &buflen)) != CRYPT_OK) {
8282 goto LBL_ERR;
8686 if (buflen >= taglen && XMEMCMP(buf, tag, taglen) == 0) {
8787 *stat = 1;
8888 }
89
89
9090 err = CRYPT_OK;
9191 LBL_ERR:
9292 #ifdef LTC_CLEAN_STACK
5050 /* finish ctomac */
5151 len = MAXBLOCKSIZE;
5252 if ((err = omac_done(&eax->ctomac, ctmac, &len)) != CRYPT_OK) {
53 goto LBL_ERR;
53 goto LBL_ERR;
5454 }
5555
5656 /* finish headeromac */
5858 /* note we specifically don't reset len so the two lens are minimal */
5959
6060 if ((err = omac_done(&eax->headeromac, headermac, &len)) != CRYPT_OK) {
61 goto LBL_ERR;
61 goto LBL_ERR;
6262 }
6363
6464 /* terminate the CTR chain */
1010
1111 /**
1212 @file eax_encrypt.c
13 EAX implementation, encrypt block by Tom St Denis
13 EAX implementation, encrypt block by Tom St Denis
1414 */
1515 #include "tomcrypt.h"
1616
2424 @param length The length of the plaintext (octets)
2525 @return CRYPT_OK if successful
2626 */
27 int eax_encrypt(eax_state *eax, const unsigned char *pt, unsigned char *ct,
27 int eax_encrypt(eax_state *eax, const unsigned char *pt, unsigned char *ct,
2828 unsigned long length)
2929 {
3030 int err;
31
31
3232 LTC_ARGCHK(eax != NULL);
3333 LTC_ARGCHK(pt != NULL);
3434 LTC_ARGCHK(ct != NULL);
5252 eax = XMALLOC(sizeof(*eax));
5353
5454 if ((err = eax_init(eax, cipher, key, keylen, nonce, noncelen, header, headerlen)) != CRYPT_OK) {
55 goto LBL_ERR;
55 goto LBL_ERR;
5656 }
5757
5858 if ((err = eax_encrypt(eax, pt, ct, ptlen)) != CRYPT_OK) {
59 goto LBL_ERR;
59 goto LBL_ERR;
6060 }
61
61
6262 if ((err = eax_done(eax, tag, taglen)) != CRYPT_OK) {
63 goto LBL_ERR;
63 goto LBL_ERR;
6464 }
6565
6666 err = CRYPT_OK;
7171
7272 XFREE(eax);
7373
74 return err;
74 return err;
7575 }
7676
7777 #endif
88 * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
99 */
1010
11 /**
11 /**
1212 @file eax_init.c
13 EAX implementation, initialized EAX state, by Tom St Denis
13 EAX implementation, initialized EAX state, by Tom St Denis
1414 */
1515 #include "tomcrypt.h"
1616
1717 #ifdef LTC_EAX_MODE
1818
19 /**
19 /**
2020 Initialized an EAX state
2121 @param eax [out] The EAX state to initialize
2222 @param cipher The index of the desired cipher
2828 @param headerlen The header length (octets)
2929 @return CRYPT_OK if successful
3030 */
31 int eax_init(eax_state *eax, int cipher,
31 int eax_init(eax_state *eax, int cipher,
3232 const unsigned char *key, unsigned long keylen,
3333 const unsigned char *nonce, unsigned long noncelen,
3434 const unsigned char *header, unsigned long headerlen)
6868 /* N = LTC_OMAC_0K(nonce) */
6969 zeromem(buf, MAXBLOCKSIZE);
7070 if ((err = omac_init(omac, cipher, key, keylen)) != CRYPT_OK) {
71 goto LBL_ERR;
71 goto LBL_ERR;
7272 }
7373
7474 /* omac the [0]_n */
7575 if ((err = omac_process(omac, buf, blklen)) != CRYPT_OK) {
76 goto LBL_ERR;
76 goto LBL_ERR;
7777 }
7878 /* omac the nonce */
7979 if ((err = omac_process(omac, nonce, noncelen)) != CRYPT_OK) {
80 goto LBL_ERR;
80 goto LBL_ERR;
8181 }
8282 /* store result */
8383 len = sizeof(eax->N);
8484 if ((err = omac_done(omac, eax->N, &len)) != CRYPT_OK) {
85 goto LBL_ERR;
85 goto LBL_ERR;
8686 }
8787
8888 /* H = LTC_OMAC_1K(header) */
9090 buf[blklen - 1] = 1;
9191
9292 if ((err = omac_init(&eax->headeromac, cipher, key, keylen)) != CRYPT_OK) {
93 goto LBL_ERR;
93 goto LBL_ERR;
9494 }
9595
9696 /* omac the [1]_n */
9797 if ((err = omac_process(&eax->headeromac, buf, blklen)) != CRYPT_OK) {
98 goto LBL_ERR;
98 goto LBL_ERR;
9999 }
100100 /* omac the header */
101101 if (headerlen != 0) {
102102 if ((err = omac_process(&eax->headeromac, header, headerlen)) != CRYPT_OK) {
103 goto LBL_ERR;
103 goto LBL_ERR;
104104 }
105105 }
106106
108108
109109 /* setup the CTR mode */
110110 if ((err = ctr_start(cipher, eax->N, key, keylen, 0, CTR_COUNTER_BIG_ENDIAN, &eax->ctr)) != CRYPT_OK) {
111 goto LBL_ERR;
111 goto LBL_ERR;
112112 }
113113
114114 /* setup the LTC_OMAC for the ciphertext */
115 if ((err = omac_init(&eax->ctomac, cipher, key, keylen)) != CRYPT_OK) {
116 goto LBL_ERR;
115 if ((err = omac_init(&eax->ctomac, cipher, key, keylen)) != CRYPT_OK) {
116 goto LBL_ERR;
117117 }
118118
119119 /* omac [2]_n */
120120 zeromem(buf, MAXBLOCKSIZE);
121121 buf[blklen-1] = 2;
122122 if ((err = omac_process(&eax->ctomac, buf, blklen)) != CRYPT_OK) {
123 goto LBL_ERR;
123 goto LBL_ERR;
124124 }
125125
126126 err = CRYPT_OK;
136136 return err;
137137 }
138138
139 #endif
139 #endif
140140
141141 /* $Source$ */
142142 /* $Revision$ */
2323 @param taglen [in/out] The length of the MAC tag
2424 @return CRYPT_OK on success
2525 */
26 int gcm_done(gcm_state *gcm,
26 int gcm_done(gcm_state *gcm,
2727 unsigned char *tag, unsigned long *taglen)
2828 {
2929 unsigned long x;
1616
1717 #if defined(LTC_GCM_TABLES) || defined(LTC_LRW_TABLES) || ((defined(LTC_GCM_MODE) || defined(LTC_GCM_MODE)) && defined(LTC_FAST))
1818
19 /* this is x*2^128 mod p(x) ... the results are 16 bytes each stored in a packed format. Since only the
19 /* this is x*2^128 mod p(x) ... the results are 16 bytes each stored in a packed format. Since only the
2020 * lower 16 bits are not zero'ed I removed the upper 14 bytes */
2121 const unsigned char gcm_shift_table[256*2] = {
2222 0x00, 0x00, 0x01, 0xc2, 0x03, 0x84, 0x02, 0x46, 0x07, 0x08, 0x06, 0xca, 0x04, 0x8c, 0x05, 0x4e,
7272 static const unsigned char mask[] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
7373 static const unsigned char poly[] = { 0x00, 0xE1 };
7474
75
75
7676 /**
7777 GCM GF multiplier (internal use only) bitserial
7878 @param a First value
7979 @param b Second value
8080 @param c Destination for a * b
81 */
81 */
8282 void gcm_gf_mult(const unsigned char *a, const unsigned char *b, unsigned char *c)
8383 {
8484 unsigned char Z[16], V[16];
8989 for (x = 0; x < 128; x++) {
9090 if (b[x>>3] & mask[x&7]) {
9191 for (y = 0; y < 16; y++) {
92 Z[y] ^= V[y];
92 Z[y] ^= V[y];
9393 }
9494 }
9595 z = V[15] & 0x01;
112112 @param a First value
113113 @param b Second value
114114 @param c Destination for a * b
115 */
115 */
116116 void gcm_gf_mult(const unsigned char *a, const unsigned char *b, unsigned char *c)
117117 {
118118 int i, j, k, u;
128128 LOAD32H(B[M(1)][i], a + (i<<2));
129129 LOAD32L(pB[i], b + (i<<2));
130130 }
131 #else
131 #else
132132 for (i = 0; i < 2; i++) {
133133 LOAD64H(B[M(1)][i], a + (i<<3));
134134 LOAD64L(pB[i], b + (i<<3));
153153 B[M(9)][i] = B[M(1)][i] ^ B[M(8)][i];
154154 B[M(10)][i] = B[M(2)][i] ^ B[M(8)][i];
155155 B[M(12)][i] = B[M(8)][i] ^ B[M(4)][i];
156
156
157157 /* now all 3 bit values and the only 4 bit value: 7, 11, 13, 14, 15 */
158158 B[M(7)][i] = B[M(3)][i] ^ B[M(4)][i];
159159 B[M(11)][i] = B[M(3)][i] ^ B[M(8)][i];
192192 for (i = 0; i < 8; i++) {
193193 STORE32H(tmp[i], pTmp + (i<<2));
194194 }
195 #else
195 #else
196196 for (i = 0; i < 4; i++) {
197197 STORE64H(tmp[i], pTmp + (i<<3));
198198 }
217217 /* $Source$ */
218218 /* $Revision$ */
219219 /* $Date$ */
220
220
2424 @param keylen The length of the secret key
2525 @return CRYPT_OK on success
2626 */
27 int gcm_init(gcm_state *gcm, int cipher,
27 int gcm_init(gcm_state *gcm, int cipher,
2828 const unsigned char *key, int keylen)
2929 {
3030 int err;
9191 }
9292 gcm->PC[x][y][0] = gcm_shift_table[t<<1];
9393 gcm->PC[x][y][1] ^= gcm_shift_table[(t<<1)+1];
94 }
95 }
94 }
95 }
9696
9797 #endif
9898
2121 @param cipher Index of cipher to use
2222 @param key The secret key
2323 @param keylen The length of the secret key
24 @param IV The initial vector
24 @param IV The initial vector
2525 @param IVlen The length of the initial vector
2626 @param adata The additional authentication data (header)
2727 @param adatalen The length of the adata
3838 const unsigned char *IV, unsigned long IVlen,
3939 const unsigned char *adata, unsigned long adatalen,
4040 unsigned char *pt, unsigned long ptlen,
41 unsigned char *ct,
41 unsigned char *ct,
4242 unsigned char *tag, unsigned long *taglen,
4343 int direction)
4444 {
4949 if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
5050 return err;
5151 }
52
52
5353 if (cipher_descriptor[cipher].accel_gcm_memory != NULL) {
54 return
54 return
5555 cipher_descriptor[cipher].accel_gcm_memory
5656 (key, keylen,
5757 IV, IVlen,
3232 gcm->buflen = 0;
3333 gcm->totlen = 0;
3434 gcm->pttotlen = 0;
35
35
3636 return CRYPT_OK;
3737 }
3838
8989
9090 /* compute L_$, L_0, L_1, ... */
9191 for (x = -1; x < 32; x++) {
92 if (x == -1) { /* gonna compute: L_$ = double(L_*) */
92 if (x == -1) { /* gonna compute: L_$ = double(L_*) */
9393 current = ocb->L_dollar;
9494 previous = ocb->L_star;
95 }
96 else if (x == 0) { /* gonna compute: L_0 = double(L_$) */
95 }
96 else if (x == 0) { /* gonna compute: L_0 = double(L_$) */
9797 current = ocb->L_[0];
9898 previous = ocb->L_dollar;
99 }
100 else { /* gonna compute: L_i = double(L_{i-1}) for every integer i > 0 */
99 }
100 else { /* gonna compute: L_i = double(L_{i-1}) for every integer i > 0 */
101101 current = ocb->L_[x];
102102 previous = ocb->L_[x-1];
103 }
104 m = previous[0] >> 7;
105 for (y = 0; y < ocb->block_len-1; y++) {
106 current[y] = ((previous[y] << 1) | (previous[y+1] >> 7)) & 255;
107 }
108 current[ocb->block_len-1] = (previous[ocb->block_len-1] << 1) & 255;
109 if (m == 1) {
110 /* current[] = current[] XOR polys[poly].poly_mul[]*/
111 ocb3_int_xor_blocks(current, current, polys[poly].poly_mul, ocb->block_len);
112 }
113 }
103 }
104 m = previous[0] >> 7;
105 for (y = 0; y < ocb->block_len-1; y++) {
106 current[y] = ((previous[y] << 1) | (previous[y+1] >> 7)) & 255;
107 }
108 current[ocb->block_len-1] = (previous[ocb->block_len-1] << 1) & 255;
109 if (m == 1) {
110 /* current[] = current[] XOR polys[poly].poly_mul[]*/
111 ocb3_int_xor_blocks(current, current, polys[poly].poly_mul, ocb->block_len);
112 }
113 }
114114
115 /* initialize ocb->Offset_current = Offset_0 */
116 ocb3_int_calc_offset_zero(ocb, nonce, noncelen);
115 /* initialize ocb->Offset_current = Offset_0 */
116 ocb3_int_calc_offset_zero(ocb, nonce, noncelen);
117117
118 /* initialize checksum to all zeros */
119 zeromem(ocb->checksum, ocb->block_len);
118 /* initialize checksum to all zeros */
119 zeromem(ocb->checksum, ocb->block_len);
120120
121 /* set block index */
122 ocb->block_index = 1;
121 /* set block index */
122 ocb->block_index = 1;
123123
124 /* initialize AAD related stuff */
125 ocb->ablock_index = 1;
126 ocb->adata_buffer_bytes = 0;
127 zeromem(ocb->aOffset_current, ocb->block_len);
128 zeromem(ocb->aSum_current, ocb->block_len);
124 /* initialize AAD related stuff */
125 ocb->ablock_index = 1;
126 ocb->adata_buffer_bytes = 0;
127 zeromem(ocb->aOffset_current, ocb->block_len);
128 zeromem(ocb->aSum_current, ocb->block_len);
129129
130 return CRYPT_OK;
130 return CRYPT_OK;
131131 }
132132
133133 #endif
1111
1212 /**
1313 @param md2.c
14 LTC_MD2 (RFC 1319) hash function implementation by Tom St Denis
14 LTC_MD2 (RFC 1319) hash function implementation by Tom St Denis
1515 */
1616
1717 #ifdef LTC_MD2
6363 L = md->md2.chksum[15];
6464 for (j = 0; j < 16; j++) {
6565
66 /* caution, the RFC says its "C[j] = S[M[i*16+j] xor L]" but the reference source code [and test vectors] say
66 /* caution, the RFC says its "C[j] = S[M[i*16+j] xor L]" but the reference source code [and test vectors] say
6767 otherwise.
6868 */
6969 L = (md->md2.chksum[j] ^= PI_SUBST[(int)(md->md2.buf[j] ^ L)] & 255);
7474 {
7575 int j, k;
7676 unsigned char t;
77
77
7878 /* copy block */
7979 for (j = 0; j < 16; j++) {
8080 md->md2.X[16+j] = md->md2.buf[j];
121121 unsigned long n;
122122 LTC_ARGCHK(md != NULL);
123123 LTC_ARGCHK(in != NULL);
124 if (md-> md2 .curlen > sizeof(md-> md2 .buf)) {
125 return CRYPT_INVALID_ARG;
126 }
124 if (md-> md2 .curlen > sizeof(md-> md2 .buf)) {
125 return CRYPT_INVALID_ARG;
126 }
127127 while (inlen > 0) {
128128 n = MIN(inlen, (16 - md->md2.curlen));
129129 XMEMCPY(md->md2.buf + md->md2.curlen, in, (size_t)n);
185185 /**
186186 Self-test the hash
187187 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
188 */
188 */
189189 int md2_test(void)
190190 {
191191 #ifndef LTC_TEST
192192 return CRYPT_NOP;
193 #else
193 #else
194194 static const struct {
195195 char *msg;
196196 unsigned char md[16];
238238 return CRYPT_FAIL_TESTVECTOR;
239239 }
240240 }
241 return CRYPT_OK;
241 return CRYPT_OK;
242242 #endif
243243 }
244244
1111
1212 /**
1313 @param md4.c
14 Submitted by Dobes Vandermeer (dobes@smartt.com)
14 Submitted by Dobes Vandermeer (dobes@smartt.com)
1515 */
1616
1717 #ifdef LTC_MD4
2222 6,
2323 16,
2424 64,
25
25
2626 /* OID */
2727 { 1, 2, 840, 113549, 2, 4, },
2828 6,
5555 /* ROTATE_LEFT rotates x left n bits. */
5656 #define ROTATE_LEFT(x, n) ROLc(x, n)
5757
58 /* FF, GG and HH are transformations for rounds 1, 2 and 3 */
59 /* Rotation is separate from addition to prevent recomputation */
58 /* FF, GG and HH are transformations for rounds 1, 2 and 3 */
59 /* Rotation is separate from addition to prevent recomputation */
6060
6161 #define FF(a, b, c, d, x, s) { \
6262 (a) += F ((b), (c), (d)) + (x); \
9090 for (i = 0; i < 16; i++) {
9191 LOAD32L(x[i], buf + (4*i));
9292 }
93
94 /* Round 1 */
95 FF (a, b, c, d, x[ 0], S11); /* 1 */
96 FF (d, a, b, c, x[ 1], S12); /* 2 */
97 FF (c, d, a, b, x[ 2], S13); /* 3 */
98 FF (b, c, d, a, x[ 3], S14); /* 4 */
99 FF (a, b, c, d, x[ 4], S11); /* 5 */
100 FF (d, a, b, c, x[ 5], S12); /* 6 */
101 FF (c, d, a, b, x[ 6], S13); /* 7 */
102 FF (b, c, d, a, x[ 7], S14); /* 8 */
103 FF (a, b, c, d, x[ 8], S11); /* 9 */
93
94 /* Round 1 */
95 FF (a, b, c, d, x[ 0], S11); /* 1 */
96 FF (d, a, b, c, x[ 1], S12); /* 2 */
97 FF (c, d, a, b, x[ 2], S13); /* 3 */
98 FF (b, c, d, a, x[ 3], S14); /* 4 */
99 FF (a, b, c, d, x[ 4], S11); /* 5 */
100 FF (d, a, b, c, x[ 5], S12); /* 6 */
101 FF (c, d, a, b, x[ 6], S13); /* 7 */
102 FF (b, c, d, a, x[ 7], S14); /* 8 */
103 FF (a, b, c, d, x[ 8], S11); /* 9 */
104104 FF (d, a, b, c, x[ 9], S12); /* 10 */
105 FF (c, d, a, b, x[10], S13); /* 11 */
105 FF (c, d, a, b, x[10], S13); /* 11 */
106106 FF (b, c, d, a, x[11], S14); /* 12 */
107107 FF (a, b, c, d, x[12], S11); /* 13 */
108 FF (d, a, b, c, x[13], S12); /* 14 */
109 FF (c, d, a, b, x[14], S13); /* 15 */
110 FF (b, c, d, a, x[15], S14); /* 16 */
111
112 /* Round 2 */
113 GG (a, b, c, d, x[ 0], S21); /* 17 */
114 GG (d, a, b, c, x[ 4], S22); /* 18 */
115 GG (c, d, a, b, x[ 8], S23); /* 19 */
116 GG (b, c, d, a, x[12], S24); /* 20 */
117 GG (a, b, c, d, x[ 1], S21); /* 21 */
118 GG (d, a, b, c, x[ 5], S22); /* 22 */
119 GG (c, d, a, b, x[ 9], S23); /* 23 */
120 GG (b, c, d, a, x[13], S24); /* 24 */
121 GG (a, b, c, d, x[ 2], S21); /* 25 */
122 GG (d, a, b, c, x[ 6], S22); /* 26 */
123 GG (c, d, a, b, x[10], S23); /* 27 */
124 GG (b, c, d, a, x[14], S24); /* 28 */
125 GG (a, b, c, d, x[ 3], S21); /* 29 */
126 GG (d, a, b, c, x[ 7], S22); /* 30 */
127 GG (c, d, a, b, x[11], S23); /* 31 */
128 GG (b, c, d, a, x[15], S24); /* 32 */
129
108 FF (d, a, b, c, x[13], S12); /* 14 */
109 FF (c, d, a, b, x[14], S13); /* 15 */
110 FF (b, c, d, a, x[15], S14); /* 16 */
111
112 /* Round 2 */
113 GG (a, b, c, d, x[ 0], S21); /* 17 */
114 GG (d, a, b, c, x[ 4], S22); /* 18 */
115 GG (c, d, a, b, x[ 8], S23); /* 19 */
116 GG (b, c, d, a, x[12], S24); /* 20 */
117 GG (a, b, c, d, x[ 1], S21); /* 21 */
118 GG (d, a, b, c, x[ 5], S22); /* 22 */
119 GG (c, d, a, b, x[ 9], S23); /* 23 */
120 GG (b, c, d, a, x[13], S24); /* 24 */
121 GG (a, b, c, d, x[ 2], S21); /* 25 */
122 GG (d, a, b, c, x[ 6], S22); /* 26 */
123 GG (c, d, a, b, x[10], S23); /* 27 */
124 GG (b, c, d, a, x[14], S24); /* 28 */
125 GG (a, b, c, d, x[ 3], S21); /* 29 */
126 GG (d, a, b, c, x[ 7], S22); /* 30 */
127 GG (c, d, a, b, x[11], S23); /* 31 */
128 GG (b, c, d, a, x[15], S24); /* 32 */
129
130130 /* Round 3 */
131 HH (a, b, c, d, x[ 0], S31); /* 33 */
132 HH (d, a, b, c, x[ 8], S32); /* 34 */
133 HH (c, d, a, b, x[ 4], S33); /* 35 */
134 HH (b, c, d, a, x[12], S34); /* 36 */
135 HH (a, b, c, d, x[ 2], S31); /* 37 */
136 HH (d, a, b, c, x[10], S32); /* 38 */
137 HH (c, d, a, b, x[ 6], S33); /* 39 */
138 HH (b, c, d, a, x[14], S34); /* 40 */
139 HH (a, b, c, d, x[ 1], S31); /* 41 */
140 HH (d, a, b, c, x[ 9], S32); /* 42 */
141 HH (c, d, a, b, x[ 5], S33); /* 43 */
142 HH (b, c, d, a, x[13], S34); /* 44 */
143 HH (a, b, c, d, x[ 3], S31); /* 45 */
144 HH (d, a, b, c, x[11], S32); /* 46 */
145 HH (c, d, a, b, x[ 7], S33); /* 47 */
146 HH (b, c, d, a, x[15], S34); /* 48 */
147
131 HH (a, b, c, d, x[ 0], S31); /* 33 */
132 HH (d, a, b, c, x[ 8], S32); /* 34 */
133 HH (c, d, a, b, x[ 4], S33); /* 35 */
134 HH (b, c, d, a, x[12], S34); /* 36 */
135 HH (a, b, c, d, x[ 2], S31); /* 37 */
136 HH (d, a, b, c, x[10], S32); /* 38 */
137 HH (c, d, a, b, x[ 6], S33); /* 39 */
138 HH (b, c, d, a, x[14], S34); /* 40 */
139 HH (a, b, c, d, x[ 1], S31); /* 41 */
140 HH (d, a, b, c, x[ 9], S32); /* 42 */
141 HH (c, d, a, b, x[ 5], S33); /* 43 */
142 HH (b, c, d, a, x[13], S34); /* 44 */
143 HH (a, b, c, d, x[ 3], S31); /* 45 */
144 HH (d, a, b, c, x[11], S32); /* 46 */
145 HH (c, d, a, b, x[ 7], S33); /* 47 */
146 HH (b, c, d, a, x[15], S34); /* 48 */
147
148148
149149 /* Update our state */
150150 md->md4.state[0] = md->md4.state[0] + a;
241241 }
242242 #ifdef LTC_CLEAN_STACK
243243 zeromem(md, sizeof(hash_state));
244 #endif
244 #endif
245245 return CRYPT_OK;
246246 }
247247
248248 /**
249249 Self-test the hash
250250 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
251 */
251 */
252252 int md4_test(void)
253253 {
254254 #ifndef LTC_TEST
255255 return CRYPT_NOP;
256 #else
256 #else
257257 static const struct md4_test_case {
258258 char *input;
259259 unsigned char digest[16];
260260 } cases[] = {
261 { "",
261 { "",
262262 {0x31, 0xd6, 0xcf, 0xe0, 0xd1, 0x6a, 0xe9, 0x31,
263263 0xb7, 0x3c, 0x59, 0xd7, 0xe0, 0xc0, 0x89, 0xc0} },
264264 { "a",
265265 {0xbd, 0xe5, 0x2c, 0xb3, 0x1d, 0xe3, 0x3e, 0x46,
266266 0x24, 0x5e, 0x05, 0xfb, 0xdb, 0xd6, 0xfb, 0x24} },
267267 { "abc",
268 {0xa4, 0x48, 0x01, 0x7a, 0xaf, 0x21, 0xd8, 0x52,
268 {0xa4, 0x48, 0x01, 0x7a, 0xaf, 0x21, 0xd8, 0x52,
269269 0x5f, 0xc1, 0x0a, 0xe8, 0x7a, 0xa6, 0x72, 0x9d} },
270 { "message digest",
271 {0xd9, 0x13, 0x0a, 0x81, 0x64, 0x54, 0x9f, 0xe8,
270 { "message digest",
271 {0xd9, 0x13, 0x0a, 0x81, 0x64, 0x54, 0x9f, 0xe8,
272272 0x18, 0x87, 0x48, 0x06, 0xe1, 0xc7, 0x01, 0x4b} },
273 { "abcdefghijklmnopqrstuvwxyz",
274 {0xd7, 0x9e, 0x1c, 0x30, 0x8a, 0xa5, 0xbb, 0xcd,
273 { "abcdefghijklmnopqrstuvwxyz",
274 {0xd7, 0x9e, 0x1c, 0x30, 0x8a, 0xa5, 0xbb, 0xcd,
275275 0xee, 0xa8, 0xed, 0x63, 0xdf, 0x41, 0x2d, 0xa9} },
276 { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
277 {0x04, 0x3f, 0x85, 0x82, 0xf2, 0x41, 0xdb, 0x35,
276 { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
277 {0x04, 0x3f, 0x85, 0x82, 0xf2, 0x41, 0xdb, 0x35,
278278 0x1c, 0xe6, 0x27, 0xe1, 0x53, 0xe7, 0xf0, 0xe4} },
279 { "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
280 {0xe3, 0x3b, 0x4d, 0xdc, 0x9c, 0x38, 0xf2, 0x19,
279 { "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
280 {0xe3, 0x3b, 0x4d, 0xdc, 0x9c, 0x38, 0xf2, 0x19,
281281 0x9c, 0x3e, 0x7b, 0x16, 0x4f, 0xcc, 0x05, 0x36} },
282282 };
283283 int i;
1212
1313 /**
1414 @file md5.c
15 LTC_MD5 hash function by Tom St Denis
15 LTC_MD5 hash function by Tom St Denis
1616 */
1717
1818 #ifdef LTC_MD5
9494 a = (a + I(b,c,d) + M + t); a = ROLc(a, s) + b;
9595
9696
97 #endif
97 #endif
9898
9999 #ifdef LTC_CLEAN_STACK
100100 static int _md5_compress(hash_state *md, unsigned char *buf)
111111 for (i = 0; i < 16; i++) {
112112 LOAD32L(W[i], buf + (4*i));
113113 }
114
114
115115 /* copy state */
116116 a = md->md5.state[0];
117117 b = md->md5.state[1];
308308 /**
309309 Self-test the hash
310310 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
311 */
311 */
312312 int md5_test(void)
313313 {
314314 #ifndef LTC_TEST
315315 return CRYPT_NOP;
316 #else
316 #else
317317 static const struct {
318318 char *msg;
319319 unsigned char hash[16];
320320 } tests[] = {
321321 { "",
322 { 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04,
322 { 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04,
323323 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e } },
324324 { "a",
325 {0x0c, 0xc1, 0x75, 0xb9, 0xc0, 0xf1, 0xb6, 0xa8,
325 {0x0c, 0xc1, 0x75, 0xb9, 0xc0, 0xf1, 0xb6, 0xa8,
326326 0x31, 0xc3, 0x99, 0xe2, 0x69, 0x77, 0x26, 0x61 } },
327327 { "abc",
328 { 0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0,
328 { 0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0,
329329 0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72 } },
330 { "message digest",
331 { 0xf9, 0x6b, 0x69, 0x7d, 0x7c, 0xb7, 0x93, 0x8d,
332 0x52, 0x5a, 0x2f, 0x31, 0xaa, 0xf1, 0x61, 0xd0 } },
330 { "message digest",
331 { 0xf9, 0x6b, 0x69, 0x7d, 0x7c, 0xb7, 0x93, 0x8d,
332 0x52, 0x5a, 0x2f, 0x31, 0xaa, 0xf1, 0x61, 0xd0 } },
333333 { "abcdefghijklmnopqrstuvwxyz",
334 { 0xc3, 0xfc, 0xd3, 0xd7, 0x61, 0x92, 0xe4, 0x00,
334 { 0xc3, 0xfc, 0xd3, 0xd7, 0x61, 0x92, 0xe4, 0x00,
335335 0x7d, 0xfb, 0x49, 0x6c, 0xca, 0x67, 0xe1, 0x3b } },
336336 { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
337 { 0xd1, 0x74, 0xab, 0x98, 0xd2, 0x77, 0xd9, 0xf5,
337 { 0xd1, 0x74, 0xab, 0x98, 0xd2, 0x77, 0xd9, 0xf5,
338338 0xa5, 0x61, 0x1c, 0x2c, 0x9f, 0x41, 0x9d, 0x9f } },
339339 { "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
340 { 0x57, 0xed, 0xf4, 0xa2, 0x2b, 0xe3, 0xc9, 0x55,
341 0xac, 0x49, 0xda, 0x2e, 0x21, 0x07, 0xb6, 0x7a } },
340 { 0x57, 0xed, 0xf4, 0xa2, 0x2b, 0xe3, 0xc9, 0x55,
341 0xac, 0x49, 0xda, 0x2e, 0x21, 0x07, 0xb6, 0x7a } },
342342 { NULL, { 0 } }
343343 };
344344
1212 /**
1313 @param rmd128.c
1414 RMD128 Hash function
15 */
15 */
1616
1717 /* Implementation of LTC_RIPEMD-128 based on the source by Antoon Bosselaers, ESAT-COSIC
1818 *
4141 };
4242
4343 /* the four basic functions F(), G() and H() */
44 #define F(x, y, z) ((x) ^ (y) ^ (z))
45 #define G(x, y, z) (((x) & (y)) | (~(x) & (z)))
44 #define F(x, y, z) ((x) ^ (y) ^ (z))
45 #define G(x, y, z) (((x) & (y)) | (~(x) & (z)))
4646 #define H(x, y, z) (((x) | ~(y)) ^ (z))
47 #define I(x, y, z) (((x) & (z)) | ((y) & ~(z)))
48
47 #define I(x, y, z) (((x) & (z)) | ((y) & ~(z)))
48
4949 /* the eight basic operations FF() through III() */
5050 #define FF(a, b, c, d, x, s) \
5151 (a) += F((b), (c), (d)) + (x);\
8787 {
8888 ulong32 aa,bb,cc,dd,aaa,bbb,ccc,ddd,X[16];
8989 int i;
90
90
9191 /* load words X */
9292 for (i = 0; i < 16; i++){
9393 LOAD32L(X[i], buf + (4 * i));
116116 FF(dd, aa, bb, cc, X[13], 7);
117117 FF(cc, dd, aa, bb, X[14], 9);
118118 FF(bb, cc, dd, aa, X[15], 8);
119
119
120120 /* round 2 */
121121 GG(aa, bb, cc, dd, X[ 7], 7);
122122 GG(dd, aa, bb, cc, X[ 4], 6);
172172 II(bb, cc, dd, aa, X[ 2], 12);
173173
174174 /* parallel round 1 */
175 III(aaa, bbb, ccc, ddd, X[ 5], 8);
175 III(aaa, bbb, ccc, ddd, X[ 5], 8);
176176 III(ddd, aaa, bbb, ccc, X[14], 9);
177177 III(ccc, ddd, aaa, bbb, X[ 7], 9);
178178 III(bbb, ccc, ddd, aaa, X[ 0], 11);
207207 HHH(ccc, ddd, aaa, bbb, X[ 1], 13);
208208 HHH(bbb, ccc, ddd, aaa, X[ 2], 11);
209209
210 /* parallel round 3 */
210 /* parallel round 3 */
211211 GGG(aaa, bbb, ccc, ddd, X[15], 9);
212212 GGG(ddd, aaa, bbb, ccc, X[ 5], 7);
213213 GGG(ccc, ddd, aaa, bbb, X[ 1], 15);
341341 #ifdef LTC_CLEAN_STACK
342342 zeromem(md, sizeof(hash_state));
343343 #endif
344 return CRYPT_OK;
344 return CRYPT_OK;
345345 }
346346
347347 /**
348348 Self-test the hash
349349 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
350 */
350 */
351351 int rmd128_test(void)
352352 {
353353 #ifndef LTC_TEST
1212 /**
1313 @file rmd160.c
1414 RMD160 hash function
15 */
15 */
1616
1717 /* Implementation of LTC_RIPEMD-160 based on the source by Antoon Bosselaers, ESAT-COSIC
1818 *
4141 };
4242
4343 /* the five basic functions F(), G() and H() */
44 #define F(x, y, z) ((x) ^ (y) ^ (z))
45 #define G(x, y, z) (((x) & (y)) | (~(x) & (z)))
44 #define F(x, y, z) ((x) ^ (y) ^ (z))
45 #define G(x, y, z) (((x) & (y)) | (~(x) & (z)))
4646 #define H(x, y, z) (((x) | ~(y)) ^ (z))
47 #define I(x, y, z) (((x) & (z)) | ((y) & ~(z)))
47 #define I(x, y, z) (((x) & (z)) | ((y) & ~(z)))
4848 #define J(x, y, z) ((x) ^ ((y) | ~(z)))
49
49
5050 /* the ten basic operations FF() through III() */
5151 #define FF(a, b, c, d, e, x, s) \
5252 (a) += F((b), (c), (d)) + (x);\
137137 FF(cc, dd, ee, aa, bb, X[13], 7);
138138 FF(bb, cc, dd, ee, aa, X[14], 9);
139139 FF(aa, bb, cc, dd, ee, X[15], 8);
140
140
141141 /* round 2 */
142142 GG(ee, aa, bb, cc, dd, X[ 7], 7);
143143 GG(dd, ee, aa, bb, cc, X[ 4], 6);
229229 JJJ(aaa, bbb, ccc, ddd, eee, X[12], 6);
230230
231231 /* parallel round 2 */
232 III(eee, aaa, bbb, ccc, ddd, X[ 6], 9);
232 III(eee, aaa, bbb, ccc, ddd, X[ 6], 9);
233233 III(ddd, eee, aaa, bbb, ccc, X[11], 13);
234234 III(ccc, ddd, eee, aaa, bbb, X[ 3], 15);
235235 III(bbb, ccc, ddd, eee, aaa, X[ 7], 7);
264264 HHH(eee, aaa, bbb, ccc, ddd, X[ 4], 7);
265265 HHH(ddd, eee, aaa, bbb, ccc, X[13], 5);
266266
267 /* parallel round 4 */
267 /* parallel round 4 */
268268 GGG(ccc, ddd, eee, aaa, bbb, X[ 8], 15);
269269 GGG(bbb, ccc, ddd, eee, aaa, X[ 6], 5);
270270 GGG(aaa, bbb, ccc, ddd, eee, X[ 4], 8);
406406 /**
407407 Self-test the hash
408408 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
409 */
409 */
410410 int rmd160_test(void)
411411 {
412412 #ifndef LTC_TEST
1111
1212 /**
1313 @file sha1.c
14 LTC_SHA1 code by Tom St Denis
14 LTC_SHA1 code by Tom St Denis
1515 */
1616
1717
6565
6666 /* expand it */
6767 for (i = 16; i < 80; i++) {
68 W[i] = ROL(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1);
68 W[i] = ROL(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1);
6969 }
7070
7171 /* compress */
7474 #define FF1(a,b,c,d,e,i) e = (ROLc(a, 5) + F1(b,c,d) + e + W[i] + 0x6ed9eba1UL); b = ROLc(b, 30);
7575 #define FF2(a,b,c,d,e,i) e = (ROLc(a, 5) + F2(b,c,d) + e + W[i] + 0x8f1bbcdcUL); b = ROLc(b, 30);
7676 #define FF3(a,b,c,d,e,i) e = (ROLc(a, 5) + F3(b,c,d) + e + W[i] + 0xca62c1d6UL); b = ROLc(b, 30);
77
77
7878 #ifdef LTC_SMALL_CODE
79
79
8080 for (i = 0; i < 20; ) {
8181 FF0(a,b,c,d,e,i++); t = e; e = d; d = c; c = b; b = a; a = t;
8282 }
104104 }
105105
106106 /* round two */
107 for (; i < 40; ) {
107 for (; i < 40; ) {
108108 FF1(a,b,c,d,e,i++);
109109 FF1(e,a,b,c,d,i++);
110110 FF1(d,e,a,b,c,i++);
113113 }
114114
115115 /* round three */
116 for (; i < 60; ) {
116 for (; i < 60; ) {
117117 FF2(a,b,c,d,e,i++);
118118 FF2(e,a,b,c,d,i++);
119119 FF2(d,e,a,b,c,i++);
122122 }
123123
124124 /* round four */
125 for (; i < 80; ) {
125 for (; i < 80; ) {
126126 FF3(a,b,c,d,e,i++);
127127 FF3(e,a,b,c,d,i++);
128128 FF3(d,e,a,b,c,i++);
240240 /**
241241 Self-test the hash
242242 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
243 */
243 */
244244 int sha1_test(void)
245245 {
246246 #ifndef LTC_TEST
247247 return CRYPT_NOP;
248 #else
248 #else
249249 static const struct {
250250 char *msg;
251251 unsigned char hash[20];
1111
1212 /**
1313 @file sha256.c
14 LTC_SHA256 by Tom St Denis
15 */
16
17 #ifdef LTC_SHA256
14 LTC_SHA256 by Tom St Denis
15 */
16
17 #ifdef LTC_SHA256
1818
1919 const struct ltc_hash_descriptor sha256_desc =
2020 {
2626 /* OID */
2727 { 2, 16, 840, 1, 101, 3, 4, 2, 1, },
2828 9,
29
29
3030 &sha256_init,
3131 &sha256_process,
3232 &sha256_done,
5555
5656 /* Various logical functions */
5757 #define Ch(x,y,z) (z ^ (x & (y ^ z)))
58 #define Maj(x,y,z) (((x | y) & z) | (x & y))
58 #define Maj(x,y,z) (((x | y) & z) | (x & y))
5959 #define S(x, n) RORc((x),(n))
6060 #define R(x, n) (((x)&0xFFFFFFFFUL)>>(n))
6161 #define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
8989 /* fill W[16..63] */
9090 for (i = 16; i < 64; i++) {
9191 W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
92 }
92 }
9393
9494 /* Compress */
95 #ifdef LTC_SMALL_CODE
95 #ifdef LTC_SMALL_CODE
9696 #define RND(a,b,c,d,e,f,g,h,i) \
9797 t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; \
9898 t1 = Sigma0(a) + Maj(a, b, c); \
101101
102102 for (i = 0; i < 64; ++i) {
103103 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],i);
104 t = S[7]; S[7] = S[6]; S[6] = S[5]; S[5] = S[4];
104 t = S[7]; S[7] = S[6]; S[6] = S[5]; S[5] = S[4];
105105 S[4] = S[3]; S[3] = S[2]; S[2] = S[1]; S[1] = S[0]; S[0] = t;
106 }
107 #else
106 }
107 #else
108108 #define RND(a,b,c,d,e,f,g,h,i,ki) \
109109 t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \
110110 t1 = Sigma0(a) + Maj(a, b, c); \
176176 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7);
177177 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2);
178178
179 #undef RND
180
181 #endif
179 #undef RND
180
181 #endif
182182
183183 /* feedback */
184184 for (i = 0; i < 8; i++) {
286286 /**
287287 Self-test the hash
288288 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
289 */
289 */
290290 int sha256_test(void)
291291 {
292292 #ifndef LTC_TEST
293293 return CRYPT_NOP;
294 #else
294 #else
295295 static const struct {
296296 char *msg;
297297 unsigned char hash[32];
303303 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad }
304304 },
305305 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
306 { 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8,
306 { 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8,
307307 0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39,
308 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
308 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
309309 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1 }
310310 },
311311 };
1111
1212 /**
1313 @param sha512.c
14 LTC_SHA512 by Tom St Denis
14 LTC_SHA512 by Tom St Denis
1515 */
1616
1717 #ifdef LTC_SHA512
3636
3737 /* the K array */
3838 static const ulong64 K[80] = {
39 CONST64(0x428a2f98d728ae22), CONST64(0x7137449123ef65cd),
39 CONST64(0x428a2f98d728ae22), CONST64(0x7137449123ef65cd),
4040 CONST64(0xb5c0fbcfec4d3b2f), CONST64(0xe9b5dba58189dbbc),
41 CONST64(0x3956c25bf348b538), CONST64(0x59f111f1b605d019),
41 CONST64(0x3956c25bf348b538), CONST64(0x59f111f1b605d019),
4242 CONST64(0x923f82a4af194f9b), CONST64(0xab1c5ed5da6d8118),
43 CONST64(0xd807aa98a3030242), CONST64(0x12835b0145706fbe),
43 CONST64(0xd807aa98a3030242), CONST64(0x12835b0145706fbe),
4444 CONST64(0x243185be4ee4b28c), CONST64(0x550c7dc3d5ffb4e2),
45 CONST64(0x72be5d74f27b896f), CONST64(0x80deb1fe3b1696b1),
45 CONST64(0x72be5d74f27b896f), CONST64(0x80deb1fe3b1696b1),
4646 CONST64(0x9bdc06a725c71235), CONST64(0xc19bf174cf692694),
47 CONST64(0xe49b69c19ef14ad2), CONST64(0xefbe4786384f25e3),
47 CONST64(0xe49b69c19ef14ad2), CONST64(0xefbe4786384f25e3),
4848 CONST64(0x0fc19dc68b8cd5b5), CONST64(0x240ca1cc77ac9c65),
49 CONST64(0x2de92c6f592b0275), CONST64(0x4a7484aa6ea6e483),
49 CONST64(0x2de92c6f592b0275), CONST64(0x4a7484aa6ea6e483),
5050 CONST64(0x5cb0a9dcbd41fbd4), CONST64(0x76f988da831153b5),
51 CONST64(0x983e5152ee66dfab), CONST64(0xa831c66d2db43210),
51 CONST64(0x983e5152ee66dfab), CONST64(0xa831c66d2db43210),
5252 CONST64(0xb00327c898fb213f), CONST64(0xbf597fc7beef0ee4),
53 CONST64(0xc6e00bf33da88fc2), CONST64(0xd5a79147930aa725),
53 CONST64(0xc6e00bf33da88fc2), CONST64(0xd5a79147930aa725),
5454 CONST64(0x06ca6351e003826f), CONST64(0x142929670a0e6e70),
55 CONST64(0x27b70a8546d22ffc), CONST64(0x2e1b21385c26c926),
55 CONST64(0x27b70a8546d22ffc), CONST64(0x2e1b21385c26c926),
5656 CONST64(0x4d2c6dfc5ac42aed), CONST64(0x53380d139d95b3df),
57 CONST64(0x650a73548baf63de), CONST64(0x766a0abb3c77b2a8),
57 CONST64(0x650a73548baf63de), CONST64(0x766a0abb3c77b2a8),
5858 CONST64(0x81c2c92e47edaee6), CONST64(0x92722c851482353b),
5959 CONST64(0xa2bfe8a14cf10364), CONST64(0xa81a664bbc423001),
6060 CONST64(0xc24b8b70d0f89791), CONST64(0xc76c51a30654be30),
61 CONST64(0xd192e819d6ef5218), CONST64(0xd69906245565a910),
61 CONST64(0xd192e819d6ef5218), CONST64(0xd69906245565a910),
6262 CONST64(0xf40e35855771202a), CONST64(0x106aa07032bbd1b8),
63 CONST64(0x19a4c116b8d2d0c8), CONST64(0x1e376c085141ab53),
63 CONST64(0x19a4c116b8d2d0c8), CONST64(0x1e376c085141ab53),
6464 CONST64(0x2748774cdf8eeb99), CONST64(0x34b0bcb5e19b48a8),
65 CONST64(0x391c0cb3c5c95a63), CONST64(0x4ed8aa4ae3418acb),
65 CONST64(0x391c0cb3c5c95a63), CONST64(0x4ed8aa4ae3418acb),
6666 CONST64(0x5b9cca4f7763e373), CONST64(0x682e6ff3d6b2b8a3),
67 CONST64(0x748f82ee5defb2fc), CONST64(0x78a5636f43172f60),
67 CONST64(0x748f82ee5defb2fc), CONST64(0x78a5636f43172f60),
6868 CONST64(0x84c87814a1f0ab72), CONST64(0x8cc702081a6439ec),
69 CONST64(0x90befffa23631e28), CONST64(0xa4506cebde82bde9),
69 CONST64(0x90befffa23631e28), CONST64(0xa4506cebde82bde9),
7070 CONST64(0xbef9a3f7b2c67915), CONST64(0xc67178f2e372532b),
71 CONST64(0xca273eceea26619c), CONST64(0xd186b8c721c0c207),
71 CONST64(0xca273eceea26619c), CONST64(0xd186b8c721c0c207),
7272 CONST64(0xeada7dd6cde0eb1e), CONST64(0xf57d4f7fee6ed178),
73 CONST64(0x06f067aa72176fba), CONST64(0x0a637dc5a2c898a6),
73 CONST64(0x06f067aa72176fba), CONST64(0x0a637dc5a2c898a6),
7474 CONST64(0x113f9804bef90dae), CONST64(0x1b710b35131c471b),
75 CONST64(0x28db77f523047d84), CONST64(0x32caab7b40c72493),
75 CONST64(0x28db77f523047d84), CONST64(0x32caab7b40c72493),
7676 CONST64(0x3c9ebe0a15c9bebc), CONST64(0x431d67c49c100d4c),
77 CONST64(0x4cc5d4becb3e42b6), CONST64(0x597f299cfc657e2a),
77 CONST64(0x4cc5d4becb3e42b6), CONST64(0x597f299cfc657e2a),
7878 CONST64(0x5fcb6fab3ad6faec), CONST64(0x6c44198c4a475817)
7979 };
8080
8181 /* Various logical functions */
8282 #define Ch(x,y,z) (z ^ (x & (y ^ z)))
83 #define Maj(x,y,z) (((x | y) & z) | (x & y))
83 #define Maj(x,y,z) (((x | y) & z) | (x & y))
8484 #define S(x, n) ROR64c(x, n)
8585 #define R(x, n) (((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)n))
8686 #define Sigma0(x) (S(x, 28) ^ S(x, 34) ^ S(x, 39))
111111 /* fill W[16..79] */
112112 for (i = 16; i < 80; i++) {
113113 W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
114 }
114 }
115115
116116 /* Compress */
117117 #ifdef LTC_SMALL_CODE
134134 d += t0; \
135135 h = t0 + t1;
136136
137 for (i = 0; i < 80; i += 8) {
138 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],i+0);
139 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],i+1);
140 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],i+2);
141 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],i+3);
142 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],i+4);
143 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],i+5);
144 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],i+6);
145 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],i+7);
146 }
147 #endif
137 for (i = 0; i < 80; i += 8) {
138 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],i+0);
139 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],i+1);
140 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],i+2);
141 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],i+3);
142 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],i+4);
143 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],i+5);
144 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],i+6);
145 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],i+7);
146 }
147 #endif
148148
149149
150150 /* feedback */
231231 md->sha512.curlen = 0;
232232 }
233233
234 /* pad upto 120 bytes of zeroes
234 /* pad upto 120 bytes of zeroes
235235 * note: that from 112 to 120 is the 64 MSB of the length. We assume that you won't hash
236236 * > 2^64 bits of data... :-)
237237 */
256256 /**
257257 Self-test the hash
258258 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
259 */
259 */
260260 int sha512_test(void)
261261 {
262262 #ifndef LTC_TEST
263263 return CRYPT_NOP;
264 #else
264 #else
265265 static const struct {
266266 char *msg;
267267 unsigned char hash[64];
557557 #ifdef _MSC_VER
558558 #define INLINE __inline
559559 #else
560 #define INLINE
561 #endif
560 #define INLINE
561 #endif
562562
563563 /* one round of the hash function */
564564 INLINE static void tiger_round(ulong64 *a, ulong64 *b, ulong64 *c, ulong64 x, int mul)
565565 {
566566 ulong64 tmp;
567 tmp = (*c ^= x);
568 *a -= t1[byte(tmp, 0)] ^ t2[byte(tmp, 2)] ^ t3[byte(tmp, 4)] ^ t4[byte(tmp, 6)];
569 tmp = (*b += t4[byte(tmp, 1)] ^ t3[byte(tmp, 3)] ^ t2[byte(tmp,5)] ^ t1[byte(tmp,7)]);
567 tmp = (*c ^= x);
568 *a -= t1[byte(tmp, 0)] ^ t2[byte(tmp, 2)] ^ t3[byte(tmp, 4)] ^ t4[byte(tmp, 6)];
569 tmp = (*b += t4[byte(tmp, 1)] ^ t3[byte(tmp, 3)] ^ t2[byte(tmp,5)] ^ t1[byte(tmp,7)]);
570570 switch (mul) {
571571 case 5: *b = (tmp << 2) + tmp; break;
572572 case 7: *b = (tmp << 3) - tmp; break;
577577 /* one complete pass */
578578 static void pass(ulong64 *a, ulong64 *b, ulong64 *c, ulong64 *x, int mul)
579579 {
580 tiger_round(a,b,c,x[0],mul);
581 tiger_round(b,c,a,x[1],mul);
582 tiger_round(c,a,b,x[2],mul);
583 tiger_round(a,b,c,x[3],mul);
584 tiger_round(b,c,a,x[4],mul);
585 tiger_round(c,a,b,x[5],mul);
586 tiger_round(a,b,c,x[6],mul);
587 tiger_round(b,c,a,x[7],mul);
588 }
580 tiger_round(a,b,c,x[0],mul);
581 tiger_round(b,c,a,x[1],mul);
582 tiger_round(c,a,b,x[2],mul);
583 tiger_round(a,b,c,x[3],mul);
584 tiger_round(b,c,a,x[4],mul);
585 tiger_round(c,a,b,x[5],mul);
586 tiger_round(a,b,c,x[6],mul);
587 tiger_round(b,c,a,x[7],mul);
588 }
589589
590590 /* The key mixing schedule */
591 static void key_schedule(ulong64 *x)
591 static void key_schedule(ulong64 *x)
592592 {
593 x[0] -= x[7] ^ CONST64(0xA5A5A5A5A5A5A5A5);
594 x[1] ^= x[0];
595 x[2] += x[1];
596 x[3] -= x[2] ^ ((~x[1])<<19);
597 x[4] ^= x[3];
598 x[5] += x[4];
599 x[6] -= x[5] ^ ((~x[4])>>23);
600 x[7] ^= x[6];
601 x[0] += x[7];
602 x[1] -= x[0] ^ ((~x[7])<<19);
603 x[2] ^= x[1];
604 x[3] += x[2];
605 x[4] -= x[3] ^ ((~x[2])>>23);
606 x[5] ^= x[4];
607 x[6] += x[5];
593 x[0] -= x[7] ^ CONST64(0xA5A5A5A5A5A5A5A5);
594 x[1] ^= x[0];
595 x[2] += x[1];
596 x[3] -= x[2] ^ ((~x[1])<<19);
597 x[4] ^= x[3];
598 x[5] += x[4];
599 x[6] -= x[5] ^ ((~x[4])>>23);
600 x[7] ^= x[6];
601 x[0] += x[7];
602 x[1] -= x[0] ^ ((~x[7])<<19);
603 x[2] ^= x[1];
604 x[3] += x[2];
605 x[4] -= x[3] ^ ((~x[2])>>23);
606 x[5] ^= x[4];
607 x[6] += x[5];
608608 x[7] -= x[6] ^ CONST64(0x0123456789ABCDEF);
609 }
609 }
610610
611611 #ifdef LTC_CLEAN_STACK
612612 static int _tiger_compress(hash_state *md, unsigned char *buf)
708708
709709 /* pad upto 56 bytes of zeroes */
710710 while (md->tiger.curlen < 56) {
711 md->tiger.buf[md->tiger.curlen++] = (unsigned char)0;
711 md->tiger.buf[md->tiger.curlen++] = (unsigned char)0;
712712 }
713713
714714 /* store length */
729729 /**
730730 Self-test the hash
731731 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
732 */
732 */
733733 int tiger_test(void)
734734 {
735735 #ifndef LTC_TEST
736736 return CRYPT_NOP;
737 #else
737 #else
738738 static const struct {
739739 char *msg;
740740 unsigned char hash[24];
6161 out[x] = f9->ACC[x];
6262 }
6363 *outlen = x;
64
64
6565 #ifdef LTC_CLEAN_STACK
6666 zeromem(f9, sizeof(*f9));
6767 #endif
4444 if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, &f9->key)) != CRYPT_OK) {
4545 goto done;
4646 }
47
47
4848 /* make the second key */
4949 for (x = 0; (unsigned)x < keylen; x++) {
5050 f9->akey[x] = key[x] ^ 0xAA;
5151 }
52
52
5353 /* setup struct */
5454 zeromem(f9->IV, cipher_descriptor[cipher].block_length);
5555 zeromem(f9->ACC, cipher_descriptor[cipher].block_length);
1616
1717 #ifdef LTC_F9_MODE
1818
19 /** f9-MAC a block of memory
19 /** f9-MAC a block of memory
2020 @param cipher Index of cipher to use
2121 @param key [in] Secret key
2222 @param keylen Length of key in octets
2626 @param outlen [in/out] Output size and final tag size
2727 Return CRYPT_OK on success.
2828 */
29 int f9_memory(int cipher,
29 int f9_memory(int cipher,
3030 const unsigned char *key, unsigned long keylen,
3131 const unsigned char *in, unsigned long inlen,
3232 unsigned char *out, unsigned long *outlen)
1010 #include "tomcrypt.h"
1111 #include <stdarg.h>
1212
13 /**
13 /**
1414 @file f9_memory_multi.c
1515 f9 support, process multiple blocks of memory, Tom St Denis
1616 */
1818 #ifdef LTC_F9_MODE
1919
2020 /**
21 f9 multiple blocks of memory
21 f9 multiple blocks of memory
2222 @param cipher The index of the desired cipher
2323 @param key The secret key
2424 @param keylen The length of the secret key (octets)
2929 @param ... tuples of (data,len) pairs to f9, terminated with a (NULL,x) (x=don't care)
3030 @return CRYPT_OK if successful
3131 */
32 int f9_memory_multi(int cipher,
32 int f9_memory_multi(int cipher,
3333 const unsigned char *key, unsigned long keylen,
3434 unsigned char *out, unsigned long *outlen,
3535 const unsigned char *in, unsigned long inlen, ...)
5656 goto LBL_ERR;
5757 }
5858 va_start(args, inlen);
59 curptr = in;
59 curptr = in;
6060 curlen = inlen;
6161 for (;;) {
6262 /* process buf */
7979 #endif
8080 XFREE(f9);
8181 va_end(args);
82 return err;
82 return err;
8383 }
8484
8585 #endif
99 */
1010 #include "tomcrypt.h"
1111
12 /**
12 /**
1313 @file pelican_memory.c
14 Pelican MAC, MAC a block of memory, by Tom St Denis
14 Pelican MAC, MAC a block of memory, by Tom St Denis
1515 */
1616
1717 #ifdef LTC_PELICAN
2222 @param keylen The length of the key (octets)
2323 @param in The input to MAC
2424 @param inlen The length of the input (octets)
25 @param out [out] The output TAG
25 @param out [out] The output TAG
2626 @return CRYPT_OK on success
2727 */
2828 int pelican_memory(const unsigned char *key, unsigned long keylen,
3333 int err;
3434
3535 pel = XMALLOC(sizeof(*pel));
36 if (pel == NULL) {
36 if (pel == NULL) {
3737 return CRYPT_MEM;
3838 }
3939
4646 return err;
4747 }
4848 err = pelican_done(pel, out);
49 XFREE(pel);
49 XFREE(pel);
5050 return err;
5151 }
5252
99 */
1010 #include "tomcrypt.h"
1111
12 /**
12 /**
1313 @file pmac_done.c
14 PMAC implementation, terminate a session, by Tom St Denis
14 PMAC implementation, terminate a session, by Tom St Denis
1515 */
1616
1717 #ifdef LTC_PMAC
109109 }
110110 }
111111
112 /* find Lr = L / x */
113 m = L[pmac->block_len-1] & 1;
112 /* find Lr = L / x */
113 m = L[pmac->block_len-1] & 1;
114114
115 /* shift right */
116 for (x = pmac->block_len - 1; x > 0; x--) {
117 pmac->Lr[x] = ((L[x] >> 1) | (L[x-1] << 7)) & 255;
118 }
119 pmac->Lr[0] = L[0] >> 1;
115 /* shift right */
116 for (x = pmac->block_len - 1; x > 0; x--) {
117 pmac->Lr[x] = ((L[x] >> 1) | (L[x-1] << 7)) & 255;
118 }
119 pmac->Lr[0] = L[0] >> 1;
120120
121 if (m == 1) {
122 for (x = 0; x < pmac->block_len; x++) {
123 pmac->Lr[x] ^= polys[poly].poly_div[x];
124 }
125 }
121 if (m == 1) {
122 for (x = 0; x < pmac->block_len; x++) {
123 pmac->Lr[x] ^= polys[poly].poly_div[x];
124 }
125 }
126126
127 /* zero buffer, counters, etc... */
128 pmac->block_index = 1;
129 pmac->cipher_idx = cipher;
130 pmac->buflen = 0;
131 zeromem(pmac->block, sizeof(pmac->block));
132 zeromem(pmac->Li, sizeof(pmac->Li));
133 zeromem(pmac->checksum, sizeof(pmac->checksum));
134 err = CRYPT_OK;
127 /* zero buffer, counters, etc... */
128 pmac->block_index = 1;
129 pmac->cipher_idx = cipher;
130 pmac->buflen = 0;
131 zeromem(pmac->block, sizeof(pmac->block));
132 zeromem(pmac->Li, sizeof(pmac->Li));
133 zeromem(pmac->checksum, sizeof(pmac->checksum));
134 err = CRYPT_OK;
135135 error:
136136 #ifdef LTC_CLEAN_STACK
137 zeromem(L, pmac->block_len);
137 zeromem(L, pmac->block_len);
138138 #endif
139139
140 XFREE(L);
140 XFREE(L);
141141
142 return err;
142 return err;
143143 }
144144
145145 #endif
99 */
1010 #include "tomcrypt.h"
1111
12 /**
12 /**
1313 @file pmac_memory.c
14 PMAC implementation, process a block of memory, by Tom St Denis
14 PMAC implementation, process a block of memory, by Tom St Denis
1515 */
1616
1717 #ifdef LTC_PMAC
2727 @param outlen [in/out] The max size and resulting size of the authentication tag
2828 @return CRYPT_OK if successful
2929 */
30 int pmac_memory(int cipher,
30 int pmac_memory(int cipher,
3131 const unsigned char *key, unsigned long keylen,
3232 const unsigned char *in, unsigned long inlen,
3333 unsigned char *out, unsigned long *outlen)
4545 if (pmac == NULL) {
4646 return CRYPT_MEM;
4747 }
48
48
4949 if ((err = pmac_init(pmac, cipher, key, keylen)) != CRYPT_OK) {
5050 goto LBL_ERR;
5151 }
6363 #endif
6464
6565 XFREE(pmac);
66 return err;
66 return err;
6767 }
6868
6969 #endif
1010 #include "tomcrypt.h"
1111 #include <stdarg.h>
1212
13 /**
13 /**
1414 @file pmac_memory_multi.c
15 PMAC implementation, process multiple blocks of memory, by Tom St Denis
15 PMAC implementation, process multiple blocks of memory, by Tom St Denis
1616 */
1717
1818 #ifdef LTC_PMAC
2929 @param ... tuples of (data,len) pairs to PMAC, terminated with a (NULL,x) (x=don't care)
3030 @return CRYPT_OK if successful
3131 */
32 int pmac_memory_multi(int cipher,
32 int pmac_memory_multi(int cipher,
3333 const unsigned char *key, unsigned long keylen,
3434 unsigned char *out, unsigned long *outlen,
3535 const unsigned char *in, unsigned long inlen, ...)
5050 if (pmac == NULL) {
5151 return CRYPT_MEM;
5252 }
53
53
5454 if ((err = pmac_init(pmac, cipher, key, keylen)) != CRYPT_OK) {
5555 goto LBL_ERR;
5656 }
5757 va_start(args, inlen);
58 curptr = in;
58 curptr = in;
5959 curlen = inlen;
6060 for (;;) {
6161 /* process buf */
7878 #endif
7979 XFREE(pmac);
8080 va_end(args);
81 return err;
81 return err;
8282 }
8383
8484 #endif
99 */
1010 #include "tomcrypt.h"
1111
12 /**
12 /**
1313 @file pmac_ntz.c
14 PMAC implementation, internal function, by Tom St Denis
14 PMAC implementation, internal function, by Tom St Denis
1515 */
1616
1717 #ifdef LTC_PMAC
6161 out[x] = xcbc->IV[x];
6262 }
6363 *outlen = x;
64
64
6565 #ifdef LTC_CLEAN_STACK
6666 zeromem(xcbc, sizeof(*xcbc));
6767 #endif
7070 if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, skey)) != CRYPT_OK) {
7171 goto done;
7272 }
73
73
7474 /* make the three keys */
7575 for (y = 0; y < 3; y++) {
7676 for (x = 0; x < cipher_descriptor[cipher].block_length; x++) {
7979 cipher_descriptor[cipher].ecb_encrypt(xcbc->K[y], xcbc->K[y], skey);
8080 }
8181 }
82
82
8383 /* setup K1 */
8484 err = cipher_descriptor[cipher].setup(xcbc->K[0], k1, 0, &xcbc->key);
85
85
8686 /* setup struct */
8787 zeromem(xcbc->IV, cipher_descriptor[cipher].block_length);
8888 xcbc->blocksize = cipher_descriptor[cipher].block_length;
9090 xcbc->buflen = 0;
9191 done:
9292 cipher_descriptor[cipher].done(skey);
93 if (skey != NULL) {
93 if (skey != NULL) {
9494 #ifdef LTC_CLEAN_STACK
9595 zeromem(skey, sizeof(*skey));
9696 #endif
1616
1717 #ifdef LTC_XCBC
1818
19 /** XCBC-MAC a block of memory
19 /** XCBC-MAC a block of memory
2020 @param cipher Index of cipher to use
2121 @param key [in] Secret key
2222 @param keylen Length of key in octets
2626 @param outlen [in/out] Output size and final tag size
2727 Return CRYPT_OK on success.
2828 */
29 int xcbc_memory(int cipher,
29 int xcbc_memory(int cipher,
3030 const unsigned char *key, unsigned long keylen,
3131 const unsigned char *in, unsigned long inlen,
3232 unsigned char *out, unsigned long *outlen)
1010 #include "tomcrypt.h"
1111 #include <stdarg.h>
1212
13 /**
13 /**
1414 @file xcbc_memory_multi.c
1515 XCBC support, process multiple blocks of memory, Tom St Denis
1616 */
1818 #ifdef LTC_XCBC
1919
2020 /**
21 XCBC multiple blocks of memory
21 XCBC multiple blocks of memory
2222 @param cipher The index of the desired cipher
2323 @param key The secret key
2424 @param keylen The length of the secret key (octets)
2929 @param ... tuples of (data,len) pairs to XCBC, terminated with a (NULL,x) (x=don't care)
3030 @return CRYPT_OK if successful
3131 */
32 int xcbc_memory_multi(int cipher,
32 int xcbc_memory_multi(int cipher,
3333 const unsigned char *key, unsigned long keylen,
3434 unsigned char *out, unsigned long *outlen,
3535 const unsigned char *in, unsigned long inlen, ...)
5656 goto LBL_ERR;
5757 }
5858 va_start(args, inlen);
59 curptr = in;
59 curptr = in;
6060 curlen = inlen;
6161 for (;;) {
6262 /* process buf */
7979 #endif
8080 XFREE(xcbc);
8181 va_end(args);
82 return err;
82 return err;
8383 }
8484
8585 #endif
1414 /**
1515 @file rand_prime.c
1616 Generate a random prime, Tom St Denis
17 */
17 */
1818
1919 #define USE_BBS 1
2020
3434 }
3535
3636 /* allow sizes between 2 and 512 bytes for a prime size */
37 if (len < 2 || len > 512) {
37 if (len < 2 || len > 512) {
3838 return CRYPT_INVALID_PRIME_SIZE;
3939 }
40
40
4141 /* valid PRNG? Better be! */
4242 if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
43 return err;
43 return err;
4444 }
4545
4646 /* allocate buffer to work with */
5959 /* munge bits */
6060 buf[0] |= 0x80 | 0x40;
6161 buf[len-1] |= 0x01 | ((type & USE_BBS) ? 0x02 : 0x00);
62
62
6363 /* load value */
6464 if ((err = mp_read_unsigned_bin(N, buf, len)) != CRYPT_OK) {
6565 XFREE(buf);
8080 XFREE(buf);
8181 return CRYPT_OK;
8282 }
83
83
8484 #endif /* LTC_NO_MATH */
8585
8686
1515 */
1616
1717 /**
18 Find a cipher flexibly. First by name then if not present by block and key size
18 Find a cipher flexibly. First by name then if not present by block and key size
1919 @param name The name of the cipher desired
2020 @param blocklen The minimum length of the block cipher desired (octets)
2121 @param keylen The minimum length of the key size desired (octets)
1515 */
1616
1717 /**
18 Find a hash flexibly. First by name then if not present by digest size
18 Find a hash flexibly. First by name then if not present by digest size
1919 @param name The name of the hash desired
2020 @param digestlen The minimum length of the digest size (octets)
2121 @return >= 0 if found, -1 if not present
1313 /**
1414 @file crypt_fsa.c
1515 LibTomCrypt FULL SPEED AHEAD!, Tom St Denis
16 */
16 */
1717
1818 /* format is ltc_mp, cipher_desc, [cipher_desc], NULL, hash_desc, [hash_desc], NULL, prng_desc, [prng_desc], NULL */
1919 int crypt_fsa(void *mp, ...)
2525 if (mp != NULL) {
2626 XMEMCPY(&ltc_mp, mp, sizeof(ltc_mp));
2727 }
28
28
2929 while ((p = va_arg(args, void*)) != NULL) {
3030 if (register_cipher(p) == -1) {
3131 va_end(args);
4848 }
4949
5050 va_end(args);
51 return CRYPT_OK;
51 return CRYPT_OK;
5252 }
5353
5454
1111
1212 /**
1313 @file crypt_hash_descriptor.c
14 Stores the hash descriptor table, Tom St Denis
14 Stores the hash descriptor table, Tom St Denis
1515 */
1616
1717 struct ltc_hash_descriptor hash_descriptor[TAB_SIZE] = {
1212 /**
1313 @file crypt_hash_is_valid.c
1414 Determine if hash is valid, Tom St Denis
15 */
15 */
1616
1717 /*
1818 Test if a hash index is valid
1212 /**
1313 @file crypt_prng_descriptor.c
1414 Stores the PRNG descriptors, Tom St Denis
15 */
15 */
1616 struct ltc_prng_descriptor prng_descriptor[TAB_SIZE] = {
1717 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
1818 };
1313 @file crypt_register_prng.c
1414 Register a PRNG, Tom St Denis
1515 */
16
16
1717 /**
1818 Register a PRNG with the descriptor table
1919 @param prng The PRNG you wish to register
3535 return CRYPT_OK;
3636 }
3737
38 #endif
38 #endif
3939
4040
4141 /* $Source$ */
2020 Initialize a CBC context
2121 @param cipher The index of the cipher desired
2222 @param IV The initial vector
23 @param key The secret key
23 @param key The secret key
2424 @param keylen The length of the secret key (octets)
2525 @param num_rounds Number of rounds in the cipher desired (0 for default)
2626 @param cbc The CBC state to initialize
2727 @return CRYPT_OK if successful
2828 */
29 int cbc_start(int cipher, const unsigned char *IV, const unsigned char *key,
29 int cbc_start(int cipher, const unsigned char *IV, const unsigned char *key,
3030 int keylen, int num_rounds, symmetric_CBC *cbc)
3131 {
3232 int x, err;
33
33
3434 LTC_ARGCHK(IV != NULL);
3535 LTC_ARGCHK(key != NULL);
3636 LTC_ARGCHK(cbc != NULL);
5151 }
5252 cfb->pad[cfb->padlen] = *ct;
5353 *pt = *ct ^ cfb->IV[cfb->padlen];
54 ++pt;
54 ++pt;
5555 ++ct;
5656 ++(cfb->padlen);
5757 }
5050 cfb->padlen = 0;
5151 }
5252 cfb->pad[cfb->padlen] = (*ct = *pt ^ cfb->IV[cfb->padlen]);
53 ++pt;
53 ++pt;
5454 ++ct;
5555 ++(cfb->padlen);
5656 }
1212 /**
1313 @file cfb_setiv.c
1414 CFB implementation, set IV, Tom St Denis
15 */
15 */
1616
1717 #ifdef LTC_CFB_MODE
1818
2626 int cfb_setiv(const unsigned char *IV, unsigned long len, symmetric_CFB *cfb)
2727 {
2828 int err;
29
29
3030 LTC_ARGCHK(IV != NULL);
3131 LTC_ARGCHK(cfb != NULL);
3232
3333 if ((err = cipher_is_valid(cfb->cipher)) != CRYPT_OK) {
3434 return err;
3535 }
36
36
3737 if (len != (unsigned long)cfb->blocklen) {
3838 return CRYPT_INVALID_ARG;
3939 }
40
40
4141 /* force next block */
4242 cfb->padlen = 0;
4343 return cipher_descriptor[cfb->cipher].ecb_encrypt(IV, cfb->IV, &cfb->key);
4444 }
4545
46 #endif
46 #endif
4747
4848
4949 /* $Source$ */
2121 Initialize a CFB context
2222 @param cipher The index of the cipher desired
2323 @param IV The initial vector
24 @param key The secret key
24 @param key The secret key
2525 @param keylen The length of the secret key (octets)
2626 @param num_rounds Number of rounds in the cipher desired (0 for default)
2727 @param cfb The CFB state to initialize
2828 @return CRYPT_OK if successful
2929 */
30 int cfb_start(int cipher, const unsigned char *IV, const unsigned char *key,
30 int cfb_start(int cipher, const unsigned char *IV, const unsigned char *key,
3131 int keylen, int num_rounds, symmetric_CFB *cfb)
3232 {
3333 int x, err;
3939 if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
4040 return err;
4141 }
42
42
4343
4444 /* copy data */
4545 cfb->cipher = cipher;
1313 @file ctr_setiv.c
1414 CTR implementation, set IV, Tom St Denis
1515 */
16
16
1717 #ifdef LTC_CTR_MODE
1818
1919 /**
2626 int ctr_setiv(const unsigned char *IV, unsigned long len, symmetric_CTR *ctr)
2727 {
2828 int err;
29
29
3030 LTC_ARGCHK(IV != NULL);
3131 LTC_ARGCHK(ctr != NULL);
3232
3434 if ((err = cipher_is_valid(ctr->cipher)) != CRYPT_OK) {
3535 return err;
3636 }
37
37
3838 if (len != (unsigned long)ctr->blocklen) {
3939 return CRYPT_INVALID_ARG;
4040 }
4141
4242 /* set IV */
4343 XMEMCPY(ctr->ctr, IV, len);
44
44
4545 /* force next block */
4646 ctr->padlen = 0;
4747 return cipher_descriptor[ctr->cipher].ecb_encrypt(IV, ctr->pad, &ctr->key);
4848 }
4949
50 #endif
50 #endif
5151
5252
5353 /* $Source$ */
2121 Initialize a CTR context
2222 @param cipher The index of the cipher desired
2323 @param IV The initial vector
24 @param key The secret key
24 @param key The secret key
2525 @param keylen The length of the secret key (octets)
2626 @param num_rounds Number of rounds in the cipher desired (0 for default)
2727 @param ctr_mode The counter mode (CTR_COUNTER_LITTLE_ENDIAN or CTR_COUNTER_BIG_ENDIAN)
2828 @param ctr The CTR state to initialize
2929 @return CRYPT_OK if successful
3030 */
31 int ctr_start( int cipher,
32 const unsigned char *IV,
33 const unsigned char *key, int keylen,
31 int ctr_start( int cipher,
32 const unsigned char *IV,
33 const unsigned char *key, int keylen,
3434 int num_rounds, int ctr_mode,
3535 symmetric_CTR *ctr)
3636 {
9090 }
9191 }
9292
93 return cipher_descriptor[ctr->cipher].ecb_encrypt(ctr->ctr, ctr->pad, &ctr->key);
93 return cipher_descriptor[ctr->cipher].ecb_encrypt(ctr->ctr, ctr->pad, &ctr->key);
9494 }
9595
9696 #endif
2020 /**
2121 Initialize a ECB context
2222 @param cipher The index of the cipher desired
23 @param key The secret key
23 @param key The secret key
2424 @param keylen The length of the secret key (octets)
2525 @param num_rounds Number of rounds in the cipher desired (0 for default)
2626 @param ecb The ECB state to initialize
3535
3636 #endif
3737
38
38
3939
4040 /* $Source$ */
4141 /* $Revision$ */
3333 if ((err = cipher_is_valid(ofb->cipher)) != CRYPT_OK) {
3434 return err;
3535 }
36
36
3737 /* is blocklen/padlen valid? */
3838 if (ofb->blocklen < 0 || ofb->blocklen > (int)sizeof(ofb->IV) ||
3939 ofb->padlen < 0 || ofb->padlen > (int)sizeof(ofb->IV)) {
4040 return CRYPT_INVALID_ARG;
4141 }
42
42
4343 while (len-- > 0) {
4444 if (ofb->padlen == ofb->blocklen) {
4545 if ((err = cipher_descriptor[ofb->cipher].ecb_encrypt(ofb->IV, ofb->IV, &ofb->key)) != CRYPT_OK) {
4343 return cipher_descriptor[ofb->cipher].ecb_encrypt(IV, ofb->IV, &ofb->key);
4444 }
4545
46 #endif
46 #endif
4747
4848
4949 /* $Source$ */
2121 Initialize a OFB context
2222 @param cipher The index of the cipher desired
2323 @param IV The initial vector
24 @param key The secret key
24 @param key The secret key
2525 @param keylen The length of the secret key (octets)
2626 @param num_rounds Number of rounds in the cipher desired (0 for default)
2727 @param ofb The OFB state to initialize
2828 @return CRYPT_OK if successful
2929 */
30 int ofb_start(int cipher, const unsigned char *IV, const unsigned char *key,
30 int ofb_start(int cipher, const unsigned char *IV, const unsigned char *key,
3131 int keylen, int num_rounds, symmetric_OFB *ofb)
3232 {
3333 int x, err;
4444 return CRYPT_INVALID_PACKET;
4545 }
4646
47 /* offset in the data */
48 x = 1;
47 /* offset in the data */
48 x = 1;
4949
5050 /* get the length of the data */
5151 if (in[x] & 0x80) {
6666 /* short format */
6767 dlen = in[x++] & 0x7F;
6868 }
69
69
7070 /* is the data len too long or too short? */
7171 if ((dlen == 0) || (dlen + x > inlen)) {
7272 return CRYPT_INVALID_PACKET;
4646 return CRYPT_INVALID_PACKET;
4747 }
4848
49 /* offset in the data */
50 x = 1;
49 /* offset in the data */
50 x = 1;
5151
5252 /* get the length of the data */
5353 if (in[x] & 0x80) {
1616
1717 #ifdef LTC_DER
1818 /**
19 Gets length of DER encoding of BIT STRING
19 Gets length of DER encoding of BIT STRING
2020 @param nbits The number of bits in the string to encode
2121 @param outlen [out] The length of the DER encoding for the given string
2222 @return CRYPT_OK if successful
2828
2929 /* get the number of the bytes */
3030 nbytes = (nbits >> 3) + ((nbits & 7) ? 1 : 0) + 1;
31
31
3232 if (nbytes < 128) {
3333 /* 03 LL PP DD DD DD ... */
3434 *outlen = 2 + nbytes;
2929 {
3030 LTC_ARGCHK(in != NULL);
3131 LTC_ARGCHK(out != NULL);
32
32
3333 if (inlen < 3 || in[0] != 0x01 || in[1] != 0x01 || (in[2] != 0x00 && in[2] != 0xFF)) {
3434 return CRYPT_INVALID_ARG;
3535 }
36
36
3737 *out = (in[2]==0xFF) ? 1 : 0;
38
38
3939 return CRYPT_OK;
4040 }
4141
2424 @param outlen [in/out] The max size and resulting size of the DER BOOLEAN
2525 @return CRYPT_OK if successful
2626 */
27 int der_encode_boolean(int in,
27 int der_encode_boolean(int in,
2828 unsigned char *out, unsigned long *outlen)
2929 {
3030 LTC_ARGCHK(outlen != NULL);
3131 LTC_ARGCHK(out != NULL);
32
32
3333 if (*outlen < 3) {
3434 *outlen = 3;
3535 return CRYPT_BUFFER_OVERFLOW;
3636 }
37
37
3838 *outlen = 3;
3939 out[0] = 0x01;
4040 out[1] = 0x01;
4141 out[2] = in ? 0xFF : 0x00;
42
42
4343 return CRYPT_OK;
4444 }
4545
1616
1717 #ifdef LTC_DER
1818 /**
19 Gets length of DER encoding of a BOOLEAN
19 Gets length of DER encoding of a BOOLEAN
2020 @param outlen [out] The length of the DER encoding
2121 @return CRYPT_OK if successful
2222 */
8787
8888 return CRYPT_OK;
8989 }
90
90
9191 #endif
9292
9393 /* $Source$ */
3636
3737 /* get the size */
3838 if ((err = der_length_ia5_string(in, inlen, &len)) != CRYPT_OK) {
39 return err;
39 return err;
4040 }
4141
4242 /* too big? */
2020 int code, value;
2121 } ia5_table[] = {
2222 { '\0', 0 },
23 { '\a', 7 },
24 { '\b', 8 },
25 { '\t', 9 },
26 { '\n', 10 },
27 { '\f', 12 },
28 { '\r', 13 },
29 { ' ', 32 },
30 { '!', 33 },
31 { '"', 34 },
32 { '#', 35 },
33 { '$', 36 },
34 { '%', 37 },
35 { '&', 38 },
36 { '\'', 39 },
37 { '(', 40 },
38 { ')', 41 },
39 { '*', 42 },
40 { '+', 43 },
41 { ',', 44 },
42 { '-', 45 },
43 { '.', 46 },
44 { '/', 47 },
45 { '0', 48 },
46 { '1', 49 },
47 { '2', 50 },
48 { '3', 51 },
49 { '4', 52 },
50 { '5', 53 },
51 { '6', 54 },
52 { '7', 55 },
53 { '8', 56 },
54 { '9', 57 },
55 { ':', 58 },
56 { ';', 59 },
57 { '<', 60 },
58 { '=', 61 },
59 { '>', 62 },
60 { '?', 63 },
61 { '@', 64 },
62 { 'A', 65 },
63 { 'B', 66 },
64 { 'C', 67 },
65 { 'D', 68 },
66 { 'E', 69 },
67 { 'F', 70 },
68 { 'G', 71 },
69 { 'H', 72 },
70 { 'I', 73 },
71 { 'J', 74 },
72 { 'K', 75 },
73 { 'L', 76 },
74 { 'M', 77 },
75 { 'N', 78 },
76 { 'O', 79 },
77 { 'P', 80 },
78 { 'Q', 81 },
79 { 'R', 82 },
80 { 'S', 83 },
81 { 'T', 84 },
82 { 'U', 85 },
83 { 'V', 86 },
84 { 'W', 87 },
85 { 'X', 88 },
86 { 'Y', 89 },
87 { 'Z', 90 },
88 { '[', 91 },
89 { '\\', 92 },
90 { ']', 93 },
91 { '^', 94 },
92 { '_', 95 },
93 { '`', 96 },
94 { 'a', 97 },
95 { 'b', 98 },
96 { 'c', 99 },
97 { 'd', 100 },
98 { 'e', 101 },
99 { 'f', 102 },
100 { 'g', 103 },
101 { 'h', 104 },
102 { 'i', 105 },
103 { 'j', 106 },
104 { 'k', 107 },
105 { 'l', 108 },
106 { 'm', 109 },
107 { 'n', 110 },
108 { 'o', 111 },
109 { 'p', 112 },
110 { 'q', 113 },
111 { 'r', 114 },
112 { 's', 115 },
113 { 't', 116 },
114 { 'u', 117 },
115 { 'v', 118 },
116 { 'w', 119 },
117 { 'x', 120 },
118 { 'y', 121 },
119 { 'z', 122 },
120 { '{', 123 },
121 { '|', 124 },
122 { '}', 125 },
23 { '\a', 7 },
24 { '\b', 8 },
25 { '\t', 9 },
26 { '\n', 10 },
27 { '\f', 12 },
28 { '\r', 13 },
29 { ' ', 32 },
30 { '!', 33 },
31 { '"', 34 },
32 { '#', 35 },
33 { '$', 36 },
34 { '%', 37 },
35 { '&', 38 },
36 { '\'', 39 },
37 { '(', 40 },
38 { ')', 41 },
39 { '*', 42 },
40 { '+', 43 },
41 { ',', 44 },
42 { '-', 45 },
43 { '.', 46 },
44 { '/', 47 },
45 { '0', 48 },
46 { '1', 49 },
47 { '2', 50 },
48 { '3', 51 },
49 { '4', 52 },
50 { '5', 53 },
51 { '6', 54 },
52 { '7', 55 },
53 { '8', 56 },
54 { '9', 57 },
55 { ':', 58 },
56 { ';', 59 },
57 { '<', 60 },
58 { '=', 61 },
59 { '>', 62 },
60 { '?', 63 },
61 { '@', 64 },
62 { 'A', 65 },
63 { 'B', 66 },
64 { 'C', 67 },
65 { 'D', 68 },
66 { 'E', 69 },
67 { 'F', 70 },
68 { 'G', 71 },
69 { 'H', 72 },
70 { 'I', 73 },
71 { 'J', 74 },
72 { 'K', 75 },
73 { 'L', 76 },
74 { 'M', 77 },
75 { 'N', 78 },
76 { 'O', 79 },
77 { 'P', 80 },
78 { 'Q', 81 },
79 { 'R', 82 },
80 { 'S', 83 },
81 { 'T', 84 },
82 { 'U', 85 },
83 { 'V', 86 },
84 { 'W', 87 },
85 { 'X', 88 },
86 { 'Y', 89 },
87 { 'Z', 90 },
88 { '[', 91 },
89 { '\\', 92 },
90 { ']', 93 },
91 { '^', 94 },
92 { '_', 95 },
93 { '`', 96 },
94 { 'a', 97 },
95 { 'b', 98 },
96 { 'c', 99 },
97 { 'd', 100 },
98 { 'e', 101 },
99 { 'f', 102 },
100 { 'g', 103 },
101 { 'h', 104 },
102 { 'i', 105 },
103 { 'j', 106 },
104 { 'k', 107 },
105 { 'l', 108 },
106 { 'm', 109 },
107 { 'n', 110 },
108 { 'o', 111 },
109 { 'p', 112 },
110 { 'q', 113 },
111 { 'r', 114 },
112 { 's', 115 },
113 { 't', 116 },
114 { 'u', 117 },
115 { 'v', 118 },
116 { 'w', 119 },
117 { 'x', 120 },
118 { 'y', 121 },
119 { 'z', 122 },
120 { '{', 123 },
121 { '|', 124 },
122 { '}', 125 },
123123 { '~', 126 }
124124 };
125125
144144 }
145145 return -1;
146146 }
147
147
148148 /**
149 Gets length of DER encoding of IA5 STRING
150 @param octets The values you want to encode
149 Gets length of DER encoding of IA5 STRING
150 @param octets The values you want to encode
151151 @param noctets The number of octets in the string to encode
152152 @param outlen [out] The length of the DER encoding for the given string
153153 @return CRYPT_OK if successful
5353 if (x + z > inlen) {
5454 return CRYPT_INVALID_PACKET;
5555 }
56
56
5757 /* no so read it */
5858 if ((err = mp_read_unsigned_bin(num, (unsigned char *)in + x, z)) != CRYPT_OK) {
5959 return err;
6161 } else {
6262 /* long form */
6363 z &= 0x7F;
64
64
6565 /* will number of length bytes overflow? (or > 4) */
6666 if (((x + z) > inlen) || (z > 4) || (z == 0)) {
6767 return CRYPT_INVALID_PACKET;
9696 return CRYPT_MEM;
9797 }
9898 mp_clear(tmp);
99 }
99 }
100100
101101 return CRYPT_OK;
102102
2626 @return CRYPT_OK if successful
2727 */
2828 int der_encode_integer(void *num, unsigned char *out, unsigned long *outlen)
29 {
29 {
3030 unsigned long tmplen, y;
3131 int err, leading_zero;
3232
9696 }
9797 } else if (mp_iszero(num) != LTC_MP_YES) {
9898 void *tmp;
99
99
100100 /* negative */
101101 if (mp_init(&tmp) != CRYPT_OK) {
102102 return CRYPT_MEM;
118118 }
119119
120120 /* we good */
121 *outlen = tmplen;
121 *outlen = tmplen;
122122 return CRYPT_OK;
123123 }
124124
4747 if ((in[x++] & 0x1F) != 0x06) {
4848 return CRYPT_INVALID_PACKET;
4949 }
50
50
5151 /* get the length */
5252 if (in[x] < 128) {
53 len = in[x++];
53 len = in[x++];
5454 } else {
55 if (in[x] < 0x81 || in[x] > 0x82) {
56 return CRYPT_INVALID_PACKET;
57 }
58 y = in[x++] & 0x7F;
59 len = 0;
60 while (y--) {
61 len = (len << 8) | (unsigned long)in[x++];
62 }
55 if (in[x] < 0x81 || in[x] > 0x82) {
56 return CRYPT_INVALID_PACKET;
57 }
58 y = in[x++] & 0x7F;
59 len = 0;
60 while (y--) {
61 len = (len << 8) | (unsigned long)in[x++];
62 }
6363 }
6464
6565 if (len < 1 || (len + x) > inlen) {
7070 y = 0;
7171 t = 0;
7272 while (len--) {
73 t = (t << 7) | (in[x] & 0x7F);
74 if (!(in[x++] & 0x80)) {
75 /* store t */
76 if (y >= *outlen) {
77 return CRYPT_BUFFER_OVERFLOW;
78 }
79 if (y == 0) {
80 words[0] = t / 40;
81 words[1] = t % 40;
82 y = 2;
83 } else {
84 words[y++] = t;
73 t = (t << 7) | (in[x] & 0x7F);
74 if (!(in[x++] & 0x80)) {
75 /* store t */
76 if (y >= *outlen) {
77 return CRYPT_BUFFER_OVERFLOW;
78 }
79 if (y == 0) {
80 words[0] = t / 40;
81 words[1] = t % 40;
82 y = 2;
83 } else {
84 words[y++] = t;
85 }
86 t = 0;
8587 }
86 t = 0;
87 }
8888 }
89
89
9090 *outlen = y;
9191 return CRYPT_OK;
9292 }
5454 }
5555
5656 /* store header + length */
57 x = 0;
57 x = 0;
5858 out[x++] = 0x06;
5959 if (z < 128) {
6060 out[x++] = (unsigned char)z;
7070 }
7171
7272 /* store first byte */
73 wordbuf = words[0] * 40 + words[1];
74 for (i = 1; i < nwords; i++) {
75 /* store 7 bit words in little endian */
76 t = wordbuf & 0xFFFFFFFF;
77 if (t) {
78 y = x;
79 mask = 0;
80 while (t) {
81 out[x++] = (unsigned char)((t & 0x7F) | mask);
82 t >>= 7;
83 mask |= 0x80; /* upper bit is set on all but the last byte */
84 }
85 /* now swap bytes y...x-1 */
86 z = x - 1;
87 while (y < z) {
88 t = out[y]; out[y] = out[z]; out[z] = (unsigned char)t;
89 ++y;
90 --z;
91 }
92 } else {
93 /* zero word */
94 out[x++] = 0x00;
95 }
96
97 if (i < nwords - 1) {
98 wordbuf = words[i + 1];
99 }
73 wordbuf = words[0] * 40 + words[1];
74 for (i = 1; i < nwords; i++) {
75 /* store 7 bit words in little endian */
76 t = wordbuf & 0xFFFFFFFF;
77 if (t) {
78 y = x;
79 mask = 0;
80 while (t) {
81 out[x++] = (unsigned char)((t & 0x7F) | mask);
82 t >>= 7;
83 mask |= 0x80; /* upper bit is set on all but the last byte */
84 }
85 /* now swap bytes y...x-1 */
86 z = x - 1;
87 while (y < z) {
88 t = out[y]; out[y] = out[z]; out[z] = (unsigned char)t;
89 ++y;
90 --z;
91 }
92 } else {
93 /* zero word */
94 out[x++] = 0x00;
95 }
96
97 if (i < nwords - 1) {
98 wordbuf = words[i + 1];
99 }
100100 }
101101
102102 *outlen = x;
3131
3232 /**
3333 Gets length of DER encoding of Object Identifier
34 @param nwords The number of OID words
34 @param nwords The number of OID words
3535 @param words The actual OID words to get the size of
3636 @param outlen [out] The length of the DER encoding for the given string
3737 @return CRYPT_OK if successful
3838 */
3939 int der_length_object_identifier(unsigned long *words, unsigned long nwords, unsigned long *outlen)
4040 {
41 unsigned long y, z, t, wordbuf;
41 unsigned long y, z, t, wordbuf;
4242
4343 LTC_ARGCHK(words != NULL);
4444 LTC_ARGCHK(outlen != NULL);
3737
3838 /* get the size */
3939 if ((err = der_length_octet_string(inlen, &len)) != CRYPT_OK) {
40 return err;
40 return err;
4141 }
4242
4343 /* too big? */
1616
1717 #ifdef LTC_DER
1818 /**
19 Gets length of DER encoding of OCTET STRING
19 Gets length of DER encoding of OCTET STRING
2020 @param noctets The number of octets in the string to encode
2121 @param outlen [out] The length of the DER encoding for the given string
2222 @return CRYPT_OK if successful
3636
3737 /* get the size */
3838 if ((err = der_length_printable_string(in, inlen, &len)) != CRYPT_OK) {
39 return err;
39 return err;
4040 }
4141
4242 /* too big? */
1919 static const struct {
2020 int code, value;
2121 } printable_table[] = {
22 { ' ', 32 },
23 { '\'', 39 },
24 { '(', 40 },
25 { ')', 41 },
26 { '+', 43 },
27 { ',', 44 },
28 { '-', 45 },
29 { '.', 46 },
30 { '/', 47 },
31 { '0', 48 },
32 { '1', 49 },
33 { '2', 50 },
34 { '3', 51 },
35 { '4', 52 },
36 { '5', 53 },
37 { '6', 54 },
38 { '7', 55 },
39 { '8', 56 },
40 { '9', 57 },
41 { ':', 58 },
42 { '=', 61 },
43 { '?', 63 },
44 { 'A', 65 },
45 { 'B', 66 },
46 { 'C', 67 },
47 { 'D', 68 },
48 { 'E', 69 },
49 { 'F', 70 },
50 { 'G', 71 },
51 { 'H', 72 },
52 { 'I', 73 },
53 { 'J', 74 },
54 { 'K', 75 },
55 { 'L', 76 },
56 { 'M', 77 },
57 { 'N', 78 },
58 { 'O', 79 },
59 { 'P', 80 },
60 { 'Q', 81 },
61 { 'R', 82 },
62 { 'S', 83 },
63 { 'T', 84 },
64 { 'U', 85 },
65 { 'V', 86 },
66 { 'W', 87 },
67 { 'X', 88 },
68 { 'Y', 89 },
69 { 'Z', 90 },
70 { 'a', 97 },
71 { 'b', 98 },
72 { 'c', 99 },
73 { 'd', 100 },
74 { 'e', 101 },
75 { 'f', 102 },
76 { 'g', 103 },
77 { 'h', 104 },
78 { 'i', 105 },
79 { 'j', 106 },
80 { 'k', 107 },
81 { 'l', 108 },
82 { 'm', 109 },
83 { 'n', 110 },
84 { 'o', 111 },
85 { 'p', 112 },
86 { 'q', 113 },
87 { 'r', 114 },
88 { 's', 115 },
89 { 't', 116 },
90 { 'u', 117 },
91 { 'v', 118 },
92 { 'w', 119 },
93 { 'x', 120 },
94 { 'y', 121 },
95 { 'z', 122 },
22 { ' ', 32 },
23 { '\'', 39 },
24 { '(', 40 },
25 { ')', 41 },
26 { '+', 43 },
27 { ',', 44 },
28 { '-', 45 },
29 { '.', 46 },
30 { '/', 47 },
31 { '0', 48 },
32 { '1', 49 },
33 { '2', 50 },
34 { '3', 51 },
35 { '4', 52 },
36 { '5', 53 },
37 { '6', 54 },
38 { '7', 55 },
39 { '8', 56 },
40 { '9', 57 },
41 { ':', 58 },
42 { '=', 61 },
43 { '?', 63 },
44 { 'A', 65 },
45 { 'B', 66 },
46 { 'C', 67 },
47 { 'D', 68 },
48 { 'E', 69 },
49 { 'F', 70 },
50 { 'G', 71 },
51 { 'H', 72 },
52 { 'I', 73 },
53 { 'J', 74 },
54 { 'K', 75 },
55 { 'L', 76 },
56 { 'M', 77 },
57 { 'N', 78 },
58 { 'O', 79 },
59 { 'P', 80 },
60 { 'Q', 81 },
61 { 'R', 82 },
62 { 'S', 83 },
63 { 'T', 84 },
64 { 'U', 85 },
65 { 'V', 86 },
66 { 'W', 87 },
67 { 'X', 88 },
68 { 'Y', 89 },
69 { 'Z', 90 },
70 { 'a', 97 },
71 { 'b', 98 },
72 { 'c', 99 },
73 { 'd', 100 },
74 { 'e', 101 },
75 { 'f', 102 },
76 { 'g', 103 },
77 { 'h', 104 },
78 { 'i', 105 },
79 { 'j', 106 },
80 { 'k', 107 },
81 { 'l', 108 },
82 { 'm', 109 },
83 { 'n', 110 },
84 { 'o', 111 },
85 { 'p', 112 },
86 { 'q', 113 },
87 { 'r', 114 },
88 { 's', 115 },
89 { 't', 116 },
90 { 'u', 117 },
91 { 'v', 118 },
92 { 'w', 119 },
93 { 'x', 120 },
94 { 'y', 121 },
95 { 'z', 122 },
9696 };
9797
9898 int der_printable_char_encode(int c)
116116 }
117117 return -1;
118118 }
119
119
120120 /**
121 Gets length of DER encoding of Printable STRING
122 @param octets The values you want to encode
121 Gets length of DER encoding of Printable STRING
122 @param octets The values you want to encode
123123 @param noctets The number of octets in the string to encode
124124 @param outlen [out] The length of the DER encoding for the given string
125125 @return CRYPT_OK if successful
1919 /**
2020 Free memory allocated by der_decode_sequence_flexi()
2121 @param in The list to free
22 */
22 */
2323 void der_sequence_free(ltc_asn1_list *in)
2424 {
2525 ltc_asn1_list *l;
2626
2727 if (!in) return;
28
28
2929 /* walk to the start of the chain */
3030 while (in->prev != NULL || in->parent != NULL) {
3131 if (in->parent != NULL) {
3434 in = in->prev;
3535 }
3636 }
37
37
3838 /* now walk the list and free stuff */
3939 while (in != NULL) {
4040 /* is there a child? */
4343 in->child->parent = NULL;
4444 der_sequence_free(in->child);
4545 }
46
47 switch (in->type) {
46
47 switch (in->type) {
4848 case LTC_ASN1_SET:
4949 case LTC_ASN1_SETOF:
5050 case LTC_ASN1_SEQUENCE: break;
5151 case LTC_ASN1_INTEGER : if (in->data != NULL) { mp_clear(in->data); } break;
5252 default : if (in->data != NULL) { XFREE(in->data); }
5353 }
54
54
5555 /* move to next and free current */
5656 l = in->next;
5757 XFREE(in);
5858 in = l;
59 }
59 }
6060 }
6161
6262 #endif
2525 @return CRYPT_OK if successful
2626 */
2727 int der_encode_short_integer(unsigned long num, unsigned char *out, unsigned long *outlen)
28 {
28 {
2929 unsigned long len, x, y, z;
3030 int err;
31
31
3232 LTC_ARGCHK(out != NULL);
3333 LTC_ARGCHK(outlen != NULL);
3434
8585
8686 /* we good */
8787 *outlen = x;
88
88
8989 return CRYPT_OK;
9090 }
9191
1717
1818 #ifdef LTC_DER
1919 /**
20 Gets length of DER encoding of num
21 @param num The integer to get the size of
20 Gets length of DER encoding of num
21 @param num The integer to get the size of
2222 @param outlen [out] The length of the DER encoding for the given integer
2323 @return CRYPT_OK if successful
2424 */
3838 ++z;
3939 y >>= 8;
4040 }
41
41
4242 /* handle zero */
4343 if (z == 0) {
4444 z = 1;
5757 len += (num&(1UL<<((z<<3) - 1))) ? 1 : 0;
5858
5959 /* return length */
60 *outlen = len;
61
60 *outlen = len;
61
6262 return CRYPT_OK;
6363 }
6464
2727 { '\v', 11 },
2828 { '\f', 12 },
2929 { '\r', 13 },
30 { ' ', 32 },
31 { '!', 33 },
32 { '"', 34 },
33 { '%', 37 },
34 { '&', 38 },
35 { '\'', 39 },
36 { '(', 40 },
37 { ')', 41 },
38 { '+', 43 },
39 { ',', 44 },
40 { '-', 45 },
41 { '.', 46 },
42 { '/', 47 },
43 { '0', 48 },
44 { '1', 49 },
45 { '2', 50 },
46 { '3', 51 },
47 { '4', 52 },
48 { '5', 53 },
49 { '6', 54 },
50 { '7', 55 },
51 { '8', 56 },
52 { '9', 57 },
53 { ':', 58 },
54 { ';', 59 },
55 { '<', 60 },
56 { '=', 61 },
57 { '>', 62 },
58 { '?', 63 },
59 { '@', 64 },
60 { 'A', 65 },
61 { 'B', 66 },
62 { 'C', 67 },
63 { 'D', 68 },
64 { 'E', 69 },
65 { 'F', 70 },
66 { 'G', 71 },
67 { 'H', 72 },
68 { 'I', 73 },
69 { 'J', 74 },
70 { 'K', 75 },
71 { 'L', 76 },
72 { 'M', 77 },
73 { 'N', 78 },
74 { 'O', 79 },
75 { 'P', 80 },
76 { 'Q', 81 },
77 { 'R', 82 },
78 { 'S', 83 },
79 { 'T', 84 },
80 { 'U', 85 },
81 { 'V', 86 },
82 { 'W', 87 },
83 { 'X', 88 },
84 { 'Y', 89 },
85 { 'Z', 90 },
86 { '[', 91 },
87 { ']', 93 },
88 { '_', 95 },
89 { 'a', 97 },
90 { 'b', 98 },
91 { 'c', 99 },
92 { 'd', 100 },
93 { 'e', 101 },
94 { 'f', 102 },
95 { 'g', 103 },
96 { 'h', 104 },
97 { 'i', 105 },
98 { 'j', 106 },
99 { 'k', 107 },
100 { 'l', 108 },
101 { 'm', 109 },
102 { 'n', 110 },
103 { 'o', 111 },
104 { 'p', 112 },
105 { 'q', 113 },
106 { 'r', 114 },
107 { 's', 115 },
108 { 't', 116 },
109 { 'u', 117 },
110 { 'v', 118 },
111 { 'w', 119 },
112 { 'x', 120 },
113 { 'y', 121 },
114 { 'z', 122 },
115 { '|', 124 },
116 { ' ', 160 },
117 { 0xa1, 161 },
118 { 0xa2, 162 },
119 { 0xa3, 163 },
120 { '$', 164 },
121 { 0xa5, 165 },
122 { '#', 166 },
123 { 0xa7, 167 },
124 { 0xa4, 168 },
125 { 0xab, 171 },
126 { 0xb0, 176 },
127 { 0xb1, 177 },
128 { 0xb2, 178 },
129 { 0xb3, 179 },
130 { 0xd7, 180 },
131 { 0xb5, 181 },
132 { 0xb6, 182 },
133 { 0xb7, 183 },
134 { 0xf7, 184 },
135 { 0xbb, 187 },
136 { 0xbc, 188 },
137 { 0xbd, 189 },
138 { 0xbe, 190 },
139 { 0xbf, 191 },
30 { ' ', 32 },
31 { '!', 33 },
32 { '"', 34 },
33 { '%', 37 },
34 { '&', 38 },
35 { '\'', 39 },
36 { '(', 40 },
37 { ')', 41 },
38 { '+', 43 },
39 { ',', 44 },
40 { '-', 45 },
41 { '.', 46 },
42 { '/', 47 },
43 { '0', 48 },
44 { '1', 49 },
45 { '2', 50 },
46 { '3', 51 },
47 { '4', 52 },
48 { '5', 53 },
49 { '6', 54 },
50 { '7', 55 },
51 { '8', 56 },
52 { '9', 57 },
53 { ':', 58 },
54 { ';', 59 },
55 { '<', 60 },
56 { '=', 61 },
57 { '>', 62 },
58 { '?', 63 },
59 { '@', 64 },
60 { 'A', 65 },
61 { 'B', 66 },
62 { 'C', 67 },
63 { 'D', 68 },
64 { 'E', 69 },
65 { 'F', 70 },
66 { 'G', 71 },
67 { 'H', 72 },
68 { 'I', 73 },
69 { 'J', 74 },
70 { 'K', 75 },
71 { 'L', 76 },
72 { 'M', 77 },
73 { 'N', 78 },
74 { 'O', 79 },
75 { 'P', 80 },
76 { 'Q', 81 },
77 { 'R', 82 },
78 { 'S', 83 },
79 { 'T', 84 },
80 { 'U', 85 },
81 { 'V', 86 },
82 { 'W', 87 },
83 { 'X', 88 },
84 { 'Y', 89 },
85 { 'Z', 90 },
86 { '[', 91 },
87 { ']', 93 },
88 { '_', 95 },
89 { 'a', 97 },
90 { 'b', 98 },
91 { 'c', 99 },
92 { 'd', 100 },
93 { 'e', 101 },
94 { 'f', 102 },
95 { 'g', 103 },
96 { 'h', 104 },
97 { 'i', 105 },
98 { 'j', 106 },
99 { 'k', 107 },
100 { 'l', 108 },
101 { 'm', 109 },
102 { 'n', 110 },
103 { 'o', 111 },
104 { 'p', 112 },
105 { 'q', 113 },
106 { 'r', 114 },
107 { 's', 115 },
108 { 't', 116 },
109 { 'u', 117 },
110 { 'v', 118 },
111 { 'w', 119 },
112 { 'x', 120 },
113 { 'y', 121 },
114 { 'z', 122 },
115 { '|', 124 },
116 { ' ', 160 },
117 { 0xa1, 161 },
118 { 0xa2, 162 },
119 { 0xa3, 163 },
120 { '$', 164 },
121 { 0xa5, 165 },
122 { '#', 166 },
123 { 0xa7, 167 },
124 { 0xa4, 168 },
125 { 0xab, 171 },
126 { 0xb0, 176 },
127 { 0xb1, 177 },
128 { 0xb2, 178 },
129 { 0xb3, 179 },
130 { 0xd7, 180 },
131 { 0xb5, 181 },
132 { 0xb6, 182 },
133 { 0xb7, 183 },
134 { 0xf7, 184 },
135 { 0xbb, 187 },
136 { 0xbc, 188 },
137 { 0xbd, 189 },
138 { 0xbe, 190 },
139 { 0xbf, 191 },
140140 };
141141
142142 int der_teletex_char_encode(int c)
160160 }
161161 return -1;
162162 }
163
163
164164 /**
165 Gets length of DER encoding of teletex STRING
166 @param octets The values you want to encode
165 Gets length of DER encoding of teletex STRING
166 @param octets The values you want to encode
167167 @param noctets The number of octets in the string to encode
168168 @param outlen [out] The length of the DER encoding for the given string
169169 @return CRYPT_OK if successful
7272 *inlen = 2 + x;
7373
7474
75 /* possible encodings are
75 /* possible encodings are
7676 YYMMDDhhmmZ
7777 YYMMDDhhmm+hh'mm'
7878 YYMMDDhhmm-hh'mm'
8080 YYMMDDhhmmss+hh'mm'
8181 YYMMDDhhmmss-hh'mm'
8282
83 So let's do a trivial decode upto [including] mm
83 So let's do a trivial decode upto [including] mm
8484 */
8585
8686 x = 0;
7272 for (y = 0; x < inlen; ) {
7373 /* get first byte */
7474 tmp = in[x++];
75
75
7676 /* count number of bytes */
7777 for (z = 0; (tmp & 0x80) && (z <= 4); z++, tmp = (tmp << 1) & 0xFF);
78
78
7979 if (z > 4 || (x + (z - 1) > inlen)) {
8080 return CRYPT_INVALID_PACKET;
8181 }
102102
103103 return CRYPT_OK;
104104 }
105
105
106106 #endif
107107
108108 /* $Source$ */
1212 /**
1313 @file dsa_decrypt_key.c
1414 DSA Crypto, Tom St Denis
15 */
15 */
1616
1717 #ifdef LTC_MDSA
1818
2626 @return CRYPT_OK if successful
2727 */
2828 int dsa_decrypt_key(const unsigned char *in, unsigned long inlen,
29 unsigned char *out, unsigned long *outlen,
29 unsigned char *out, unsigned long *outlen,
3030 dsa_key *key)
3131 {
3232 unsigned char *skey, *expt;
4444 if (key->type != PK_PRIVATE) {
4545 return CRYPT_PK_NOT_PRIVATE;
4646 }
47
47
4848 /* decode to find out hash */
4949 LTC_SET_ASN1(decode, 0, LTC_ASN1_OBJECT_IDENTIFIER, hashOID, sizeof(hashOID)/sizeof(hashOID[0]));
50
50
5151 if ((err = der_decode_sequence(in, inlen, decode, 1)) != CRYPT_OK) {
5252 return err;
5353 }
5454
55 hash = find_hash_oid(hashOID, decode[0].size);
55 hash = find_hash_oid(hashOID, decode[0].size);
5656 if (hash_is_valid(hash) != CRYPT_OK) {
5757 return CRYPT_INVALID_PACKET;
5858 }
5959
6060 /* we now have the hash! */
61
61
6262 if ((err = mp_init(&g_pub)) != CRYPT_OK) {
6363 return err;
6464 }
7676 mp_clear(g_pub);
7777 return CRYPT_MEM;
7878 }
79
79
8080 LTC_SET_ASN1(decode, 1, LTC_ASN1_INTEGER, g_pub, 1UL);
8181 LTC_SET_ASN1(decode, 2, LTC_ASN1_OCTET_STRING, skey, MAXBLOCKSIZE);
8282
124124
125125 XFREE(expt);
126126 XFREE(skey);
127
127
128128 mp_clear(g_pub);
129129
130130 return err;
1212 /**
1313 @file dsa_encrypt_key.c
1414 DSA Crypto, Tom St Denis
15 */
15 */
1616
1717 #ifdef LTC_MDSA
1818
2323 @param out [out] The destination for the ciphertext
2424 @param outlen [in/out] The max size and resulting size of the ciphertext
2525 @param prng An active PRNG state
26 @param wprng The index of the PRNG you wish to use
27 @param hash The index of the hash you want to use
26 @param wprng The index of the PRNG you wish to use
27 @param hash The index of the hash you want to use
2828 @param key The DSA key you want to encrypt to
2929 @return CRYPT_OK if successful
3030 */
3131 int dsa_encrypt_key(const unsigned char *in, unsigned long inlen,
32 unsigned char *out, unsigned long *outlen,
33 prng_state *prng, int wprng, int hash,
32 unsigned char *out, unsigned long *outlen,
33 prng_state *prng, int wprng, int hash,
3434 dsa_key *key)
3535 {
3636 unsigned char *expt, *skey;
6060 if ((err = mp_init_multi(&g_pub, &g_priv, NULL)) != CRYPT_OK) {
6161 return err;
6262 }
63
63
6464 expt = XMALLOC(mp_unsigned_bin_size(key->p) + 1);
6565 skey = XMALLOC(MAXBLOCKSIZE);
6666 if (expt == NULL || skey == NULL) {
7373 mp_clear_multi(g_pub, g_priv, NULL);
7474 return CRYPT_MEM;
7575 }
76
76
7777 /* make a random g_priv, g_pub = g^x pair */
7878 qbits = mp_count_bits(key->q);
7979 do {
8787 if ((err = mp_exptmod(key->g, g_priv, key->p, g_pub)) != CRYPT_OK) {
8888 goto LBL_ERR;
8989 }
90
90
9191 /* make random key */
9292 x = mp_unsigned_bin_size(key->p) + 1;
9393 if ((err = dsa_shared_secret(g_priv, key->y, key, expt, &x)) != CRYPT_OK) {
9898 if ((err = hash_memory(hash, expt, x, skey, &y)) != CRYPT_OK) {
9999 goto LBL_ERR;
100100 }
101
101
102102 /* Encrypt key */
103103 for (x = 0; x < inlen; x++) {
104104 skey[x] ^= in[x];
119119
120120 XFREE(skey);
121121 XFREE(expt);
122
122
123123 mp_clear_multi(g_pub, g_priv, NULL);
124124 return err;
125125 }
3636 /* init key */
3737 err = mp_init_multi(&key->p, &key->g, &key->q, &key->x, &key->y, NULL);
3838 if (err != CRYPT_OK) return err;
39
39
4040 if ((err = mp_read_radix(key->p , p , radix)) != CRYPT_OK) { goto LBL_ERR; }
4141 if ((err = mp_read_radix(key->q , q , radix)) != CRYPT_OK) { goto LBL_ERR; }
4242 if ((err = mp_read_radix(key->g , g , radix)) != CRYPT_OK) { goto LBL_ERR; }
1212 /**
1313 @file dsa_shared_secret.c
1414 DSA Crypto, Tom St Denis
15 */
15 */
1616
1717 #ifdef LTC_MDSA
1818
1919 /**
2020 Create a DSA shared secret between two keys
2121 @param private_key The private DSA key (the exponent)
22 @param base The base of the exponentiation (allows this to be used for both encrypt and decrypt)
22 @param base The base of the exponentiation (allows this to be used for both encrypt and decrypt)
2323 @param public_key The public key
2424 @param out [out] Destination of the shared secret
2525 @param outlen [in/out] The max size and resulting size of the shared secret
4747 mp_clear(res);
4848 return err;
4949 }
50
50
5151 x = (unsigned long)mp_unsigned_bin_size(res);
5252 if (*outlen < x) {
5353 *outlen = x;
8888 /* at this point we are out of tests ;-( */
8989 err = CRYPT_OK;
9090 *stat = 1;
91 error:
91 error:
9292 mp_clear_multi(tmp, tmp2, NULL);
9393 return err;
9494 }
99 */
1010 #include "tomcrypt.h"
1111
12 /**
12 /**
1313 @file pkcs_1_os2ip.c
14 Octet to Integer OS2IP, Tom St Denis
14 Octet to Integer OS2IP, Tom St Denis
1515 */
1616 #ifdef LTC_PKCS_1
1717
1212 /**
1313 @file rsa_free.c
1414 Free an RSA key, Tom St Denis
15 */
15 */
1616
1717 #ifdef LTC_MRSA
1818
8787 return CRYPT_INVALID_ARG;
8888 }
8989
90 /* construct the SEQUENCE
90 /* construct the SEQUENCE
9191 SEQUENCE {
9292 SEQUENCE {hashoid OID
9393 blah NULL
9494 }
95 hash OCTET STRING
95 hash OCTET STRING
9696 }
9797 */
9898 LTC_SET_ASN1(digestinfo, 0, LTC_ASN1_OBJECT_IDENTIFIER, hash_descriptor[hash_idx].OID, hash_descriptor[hash_idx].OIDlen);
1313 @file fortuna.c
1414 Fortuna PRNG, Tom St Denis
1515 */
16
17 /* Implementation of Fortuna by Tom St Denis
16
17 /* Implementation of Fortuna by Tom St Denis
1818
1919 We deviate slightly here for reasons of simplicity [and to fit in the API]. First all "sources"
20 in the AddEntropy function are fixed to 0. Second since no reliable timer is provided
20 in the AddEntropy function are fixed to 0. Second since no reliable timer is provided
2121 we reseed automatically when len(pool0) >= 64 or every LTC_FORTUNA_WD calls to the read function */
2222
23 #ifdef LTC_FORTUNA
23 #ifdef LTC_FORTUNA
2424
2525 /* requries LTC_SHA256 and AES */
2626 #if !(defined(LTC_RIJNDAEL) && defined(LTC_SHA256))
7878 }
7979
8080 for (x = 0; x < LTC_FORTUNA_POOLS; x++) {
81 if (x == 0 || ((prng->fortuna.reset_cnt >> (x-1)) & 1) == 0) {
81 if (x == 0 || ((prng->fortuna.reset_cnt >> (x-1)) & 1) == 0) {
8282 /* terminate this hash */
8383 if ((err = sha256_done(&prng->fortuna.pool[x], tmp)) != CRYPT_OK) {
8484 sha256_done(&md, tmp);
85 return err;
85 return err;
8686 }
8787 /* add it to the string */
8888 if ((err = sha256_process(&md, tmp, 32)) != CRYPT_OK) {
101101
102102 /* finish key */
103103 if ((err = sha256_done(&md, prng->fortuna.K)) != CRYPT_OK) {
104 return err;
104 return err;
105105 }
106106 if ((err = rijndael_setup(prng->fortuna.K, 32, 0, &prng->fortuna.skey)) != CRYPT_OK) {
107107 return err;
125125 Start the PRNG
126126 @param prng [out] The PRNG state to initialize
127127 @return CRYPT_OK if successful
128 */
128 */
129129 int fortuna_start(prng_state *prng)
130130 {
131131 int err, x, y;
132132 unsigned char tmp[MAXBLOCKSIZE];
133133
134134 LTC_ARGCHK(prng != NULL);
135
135
136136 /* initialize the pools */
137137 for (x = 0; x < LTC_FORTUNA_POOLS; x++) {
138138 if ((err = sha256_init(&prng->fortuna.pool[x])) != CRYPT_OK) {
154154 return err;
155155 }
156156 zeromem(prng->fortuna.IV, 16);
157
157
158158 LTC_MUTEX_INIT(&prng->fortuna.prng_lock)
159
159
160160 return CRYPT_OK;
161161 }
162162
166166 @param inlen Length of the data to add
167167 @param prng PRNG state to update
168168 @return CRYPT_OK if successful
169 */
169 */
170170 int fortuna_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng)
171171 {
172172 unsigned char tmp[2];
209209 Make the PRNG ready to read from
210210 @param prng The PRNG to make active
211211 @return CRYPT_OK if successful
212 */
212 */
213213 int fortuna_ready(prng_state *prng)
214214 {
215215 return fortuna_reseed(prng);
221221 @param outlen Length of output
222222 @param prng The active PRNG to read from
223223 @return Number of octets read
224 */
224 */
225225 unsigned long fortuna_read(unsigned char *out, unsigned long outlen, prng_state *prng)
226226 {
227227 unsigned char tmp[16];
258258 XMEMCPY(out, tmp, outlen);
259259 fortuna_update_iv(prng);
260260 }
261
261
262262 /* generate new key */
263 rijndael_ecb_encrypt(prng->fortuna.IV, prng->fortuna.K , &prng->fortuna.skey);
263 rijndael_ecb_encrypt(prng->fortuna.IV, prng->fortuna.K , &prng->fortuna.skey);
264264 fortuna_update_iv(prng);
265
266 rijndael_ecb_encrypt(prng->fortuna.IV, prng->fortuna.K+16, &prng->fortuna.skey);
265
266 rijndael_ecb_encrypt(prng->fortuna.IV, prng->fortuna.K+16, &prng->fortuna.skey);
267267 fortuna_update_iv(prng);
268
268
269269 if (rijndael_setup(prng->fortuna.K, 32, 0, &prng->fortuna.skey) != CRYPT_OK) {
270270 LTC_MUTEX_UNLOCK(&prng->fortuna.prng_lock);
271271 return 0;
276276 #endif
277277 LTC_MUTEX_UNLOCK(&prng->fortuna.prng_lock);
278278 return tlen;
279 }
279 }
280280
281281 /**
282282 Terminate the PRNG
283283 @param prng The PRNG to terminate
284284 @return CRYPT_OK if successful
285 */
285 */
286286 int fortuna_done(prng_state *prng)
287287 {
288288 int err, x;
295295 for (x = 0; x < LTC_FORTUNA_POOLS; x++) {
296296 if ((err = sha256_done(&(prng->fortuna.pool[x]), tmp)) != CRYPT_OK) {
297297 LTC_MUTEX_UNLOCK(&prng->fortuna.prng_lock);
298 return err;
298 return err;
299299 }
300300 }
301301 /* call cipher done when we invent one ;-) */
314314 @param outlen [in/out] Max size and resulting size of the state
315315 @param prng The PRNG to export
316316 @return CRYPT_OK if successful
317 */
317 */
318318 int fortuna_export(unsigned char *out, unsigned long *outlen, prng_state *prng)
319319 {
320320 int x, err;
339339 return CRYPT_MEM;
340340 }
341341
342 /* to emit the state we copy each pool, terminate it then hash it again so
343 * an attacker who sees the state can't determine the current state of the PRNG
344 */
342 /* to emit the state we copy each pool, terminate it then hash it again so
343 * an attacker who sees the state can't determine the current state of the PRNG
344 */
345345 for (x = 0; x < LTC_FORTUNA_POOLS; x++) {
346346 /* copy the PRNG */
347347 XMEMCPY(md, &(prng->fortuna.pool[x]), sizeof(*md));
373373 LTC_MUTEX_UNLOCK(&prng->fortuna.prng_lock);
374374 return err;
375375 }
376
376
377377 /**
378378 Import a PRNG state
379379 @param in The PRNG state
380380 @param inlen Size of the state
381381 @param prng The PRNG to import
382382 @return CRYPT_OK if successful
383 */
383 */
384384 int fortuna_import(const unsigned char *in, unsigned long inlen, prng_state *prng)
385385 {
386386 int err, x;
406406 /**
407407 PRNG self-test
408408 @return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled
409 */
409 */
410410 int fortuna_test(void)
411411 {
412412 #ifndef LTC_TEST
1212 /**
1313 @file rc4.c
1414 LTC_RC4 PRNG, Tom St Denis
15 */
15 */
1616
1717 #ifdef LTC_RC4
1818
19 const struct ltc_prng_descriptor rc4_desc =
19 const struct ltc_prng_descriptor rc4_desc =
2020 {
2121 "rc4", 32,
2222 &rc4_start,
3333 Start the PRNG
3434 @param prng [out] The PRNG state to initialize
3535 @return CRYPT_OK if successful
36 */
36 */
3737 int rc4_start(prng_state *prng)
3838 {
3939 LTC_ARGCHK(prng != NULL);
4040
4141 /* set keysize to zero */
4242 prng->rc4.x = 0;
43
43
4444 return CRYPT_OK;
4545 }
4646
5050 @param inlen Length of the data to add
5151 @param prng PRNG state to update
5252 @return CRYPT_OK if successful
53 */
53 */
5454 int rc4_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng)
5555 {
5656 LTC_ARGCHK(in != NULL);
5757 LTC_ARGCHK(prng != NULL);
58
58
5959 /* trim as required */
6060 if (prng->rc4.x + inlen > 256) {
6161 if (prng->rc4.x == 256) {
6464 } else {
6565 /* only accept part of it */
6666 inlen = 256 - prng->rc4.x;
67 }
67 }
6868 }
6969
7070 while (inlen--) {
7272 }
7373
7474 return CRYPT_OK;
75
75
7676 }
7777
7878 /**
7979 Make the PRNG ready to read from
8080 @param prng The PRNG to make active
8181 @return CRYPT_OK if successful
82 */
82 */
8383 int rc4_ready(prng_state *prng)
8484 {
8585 unsigned char key[256], tmp, *s;
100100 for (j = x = y = 0; x < 256; x++) {
101101 y = (y + prng->rc4.buf[x] + key[j++]) & 255;
102102 if (j == keylen) {
103 j = 0;
103 j = 0;
104104 }
105105 tmp = s[x]; s[x] = s[y]; s[y] = tmp;
106106 }
120120 @param outlen Length of output
121121 @param prng The active PRNG to read from
122122 @return Number of octets read
123 */
123 */
124124 unsigned long rc4_read(unsigned char *out, unsigned long outlen, prng_state *prng)
125125 {
126126 unsigned char x, y, *s, tmp;
153153 Terminate the PRNG
154154 @param prng The PRNG to terminate
155155 @return CRYPT_OK if successful
156 */
156 */
157157 int rc4_done(prng_state *prng)
158158 {
159159 LTC_ARGCHK(prng != NULL);
166166 @param outlen [in/out] Max size and resulting size of the state
167167 @param prng The PRNG to export
168168 @return CRYPT_OK if successful
169 */
169 */
170170 int rc4_export(unsigned char *out, unsigned long *outlen, prng_state *prng)
171171 {
172172 LTC_ARGCHK(outlen != NULL);
185185
186186 return CRYPT_OK;
187187 }
188
188
189189 /**
190190 Import a PRNG state
191191 @param in The PRNG state
192192 @param inlen Size of the state
193193 @param prng The PRNG to import
194194 @return CRYPT_OK if successful
195 */
195 */
196196 int rc4_import(const unsigned char *in, unsigned long inlen, prng_state *prng)
197197 {
198198 int err;
202202 if (inlen != 32) {
203203 return CRYPT_INVALID_ARG;
204204 }
205
205
206206 if ((err = rc4_start(prng)) != CRYPT_OK) {
207207 return err;
208208 }
212212 /**
213213 PRNG self-test
214214 @return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled
215 */
215 */
216216 int rc4_test(void)
217217 {
218218 #if !defined(LTC_TEST) || defined(LTC_VALGRIND)
249249 if (XMEMCMP(dst, tests[x].ct, 8)) {
250250 #if 0
251251 int y;
252 printf("\n\nLTC_RC4 failed, I got:\n");
252 printf("\n\nLTC_RC4 failed, I got:\n");
253253 for (y = 0; y < 8; y++) printf("%02x ", dst[y]);
254254 printf("\n");
255255 #endif
310310 }
311311
312312 #ifndef LTC_SMALL_CODE
313 /* do lots at a time, if there's enough to do */
314 while (outlen >= N*4) {
313 /* do lots at a time, if there's enough to do */
314 while (outlen >= N*4) {
315315 SROUND(0);
316316 SROUND(1);
317317 SROUND(2);
331331 SROUND(16);
332332 out += 4*N;
333333 outlen -= 4*N;
334 }
334 }
335335 #endif
336336
337 /* do small or odd size buffers the slow way */
338 while (4 <= outlen) {
337 /* do small or odd size buffers the slow way */
338 while (4 <= outlen) {
339339 cycle(c->R);
340340 t = nltap(c);
341341 XORWORD(t, out);
342342 out += 4;
343343 outlen -= 4;
344 }
345
346 /* handle any trailing bytes */
347 if (outlen != 0) {
344 }
345
346 /* handle any trailing bytes */
347 if (outlen != 0) {
348348 cycle(c->R);
349349 c->sbuf = nltap(c);
350350 c->nbuf = 32;
354354 c->nbuf -= 8;
355355 --outlen;
356356 }
357 }
358
359 return tlen;
357 }
358
359 return tlen;
360360 }
361361
362362 /**