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_encrypt_authenticate_memory.c 14 OCB implementation, encrypt block of memory, by Tom St Denis 15 */ 16 #include "tomcrypt.h" 17 18 #ifdef OCB_MODE 19 20 /** 21 Encrypt and generate an authentication code for a buffer of memory 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 ciphers block size) 26 @param pt The plaintext 27 @param ptlen The length of the plaintext (octets) 28 @param ct [out] The ciphertext 29 @param tag [out] The authentication tag 30 @param taglen [in/out] The max size and resulting size of the authentication tag 31 @return CRYPT_OK if successful 32 */ 33 int ocb_encrypt_authenticate_memory(int cipher, 34 const unsigned char *key, unsigned long keylen, 35 const unsigned char *nonce, 36 const unsigned char *pt, unsigned long ptlen, 37 unsigned char *ct, 38 unsigned char *tag, unsigned long *taglen) 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(taglen != NULL); 49 50 /* allocate ram */ 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 (ptlen > (unsigned long)ocb->block_len) { 61 if ((err = ocb_encrypt(ocb, pt, ct)) != CRYPT_OK) { 62 goto LBL_ERR; 63 } 64 ptlen -= ocb->block_len; 65 pt += ocb->block_len; 66 ct += ocb->block_len; 67 } 68 69 err = ocb_done_encrypt(ocb, pt, ptlen, ct, tag, taglen); 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 /* $Source: /cvs/libtom/libtomcrypt/src/encauth/ocb/ocb_encrypt_authenticate_memory.c,v $ */ 83 /* $Revision: 1.4 $ */ 84 /* $Date: 2006/03/31 14:15:35 $ */ 85