authorgravatar for luke.champine@gmail.comlukechampine <luke.champine@gmail.com> 2019-11-05 16:15:40-05:00
committergravatar for luke.champine@gmail.comlukechampine <luke.champine@gmail.com> 2019-12-30 13:35:05-05:00
log1953b605998c7b06acffd4aaef50846cacbb64ea
tree61bffe9cb069cdf0e289fbb203246a0ef38dce02
parentae7bb4ecc03d063acc75058f74fcf43b61b5a358
signature Commit is signed but in an unrecognized format.

chacha20poly1305: Return false on short ciphertext


1 files changed, 6 insertions(+), 2 deletions(-)

lib/std/crypto/chacha20.zig+6-2
...@@ -469,11 +469,15 @@ pub fn chacha20poly1305Seal(dst: []u8, plaintext: []const u8, data: []const u8,...@@ -469,11 +469,15 @@ pub fn chacha20poly1305Seal(dst: []u8, plaintext: []const u8, data: []const u8,
469 mac.final(dst[plaintext.len..]);469 mac.final(dst[plaintext.len..]);
470}470}
471471
472/// Verifies and decrypts an authenticated message produced by chacha20poly1305Open.
473/// Returns false if message was invalid or authentication failed.
472pub fn chacha20poly1305Open(dst: []u8, ciphertext: []const u8, data: []const u8, key: [32]u8, nonce: [12]u8) bool {474pub fn chacha20poly1305Open(dst: []u8, ciphertext: []const u8, data: []const u8, key: [32]u8, nonce: [12]u8) bool {
473 assert(ciphertext.len >= chacha20poly1305_tag_size);475 if (ciphertext.len < chacha20poly1305_tag_size) {
474 assert(dst.len >= ciphertext.len - chacha20poly1305_tag_size);476 return false;
477 }
475478
476 // split ciphertext and tag479 // split ciphertext and tag
480 assert(dst.len >= ciphertext.len - chacha20poly1305_tag_size);
477 var polyTag = ciphertext[ciphertext.len - chacha20poly1305_tag_size ..];481 var polyTag = ciphertext[ciphertext.len - chacha20poly1305_tag_size ..];
478 ciphertext = ciphertext[0 .. ciphertext.len - chacha20poly1305_tag_size];482 ciphertext = ciphertext[0 .. ciphertext.len - chacha20poly1305_tag_size];
479483