Skip to content

Commit b2448c5

Browse files
committed
ocb3: properly handle empty AAD
* allow passing "no additional data" to ocb3_decrypt_verify_memory() and ocb3_encrypt_authenticate_memory() * ensure that the caller didn't want to add AAD
1 parent 4805c89 commit b2448c5

4 files changed

Lines changed: 14 additions & 16 deletions

File tree

src/encauth/ocb3/ocb3_add_aad.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ int ocb3_add_aad(ocb3_state *ocb, const unsigned char *aad, unsigned long aadlen
2929
unsigned long datalen, l;
3030

3131
LTC_ARGCHK(ocb != NULL);
32-
LTC_ARGCHK(aad != NULL);
32+
if (aad == NULL) LTC_ARGCHK(aadlen == 0);
33+
if (aadlen == 0) LTC_ARGCHK(aad == NULL);
3334

34-
if (aadlen == 0) return CRYPT_OK;
35+
if (aad == NULL || aadlen == 0) return CRYPT_OK;
3536

3637
if (ocb->adata_buffer_bytes > 0) {
3738
l = ocb->block_len - ocb->adata_buffer_bytes;

src/encauth/ocb3/ocb3_decrypt_verify_memory.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,10 @@ int ocb3_decrypt_verify_memory(int cipher,
7373
goto LBL_ERR;
7474
}
7575

76-
if ((err = ocb3_add_aad(ocb, adata, adatalen)) != CRYPT_OK) {
77-
goto LBL_ERR;
76+
if (adata != NULL || adatalen != 0) {
77+
if ((err = ocb3_add_aad(ocb, adata, adatalen)) != CRYPT_OK) {
78+
goto LBL_ERR;
79+
}
7880
}
7981

8082
if ((err = ocb3_decrypt_last(ocb, ct, ctlen, pt)) != CRYPT_OK) {

src/encauth/ocb3/ocb3_encrypt_authenticate_memory.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ int ocb3_encrypt_authenticate_memory(int cipher,
5959
goto LBL_ERR;
6060
}
6161

62-
if ((err = ocb3_add_aad(ocb, adata, adatalen)) != CRYPT_OK) {
63-
goto LBL_ERR;
62+
if (adata != NULL || adatalen != 0) {
63+
if ((err = ocb3_add_aad(ocb, adata, adatalen)) != CRYPT_OK) {
64+
goto LBL_ERR;
65+
}
6466
}
6567

6668
if ((err = ocb3_encrypt_last(ocb, pt, ptlen, ct)) != CRYPT_OK) {

src/encauth/ocb3/ocb3_test.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ int ocb3_test(void)
180180
if ((err = ocb3_encrypt_authenticate_memory(idx,
181181
key, sizeof(key),
182182
nonce, sizeof(nonce),
183-
tests[x].aad, tests[x].aadlen,
183+
tests[x].aadlen != 0 ? tests[x].aad : NULL, tests[x].aadlen,
184184
tests[x].pt, tests[x].ptlen,
185185
outct, outtag, &len)) != CRYPT_OK) {
186186
return err;
@@ -194,9 +194,9 @@ int ocb3_test(void)
194194
if ((err = ocb3_decrypt_verify_memory(idx,
195195
key, sizeof(key),
196196
nonce, sizeof(nonce),
197-
tests[x].aad, tests[x].aadlen,
197+
tests[x].aadlen != 0 ? tests[x].aad : NULL, tests[x].aadlen,
198198
outct, tests[x].ptlen,
199-
outct, tests[x].tag, len, &res)) != CRYPT_OK) {
199+
outct, tests[x].tag, len, &res)) != CRYPT_OK) {
200200
return err;
201201
}
202202
if ((res != 1) || compare_testvector(outct, tests[x].ptlen, tests[x].pt, tests[x].ptlen, "OCB3", x)) {
@@ -212,13 +212,6 @@ int ocb3_test(void)
212212

213213
#endif /* LTC_OCB3_MODE */
214214

215-
/* some comments
216-
217-
-- it's hard to seek
218-
-- hard to stream [you can't emit ciphertext until full block]
219-
-- The setup is somewhat complicated...
220-
*/
221-
222215
/* ref: $Format:%D$ */
223216
/* git commit: $Format:%H$ */
224217
/* commit time: $Format:%ai$ */

0 commit comments

Comments
 (0)