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
12 OCB implementation, add AAD data, by Karel Miko
19 Add one block of AAD data (internal function)
20 @param ocb The OCB state
21 @param aad_block [in] AAD data (block_len size)
22 @return CRYPT_OK if successful
24 static int _ocb3_int_aad_add_block(ocb3_state *ocb, const unsigned char *aad_block)
26 unsigned char tmp[MAXBLOCKSIZE];
29 /* Offset_i = Offset_{i-1} xor L_{ntz(i)} */
30 ocb3_int_xor_blocks(ocb->aOffset_current, ocb->aOffset_current, ocb->L_[ocb3_int_ntz(ocb->ablock_index)], ocb->block_len);
32 /* Sum_i = Sum_{i-1} xor ENCIPHER(K, A_i xor Offset_i) */
33 ocb3_int_xor_blocks(tmp, aad_block, ocb->aOffset_current, ocb->block_len);
34 if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(tmp, tmp, &ocb->key)) != CRYPT_OK) {
37 ocb3_int_xor_blocks(ocb->aSum_current, ocb->aSum_current, tmp, ocb->block_len);
45 Add AAD - additional associated data
46 @param ocb The OCB state
47 @param aad The AAD data
48 @param aadlen The size of AAD data (octets)
49 @return CRYPT_OK if successful
51 int ocb3_add_aad(ocb3_state *ocb, const unsigned char *aad, unsigned long aadlen)
53 int err, x, full_blocks, full_blocks_len, last_block_len;
55 unsigned long datalen, l;
57 LTC_ARGCHK(ocb != NULL);
58 if (aadlen == 0) return CRYPT_OK;
59 LTC_ARGCHK(aad != NULL);
61 if (ocb->adata_buffer_bytes > 0) {
62 l = ocb->block_len - ocb->adata_buffer_bytes;
63 if (l > aadlen) l = aadlen;
64 XMEMCPY(ocb->adata_buffer+ocb->adata_buffer_bytes, aad, l);
65 ocb->adata_buffer_bytes += l;
67 if (ocb->adata_buffer_bytes == ocb->block_len) {
68 if ((err = _ocb3_int_aad_add_block(ocb, ocb->adata_buffer)) != CRYPT_OK) {
71 ocb->adata_buffer_bytes = 0;
74 data = (unsigned char *)aad + l;
78 data = (unsigned char *)aad;
82 if (datalen == 0) return CRYPT_OK;
84 full_blocks = datalen/ocb->block_len;
85 full_blocks_len = full_blocks * ocb->block_len;
86 last_block_len = datalen - full_blocks_len;
88 for (x=0; x<full_blocks; x++) {
89 if ((err = _ocb3_int_aad_add_block(ocb, data+x*ocb->block_len)) != CRYPT_OK) {
94 if (last_block_len>0) {
95 XMEMCPY(ocb->adata_buffer, data+full_blocks_len, last_block_len);
96 ocb->adata_buffer_bytes = last_block_len;
104 /* ref: $Format:%D$ */
105 /* git commit: $Format:%H$ */
106 /* commit time: $Format:%ai$ */