Skip to content

Commit af63d0a

Browse files
karel-msjaeckel
authored andcommitted
OCBv3: improved handling of taglen in ocb3_done
1 parent 4113090 commit af63d0a

4 files changed

Lines changed: 33 additions & 6 deletions

File tree

src/encauth/ocb3/ocb3_done.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ int ocb3_done(ocb3_state *ocb, unsigned char *tag, unsigned long *taglen)
3434
goto LBL_ERR;
3535
}
3636

37+
/* check taglen */
38+
if ((int)*taglen < ocb->tag_len) {
39+
*taglen = (unsigned long)ocb->tag_len;
40+
return CRYPT_BUFFER_OVERFLOW;
41+
}
42+
3743
/* finalize AAD processing */
3844

3945
if (ocb->adata_buffer_bytes>0) {
@@ -64,13 +70,9 @@ int ocb3_done(ocb3_state *ocb, unsigned char *tag, unsigned long *taglen)
6470
/* tag = tag ^ HASH(K, A) */
6571
ocb3_int_xor_blocks(tmp, ocb->tag_part, ocb->aSum_current, ocb->block_len);
6672

67-
/* fix taglen if needed */
68-
if ((int)*taglen > ocb->block_len) {
69-
*taglen = (unsigned long)ocb->block_len;
70-
}
71-
7273
/* copy tag bytes */
73-
for(x=0; x<(int)*taglen; x++) tag[x] = tmp[x];
74+
for(x = 0; x < ocb->tag_len; x++) tag[x] = tmp[x];
75+
*taglen = (unsigned long)ocb->tag_len;
7476

7577
err = CRYPT_OK;
7678

src/encauth/ocb3/ocb3_init.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ int ocb3_init(ocb3_state *ocb, int cipher,
118118
if (taglen > (unsigned long)cipher_descriptor[cipher].block_length) {
119119
taglen = cipher_descriptor[cipher].block_length;
120120
}
121+
ocb->tag_len = taglen;
121122

122123
/* determine which polys to use */
123124
ocb->block_len = cipher_descriptor[cipher].block_length;

src/encauth/ocb3/ocb3_test.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ int ocb3_test(void)
205205
int err, x, idx, res;
206206
unsigned long len;
207207
unsigned char outct[MAXBLOCKSIZE], outtag[MAXBLOCKSIZE];
208+
ocb3_state ocb;
208209

209210
/* AES can be under rijndael or aes... try to find it */
210211
if ((idx = find_cipher("aes")) == -1) {
@@ -244,6 +245,8 @@ int ocb3_test(void)
244245
return CRYPT_FAIL_TESTVECTOR;
245246
}
246247
}
248+
249+
/* RFC 7253 - test vector with a tag length of 96 bits - part 1 */
247250
x = 99;
248251
len = 12;
249252
if ((err = ocb3_encrypt_authenticate_memory(idx,
@@ -274,6 +277,26 @@ int ocb3_test(void)
274277
#endif
275278
return CRYPT_FAIL_TESTVECTOR;
276279
}
280+
281+
/* RFC 7253 - test vector with a tag length of 96 bits - part 2 */
282+
x = 100;
283+
if ((err = ocb3_init(&ocb, idx, K, sizeof(K), N, sizeof(N), 12)) != CRYPT_OK) return err;
284+
if ((err = ocb3_add_aad(&ocb, A, sizeof(A))) != CRYPT_OK) return err;
285+
if ((err = ocb3_encrypt(&ocb, P, 32, outct)) != CRYPT_OK) return err;
286+
if ((err = ocb3_encrypt_last(&ocb, P+32, sizeof(P)-32, outct+32)) != CRYPT_OK) return err;
287+
len = sizeof(outtag); /* intentionally more than 12 */
288+
if ((err = ocb3_done(&ocb, outtag, &len)) != CRYPT_OK) return err;
289+
if (compare_testvector(outct, sizeof(P), C, sizeof(C), "OCB3 CT", x)) return CRYPT_FAIL_TESTVECTOR;
290+
if (compare_testvector(outtag, len, T, sizeof(T), "OCB3 Tag.enc", x)) return CRYPT_FAIL_TESTVECTOR;
291+
if ((err = ocb3_init(&ocb, idx, K, sizeof(K), N, sizeof(N), 12)) != CRYPT_OK) return err;
292+
if ((err = ocb3_add_aad(&ocb, A, sizeof(A))) != CRYPT_OK) return err;
293+
if ((err = ocb3_decrypt(&ocb, C, 32, outct)) != CRYPT_OK) return err;
294+
if ((err = ocb3_decrypt_last(&ocb, C+32, sizeof(C)-32, outct+32)) != CRYPT_OK) return err;
295+
len = sizeof(outtag); /* intentionally more than 12 */
296+
if ((err = ocb3_done(&ocb, outtag, &len)) != CRYPT_OK) return err;
297+
if (compare_testvector(outct, sizeof(C), P, sizeof(P), "OCB3 PT", x)) return CRYPT_FAIL_TESTVECTOR;
298+
if (compare_testvector(outtag, len, T, sizeof(T), "OCB3 Tag.dec", x)) return CRYPT_FAIL_TESTVECTOR;
299+
277300
return CRYPT_OK;
278301
#endif /* LTC_TEST */
279302
}

src/headers/tomcrypt_mac.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ typedef struct {
266266
symmetric_key key; /* scheduled key for cipher */
267267
unsigned long block_index; /* index # for current data block */
268268
int cipher, /* cipher idx */
269+
tag_len, /* length of tag */
269270
block_len; /* length of block */
270271
} ocb3_state;
271272

0 commit comments

Comments
 (0)