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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
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
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
22 &yarrow_read,
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
23 &yarrow_done,
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
24 &yarrow_export,
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
25 &yarrow_import,
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
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
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
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
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
121 if ((err = hash_descriptor[prng->yarrow.hash].init(&md)) != CRYPT_OK) {
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
122 return err;
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
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
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
189 int yarrow_done(prng_state *prng)
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
190 {
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
191 _ARGCHK(prng != NULL);
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
192 /* call cipher done when we invent one ;-) */
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
193
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
194 return CRYPT_OK;
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
195 }
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
196
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
197 int yarrow_export(unsigned char *out, unsigned long *outlen, prng_state *prng)
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
198 {
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
199 _ARGCHK(out != NULL);
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
200 _ARGCHK(outlen != NULL);
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
201 _ARGCHK(prng != NULL);
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
202
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
203 /* we'll write 64 bytes for s&g's */
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
204 if (*outlen < 64) {
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
205 return CRYPT_BUFFER_OVERFLOW;
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
206 }
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
207
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
208 if (yarrow_read(out, 64, prng) != 64) {
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
209 return CRYPT_ERROR_READPRNG;
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
210 }
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
211 *outlen = 64;
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
212
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
213 return CRYPT_OK;
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
214 }
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
215
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
216 int yarrow_import(const unsigned char *in, unsigned long inlen, prng_state *prng)
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
217 {
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
218 int err;
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
219
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
220 _ARGCHK(in != NULL);
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
221 _ARGCHK(prng != NULL);
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
222
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
223 if (inlen != 64) {
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
224 return CRYPT_INVALID_ARG;
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
225 }
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
226
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
227 if ((err = yarrow_start(prng)) != CRYPT_OK) {
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
228 return err;
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
229 }
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
230 return yarrow_add_entropy(in, 64, prng);
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
231 }
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
232
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
233 int yarrow_test(void)
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
234 {
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
235 #ifndef LTC_TEST
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
236 return CRYPT_NOP;
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
237 #else
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
238 int err;
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
239 prng_state prng;
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
240
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
241 if ((err = yarrow_start(&prng)) != CRYPT_OK) {
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
242 return err;
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
243 }
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
244
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
245 /* now let's test the hash/cipher that was chosen */
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
246 if ((err = cipher_descriptor[prng.yarrow.cipher].test()) != CRYPT_OK) {
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
247 return err;
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
248 }
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
249 if ((err = hash_descriptor[prng.yarrow.hash].test()) != CRYPT_OK) {
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
250 return err;
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
251 }
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
252
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
253 yarrow_done(&prng);
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
254 return CRYPT_OK;
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
255 #endif
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
256 }
5d99163f7e32 import of libtomcrypt 0.99
Matt Johnston <matt@ucc.asn.au>
parents: 15
diff changeset
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