| ... | @@ -470,15 +470,15 @@ pub fn chacha20poly1305Seal(dst: []u8, plaintext: []const u8, data: []const u8, | ... | @@ -470,15 +470,15 @@ pub fn chacha20poly1305Seal(dst: []u8, plaintext: []const u8, data: []const u8, |
| 470 | } | 470 | } |
| 471 | | 471 | |
| 472 | /// Verifies and decrypts an authenticated message produced by chacha20poly1305Seal. | 472 | /// Verifies and decrypts an authenticated message produced by chacha20poly1305Seal. |
| 473 | pub fn chacha20poly1305Open(dst: []u8, ciphertext: []const u8, data: []const u8, key: [32]u8, nonce: [12]u8) !void { | 473 | pub fn chacha20poly1305Open(dst: []u8, msgAndTag: []const u8, data: []const u8, key: [32]u8, nonce: [12]u8) !void { |
| 474 | if (ciphertext.len < chacha20poly1305_tag_size) { | 474 | if (msgAndTag.len < chacha20poly1305_tag_size) { |
| 475 | return error.InvalidMessage; | 475 | return error.InvalidMessage; |
| 476 | } | 476 | } |
| 477 | | 477 | |
| 478 | // split ciphertext and tag | 478 | // split ciphertext and tag |
| 479 | assert(dst.len >= ciphertext.len - chacha20poly1305_tag_size); | 479 | assert(dst.len >= msgAndTag.len - chacha20poly1305_tag_size); |
| 480 | var polyTag = ciphertext[ciphertext.len - chacha20poly1305_tag_size ..]; | 480 | var ciphertext = msgAndTag[0 .. msgAndTag.len - chacha20poly1305_tag_size]; |
| 481 | ciphertext = ciphertext[0 .. ciphertext.len - chacha20poly1305_tag_size]; | 481 | var polyTag = msgAndTag[ciphertext.len..]; |
| 482 | | 482 | |
| 483 | // derive poly1305 key | 483 | // derive poly1305 key |
| 484 | var polyKey = [_]u8{0} ** 32; | 484 | var polyKey = [_]u8{0} ** 32; |
| ... | @@ -534,7 +534,7 @@ test "seal" { | ... | @@ -534,7 +534,7 @@ test "seal" { |
| 534 | | 534 | |
| 535 | var out: [exp_out.len]u8 = undefined; | 535 | var out: [exp_out.len]u8 = undefined; |
| 536 | chacha20poly1305Seal(out[0..], plaintext, data, key, nonce); | 536 | chacha20poly1305Seal(out[0..], plaintext, data, key, nonce); |
| 537 | testing.expectEqualSlices(u8, exp_out, out); | 537 | testing.expectEqualSlices(u8, exp_out[0..], out[0..]); |
| 538 | } | 538 | } |
| 539 | { | 539 | { |
| 540 | const plaintext = [_]u8{ | 540 | const plaintext = [_]u8{ |
| ... | @@ -567,7 +567,7 @@ test "seal" { | ... | @@ -567,7 +567,7 @@ test "seal" { |
| 567 | | 567 | |
| 568 | var out: [exp_out.len]u8 = undefined; | 568 | var out: [exp_out.len]u8 = undefined; |
| 569 | chacha20poly1305Seal(out[0..], plaintext[0..], data[0..], key, nonce); | 569 | chacha20poly1305Seal(out[0..], plaintext[0..], data[0..], key, nonce); |
| 570 | testing.expectEqualSlices(u8, exp_out, out); | 570 | testing.expectEqualSlices(u8, exp_out[0..], out[0..]); |
| 571 | } | 571 | } |
| 572 | } | 572 | } |
| 573 | | 573 | |
| ... | @@ -584,7 +584,7 @@ test "open" { | ... | @@ -584,7 +584,7 @@ test "open" { |
| 584 | | 584 | |
| 585 | var out: [exp_out.len]u8 = undefined; | 585 | var out: [exp_out.len]u8 = undefined; |
| 586 | try chacha20poly1305Open(out[0..], ciphertext[0..], data, key, nonce); | 586 | try chacha20poly1305Open(out[0..], ciphertext[0..], data, key, nonce); |
| 587 | testing.expectEqualSlices(u8, exp_out, out); | 587 | testing.expectEqualSlices(u8, exp_out[0..], out[0..]); |
| 588 | } | 588 | } |
| 589 | { | 589 | { |
| 590 | const ciphertext = [_]u8{ | 590 | const ciphertext = [_]u8{ |
| ... | @@ -617,23 +617,23 @@ test "open" { | ... | @@ -617,23 +617,23 @@ test "open" { |
| 617 | | 617 | |
| 618 | var out: [exp_out.len]u8 = undefined; | 618 | var out: [exp_out.len]u8 = undefined; |
| 619 | try chacha20poly1305Open(out[0..], ciphertext[0..], data[0..], key, nonce); | 619 | try chacha20poly1305Open(out[0..], ciphertext[0..], data[0..], key, nonce); |
| 620 | testing.expectEqualSlices(u8, exp_out, out); | 620 | testing.expectEqualSlices(u8, exp_out[0..], out[0..]); |
| 621 | | 621 | |
| 622 | // corrupting the ciphertext, data, key, or nonce should cause a failure | 622 | // corrupting the ciphertext, data, key, or nonce should cause a failure |
| 623 | var bad_ciphertext = ciphertext; | 623 | var bad_ciphertext = ciphertext; |
| 624 | bad_ciphertext[0] ^= 1; | 624 | bad_ciphertext[0] ^= 1; |
| 625 | testing.expectError(error.AuthenticationFailed, chacha20poly1305Open(out[0..], bad_ciphertext[0..], data, key, nonce)); | 625 | testing.expectError(error.AuthenticationFailed, chacha20poly1305Open(out[0..], bad_ciphertext[0..], data[0..], key, nonce)); |
| 626 | var bad_data = data; | 626 | var bad_data = data; |
| 627 | bad_data[0] ^= 1; | 627 | bad_data[0] ^= 1; |
| 628 | testing.expectError(error.AuthenticationFailed, chacha20poly1305Open(out[0..], ciphertext[0..], bad_data, key, nonce)); | 628 | testing.expectError(error.AuthenticationFailed, chacha20poly1305Open(out[0..], ciphertext[0..], bad_data[0..], key, nonce)); |
| 629 | var bad_key = key; | 629 | var bad_key = key; |
| 630 | bad_key[0] ^= 1; | 630 | bad_key[0] ^= 1; |
| 631 | testing.expectError(error.AuthenticationFailed, chacha20poly1305Open(out[0..], ciphertext[0..], data, bad_key, nonce)); | 631 | testing.expectError(error.AuthenticationFailed, chacha20poly1305Open(out[0..], ciphertext[0..], data[0..], bad_key, nonce)); |
| 632 | var bad_nonce = nonce; | 632 | var bad_nonce = nonce; |
| 633 | bad_nonce[0] ^= 1; | 633 | bad_nonce[0] ^= 1; |
| 634 | testing.expectError(error.AuthenticationFailed, chacha20poly1305Open(out[0..], ciphertext[0..], data, key, bad_nonce)); | 634 | testing.expectError(error.AuthenticationFailed, chacha20poly1305Open(out[0..], ciphertext[0..], data[0..], key, bad_nonce)); |
| 635 | | 635 | |
| 636 | // a short ciphertext should result in a different error | 636 | // a short ciphertext should result in a different error |
| 637 | testing.expectError(error.InvalidMessage, chacha20poly1305Open(out[0..], "", data, key, bad_nonce)); | 637 | testing.expectError(error.InvalidMessage, chacha20poly1305Open(out[0..], "", data[0..], key, bad_nonce)); |
| 638 | } | 638 | } |
| 639 | } | 639 | } |