Mercurial > dropbear
annotate yarrow.c @ 164:cd1143579f00 libtomcrypt LTC_DB_0.44
mpi.c isn't needed if we're using libtommath seperately
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sun, 02 Jan 2005 17:19:46 +0000 |
parents | 7ed585a2c53b |
children |
rev | line source |
---|---|
20
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
2 * |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
3 * LibTomCrypt is a library that provides various cryptographic |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
4 * algorithms in a highly modular and flexible manner. |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
5 * |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
6 * The library is free for all purposes without any express |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
7 * guarantee it works. |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
8 * |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
9 * Tom St Denis, [email protected], http://libtomcrypt.org |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
10 */ |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
11 |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
12 #include "mycrypt.h" |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
13 |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
14 #ifdef YARROW |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
15 |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
16 const struct _prng_descriptor yarrow_desc = |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
17 { |
143 | 18 "yarrow", 64, |
20
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
19 &yarrow_start, |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
20 &yarrow_add_entropy, |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
21 &yarrow_ready, |
143 | 22 &yarrow_read, |
23 &yarrow_done, | |
24 &yarrow_export, | |
25 &yarrow_import, | |
26 &yarrow_test | |
20
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
27 }; |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
28 |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
29 int yarrow_start(prng_state *prng) |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
30 { |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
31 int err; |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
32 |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
33 _ARGCHK(prng != NULL); |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
34 |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
35 /* these are the default hash/cipher combo used */ |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
36 #ifdef RIJNDAEL |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
37 #if YARROW_AES==0 |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
38 prng->yarrow.cipher = register_cipher(&rijndael_enc_desc); |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
39 #elif YARROW_AES==1 |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
40 prng->yarrow.cipher = register_cipher(&aes_enc_desc); |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
41 #elif YARROW_AES==2 |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
42 prng->yarrow.cipher = register_cipher(&rijndael_desc); |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
43 #elif YARROW_AES==3 |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
44 prng->yarrow.cipher = register_cipher(&aes_desc); |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
45 #endif |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
46 #elif defined(BLOWFISH) |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
47 prng->yarrow.cipher = register_cipher(&blowfish_desc); |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
48 #elif defined(TWOFISH) |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
49 prng->yarrow.cipher = register_cipher(&twofish_desc); |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
50 #elif defined(RC6) |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
51 prng->yarrow.cipher = register_cipher(&rc6_desc); |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
52 #elif defined(RC5) |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
53 prng->yarrow.cipher = register_cipher(&rc5_desc); |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
54 #elif defined(SAFERP) |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
55 prng->yarrow.cipher = register_cipher(&saferp_desc); |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
56 #elif defined(RC2) |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
57 prng->yarrow.cipher = register_cipher(&rc2_desc); |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
58 #elif defined(NOEKEON) |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
59 prng->yarrow.cipher = register_cipher(&noekeon_desc); |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
60 #elif defined(CAST5) |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
61 prng->yarrow.cipher = register_cipher(&cast5_desc); |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
62 #elif defined(XTEA) |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
63 prng->yarrow.cipher = register_cipher(&xtea_desc); |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
64 #elif defined(SAFER) |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
65 prng->yarrow.cipher = register_cipher(&safer_sk128_desc); |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
66 #elif defined(DES) |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
67 prng->yarrow.cipher = register_cipher(&des3_desc); |
143 | 68 #else |
20
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
69 #error YARROW needs at least one CIPHER |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
70 #endif |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
71 if ((err = cipher_is_valid(prng->yarrow.cipher)) != CRYPT_OK) { |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
72 return err; |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
73 } |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
74 |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
75 #ifdef SHA256 |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
76 prng->yarrow.hash = register_hash(&sha256_desc); |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
77 #elif defined(SHA512) |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
78 prng->yarrow.hash = register_hash(&sha512_desc); |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
79 #elif defined(TIGER) |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
80 prng->yarrow.hash = register_hash(&tiger_desc); |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
81 #elif defined(SHA1) |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
82 prng->yarrow.hash = register_hash(&sha1_desc); |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
83 #elif defined(RIPEMD160) |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
84 prng->yarrow.hash = register_hash(&rmd160_desc); |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
85 #elif defined(RIPEMD128) |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
86 prng->yarrow.hash = register_hash(&rmd128_desc); |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
87 #elif defined(MD5) |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
88 prng->yarrow.hash = register_hash(&md5_desc); |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
89 #elif defined(MD4) |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
90 prng->yarrow.hash = register_hash(&md4_desc); |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
91 #elif defined(MD2) |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
92 prng->yarrow.hash = register_hash(&md2_desc); |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
93 #elif defined(WHIRLPOOL) |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
94 prng->yarrow.hash = register_hash(&whirlpool_desc); |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
95 #else |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
96 #error YARROW needs at least one HASH |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
97 #endif |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
98 if ((err = hash_is_valid(prng->yarrow.hash)) != CRYPT_OK) { |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
99 return err; |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
100 } |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
101 |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
102 /* zero the memory used */ |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
103 zeromem(prng->yarrow.pool, sizeof(prng->yarrow.pool)); |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
104 |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
105 return CRYPT_OK; |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
106 } |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
107 |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
108 int yarrow_add_entropy(const unsigned char *buf, unsigned long len, prng_state *prng) |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
109 { |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
110 hash_state md; |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
111 int err; |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
112 |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
113 _ARGCHK(buf != NULL); |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
114 _ARGCHK(prng != NULL); |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
115 |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
116 if ((err = hash_is_valid(prng->yarrow.hash)) != CRYPT_OK) { |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
117 return err; |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
118 } |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
119 |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
120 /* start the hash */ |
143 | 121 if ((err = hash_descriptor[prng->yarrow.hash].init(&md)) != CRYPT_OK) { |
122 return err; | |
123 } | |
20
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
124 |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
125 /* hash the current pool */ |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
126 if ((err = hash_descriptor[prng->yarrow.hash].process(&md, prng->yarrow.pool, |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
127 hash_descriptor[prng->yarrow.hash].hashsize)) != CRYPT_OK) { |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
128 return err; |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
129 } |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
130 |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
131 /* add the new entropy */ |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
132 if ((err = hash_descriptor[prng->yarrow.hash].process(&md, buf, len)) != CRYPT_OK) { |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
133 return err; |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
134 } |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
135 |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
136 /* store result */ |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
137 if ((err = hash_descriptor[prng->yarrow.hash].done(&md, prng->yarrow.pool)) != CRYPT_OK) { |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
138 return err; |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
139 } |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
140 |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
141 return CRYPT_OK; |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
142 } |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
143 |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
144 int yarrow_ready(prng_state *prng) |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
145 { |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
146 int ks, err; |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
147 |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
148 _ARGCHK(prng != NULL); |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
149 |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
150 if ((err = hash_is_valid(prng->yarrow.hash)) != CRYPT_OK) { |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
151 return err; |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
152 } |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
153 |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
154 if ((err = cipher_is_valid(prng->yarrow.cipher)) != CRYPT_OK) { |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
155 return err; |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
156 } |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
157 |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
158 /* setup CTR mode using the "pool" as the key */ |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
159 ks = (int)hash_descriptor[prng->yarrow.hash].hashsize; |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
160 if ((err = cipher_descriptor[prng->yarrow.cipher].keysize(&ks)) != CRYPT_OK) { |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
161 return err; |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
162 } |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
163 |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
164 if ((err = ctr_start(prng->yarrow.cipher, /* what cipher to use */ |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
165 prng->yarrow.pool, /* IV */ |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
166 prng->yarrow.pool, ks, /* KEY and key size */ |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
167 0, /* number of rounds */ |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
168 &prng->yarrow.ctr)) != CRYPT_OK) { |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
169 return err; |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
170 } |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
171 return CRYPT_OK; |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
172 } |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
173 |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
174 unsigned long yarrow_read(unsigned char *buf, unsigned long len, prng_state *prng) |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
175 { |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
176 _ARGCHK(buf != NULL); |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
177 _ARGCHK(prng != NULL); |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
178 |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
179 /* put buf in predictable state first */ |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
180 zeromem(buf, len); |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
181 |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
182 /* now randomize it */ |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
183 if (ctr_encrypt(buf, buf, len, &prng->yarrow.ctr) != CRYPT_OK) { |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
184 return 0; |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
185 } |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
186 return len; |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
187 } |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
188 |
143 | 189 int yarrow_done(prng_state *prng) |
190 { | |
191 _ARGCHK(prng != NULL); | |
192 /* call cipher done when we invent one ;-) */ | |
193 | |
194 return CRYPT_OK; | |
195 } | |
196 | |
197 int yarrow_export(unsigned char *out, unsigned long *outlen, prng_state *prng) | |
198 { | |
199 _ARGCHK(out != NULL); | |
200 _ARGCHK(outlen != NULL); | |
201 _ARGCHK(prng != NULL); | |
202 | |
203 /* we'll write 64 bytes for s&g's */ | |
204 if (*outlen < 64) { | |
205 return CRYPT_BUFFER_OVERFLOW; | |
206 } | |
207 | |
208 if (yarrow_read(out, 64, prng) != 64) { | |
209 return CRYPT_ERROR_READPRNG; | |
210 } | |
211 *outlen = 64; | |
212 | |
213 return CRYPT_OK; | |
214 } | |
215 | |
216 int yarrow_import(const unsigned char *in, unsigned long inlen, prng_state *prng) | |
217 { | |
218 int err; | |
219 | |
220 _ARGCHK(in != NULL); | |
221 _ARGCHK(prng != NULL); | |
222 | |
223 if (inlen != 64) { | |
224 return CRYPT_INVALID_ARG; | |
225 } | |
226 | |
227 if ((err = yarrow_start(prng)) != CRYPT_OK) { | |
228 return err; | |
229 } | |
230 return yarrow_add_entropy(in, 64, prng); | |
231 } | |
232 | |
233 int yarrow_test(void) | |
234 { | |
235 #ifndef LTC_TEST | |
236 return CRYPT_NOP; | |
237 #else | |
238 int err; | |
239 prng_state prng; | |
240 | |
241 if ((err = yarrow_start(&prng)) != CRYPT_OK) { | |
242 return err; | |
243 } | |
244 | |
245 /* now let's test the hash/cipher that was chosen */ | |
246 if ((err = cipher_descriptor[prng.yarrow.cipher].test()) != CRYPT_OK) { | |
247 return err; | |
248 } | |
249 if ((err = hash_descriptor[prng.yarrow.hash].test()) != CRYPT_OK) { | |
250 return err; | |
251 } | |
252 | |
253 yarrow_done(&prng); | |
254 return CRYPT_OK; | |
255 #endif | |
256 } | |
257 | |
20
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
258 #endif |
b939f2d4431e
Include files accidentally zeroed when merging 0.96 release
Matt Johnston <matt@ucc.asn.au>
parents:
16
diff
changeset
|
259 |