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(ocb_state *ocb, const unsigned char *ct, unsigned char *pt) |
|
18 { |
|
19 unsigned char Z[MAXBLOCKSIZE], tmp[MAXBLOCKSIZE]; |
|
20 int err, x; |
|
21 |
|
22 _ARGCHK(ocb != NULL); |
|
23 _ARGCHK(pt != NULL); |
|
24 _ARGCHK(ct != NULL); |
15
|
25 |
|
26 /* check if valid cipher */ |
3
|
27 if ((err = cipher_is_valid(ocb->cipher)) != CRYPT_OK) { |
|
28 return err; |
|
29 } |
15
|
30 _ARGCHK(cipher_descriptor[ocb->cipher].ecb_decrypt != NULL); |
|
31 |
|
32 /* check length */ |
3
|
33 if (ocb->block_len != cipher_descriptor[ocb->cipher].block_length) { |
|
34 return CRYPT_INVALID_ARG; |
|
35 } |
|
36 |
|
37 /* Get Z[i] value */ |
|
38 ocb_shift_xor(ocb, Z); |
|
39 |
|
40 /* xor ct in, encrypt, xor Z out */ |
|
41 for (x = 0; x < ocb->block_len; x++) { |
|
42 tmp[x] = ct[x] ^ Z[x]; |
|
43 } |
|
44 cipher_descriptor[ocb->cipher].ecb_decrypt(tmp, pt, &ocb->key); |
|
45 for (x = 0; x < ocb->block_len; x++) { |
|
46 pt[x] ^= Z[x]; |
|
47 } |
|
48 |
|
49 /* compute checksum */ |
|
50 for (x = 0; x < ocb->block_len; x++) { |
|
51 ocb->checksum[x] ^= pt[x]; |
|
52 } |
|
53 |
|
54 |
|
55 #ifdef CLEAN_STACK |
|
56 zeromem(Z, sizeof(Z)); |
|
57 zeromem(tmp, sizeof(tmp)); |
|
58 #endif |
|
59 return CRYPT_OK; |
|
60 } |
|
61 |
|
62 #endif |
|
63 |