1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
11 @file ocb3_decrypt_verify_memory.c
12 OCB implementation, helper to decrypt block of memory, by Tom St Denis
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 noncelen The length of the nonce (octets)
25 @param adata The AAD - additional associated data
26 @param adatalen The length of AAD (octets)
27 @param ct The ciphertext
28 @param ctlen The length of the ciphertext (octets)
29 @param pt [out] The plaintext
30 @param tag The tag to compare against
31 @param taglen The length of the tag (octets)
32 @param stat [out] The result of the tag comparison (1==valid, 0==invalid)
33 @return CRYPT_OK if successful regardless of the tag comparison
35 int ocb3_decrypt_verify_memory(int cipher,
36 const unsigned char *key, unsigned long keylen,
37 const unsigned char *nonce, unsigned long noncelen,
38 const unsigned char *adata, unsigned long adatalen,
39 const unsigned char *ct, unsigned long ctlen,
41 const unsigned char *tag, unsigned long taglen,
49 LTC_ARGCHK(stat != NULL);
55 taglen = MIN(taglen, MAXBLOCKSIZE);
58 buf = XMALLOC(taglen);
59 ocb = XMALLOC(sizeof(ocb3_state));
60 if (ocb == NULL || buf == NULL) {
70 if ((err = ocb3_init(ocb, cipher, key, keylen, nonce, noncelen, taglen)) != CRYPT_OK) {
74 if (adata != NULL || adatalen != 0) {
75 if ((err = ocb3_add_aad(ocb, adata, adatalen)) != CRYPT_OK) {
80 if ((err = ocb3_decrypt_last(ocb, ct, ctlen, pt)) != CRYPT_OK) {
85 if ((err = ocb3_done(ocb, buf, &buflen)) != CRYPT_OK) {
90 if (buflen >= taglen && XMEM_NEQ(buf, tag, taglen) == 0) {
97 #ifdef LTC_CLEAN_STACK
98 zeromem(ocb, sizeof(ocb3_state));
108 /* ref: $Format:%D$ */
109 /* git commit: $Format:%H$ */
110 /* commit time: $Format:%ai$ */