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-02-22 11:32:15 +00:00
|
|
|
|
2019-02-28 12:17:39 +00:00
|
|
|
## This module implements constant-time ECDSA and ECDHE for NIST elliptic
|
|
|
|
## curves secp256r1, secp384r1 and secp521r1.
|
|
|
|
##
|
|
|
|
## This module uses unmodified parts of code from
|
|
|
|
## BearSSL library <https://bearssl.org/>
|
|
|
|
## Copyright(C) 2018 Thomas Pornin <pornin@bolet.org>.
|
|
|
|
|
2023-06-07 11:12:49 +00:00
|
|
|
{.push raises: [].}
|
2020-05-18 05:25:55 +00:00
|
|
|
|
2022-06-16 08:08:52 +00:00
|
|
|
import bearssl/[ec, rand, hash]
|
2020-08-11 14:48:21 +00:00
|
|
|
# We use `ncrutils` for constant-time hexadecimal encoding/decoding procedures.
|
|
|
|
import nimcrypto/utils as ncrutils
|
2019-02-21 04:10:21 +00:00
|
|
|
import minasn1
|
2020-04-23 12:10:20 +00:00
|
|
|
export minasn1.Asn1Error
|
2020-08-08 06:53:33 +00:00
|
|
|
import stew/[results, ctops]
|
2023-05-18 08:24:17 +00:00
|
|
|
|
|
|
|
import ../utility
|
|
|
|
|
2020-04-23 12:10:20 +00:00
|
|
|
export results
|
2019-02-19 22:11:59 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
PubKey256Length* = 65
|
|
|
|
PubKey384Length* = 97
|
|
|
|
PubKey521Length* = 133
|
|
|
|
SecKey256Length* = 32
|
|
|
|
SecKey384Length* = 48
|
|
|
|
SecKey521Length* = 66
|
|
|
|
Sig256Length* = 64
|
|
|
|
Sig384Length* = 96
|
|
|
|
Sig521Length* = 132
|
2019-03-02 19:19:41 +00:00
|
|
|
Secret256Length* = SecKey256Length
|
|
|
|
Secret384Length* = SecKey384Length
|
|
|
|
Secret521Length* = SecKey521Length
|
2019-02-19 22:11:59 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
EcPrivateKey* = ref object
|
2022-06-16 08:08:52 +00:00
|
|
|
buffer*: array[EC_KBUF_PRIV_MAX_SIZE, byte]
|
|
|
|
key*: ec.EcPrivateKey
|
2019-02-19 22:11:59 +00:00
|
|
|
|
|
|
|
EcPublicKey* = ref object
|
2022-06-16 08:08:52 +00:00
|
|
|
buffer*: array[EC_KBUF_PUB_MAX_SIZE, byte]
|
|
|
|
key*: ec.EcPublicKey
|
2019-02-19 22:11:59 +00:00
|
|
|
|
|
|
|
EcKeyPair* = object
|
|
|
|
seckey*: EcPrivateKey
|
|
|
|
pubkey*: EcPublicKey
|
|
|
|
|
|
|
|
EcSignature* = ref object
|
|
|
|
buffer*: seq[byte]
|
|
|
|
|
|
|
|
EcCurveKind* = enum
|
2024-06-11 15:18:06 +00:00
|
|
|
Secp256r1 = EC_secp256r1
|
|
|
|
Secp384r1 = EC_secp384r1
|
2022-07-27 11:47:50 +00:00
|
|
|
Secp521r1 = EC_secp521r1
|
2019-02-19 22:11:59 +00:00
|
|
|
|
|
|
|
EcPKI* = EcPrivateKey | EcPublicKey | EcSignature
|
|
|
|
|
2020-05-18 05:25:55 +00:00
|
|
|
EcError* = enum
|
2024-06-11 15:18:06 +00:00
|
|
|
EcRngError
|
|
|
|
EcKeyGenError
|
|
|
|
EcPublicKeyError
|
|
|
|
EcKeyIncorrectError
|
2020-05-18 05:25:55 +00:00
|
|
|
EcSignatureError
|
|
|
|
|
|
|
|
EcResult*[T] = Result[T, EcError]
|
2019-02-19 22:11:59 +00:00
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
const EcSupportedCurvesCint* = @[cint(Secp256r1), cint(Secp384r1), cint(Secp521r1)]
|
2019-02-19 22:11:59 +00:00
|
|
|
|
|
|
|
proc `-`(x: uint32): uint32 {.inline.} =
|
|
|
|
result = (0xFFFF_FFFF'u32 - x) + 1'u32
|
|
|
|
|
|
|
|
proc GT(x, y: uint32): uint32 {.inline.} =
|
|
|
|
var z = cast[uint32](y - x)
|
|
|
|
result = (z xor ((x xor y) and (x xor z))) shr 31
|
|
|
|
|
|
|
|
proc CMP(x, y: uint32): int32 {.inline.} =
|
|
|
|
cast[int32](GT(x, y)) or -(cast[int32](GT(y, x)))
|
|
|
|
|
|
|
|
proc EQ0(x: int32): uint32 {.inline.} =
|
|
|
|
var q = cast[uint32](x)
|
2024-06-11 15:18:06 +00:00
|
|
|
result = not (q or -q) shr 31
|
2019-02-19 22:11:59 +00:00
|
|
|
|
|
|
|
proc NEQ(x, y: uint32): uint32 {.inline.} =
|
|
|
|
var q = cast[uint32](x xor y)
|
|
|
|
result = ((q or -q) shr 31)
|
|
|
|
|
|
|
|
proc LT0(x: int32): uint32 {.inline.} =
|
|
|
|
result = cast[uint32](x) shr 31
|
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc checkScalar(scalar: openArray[byte], curve: cint): uint32 =
|
2019-02-19 22:11:59 +00:00
|
|
|
## Return ``1`` if all of the following hold:
|
|
|
|
## - len(``scalar``) <= ``orderlen``
|
|
|
|
## - ``scalar`` != 0
|
|
|
|
## - ``scalar`` is lower than the curve ``order``.
|
|
|
|
##
|
|
|
|
## Otherwise, return ``0``.
|
2022-06-16 08:08:52 +00:00
|
|
|
var impl = ecGetDefault()
|
|
|
|
var orderlen: uint = 0
|
|
|
|
var order = cast[ptr UncheckedArray[byte]](impl.order(curve, orderlen))
|
2019-02-19 22:11:59 +00:00
|
|
|
|
|
|
|
var z = 0'u32
|
|
|
|
var c = 0'i32
|
|
|
|
for u in scalar:
|
|
|
|
z = z or u
|
2022-06-16 08:08:52 +00:00
|
|
|
if len(scalar) == int(orderlen):
|
2024-06-11 15:18:06 +00:00
|
|
|
for i in 0 ..< len(scalar):
|
2019-02-28 12:17:39 +00:00
|
|
|
c = c or (-(cast[int32](EQ0(c))) and CMP(scalar[i], order[i]))
|
2019-02-19 22:11:59 +00:00
|
|
|
else:
|
|
|
|
c = -1
|
|
|
|
result = NEQ(z, 0'u32) and LT0(c)
|
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc checkPublic(key: openArray[byte], curve: cint): uint32 =
|
2019-02-19 22:11:59 +00:00
|
|
|
## Return ``1`` if public key ``key`` is on curve.
|
|
|
|
var ckey = @key
|
2022-06-16 08:08:52 +00:00
|
|
|
var x = [byte 0x00, 0x01]
|
|
|
|
var impl = ecGetDefault()
|
|
|
|
var orderlen: uint = 0
|
|
|
|
discard impl.order(curve, orderlen)
|
2024-06-11 15:18:06 +00:00
|
|
|
result = impl.mul(unsafeAddr ckey[0], uint(len(ckey)), addr x[0], uint(len(x)), curve)
|
2019-02-19 22:11:59 +00:00
|
|
|
|
|
|
|
proc getOffset(pubkey: EcPublicKey): int {.inline.} =
|
|
|
|
let o = cast[uint](pubkey.key.q) - cast[uint](unsafeAddr pubkey.buffer[0])
|
|
|
|
if o + cast[uint](pubkey.key.qlen) > uint(len(pubkey.buffer)):
|
|
|
|
result = -1
|
|
|
|
else:
|
|
|
|
result = cast[int](o)
|
|
|
|
|
|
|
|
proc getOffset(seckey: EcPrivateKey): int {.inline.} =
|
|
|
|
let o = cast[uint](seckey.key.x) - cast[uint](unsafeAddr seckey.buffer[0])
|
|
|
|
if o + cast[uint](seckey.key.xlen) > uint(len(seckey.buffer)):
|
|
|
|
result = -1
|
|
|
|
else:
|
|
|
|
result = cast[int](o)
|
|
|
|
|
|
|
|
template getPublicKeyLength*(curve: EcCurveKind): int =
|
|
|
|
case curve
|
2024-06-11 15:18:06 +00:00
|
|
|
of Secp256r1: PubKey256Length
|
|
|
|
of Secp384r1: PubKey384Length
|
|
|
|
of Secp521r1: PubKey521Length
|
2019-02-19 22:11:59 +00:00
|
|
|
|
2019-09-11 16:03:39 +00:00
|
|
|
template getPrivateKeyLength*(curve: EcCurveKind): int =
|
|
|
|
case curve
|
2024-06-11 15:18:06 +00:00
|
|
|
of Secp256r1: SecKey256Length
|
|
|
|
of Secp384r1: SecKey384Length
|
|
|
|
of Secp521r1: SecKey521Length
|
2019-09-11 16:03:39 +00:00
|
|
|
|
2019-02-19 22:11:59 +00:00
|
|
|
proc copy*[T: EcPKI](dst: var T, src: T): bool =
|
2019-02-25 18:03:52 +00:00
|
|
|
## Copy EC `private key`, `public key` or `signature` ``src`` to ``dst``.
|
2019-02-19 22:11:59 +00:00
|
|
|
##
|
|
|
|
## Returns ``true`` on success, ``false`` otherwise.
|
2019-09-11 16:03:39 +00:00
|
|
|
if isNil(src):
|
|
|
|
result = false
|
|
|
|
else:
|
|
|
|
dst = new T
|
|
|
|
when T is EcPrivateKey:
|
|
|
|
let length = src.key.xlen
|
|
|
|
if length > 0 and len(src.buffer) > 0:
|
|
|
|
let offset = getOffset(src)
|
|
|
|
if offset >= 0:
|
|
|
|
dst.buffer = src.buffer
|
|
|
|
dst.key.curve = src.key.curve
|
|
|
|
dst.key.xlen = length
|
2022-06-16 08:08:52 +00:00
|
|
|
dst.key.x = addr dst.buffer[offset]
|
2019-09-11 16:03:39 +00:00
|
|
|
result = true
|
|
|
|
elif T is EcPublicKey:
|
|
|
|
let length = src.key.qlen
|
|
|
|
if length > 0 and len(src.buffer) > 0:
|
|
|
|
let offset = getOffset(src)
|
|
|
|
if offset >= 0:
|
|
|
|
dst.buffer = src.buffer
|
|
|
|
dst.key.curve = src.key.curve
|
|
|
|
dst.key.qlen = length
|
2022-06-16 08:08:52 +00:00
|
|
|
dst.key.q = addr dst.buffer[offset]
|
2019-09-11 16:03:39 +00:00
|
|
|
result = true
|
|
|
|
else:
|
|
|
|
let length = len(src.buffer)
|
|
|
|
if length > 0:
|
2019-02-19 22:11:59 +00:00
|
|
|
dst.buffer = src.buffer
|
|
|
|
result = true
|
|
|
|
|
|
|
|
proc copy*[T: EcPKI](src: T): T {.inline.} =
|
2019-02-25 18:03:52 +00:00
|
|
|
## Returns copy of EC `private key`, `public key` or `signature`
|
|
|
|
## object ``src``.
|
2019-02-19 22:11:59 +00:00
|
|
|
if not copy(result, src):
|
|
|
|
raise newException(EcKeyIncorrectError, "Incorrect key or signature")
|
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
proc clear*[T: EcPKI | EcKeyPair](pki: var T) =
|
2019-02-25 18:03:52 +00:00
|
|
|
## Wipe and clear EC `private key`, `public key` or `signature` object.
|
2019-09-11 16:03:39 +00:00
|
|
|
doAssert(not isNil(pki))
|
2019-02-19 22:11:59 +00:00
|
|
|
when T is EcPrivateKey:
|
|
|
|
burnMem(pki.buffer)
|
|
|
|
pki.buffer.setLen(0)
|
|
|
|
pki.key.x = nil
|
|
|
|
pki.key.xlen = 0
|
|
|
|
pki.key.curve = 0
|
|
|
|
elif T is EcPublicKey:
|
|
|
|
burnMem(pki.buffer)
|
|
|
|
pki.buffer.setLen(0)
|
|
|
|
pki.key.q = nil
|
|
|
|
pki.key.qlen = 0
|
|
|
|
pki.key.curve = 0
|
|
|
|
elif T is EcSignature:
|
|
|
|
burnMem(pki.buffer)
|
|
|
|
pki.buffer.setLen(0)
|
|
|
|
else:
|
|
|
|
burnMem(pki.seckey.buffer)
|
|
|
|
burnMem(pki.pubkey.buffer)
|
|
|
|
pki.seckey.buffer.setLen(0)
|
|
|
|
pki.pubkey.buffer.setLen(0)
|
|
|
|
pki.seckey.key.x = nil
|
|
|
|
pki.seckey.key.xlen = 0
|
|
|
|
pki.seckey.key.curve = 0
|
|
|
|
pki.pubkey.key.q = nil
|
|
|
|
pki.pubkey.key.qlen = 0
|
|
|
|
pki.pubkey.key.curve = 0
|
|
|
|
|
2020-07-07 11:14:11 +00:00
|
|
|
proc random*(
|
2024-06-11 15:18:06 +00:00
|
|
|
T: typedesc[EcPrivateKey], kind: EcCurveKind, rng: var HmacDrbgContext
|
|
|
|
): EcResult[EcPrivateKey] =
|
2019-02-19 22:11:59 +00:00
|
|
|
## Generate new random EC private key using BearSSL's HMAC-SHA256-DRBG
|
|
|
|
## algorithm.
|
|
|
|
##
|
|
|
|
## ``kind`` elliptic curve kind of your choice (secp256r1, secp384r1 or
|
|
|
|
## secp521r1).
|
2022-06-16 08:08:52 +00:00
|
|
|
var ecimp = ecGetDefault()
|
2020-05-18 05:25:55 +00:00
|
|
|
var res = new EcPrivateKey
|
2024-06-11 15:18:06 +00:00
|
|
|
if ecKeygen(
|
|
|
|
addr rng.vtable, ecimp, addr res.key, addr res.buffer[0], safeConvert[cint](kind)
|
|
|
|
) == 0:
|
2020-05-18 05:25:55 +00:00
|
|
|
err(EcKeyGenError)
|
|
|
|
else:
|
|
|
|
ok(res)
|
2019-02-19 22:11:59 +00:00
|
|
|
|
2021-09-02 10:03:40 +00:00
|
|
|
proc getPublicKey*(seckey: EcPrivateKey): EcResult[EcPublicKey] =
|
2019-02-19 22:11:59 +00:00
|
|
|
## Calculate and return EC public key from private key ``seckey``.
|
2020-05-18 05:25:55 +00:00
|
|
|
if isNil(seckey):
|
|
|
|
return err(EcKeyIncorrectError)
|
|
|
|
|
2022-06-16 08:08:52 +00:00
|
|
|
var ecimp = ecGetDefault()
|
2019-02-19 22:11:59 +00:00
|
|
|
if seckey.key.curve in EcSupportedCurvesCint:
|
2020-05-18 05:25:55 +00:00
|
|
|
var res = new EcPublicKey
|
2022-05-24 13:10:57 +00:00
|
|
|
assert res.buffer.len > getPublicKeyLength(cast[EcCurveKind](seckey.key.curve))
|
2024-06-11 15:18:06 +00:00
|
|
|
if ecComputePub(ecimp, addr res.key, addr res.buffer[0], unsafeAddr seckey.key) == 0:
|
2020-05-18 05:25:55 +00:00
|
|
|
err(EcKeyIncorrectError)
|
|
|
|
else:
|
|
|
|
ok(res)
|
2019-02-19 22:11:59 +00:00
|
|
|
else:
|
2020-05-18 05:25:55 +00:00
|
|
|
err(EcKeyIncorrectError)
|
2019-02-19 22:11:59 +00:00
|
|
|
|
2020-07-07 11:14:11 +00:00
|
|
|
proc random*(
|
2024-06-11 15:18:06 +00:00
|
|
|
T: typedesc[EcKeyPair], kind: EcCurveKind, rng: var HmacDrbgContext
|
|
|
|
): EcResult[T] =
|
2019-02-19 22:11:59 +00:00
|
|
|
## Generate new random EC private and public keypair using BearSSL's
|
|
|
|
## HMAC-SHA256-DRBG algorithm.
|
|
|
|
##
|
|
|
|
## ``kind`` elliptic curve kind of your choice (secp256r1, secp384r1 or
|
|
|
|
## secp521r1).
|
2020-05-18 05:25:55 +00:00
|
|
|
let
|
2024-06-11 15:18:06 +00:00
|
|
|
seckey = ?EcPrivateKey.random(kind, rng)
|
|
|
|
pubkey = ?seckey.getPublicKey()
|
2020-05-18 05:25:55 +00:00
|
|
|
key = EcKeyPair(seckey: seckey, pubkey: pubkey)
|
|
|
|
ok(key)
|
2019-02-19 22:11:59 +00:00
|
|
|
|
|
|
|
proc `$`*(seckey: EcPrivateKey): string =
|
|
|
|
## Return string representation of EC private key.
|
2019-09-11 16:03:39 +00:00
|
|
|
if isNil(seckey) or seckey.key.curve == 0 or seckey.key.xlen == 0 or
|
2024-06-11 15:18:06 +00:00
|
|
|
len(seckey.buffer) == 0:
|
2019-09-11 16:03:39 +00:00
|
|
|
result = "Empty or uninitialized ECNIST key"
|
2019-02-19 22:11:59 +00:00
|
|
|
else:
|
|
|
|
if seckey.key.curve notin EcSupportedCurvesCint:
|
|
|
|
result = "Unknown key"
|
|
|
|
else:
|
|
|
|
let offset = seckey.getOffset()
|
|
|
|
if offset < 0:
|
|
|
|
result = "Corrupted key"
|
|
|
|
else:
|
|
|
|
let e = offset + cast[int](seckey.key.xlen) - 1
|
2020-08-11 14:48:21 +00:00
|
|
|
result = ncrutils.toHex(seckey.buffer.toOpenArray(offset, e))
|
2019-02-19 22:11:59 +00:00
|
|
|
|
|
|
|
proc `$`*(pubkey: EcPublicKey): string =
|
|
|
|
## Return string representation of EC public key.
|
2019-09-11 16:03:39 +00:00
|
|
|
if isNil(pubkey) or pubkey.key.curve == 0 or pubkey.key.qlen == 0 or
|
2024-06-11 15:18:06 +00:00
|
|
|
len(pubkey.buffer) == 0:
|
2019-09-11 16:03:39 +00:00
|
|
|
result = "Empty or uninitialized ECNIST key"
|
2019-02-19 22:11:59 +00:00
|
|
|
else:
|
|
|
|
if pubkey.key.curve notin EcSupportedCurvesCint:
|
|
|
|
result = "Unknown key"
|
|
|
|
else:
|
|
|
|
let offset = pubkey.getOffset()
|
|
|
|
if offset < 0:
|
|
|
|
result = "Corrupted key"
|
|
|
|
else:
|
|
|
|
let e = offset + cast[int](pubkey.key.qlen) - 1
|
2020-08-11 14:48:21 +00:00
|
|
|
result = ncrutils.toHex(pubkey.buffer.toOpenArray(offset, e))
|
2019-02-19 22:11:59 +00:00
|
|
|
|
|
|
|
proc `$`*(sig: EcSignature): string =
|
|
|
|
## Return hexadecimal string representation of EC signature.
|
2019-09-11 16:03:39 +00:00
|
|
|
if isNil(sig) or len(sig.buffer) == 0:
|
|
|
|
result = "Empty or uninitialized ECNIST signature"
|
|
|
|
else:
|
2020-08-11 14:48:21 +00:00
|
|
|
result = ncrutils.toHex(sig.buffer)
|
2019-09-11 16:03:39 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc toRawBytes*(seckey: EcPrivateKey, data: var openArray[byte]): EcResult[int] =
|
2019-09-11 16:03:39 +00:00
|
|
|
## Serialize EC private key ``seckey`` to raw binary form and store it
|
|
|
|
## to ``data``.
|
|
|
|
##
|
|
|
|
## Returns number of bytes (octets) needed to store EC private key, or `0`
|
|
|
|
## if private key is not in supported curve.
|
2020-05-18 05:25:55 +00:00
|
|
|
if isNil(seckey):
|
|
|
|
return err(EcKeyIncorrectError)
|
2019-09-11 16:03:39 +00:00
|
|
|
if seckey.key.curve in EcSupportedCurvesCint:
|
2020-05-18 05:25:55 +00:00
|
|
|
let klen = getPrivateKeyLength(cast[EcCurveKind](seckey.key.curve))
|
|
|
|
if len(data) >= klen:
|
|
|
|
copyMem(addr data[0], unsafeAddr seckey.buffer[0], klen)
|
|
|
|
ok(klen)
|
|
|
|
else:
|
|
|
|
err(EcKeyIncorrectError)
|
2019-09-11 16:03:39 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc toRawBytes*(pubkey: EcPublicKey, data: var openArray[byte]): EcResult[int] =
|
2019-09-11 16:03:39 +00:00
|
|
|
## Serialize EC public key ``pubkey`` to uncompressed form specified in
|
|
|
|
## section 4.3.6 of ANSI X9.62.
|
|
|
|
##
|
|
|
|
## Returns number of bytes (octets) needed to store EC public key, or `0`
|
|
|
|
## if public key is not in supported curve.
|
2020-05-18 05:25:55 +00:00
|
|
|
if isNil(pubkey):
|
|
|
|
return err(EcKeyIncorrectError)
|
2019-09-11 16:03:39 +00:00
|
|
|
if pubkey.key.curve in EcSupportedCurvesCint:
|
2020-05-18 05:25:55 +00:00
|
|
|
let klen = getPublicKeyLength(cast[EcCurveKind](pubkey.key.curve))
|
|
|
|
if len(data) >= klen:
|
|
|
|
copyMem(addr data[0], unsafeAddr pubkey.buffer[0], klen)
|
|
|
|
ok(klen)
|
|
|
|
else:
|
|
|
|
err(EcKeyIncorrectError)
|
2019-09-11 16:03:39 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc toRawBytes*(sig: EcSignature, data: var openArray[byte]): int =
|
2019-09-11 16:03:39 +00:00
|
|
|
## Serialize EC signature ``sig`` to raw binary form and store it to ``data``.
|
|
|
|
##
|
|
|
|
## Returns number of bytes (octets) needed to store EC signature, or `0`
|
|
|
|
## if signature is not in supported curve.
|
|
|
|
doAssert(not isNil(sig))
|
|
|
|
result = len(sig.buffer)
|
|
|
|
if len(data) >= len(sig.buffer):
|
|
|
|
if len(sig.buffer) > 0:
|
|
|
|
copyMem(addr data[0], unsafeAddr sig.buffer[0], len(sig.buffer))
|
2019-02-19 22:11:59 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc toBytes*(seckey: EcPrivateKey, data: var openArray[byte]): EcResult[int] =
|
2019-02-21 04:10:21 +00:00
|
|
|
## Serialize EC private key ``seckey`` to ASN.1 DER binary form and store it
|
|
|
|
## to ``data``.
|
2019-02-19 22:11:59 +00:00
|
|
|
##
|
2019-02-21 04:10:21 +00:00
|
|
|
## Procedure returns number of bytes (octets) needed to store EC private key,
|
|
|
|
## or `0` if private key is not in supported curve.
|
2020-05-18 05:25:55 +00:00
|
|
|
if isNil(seckey):
|
|
|
|
return err(EcKeyIncorrectError)
|
2019-02-19 22:11:59 +00:00
|
|
|
if seckey.key.curve in EcSupportedCurvesCint:
|
2019-02-21 04:10:21 +00:00
|
|
|
var offset, length: int
|
2024-06-11 15:18:06 +00:00
|
|
|
var pubkey = ?seckey.getPublicKey()
|
2019-02-21 04:10:21 +00:00
|
|
|
var b = Asn1Buffer.init()
|
|
|
|
var p = Asn1Composite.init(Asn1Tag.Sequence)
|
|
|
|
var c0 = Asn1Composite.init(0)
|
|
|
|
var c1 = Asn1Composite.init(1)
|
2022-07-27 11:47:50 +00:00
|
|
|
if seckey.key.curve == EC_secp256r1:
|
2019-02-21 04:10:21 +00:00
|
|
|
c0.write(Asn1Tag.Oid, Asn1OidSecp256r1)
|
2022-07-27 11:47:50 +00:00
|
|
|
elif seckey.key.curve == EC_secp384r1:
|
2019-02-21 04:10:21 +00:00
|
|
|
c0.write(Asn1Tag.Oid, Asn1OidSecp384r1)
|
2022-07-27 11:47:50 +00:00
|
|
|
elif seckey.key.curve == EC_secp521r1:
|
2019-02-21 04:10:21 +00:00
|
|
|
c0.write(Asn1Tag.Oid, Asn1OidSecp521r1)
|
|
|
|
c0.finish()
|
|
|
|
offset = pubkey.getOffset()
|
2021-12-13 18:46:25 +00:00
|
|
|
if offset < 0:
|
|
|
|
return err(EcKeyIncorrectError)
|
2022-06-16 08:08:52 +00:00
|
|
|
length = int(pubkey.key.qlen)
|
2024-06-11 15:18:06 +00:00
|
|
|
c1.write(Asn1Tag.BitString, pubkey.buffer.toOpenArray(offset, offset + length - 1))
|
2019-02-21 04:10:21 +00:00
|
|
|
c1.finish()
|
|
|
|
offset = seckey.getOffset()
|
2021-12-13 18:46:25 +00:00
|
|
|
if offset < 0:
|
|
|
|
return err(EcKeyIncorrectError)
|
2022-06-16 08:08:52 +00:00
|
|
|
length = int(seckey.key.xlen)
|
2019-02-21 04:10:21 +00:00
|
|
|
p.write(1'u64)
|
2024-06-11 15:18:06 +00:00
|
|
|
p.write(Asn1Tag.OctetString, seckey.buffer.toOpenArray(offset, offset + length - 1))
|
2019-02-21 04:10:21 +00:00
|
|
|
p.write(c0)
|
|
|
|
p.write(c1)
|
|
|
|
p.finish()
|
|
|
|
b.write(p)
|
|
|
|
b.finish()
|
2020-05-18 05:25:55 +00:00
|
|
|
var blen = len(b)
|
|
|
|
if len(data) >= blen:
|
|
|
|
copyMem(addr data[0], addr b.buffer[0], blen)
|
|
|
|
# ok anyway, since it might have been a query...
|
|
|
|
ok(blen)
|
|
|
|
else:
|
|
|
|
err(EcKeyIncorrectError)
|
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc toBytes*(pubkey: EcPublicKey, data: var openArray[byte]): EcResult[int] =
|
2019-02-21 04:10:21 +00:00
|
|
|
## Serialize EC public key ``pubkey`` to ASN.1 DER binary form and store it
|
|
|
|
## to ``data``.
|
2019-02-19 22:11:59 +00:00
|
|
|
##
|
2019-02-21 04:10:21 +00:00
|
|
|
## Procedure returns number of bytes (octets) needed to store EC public key,
|
|
|
|
## or `0` if public key is not in supported curve.
|
2020-05-18 05:25:55 +00:00
|
|
|
if isNil(pubkey):
|
|
|
|
return err(EcKeyIncorrectError)
|
2019-02-19 22:11:59 +00:00
|
|
|
if pubkey.key.curve in EcSupportedCurvesCint:
|
2019-02-21 04:10:21 +00:00
|
|
|
var b = Asn1Buffer.init()
|
|
|
|
var p = Asn1Composite.init(Asn1Tag.Sequence)
|
|
|
|
var c = Asn1Composite.init(Asn1Tag.Sequence)
|
|
|
|
c.write(Asn1Tag.Oid, Asn1OidEcPublicKey)
|
2022-07-27 11:47:50 +00:00
|
|
|
if pubkey.key.curve == EC_secp256r1:
|
2019-02-21 04:10:21 +00:00
|
|
|
c.write(Asn1Tag.Oid, Asn1OidSecp256r1)
|
2022-07-27 11:47:50 +00:00
|
|
|
elif pubkey.key.curve == EC_secp384r1:
|
2019-02-21 04:10:21 +00:00
|
|
|
c.write(Asn1Tag.Oid, Asn1OidSecp384r1)
|
2022-07-27 11:47:50 +00:00
|
|
|
elif pubkey.key.curve == EC_secp521r1:
|
2019-02-21 04:10:21 +00:00
|
|
|
c.write(Asn1Tag.Oid, Asn1OidSecp521r1)
|
|
|
|
c.finish()
|
|
|
|
p.write(c)
|
|
|
|
let offset = getOffset(pubkey)
|
2021-12-13 18:46:25 +00:00
|
|
|
if offset < 0:
|
|
|
|
return err(EcKeyIncorrectError)
|
2022-06-16 08:08:52 +00:00
|
|
|
let length = int(pubkey.key.qlen)
|
2024-06-11 15:18:06 +00:00
|
|
|
p.write(Asn1Tag.BitString, pubkey.buffer.toOpenArray(offset, offset + length - 1))
|
2019-02-21 04:10:21 +00:00
|
|
|
p.finish()
|
|
|
|
b.write(p)
|
|
|
|
b.finish()
|
2020-05-18 05:25:55 +00:00
|
|
|
var blen = len(b)
|
|
|
|
if len(data) >= blen:
|
|
|
|
copyMem(addr data[0], addr b.buffer[0], blen)
|
|
|
|
ok(blen)
|
|
|
|
else:
|
|
|
|
err(EcKeyIncorrectError)
|
2019-02-22 11:32:15 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc toBytes*(sig: EcSignature, data: var openArray[byte]): EcResult[int] =
|
2019-02-22 11:32:15 +00:00
|
|
|
## Serialize EC signature ``sig`` to ASN.1 DER binary form and store it
|
|
|
|
## to ``data``.
|
|
|
|
##
|
|
|
|
## Procedure returns number of bytes (octets) needed to store EC signature,
|
|
|
|
## or `0` if signature is not in supported curve.
|
2020-05-18 05:25:55 +00:00
|
|
|
if isNil(sig):
|
|
|
|
return err(EcSignatureError)
|
|
|
|
let slen = len(sig.buffer)
|
|
|
|
if len(data) >= slen:
|
|
|
|
copyMem(addr data[0], unsafeAddr sig.buffer[0], slen)
|
|
|
|
ok(slen)
|
|
|
|
|
|
|
|
proc getBytes*(seckey: EcPrivateKey): EcResult[seq[byte]] =
|
2019-02-21 04:10:21 +00:00
|
|
|
## Serialize EC private key ``seckey`` to ASN.1 DER binary form and return it.
|
2020-05-18 05:25:55 +00:00
|
|
|
if isNil(seckey):
|
|
|
|
return err(EcKeyIncorrectError)
|
2019-02-19 22:11:59 +00:00
|
|
|
if seckey.key.curve in EcSupportedCurvesCint:
|
2020-05-18 05:25:55 +00:00
|
|
|
var res = newSeq[byte]()
|
2024-06-11 15:18:06 +00:00
|
|
|
let length = ?seckey.toBytes(res)
|
2020-05-18 05:25:55 +00:00
|
|
|
res.setLen(length)
|
2024-06-11 15:18:06 +00:00
|
|
|
discard ?seckey.toBytes(res)
|
2020-05-18 05:25:55 +00:00
|
|
|
ok(res)
|
2019-02-19 22:11:59 +00:00
|
|
|
else:
|
2020-05-18 05:25:55 +00:00
|
|
|
err(EcKeyIncorrectError)
|
2019-02-19 22:11:59 +00:00
|
|
|
|
2020-05-18 05:25:55 +00:00
|
|
|
proc getBytes*(pubkey: EcPublicKey): EcResult[seq[byte]] =
|
2019-02-21 04:10:21 +00:00
|
|
|
## Serialize EC public key ``pubkey`` to ASN.1 DER binary form and return it.
|
2020-05-18 05:25:55 +00:00
|
|
|
if isNil(pubkey):
|
|
|
|
return err(EcKeyIncorrectError)
|
2019-02-19 22:11:59 +00:00
|
|
|
if pubkey.key.curve in EcSupportedCurvesCint:
|
2020-05-18 05:25:55 +00:00
|
|
|
var res = newSeq[byte]()
|
2024-06-11 15:18:06 +00:00
|
|
|
let length = ?pubkey.toBytes(res)
|
2020-05-18 05:25:55 +00:00
|
|
|
res.setLen(length)
|
2024-06-11 15:18:06 +00:00
|
|
|
discard ?pubkey.toBytes(res)
|
2020-05-18 05:25:55 +00:00
|
|
|
ok(res)
|
2019-02-19 22:11:59 +00:00
|
|
|
else:
|
2020-05-18 05:25:55 +00:00
|
|
|
err(EcKeyIncorrectError)
|
2019-02-19 22:11:59 +00:00
|
|
|
|
2020-05-18 05:25:55 +00:00
|
|
|
proc getBytes*(sig: EcSignature): EcResult[seq[byte]] =
|
2019-02-22 11:32:15 +00:00
|
|
|
## Serialize EC signature ``sig`` to ASN.1 DER binary form and return it.
|
2020-05-18 05:25:55 +00:00
|
|
|
if isNil(sig):
|
|
|
|
return err(EcSignatureError)
|
|
|
|
var res = newSeq[byte]()
|
2024-06-11 15:18:06 +00:00
|
|
|
let length = ?sig.toBytes(res)
|
2020-05-18 05:25:55 +00:00
|
|
|
res.setLen(length)
|
2024-06-11 15:18:06 +00:00
|
|
|
discard ?sig.toBytes(res)
|
2020-05-18 05:25:55 +00:00
|
|
|
ok(res)
|
|
|
|
|
|
|
|
proc getRawBytes*(seckey: EcPrivateKey): EcResult[seq[byte]] =
|
2019-09-11 16:03:39 +00:00
|
|
|
## Serialize EC private key ``seckey`` to raw binary form and return it.
|
2020-05-18 05:25:55 +00:00
|
|
|
if isNil(seckey):
|
|
|
|
return err(EcKeyIncorrectError)
|
2019-09-11 16:03:39 +00:00
|
|
|
if seckey.key.curve in EcSupportedCurvesCint:
|
2020-05-18 05:25:55 +00:00
|
|
|
var res = newSeq[byte]()
|
2024-06-11 15:18:06 +00:00
|
|
|
let length = ?seckey.toRawBytes(res)
|
2020-05-18 05:25:55 +00:00
|
|
|
res.setLen(length)
|
2024-06-11 15:18:06 +00:00
|
|
|
discard ?seckey.toRawBytes(res)
|
2020-05-18 05:25:55 +00:00
|
|
|
ok(res)
|
2019-09-11 16:03:39 +00:00
|
|
|
else:
|
2020-05-18 05:25:55 +00:00
|
|
|
err(EcKeyIncorrectError)
|
2019-09-11 16:03:39 +00:00
|
|
|
|
2020-05-18 05:25:55 +00:00
|
|
|
proc getRawBytes*(pubkey: EcPublicKey): EcResult[seq[byte]] =
|
2019-09-11 16:03:39 +00:00
|
|
|
## Serialize EC public key ``pubkey`` to raw binary form and return it.
|
2020-05-18 05:25:55 +00:00
|
|
|
if isNil(pubkey):
|
|
|
|
return err(EcKeyIncorrectError)
|
2019-09-11 16:03:39 +00:00
|
|
|
if pubkey.key.curve in EcSupportedCurvesCint:
|
2020-05-18 05:25:55 +00:00
|
|
|
var res = newSeq[byte]()
|
2024-06-11 15:18:06 +00:00
|
|
|
let length = ?pubkey.toRawBytes(res)
|
2020-05-18 05:25:55 +00:00
|
|
|
res.setLen(length)
|
2024-06-11 15:18:06 +00:00
|
|
|
discard ?pubkey.toRawBytes(res)
|
2020-10-30 12:49:54 +00:00
|
|
|
return ok(res)
|
2019-09-11 16:03:39 +00:00
|
|
|
else:
|
2020-05-18 05:25:55 +00:00
|
|
|
return err(EcKeyIncorrectError)
|
2019-09-11 16:03:39 +00:00
|
|
|
|
2020-05-18 05:25:55 +00:00
|
|
|
proc getRawBytes*(sig: EcSignature): EcResult[seq[byte]] =
|
2019-09-11 16:03:39 +00:00
|
|
|
## Serialize EC signature ``sig`` to raw binary form and return it.
|
2020-05-18 05:25:55 +00:00
|
|
|
if isNil(sig):
|
|
|
|
return err(EcSignatureError)
|
|
|
|
var res = newSeq[byte]()
|
2024-06-11 15:18:06 +00:00
|
|
|
let length = ?sig.toBytes(res)
|
2020-05-18 05:25:55 +00:00
|
|
|
res.setLen(length)
|
2024-06-11 15:18:06 +00:00
|
|
|
discard ?sig.toBytes(res)
|
2020-05-18 05:25:55 +00:00
|
|
|
ok(res)
|
2019-02-22 11:32:15 +00:00
|
|
|
|
2019-02-19 22:11:59 +00:00
|
|
|
proc `==`*(pubkey1, pubkey2: EcPublicKey): bool =
|
|
|
|
## Returns ``true`` if both keys ``pubkey1`` and ``pubkey2`` are equal.
|
2019-09-11 16:03:39 +00:00
|
|
|
if isNil(pubkey1) and isNil(pubkey2):
|
|
|
|
result = true
|
|
|
|
elif isNil(pubkey1) and (not isNil(pubkey2)):
|
|
|
|
result = false
|
|
|
|
elif isNil(pubkey2) and (not isNil(pubkey1)):
|
|
|
|
result = false
|
|
|
|
else:
|
|
|
|
if pubkey1.key.curve != pubkey2.key.curve:
|
|
|
|
return false
|
|
|
|
if pubkey1.key.qlen != pubkey2.key.qlen:
|
|
|
|
return false
|
|
|
|
let op1 = pubkey1.getOffset()
|
|
|
|
let op2 = pubkey2.getOffset()
|
|
|
|
if op1 == -1 or op2 == -1:
|
|
|
|
return false
|
2024-06-11 15:18:06 +00:00
|
|
|
return CT.isEqual(
|
|
|
|
pubkey1.buffer.toOpenArray(op1, pubkey1.key.qlen - 1),
|
|
|
|
pubkey2.buffer.toOpenArray(op2, pubkey2.key.qlen - 1),
|
|
|
|
)
|
2019-02-19 22:11:59 +00:00
|
|
|
|
|
|
|
proc `==`*(seckey1, seckey2: EcPrivateKey): bool =
|
|
|
|
## Returns ``true`` if both keys ``seckey1`` and ``seckey2`` are equal.
|
2019-09-11 16:03:39 +00:00
|
|
|
if isNil(seckey1) and isNil(seckey2):
|
|
|
|
result = true
|
|
|
|
elif isNil(seckey1) and (not isNil(seckey2)):
|
|
|
|
result = false
|
|
|
|
elif isNil(seckey2) and (not isNil(seckey1)):
|
|
|
|
result = false
|
|
|
|
else:
|
|
|
|
if seckey1.key.curve != seckey2.key.curve:
|
|
|
|
return false
|
|
|
|
if seckey1.key.xlen != seckey2.key.xlen:
|
|
|
|
return false
|
|
|
|
let op1 = seckey1.getOffset()
|
|
|
|
let op2 = seckey2.getOffset()
|
|
|
|
if op1 == -1 or op2 == -1:
|
|
|
|
return false
|
2024-06-11 15:18:06 +00:00
|
|
|
return CT.isEqual(
|
|
|
|
seckey1.buffer.toOpenArray(op1, seckey1.key.xlen - 1),
|
|
|
|
seckey2.buffer.toOpenArray(op2, seckey2.key.xlen - 1),
|
|
|
|
)
|
2019-02-19 22:11:59 +00:00
|
|
|
|
2020-08-08 06:53:33 +00:00
|
|
|
proc `==`*(a, b: EcSignature): bool =
|
2019-02-19 22:11:59 +00:00
|
|
|
## Return ``true`` if both signatures ``sig1`` and ``sig2`` are equal.
|
2020-08-08 06:53:33 +00:00
|
|
|
if isNil(a) and isNil(b):
|
|
|
|
true
|
|
|
|
elif isNil(a) and (not isNil(b)):
|
|
|
|
false
|
|
|
|
elif isNil(b) and (not isNil(a)):
|
|
|
|
false
|
2019-09-11 16:03:39 +00:00
|
|
|
else:
|
2020-08-08 06:53:33 +00:00
|
|
|
# We need to cover all the cases because Signature initialization procedure
|
|
|
|
# do not perform any checks.
|
|
|
|
if len(a.buffer) == 0 and len(b.buffer) == 0:
|
|
|
|
true
|
|
|
|
elif len(a.buffer) == 0 and len(b.buffer) != 0:
|
|
|
|
false
|
|
|
|
elif len(b.buffer) == 0 and len(a.buffer) != 0:
|
|
|
|
false
|
|
|
|
elif len(a.buffer) != len(b.buffer):
|
|
|
|
false
|
|
|
|
else:
|
|
|
|
CT.isEqual(a.buffer, b.buffer)
|
2019-02-19 22:11:59 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc init*(key: var EcPrivateKey, data: openArray[byte]): Result[void, Asn1Error] =
|
2019-02-25 18:03:52 +00:00
|
|
|
## Initialize EC `private key` or `signature` ``key`` from ASN.1 DER binary
|
2019-02-19 22:11:59 +00:00
|
|
|
## representation ``data``.
|
|
|
|
##
|
2020-04-23 12:10:20 +00:00
|
|
|
## Procedure returns ``Result[void, Asn1Error]``.
|
2019-02-21 04:10:21 +00:00
|
|
|
var raw, oid, field: Asn1Field
|
2019-02-19 22:11:59 +00:00
|
|
|
var curve: cint
|
2019-02-21 04:10:21 +00:00
|
|
|
|
|
|
|
var ab = Asn1Buffer.init(data)
|
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
field = ?ab.read()
|
2020-04-23 12:10:20 +00:00
|
|
|
|
2019-02-21 04:10:21 +00:00
|
|
|
if field.kind != Asn1Tag.Sequence:
|
2020-04-23 12:10:20 +00:00
|
|
|
return err(Asn1Error.Incorrect)
|
2019-02-21 04:10:21 +00:00
|
|
|
|
|
|
|
var ib = field.getBuffer()
|
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
field = ?ib.read()
|
2020-04-23 12:10:20 +00:00
|
|
|
|
2019-02-21 04:10:21 +00:00
|
|
|
if field.kind != Asn1Tag.Integer:
|
2020-04-23 12:10:20 +00:00
|
|
|
return err(Asn1Error.Incorrect)
|
2019-02-21 04:10:21 +00:00
|
|
|
if field.vint != 1'u64:
|
2020-04-23 12:10:20 +00:00
|
|
|
return err(Asn1Error.Incorrect)
|
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
raw = ?ib.read()
|
2019-02-21 04:10:21 +00:00
|
|
|
|
|
|
|
if raw.kind != Asn1Tag.OctetString:
|
2020-04-23 12:10:20 +00:00
|
|
|
return err(Asn1Error.Incorrect)
|
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
oid = ?ib.read()
|
2019-02-21 04:10:21 +00:00
|
|
|
|
|
|
|
if oid.kind != Asn1Tag.Oid:
|
2020-04-23 12:10:20 +00:00
|
|
|
return err(Asn1Error.Incorrect)
|
2019-02-21 04:10:21 +00:00
|
|
|
|
|
|
|
if oid == Asn1OidSecp256r1:
|
2023-05-18 08:24:17 +00:00
|
|
|
curve = safeConvert[cint](Secp256r1)
|
2019-02-21 04:10:21 +00:00
|
|
|
elif oid == Asn1OidSecp384r1:
|
2023-05-18 08:24:17 +00:00
|
|
|
curve = safeConvert[cint](Secp384r1)
|
2019-02-21 04:10:21 +00:00
|
|
|
elif oid == Asn1OidSecp521r1:
|
2023-05-18 08:24:17 +00:00
|
|
|
curve = safeConvert[cint](Secp521r1)
|
2019-02-21 04:10:21 +00:00
|
|
|
else:
|
2020-04-23 12:10:20 +00:00
|
|
|
return err(Asn1Error.Incorrect)
|
2019-02-21 04:10:21 +00:00
|
|
|
|
|
|
|
if checkScalar(raw.toOpenArray(), curve) == 1'u32:
|
|
|
|
key = new EcPrivateKey
|
|
|
|
copyMem(addr key.buffer[0], addr raw.buffer[raw.offset], raw.length)
|
2022-06-16 08:08:52 +00:00
|
|
|
key.key.x = addr key.buffer[0]
|
|
|
|
key.key.xlen = uint(raw.length)
|
2019-02-21 04:10:21 +00:00
|
|
|
key.key.curve = curve
|
2020-04-23 12:10:20 +00:00
|
|
|
ok()
|
2019-02-21 04:10:21 +00:00
|
|
|
else:
|
2020-04-23 12:10:20 +00:00
|
|
|
err(Asn1Error.Incorrect)
|
2019-02-19 22:11:59 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc init*(pubkey: var EcPublicKey, data: openArray[byte]): Result[void, Asn1Error] =
|
2019-02-21 04:10:21 +00:00
|
|
|
## Initialize EC public key ``pubkey`` from ASN.1 DER binary representation
|
2019-02-19 22:11:59 +00:00
|
|
|
## ``data``.
|
|
|
|
##
|
2020-04-23 12:10:20 +00:00
|
|
|
## Procedure returns ``Result[void, Asn1Error]``.
|
2019-02-21 04:10:21 +00:00
|
|
|
var raw, oid, field: Asn1Field
|
2019-02-19 22:11:59 +00:00
|
|
|
var curve: cint
|
2019-02-21 04:10:21 +00:00
|
|
|
|
|
|
|
var ab = Asn1Buffer.init(data)
|
2020-04-23 12:10:20 +00:00
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
field = ?ab.read()
|
2020-04-23 12:10:20 +00:00
|
|
|
|
2019-02-21 04:10:21 +00:00
|
|
|
if field.kind != Asn1Tag.Sequence:
|
2020-04-23 12:10:20 +00:00
|
|
|
return err(Asn1Error.Incorrect)
|
2019-02-21 04:10:21 +00:00
|
|
|
|
|
|
|
var ib = field.getBuffer()
|
2024-06-11 15:18:06 +00:00
|
|
|
field = ?ib.read()
|
2020-04-23 12:10:20 +00:00
|
|
|
|
2019-02-21 04:10:21 +00:00
|
|
|
if field.kind != Asn1Tag.Sequence:
|
2020-04-23 12:10:20 +00:00
|
|
|
return err(Asn1Error.Incorrect)
|
2019-02-21 04:10:21 +00:00
|
|
|
|
|
|
|
var ob = field.getBuffer()
|
2024-06-11 15:18:06 +00:00
|
|
|
oid = ?ob.read()
|
2020-04-23 12:10:20 +00:00
|
|
|
|
2019-02-21 04:10:21 +00:00
|
|
|
if oid.kind != Asn1Tag.Oid:
|
2020-04-23 12:10:20 +00:00
|
|
|
return err(Asn1Error.Incorrect)
|
2019-02-21 04:10:21 +00:00
|
|
|
|
|
|
|
if oid != Asn1OidEcPublicKey:
|
2020-04-23 12:10:20 +00:00
|
|
|
return err(Asn1Error.Incorrect)
|
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
oid = ?ob.read()
|
2019-02-21 04:10:21 +00:00
|
|
|
|
|
|
|
if oid.kind != Asn1Tag.Oid:
|
2020-04-23 12:10:20 +00:00
|
|
|
return err(Asn1Error.Incorrect)
|
2019-02-21 04:10:21 +00:00
|
|
|
|
|
|
|
if oid == Asn1OidSecp256r1:
|
2023-05-18 08:24:17 +00:00
|
|
|
curve = safeConvert[cint](Secp256r1)
|
2019-02-21 04:10:21 +00:00
|
|
|
elif oid == Asn1OidSecp384r1:
|
2023-05-18 08:24:17 +00:00
|
|
|
curve = safeConvert[cint](Secp384r1)
|
2019-02-21 04:10:21 +00:00
|
|
|
elif oid == Asn1OidSecp521r1:
|
2023-05-18 08:24:17 +00:00
|
|
|
curve = safeConvert[cint](Secp521r1)
|
2019-02-21 04:10:21 +00:00
|
|
|
else:
|
2020-04-23 12:10:20 +00:00
|
|
|
return err(Asn1Error.Incorrect)
|
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
raw = ?ib.read()
|
2019-02-21 04:10:21 +00:00
|
|
|
|
|
|
|
if raw.kind != Asn1Tag.BitString:
|
2020-04-23 12:10:20 +00:00
|
|
|
return err(Asn1Error.Incorrect)
|
2019-02-21 04:10:21 +00:00
|
|
|
|
|
|
|
if checkPublic(raw.toOpenArray(), curve) != 0:
|
|
|
|
pubkey = new EcPublicKey
|
|
|
|
copyMem(addr pubkey.buffer[0], addr raw.buffer[raw.offset], raw.length)
|
2022-06-16 08:08:52 +00:00
|
|
|
pubkey.key.q = addr pubkey.buffer[0]
|
|
|
|
pubkey.key.qlen = uint(raw.length)
|
2019-02-21 04:10:21 +00:00
|
|
|
pubkey.key.curve = curve
|
2020-04-23 12:10:20 +00:00
|
|
|
ok()
|
2019-02-21 04:10:21 +00:00
|
|
|
else:
|
2020-04-23 12:10:20 +00:00
|
|
|
err(Asn1Error.Incorrect)
|
2019-02-19 22:11:59 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc init*(sig: var EcSignature, data: openArray[byte]): Result[void, Asn1Error] =
|
2019-02-19 22:11:59 +00:00
|
|
|
## Initialize EC signature ``sig`` from raw binary representation ``data``.
|
|
|
|
##
|
2020-04-23 12:10:20 +00:00
|
|
|
## Procedure returns ``Result[void, Asn1Error]``.
|
2019-02-19 22:11:59 +00:00
|
|
|
if len(data) > 0:
|
|
|
|
sig = new EcSignature
|
|
|
|
sig.buffer = @data
|
2020-04-23 12:10:20 +00:00
|
|
|
ok()
|
|
|
|
else:
|
|
|
|
err(Asn1Error.Incorrect)
|
2019-02-19 22:11:59 +00:00
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
proc init*[T: EcPKI](sospk: var T, data: string): Result[void, Asn1Error] {.inline.} =
|
2019-02-25 18:03:52 +00:00
|
|
|
## Initialize EC `private key`, `public key` or `signature` ``sospk`` from
|
2019-03-01 06:34:52 +00:00
|
|
|
## ASN.1 DER hexadecimal string representation ``data``.
|
2019-02-19 22:11:59 +00:00
|
|
|
##
|
2019-02-22 11:32:15 +00:00
|
|
|
## Procedure returns ``Asn1Status``.
|
2020-08-11 14:48:21 +00:00
|
|
|
sospk.init(ncrutils.fromHex(data))
|
2019-02-19 22:11:59 +00:00
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
proc init*(t: typedesc[EcPrivateKey], data: openArray[byte]): EcResult[EcPrivateKey] =
|
2019-02-21 04:10:21 +00:00
|
|
|
## Initialize EC private key from ASN.1 DER binary representation ``data`` and
|
2019-02-19 22:11:59 +00:00
|
|
|
## return constructed object.
|
2020-05-18 05:25:55 +00:00
|
|
|
var key: EcPrivateKey
|
|
|
|
let res = key.init(data)
|
2020-04-23 12:10:20 +00:00
|
|
|
if res.isErr:
|
2020-05-18 05:25:55 +00:00
|
|
|
err(EcKeyIncorrectError)
|
|
|
|
else:
|
|
|
|
ok(key)
|
2019-02-19 22:11:59 +00:00
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
proc init*(t: typedesc[EcPublicKey], data: openArray[byte]): EcResult[EcPublicKey] =
|
2019-02-21 04:10:21 +00:00
|
|
|
## Initialize EC public key from ASN.1 DER binary representation ``data`` and
|
2019-02-19 22:11:59 +00:00
|
|
|
## return constructed object.
|
2020-05-18 05:25:55 +00:00
|
|
|
var key: EcPublicKey
|
|
|
|
let res = key.init(data)
|
2020-04-23 12:10:20 +00:00
|
|
|
if res.isErr:
|
2020-05-18 05:25:55 +00:00
|
|
|
err(EcKeyIncorrectError)
|
|
|
|
else:
|
|
|
|
ok(key)
|
2019-02-19 22:11:59 +00:00
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
proc init*(t: typedesc[EcSignature], data: openArray[byte]): EcResult[EcSignature] =
|
2019-02-19 22:11:59 +00:00
|
|
|
## Initialize EC signature from raw binary representation ``data`` and
|
|
|
|
## return constructed object.
|
2020-05-18 05:25:55 +00:00
|
|
|
var sig: EcSignature
|
|
|
|
let res = sig.init(data)
|
2020-04-23 12:10:20 +00:00
|
|
|
if res.isErr:
|
2020-05-18 05:25:55 +00:00
|
|
|
err(EcSignatureError)
|
|
|
|
else:
|
|
|
|
ok(sig)
|
2019-02-19 22:11:59 +00:00
|
|
|
|
2020-05-18 05:25:55 +00:00
|
|
|
proc init*[T: EcPKI](t: typedesc[T], data: string): EcResult[T] =
|
2019-02-25 18:03:52 +00:00
|
|
|
## Initialize EC `private key`, `public key` or `signature` from hexadecimal
|
2019-02-19 22:11:59 +00:00
|
|
|
## string representation ``data`` and return constructed object.
|
2020-08-11 14:48:21 +00:00
|
|
|
t.init(ncrutils.fromHex(data))
|
2019-02-19 22:11:59 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc initRaw*(key: var EcPrivateKey, data: openArray[byte]): bool =
|
2019-03-01 06:34:52 +00:00
|
|
|
## Initialize EC `private key` or `scalar` ``key`` from raw binary
|
|
|
|
## representation ``data``.
|
|
|
|
##
|
|
|
|
## Length of ``data`` array must be ``SecKey256Length``, ``SecKey384Length``
|
|
|
|
## or ``SecKey521Length``.
|
|
|
|
##
|
|
|
|
## Procedure returns ``true`` on success, ``false`` otherwise.
|
|
|
|
var curve: cint
|
|
|
|
if len(data) == SecKey256Length:
|
2023-05-18 08:24:17 +00:00
|
|
|
curve = safeConvert[cint](Secp256r1)
|
2019-03-01 06:34:52 +00:00
|
|
|
result = true
|
|
|
|
elif len(data) == SecKey384Length:
|
2023-05-18 08:24:17 +00:00
|
|
|
curve = safeConvert[cint](Secp384r1)
|
2019-03-01 06:34:52 +00:00
|
|
|
result = true
|
|
|
|
elif len(data) == SecKey521Length:
|
2023-05-18 08:24:17 +00:00
|
|
|
curve = safeConvert[cint](Secp521r1)
|
2019-03-01 06:34:52 +00:00
|
|
|
result = true
|
|
|
|
if result:
|
|
|
|
result = false
|
|
|
|
if checkScalar(data, curve) == 1'u32:
|
|
|
|
let length = len(data)
|
|
|
|
key = new EcPrivateKey
|
|
|
|
copyMem(addr key.buffer[0], unsafeAddr data[0], length)
|
2022-06-16 08:08:52 +00:00
|
|
|
key.key.x = addr key.buffer[0]
|
|
|
|
key.key.xlen = uint(length)
|
2019-03-01 06:34:52 +00:00
|
|
|
key.key.curve = curve
|
|
|
|
result = true
|
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc initRaw*(pubkey: var EcPublicKey, data: openArray[byte]): bool =
|
2019-03-01 06:34:52 +00:00
|
|
|
## Initialize EC public key ``pubkey`` from raw binary representation
|
|
|
|
## ``data``.
|
|
|
|
##
|
|
|
|
## Length of ``data`` array must be ``PubKey256Length``, ``PubKey384Length``
|
|
|
|
## or ``PubKey521Length``.
|
|
|
|
##
|
|
|
|
## Procedure returns ``true`` on success, ``false`` otherwise.
|
|
|
|
var curve: cint
|
|
|
|
if len(data) > 0:
|
|
|
|
if data[0] == 0x04'u8:
|
|
|
|
if len(data) == PubKey256Length:
|
2023-05-18 08:24:17 +00:00
|
|
|
curve = safeConvert[cint](Secp256r1)
|
2019-03-01 06:34:52 +00:00
|
|
|
result = true
|
|
|
|
elif len(data) == PubKey384Length:
|
2023-05-18 08:24:17 +00:00
|
|
|
curve = safeConvert[cint](Secp384r1)
|
2019-03-01 06:34:52 +00:00
|
|
|
result = true
|
|
|
|
elif len(data) == PubKey521Length:
|
2023-05-18 08:24:17 +00:00
|
|
|
curve = safeConvert[cint](Secp521r1)
|
2019-03-01 06:34:52 +00:00
|
|
|
result = true
|
|
|
|
if result:
|
|
|
|
result = false
|
|
|
|
if checkPublic(data, curve) != 0:
|
|
|
|
let length = len(data)
|
|
|
|
pubkey = new EcPublicKey
|
|
|
|
copyMem(addr pubkey.buffer[0], unsafeAddr data[0], length)
|
2022-06-16 08:08:52 +00:00
|
|
|
pubkey.key.q = addr pubkey.buffer[0]
|
|
|
|
pubkey.key.qlen = uint(length)
|
2019-03-01 06:34:52 +00:00
|
|
|
pubkey.key.curve = curve
|
|
|
|
result = true
|
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc initRaw*(sig: var EcSignature, data: openArray[byte]): bool =
|
2019-03-01 06:34:52 +00:00
|
|
|
## Initialize EC signature ``sig`` from raw binary representation ``data``.
|
|
|
|
##
|
|
|
|
## Length of ``data`` array must be ``Sig256Length``, ``Sig384Length``
|
|
|
|
## or ``Sig521Length``.
|
|
|
|
##
|
|
|
|
## Procedure returns ``true`` on success, ``false`` otherwise.
|
|
|
|
let length = len(data)
|
2024-06-11 15:18:06 +00:00
|
|
|
if (length == Sig256Length) or (length == Sig384Length) or (length == Sig521Length):
|
2019-03-01 06:34:52 +00:00
|
|
|
result = true
|
|
|
|
if result:
|
|
|
|
sig = new EcSignature
|
|
|
|
sig.buffer = @data
|
|
|
|
|
|
|
|
proc initRaw*[T: EcPKI](sospk: var T, data: string): bool {.inline.} =
|
|
|
|
## Initialize EC `private key`, `public key` or `signature` ``sospk`` from
|
|
|
|
## raw hexadecimal string representation ``data``.
|
|
|
|
##
|
|
|
|
## Procedure returns ``true`` on success, ``false`` otherwise.
|
2020-08-11 14:48:21 +00:00
|
|
|
result = sospk.initRaw(ncrutils.fromHex(data))
|
2019-03-01 06:34:52 +00:00
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
proc initRaw*(
|
|
|
|
t: typedesc[EcPrivateKey], data: openArray[byte]
|
|
|
|
): EcResult[EcPrivateKey] =
|
2019-03-01 06:34:52 +00:00
|
|
|
## Initialize EC private key from raw binary representation ``data`` and
|
|
|
|
## return constructed object.
|
2020-05-18 05:25:55 +00:00
|
|
|
var res: EcPrivateKey
|
|
|
|
if not res.initRaw(data):
|
|
|
|
err(EcKeyIncorrectError)
|
|
|
|
else:
|
|
|
|
ok(res)
|
2019-03-01 06:34:52 +00:00
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
proc initRaw*(t: typedesc[EcPublicKey], data: openArray[byte]): EcResult[EcPublicKey] =
|
2019-03-01 06:34:52 +00:00
|
|
|
## Initialize EC public key from raw binary representation ``data`` and
|
|
|
|
## return constructed object.
|
2020-05-18 05:25:55 +00:00
|
|
|
var res: EcPublicKey
|
|
|
|
if not res.initRaw(data):
|
|
|
|
err(EcKeyIncorrectError)
|
|
|
|
else:
|
|
|
|
ok(res)
|
2019-03-01 06:34:52 +00:00
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
proc initRaw*(t: typedesc[EcSignature], data: openArray[byte]): EcResult[EcSignature] =
|
2019-03-01 06:34:52 +00:00
|
|
|
## Initialize EC signature from raw binary representation ``data`` and
|
|
|
|
## return constructed object.
|
2020-05-18 05:25:55 +00:00
|
|
|
var res: EcSignature
|
|
|
|
if not res.initRaw(data):
|
|
|
|
err(EcSignatureError)
|
|
|
|
else:
|
|
|
|
ok(res)
|
2019-03-01 06:34:52 +00:00
|
|
|
|
|
|
|
proc initRaw*[T: EcPKI](t: typedesc[T], data: string): T {.inline.} =
|
|
|
|
## Initialize EC `private key`, `public key` or `signature` from raw
|
|
|
|
## hexadecimal string representation ``data`` and return constructed object.
|
2020-08-11 14:48:21 +00:00
|
|
|
result = t.initRaw(ncrutils.fromHex(data))
|
2019-03-01 06:34:52 +00:00
|
|
|
|
2019-02-19 22:11:59 +00:00
|
|
|
proc scalarMul*(pub: EcPublicKey, sec: EcPrivateKey): EcPublicKey =
|
|
|
|
## Return scalar multiplication of ``pub`` and ``sec``.
|
|
|
|
##
|
|
|
|
## Returns point in curve as ``pub * sec`` or ``nil`` otherwise.
|
2019-09-11 16:03:39 +00:00
|
|
|
doAssert((not isNil(pub)) and (not isNil(sec)))
|
2022-06-16 08:08:52 +00:00
|
|
|
var impl = ecGetDefault()
|
2019-02-19 22:11:59 +00:00
|
|
|
if sec.key.curve in EcSupportedCurvesCint:
|
|
|
|
if pub.key.curve == sec.key.curve:
|
|
|
|
var key = new EcPublicKey
|
|
|
|
if key.copy(pub):
|
|
|
|
let poffset = key.getOffset()
|
|
|
|
let soffset = sec.getOffset()
|
|
|
|
if poffset >= 0 and soffset >= 0:
|
2024-06-11 15:18:06 +00:00
|
|
|
let res = impl.mul(
|
|
|
|
addr key.buffer[poffset],
|
|
|
|
key.key.qlen,
|
|
|
|
unsafeAddr sec.buffer[soffset],
|
|
|
|
sec.key.xlen,
|
|
|
|
key.key.curve,
|
|
|
|
)
|
2019-02-19 22:11:59 +00:00
|
|
|
if res != 0:
|
|
|
|
result = key
|
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
proc toSecret*(
|
|
|
|
pubkey: EcPublicKey, seckey: EcPrivateKey, data: var openArray[byte]
|
|
|
|
): int =
|
2019-03-01 06:34:52 +00:00
|
|
|
## Calculate ECDHE shared secret using Go's elliptic/curve approach, using
|
|
|
|
## remote public key ``pubkey`` and local private key ``seckey`` and store
|
|
|
|
## shared secret to ``data``.
|
|
|
|
##
|
|
|
|
## Returns number of bytes (octets) needed to store shared secret, or ``0``
|
|
|
|
## on error.
|
|
|
|
##
|
|
|
|
## ``data`` array length must be at least 32 bytes for `secp256r1`, 48 bytes
|
|
|
|
## for `secp384r1` and 66 bytes for `secp521r1`.
|
2019-09-11 16:03:39 +00:00
|
|
|
doAssert((not isNil(pubkey)) and (not isNil(seckey)))
|
2019-03-01 06:34:52 +00:00
|
|
|
var mult = scalarMul(pubkey, seckey)
|
|
|
|
if not isNil(mult):
|
2022-07-27 11:47:50 +00:00
|
|
|
if seckey.key.curve == EC_secp256r1:
|
2019-03-02 19:19:41 +00:00
|
|
|
result = Secret256Length
|
2022-07-27 11:47:50 +00:00
|
|
|
elif seckey.key.curve == EC_secp384r1:
|
2019-03-02 19:19:41 +00:00
|
|
|
result = Secret384Length
|
2022-07-27 11:47:50 +00:00
|
|
|
elif seckey.key.curve == EC_secp521r1:
|
2019-03-02 19:19:41 +00:00
|
|
|
result = Secret521Length
|
2019-03-01 06:34:52 +00:00
|
|
|
if len(data) >= result:
|
|
|
|
var qplus1 = cast[pointer](cast[uint](mult.key.q) + 1'u)
|
|
|
|
copyMem(addr data[0], qplus1, result)
|
|
|
|
|
|
|
|
proc getSecret*(pubkey: EcPublicKey, seckey: EcPrivateKey): seq[byte] =
|
|
|
|
## Calculate ECDHE shared secret using Go's elliptic curve approach, using
|
|
|
|
## remote public key ``pubkey`` and local private key ``seckey`` and return
|
|
|
|
## shared secret.
|
|
|
|
##
|
|
|
|
## If error happens length of result array will be ``0``.
|
2019-09-11 16:03:39 +00:00
|
|
|
doAssert((not isNil(pubkey)) and (not isNil(seckey)))
|
2019-03-02 19:19:41 +00:00
|
|
|
var data: array[Secret521Length, byte]
|
2019-03-01 06:34:52 +00:00
|
|
|
let res = toSecret(pubkey, seckey, data)
|
|
|
|
if res > 0:
|
|
|
|
result = newSeq[byte](res)
|
|
|
|
copyMem(addr result[0], addr data[0], res)
|
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
proc sign*[T: byte | char](
|
|
|
|
seckey: EcPrivateKey, message: openArray[T]
|
|
|
|
): EcResult[EcSignature] {.gcsafe.} =
|
2019-02-25 18:03:52 +00:00
|
|
|
## Get ECDSA signature of data ``message`` using private key ``seckey``.
|
2020-05-18 05:25:55 +00:00
|
|
|
if isNil(seckey):
|
|
|
|
return err(EcKeyIncorrectError)
|
2022-06-16 08:08:52 +00:00
|
|
|
var hc: HashCompatContext
|
2019-02-19 22:11:59 +00:00
|
|
|
var hash: array[32, byte]
|
2022-06-16 08:08:52 +00:00
|
|
|
var impl = ecGetDefault()
|
2019-02-22 11:32:15 +00:00
|
|
|
if seckey.key.curve in EcSupportedCurvesCint:
|
2020-05-18 05:25:55 +00:00
|
|
|
var sig = new EcSignature
|
|
|
|
sig.buffer = newSeq[byte](256)
|
2019-02-22 11:32:15 +00:00
|
|
|
var kv = addr sha256Vtable
|
|
|
|
kv.init(addr hc.vtable)
|
|
|
|
if len(message) > 0:
|
2022-06-16 08:08:52 +00:00
|
|
|
kv.update(addr hc.vtable, unsafeAddr message[0], uint(len(message)))
|
2019-02-19 22:11:59 +00:00
|
|
|
else:
|
2019-02-22 11:32:15 +00:00
|
|
|
kv.update(addr hc.vtable, nil, 0)
|
2022-06-16 08:08:52 +00:00
|
|
|
kv.out(addr hc.vtable, addr hash[0])
|
2024-06-11 15:18:06 +00:00
|
|
|
let res =
|
|
|
|
ecdsaI31SignAsn1(impl, kv, addr hash[0], addr seckey.key, addr sig.buffer[0])
|
2019-02-22 11:32:15 +00:00
|
|
|
# Clear context with initial value
|
|
|
|
kv.init(addr hc.vtable)
|
|
|
|
if res != 0:
|
2020-05-18 05:25:55 +00:00
|
|
|
sig.buffer.setLen(res)
|
|
|
|
ok(sig)
|
2019-02-22 11:32:15 +00:00
|
|
|
else:
|
2020-05-18 05:25:55 +00:00
|
|
|
err(EcSignatureError)
|
2019-02-19 22:11:59 +00:00
|
|
|
else:
|
2020-05-18 05:25:55 +00:00
|
|
|
err(EcKeyIncorrectError)
|
2019-02-19 22:11:59 +00:00
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
proc verify*[T: byte | char](
|
|
|
|
sig: EcSignature, message: openArray[T], pubkey: EcPublicKey
|
|
|
|
): bool {.inline.} =
|
2019-02-19 22:11:59 +00:00
|
|
|
## Verify ECDSA signature ``sig`` using public key ``pubkey`` and data
|
|
|
|
## ``message``.
|
|
|
|
##
|
|
|
|
## Return ``true`` if message verification succeeded, ``false`` if
|
|
|
|
## verification failed.
|
2019-09-11 16:03:39 +00:00
|
|
|
doAssert((not isNil(sig)) and (not isNil(pubkey)))
|
2022-06-16 08:08:52 +00:00
|
|
|
var hc: HashCompatContext
|
2019-02-19 22:11:59 +00:00
|
|
|
var hash: array[32, byte]
|
2022-06-16 08:08:52 +00:00
|
|
|
var impl = ecGetDefault()
|
2019-02-22 11:32:15 +00:00
|
|
|
if pubkey.key.curve in EcSupportedCurvesCint:
|
|
|
|
var kv = addr sha256Vtable
|
|
|
|
kv.init(addr hc.vtable)
|
|
|
|
if len(message) > 0:
|
2022-06-16 08:08:52 +00:00
|
|
|
kv.update(addr hc.vtable, unsafeAddr message[0], uint(len(message)))
|
2019-02-22 11:32:15 +00:00
|
|
|
else:
|
|
|
|
kv.update(addr hc.vtable, nil, 0)
|
2022-06-16 08:08:52 +00:00
|
|
|
kv.out(addr hc.vtable, addr hash[0])
|
2024-06-11 15:18:06 +00:00
|
|
|
let res = ecdsaI31VrfyAsn1(
|
|
|
|
impl,
|
|
|
|
addr hash[0],
|
|
|
|
uint(len(hash)),
|
|
|
|
unsafeAddr pubkey.key,
|
|
|
|
addr sig.buffer[0],
|
|
|
|
uint(len(sig.buffer)),
|
|
|
|
)
|
2019-02-22 11:32:15 +00:00
|
|
|
# Clear context with initial value
|
|
|
|
kv.init(addr hc.vtable)
|
|
|
|
result = (res == 1)
|
2023-08-30 09:39:48 +00:00
|
|
|
|
|
|
|
type ECDHEScheme* = EcCurveKind
|
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
proc ephemeral*(scheme: ECDHEScheme, rng: var HmacDrbgContext): EcResult[EcKeyPair] =
|
2023-08-30 09:39:48 +00:00
|
|
|
## Generate ephemeral keys used to perform ECDHE.
|
|
|
|
var keypair: EcKeyPair
|
|
|
|
if scheme == Secp256r1:
|
2024-06-11 15:18:06 +00:00
|
|
|
keypair = ?EcKeyPair.random(Secp256r1, rng)
|
2023-08-30 09:39:48 +00:00
|
|
|
elif scheme == Secp384r1:
|
2024-06-11 15:18:06 +00:00
|
|
|
keypair = ?EcKeyPair.random(Secp384r1, rng)
|
2023-08-30 09:39:48 +00:00
|
|
|
elif scheme == Secp521r1:
|
2024-06-11 15:18:06 +00:00
|
|
|
keypair = ?EcKeyPair.random(Secp521r1, rng)
|
2023-08-30 09:39:48 +00:00
|
|
|
ok(keypair)
|
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
proc ephemeral*(scheme: string, rng: var HmacDrbgContext): EcResult[EcKeyPair] =
|
2023-08-30 09:39:48 +00:00
|
|
|
## Generate ephemeral keys used to perform ECDHE using string encoding.
|
|
|
|
##
|
|
|
|
## Currently supported encoding strings are P-256, P-384, P-521, if encoding
|
|
|
|
## string is not supported P-521 key will be generated.
|
|
|
|
if scheme == "P-256":
|
|
|
|
ephemeral(Secp256r1, rng)
|
|
|
|
elif scheme == "P-384":
|
|
|
|
ephemeral(Secp384r1, rng)
|
|
|
|
elif scheme == "P-521":
|
|
|
|
ephemeral(Secp521r1, rng)
|
|
|
|
else:
|
|
|
|
ephemeral(Secp521r1, rng)
|