Skip to content

Commit b6cdf9f

Browse files
committed
Correct chacha20poly1305's empty padding case
Add a bunch of tests generated with libsodium.
1 parent 11f68df commit b6cdf9f

File tree

3 files changed

+503
-8
lines changed

3 files changed

+503
-8
lines changed

extra_vecs/libsodium-extra.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import pysodium
2+
3+
def test(msg, aad, nonce, key):
4+
ct = pysodium.crypto_aead_chacha20poly1305_ietf_encrypt(msg, aad, nonce, key)
5+
ct, tag = ct[:len(msg)], ct[len(msg):]
6+
7+
print """
8+
vector("%s",
9+
"%s",
10+
"%s",
11+
"%s",
12+
"%s",
13+
"%s");""" % (key.encode('hex'), nonce.encode('hex'), aad.encode('hex'), msg.encode('hex'), ct.encode('hex'), tag.encode('hex'))
14+
15+
key = 'key.' * 8
16+
nonce = 'nonce.' * 2
17+
msg = 'message' * 5
18+
aad = 'aad' * 12
19+
20+
for msgl in range(32):
21+
test(msg[:msgl], aad, nonce, key)
22+
23+
for aadl in range(32):
24+
test(msg, aad[:aadl], nonce, key)

src/chacha20poly1305.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static int process(const uint8_t key[static 32],
5656
* AAD || pad(AAD) || cipher || pad(cipher) || len_64(aad) || len_64(cipher) */
5757
uint8_t padbuf[16] = { 0 };
5858

59-
#define PADLEN(x) (16 - ((x) & 0xf))
59+
#define PADLEN(x) ((16 - ((x) & 0xf)) & 0xf)
6060

6161
/* AAD || pad(AAD) */
6262
cf_poly1305_update(&poly, header, nheader);

0 commit comments

Comments
 (0)