Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion libp2p/crypto/crypto.nim
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,9 @@ proc getRawBytes*(key: PrivateKey | PublicKey): CryptoResult[seq[byte]] =
proc getBytes*(key: PrivateKey): CryptoResult[seq[byte]] =
## Return private key ``key`` in binary form (using libp2p's protobuf
## serialization).
let rawBytes = ?key.getRawBytes()
var rawBytes = ?key.getRawBytes()
defer:
ncrutils.burnMem(rawBytes)
var encoded: seq[byte]
try:
var stream = memoryOutput()
Expand Down Expand Up @@ -474,6 +476,9 @@ proc toBytes*(sig: Signature, data: var openArray[byte]): int =
template initImpl[T: PrivateKey | PublicKey](key: var T, data: openArray[byte]): bool =
var id: uint64
var buffer: seq[byte]
when T is PrivateKey:
defer:
ncrutils.burnMem(buffer)
var gotId = false
var gotBuffer = false
var parseOk = false
Expand All @@ -491,6 +496,8 @@ template initImpl[T: PrivateKey | PublicKey](key: var T, data: openArray[byte]):
id = uint64(readValue(stream, puint64))
gotId = true
of 2:
when T is PrivateKey:
ncrutils.burnMem(buffer)
buffer = seq[byte](readValue(stream, pbytes))
gotBuffer = true
else:
Expand Down Expand Up @@ -598,6 +605,18 @@ proc init*(t: typedesc[PublicKey], data: openArray[byte]): CryptoResult[PublicKe
else:
ok(res)

proc copy*[T: PublicKey | PrivateKey](key: T): CryptoResult[T] =
## Copy key material independently, returning an error if copying fails.
if key.scheme notin SupportedSchemes:
return err(SchemeError)
if key.scheme in {PKScheme.Ed25519, PKScheme.Secp256k1}:
return ok(key)
var encoded = ?key.getBytes()
when T is PrivateKey:
defer:
ncrutils.burnMem(encoded)
T.init(encoded)

proc init*(t: typedesc[Signature], data: openArray[byte]): CryptoResult[Signature] =
## Create new public key from libp2p's protobuf serialized binary form.
var res: t
Expand Down
38 changes: 38 additions & 0 deletions tests/libp2p/crypto/test_crypto.nim
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,44 @@ proc testStretcher(s, e: int, cs: string, ds: string): bool =
allMatch

suite "Key interface test suite":
test "Explicit key copies preserve independent key material":
for scheme in PKScheme:
let privateKey = PrivateKey.random(scheme, rng(), 2048).get()
let publicKey = privateKey.getPublicKey().get()
let privateBytes = privateKey.getBytes().get()
let publicBytes = publicKey.getBytes().get()
var privateCopy = privateKey.copy().get()
var publicCopy = publicKey.copy().get()
check:
privateCopy.getBytes().get() == privateBytes
publicCopy.getBytes().get() == publicBytes
case scheme
of RSA:
privateCopy.rsakey.seck.p[] = privateCopy.rsakey.seck.p[] xor 1
publicCopy.rsakey.key.n[] = publicCopy.rsakey.key.n[] xor 1
check:
privateCopy.getBytes().get() != privateBytes
publicCopy.getBytes().get() != publicBytes
of ECDSA:
privateCopy.eckey.buffer[0] = privateCopy.eckey.buffer[0] xor 1
publicCopy.eckey.buffer[0] = publicCopy.eckey.buffer[0] xor 1
of Ed25519:
privateCopy.edkey.data[0] = privateCopy.edkey.data[0] xor 1
publicCopy.edkey.data[0] = publicCopy.edkey.data[0] xor 1
of Secp256k1:
privateCopy.skkey = default(typeof(privateCopy.skkey))
publicCopy.skkey = default(typeof(publicCopy.skkey))
check:
privateKey.getBytes().get() == privateBytes
publicKey.getBytes().get() == publicBytes

test "Copy returns errors for missing keys":
check:
PrivateKey(scheme: RSA).copy().isErr
PublicKey(scheme: RSA).copy().isErr
PrivateKey(scheme: ECDSA).copy().isErr
PublicKey(scheme: ECDSA).copy().isErr

test "Go test vectors":
for i in 0 ..< len(PrivateKeys):
var seckey =
Expand Down
Loading