1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 
4 /**
5    @file ocb_done_decrypt.c
6    OCB implementation, terminate decryption, by Tom St Denis
7 */
8 #include "tomcrypt_private.h"
9 
10 #ifdef LTC_OCB_MODE
11 
12 /**
13    Terminate a decrypting OCB state
14    @param ocb    The OCB state
15    @param ct     The ciphertext (if any)
16    @param ctlen  The length of the ciphertext (octets)
17    @param pt     [out] The plaintext
18    @param tag    The authentication tag (to compare against)
19    @param taglen The length of the authentication tag provided
20    @param stat    [out] The result of the tag comparison
21    @return CRYPT_OK if the process was successful regardless if the tag is valid
22 */
ocb_done_decrypt(ocb_state * ocb,const unsigned char * ct,unsigned long ctlen,unsigned char * pt,const unsigned char * tag,unsigned long taglen,int * stat)23 int ocb_done_decrypt(ocb_state *ocb,
24                      const unsigned char *ct,  unsigned long ctlen,
25                            unsigned char *pt,
26                      const unsigned char *tag, unsigned long taglen, int *stat)
27 {
28    int err;
29    unsigned char *tagbuf;
30    unsigned long tagbuflen;
31 
32    LTC_ARGCHK(ocb  != NULL);
33    LTC_ARGCHK(pt   != NULL);
34    LTC_ARGCHK(ct   != NULL);
35    LTC_ARGCHK(tag  != NULL);
36    LTC_ARGCHK(stat != NULL);
37 
38    /* default to failed */
39    *stat = 0;
40 
41    /* allocate memory */
42    tagbuf = XMALLOC(MAXBLOCKSIZE);
43    if (tagbuf == NULL) {
44       return CRYPT_MEM;
45    }
46 
47    tagbuflen = MAXBLOCKSIZE;
48    if ((err = s_ocb_done(ocb, ct, ctlen, pt, tagbuf, &tagbuflen, 1)) != CRYPT_OK) {
49       goto LBL_ERR;
50    }
51 
52    if (taglen <= tagbuflen && XMEM_NEQ(tagbuf, tag, taglen) == 0) {
53       *stat = 1;
54    }
55 
56    err = CRYPT_OK;
57 LBL_ERR:
58 #ifdef LTC_CLEAN_STACK
59    zeromem(tagbuf, MAXBLOCKSIZE);
60 #endif
61 
62    XFREE(tagbuf);
63 
64    return err;
65 }
66 
67 #endif
68 
69