encrypt and decrypt empty sequences (#713)
* encrypt and decrypt empty sequences * use assign in curve25519
This commit is contained in:
parent
9973b9466d
commit
fc6b8f46f1
|
@ -1,5 +1,5 @@
|
||||||
## Nim-Libp2p
|
## Nim-Libp2p
|
||||||
## Copyright (c) 2020 Status Research & Development GmbH
|
## Copyright (c) 2020-2022 Status Research & Development GmbH
|
||||||
## Licensed under either of
|
## Licensed under either of
|
||||||
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
||||||
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
||||||
|
@ -18,6 +18,8 @@
|
||||||
{.push raises: [Defect].}
|
{.push raises: [Defect].}
|
||||||
|
|
||||||
import bearssl
|
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
|
# have to do this due to a nim bug and raises[] on callbacks
|
||||||
# https://github.com/nim-lang/Nim/issues/13905
|
# https://github.com/nim-lang/Nim/issues/13905
|
||||||
|
@ -39,15 +41,15 @@ type
|
||||||
|
|
||||||
proc intoChaChaPolyKey*(s: openArray[byte]): ChaChaPolyKey =
|
proc intoChaChaPolyKey*(s: openArray[byte]): ChaChaPolyKey =
|
||||||
assert s.len == ChaChaPolyKeySize
|
assert s.len == ChaChaPolyKeySize
|
||||||
copyMem(addr result[0], unsafeAddr s[0], ChaChaPolyKeySize)
|
assign(result, s)
|
||||||
|
|
||||||
proc intoChaChaPolyNonce*(s: openArray[byte]): ChaChaPolyNonce =
|
proc intoChaChaPolyNonce*(s: openArray[byte]): ChaChaPolyNonce =
|
||||||
assert s.len == ChaChaPolyNonceSize
|
assert s.len == ChaChaPolyNonceSize
|
||||||
copyMem(addr result[0], unsafeAddr s[0], ChaChaPolyNonceSize)
|
assign(result, s)
|
||||||
|
|
||||||
proc intoChaChaPolyTag*(s: openArray[byte]): ChaChaPolyTag =
|
proc intoChaChaPolyTag*(s: openArray[byte]): ChaChaPolyTag =
|
||||||
assert s.len == ChaChaPolyTagSize
|
assert s.len == ChaChaPolyTagSize
|
||||||
copyMem(addr result[0], unsafeAddr s[0], ChaChaPolyTagSize)
|
assign(result, s)
|
||||||
|
|
||||||
# bearssl allows us to use optimized versions
|
# bearssl allows us to use optimized versions
|
||||||
# this is reconciled at runtime
|
# this is reconciled at runtime
|
||||||
|
@ -68,11 +70,11 @@ proc encrypt*(_: type[ChaChaPoly],
|
||||||
ourPoly1305CtmulRun(
|
ourPoly1305CtmulRun(
|
||||||
unsafeAddr key[0],
|
unsafeAddr key[0],
|
||||||
unsafeAddr nonce[0],
|
unsafeAddr nonce[0],
|
||||||
addr data[0],
|
baseAddr(data),
|
||||||
data.len,
|
data.len,
|
||||||
ad,
|
ad,
|
||||||
aad.len,
|
aad.len,
|
||||||
addr tag[0],
|
baseAddr(tag),
|
||||||
chacha20CtRun,
|
chacha20CtRun,
|
||||||
#[encrypt]# 1.cint)
|
#[encrypt]# 1.cint)
|
||||||
|
|
||||||
|
@ -91,10 +93,10 @@ proc decrypt*(_: type[ChaChaPoly],
|
||||||
ourPoly1305CtmulRun(
|
ourPoly1305CtmulRun(
|
||||||
unsafeAddr key[0],
|
unsafeAddr key[0],
|
||||||
unsafeAddr nonce[0],
|
unsafeAddr nonce[0],
|
||||||
addr data[0],
|
baseAddr(data),
|
||||||
data.len,
|
data.len,
|
||||||
ad,
|
ad,
|
||||||
aad.len,
|
aad.len,
|
||||||
addr tag[0],
|
baseAddr(tag),
|
||||||
chacha20CtRun,
|
chacha20CtRun,
|
||||||
#[decrypt]# 0.cint)
|
#[decrypt]# 0.cint)
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
## Nim-Libp2p
|
## Nim-Libp2p
|
||||||
## Copyright (c) 2020 Status Research & Development GmbH
|
## Copyright (c) 2020-2022 Status Research & Development GmbH
|
||||||
## Licensed under either of
|
## Licensed under either of
|
||||||
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
||||||
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
||||||
|
@ -19,6 +19,7 @@
|
||||||
|
|
||||||
import bearssl
|
import bearssl
|
||||||
import stew/results
|
import stew/results
|
||||||
|
from stew/assign2 import assign
|
||||||
export results
|
export results
|
||||||
|
|
||||||
const
|
const
|
||||||
|
@ -33,7 +34,7 @@ type
|
||||||
|
|
||||||
proc intoCurve25519Key*(s: openArray[byte]): Curve25519Key =
|
proc intoCurve25519Key*(s: openArray[byte]): Curve25519Key =
|
||||||
assert s.len == Curve25519KeySize
|
assert s.len == Curve25519KeySize
|
||||||
copyMem(addr result[0], unsafeAddr s[0], Curve25519KeySize)
|
assign(result, s)
|
||||||
|
|
||||||
proc getBytes*(key: Curve25519Key): seq[byte] = @key
|
proc getBytes*(key: Curve25519Key): seq[byte] = @key
|
||||||
|
|
||||||
|
|
|
@ -483,6 +483,17 @@ suite "Key interface test suite":
|
||||||
ChaChaPoly.decrypt(key, nonce, btag, smallPlain, noaed)
|
ChaChaPoly.decrypt(key, nonce, btag, smallPlain, noaed)
|
||||||
check ntag.toHex == btag.toHex
|
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":
|
test "Curve25519":
|
||||||
# from bearssl test_crypto.c
|
# from bearssl test_crypto.c
|
||||||
var
|
var
|
||||||
|
|
Loading…
Reference in New Issue