diff options
| author | Frank Denis <github@pureftpd.org> | 2020-10-05 23:50:38 +0200 |
|---|---|---|
| committer | Frank Denis <github@pureftpd.org> | 2020-10-05 23:50:38 +0200 |
| commit | d343b75e7fa11d94e9668fb306b9e6b2ba68a0da (patch) | |
| tree | 01171c58d9aec7ec9e7e314f8bef4a1b630fe734 /lib/std/crypto/poly1305.zig | |
| parent | 7f7e2d608adb81cd00e54fd7fe5e7035a890565f (diff) | |
| download | zig-d343b75e7fa11d94e9668fb306b9e6b2ba68a0da.tar.gz zig-d343b75e7fa11d94e9668fb306b9e6b2ba68a0da.zip | |
ghash & poly1305: fix handling of partial blocks and add pad()
pad() aligns the next input to the first byte of a block, which is
useful to implement the IETF version of ChaCha20Poly1305 and AES-GCM.
Diffstat (limited to 'lib/std/crypto/poly1305.zig')
| -rw-r--r-- | lib/std/crypto/poly1305.zig | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/lib/std/crypto/poly1305.zig b/lib/std/crypto/poly1305.zig index 31d1d6ba5a..c6613f64ba 100644 --- a/lib/std/crypto/poly1305.zig +++ b/lib/std/crypto/poly1305.zig @@ -91,7 +91,7 @@ pub const Poly1305 = struct { } mb = mb[want..]; st.leftover += want; - if (st.leftover > block_size) { + if (st.leftover < block_size) { return; } st.blocks(&st.buf, false); @@ -114,6 +114,19 @@ pub const Poly1305 = struct { } } + /// Zero-pad to align the next input to the first byte of a block + pub fn pad(st: *Poly1305) void { + if (st.leftover == 0) { + return; + } + var i = st.leftover; + while (i < block_size) : (i += 1) { + st.buf[i] = 0; + } + st.blocks(&st.buf); + st.leftover = 0; + } + pub fn final(st: *Poly1305, out: *[mac_length]u8) void { if (st.leftover > 0) { var i = st.leftover; |
