authorgravatar for luke.champine@gmail.comlukechampine <luke.champine@gmail.com> 2019-11-06 20:33:45-05:00
committergravatar for luke.champine@gmail.comlukechampine <luke.champine@gmail.com> 2019-12-30 13:35:13-05:00
logd6ca2323cf7f544836b1ab2d318d5286c4ca0e62
tree28d6478dd75e6ed906b854412e7d4bb7f964fd67
parent1953b605998c7b06acffd4aaef50846cacbb64ea
signature Commit is signed but in an unrecognized format.

chacha: Use error set instead of bool


1 files changed, 12 insertions(+), 13 deletions(-)

lib/std/crypto/chacha20.zig+12-13
......@@ -470,10 +470,9 @@ pub fn chacha20poly1305Seal(dst: []u8, plaintext: []const u8, data: []const u8,
470470}
471471
472472/// Verifies and decrypts an authenticated message produced by chacha20poly1305Open.
473/// Returns false if message was invalid or authentication failed.
474pub fn chacha20poly1305Open(dst: []u8, ciphertext: []const u8, data: []const u8, key: [32]u8, nonce: [12]u8) bool {
473pub fn chacha20poly1305Open(dst: []u8, ciphertext: []const u8, data: []const u8, key: [32]u8, nonce: [12]u8) !void {
475474 if (ciphertext.len < chacha20poly1305_tag_size) {
476 return false;
475 return error.InvalidMessage;
477476 }
478477
479478 // split ciphertext and tag
......@@ -515,12 +514,11 @@ pub fn chacha20poly1305Open(dst: []u8, ciphertext: []const u8, data: []const u8,
515514 acc |= (computedTag[i] ^ polyTag[i]);
516515 }
517516 if (acc != 0) {
518 return false;
517 return error.AuthenticationFailed;
519518 }
520519
521520 // decrypt ciphertext
522521 chaCha20IETF(dst[0..ciphertext.len], ciphertext, 1, key, nonce);
523 return true;
524522}
525523
526524test "seal" {
......@@ -585,8 +583,7 @@ test "open" {
585583 const exp_out = "";
586584
587585 var out: [exp_out.len]u8 = undefined;
588 var valid: bool = chacha20poly1305Open(out[0..], ciphertext[0..], data, key, nonce);
589 testing.expect(valid);
586 try chacha20poly1305Open(out[0..], ciphertext[0..], data, key, nonce);
590587 testing.expectEqualSlices(u8, exp_out, out);
591588 }
592589 {
......@@ -619,22 +616,24 @@ test "open" {
619616 };
620617
621618 var out: [exp_out.len]u8 = undefined;
622 var valid: bool = chacha20poly1305Open(out[0..], ciphertext[0..], data[0..], key, nonce);
623 testing.expect(valid);
619 try chacha20poly1305Open(out[0..], ciphertext[0..], data[0..], key, nonce);
624620 testing.expectEqualSlices(u8, exp_out, out);
625621
626622 // corrupting the ciphertext, data, key, or nonce should cause a failure
627623 var bad_ciphertext = ciphertext;
628624 bad_ciphertext[0] ^= 1;
629 testing.expect(!chacha20poly1305Open(out[0..], bad_ciphertext[0..], data, key, nonce));
625 testing.expectError(error.AuthenticationFailed, chacha20poly1305Open(out[0..], bad_ciphertext[0..], data, key, nonce));
630626 var bad_data = data;
631627 bad_data[0] ^= 1;
632 testing.expect(!chacha20poly1305Open(out[0..], ciphertext[0..], bad_data, key, nonce));
628 testing.expectError(error.AuthenticationFailed, chacha20poly1305Open(out[0..], ciphertext[0..], bad_data, key, nonce));
633629 var bad_key = key;
634630 bad_key[0] ^= 1;
635 testing.expect(!chacha20poly1305Open(out[0..], ciphertext[0..], data, bad_key, nonce));
631 testing.expectError(error.AuthenticationFailed, chacha20poly1305Open(out[0..], ciphertext[0..], data, bad_key, nonce));
636632 var bad_nonce = nonce;
637633 bad_nonce[0] ^= 1;
638 testing.expect(!chacha20poly1305Open(out[0..], ciphertext[0..], data, key, bad_nonce));
634 testing.expectError(error.AuthenticationFailed, chacha20poly1305Open(out[0..], ciphertext[0..], data, key, bad_nonce));
635
636 // a short ciphertext should result in a different error
637 testing.expectError(error.InvalidMessage, chacha20poly1305Open(out[0..], "", data, key, bad_nonce));
639638 }
640639}