2022-07-01 18:19:57 +00:00
|
|
|
# Nim-Libp2p
|
2023-01-20 14:47:40 +00:00
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
2022-07-01 18:19:57 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
2019-03-02 19:19:41 +00:00
|
|
|
|
|
|
|
## This module implements Public Key and Private Key interface for libp2p.
|
2023-06-07 11:12:49 +00:00
|
|
|
{.push raises: [].}
|
2021-02-25 15:24:49 +00:00
|
|
|
|
2020-07-21 20:10:21 +00:00
|
|
|
from strutils import split, strip, cmpIgnoreCase
|
|
|
|
|
|
|
|
const libp2p_pki_schemes* {.strdefine.} = "rsa,ed25519,secp256k1,ecnist"
|
|
|
|
|
|
|
|
type PKScheme* = enum
|
|
|
|
RSA = 0
|
|
|
|
Ed25519
|
|
|
|
Secp256k1
|
|
|
|
ECDSA
|
|
|
|
|
|
|
|
proc initSupportedSchemes(list: static string): set[PKScheme] =
|
|
|
|
var res: set[PKScheme]
|
|
|
|
|
|
|
|
let schemes = split(list, {',', ';', '|'})
|
|
|
|
for item in schemes:
|
|
|
|
if cmpIgnoreCase(strip(item), "rsa") == 0:
|
|
|
|
res.incl(PKScheme.RSA)
|
|
|
|
elif cmpIgnoreCase(strip(item), "ed25519") == 0:
|
|
|
|
res.incl(PKScheme.Ed25519)
|
|
|
|
elif cmpIgnoreCase(strip(item), "secp256k1") == 0:
|
|
|
|
res.incl(PKScheme.Secp256k1)
|
|
|
|
elif cmpIgnoreCase(strip(item), "ecnist") == 0:
|
|
|
|
res.incl(PKScheme.ECDSA)
|
|
|
|
if len(res) == 0:
|
|
|
|
res = {PKScheme.RSA, PKScheme.Ed25519, PKScheme.Secp256k1, PKScheme.ECDSA}
|
|
|
|
res
|
|
|
|
|
|
|
|
proc initSupportedSchemes(schemes: static set[PKScheme]): set[int8] =
|
|
|
|
var res: set[int8]
|
|
|
|
if PKScheme.RSA in schemes:
|
|
|
|
res.incl(int8(PKScheme.RSA))
|
|
|
|
if PKScheme.Ed25519 in schemes:
|
|
|
|
res.incl(int8(PKScheme.Ed25519))
|
|
|
|
if PKScheme.Secp256k1 in schemes:
|
|
|
|
res.incl(int8(PKScheme.Secp256k1))
|
|
|
|
if PKScheme.ECDSA in schemes:
|
|
|
|
res.incl(int8(PKScheme.ECDSA))
|
|
|
|
res
|
2020-05-18 05:25:55 +00:00
|
|
|
|
2020-07-21 20:10:21 +00:00
|
|
|
const
|
|
|
|
SupportedSchemes* = initSupportedSchemes(libp2p_pki_schemes)
|
|
|
|
SupportedSchemesInt* = initSupportedSchemes(SupportedSchemes)
|
|
|
|
RsaDefaultKeySize* = 3072
|
|
|
|
|
|
|
|
template supported*(scheme: PKScheme): bool =
|
|
|
|
## Returns true if specified ``scheme`` is currently available.
|
|
|
|
scheme in SupportedSchemes
|
|
|
|
|
|
|
|
when supported(PKScheme.RSA):
|
|
|
|
import rsa
|
|
|
|
when supported(PKScheme.Ed25519):
|
|
|
|
import ed25519/ed25519
|
|
|
|
when supported(PKScheme.Secp256k1):
|
|
|
|
import secp
|
2023-08-30 09:39:48 +00:00
|
|
|
when supported(PKScheme.ECDSA):
|
|
|
|
import ecnist
|
2020-07-21 20:10:21 +00:00
|
|
|
|
2023-08-30 09:39:48 +00:00
|
|
|
# These used to be declared in `crypto` itself
|
|
|
|
export ecnist.ephemeral, ecnist.ECDHEScheme
|
2020-07-21 20:10:21 +00:00
|
|
|
|
2023-08-30 09:39:48 +00:00
|
|
|
import bearssl/rand, bearssl/hash as bhash
|
2019-09-11 16:03:39 +00:00
|
|
|
import ../protobuf/minprotobuf, ../vbuffer, ../multihash, ../multicodec
|
2020-08-11 14:48:21 +00:00
|
|
|
import nimcrypto/[rijndael, twofish, sha2, hash, hmac]
|
|
|
|
# We use `ncrutils` for constant-time hexadecimal encoding/decoding procedures.
|
|
|
|
import nimcrypto/utils as ncrutils
|
2020-03-23 06:03:36 +00:00
|
|
|
import ../utility
|
2020-04-23 12:10:20 +00:00
|
|
|
import stew/results
|
2022-07-01 18:19:57 +00:00
|
|
|
export results, utility
|
2019-03-02 19:19:41 +00:00
|
|
|
|
2020-07-07 13:48:15 +00:00
|
|
|
# This is workaround for Nim's `import` bug
|
2022-06-16 08:08:52 +00:00
|
|
|
export rijndael, twofish, sha2, hash, hmac, ncrutils, rand
|
2019-03-06 11:23:12 +00:00
|
|
|
|
2019-03-02 19:19:41 +00:00
|
|
|
type
|
|
|
|
DigestSheme* = enum
|
|
|
|
Sha256
|
|
|
|
Sha512
|
|
|
|
|
|
|
|
PublicKey* = object
|
|
|
|
case scheme*: PKScheme
|
2020-07-21 20:10:21 +00:00
|
|
|
of PKScheme.RSA:
|
|
|
|
when PKScheme.RSA in SupportedSchemes:
|
|
|
|
rsakey*: rsa.RsaPublicKey
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
of PKScheme.Ed25519:
|
|
|
|
when supported(PKScheme.Ed25519):
|
|
|
|
edkey*: EdPublicKey
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
of PKScheme.Secp256k1:
|
|
|
|
when supported(PKScheme.Secp256k1):
|
|
|
|
skkey*: SkPublicKey
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
of PKScheme.ECDSA:
|
|
|
|
when supported(PKScheme.ECDSA):
|
|
|
|
eckey*: ecnist.EcPublicKey
|
|
|
|
else:
|
|
|
|
discard
|
2019-03-02 19:19:41 +00:00
|
|
|
|
|
|
|
PrivateKey* = object
|
|
|
|
case scheme*: PKScheme
|
2020-07-21 20:10:21 +00:00
|
|
|
of PKScheme.RSA:
|
|
|
|
when supported(PKScheme.RSA):
|
|
|
|
rsakey*: rsa.RsaPrivateKey
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
of PKScheme.Ed25519:
|
|
|
|
when supported(PKScheme.Ed25519):
|
|
|
|
edkey*: EdPrivateKey
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
of PKScheme.Secp256k1:
|
|
|
|
when supported(PKScheme.Secp256k1):
|
|
|
|
skkey*: SkPrivateKey
|
|
|
|
else:
|
|
|
|
discard
|
2021-12-16 10:05:20 +00:00
|
|
|
of PKScheme.ECDSA:
|
|
|
|
when supported(PKScheme.ECDSA):
|
2020-07-21 20:10:21 +00:00
|
|
|
eckey*: ecnist.EcPrivateKey
|
|
|
|
else:
|
|
|
|
discard
|
2019-03-02 19:19:41 +00:00
|
|
|
|
|
|
|
KeyPair* = object
|
|
|
|
seckey*: PrivateKey
|
|
|
|
pubkey*: PublicKey
|
|
|
|
|
|
|
|
Secret* = object
|
|
|
|
ivsize*: int
|
|
|
|
keysize*: int
|
|
|
|
macsize*: int
|
|
|
|
data*: seq[byte]
|
|
|
|
|
2019-03-06 11:23:12 +00:00
|
|
|
Signature* = object
|
|
|
|
data*: seq[byte]
|
|
|
|
|
2020-05-18 05:25:55 +00:00
|
|
|
CryptoError* = enum
|
|
|
|
KeyError
|
|
|
|
SigError
|
|
|
|
HashError
|
|
|
|
SchemeError
|
|
|
|
|
|
|
|
CryptoResult*[T] = Result[T, CryptoError]
|
2019-03-02 19:19:41 +00:00
|
|
|
|
2020-07-01 06:25:09 +00:00
|
|
|
template orError*(exp: untyped, err: untyped): untyped =
|
2020-05-18 05:25:55 +00:00
|
|
|
exp.mapErr do(_: auto) -> auto:
|
|
|
|
err
|
|
|
|
|
2022-06-16 08:08:52 +00:00
|
|
|
proc newRng*(): ref HmacDrbgContext =
|
2020-07-07 11:14:11 +00:00
|
|
|
# You should only create one instance of the RNG per application / library
|
|
|
|
# Ref is used so that it can be shared between components
|
|
|
|
# TODO consider moving to bearssl
|
2022-06-16 08:08:52 +00:00
|
|
|
var seeder = prngSeederSystem(nil)
|
2020-07-07 11:14:11 +00:00
|
|
|
if seeder == nil:
|
|
|
|
return nil
|
|
|
|
|
2022-06-16 08:08:52 +00:00
|
|
|
var rng = (ref HmacDrbgContext)()
|
|
|
|
hmacDrbgInit(rng[], addr sha256Vtable, nil, 0)
|
2020-07-07 11:14:11 +00:00
|
|
|
if seeder(addr rng.vtable) == 0:
|
|
|
|
return nil
|
|
|
|
rng
|
|
|
|
|
2022-06-16 08:08:52 +00:00
|
|
|
proc shuffle*[T](rng: ref HmacDrbgContext, x: var openArray[T]) =
|
|
|
|
if x.len == 0:
|
|
|
|
return
|
|
|
|
|
2021-10-25 08:26:32 +00:00
|
|
|
var randValues = newSeqUninitialized[byte](len(x) * 2)
|
2022-06-16 08:08:52 +00:00
|
|
|
hmacDrbgGenerate(rng[], randValues)
|
2021-10-25 08:26:32 +00:00
|
|
|
|
|
|
|
for i in countdown(x.high, 1):
|
|
|
|
let
|
|
|
|
rand = randValues[i * 2].int32 or (randValues[i * 2 + 1].int32 shl 8)
|
|
|
|
y = rand mod i
|
|
|
|
swap(x[i], x[y])
|
|
|
|
|
2020-07-21 20:10:21 +00:00
|
|
|
proc random*(
|
|
|
|
T: typedesc[PrivateKey],
|
|
|
|
scheme: PKScheme,
|
2022-06-16 08:08:52 +00:00
|
|
|
rng: var HmacDrbgContext,
|
2020-07-21 20:10:21 +00:00
|
|
|
bits = RsaDefaultKeySize,
|
|
|
|
): CryptoResult[PrivateKey] =
|
2019-03-02 19:19:41 +00:00
|
|
|
## Generate random private key for scheme ``scheme``.
|
|
|
|
##
|
|
|
|
## ``bits`` is number of bits for RSA key, ``bits`` value must be in
|
2020-07-21 20:10:21 +00:00
|
|
|
## [2048, 4096], default value is 3072 bits.
|
2020-05-18 05:25:55 +00:00
|
|
|
case scheme
|
2020-07-21 20:10:21 +00:00
|
|
|
of PKScheme.RSA:
|
|
|
|
when supported(PKScheme.RSA):
|
|
|
|
let rsakey = ?RsaPrivateKey.random(rng, bits).orError(KeyError)
|
|
|
|
ok(PrivateKey(scheme: scheme, rsakey: rsakey))
|
|
|
|
else:
|
|
|
|
err(SchemeError)
|
|
|
|
of PKScheme.Ed25519:
|
|
|
|
when supported(PKScheme.Ed25519):
|
|
|
|
let edkey = EdPrivateKey.random(rng)
|
|
|
|
ok(PrivateKey(scheme: scheme, edkey: edkey))
|
|
|
|
else:
|
|
|
|
err(SchemeError)
|
|
|
|
of PKScheme.ECDSA:
|
|
|
|
when supported(PKScheme.ECDSA):
|
|
|
|
let eckey = ?ecnist.EcPrivateKey.random(Secp256r1, rng).orError(KeyError)
|
|
|
|
ok(PrivateKey(scheme: scheme, eckey: eckey))
|
|
|
|
else:
|
|
|
|
err(SchemeError)
|
|
|
|
of PKScheme.Secp256k1:
|
|
|
|
when supported(PKScheme.Secp256k1):
|
|
|
|
let skkey = SkPrivateKey.random(rng)
|
|
|
|
ok(PrivateKey(scheme: scheme, skkey: skkey))
|
|
|
|
else:
|
|
|
|
err(SchemeError)
|
|
|
|
|
2022-06-16 08:08:52 +00:00
|
|
|
proc random*(
|
|
|
|
T: typedesc[PrivateKey], rng: var HmacDrbgContext, bits = RsaDefaultKeySize
|
2020-07-21 20:10:21 +00:00
|
|
|
): CryptoResult[PrivateKey] =
|
|
|
|
## Generate random private key using default public-key cryptography scheme.
|
|
|
|
##
|
|
|
|
## Default public-key cryptography schemes are following order:
|
|
|
|
## ed25519, secp256k1, RSA, secp256r1.
|
|
|
|
##
|
|
|
|
## So will be used first available (supported) method.
|
|
|
|
when supported(PKScheme.Ed25519):
|
2020-07-07 11:14:11 +00:00
|
|
|
let edkey = EdPrivateKey.random(rng)
|
2020-07-21 20:10:21 +00:00
|
|
|
ok(PrivateKey(scheme: PKScheme.Ed25519, edkey: edkey))
|
|
|
|
elif supported(PKScheme.Secp256k1):
|
2020-07-07 11:14:11 +00:00
|
|
|
let skkey = SkPrivateKey.random(rng)
|
2020-07-21 20:10:21 +00:00
|
|
|
ok(PrivateKey(scheme: PKScheme.Secp256k1, skkey: skkey))
|
|
|
|
elif supported(PKScheme.RSA):
|
|
|
|
let rsakey = ?RsaPrivateKey.random(rng, bits).orError(KeyError)
|
|
|
|
ok(PrivateKey(scheme: PKScheme.RSA, rsakey: rsakey))
|
|
|
|
elif supported(PKScheme.ECDSA):
|
|
|
|
let eckey = ?ecnist.EcPrivateKey.random(Secp256r1, rng).orError(KeyError)
|
|
|
|
ok(PrivateKey(scheme: PKScheme.ECDSA, eckey: eckey))
|
2020-05-18 05:25:55 +00:00
|
|
|
else:
|
|
|
|
err(SchemeError)
|
2019-03-02 19:19:41 +00:00
|
|
|
|
2020-07-21 20:10:21 +00:00
|
|
|
proc random*(
|
|
|
|
T: typedesc[KeyPair],
|
|
|
|
scheme: PKScheme,
|
2022-06-16 08:08:52 +00:00
|
|
|
rng: var HmacDrbgContext,
|
2020-07-21 20:10:21 +00:00
|
|
|
bits = RsaDefaultKeySize,
|
|
|
|
): CryptoResult[KeyPair] =
|
2019-03-02 19:19:41 +00:00
|
|
|
## Generate random key pair for scheme ``scheme``.
|
|
|
|
##
|
|
|
|
## ``bits`` is number of bits for RSA key, ``bits`` value must be in
|
|
|
|
## [512, 4096], default value is 2048 bits.
|
2020-05-18 05:25:55 +00:00
|
|
|
case scheme
|
2020-07-21 20:10:21 +00:00
|
|
|
of PKScheme.RSA:
|
|
|
|
when supported(PKScheme.RSA):
|
|
|
|
let pair = ?RsaKeyPair.random(rng, bits).orError(KeyError)
|
|
|
|
ok(
|
|
|
|
KeyPair(
|
|
|
|
seckey: PrivateKey(scheme: scheme, rsakey: pair.seckey),
|
|
|
|
pubkey: PublicKey(scheme: scheme, rsakey: pair.pubkey),
|
|
|
|
)
|
2024-06-11 15:18:06 +00:00
|
|
|
)
|
2020-07-21 20:10:21 +00:00
|
|
|
else:
|
|
|
|
err(SchemeError)
|
|
|
|
of PKScheme.Ed25519:
|
|
|
|
when supported(PKScheme.Ed25519):
|
|
|
|
let pair = EdKeyPair.random(rng)
|
|
|
|
ok(
|
|
|
|
KeyPair(
|
|
|
|
seckey: PrivateKey(scheme: scheme, edkey: pair.seckey),
|
|
|
|
pubkey: PublicKey(scheme: scheme, edkey: pair.pubkey),
|
|
|
|
)
|
2024-06-11 15:18:06 +00:00
|
|
|
)
|
2020-07-21 20:10:21 +00:00
|
|
|
else:
|
|
|
|
err(SchemeError)
|
|
|
|
of PKScheme.ECDSA:
|
|
|
|
when supported(PKScheme.ECDSA):
|
|
|
|
let pair = ?EcKeyPair.random(Secp256r1, rng).orError(KeyError)
|
|
|
|
ok(
|
|
|
|
KeyPair(
|
|
|
|
seckey: PrivateKey(scheme: scheme, eckey: pair.seckey),
|
|
|
|
pubkey: PublicKey(scheme: scheme, eckey: pair.pubkey),
|
|
|
|
)
|
2024-06-11 15:18:06 +00:00
|
|
|
)
|
2020-07-21 20:10:21 +00:00
|
|
|
else:
|
|
|
|
err(SchemeError)
|
|
|
|
of PKScheme.Secp256k1:
|
|
|
|
when supported(PKScheme.Secp256k1):
|
|
|
|
let pair = SkKeyPair.random(rng)
|
|
|
|
ok(
|
|
|
|
KeyPair(
|
|
|
|
seckey: PrivateKey(scheme: scheme, skkey: pair.seckey),
|
|
|
|
pubkey: PublicKey(scheme: scheme, skkey: pair.pubkey),
|
|
|
|
)
|
2024-06-11 15:18:06 +00:00
|
|
|
)
|
2020-07-21 20:10:21 +00:00
|
|
|
else:
|
|
|
|
err(SchemeError)
|
|
|
|
|
2022-06-16 08:08:52 +00:00
|
|
|
proc random*(
|
|
|
|
T: typedesc[KeyPair], rng: var HmacDrbgContext, bits = RsaDefaultKeySize
|
2020-07-21 20:10:21 +00:00
|
|
|
): CryptoResult[KeyPair] =
|
|
|
|
## Generate random private pair of keys using default public-key cryptography
|
|
|
|
## scheme.
|
|
|
|
##
|
|
|
|
## Default public-key cryptography schemes are following order:
|
|
|
|
## ed25519, secp256k1, RSA, secp256r1.
|
|
|
|
##
|
|
|
|
## So will be used first available (supported) method.
|
|
|
|
when supported(PKScheme.Ed25519):
|
2020-07-07 11:14:11 +00:00
|
|
|
let pair = EdKeyPair.random(rng)
|
2020-05-18 05:25:55 +00:00
|
|
|
ok(
|
|
|
|
KeyPair(
|
2020-07-21 20:10:21 +00:00
|
|
|
seckey: PrivateKey(scheme: PKScheme.Ed25519, edkey: pair.seckey),
|
|
|
|
pubkey: PublicKey(scheme: PKScheme.Ed25519, edkey: pair.pubkey),
|
|
|
|
)
|
2024-06-11 15:18:06 +00:00
|
|
|
)
|
2020-07-21 20:10:21 +00:00
|
|
|
elif supported(PKScheme.Secp256k1):
|
2020-07-07 11:14:11 +00:00
|
|
|
let pair = SkKeyPair.random(rng)
|
2020-05-18 05:25:55 +00:00
|
|
|
ok(
|
|
|
|
KeyPair(
|
2020-07-21 20:10:21 +00:00
|
|
|
seckey: PrivateKey(scheme: PKScheme.Secp256k1, skkey: pair.seckey),
|
|
|
|
pubkey: PublicKey(scheme: PKScheme.Secp256k1, skkey: pair.pubkey),
|
|
|
|
)
|
2024-06-11 15:18:06 +00:00
|
|
|
)
|
2020-07-21 20:10:21 +00:00
|
|
|
elif supported(PKScheme.RSA):
|
|
|
|
let pair = ?RsaKeyPair.random(rng, bits).orError(KeyError)
|
|
|
|
ok(
|
|
|
|
KeyPair(
|
|
|
|
seckey: PrivateKey(scheme: PKScheme.RSA, rsakey: pair.seckey),
|
|
|
|
pubkey: PublicKey(scheme: PKScheme.RSA, rsakey: pair.pubkey),
|
|
|
|
)
|
2024-06-11 15:18:06 +00:00
|
|
|
)
|
2020-07-21 20:10:21 +00:00
|
|
|
elif supported(PKScheme.ECDSA):
|
|
|
|
let pair = ?EcKeyPair.random(Secp256r1, rng).orError(KeyError)
|
|
|
|
ok(
|
|
|
|
KeyPair(
|
|
|
|
seckey: PrivateKey(scheme: PKScheme.ECDSA, eckey: pair.seckey),
|
|
|
|
pubkey: PublicKey(scheme: PKScheme.ECDSA, eckey: pair.pubkey),
|
|
|
|
)
|
2024-06-11 15:18:06 +00:00
|
|
|
)
|
2020-05-18 05:25:55 +00:00
|
|
|
else:
|
|
|
|
err(SchemeError)
|
|
|
|
|
2021-09-02 10:03:40 +00:00
|
|
|
proc getPublicKey*(key: PrivateKey): CryptoResult[PublicKey] =
|
2019-03-02 19:19:41 +00:00
|
|
|
## Get public key from corresponding private key ``key``.
|
2020-05-18 05:25:55 +00:00
|
|
|
case key.scheme
|
2020-07-21 20:10:21 +00:00
|
|
|
of PKScheme.RSA:
|
|
|
|
when supported(PKScheme.RSA):
|
2021-09-02 10:03:40 +00:00
|
|
|
let rsakey = key.rsakey.getPublicKey()
|
2020-07-21 20:10:21 +00:00
|
|
|
ok(PublicKey(scheme: RSA, rsakey: rsakey))
|
|
|
|
else:
|
|
|
|
err(SchemeError)
|
|
|
|
of PKScheme.Ed25519:
|
|
|
|
when supported(PKScheme.Ed25519):
|
2021-09-02 10:03:40 +00:00
|
|
|
let edkey = key.edkey.getPublicKey()
|
2020-07-21 20:10:21 +00:00
|
|
|
ok(PublicKey(scheme: Ed25519, edkey: edkey))
|
|
|
|
else:
|
|
|
|
err(SchemeError)
|
|
|
|
of PKScheme.ECDSA:
|
|
|
|
when supported(PKScheme.ECDSA):
|
2021-09-02 10:03:40 +00:00
|
|
|
let eckey = ?key.eckey.getPublicKey().orError(KeyError)
|
2020-07-21 20:10:21 +00:00
|
|
|
ok(PublicKey(scheme: ECDSA, eckey: eckey))
|
|
|
|
else:
|
|
|
|
err(SchemeError)
|
|
|
|
of PKScheme.Secp256k1:
|
|
|
|
when supported(PKScheme.Secp256k1):
|
2021-09-02 10:03:40 +00:00
|
|
|
let skkey = key.skkey.getPublicKey()
|
2020-07-21 20:10:21 +00:00
|
|
|
ok(PublicKey(scheme: Secp256k1, skkey: skkey))
|
|
|
|
else:
|
|
|
|
err(SchemeError)
|
2019-03-02 19:19:41 +00:00
|
|
|
|
2020-07-07 13:48:15 +00:00
|
|
|
proc toRawBytes*(
|
|
|
|
key: PrivateKey | PublicKey, data: var openArray[byte]
|
2021-12-16 10:05:20 +00:00
|
|
|
): CryptoResult[int] =
|
2019-03-02 19:19:41 +00:00
|
|
|
## Serialize private key ``key`` (using scheme's own serialization) and store
|
|
|
|
## it to ``data``.
|
|
|
|
##
|
|
|
|
## Returns number of bytes (octets) needed to store private key ``key``.
|
2020-05-18 05:25:55 +00:00
|
|
|
case key.scheme
|
2020-07-21 20:10:21 +00:00
|
|
|
of PKScheme.RSA:
|
|
|
|
when supported(PKScheme.RSA):
|
|
|
|
key.rsakey.toBytes(data).orError(KeyError)
|
|
|
|
else:
|
|
|
|
err(SchemeError)
|
|
|
|
of PKScheme.Ed25519:
|
|
|
|
when supported(PKScheme.Ed25519):
|
|
|
|
ok(key.edkey.toBytes(data))
|
|
|
|
else:
|
|
|
|
err(SchemeError)
|
|
|
|
of PKScheme.ECDSA:
|
|
|
|
when supported(PKScheme.ECDSA):
|
|
|
|
key.eckey.toBytes(data).orError(KeyError)
|
|
|
|
else:
|
|
|
|
err(SchemeError)
|
|
|
|
of PKScheme.Secp256k1:
|
|
|
|
when supported(PKScheme.Secp256k1):
|
|
|
|
key.skkey.toBytes(data).orError(KeyError)
|
|
|
|
else:
|
|
|
|
err(SchemeError)
|
2019-03-02 19:19:41 +00:00
|
|
|
|
2020-05-18 05:25:55 +00:00
|
|
|
proc getRawBytes*(key: PrivateKey | PublicKey): CryptoResult[seq[byte]] =
|
2019-03-02 19:19:41 +00:00
|
|
|
## Return private key ``key`` in binary form (using scheme's own
|
|
|
|
## serialization).
|
2020-05-18 05:25:55 +00:00
|
|
|
case key.scheme
|
2020-07-21 20:10:21 +00:00
|
|
|
of PKScheme.RSA:
|
|
|
|
when supported(PKScheme.RSA):
|
|
|
|
key.rsakey.getBytes().orError(KeyError)
|
|
|
|
else:
|
|
|
|
err(SchemeError)
|
|
|
|
of PKScheme.Ed25519:
|
|
|
|
when supported(PKScheme.Ed25519):
|
|
|
|
ok(key.edkey.getBytes())
|
|
|
|
else:
|
|
|
|
err(SchemeError)
|
|
|
|
of PKScheme.ECDSA:
|
|
|
|
when supported(PKScheme.ECDSA):
|
|
|
|
key.eckey.getBytes().orError(KeyError)
|
|
|
|
else:
|
|
|
|
err(SchemeError)
|
|
|
|
of PKScheme.Secp256k1:
|
|
|
|
when supported(PKScheme.Secp256k1):
|
|
|
|
ok(key.skkey.getBytes())
|
|
|
|
else:
|
|
|
|
err(SchemeError)
|
2019-03-02 19:19:41 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc toBytes*(key: PrivateKey, data: var openArray[byte]): CryptoResult[int] =
|
2019-03-02 19:19:41 +00:00
|
|
|
## Serialize private key ``key`` (using libp2p protobuf scheme) and store
|
|
|
|
## it to ``data``.
|
|
|
|
##
|
|
|
|
## Returns number of bytes (octets) needed to store private key ``key``.
|
|
|
|
var msg = initProtoBuffer()
|
2020-07-13 12:43:07 +00:00
|
|
|
msg.write(1, uint64(key.scheme))
|
|
|
|
msg.write(2, ?key.getRawBytes())
|
2019-03-02 19:19:41 +00:00
|
|
|
msg.finish()
|
2020-05-18 05:25:55 +00:00
|
|
|
var blen = len(msg.buffer)
|
|
|
|
if len(data) >= blen:
|
|
|
|
copyMem(addr data[0], addr msg.buffer[0], blen)
|
|
|
|
ok(blen)
|
2019-03-02 19:19:41 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc toBytes*(key: PublicKey, data: var openArray[byte]): CryptoResult[int] =
|
2019-03-02 19:19:41 +00:00
|
|
|
## Serialize public key ``key`` (using libp2p protobuf scheme) and store
|
|
|
|
## it to ``data``.
|
|
|
|
##
|
|
|
|
## Returns number of bytes (octets) needed to store public key ``key``.
|
|
|
|
var msg = initProtoBuffer()
|
2020-07-13 12:43:07 +00:00
|
|
|
msg.write(1, uint64(key.scheme))
|
|
|
|
msg.write(2, ?key.getRawBytes())
|
2019-03-02 19:19:41 +00:00
|
|
|
msg.finish()
|
2020-05-18 05:25:55 +00:00
|
|
|
var blen = len(msg.buffer)
|
|
|
|
if len(data) >= blen and blen > 0:
|
|
|
|
copyMem(addr data[0], addr msg.buffer[0], blen)
|
|
|
|
ok(blen)
|
2019-03-02 19:19:41 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc toBytes*(sig: Signature, data: var openArray[byte]): int =
|
2019-03-06 11:23:12 +00:00
|
|
|
## Serialize signature ``sig`` and store it to ``data``.
|
|
|
|
##
|
|
|
|
## Returns number of bytes (octets) needed to store signature ``sig``.
|
|
|
|
result = len(sig.data)
|
2020-05-18 05:25:55 +00:00
|
|
|
if len(data) >= result and result > 0:
|
2019-03-06 11:23:12 +00:00
|
|
|
copyMem(addr data[0], unsafeAddr sig.data[0], len(sig.data))
|
|
|
|
|
2020-05-18 05:25:55 +00:00
|
|
|
proc getBytes*(key: PrivateKey): CryptoResult[seq[byte]] =
|
2019-03-02 19:19:41 +00:00
|
|
|
## Return private key ``key`` in binary form (using libp2p's protobuf
|
|
|
|
## serialization).
|
|
|
|
var msg = initProtoBuffer()
|
2020-07-13 12:43:07 +00:00
|
|
|
msg.write(1, uint64(key.scheme))
|
|
|
|
msg.write(2, ?key.getRawBytes())
|
2019-03-02 19:19:41 +00:00
|
|
|
msg.finish()
|
2020-05-18 05:25:55 +00:00
|
|
|
ok(msg.buffer)
|
2019-03-02 19:19:41 +00:00
|
|
|
|
2020-05-18 05:25:55 +00:00
|
|
|
proc getBytes*(key: PublicKey): CryptoResult[seq[byte]] =
|
2019-03-02 19:19:41 +00:00
|
|
|
## Return public key ``key`` in binary form (using libp2p's protobuf
|
|
|
|
## serialization).
|
|
|
|
var msg = initProtoBuffer()
|
2020-07-13 12:43:07 +00:00
|
|
|
msg.write(1, uint64(key.scheme))
|
|
|
|
msg.write(2, ?key.getRawBytes())
|
2019-03-02 19:19:41 +00:00
|
|
|
msg.finish()
|
2020-05-18 05:25:55 +00:00
|
|
|
ok(msg.buffer)
|
2019-03-02 19:19:41 +00:00
|
|
|
|
2019-03-06 11:23:12 +00:00
|
|
|
proc getBytes*(sig: Signature): seq[byte] =
|
|
|
|
## Return signature ``sig`` in binary form.
|
|
|
|
result = sig.data
|
|
|
|
|
2023-06-29 13:28:25 +00:00
|
|
|
template initImpl[T: PrivateKey | PublicKey](key: var T, data: openArray[byte]): bool =
|
2019-03-02 19:19:41 +00:00
|
|
|
## Initialize private key ``key`` from libp2p's protobuf serialized raw
|
|
|
|
## binary form.
|
|
|
|
##
|
|
|
|
## Returns ``true`` on success.
|
|
|
|
var id: uint64
|
|
|
|
var buffer: seq[byte]
|
2020-07-21 20:10:21 +00:00
|
|
|
if len(data) <= 0:
|
|
|
|
false
|
|
|
|
else:
|
2019-03-02 19:19:41 +00:00
|
|
|
var pb = initProtoBuffer(@data)
|
2020-07-15 08:25:39 +00:00
|
|
|
let r1 = pb.getField(1, id)
|
|
|
|
let r2 = pb.getField(2, buffer)
|
2023-06-28 14:44:58 +00:00
|
|
|
if not (r1.get(false) and r2.get(false)):
|
2020-07-21 20:10:21 +00:00
|
|
|
false
|
|
|
|
else:
|
|
|
|
if cast[int8](id) notin SupportedSchemesInt or len(buffer) <= 0:
|
|
|
|
false
|
|
|
|
else:
|
2020-07-13 12:43:07 +00:00
|
|
|
var scheme = cast[PKScheme](cast[int8](id))
|
|
|
|
when key is PrivateKey:
|
|
|
|
var nkey = PrivateKey(scheme: scheme)
|
|
|
|
else:
|
|
|
|
var nkey = PublicKey(scheme: scheme)
|
|
|
|
case scheme
|
|
|
|
of PKScheme.RSA:
|
2020-07-21 20:10:21 +00:00
|
|
|
when supported(PKScheme.RSA):
|
|
|
|
if init(nkey.rsakey, buffer).isOk:
|
|
|
|
key = nkey
|
|
|
|
true
|
|
|
|
else:
|
|
|
|
false
|
|
|
|
else:
|
|
|
|
false
|
2020-07-13 12:43:07 +00:00
|
|
|
of PKScheme.Ed25519:
|
2020-07-21 20:10:21 +00:00
|
|
|
when supported(PKScheme.Ed25519):
|
|
|
|
if init(nkey.edkey, buffer):
|
|
|
|
key = nkey
|
|
|
|
true
|
|
|
|
else:
|
|
|
|
false
|
|
|
|
else:
|
|
|
|
false
|
2020-07-13 12:43:07 +00:00
|
|
|
of PKScheme.ECDSA:
|
2020-07-21 20:10:21 +00:00
|
|
|
when supported(PKScheme.ECDSA):
|
|
|
|
if init(nkey.eckey, buffer).isOk:
|
|
|
|
key = nkey
|
|
|
|
true
|
|
|
|
else:
|
|
|
|
false
|
|
|
|
else:
|
|
|
|
false
|
2020-07-13 12:43:07 +00:00
|
|
|
of PKScheme.Secp256k1:
|
2020-07-21 20:10:21 +00:00
|
|
|
when supported(PKScheme.Secp256k1):
|
|
|
|
if init(nkey.skkey, buffer).isOk:
|
|
|
|
key = nkey
|
|
|
|
true
|
|
|
|
else:
|
|
|
|
false
|
|
|
|
else:
|
|
|
|
false
|
2019-03-02 19:19:41 +00:00
|
|
|
|
2023-06-29 13:28:25 +00:00
|
|
|
{.push warning[ProveField]: off.} # https://github.com/nim-lang/Nim/issues/22060
|
|
|
|
proc init*(key: var PrivateKey, data: openArray[byte]): bool =
|
|
|
|
initImpl(key, data)
|
|
|
|
|
|
|
|
proc init*(key: var PublicKey, data: openArray[byte]): bool =
|
|
|
|
initImpl(key, data)
|
2024-06-11 15:18:06 +00:00
|
|
|
|
2023-06-29 13:28:25 +00:00
|
|
|
{.pop.}
|
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc init*(sig: var Signature, data: openArray[byte]): bool =
|
2019-03-06 11:23:12 +00:00
|
|
|
## Initialize signature ``sig`` from raw binary form.
|
|
|
|
##
|
|
|
|
## Returns ``true`` on success.
|
|
|
|
if len(data) > 0:
|
|
|
|
sig.data = @data
|
|
|
|
result = true
|
|
|
|
|
2020-07-07 13:48:15 +00:00
|
|
|
proc init*[T: PrivateKey | PublicKey](key: var T, data: string): bool =
|
|
|
|
## Initialize private/public key ``key`` from libp2p's protobuf serialized
|
2019-03-02 19:19:41 +00:00
|
|
|
## hexadecimal string representation.
|
|
|
|
##
|
|
|
|
## Returns ``true`` on success.
|
2020-08-11 14:48:21 +00:00
|
|
|
key.init(ncrutils.fromHex(data))
|
2019-03-02 19:19:41 +00:00
|
|
|
|
2019-03-06 11:23:12 +00:00
|
|
|
proc init*(sig: var Signature, data: string): bool =
|
|
|
|
## Initialize signature ``sig`` from serialized hexadecimal string
|
|
|
|
## representation.
|
|
|
|
##
|
|
|
|
## Returns ``true`` on success.
|
2020-08-11 14:48:21 +00:00
|
|
|
sig.init(ncrutils.fromHex(data))
|
2019-03-06 11:23:12 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc init*(t: typedesc[PrivateKey], data: openArray[byte]): CryptoResult[PrivateKey] =
|
2019-03-02 19:19:41 +00:00
|
|
|
## Create new private key from libp2p's protobuf serialized binary form.
|
2020-05-18 05:25:55 +00:00
|
|
|
var res: t
|
|
|
|
if not res.init(data):
|
|
|
|
err(KeyError)
|
|
|
|
else:
|
|
|
|
ok(res)
|
2019-03-02 19:19:41 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc init*(t: typedesc[PublicKey], data: openArray[byte]): CryptoResult[PublicKey] =
|
2019-03-02 19:19:41 +00:00
|
|
|
## Create new public key from libp2p's protobuf serialized binary form.
|
2020-05-18 05:25:55 +00:00
|
|
|
var res: t
|
|
|
|
if not res.init(data):
|
|
|
|
err(KeyError)
|
|
|
|
else:
|
|
|
|
ok(res)
|
2019-03-02 19:19:41 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc init*(t: typedesc[Signature], data: openArray[byte]): CryptoResult[Signature] =
|
2019-03-06 11:23:12 +00:00
|
|
|
## Create new public key from libp2p's protobuf serialized binary form.
|
2020-05-18 05:25:55 +00:00
|
|
|
var res: t
|
|
|
|
if not res.init(data):
|
|
|
|
err(SigError)
|
|
|
|
else:
|
|
|
|
ok(res)
|
2019-03-06 11:23:12 +00:00
|
|
|
|
2020-05-18 05:25:55 +00:00
|
|
|
proc init*(t: typedesc[PrivateKey], data: string): CryptoResult[PrivateKey] =
|
2019-03-02 19:19:41 +00:00
|
|
|
## Create new private key from libp2p's protobuf serialized hexadecimal string
|
|
|
|
## form.
|
2020-08-11 14:48:21 +00:00
|
|
|
t.init(ncrutils.fromHex(data))
|
2019-03-02 19:19:41 +00:00
|
|
|
|
2020-07-21 20:10:21 +00:00
|
|
|
when supported(PKScheme.RSA):
|
|
|
|
proc init*(t: typedesc[PrivateKey], key: rsa.RsaPrivateKey): PrivateKey =
|
|
|
|
PrivateKey(scheme: RSA, rsakey: key)
|
2024-06-11 15:18:06 +00:00
|
|
|
|
2020-07-21 20:10:21 +00:00
|
|
|
proc init*(t: typedesc[PublicKey], key: rsa.RsaPublicKey): PublicKey =
|
|
|
|
PublicKey(scheme: RSA, rsakey: key)
|
|
|
|
|
|
|
|
when supported(PKScheme.Ed25519):
|
|
|
|
proc init*(t: typedesc[PrivateKey], key: EdPrivateKey): PrivateKey =
|
|
|
|
PrivateKey(scheme: Ed25519, edkey: key)
|
2024-06-11 15:18:06 +00:00
|
|
|
|
2020-07-21 20:10:21 +00:00
|
|
|
proc init*(t: typedesc[PublicKey], key: EdPublicKey): PublicKey =
|
|
|
|
PublicKey(scheme: Ed25519, edkey: key)
|
|
|
|
|
|
|
|
when supported(PKScheme.Secp256k1):
|
|
|
|
proc init*(t: typedesc[PrivateKey], key: SkPrivateKey): PrivateKey =
|
|
|
|
PrivateKey(scheme: Secp256k1, skkey: key)
|
2024-06-11 15:18:06 +00:00
|
|
|
|
2020-07-21 20:10:21 +00:00
|
|
|
proc init*(t: typedesc[PublicKey], key: SkPublicKey): PublicKey =
|
|
|
|
PublicKey(scheme: Secp256k1, skkey: key)
|
|
|
|
|
|
|
|
when supported(PKScheme.ECDSA):
|
|
|
|
proc init*(t: typedesc[PrivateKey], key: ecnist.EcPrivateKey): PrivateKey =
|
|
|
|
PrivateKey(scheme: ECDSA, eckey: key)
|
2024-06-11 15:18:06 +00:00
|
|
|
|
2020-07-21 20:10:21 +00:00
|
|
|
proc init*(t: typedesc[PublicKey], key: ecnist.EcPublicKey): PublicKey =
|
|
|
|
PublicKey(scheme: ECDSA, eckey: key)
|
2020-07-09 08:59:09 +00:00
|
|
|
|
2020-05-18 05:25:55 +00:00
|
|
|
proc init*(t: typedesc[PublicKey], data: string): CryptoResult[PublicKey] =
|
2019-03-02 19:19:41 +00:00
|
|
|
## Create new public key from libp2p's protobuf serialized hexadecimal string
|
|
|
|
## form.
|
2020-08-11 14:48:21 +00:00
|
|
|
t.init(ncrutils.fromHex(data))
|
2019-03-02 19:19:41 +00:00
|
|
|
|
2020-05-18 05:25:55 +00:00
|
|
|
proc init*(t: typedesc[Signature], data: string): CryptoResult[Signature] =
|
2019-03-06 11:23:12 +00:00
|
|
|
## Create new signature from serialized hexadecimal string form.
|
2020-08-11 14:48:21 +00:00
|
|
|
t.init(ncrutils.fromHex(data))
|
2019-03-06 11:23:12 +00:00
|
|
|
|
2020-07-07 13:48:15 +00:00
|
|
|
proc `==`*(key1, key2: PublicKey): bool {.inline.} =
|
2019-03-02 19:19:41 +00:00
|
|
|
## Return ``true`` if two public keys ``key1`` and ``key2`` of the same
|
|
|
|
## scheme and equal.
|
|
|
|
if key1.scheme == key2.scheme:
|
2020-07-07 13:48:15 +00:00
|
|
|
case key1.scheme
|
|
|
|
of PKScheme.RSA:
|
2020-07-21 20:10:21 +00:00
|
|
|
when supported(PKScheme.RSA):
|
|
|
|
(key1.rsakey == key2.rsakey)
|
|
|
|
else:
|
|
|
|
false
|
2020-07-07 13:48:15 +00:00
|
|
|
of PKScheme.Ed25519:
|
2020-07-21 20:10:21 +00:00
|
|
|
when supported(PKScheme.Ed25519):
|
|
|
|
(key1.edkey == key2.edkey)
|
|
|
|
else:
|
|
|
|
false
|
2020-07-07 13:48:15 +00:00
|
|
|
of PKScheme.ECDSA:
|
2020-07-21 20:10:21 +00:00
|
|
|
when supported(PKScheme.ECDSA):
|
|
|
|
(key1.eckey == key2.eckey)
|
|
|
|
else:
|
|
|
|
false
|
2020-07-07 13:48:15 +00:00
|
|
|
of PKScheme.Secp256k1:
|
2020-07-21 20:10:21 +00:00
|
|
|
when supported(PKScheme.Secp256k1):
|
|
|
|
(key1.skkey == key2.skkey)
|
|
|
|
else:
|
|
|
|
false
|
2020-07-07 13:48:15 +00:00
|
|
|
else:
|
|
|
|
false
|
2019-03-02 19:19:41 +00:00
|
|
|
|
|
|
|
proc `==`*(key1, key2: PrivateKey): bool =
|
|
|
|
## Return ``true`` if two private keys ``key1`` and ``key2`` of the same
|
|
|
|
## scheme and equal.
|
|
|
|
if key1.scheme == key2.scheme:
|
2020-07-07 13:48:15 +00:00
|
|
|
case key1.scheme
|
|
|
|
of PKScheme.RSA:
|
2020-07-21 20:10:21 +00:00
|
|
|
when supported(PKScheme.RSA):
|
|
|
|
(key1.rsakey == key2.rsakey)
|
|
|
|
else:
|
|
|
|
false
|
2020-07-07 13:48:15 +00:00
|
|
|
of PKScheme.Ed25519:
|
2020-07-21 20:10:21 +00:00
|
|
|
when supported(PKScheme.Ed25519):
|
|
|
|
(key1.edkey == key2.edkey)
|
|
|
|
else:
|
|
|
|
false
|
2020-07-07 13:48:15 +00:00
|
|
|
of PKScheme.ECDSA:
|
2020-07-21 20:10:21 +00:00
|
|
|
when supported(PKScheme.ECDSA):
|
|
|
|
(key1.eckey == key2.eckey)
|
|
|
|
else:
|
|
|
|
false
|
2020-07-07 13:48:15 +00:00
|
|
|
of PKScheme.Secp256k1:
|
2020-07-21 20:10:21 +00:00
|
|
|
when supported(PKScheme.Secp256k1):
|
|
|
|
(key1.skkey == key2.skkey)
|
|
|
|
else:
|
|
|
|
false
|
2020-07-07 13:48:15 +00:00
|
|
|
else:
|
|
|
|
false
|
|
|
|
|
|
|
|
proc `$`*(key: PrivateKey | PublicKey): string =
|
|
|
|
## Get string representation of private/public key ``key``.
|
|
|
|
case key.scheme
|
|
|
|
of PKScheme.RSA:
|
2020-07-21 20:10:21 +00:00
|
|
|
when supported(PKScheme.RSA):
|
|
|
|
$(key.rsakey)
|
|
|
|
else:
|
|
|
|
"unsupported RSA key"
|
2020-07-07 13:48:15 +00:00
|
|
|
of PKScheme.Ed25519:
|
2020-07-21 20:10:21 +00:00
|
|
|
when supported(PKScheme.Ed25519):
|
|
|
|
"ed25519 key (" & $key.edkey & ")"
|
|
|
|
else:
|
|
|
|
"unsupported ed25519 key"
|
2020-07-07 13:48:15 +00:00
|
|
|
of PKScheme.ECDSA:
|
2020-07-21 20:10:21 +00:00
|
|
|
when supported(PKScheme.ECDSA):
|
|
|
|
"secp256r1 key (" & $key.eckey & ")"
|
|
|
|
else:
|
|
|
|
"unsupported secp256r1 key"
|
2020-07-07 13:48:15 +00:00
|
|
|
of PKScheme.Secp256k1:
|
2020-07-21 20:10:21 +00:00
|
|
|
when supported(PKScheme.Secp256k1):
|
|
|
|
"secp256k1 key (" & $key.skkey & ")"
|
|
|
|
else:
|
|
|
|
"unsupported secp256k1 key"
|
2020-07-07 13:48:15 +00:00
|
|
|
|
|
|
|
func shortLog*(key: PrivateKey | PublicKey): string =
|
|
|
|
## Get short string representation of private/public key ``key``.
|
|
|
|
case key.scheme
|
|
|
|
of PKScheme.RSA:
|
2020-07-21 20:10:21 +00:00
|
|
|
when supported(PKScheme.RSA):
|
|
|
|
($key.rsakey).shortLog
|
|
|
|
else:
|
|
|
|
"unsupported RSA key"
|
2020-07-07 13:48:15 +00:00
|
|
|
of PKScheme.Ed25519:
|
2020-07-21 20:10:21 +00:00
|
|
|
when supported(PKScheme.Ed25519):
|
|
|
|
"ed25519 key (" & ($key.edkey).shortLog & ")"
|
|
|
|
else:
|
|
|
|
"unsupported ed25519 key"
|
2020-07-07 13:48:15 +00:00
|
|
|
of PKScheme.ECDSA:
|
2020-07-21 20:10:21 +00:00
|
|
|
when supported(PKScheme.ECDSA):
|
|
|
|
"secp256r1 key (" & ($key.eckey).shortLog & ")"
|
|
|
|
else:
|
|
|
|
"unsupported secp256r1 key"
|
2020-07-07 13:48:15 +00:00
|
|
|
of PKScheme.Secp256k1:
|
2020-07-21 20:10:21 +00:00
|
|
|
when supported(PKScheme.Secp256k1):
|
|
|
|
"secp256k1 key (" & ($key.skkey).shortLog & ")"
|
|
|
|
else:
|
|
|
|
"unsupported secp256k1 key"
|
2020-03-23 06:03:36 +00:00
|
|
|
|
2019-03-06 11:23:12 +00:00
|
|
|
proc `$`*(sig: Signature): string =
|
|
|
|
## Get string representation of signature ``sig``.
|
2020-08-11 14:48:21 +00:00
|
|
|
result = ncrutils.toHex(sig.data)
|
2019-03-06 11:23:12 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc sign*(key: PrivateKey, data: openArray[byte]): CryptoResult[Signature] {.gcsafe.} =
|
2019-03-02 19:19:41 +00:00
|
|
|
## Sign message ``data`` using private key ``key`` and return generated
|
|
|
|
## signature in raw binary form.
|
2020-05-18 05:25:55 +00:00
|
|
|
var res: Signature
|
2020-07-07 13:48:15 +00:00
|
|
|
case key.scheme
|
|
|
|
of PKScheme.RSA:
|
2020-07-21 20:10:21 +00:00
|
|
|
when supported(PKScheme.RSA):
|
|
|
|
let sig = ?key.rsakey.sign(data).orError(SigError)
|
|
|
|
res.data = ?sig.getBytes().orError(SigError)
|
|
|
|
ok(res)
|
|
|
|
else:
|
|
|
|
err(SchemeError)
|
2020-07-07 13:48:15 +00:00
|
|
|
of PKScheme.Ed25519:
|
2020-07-21 20:10:21 +00:00
|
|
|
when supported(PKScheme.Ed25519):
|
|
|
|
let sig = key.edkey.sign(data)
|
|
|
|
res.data = sig.getBytes()
|
|
|
|
ok(res)
|
|
|
|
else:
|
|
|
|
err(SchemeError)
|
2020-07-07 13:48:15 +00:00
|
|
|
of PKScheme.ECDSA:
|
2020-07-21 20:10:21 +00:00
|
|
|
when supported(PKScheme.ECDSA):
|
|
|
|
let sig = ?key.eckey.sign(data).orError(SigError)
|
|
|
|
res.data = ?sig.getBytes().orError(SigError)
|
|
|
|
ok(res)
|
|
|
|
else:
|
|
|
|
err(SchemeError)
|
2020-07-07 13:48:15 +00:00
|
|
|
of PKScheme.Secp256k1:
|
2020-07-21 20:10:21 +00:00
|
|
|
when supported(PKScheme.Secp256k1):
|
|
|
|
let sig = key.skkey.sign(data)
|
|
|
|
res.data = sig.getBytes()
|
|
|
|
ok(res)
|
|
|
|
else:
|
|
|
|
err(SchemeError)
|
2019-03-02 19:19:41 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc verify*(sig: Signature, message: openArray[byte], key: PublicKey): bool =
|
2019-03-02 19:19:41 +00:00
|
|
|
## Verify signature ``sig`` using message ``message`` and public key ``key``.
|
|
|
|
## Return ``true`` if message signature is valid.
|
2020-07-07 13:48:15 +00:00
|
|
|
case key.scheme
|
|
|
|
of PKScheme.RSA:
|
2020-07-21 20:10:21 +00:00
|
|
|
when supported(PKScheme.RSA):
|
|
|
|
var signature: RsaSignature
|
|
|
|
if signature.init(sig.data).isOk:
|
|
|
|
signature.verify(message, key.rsakey)
|
|
|
|
else:
|
|
|
|
false
|
2020-07-07 13:48:15 +00:00
|
|
|
else:
|
|
|
|
false
|
|
|
|
of PKScheme.Ed25519:
|
2020-07-21 20:10:21 +00:00
|
|
|
when supported(PKScheme.Ed25519):
|
|
|
|
var signature: EdSignature
|
|
|
|
if signature.init(sig.data):
|
|
|
|
signature.verify(message, key.edkey)
|
|
|
|
else:
|
|
|
|
false
|
2020-07-07 13:48:15 +00:00
|
|
|
else:
|
|
|
|
false
|
|
|
|
of PKScheme.ECDSA:
|
2020-07-21 20:10:21 +00:00
|
|
|
when supported(PKScheme.ECDSA):
|
|
|
|
var signature: EcSignature
|
|
|
|
if signature.init(sig.data).isOk:
|
|
|
|
signature.verify(message, key.eckey)
|
|
|
|
else:
|
|
|
|
false
|
2020-07-07 13:48:15 +00:00
|
|
|
else:
|
|
|
|
false
|
|
|
|
of PKScheme.Secp256k1:
|
2020-07-21 20:10:21 +00:00
|
|
|
when supported(PKScheme.Secp256k1):
|
|
|
|
var signature: SkSignature
|
|
|
|
if signature.init(sig.data).isOk:
|
|
|
|
signature.verify(message, key.skkey)
|
|
|
|
else:
|
|
|
|
false
|
2020-07-07 13:48:15 +00:00
|
|
|
else:
|
|
|
|
false
|
2019-03-02 19:19:41 +00:00
|
|
|
|
2019-09-11 16:03:39 +00:00
|
|
|
template makeSecret(buffer, hmactype, secret, seed: untyped) {.dirty.} =
|
2019-03-02 19:19:41 +00:00
|
|
|
var ctx: hmactype
|
|
|
|
var j = 0
|
|
|
|
# We need to strip leading zeros, because Go bigint serialization do it.
|
|
|
|
var offset = 0
|
|
|
|
for i in 0 ..< len(secret):
|
|
|
|
if secret[i] != 0x00'u8:
|
|
|
|
break
|
|
|
|
inc(offset)
|
2020-05-18 05:25:55 +00:00
|
|
|
ctx.init(secret.toOpenArray(offset, secret.high))
|
2019-03-02 19:19:41 +00:00
|
|
|
ctx.update(seed)
|
|
|
|
var a = ctx.finish()
|
|
|
|
while j < len(buffer):
|
2020-05-18 05:25:55 +00:00
|
|
|
ctx.init(secret.toOpenArray(offset, secret.high))
|
2019-03-02 19:19:41 +00:00
|
|
|
ctx.update(a.data)
|
|
|
|
ctx.update(seed)
|
|
|
|
var b = ctx.finish()
|
|
|
|
var todo = len(b.data)
|
|
|
|
if j + todo > len(buffer):
|
|
|
|
todo = len(buffer) - j
|
|
|
|
copyMem(addr buffer[j], addr b.data[0], todo)
|
|
|
|
j += todo
|
2020-05-18 05:25:55 +00:00
|
|
|
ctx.init(secret.toOpenArray(offset, secret.high))
|
2019-03-02 19:19:41 +00:00
|
|
|
ctx.update(a.data)
|
|
|
|
a = ctx.finish()
|
|
|
|
|
2019-09-11 16:03:39 +00:00
|
|
|
proc stretchKeys*(
|
|
|
|
cipherType: string, hashType: string, sharedSecret: seq[byte]
|
|
|
|
): Secret =
|
2019-03-02 19:19:41 +00:00
|
|
|
## Expand shared secret to cryptographic keys.
|
2019-09-11 16:03:39 +00:00
|
|
|
if cipherType == "AES-128":
|
2019-03-02 19:19:41 +00:00
|
|
|
result.ivsize = aes128.sizeBlock
|
|
|
|
result.keysize = aes128.sizeKey
|
2019-09-11 16:03:39 +00:00
|
|
|
elif cipherType == "AES-256":
|
2019-03-02 19:19:41 +00:00
|
|
|
result.ivsize = aes256.sizeBlock
|
|
|
|
result.keysize = aes256.sizeKey
|
2019-09-13 06:52:13 +00:00
|
|
|
elif cipherType == "TwofishCTR":
|
|
|
|
result.ivsize = twofish256.sizeBlock
|
|
|
|
result.keysize = twofish256.sizeKey
|
2019-03-02 19:19:41 +00:00
|
|
|
|
|
|
|
var seed = "key expansion"
|
|
|
|
result.macsize = 20
|
|
|
|
let length = result.ivsize + result.keysize + result.macsize
|
|
|
|
result.data = newSeq[byte](2 * length)
|
|
|
|
|
2019-09-11 16:03:39 +00:00
|
|
|
if hashType == "SHA256":
|
|
|
|
makeSecret(result.data, HMAC[sha256], sharedSecret, seed)
|
|
|
|
elif hashType == "SHA512":
|
|
|
|
makeSecret(result.data, HMAC[sha512], sharedSecret, seed)
|
2019-03-02 19:19:41 +00:00
|
|
|
|
|
|
|
template goffset*(secret, id, o: untyped): untyped =
|
|
|
|
id * (len(secret.data) shr 1) + o
|
|
|
|
|
|
|
|
template ivOpenArray*(secret: Secret, id: int): untyped =
|
|
|
|
toOpenArray(
|
|
|
|
secret.data, goffset(secret, id, 0), goffset(secret, id, secret.ivsize - 1)
|
2024-06-11 15:18:06 +00:00
|
|
|
)
|
2019-03-02 19:19:41 +00:00
|
|
|
|
|
|
|
template keyOpenArray*(secret: Secret, id: int): untyped =
|
|
|
|
toOpenArray(
|
|
|
|
secret.data,
|
|
|
|
goffset(secret, id, secret.ivsize),
|
|
|
|
goffset(secret, id, secret.ivsize + secret.keysize - 1),
|
|
|
|
)
|
|
|
|
|
|
|
|
template macOpenArray*(secret: Secret, id: int): untyped =
|
|
|
|
toOpenArray(
|
|
|
|
secret.data,
|
|
|
|
goffset(secret, id, secret.ivsize + secret.keysize),
|
|
|
|
goffset(secret, id, secret.ivsize + secret.keysize + secret.macsize - 1),
|
|
|
|
)
|
|
|
|
|
|
|
|
proc iv*(secret: Secret, id: int): seq[byte] {.inline.} =
|
|
|
|
## Get array of bytes with with initial vector.
|
|
|
|
result = newSeq[byte](secret.ivsize)
|
|
|
|
var offset =
|
|
|
|
if id == 0:
|
|
|
|
0
|
|
|
|
else:
|
|
|
|
(len(secret.data) div 2)
|
|
|
|
copyMem(addr result[0], unsafeAddr secret.data[offset], secret.ivsize)
|
|
|
|
|
|
|
|
proc key*(secret: Secret, id: int): seq[byte] {.inline.} =
|
|
|
|
result = newSeq[byte](secret.keysize)
|
|
|
|
var offset =
|
|
|
|
if id == 0:
|
|
|
|
0
|
|
|
|
else:
|
|
|
|
(len(secret.data) div 2)
|
|
|
|
offset += secret.ivsize
|
|
|
|
copyMem(addr result[0], unsafeAddr secret.data[offset], secret.keysize)
|
|
|
|
|
|
|
|
proc mac*(secret: Secret, id: int): seq[byte] {.inline.} =
|
|
|
|
result = newSeq[byte](secret.macsize)
|
|
|
|
var offset =
|
|
|
|
if id == 0:
|
|
|
|
0
|
|
|
|
else:
|
|
|
|
(len(secret.data) div 2)
|
|
|
|
offset += secret.ivsize + secret.keysize
|
|
|
|
copyMem(addr result[0], unsafeAddr secret.data[offset], secret.macsize)
|
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc getOrder*(
|
|
|
|
remotePubkey, localNonce: openArray[byte], localPubkey, remoteNonce: openArray[byte]
|
|
|
|
): CryptoResult[int] =
|
2019-09-09 16:57:17 +00:00
|
|
|
## Compare values and calculate `order` parameter.
|
|
|
|
var ctx: sha256
|
|
|
|
ctx.init()
|
|
|
|
ctx.update(remotePubkey)
|
|
|
|
ctx.update(localNonce)
|
|
|
|
var digest1 = ctx.finish()
|
|
|
|
ctx.init()
|
|
|
|
ctx.update(localPubkey)
|
|
|
|
ctx.update(remoteNonce)
|
|
|
|
var digest2 = ctx.finish()
|
2020-05-18 05:25:55 +00:00
|
|
|
var mh1 = ?MultiHash.init(multiCodec("sha2-256"), digest1).orError(HashError)
|
|
|
|
var mh2 = ?MultiHash.init(multiCodec("sha2-256"), digest2).orError(HashError)
|
|
|
|
var res = 0
|
2019-09-11 16:03:39 +00:00
|
|
|
for i in 0 ..< len(mh1.data.buffer):
|
2020-05-18 05:25:55 +00:00
|
|
|
res = int(mh1.data.buffer[i]) - int(mh2.data.buffer[i])
|
|
|
|
if res != 0:
|
|
|
|
if res < 0:
|
|
|
|
res = -1
|
|
|
|
elif res > 0:
|
|
|
|
res = 1
|
2019-09-11 16:03:39 +00:00
|
|
|
break
|
2020-05-18 05:25:55 +00:00
|
|
|
ok(res)
|
2019-09-09 16:57:17 +00:00
|
|
|
|
|
|
|
proc selectBest*(order: int, p1, p2: string): string =
|
|
|
|
## Determines which algorithm to use from list `p1` and `p2`.
|
|
|
|
##
|
|
|
|
## Returns empty string if there no algorithms in common.
|
|
|
|
var f, s: seq[string]
|
|
|
|
if order < 0:
|
|
|
|
f = strutils.split(p2, ",")
|
|
|
|
s = strutils.split(p1, ",")
|
|
|
|
elif order > 0:
|
|
|
|
f = strutils.split(p1, ",")
|
|
|
|
s = strutils.split(p2, ",")
|
|
|
|
else:
|
|
|
|
var p = strutils.split(p1, ",")
|
2020-07-21 20:10:21 +00:00
|
|
|
return p[0]
|
2019-09-09 16:57:17 +00:00
|
|
|
|
|
|
|
for felement in f:
|
|
|
|
for selement in s:
|
|
|
|
if felement == selement:
|
2020-07-21 20:10:21 +00:00
|
|
|
return felement
|
2019-09-09 16:57:17 +00:00
|
|
|
|
2019-03-06 11:23:12 +00:00
|
|
|
## Serialization/Deserialization helpers
|
|
|
|
|
2020-07-07 13:48:15 +00:00
|
|
|
proc write*(
|
|
|
|
vb: var VBuffer, pubkey: PublicKey
|
2023-06-07 11:12:49 +00:00
|
|
|
) {.inline, raises: [ResultError[CryptoError]].} =
|
2019-03-06 11:23:12 +00:00
|
|
|
## Write PublicKey value ``pubkey`` to buffer ``vb``.
|
2020-05-18 05:25:55 +00:00
|
|
|
vb.writeSeq(pubkey.getBytes().tryGet())
|
2019-03-06 11:23:12 +00:00
|
|
|
|
2020-07-07 13:48:15 +00:00
|
|
|
proc write*(
|
|
|
|
vb: var VBuffer, seckey: PrivateKey
|
2023-06-07 11:12:49 +00:00
|
|
|
) {.inline, raises: [ResultError[CryptoError]].} =
|
2019-03-06 11:23:12 +00:00
|
|
|
## Write PrivateKey value ``seckey`` to buffer ``vb``.
|
2020-05-18 05:25:55 +00:00
|
|
|
vb.writeSeq(seckey.getBytes().tryGet())
|
2019-03-06 11:23:12 +00:00
|
|
|
|
2020-07-07 13:48:15 +00:00
|
|
|
proc write*(
|
|
|
|
vb: var VBuffer, sig: PrivateKey
|
2023-06-07 11:12:49 +00:00
|
|
|
) {.inline, raises: [ResultError[CryptoError]].} =
|
2019-03-06 11:23:12 +00:00
|
|
|
## Write Signature value ``sig`` to buffer ``vb``.
|
2020-05-18 05:25:55 +00:00
|
|
|
vb.writeSeq(sig.getBytes().tryGet())
|
2019-03-06 11:23:12 +00:00
|
|
|
|
2020-07-13 12:43:07 +00:00
|
|
|
proc write*[T: PublicKey | PrivateKey](
|
|
|
|
pb: var ProtoBuffer, field: int, key: T
|
2023-06-07 11:12:49 +00:00
|
|
|
) {.inline, raises: [ResultError[CryptoError]].} =
|
2020-07-13 12:43:07 +00:00
|
|
|
write(pb, field, key.getBytes().tryGet())
|
|
|
|
|
|
|
|
proc write*(pb: var ProtoBuffer, field: int, sig: Signature) {.inline, raises: [].} =
|
|
|
|
write(pb, field, sig.getBytes())
|
2019-03-06 11:23:12 +00:00
|
|
|
|
2020-07-13 12:43:07 +00:00
|
|
|
proc getField*[T: PublicKey | PrivateKey](
|
|
|
|
pb: ProtoBuffer, field: int, value: var T
|
2020-07-15 08:25:39 +00:00
|
|
|
): ProtoResult[bool] =
|
|
|
|
## Deserialize public/private key from protobuf's message ``pb`` using field
|
|
|
|
## index ``field``.
|
|
|
|
##
|
|
|
|
## On success deserialized key will be stored in ``value``.
|
2020-07-13 12:43:07 +00:00
|
|
|
var buffer: seq[byte]
|
|
|
|
var key: T
|
2020-07-15 08:25:39 +00:00
|
|
|
let res = ?pb.getField(field, buffer)
|
|
|
|
if not (res):
|
|
|
|
ok(false)
|
2020-07-13 12:43:07 +00:00
|
|
|
else:
|
2020-07-15 08:25:39 +00:00
|
|
|
if key.init(buffer):
|
|
|
|
value = key
|
|
|
|
ok(true)
|
|
|
|
else:
|
|
|
|
err(ProtoError.IncorrectBlob)
|
2020-07-13 12:43:07 +00:00
|
|
|
|
2020-07-15 08:25:39 +00:00
|
|
|
proc getField*(pb: ProtoBuffer, field: int, value: var Signature): ProtoResult[bool] =
|
|
|
|
## Deserialize signature from protobuf's message ``pb`` using field index
|
|
|
|
## ``field``.
|
|
|
|
##
|
|
|
|
## On success deserialized signature will be stored in ``value``.
|
2020-07-13 12:43:07 +00:00
|
|
|
var buffer: seq[byte]
|
|
|
|
var sig: Signature
|
2020-07-15 08:25:39 +00:00
|
|
|
let res = ?pb.getField(field, buffer)
|
|
|
|
if not (res):
|
|
|
|
ok(false)
|
2020-07-13 12:43:07 +00:00
|
|
|
else:
|
2020-07-15 08:25:39 +00:00
|
|
|
if sig.init(buffer):
|
|
|
|
value = sig
|
|
|
|
ok(true)
|
|
|
|
else:
|
|
|
|
err(ProtoError.IncorrectBlob)
|