comparison libtomcrypt/src/encauth/ocb3/ocb3_add_aad.c @ 1471:6dba84798cd5

Update to libtomcrypt 1.18.1, merged with Dropbear changes
author Matt Johnston <matt@ucc.asn.au>
date Fri, 09 Feb 2018 21:44:05 +0800
parents
children
comparison
equal deleted inserted replaced
1470:8bba51a55704 1471:6dba84798cd5
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
10 /**
11 @file ocb3_add_aad.c
12 OCB implementation, add AAD data, by Karel Miko
13 */
14 #include "tomcrypt.h"
15
16 #ifdef LTC_OCB3_MODE
17
18 /**
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
23 */
24 static int _ocb3_int_aad_add_block(ocb3_state *ocb, const unsigned char *aad_block)
25 {
26 unsigned char tmp[MAXBLOCKSIZE];
27 int err;
28
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);
31
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) {
35 return err;
36 }
37 ocb3_int_xor_blocks(ocb->aSum_current, ocb->aSum_current, tmp, ocb->block_len);
38
39 ocb->ablock_index++;
40
41 return CRYPT_OK;
42 }
43
44 /**
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
50 */
51 int ocb3_add_aad(ocb3_state *ocb, const unsigned char *aad, unsigned long aadlen)
52 {
53 int err, x, full_blocks, full_blocks_len, last_block_len;
54 unsigned char *data;
55 unsigned long datalen, l;
56
57 LTC_ARGCHK(ocb != NULL);
58 if (aadlen == 0) return CRYPT_OK;
59 LTC_ARGCHK(aad != NULL);
60
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;
66
67 if (ocb->adata_buffer_bytes == ocb->block_len) {
68 if ((err = _ocb3_int_aad_add_block(ocb, ocb->adata_buffer)) != CRYPT_OK) {
69 return err;
70 }
71 ocb->adata_buffer_bytes = 0;
72 }
73
74 data = (unsigned char *)aad + l;
75 datalen = aadlen - l;
76 }
77 else {
78 data = (unsigned char *)aad;
79 datalen = aadlen;
80 }
81
82 if (datalen == 0) return CRYPT_OK;
83
84 full_blocks = datalen/ocb->block_len;
85 full_blocks_len = full_blocks * ocb->block_len;
86 last_block_len = datalen - full_blocks_len;
87
88 for (x=0; x<full_blocks; x++) {
89 if ((err = _ocb3_int_aad_add_block(ocb, data+x*ocb->block_len)) != CRYPT_OK) {
90 return err;
91 }
92 }
93
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;
97 }
98
99 return CRYPT_OK;
100 }
101
102 #endif
103
104 /* ref: $Format:%D$ */
105 /* git commit: $Format:%H$ */
106 /* commit time: $Format:%ai$ */