Revert "Revert "and added files""
[bcm963xx.git] / userapps / opensource / sshd / libtomcrypt / yarrow.c
1 #include "mycrypt.h"
2
3 #ifdef YARROW
4
5 const struct _prng_descriptor yarrow_desc =
6 {
7     "yarrow",
8     &yarrow_start,
9     &yarrow_add_entropy,
10     &yarrow_ready,
11     &yarrow_read
12 };
13
14 int yarrow_start(prng_state *prng)
15 {
16    int err;
17    
18    _ARGCHK(prng != NULL);
19
20    /* these are the default hash/cipher combo used */
21 #ifdef RIJNDAEL
22    prng->yarrow.cipher = register_cipher(&rijndael_desc);
23 #elif defined(NOEKEON)   
24    prng->yarrow.cipher = register_cipher(&noekeon_desc);
25 #elif defined(BLOWFISH)
26    prng->yarrow.cipher = register_cipher(&blowfish_desc);
27 #elif defined(TWOFISH)
28    prng->yarrow.cipher = register_cipher(&twofish_desc);
29 #elif defined(CAST5)
30    prng->yarrow.cipher = register_cipher(&cast5_desc);
31 #elif defined(SAFER)
32    prng->yarrow.cipher = register_cipher(&saferp_desc);
33 #elif defined(RC5)
34    prng->yarrow.cipher = register_cipher(&rc5_desc);
35 #elif defined(RC6)
36    prng->yarrow.cipher = register_cipher(&rc6_desc);
37 #elif defined(XTEA)
38    prng->yarrow.cipher = register_cipher(&xtea_desc);
39 #elif defined(RC2)
40    prng->yarrow.cipher = register_cipher(&rc2_desc);
41 #elif defined(DES)
42    prng->yarrow.cipher = register_cipher(&des3_desc);
43 #elif
44    #error YARROW needs at least one CIPHER
45 #endif
46    if ((err = cipher_is_valid(prng->yarrow.cipher)) != CRYPT_OK) {
47       return err;
48    }
49
50 #ifdef SHA256
51    prng->yarrow.hash   = register_hash(&sha256_desc);
52 #elif defined(SHA512)
53    prng->yarrow.hash   = register_hash(&sha512_desc);
54 #elif defined(SHA384)
55    prng->yarrow.hash   = register_hash(&sha384_desc);
56 #elif defined(SHA1)
57    prng->yarrow.hash   = register_hash(&sha1_desc);
58 #elif defined(TIGER)
59    prng->yarrow.hash   = register_hash(&tiger_desc);
60 #elif defined(MD5)
61    prng->yarrow.hash   = register_hash(&md5_desc);
62 #elif defined(MD4)
63    prng->yarrow.hash   = register_hash(&md4_desc);
64 #elif defined(MD2)
65    prng->yarrow.hash   = register_hash(&md2_desc);
66 #else
67    #error YARROW needs at least one HASH
68 #endif
69    if ((err = hash_is_valid(prng->yarrow.hash)) != CRYPT_OK) {
70       return err;
71    }
72
73    /* zero the memory used */
74    zeromem(prng->yarrow.pool, sizeof(prng->yarrow.pool));
75
76    return CRYPT_OK;
77 }
78
79 int yarrow_add_entropy(const unsigned char *buf, unsigned long len, prng_state *prng)
80 {
81    hash_state md;
82    int err;
83
84    _ARGCHK(buf != NULL);
85    _ARGCHK(prng != NULL);
86
87    if ((err = hash_is_valid(prng->yarrow.hash)) != CRYPT_OK) {
88       return err;
89    }
90
91    /* start the hash */
92    hash_descriptor[prng->yarrow.hash].init(&md);
93
94    /* hash the current pool */
95    hash_descriptor[prng->yarrow.hash].process(&md, prng->yarrow.pool, hash_descriptor[prng->yarrow.hash].hashsize);
96
97    /* add the new entropy */
98    hash_descriptor[prng->yarrow.hash].process(&md, buf, len);
99
100    /* store result */
101    hash_descriptor[prng->yarrow.hash].done(&md, prng->yarrow.pool);
102
103    return CRYPT_OK;
104 }
105
106 int yarrow_ready(prng_state *prng)
107 {
108    int ks, err;
109
110    _ARGCHK(prng != NULL);
111
112    if ((err = hash_is_valid(prng->yarrow.hash)) != CRYPT_OK) {
113       return err;
114    }
115    
116    if ((err = cipher_is_valid(prng->yarrow.cipher)) != CRYPT_OK) {
117       return err;
118    }
119
120    /* setup CTR mode using the "pool" as the key */
121    ks = (int)hash_descriptor[prng->yarrow.hash].hashsize;
122    if ((err = cipher_descriptor[prng->yarrow.cipher].keysize(&ks)) != CRYPT_OK) {
123       return err;
124    }
125
126    if ((err = ctr_start(prng->yarrow.cipher, prng->yarrow.pool, prng->yarrow.pool, ks, 0, &prng->yarrow.ctr)) != CRYPT_OK) {
127       return err;
128    }
129    return CRYPT_OK;
130 }
131
132 unsigned long yarrow_read(unsigned char *buf, unsigned long len, prng_state *prng)
133 {
134    _ARGCHK(buf != NULL);
135    _ARGCHK(prng != NULL);
136
137    /* put buf in predictable state first */
138    zeromem(buf, len);
139    
140    /* now randomize it */
141    if (ctr_encrypt(buf, buf, len, &prng->yarrow.ctr) != CRYPT_OK) {
142       return 0;
143    }
144    return len;
145 }
146
147 #endif
148