hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit b41a3b9cbad400925a54790f265179c36926553f
parent 266ed58ec091fa7f290d736d0b9b6605f644bdae
Author: Armin Preiml <apreiml@strohwolke.at>
Date:   Mon, 16 Jan 2023 15:56:49 +0100

crypto::chacha20: slice instead of array ptr

Slices avoid needless copying of secret data if working with buffers.
Should not break any code, since array pointers are automatically casted
to slices.

Signed-off-by: Armin Preiml <apreiml@strohwolke.at>

Diffstat:
Mcrypto/chacha/+test.ha | 10+++++-----
Mcrypto/chacha/chacha20.ha | 28+++++++++++++++++-----------
2 files changed, 22 insertions(+), 16 deletions(-)

diff --git a/crypto/chacha/+test.ha b/crypto/chacha/+test.ha @@ -53,7 +53,7 @@ use io; let c = chacha20(); defer io::close(&c)!; - chacha20_init(&c, &cipherbuf, &key, &nonce); + chacha20_init(&c, &cipherbuf, key, nonce); setctr(&c, 1); const n = io::writeall(&c, msg)!; @@ -63,7 +63,7 @@ use io; result = [0...]; cipherbuf = bufio::fixed(result, io::mode::WRITE); - chacha20_init(&c, &cipherbuf, &key, &nonce); + chacha20_init(&c, &cipherbuf, key, nonce); setctr(&c, 1); io::write(&c, msg[..10])!; io::write(&c, msg[10..63])!; @@ -160,7 +160,7 @@ const xcipher: [_]u8 = [ let c = chacha20(); defer io::close(&c)!; - xchacha20_init(&c, &cipherbuf, &xkey, &xnonce); + xchacha20_init(&c, &cipherbuf, xkey, xnonce); setctr(&c, 1); io::writeall(&c, xmsg)!; @@ -174,7 +174,7 @@ const xcipher: [_]u8 = [ let c = chacha20(); defer io::close(&c)!; - xchacha20_init(&c, &cipherbuf, &xkey, &xnonce); + xchacha20_init(&c, &cipherbuf, xkey, xnonce); // just encrypt a few bytes of each block, to check if setctr works setctr(&c, 1); @@ -211,6 +211,6 @@ const xcipher: [_]u8 = [ ]; let out: [32]u8 = [0...]; - hchacha20(&out, &key, &nonce); + hchacha20(&out, key, nonce); assert(bytes::equal(out, expected)); }; diff --git a/crypto/chacha/chacha20.ha b/crypto/chacha/chacha20.ha @@ -51,9 +51,12 @@ export fn chacha20() stream = { export fn chacha20_init( s: *stream, h: io::handle, - key: *[KEYSIZE]u8, - nonce: *[NONCESIZE]u8 + key: []u8, + nonce: []u8 ) void = { + assert(len(key) == KEYSIZE); + assert(len(nonce) == NONCESIZE); + s.h = h; s.state[0] = magic[0]; @@ -77,29 +80,32 @@ export fn chacha20_init( export fn xchacha20_init( s: *stream, h: io::handle, - key: *[KEYSIZE]u8, - nonce: *[XNONCESIZE]u8 + key: []u8, + nonce: []u8 ) void = { + assert(len(key) == KEYSIZE); + assert(len(nonce) == XNONCESIZE); + let dkey: [32]u8 = [0...]; - // XXX: https://todo.sr.ht/~sircmpwn/hare/719 - let hnonce: [16]u8 = [0...]; - hnonce[..] = nonce[..16]; - hchacha20(&dkey, key, &hnonce); + hchacha20(&dkey, key, nonce[..16]); let dnonce: [NONCESIZE]u8 = [0...]; dnonce[4..] = nonce[16..]; - chacha20_init(s, h, &dkey, &dnonce); + chacha20_init(s, h, &dkey, dnonce); bytes::zero(dkey); bytes::zero(dnonce); - bytes::zero(hnonce); }; // Derives a new key from 'key' and 'nonce' as used during XChaCha20 // initialization. This function may only be used for specific purposes // such as X25519 key derivation. Do not use if in doubt. -export fn hchacha20(out: *[32]u8, key: *[32]u8, nonce: *[16]u8) void = { +export fn hchacha20(out: []u8, key: []u8, nonce: []u8) void = { + assert(len(out) == KEYSIZE); + assert(len(key) == KEYSIZE); + assert(len(nonce) == 16); + let state: [16]u32 = [0...]; defer bytes::zero((state: []u8: *[*]u8)[..BLOCKSIZE]);