1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
14 CHC support. (Tom St Denis)
19 #define UNDEFED_HASH -17
22 static int cipher_idx=UNDEFED_HASH, /* which cipher */
23 cipher_blocksize; /* blocksize of cipher */
26 const struct ltc_hash_descriptor chc_desc = {
27 "chc_hash", 12, 0, 0, { 0 }, 0,
36 Initialize the CHC state with a given cipher
37 @param cipher The index of the cipher you wish to bind
38 @return CRYPT_OK if successful
40 int chc_register(int cipher)
44 if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
48 /* will it be valid? */
49 kl = cipher_descriptor[cipher].block_length;
51 /* must be >64 bit block */
53 return CRYPT_INVALID_CIPHER;
56 /* can we use the ideal keysize? */
57 if ((err = cipher_descriptor[cipher].keysize(&kl)) != CRYPT_OK) {
60 /* we require that key size == block size be a valid choice */
61 if (kl != cipher_descriptor[cipher].block_length) {
62 return CRYPT_INVALID_CIPHER;
65 /* determine if chc_hash has been register_hash'ed already */
66 if ((err = hash_is_valid(idx = find_hash("chc_hash"))) != CRYPT_OK) {
70 /* store into descriptor */
71 hash_descriptor[idx].hashsize =
72 hash_descriptor[idx].blocksize = cipher_descriptor[cipher].block_length;
74 /* store the idx and block size */
76 cipher_blocksize = cipher_descriptor[cipher].block_length;
81 Initialize the hash state
82 @param md The hash state you wish to initialize
83 @return CRYPT_OK if successful
85 int chc_init(hash_state *md)
88 unsigned char buf[MAXBLOCKSIZE];
91 LTC_ARGCHK(md != NULL);
93 /* is the cipher valid? */
94 if ((err = cipher_is_valid(cipher_idx)) != CRYPT_OK) {
98 if (cipher_blocksize != cipher_descriptor[cipher_idx].block_length) {
99 return CRYPT_INVALID_CIPHER;
102 if ((key = XMALLOC(sizeof(*key))) == NULL) {
106 /* zero key and what not */
107 zeromem(buf, cipher_blocksize);
108 if ((err = cipher_descriptor[cipher_idx].setup(buf, cipher_blocksize, 0, key)) != CRYPT_OK) {
113 /* encrypt zero block */
114 cipher_descriptor[cipher_idx].ecb_encrypt(buf, md->chc.state, key);
116 /* zero other members */
119 zeromem(md->chc.buf, sizeof(md->chc.buf));
128 state <= state xor T0 xor T1
130 static int chc_compress(hash_state *md, unsigned char *buf)
132 unsigned char T[2][MAXBLOCKSIZE];
136 if ((key = XMALLOC(sizeof(*key))) == NULL) {
139 if ((err = cipher_descriptor[cipher_idx].setup(md->chc.state, cipher_blocksize, 0, key)) != CRYPT_OK) {
143 XMEMCPY(T[1], buf, cipher_blocksize);
144 cipher_descriptor[cipher_idx].ecb_encrypt(buf, T[0], key);
145 for (x = 0; x < cipher_blocksize; x++) {
146 md->chc.state[x] ^= T[0][x] ^ T[1][x];
148 #ifdef LTC_CLEAN_STACK
149 zeromem(T, sizeof(T));
150 zeromem(key, sizeof(*key));
157 Function for processing blocks
158 @param md The hash state
159 @param buf The data to hash
160 @param len The length of the data (octets)
161 @return CRYPT_OK if successful
163 static int _chc_process(hash_state * md, const unsigned char *buf, unsigned long len);
164 static HASH_PROCESS(_chc_process, chc_compress, chc, (unsigned long)cipher_blocksize)
167 Process a block of memory though the hash
168 @param md The hash state
169 @param in The data to hash
170 @param inlen The length of the data (octets)
171 @return CRYPT_OK if successful
173 int chc_process(hash_state * md, const unsigned char *in, unsigned long inlen)
177 LTC_ARGCHK(md != NULL);
178 LTC_ARGCHK(in != NULL);
180 /* is the cipher valid? */
181 if ((err = cipher_is_valid(cipher_idx)) != CRYPT_OK) {
184 if (cipher_blocksize != cipher_descriptor[cipher_idx].block_length) {
185 return CRYPT_INVALID_CIPHER;
188 return _chc_process(md, in, inlen);
192 Terminate the hash to get the digest
193 @param md The hash state
194 @param out [out] The destination of the hash (length of the block size of the block cipher)
195 @return CRYPT_OK if successful
197 int chc_done(hash_state *md, unsigned char *out)
201 LTC_ARGCHK(md != NULL);
202 LTC_ARGCHK(out != NULL);
204 /* is the cipher valid? */
205 if ((err = cipher_is_valid(cipher_idx)) != CRYPT_OK) {
208 if (cipher_blocksize != cipher_descriptor[cipher_idx].block_length) {
209 return CRYPT_INVALID_CIPHER;
212 if (md->chc.curlen >= sizeof(md->chc.buf)) {
213 return CRYPT_INVALID_ARG;
216 /* increase the length of the message */
217 md->chc.length += md->chc.curlen * 8;
219 /* append the '1' bit */
220 md->chc.buf[md->chc.curlen++] = (unsigned char)0x80;
222 /* if the length is currently above l-8 bytes we append zeros
223 * then compress. Then we can fall back to padding zeros and length
224 * encoding like normal.
226 if (md->chc.curlen > (unsigned long)(cipher_blocksize - 8)) {
227 while (md->chc.curlen < (unsigned long)cipher_blocksize) {
228 md->chc.buf[md->chc.curlen++] = (unsigned char)0;
230 chc_compress(md, md->chc.buf);
234 /* pad upto l-8 bytes of zeroes */
235 while (md->chc.curlen < (unsigned long)(cipher_blocksize - 8)) {
236 md->chc.buf[md->chc.curlen++] = (unsigned char)0;
240 STORE64L(md->chc.length, md->chc.buf+(cipher_blocksize-8));
241 chc_compress(md, md->chc.buf);
244 XMEMCPY(out, md->chc.state, cipher_blocksize);
246 #ifdef LTC_CLEAN_STACK
247 zeromem(md, sizeof(hash_state));
254 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
261 static const struct {
267 (unsigned char *)"hello world",
268 { 0xcf, 0x57, 0x9d, 0xc3, 0x0a, 0x0e, 0xea, 0x61,
269 0x0d, 0x54, 0x47, 0xc4, 0x3c, 0x06, 0xf5, 0x4e },
273 int i, oldhashidx, idx;
274 unsigned char tmp[MAXBLOCKSIZE];
277 /* AES can be under rijndael or aes... try to find it */
278 if ((idx = find_cipher("aes")) == -1) {
279 if ((idx = find_cipher("rijndael")) == -1) {
283 oldhashidx = cipher_idx;
286 for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
288 chc_process(&md, tests[i].msg, strlen((char *)tests[i].msg));
290 if (compare_testvector(tmp, tests[i].len, tests[i].hash, tests[i].len, "CHC", i)) {
291 return CRYPT_FAIL_TESTVECTOR;
294 if (oldhashidx != UNDEFED_HASH) {
295 chc_register(oldhashidx);
304 /* ref: $Format:%D$ */
305 /* git commit: $Format:%H$ */
306 /* commit time: $Format:%ai$ */