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_encrypt_authenticate_memory(int cipher, |
|
18 const unsigned char *key, unsigned long keylen, |
|
19 const unsigned char *nonce, |
|
20 const unsigned char *pt, unsigned long ptlen, |
|
21 unsigned char *ct, |
|
22 unsigned char *tag, unsigned long *taglen) |
|
23 { |
|
24 int err; |
143
|
25 ocb_state *ocb; |
3
|
26 |
|
27 _ARGCHK(key != NULL); |
|
28 _ARGCHK(nonce != NULL); |
|
29 _ARGCHK(pt != NULL); |
|
30 _ARGCHK(ct != NULL); |
|
31 _ARGCHK(tag != NULL); |
|
32 _ARGCHK(taglen != NULL); |
|
33 |
143
|
34 /* allocate ram */ |
|
35 ocb = XMALLOC(sizeof(ocb_state)); |
|
36 if (ocb == NULL) { |
|
37 return CRYPT_MEM; |
|
38 } |
|
39 |
|
40 if ((err = ocb_init(ocb, cipher, key, keylen, nonce)) != CRYPT_OK) { |
|
41 goto __ERR; |
3
|
42 } |
|
43 |
143
|
44 while (ptlen > (unsigned long)ocb->block_len) { |
|
45 if ((err = ocb_encrypt(ocb, pt, ct)) != CRYPT_OK) { |
|
46 goto __ERR; |
3
|
47 } |
143
|
48 ptlen -= ocb->block_len; |
|
49 pt += ocb->block_len; |
|
50 ct += ocb->block_len; |
3
|
51 } |
|
52 |
143
|
53 err = ocb_done_encrypt(ocb, pt, ptlen, ct, tag, taglen); |
|
54 __ERR: |
|
55 #ifdef CLEAN_STACK |
|
56 zeromem(ocb, sizeof(ocb_state)); |
|
57 #endif |
|
58 |
|
59 XFREE(ocb); |
|
60 |
|
61 return err; |
3
|
62 } |
|
63 |
|
64 #endif |