3
|
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis |
|
2 * |
|
3 * LibTomCrypt is a library that provides various cryptographic |
|
4 * algorithms in a highly modular and flexible manner. |
|
5 * |
|
6 * The library is free for all purposes without any express |
|
7 * guarantee it works. |
|
8 * |
|
9 * Tom St Denis, [email protected], http://libtomcrypt.org |
|
10 */ |
|
11 |
|
12 /* OCB Implementation by Tom St Denis */ |
|
13 #include "mycrypt.h" |
|
14 |
|
15 #ifdef OCB_MODE |
|
16 |
|
17 int ocb_decrypt_verify_memory(int cipher, |
|
18 const unsigned char *key, unsigned long keylen, |
|
19 const unsigned char *nonce, |
|
20 const unsigned char *ct, unsigned long ctlen, |
|
21 unsigned char *pt, |
|
22 const unsigned char *tag, unsigned long taglen, |
|
23 int *res) |
|
24 { |
|
25 int err; |
143
|
26 ocb_state *ocb; |
3
|
27 |
|
28 _ARGCHK(key != NULL); |
|
29 _ARGCHK(nonce != NULL); |
|
30 _ARGCHK(pt != NULL); |
|
31 _ARGCHK(ct != NULL); |
|
32 _ARGCHK(tag != NULL); |
|
33 _ARGCHK(res != NULL); |
|
34 |
143
|
35 /* allocate memory */ |
|
36 ocb = XMALLOC(sizeof(ocb_state)); |
|
37 if (ocb == NULL) { |
|
38 return CRYPT_MEM; |
|
39 } |
|
40 |
|
41 if ((err = ocb_init(ocb, cipher, key, keylen, nonce)) != CRYPT_OK) { |
|
42 goto __ERR; |
3
|
43 } |
|
44 |
143
|
45 while (ctlen > (unsigned long)ocb->block_len) { |
|
46 if ((err = ocb_decrypt(ocb, ct, pt)) != CRYPT_OK) { |
|
47 goto __ERR; |
3
|
48 } |
143
|
49 ctlen -= ocb->block_len; |
|
50 pt += ocb->block_len; |
|
51 ct += ocb->block_len; |
3
|
52 } |
|
53 |
143
|
54 err = ocb_done_decrypt(ocb, ct, ctlen, pt, tag, taglen, res); |
|
55 __ERR: |
|
56 #ifdef CLEAN_STACK |
|
57 zeromem(ocb, sizeof(ocb_state)); |
|
58 #endif |
|
59 |
|
60 XFREE(ocb); |
|
61 |
|
62 return err; |
3
|
63 } |
|
64 |
|
65 #endif |