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.
|
2020-05-18 05:25:55 +00:00
|
|
|
|
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/rand
|
2020-07-07 11:14:11 +00:00
|
|
|
import secp256k1, stew/[byteutils, results], nimcrypto/[hash, sha2]
|
|
|
|
|
2022-06-16 08:08:52 +00:00
|
|
|
export sha2, results, rand
|
2019-09-02 18:55:42 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
SkRawPrivateKeySize* = 256 div 8 ## Size of private key in octets (bytes)
|
|
|
|
SkRawSignatureSize* = SkRawPrivateKeySize * 2 + 1
|
|
|
|
## Size of signature in octets (bytes)
|
|
|
|
SkRawPublicKeySize* = SkRawPrivateKeySize + 1 ## Size of public key in octets (bytes)
|
|
|
|
|
2020-05-19 12:48:55 +00:00
|
|
|
# This is extremely confusing but it's to avoid.. confusion between Eth standard and Secp standard
|
2019-09-02 18:55:42 +00:00
|
|
|
type
|
2020-05-19 12:48:55 +00:00
|
|
|
SkPrivateKey* = distinct secp256k1.SkSecretKey
|
|
|
|
SkPublicKey* = distinct secp256k1.SkPublicKey
|
|
|
|
SkSignature* = distinct secp256k1.SkSignature
|
|
|
|
SkKeyPair* = distinct secp256k1.SkKeyPair
|
|
|
|
|
2022-06-16 08:08:52 +00:00
|
|
|
proc random*(t: typedesc[SkPrivateKey], rng: var HmacDrbgContext): SkPrivateKey =
|
|
|
|
#TODO is there a better way?
|
|
|
|
var rngPtr = addr rng
|
2020-07-07 11:14:11 +00:00
|
|
|
proc callRng(data: var openArray[byte]) =
|
2022-06-16 08:08:52 +00:00
|
|
|
hmacDrbgGenerate(rngPtr[], data)
|
2020-07-07 11:14:11 +00:00
|
|
|
|
|
|
|
SkPrivateKey(SkSecretKey.random(callRng))
|
|
|
|
|
2022-06-16 08:08:52 +00:00
|
|
|
proc random*(t: typedesc[SkKeyPair], rng: var HmacDrbgContext): SkKeyPair =
|
|
|
|
let rngPtr = addr rng
|
2020-07-07 11:14:11 +00:00
|
|
|
proc callRng(data: var openArray[byte]) =
|
2022-06-16 08:08:52 +00:00
|
|
|
hmacDrbgGenerate(rngPtr[], data)
|
2020-05-19 12:48:55 +00:00
|
|
|
|
2020-07-07 11:14:11 +00:00
|
|
|
SkKeyPair(secp256k1.SkKeyPair.random(callRng))
|
2020-05-19 12:48:55 +00:00
|
|
|
|
|
|
|
template seckey*(v: SkKeyPair): SkPrivateKey =
|
|
|
|
SkPrivateKey(secp256k1.SkKeyPair(v).seckey)
|
2019-09-02 18:55:42 +00:00
|
|
|
|
2020-05-19 12:48:55 +00:00
|
|
|
template pubkey*(v: SkKeyPair): SkPublicKey =
|
|
|
|
SkPublicKey(secp256k1.SkKeyPair(v).pubkey)
|
2020-04-17 10:51:13 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc init*(key: var SkPrivateKey, data: openArray[byte]): SkResult[void] =
|
2019-09-02 18:55:42 +00:00
|
|
|
## Initialize Secp256k1 `private key` ``key`` from raw binary
|
|
|
|
## representation ``data``.
|
2020-05-19 12:48:55 +00:00
|
|
|
key = SkPrivateKey(?secp256k1.SkSecretKey.fromRaw(data))
|
2020-05-18 05:25:55 +00:00
|
|
|
ok()
|
2019-09-02 18:55:42 +00:00
|
|
|
|
2020-05-18 05:25:55 +00:00
|
|
|
proc init*(key: var SkPrivateKey, data: string): SkResult[void] =
|
2019-09-02 18:55:42 +00:00
|
|
|
## Initialize Secp256k1 `private key` ``key`` from hexadecimal string
|
|
|
|
## representation ``data``.
|
2020-05-19 12:48:55 +00:00
|
|
|
key = SkPrivateKey(?secp256k1.SkSecretKey.fromHex(data))
|
2020-05-18 05:25:55 +00:00
|
|
|
ok()
|
2019-09-02 18:55:42 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc init*(key: var SkPublicKey, data: openArray[byte]): SkResult[void] =
|
2019-09-02 18:55:42 +00:00
|
|
|
## Initialize Secp256k1 `public key` ``key`` from raw binary
|
|
|
|
## representation ``data``.
|
2020-05-19 12:48:55 +00:00
|
|
|
key = SkPublicKey(?secp256k1.SkPublicKey.fromRaw(data))
|
2020-05-18 05:25:55 +00:00
|
|
|
ok()
|
2020-04-17 10:51:13 +00:00
|
|
|
|
2020-05-18 05:25:55 +00:00
|
|
|
proc init*(key: var SkPublicKey, data: string): SkResult[void] =
|
2019-09-02 18:55:42 +00:00
|
|
|
## Initialize Secp256k1 `public key` ``key`` from hexadecimal string
|
|
|
|
## representation ``data``.
|
2020-05-19 12:48:55 +00:00
|
|
|
key = SkPublicKey(?secp256k1.SkPublicKey.fromHex(data))
|
2020-05-18 05:25:55 +00:00
|
|
|
ok()
|
2019-09-02 18:55:42 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc init*(sig: var SkSignature, data: openArray[byte]): SkResult[void] =
|
2019-09-02 18:55:42 +00:00
|
|
|
## Initialize Secp256k1 `signature` ``sig`` from raw binary
|
|
|
|
## representation ``data``.
|
2020-05-19 12:48:55 +00:00
|
|
|
sig = SkSignature(?secp256k1.SkSignature.fromDer(data))
|
2020-05-18 05:25:55 +00:00
|
|
|
ok()
|
2020-04-17 10:51:13 +00:00
|
|
|
|
2020-05-18 05:25:55 +00:00
|
|
|
proc init*(sig: var SkSignature, data: string): SkResult[void] =
|
2019-09-02 18:55:42 +00:00
|
|
|
## Initialize Secp256k1 `signature` ``sig`` from hexadecimal string
|
|
|
|
## representation ``data``.
|
2020-04-17 10:51:13 +00:00
|
|
|
# TODO DER vs raw here is fishy
|
2019-09-02 18:55:42 +00:00
|
|
|
var buffer: seq[byte]
|
|
|
|
try:
|
2020-04-17 10:51:13 +00:00
|
|
|
buffer = hexToSeqByte(data)
|
|
|
|
except ValueError:
|
2020-05-31 14:22:49 +00:00
|
|
|
return err("secp: Hex to bytes failed")
|
2020-05-18 05:25:55 +00:00
|
|
|
init(sig, buffer)
|
2019-09-02 18:55:42 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc init*(t: typedesc[SkPrivateKey], data: openArray[byte]): SkResult[SkPrivateKey] =
|
2019-09-02 18:55:42 +00:00
|
|
|
## Initialize Secp256k1 `private key` from raw binary
|
|
|
|
## representation ``data``.
|
|
|
|
##
|
|
|
|
## Procedure returns `private key` on success.
|
2020-07-07 11:14:11 +00:00
|
|
|
SkSecretKey.fromRaw(data).mapConvert(SkPrivateKey)
|
2019-09-02 18:55:42 +00:00
|
|
|
|
2020-05-18 05:25:55 +00:00
|
|
|
proc init*(t: typedesc[SkPrivateKey], data: string): SkResult[SkPrivateKey] =
|
2019-09-02 18:55:42 +00:00
|
|
|
## Initialize Secp256k1 `private key` from hexadecimal string
|
|
|
|
## representation ``data``.
|
|
|
|
##
|
|
|
|
## Procedure returns `private key` on success.
|
2020-07-07 11:14:11 +00:00
|
|
|
SkSecretKey.fromHex(data).mapConvert(SkPrivateKey)
|
2019-09-02 18:55:42 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc init*(t: typedesc[SkPublicKey], data: openArray[byte]): SkResult[SkPublicKey] =
|
2019-09-02 18:55:42 +00:00
|
|
|
## Initialize Secp256k1 `public key` from raw binary
|
|
|
|
## representation ``data``.
|
|
|
|
##
|
|
|
|
## Procedure returns `public key` on success.
|
2020-05-18 05:25:55 +00:00
|
|
|
var key: SkPublicKey
|
|
|
|
key.init(data) and ok(key)
|
2019-09-02 18:55:42 +00:00
|
|
|
|
2020-05-18 05:25:55 +00:00
|
|
|
proc init*(t: typedesc[SkPublicKey], data: string): SkResult[SkPublicKey] =
|
2019-09-02 18:55:42 +00:00
|
|
|
## Initialize Secp256k1 `public key` from hexadecimal string
|
|
|
|
## representation ``data``.
|
|
|
|
##
|
|
|
|
## Procedure returns `public key` on success.
|
2020-05-18 05:25:55 +00:00
|
|
|
var key: SkPublicKey
|
|
|
|
key.init(data) and ok(key)
|
2019-09-02 18:55:42 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc init*(t: typedesc[SkSignature], data: openArray[byte]): SkResult[SkSignature] =
|
2019-09-02 18:55:42 +00:00
|
|
|
## Initialize Secp256k1 `signature` from raw binary
|
|
|
|
## representation ``data``.
|
|
|
|
##
|
|
|
|
## Procedure returns `signature` on success.
|
2020-05-18 05:25:55 +00:00
|
|
|
var sig: SkSignature
|
|
|
|
sig.init(data) and ok(sig)
|
2019-09-02 18:55:42 +00:00
|
|
|
|
2020-05-18 05:25:55 +00:00
|
|
|
proc init*(t: typedesc[SkSignature], data: string): SkResult[SkSignature] =
|
2019-09-02 18:55:42 +00:00
|
|
|
## Initialize Secp256k1 `signature` from hexadecimal string
|
|
|
|
## representation ``data``.
|
|
|
|
##
|
|
|
|
## Procedure returns `signature` on success.
|
2020-05-18 05:25:55 +00:00
|
|
|
var sig: SkSignature
|
|
|
|
sig.init(data) and ok(sig)
|
2019-09-02 18:55:42 +00:00
|
|
|
|
2021-09-02 10:03:40 +00:00
|
|
|
proc getPublicKey*(key: SkPrivateKey): SkPublicKey =
|
2019-09-02 18:55:42 +00:00
|
|
|
## Calculate and return Secp256k1 `public key` from `private key` ``key``.
|
2020-06-22 17:03:15 +00:00
|
|
|
SkPublicKey(SkSecretKey(key).toPublicKey())
|
2019-09-02 18:55:42 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc toBytes*(key: SkPrivateKey, data: var openArray[byte]): SkResult[int] =
|
2019-09-02 18:55:42 +00:00
|
|
|
## Serialize Secp256k1 `private key` ``key`` to raw binary form and store it
|
|
|
|
## to ``data``.
|
|
|
|
##
|
|
|
|
## Procedure returns number of bytes (octets) needed to store
|
|
|
|
## Secp256k1 private key.
|
|
|
|
if len(data) >= SkRawPrivateKeySize:
|
2020-05-18 05:25:55 +00:00
|
|
|
data[0 ..< SkRawPrivateKeySize] = SkSecretKey(key).toRaw()
|
|
|
|
ok(SkRawPrivateKeySize)
|
|
|
|
else:
|
2020-05-31 14:22:49 +00:00
|
|
|
err("secp: Not enough bytes")
|
2019-09-02 18:55:42 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc toBytes*(key: SkPublicKey, data: var openArray[byte]): SkResult[int] =
|
2019-09-02 18:55:42 +00:00
|
|
|
## Serialize Secp256k1 `public key` ``key`` to raw binary form and store it
|
|
|
|
## to ``data``.
|
|
|
|
##
|
|
|
|
## Procedure returns number of bytes (octets) needed to store
|
|
|
|
## Secp256k1 public key.
|
|
|
|
if len(data) >= SkRawPublicKeySize:
|
2020-05-19 12:48:55 +00:00
|
|
|
data[0 ..< SkRawPublicKeySize] = secp256k1.SkPublicKey(key).toRawCompressed()
|
2020-05-18 05:25:55 +00:00
|
|
|
ok(SkRawPublicKeySize)
|
|
|
|
else:
|
2020-05-31 14:22:49 +00:00
|
|
|
err("secp: Not enough bytes")
|
2019-09-02 18:55:42 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc toBytes*(sig: SkSignature, data: var openArray[byte]): int =
|
2019-09-02 18:55:42 +00:00
|
|
|
## Serialize Secp256k1 `signature` ``sig`` to raw binary form and store it
|
|
|
|
## to ``data``.
|
|
|
|
##
|
|
|
|
## Procedure returns number of bytes (octets) needed to store
|
|
|
|
## Secp256k1 signature.
|
2020-05-19 12:48:55 +00:00
|
|
|
secp256k1.SkSignature(sig).toDer(data)
|
2019-09-02 18:55:42 +00:00
|
|
|
|
|
|
|
proc getBytes*(key: SkPrivateKey): seq[byte] {.inline.} =
|
|
|
|
## Serialize Secp256k1 `private key` and return it.
|
2020-07-07 11:14:11 +00:00
|
|
|
@(SkSecretKey(key).toRaw())
|
2019-09-02 18:55:42 +00:00
|
|
|
|
|
|
|
proc getBytes*(key: SkPublicKey): seq[byte] {.inline.} =
|
|
|
|
## Serialize Secp256k1 `public key` and return it.
|
2020-07-07 11:14:11 +00:00
|
|
|
@(secp256k1.SkPublicKey(key).toRawCompressed())
|
2019-09-02 18:55:42 +00:00
|
|
|
|
|
|
|
proc getBytes*(sig: SkSignature): seq[byte] {.inline.} =
|
|
|
|
## Serialize Secp256k1 `signature` and return it.
|
2020-01-09 08:33:57 +00:00
|
|
|
result = newSeq[byte](72)
|
|
|
|
let length = toBytes(sig, result)
|
|
|
|
result.setLen(length)
|
2019-09-02 18:55:42 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc sign*[T: byte | char](key: SkPrivateKey, msg: openArray[T]): SkSignature =
|
2019-09-02 18:55:42 +00:00
|
|
|
## Sign message `msg` using private key `key` and return signature object.
|
2020-04-17 10:51:13 +00:00
|
|
|
let h = sha256.digest(msg)
|
2020-07-07 11:14:11 +00:00
|
|
|
SkSignature(sign(SkSecretKey(key), SkMessage(h.data)))
|
2019-09-02 18:55:42 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc verify*[T: byte | char](
|
|
|
|
sig: SkSignature, msg: openArray[T], key: SkPublicKey
|
2019-09-02 18:55:42 +00:00
|
|
|
): bool =
|
2020-04-17 10:51:13 +00:00
|
|
|
let h = sha256.digest(msg)
|
2020-07-07 11:14:11 +00:00
|
|
|
verify(secp256k1.SkSignature(sig), SkMessage(h.data), secp256k1.SkPublicKey(key))
|
2020-05-19 12:48:55 +00:00
|
|
|
|
2021-07-11 07:59:42 +00:00
|
|
|
func clear*(key: var SkPrivateKey) =
|
|
|
|
clear(secp256k1.SkSecretKey(key))
|
2024-06-11 15:18:06 +00:00
|
|
|
|
2021-07-11 07:59:42 +00:00
|
|
|
func `$`*(key: SkPrivateKey): string =
|
|
|
|
$secp256k1.SkSecretKey(key)
|
|
|
|
func `$`*(key: SkPublicKey): string =
|
|
|
|
$secp256k1.SkPublicKey(key)
|
|
|
|
func `$`*(key: SkSignature): string =
|
|
|
|
$secp256k1.SkSignature(key)
|
|
|
|
func `$`*(key: SkKeyPair): string =
|
|
|
|
$secp256k1.SkKeyPair(key)
|
|
|
|
|
|
|
|
func `==`*(a, b: SkPrivateKey): bool =
|
|
|
|
secp256k1.SkSecretKey(a) == secp256k1.SkSecretKey(b)
|
|
|
|
func `==`*(a, b: SkPublicKey): bool =
|
|
|
|
secp256k1.SkPublicKey(a) == secp256k1.SkPublicKey(b)
|
|
|
|
func `==`*(a, b: SkSignature): bool =
|
|
|
|
secp256k1.SkSignature(a) == secp256k1.SkSignature(b)
|
|
|
|
func `==`*(a, b: SkKeyPair): bool =
|
2021-09-09 10:30:46 +00:00
|
|
|
secp256k1.SkKeyPair(a) == secp256k1.SkKeyPair(b)
|