]> pd.if.org Git - zpackage/blob - libtomcrypt/src/encauth/ocb/ocb_decrypt_verify_memory.c
a7a47f06bb70456509036c27390bb721ee78e752
[zpackage] / libtomcrypt / src / encauth / ocb / ocb_decrypt_verify_memory.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_verify_memory.c
12   OCB implementation, helper to decrypt block of memory, by Tom St Denis
13 */
14 #include "tomcrypt.h"
15
16 #ifdef LTC_OCB_MODE
17
18 /**
19    Decrypt and compare the tag with OCB.
20    @param cipher     The index of the cipher desired
21    @param key        The secret key
22    @param keylen     The length of the secret key (octets)
23    @param nonce      The session nonce (length of the block size of the block cipher)
24    @param ct         The ciphertext
25    @param ctlen      The length of the ciphertext (octets)
26    @param pt         [out] The plaintext
27    @param tag        The tag to compare against
28    @param taglen     The length of the tag (octets)
29    @param stat       [out] The result of the tag comparison (1==valid, 0==invalid)
30    @return CRYPT_OK if successful regardless of the tag comparison
31 */
32 int ocb_decrypt_verify_memory(int cipher,
33     const unsigned char *key,    unsigned long keylen,
34     const unsigned char *nonce,
35     const unsigned char *ct,     unsigned long ctlen,
36           unsigned char *pt,
37     const unsigned char *tag,    unsigned long taglen,
38           int           *stat)
39 {
40    int err;
41    ocb_state *ocb;
42
43    LTC_ARGCHK(key    != NULL);
44    LTC_ARGCHK(nonce  != NULL);
45    LTC_ARGCHK(pt     != NULL);
46    LTC_ARGCHK(ct     != NULL);
47    LTC_ARGCHK(tag    != NULL);
48    LTC_ARGCHK(stat    != NULL);
49
50    /* allocate memory */
51    ocb = XMALLOC(sizeof(ocb_state));
52    if (ocb == NULL) {
53       return CRYPT_MEM;
54    }
55
56    if ((err = ocb_init(ocb, cipher, key, keylen, nonce)) != CRYPT_OK) {
57       goto LBL_ERR;
58    }
59
60    while (ctlen > (unsigned long)ocb->block_len) {
61         if ((err = ocb_decrypt(ocb, ct, pt)) != CRYPT_OK) {
62             goto LBL_ERR;
63         }
64         ctlen   -= ocb->block_len;
65         pt      += ocb->block_len;
66         ct      += ocb->block_len;
67    }
68
69    err = ocb_done_decrypt(ocb, ct, ctlen, pt, tag, taglen, stat);
70 LBL_ERR:
71 #ifdef LTC_CLEAN_STACK
72    zeromem(ocb, sizeof(ocb_state));
73 #endif
74
75    XFREE(ocb);
76
77    return err;
78 }
79
80 #endif
81
82 /* ref:         $Format:%D$ */
83 /* git commit:  $Format:%H$ */
84 /* commit time: $Format:%ai$ */