From fc6b8f46f17f5be71771eff55adcd47cc6939cf4 Mon Sep 17 00:00:00 2001 From: tersec Date: Tue, 12 Apr 2022 10:41:48 +0000 Subject: [PATCH] encrypt and decrypt empty sequences (#713) * encrypt and decrypt empty sequences * use assign in curve25519 --- libp2p/crypto/chacha20poly1305.nim | 18 ++++++++++-------- libp2p/crypto/curve25519.nim | 5 +++-- tests/testcrypto.nim | 11 +++++++++++ 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/libp2p/crypto/chacha20poly1305.nim b/libp2p/crypto/chacha20poly1305.nim index afc4ce568..094423e52 100644 --- a/libp2p/crypto/chacha20poly1305.nim +++ b/libp2p/crypto/chacha20poly1305.nim @@ -1,5 +1,5 @@ ## Nim-Libp2p -## Copyright (c) 2020 Status Research & Development GmbH +## Copyright (c) 2020-2022 Status Research & Development GmbH ## Licensed under either of ## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) ## * MIT license ([LICENSE-MIT](LICENSE-MIT)) @@ -18,6 +18,8 @@ {.push raises: [Defect].} import bearssl +from stew/assign2 import assign +from stew/ranges/ptr_arith import baseAddr # have to do this due to a nim bug and raises[] on callbacks # https://github.com/nim-lang/Nim/issues/13905 @@ -39,15 +41,15 @@ type proc intoChaChaPolyKey*(s: openArray[byte]): ChaChaPolyKey = assert s.len == ChaChaPolyKeySize - copyMem(addr result[0], unsafeAddr s[0], ChaChaPolyKeySize) + assign(result, s) proc intoChaChaPolyNonce*(s: openArray[byte]): ChaChaPolyNonce = assert s.len == ChaChaPolyNonceSize - copyMem(addr result[0], unsafeAddr s[0], ChaChaPolyNonceSize) + assign(result, s) proc intoChaChaPolyTag*(s: openArray[byte]): ChaChaPolyTag = assert s.len == ChaChaPolyTagSize - copyMem(addr result[0], unsafeAddr s[0], ChaChaPolyTagSize) + assign(result, s) # bearssl allows us to use optimized versions # this is reconciled at runtime @@ -68,11 +70,11 @@ proc encrypt*(_: type[ChaChaPoly], ourPoly1305CtmulRun( unsafeAddr key[0], unsafeAddr nonce[0], - addr data[0], + baseAddr(data), data.len, ad, aad.len, - addr tag[0], + baseAddr(tag), chacha20CtRun, #[encrypt]# 1.cint) @@ -91,10 +93,10 @@ proc decrypt*(_: type[ChaChaPoly], ourPoly1305CtmulRun( unsafeAddr key[0], unsafeAddr nonce[0], - addr data[0], + baseAddr(data), data.len, ad, aad.len, - addr tag[0], + baseAddr(tag), chacha20CtRun, #[decrypt]# 0.cint) diff --git a/libp2p/crypto/curve25519.nim b/libp2p/crypto/curve25519.nim index ef054171c..d1b0b0029 100644 --- a/libp2p/crypto/curve25519.nim +++ b/libp2p/crypto/curve25519.nim @@ -1,5 +1,5 @@ ## Nim-Libp2p -## Copyright (c) 2020 Status Research & Development GmbH +## Copyright (c) 2020-2022 Status Research & Development GmbH ## Licensed under either of ## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) ## * MIT license ([LICENSE-MIT](LICENSE-MIT)) @@ -19,6 +19,7 @@ import bearssl import stew/results +from stew/assign2 import assign export results const @@ -33,7 +34,7 @@ type proc intoCurve25519Key*(s: openArray[byte]): Curve25519Key = assert s.len == Curve25519KeySize - copyMem(addr result[0], unsafeAddr s[0], Curve25519KeySize) + assign(result, s) proc getBytes*(key: Curve25519Key): seq[byte] = @key diff --git a/tests/testcrypto.nim b/tests/testcrypto.nim index ceecb2225..cc72e1a83 100644 --- a/tests/testcrypto.nim +++ b/tests/testcrypto.nim @@ -483,6 +483,17 @@ suite "Key interface test suite": ChaChaPoly.decrypt(key, nonce, btag, smallPlain, noaed) check ntag.toHex == btag.toHex + # ensure even a 0 byte array works + block: + var + emptyPlain: array[0, byte] + btag: ChaChaPolyTag + noaed: array[0, byte] + ChaChaPoly.encrypt(key, nonce, btag, emptyPlain, noaed) + ntag = btag + ChaChaPoly.decrypt(key, nonce, btag, emptyPlain, noaed) + check ntag.toHex == btag.toHex + test "Curve25519": # from bearssl test_crypto.c var