]> pd.if.org Git - zpackage/blob - libtomcrypt/src/encauth/ocb/ocb_decrypt.c
remove ltc ciphers
[zpackage] / libtomcrypt / src / encauth / ocb / ocb_decrypt.c
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
10 /**
11    @file ocb_decrypt.c
12    OCB implementation, decrypt data, by Tom St Denis
13 */
14 #include "tomcrypt.h"
15
16 #ifdef LTC_OCB_MODE
17
18 /**
19   Decrypt a block with OCB.
20   @param ocb    The OCB state
21   @param ct     The ciphertext (length of the block size of the block cipher)
22   @param pt     [out] The plaintext (length of ct)
23   @return CRYPT_OK if successful
24 */
25 int ocb_decrypt(ocb_state *ocb, const unsigned char *ct, unsigned char *pt)
26 {
27    unsigned char Z[MAXBLOCKSIZE], tmp[MAXBLOCKSIZE];
28    int err, x;
29
30    LTC_ARGCHK(ocb != NULL);
31    LTC_ARGCHK(pt  != NULL);
32    LTC_ARGCHK(ct  != NULL);
33
34    /* check if valid cipher */
35    if ((err = cipher_is_valid(ocb->cipher)) != CRYPT_OK) {
36       return err;
37    }
38    LTC_ARGCHK(cipher_descriptor[ocb->cipher].ecb_decrypt != NULL);
39
40    /* check length */
41    if (ocb->block_len != cipher_descriptor[ocb->cipher].block_length) {
42       return CRYPT_INVALID_ARG;
43    }
44
45    /* Get Z[i] value */
46    ocb_shift_xor(ocb, Z);
47
48    /* xor ct in, encrypt, xor Z out */
49    for (x = 0; x < ocb->block_len; x++) {
50        tmp[x] = ct[x] ^ Z[x];
51    }
52    if ((err = cipher_descriptor[ocb->cipher].ecb_decrypt(tmp, pt, &ocb->key)) != CRYPT_OK) {
53       return err;
54    }
55    for (x = 0; x < ocb->block_len; x++) {
56        pt[x] ^= Z[x];
57    }
58
59    /* compute checksum */
60    for (x = 0; x < ocb->block_len; x++) {
61        ocb->checksum[x] ^= pt[x];
62    }
63
64
65 #ifdef LTC_CLEAN_STACK
66    zeromem(Z, sizeof(Z));
67    zeromem(tmp, sizeof(tmp));
68 #endif
69    return CRYPT_OK;
70 }
71
72 #endif
73
74
75 /* ref:         $Format:%D$ */
76 /* git commit:  $Format:%H$ */
77 /* commit time: $Format:%ai$ */