Skip to content

Commit 9616356

Browse files
committed
review CCM
* improve some comments * harden some arguments * fix the overflow warning fixes #555, fixes #544
1 parent ea7d8eb commit 9616356

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

src/encauth/ccm/ccm_add_nonce.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ int ccm_add_nonce(ccm_state *ccm,
2525
if ((15 - ccm->noncelen) > ccm->L) {
2626
ccm->L = 15 - ccm->noncelen;
2727
}
28+
if (ccm->L > 8) {
29+
return CRYPT_INVALID_ARG;
30+
}
2831

2932
/* decrease noncelen to match L */
3033
if ((ccm->noncelen + ccm->L) > 15) {
@@ -38,7 +41,7 @@ int ccm_add_nonce(ccm_state *ccm,
3841
(ccm->L-1));
3942

4043
/* nonce */
41-
for (y = 0; y < (16 - (ccm->L + 1)); y++) {
44+
for (y = 0; y < 15 - ccm->L; y++) {
4245
ccm->PAD[x++] = nonce[y];
4346
}
4447

src/encauth/ccm/ccm_init.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ int ccm_init(ccm_state *ccm, int cipher,
3535
}
3636

3737
/* make sure the taglen is valid */
38-
if (taglen < 4 || taglen > 16 || (taglen % 2) == 1) {
38+
if (taglen < 4 || taglen > 16 || (taglen % 2) == 1 || aadlen < 0 || ptlen < 0) {
3939
return CRYPT_INVALID_ARG;
4040
}
4141
ccm->taglen = taglen;

src/encauth/ccm/ccm_memory.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ int ccm_memory(int cipher,
7575
}
7676

7777
/* make sure the taglen is valid */
78-
if (*taglen < 4 || *taglen > 16 || (*taglen % 2) == 1) {
78+
if (*taglen < 4 || *taglen > 16 || (*taglen % 2) == 1 || headerlen > 0x7fffffffu) {
7979
return CRYPT_INVALID_ARG;
8080
}
8181

@@ -108,6 +108,9 @@ int ccm_memory(int cipher,
108108
if ((15 - noncelen) > L) {
109109
L = 15 - noncelen;
110110
}
111+
if (L > 8) {
112+
return CRYPT_INVALID_ARG;
113+
}
111114

112115
/* allocate mem for the symmetric key */
113116
if (uskey == NULL) {
@@ -141,7 +144,7 @@ int ccm_memory(int cipher,
141144
(L-1));
142145

143146
/* nonce */
144-
for (y = 0; y < (16 - (L + 1)); y++) {
147+
for (y = 0; y < 15 - L; y++) {
145148
PAD[x++] = nonce[y];
146149
}
147150

src/headers/tomcrypt_mac.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ int ocb3_test(void);
395395
typedef struct {
396396
symmetric_key K;
397397
int cipher, /* which cipher */
398-
taglen, /* length of the tag */
398+
taglen, /* length of the tag (encoded in M value) */
399399
x; /* index in PAD */
400400

401401
unsigned long L, /* L value */
@@ -405,7 +405,7 @@ typedef struct {
405405
current_aadlen, /* length of the currently provided add */
406406
noncelen; /* length of the nonce */
407407

408-
unsigned char PAD[16],
408+
unsigned char PAD[16], /* flags | Nonce N | l(m) */
409409
ctr[16],
410410
CTRPAD[16],
411411
CTRlen;

0 commit comments

Comments
 (0)