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