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-24 00:07:13 +00:00
|
|
|
|
2019-02-28 12:17:39 +00:00
|
|
|
## This module implements constant-time RSA PKCS#1.5 DSA.
|
|
|
|
##
|
|
|
|
## This module uses unmodified parts of code from
|
|
|
|
## BearSSL library <https://bearssl.org/>
|
|
|
|
## Copyright(C) 2018 Thomas Pornin <pornin@bolet.org>.
|
2020-05-18 05:25:55 +00:00
|
|
|
|
2023-06-07 11:12:49 +00:00
|
|
|
{.push raises: [].}
|
2022-08-03 11:33:19 +00:00
|
|
|
|
2022-06-16 08:08:52 +00:00
|
|
|
import bearssl/[rsa, rand, hash]
|
2019-10-07 13:15:27 +00:00
|
|
|
import minasn1
|
2020-08-08 06:53:33 +00:00
|
|
|
import stew/[results, ctops]
|
2020-08-11 14:48:21 +00:00
|
|
|
# We use `ncrutils` for constant-time hexadecimal encoding/decoding procedures.
|
|
|
|
import nimcrypto/utils as ncrutils
|
|
|
|
|
|
|
|
export Asn1Error, results
|
2019-02-19 22:11:59 +00:00
|
|
|
|
|
|
|
const
|
2020-07-15 08:24:04 +00:00
|
|
|
DefaultPublicExponent* = 65537'u32
|
2019-02-24 00:07:13 +00:00
|
|
|
## Default value for RSA public exponent.
|
2020-07-15 08:24:04 +00:00
|
|
|
## https://golang.org/src/crypto/rsa/rsa.go#226
|
|
|
|
MinKeySize* = 2048
|
2019-02-24 00:07:13 +00:00
|
|
|
## Minimal allowed RSA key size in bits.
|
2020-07-15 08:24:04 +00:00
|
|
|
## https://github.com/libp2p/go-libp2p-core/blob/master/crypto/rsa_common.go#L13
|
2019-02-24 00:07:13 +00:00
|
|
|
DefaultKeySize* = 3072 ## Default RSA key size in bits.
|
|
|
|
|
2022-06-16 08:08:52 +00:00
|
|
|
RsaOidSha1* = [byte 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A]
|
2019-02-24 00:07:13 +00:00
|
|
|
## RSA PKCS#1.5 SHA-1 hash object identifier.
|
2022-06-16 08:08:52 +00:00
|
|
|
RsaOidSha224* = [byte 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04]
|
2019-02-24 00:07:13 +00:00
|
|
|
## RSA PKCS#1.5 SHA-224 hash object identifier.
|
2022-06-16 08:08:52 +00:00
|
|
|
RsaOidSha256* = [byte 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01]
|
2019-02-24 00:07:13 +00:00
|
|
|
## RSA PKCS#1.5 SHA-256 hash object identifier.
|
2022-06-16 08:08:52 +00:00
|
|
|
RsaOidSha384* = [byte 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02]
|
2019-02-24 00:07:13 +00:00
|
|
|
## RSA PKCS#1.5 SHA-384 hash object identifier.
|
2022-06-16 08:08:52 +00:00
|
|
|
RsaOidSha512* = [byte 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03]
|
2019-02-24 00:07:13 +00:00
|
|
|
## RSA PKCS#1.5 SHA-512 hash object identifier.
|
2019-02-19 22:11:59 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
RsaPrivateKey* = ref object
|
|
|
|
buffer*: seq[byte]
|
2022-06-16 08:08:52 +00:00
|
|
|
seck*: rsa.RsaPrivateKey
|
|
|
|
pubk*: rsa.RsaPublicKey
|
|
|
|
pexp*: ptr byte
|
|
|
|
pexplen*: uint
|
2019-02-19 22:11:59 +00:00
|
|
|
|
|
|
|
RsaPublicKey* = ref object
|
|
|
|
buffer*: seq[byte]
|
2022-06-16 08:08:52 +00:00
|
|
|
key*: rsa.RsaPublicKey
|
2019-02-19 22:11:59 +00:00
|
|
|
|
2019-02-24 00:07:13 +00:00
|
|
|
RsaKeyPair* = RsaPrivateKey
|
|
|
|
|
|
|
|
RsaSignature* = ref object
|
|
|
|
buffer*: seq[byte]
|
2019-02-19 22:11:59 +00:00
|
|
|
|
2019-02-24 00:07:13 +00:00
|
|
|
RsaPKI* = RsaPrivateKey | RsaPublicKey | RsaSignature
|
|
|
|
RsaKP* = RsaPrivateKey | RsaKeyPair
|
2019-02-19 22:11:59 +00:00
|
|
|
|
2020-05-18 05:25:55 +00:00
|
|
|
RsaError* = enum
|
|
|
|
RsaGenError
|
|
|
|
RsaKeyIncorrectError
|
2020-07-15 08:24:04 +00:00
|
|
|
RsaSignatureError
|
|
|
|
RsaLowSecurityError
|
2020-05-18 05:25:55 +00:00
|
|
|
|
|
|
|
RsaResult*[T] = Result[T, RsaError]
|
2019-02-19 22:11:59 +00:00
|
|
|
|
2019-02-24 00:07:13 +00:00
|
|
|
template getStart(bs, os, ls: untyped): untyped =
|
|
|
|
let p = cast[uint](os)
|
|
|
|
let s = cast[uint](unsafeAddr bs[0])
|
|
|
|
var so = 0
|
|
|
|
if p >= s:
|
|
|
|
so = cast[int](p - s)
|
|
|
|
so
|
|
|
|
|
|
|
|
template getFinish(bs, os, ls: untyped): untyped =
|
|
|
|
let p = cast[uint](os)
|
|
|
|
let s = cast[uint](unsafeAddr bs[0])
|
|
|
|
var eo = -1
|
|
|
|
if p >= s:
|
|
|
|
let so = cast[int](p - s)
|
2022-06-16 08:08:52 +00:00
|
|
|
if so + int(ls) <= len(bs):
|
|
|
|
eo = so + int(ls) - 1
|
2019-02-24 00:07:13 +00:00
|
|
|
eo
|
|
|
|
|
|
|
|
template getArray*(bs, os, ls: untyped): untyped =
|
|
|
|
toOpenArray(bs, getStart(bs, os, ls), getFinish(bs, os, ls))
|
2019-02-19 22:11:59 +00:00
|
|
|
|
2019-02-24 00:07:13 +00:00
|
|
|
template trimZeroes(b: seq[byte], pt, ptlen: untyped) =
|
|
|
|
var length = ptlen
|
|
|
|
for i in 0 ..< length:
|
2022-06-16 08:08:52 +00:00
|
|
|
if pt[] != byte(0x00):
|
2019-02-24 00:07:13 +00:00
|
|
|
break
|
2022-06-16 08:08:52 +00:00
|
|
|
pt = cast[ptr byte](cast[uint](pt) + 1)
|
2019-02-24 00:07:13 +00:00
|
|
|
ptlen -= 1
|
|
|
|
|
2022-06-16 08:08:52 +00:00
|
|
|
proc random*[T: RsaKP](
|
|
|
|
t: typedesc[T],
|
|
|
|
rng: var HmacDrbgContext,
|
2020-07-07 11:14:11 +00:00
|
|
|
bits = DefaultKeySize,
|
2020-05-18 05:25:55 +00:00
|
|
|
pubexp = DefaultPublicExponent,
|
|
|
|
): RsaResult[T] =
|
2019-02-19 22:11:59 +00:00
|
|
|
## Generate new random RSA private key using BearSSL's HMAC-SHA256-DRBG
|
|
|
|
## algorithm.
|
|
|
|
##
|
2019-02-24 00:07:13 +00:00
|
|
|
## ``bits`` number of bits in RSA key, must be in
|
2020-07-15 08:24:04 +00:00
|
|
|
## range [2048, 4096] (default = 3072).
|
2019-02-24 00:07:13 +00:00
|
|
|
##
|
|
|
|
## ``pubexp`` is RSA public exponent, which must be prime (default = 3).
|
2020-07-15 08:24:04 +00:00
|
|
|
if bits < MinKeySize:
|
|
|
|
return err(RsaLowSecurityError)
|
|
|
|
|
2020-07-07 11:14:11 +00:00
|
|
|
let
|
|
|
|
sko = 0
|
2022-06-16 08:08:52 +00:00
|
|
|
pko = rsaKbufPrivSize(bits)
|
|
|
|
eko = pko + rsaKbufPubSize(bits)
|
2020-07-07 11:14:11 +00:00
|
|
|
length = eko + ((bits + 7) shr 3)
|
2019-02-24 00:07:13 +00:00
|
|
|
|
2020-07-07 11:14:11 +00:00
|
|
|
let res = new T
|
2020-05-18 05:25:55 +00:00
|
|
|
res.buffer = newSeq[byte](length)
|
2020-07-07 11:14:11 +00:00
|
|
|
|
2022-06-16 08:08:52 +00:00
|
|
|
var keygen = rsaKeygenGetDefault()
|
2020-07-07 11:14:11 +00:00
|
|
|
|
2019-02-19 22:11:59 +00:00
|
|
|
if keygen(
|
|
|
|
addr rng.vtable,
|
2020-05-18 05:25:55 +00:00
|
|
|
addr res.seck,
|
|
|
|
addr res.buffer[sko],
|
|
|
|
addr res.pubk,
|
|
|
|
addr res.buffer[pko],
|
2019-02-24 00:07:13 +00:00
|
|
|
cuint(bits),
|
|
|
|
pubexp,
|
|
|
|
) == 0:
|
2020-05-18 05:25:55 +00:00
|
|
|
return err(RsaGenError)
|
2019-02-19 22:11:59 +00:00
|
|
|
|
2020-07-07 11:14:11 +00:00
|
|
|
let
|
2022-06-16 08:08:52 +00:00
|
|
|
compute = rsaComputePrivexpGetDefault()
|
2020-07-07 11:14:11 +00:00
|
|
|
computed = compute(addr res.buffer[eko], addr res.seck, pubexp)
|
2020-05-18 05:25:55 +00:00
|
|
|
if computed == 0:
|
|
|
|
return err(RsaGenError)
|
|
|
|
|
2022-06-16 08:08:52 +00:00
|
|
|
res.pexp = addr res.buffer[eko]
|
2020-05-18 05:25:55 +00:00
|
|
|
res.pexplen = computed
|
|
|
|
|
|
|
|
trimZeroes(res.buffer, res.seck.p, res.seck.plen)
|
|
|
|
trimZeroes(res.buffer, res.seck.q, res.seck.qlen)
|
|
|
|
trimZeroes(res.buffer, res.seck.dp, res.seck.dplen)
|
|
|
|
trimZeroes(res.buffer, res.seck.dq, res.seck.dqlen)
|
|
|
|
trimZeroes(res.buffer, res.seck.iq, res.seck.iqlen)
|
|
|
|
trimZeroes(res.buffer, res.pubk.n, res.pubk.nlen)
|
|
|
|
trimZeroes(res.buffer, res.pubk.e, res.pubk.elen)
|
|
|
|
trimZeroes(res.buffer, res.pexp, res.pexplen)
|
|
|
|
|
|
|
|
ok(res)
|
2019-02-24 00:07:13 +00:00
|
|
|
|
|
|
|
proc copy*[T: RsaPKI](key: T): T =
|
|
|
|
## Create copy of RSA private key, public key or signature.
|
2019-09-11 16:03:39 +00:00
|
|
|
doAssert(not isNil(key))
|
2019-02-24 00:07:13 +00:00
|
|
|
when T is RsaPrivateKey:
|
|
|
|
if len(key.buffer) > 0:
|
2022-06-16 08:08:52 +00:00
|
|
|
let length =
|
|
|
|
key.seck.plen.uint + key.seck.qlen.uint + key.seck.dplen.uint +
|
|
|
|
key.seck.dqlen.uint + key.seck.iqlen.uint + key.pubk.nlen.uint +
|
|
|
|
key.pubk.elen.uint + key.pexplen.uint
|
2019-02-24 00:07:13 +00:00
|
|
|
result = new RsaPrivateKey
|
|
|
|
result.buffer = newSeq[byte](length)
|
2022-06-16 08:08:52 +00:00
|
|
|
let po: uint = 0
|
2019-02-24 00:07:13 +00:00
|
|
|
let qo = po + key.seck.plen
|
|
|
|
let dpo = qo + key.seck.qlen
|
|
|
|
let dqo = dpo + key.seck.dplen
|
|
|
|
let iqo = dqo + key.seck.dqlen
|
|
|
|
let no = iqo + key.seck.iqlen
|
|
|
|
let eo = no + key.pubk.nlen
|
|
|
|
let peo = eo + key.pubk.elen
|
|
|
|
copyMem(addr result.buffer[po], key.seck.p, key.seck.plen)
|
|
|
|
copyMem(addr result.buffer[qo], key.seck.q, key.seck.qlen)
|
|
|
|
copyMem(addr result.buffer[dpo], key.seck.dp, key.seck.dplen)
|
|
|
|
copyMem(addr result.buffer[dqo], key.seck.dq, key.seck.dqlen)
|
|
|
|
copyMem(addr result.buffer[iqo], key.seck.iq, key.seck.iqlen)
|
|
|
|
copyMem(addr result.buffer[no], key.pubk.n, key.pubk.nlen)
|
|
|
|
copyMem(addr result.buffer[eo], key.pubk.e, key.pubk.elen)
|
|
|
|
copyMem(addr result.buffer[peo], key.pexp, key.pexplen)
|
2022-06-16 08:08:52 +00:00
|
|
|
result.seck.p = addr result.buffer[po]
|
|
|
|
result.seck.q = addr result.buffer[qo]
|
|
|
|
result.seck.dp = addr result.buffer[dpo]
|
|
|
|
result.seck.dq = addr result.buffer[dqo]
|
|
|
|
result.seck.iq = addr result.buffer[iqo]
|
|
|
|
result.pubk.n = addr result.buffer[no]
|
|
|
|
result.pubk.e = addr result.buffer[eo]
|
|
|
|
result.pexp = addr result.buffer[peo]
|
2019-02-24 00:07:13 +00:00
|
|
|
result.seck.plen = key.seck.plen
|
|
|
|
result.seck.qlen = key.seck.qlen
|
|
|
|
result.seck.dplen = key.seck.dplen
|
|
|
|
result.seck.dqlen = key.seck.dqlen
|
|
|
|
result.seck.iqlen = key.seck.iqlen
|
|
|
|
result.pubk.nlen = key.pubk.nlen
|
|
|
|
result.pubk.elen = key.pubk.elen
|
|
|
|
result.pexplen = key.pexplen
|
|
|
|
result.seck.nBitlen = key.seck.nBitlen
|
|
|
|
elif T is RsaPublicKey:
|
|
|
|
if len(key.buffer) > 0:
|
|
|
|
let length = key.key.nlen + key.key.elen
|
|
|
|
result = new RsaPublicKey
|
|
|
|
result.buffer = newSeq[byte](length)
|
|
|
|
let no = 0
|
|
|
|
let eo = no + key.key.nlen
|
|
|
|
copyMem(addr result.buffer[no], key.key.n, key.key.nlen)
|
|
|
|
copyMem(addr result.buffer[eo], key.key.e, key.key.elen)
|
2022-05-24 13:10:57 +00:00
|
|
|
result.key.n = cast[ptr char](addr result.buffer[no])
|
|
|
|
result.key.e = cast[ptr char](addr result.buffer[eo])
|
2019-02-24 00:07:13 +00:00
|
|
|
result.key.nlen = key.key.nlen
|
|
|
|
result.key.elen = key.key.elen
|
|
|
|
elif T is RsaSignature:
|
|
|
|
if len(key.buffer) > 0:
|
|
|
|
result = new RsaSignature
|
|
|
|
result.buffer = key.buffer
|
|
|
|
|
2021-09-02 10:03:40 +00:00
|
|
|
proc getPublicKey*(key: RsaPrivateKey): RsaPublicKey =
|
2019-02-24 00:07:13 +00:00
|
|
|
## Get RSA public key from RSA private key.
|
2019-09-11 16:03:39 +00:00
|
|
|
doAssert(not isNil(key))
|
2019-02-24 00:07:13 +00:00
|
|
|
let length = key.pubk.nlen + key.pubk.elen
|
2019-02-19 22:11:59 +00:00
|
|
|
result = new RsaPublicKey
|
2019-02-24 00:07:13 +00:00
|
|
|
result.buffer = newSeq[byte](length)
|
2022-06-16 08:08:52 +00:00
|
|
|
result.key.n = addr result.buffer[0]
|
|
|
|
result.key.e = addr result.buffer[key.pubk.nlen]
|
2019-02-24 00:07:13 +00:00
|
|
|
copyMem(addr result.buffer[0], cast[pointer](key.pubk.n), key.pubk.nlen)
|
|
|
|
copyMem(addr result.buffer[key.pubk.nlen], cast[pointer](key.pubk.e), key.pubk.elen)
|
|
|
|
result.key.nlen = key.pubk.nlen
|
|
|
|
result.key.elen = key.pubk.elen
|
|
|
|
|
|
|
|
proc seckey*(pair: RsaKeyPair): RsaPrivateKey {.inline.} =
|
|
|
|
## Get RSA private key from pair ``pair``.
|
|
|
|
result = cast[RsaPrivateKey](pair).copy()
|
|
|
|
|
|
|
|
proc pubkey*(pair: RsaKeyPair): RsaPublicKey {.inline.} =
|
|
|
|
## Get RSA public key from pair ``pair``.
|
2021-09-02 10:03:40 +00:00
|
|
|
result = cast[RsaPrivateKey](pair).getPublicKey()
|
2019-02-24 00:07:13 +00:00
|
|
|
|
|
|
|
proc clear*[T: RsaPKI | RsaKeyPair](pki: var T) =
|
|
|
|
## Wipe and clear EC private key, public key or scalar object.
|
2019-09-11 16:03:39 +00:00
|
|
|
doAssert(not isNil(pki))
|
2019-02-24 00:07:13 +00:00
|
|
|
when T is RsaPrivateKey:
|
|
|
|
burnMem(pki.buffer)
|
|
|
|
pki.buffer.setLen(0)
|
|
|
|
pki.seckey.p = nil
|
|
|
|
pki.seckey.q = nil
|
|
|
|
pki.seckey.dp = nil
|
|
|
|
pki.seckey.dq = nil
|
|
|
|
pki.seckey.iq = nil
|
|
|
|
pki.seckey.plen = 0
|
|
|
|
pki.seckey.qlen = 0
|
|
|
|
pki.seckey.dplen = 0
|
|
|
|
pki.seckey.dqlen = 0
|
|
|
|
pki.seckey.iqlen = 0
|
|
|
|
pki.seckey.nBitlen = 0
|
|
|
|
pki.pubkey.n = nil
|
|
|
|
pki.pubkey.e = nil
|
|
|
|
pki.pubkey.nlen = 0
|
|
|
|
pki.pubkey.elen = 0
|
|
|
|
elif T is RsaPublicKey:
|
|
|
|
burnMem(pki.buffer)
|
|
|
|
pki.buffer.setLen(0)
|
|
|
|
pki.key.n = nil
|
|
|
|
pki.key.e = nil
|
|
|
|
pki.key.nlen = 0
|
|
|
|
pki.key.elen = 0
|
|
|
|
elif T is RsaSignature:
|
|
|
|
burnMem(pki.buffer)
|
|
|
|
pki.buffer.setLen(0)
|
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc toBytes*(key: RsaPrivateKey, data: var openArray[byte]): RsaResult[int] =
|
2019-02-24 00:07:13 +00:00
|
|
|
## Serialize RSA private key ``key`` to ASN.1 DER binary form and store it
|
|
|
|
## to ``data``.
|
|
|
|
##
|
|
|
|
## Procedure returns number of bytes (octets) needed to store RSA private key,
|
|
|
|
## or `0` if private key is is incorrect.
|
2020-05-18 05:25:55 +00:00
|
|
|
if isNil(key):
|
|
|
|
err(RsaKeyIncorrectError)
|
|
|
|
elif len(key.buffer) > 0:
|
2019-02-24 00:07:13 +00:00
|
|
|
var b = Asn1Buffer.init()
|
|
|
|
var p = Asn1Composite.init(Asn1Tag.Sequence)
|
|
|
|
p.write(0'u64)
|
|
|
|
p.write(Asn1Tag.Integer, getArray(key.buffer, key.pubk.n, key.pubk.nlen))
|
|
|
|
p.write(Asn1Tag.Integer, getArray(key.buffer, key.pubk.e, key.pubk.elen))
|
|
|
|
p.write(Asn1Tag.Integer, getArray(key.buffer, key.pexp, key.pexplen))
|
|
|
|
p.write(Asn1Tag.Integer, getArray(key.buffer, key.seck.p, key.seck.plen))
|
|
|
|
p.write(Asn1Tag.Integer, getArray(key.buffer, key.seck.q, key.seck.qlen))
|
|
|
|
p.write(Asn1Tag.Integer, getArray(key.buffer, key.seck.dp, key.seck.dplen))
|
|
|
|
p.write(Asn1Tag.Integer, getArray(key.buffer, key.seck.dq, key.seck.dqlen))
|
|
|
|
p.write(Asn1Tag.Integer, getArray(key.buffer, key.seck.iq, key.seck.iqlen))
|
|
|
|
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(RsaKeyIncorrectError)
|
2019-02-24 00:07:13 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc toBytes*(key: RsaPublicKey, data: var openArray[byte]): RsaResult[int] =
|
2019-02-24 00:07:13 +00:00
|
|
|
## Serialize RSA public key ``key`` to ASN.1 DER binary form and store it
|
|
|
|
## to ``data``.
|
|
|
|
##
|
|
|
|
## Procedure returns number of bytes (octets) needed to store RSA public key,
|
|
|
|
## or `0` if public key is incorrect.
|
2020-05-18 05:25:55 +00:00
|
|
|
if isNil(key):
|
|
|
|
err(RsaKeyIncorrectError)
|
|
|
|
elif len(key.buffer) > 0:
|
2019-02-24 00:07:13 +00:00
|
|
|
var b = Asn1Buffer.init()
|
|
|
|
var p = Asn1Composite.init(Asn1Tag.Sequence)
|
|
|
|
var c0 = Asn1Composite.init(Asn1Tag.Sequence)
|
|
|
|
var c1 = Asn1Composite.init(Asn1Tag.BitString)
|
|
|
|
var c10 = Asn1Composite.init(Asn1Tag.Sequence)
|
|
|
|
c0.write(Asn1Tag.Oid, Asn1OidRsaEncryption)
|
|
|
|
c0.write(Asn1Tag.Null)
|
|
|
|
c0.finish()
|
|
|
|
c10.write(Asn1Tag.Integer, getArray(key.buffer, key.key.n, key.key.nlen))
|
|
|
|
c10.write(Asn1Tag.Integer, getArray(key.buffer, key.key.e, key.key.elen))
|
|
|
|
c10.finish()
|
|
|
|
c1.write(c10)
|
|
|
|
c1.finish()
|
|
|
|
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(blen)
|
|
|
|
else:
|
|
|
|
err(RsaKeyIncorrectError)
|
2019-02-24 00:07:13 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc toBytes*(sig: RsaSignature, data: var openArray[byte]): RsaResult[int] =
|
2019-02-24 00:07:13 +00:00
|
|
|
## Serialize RSA signature ``sig`` to raw binary form and store it
|
|
|
|
## to ``data``.
|
|
|
|
##
|
|
|
|
## Procedure returns number of bytes (octets) needed to store RSA public key,
|
|
|
|
## or `0` if public key is incorrect.
|
2020-05-18 05:25:55 +00:00
|
|
|
if isNil(sig):
|
|
|
|
err(RsaSignatureError)
|
|
|
|
else:
|
|
|
|
var slen = len(sig.buffer)
|
|
|
|
if len(data) >= slen:
|
|
|
|
copyMem(addr data[0], addr sig.buffer[0], slen)
|
|
|
|
ok(slen)
|
2019-02-24 00:07:13 +00:00
|
|
|
|
2020-05-18 05:25:55 +00:00
|
|
|
proc getBytes*(key: RsaPrivateKey): RsaResult[seq[byte]] =
|
2019-02-24 00:07:13 +00:00
|
|
|
## Serialize RSA private key ``key`` to ASN.1 DER binary form and
|
|
|
|
## return it.
|
2020-05-18 05:25:55 +00:00
|
|
|
if isNil(key):
|
|
|
|
return err(RsaKeyIncorrectError)
|
|
|
|
var res = newSeq[byte](4096)
|
|
|
|
let length = ?key.toBytes(res)
|
2019-02-24 00:07:13 +00:00
|
|
|
if length > 0:
|
2020-05-18 05:25:55 +00:00
|
|
|
res.setLen(length)
|
|
|
|
ok(res)
|
2019-02-24 00:07:13 +00:00
|
|
|
else:
|
2020-05-18 05:25:55 +00:00
|
|
|
err(RsaKeyIncorrectError)
|
2019-02-24 00:07:13 +00:00
|
|
|
|
2020-05-18 05:25:55 +00:00
|
|
|
proc getBytes*(key: RsaPublicKey): RsaResult[seq[byte]] =
|
2019-02-24 00:07:13 +00:00
|
|
|
## Serialize RSA public key ``key`` to ASN.1 DER binary form and
|
|
|
|
## return it.
|
2020-05-18 05:25:55 +00:00
|
|
|
if isNil(key):
|
|
|
|
return err(RsaKeyIncorrectError)
|
|
|
|
var res = newSeq[byte](4096)
|
|
|
|
let length = ?key.toBytes(res)
|
2019-02-24 00:07:13 +00:00
|
|
|
if length > 0:
|
2020-05-18 05:25:55 +00:00
|
|
|
res.setLen(length)
|
|
|
|
ok(res)
|
2019-02-24 00:07:13 +00:00
|
|
|
else:
|
2020-05-18 05:25:55 +00:00
|
|
|
err(RsaKeyIncorrectError)
|
2019-02-24 00:07:13 +00:00
|
|
|
|
2020-05-18 05:25:55 +00:00
|
|
|
proc getBytes*(sig: RsaSignature): RsaResult[seq[byte]] =
|
2019-02-24 00:07:13 +00:00
|
|
|
## Serialize RSA signature ``sig`` to raw binary form and return it.
|
2020-05-18 05:25:55 +00:00
|
|
|
if isNil(sig):
|
|
|
|
return err(RsaSignatureError)
|
|
|
|
var res = newSeq[byte](4096)
|
|
|
|
let length = ?sig.toBytes(res)
|
2019-02-24 00:07:13 +00:00
|
|
|
if length > 0:
|
2020-05-18 05:25:55 +00:00
|
|
|
res.setLen(length)
|
|
|
|
ok(res)
|
2019-02-24 00:07:13 +00:00
|
|
|
else:
|
2020-05-18 05:25:55 +00:00
|
|
|
err(RsaSignatureError)
|
2019-02-24 00:07:13 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc init*(key: var RsaPrivateKey, data: openArray[byte]): Result[void, Asn1Error] =
|
2019-02-24 00:07:13 +00:00
|
|
|
## Initialize RSA private key ``key`` from ASN.1 DER binary representation
|
|
|
|
## ``data``.
|
|
|
|
##
|
|
|
|
## Procedure returns ``Asn1Status``.
|
|
|
|
var field, rawn, rawpube, rawprie, rawp, rawq, rawdp, rawdq, rawiq: Asn1Field
|
|
|
|
|
2020-04-23 12:10:20 +00:00
|
|
|
# Asn1Field is not trivial so avoid too much Result
|
|
|
|
|
2019-02-24 00:07:13 +00:00
|
|
|
var ab = Asn1Buffer.init(data)
|
2020-04-23 12:10:20 +00:00
|
|
|
field = ?ab.read()
|
|
|
|
|
2019-02-24 00:07:13 +00:00
|
|
|
if field.kind != Asn1Tag.Sequence:
|
2020-04-23 12:10:20 +00:00
|
|
|
return err(Asn1Error.Incorrect)
|
2019-02-24 00:07:13 +00:00
|
|
|
|
|
|
|
var ib = field.getBuffer()
|
|
|
|
|
2020-04-23 12:10:20 +00:00
|
|
|
field = ?ib.read()
|
|
|
|
|
2019-02-24 00:07:13 +00:00
|
|
|
if field.kind != Asn1Tag.Integer:
|
2020-04-23 12:10:20 +00:00
|
|
|
return err(Asn1Error.Incorrect)
|
2019-02-24 00:07:13 +00:00
|
|
|
|
|
|
|
if field.vint != 0'u64:
|
2020-04-23 12:10:20 +00:00
|
|
|
return err(Asn1Error.Incorrect)
|
|
|
|
|
|
|
|
rawn = ?ib.read()
|
2019-02-24 00:07:13 +00:00
|
|
|
|
|
|
|
if rawn.kind != Asn1Tag.Integer:
|
2020-04-23 12:10:20 +00:00
|
|
|
return err(Asn1Error.Incorrect)
|
|
|
|
|
|
|
|
rawpube = ?ib.read()
|
2019-02-24 00:07:13 +00:00
|
|
|
|
|
|
|
if rawpube.kind != Asn1Tag.Integer:
|
2020-04-23 12:10:20 +00:00
|
|
|
return err(Asn1Error.Incorrect)
|
|
|
|
|
|
|
|
rawprie = ?ib.read()
|
2019-02-24 00:07:13 +00:00
|
|
|
|
|
|
|
if rawprie.kind != Asn1Tag.Integer:
|
2020-04-23 12:10:20 +00:00
|
|
|
return err(Asn1Error.Incorrect)
|
|
|
|
|
|
|
|
rawp = ?ib.read()
|
2019-02-24 00:07:13 +00:00
|
|
|
|
|
|
|
if rawp.kind != Asn1Tag.Integer:
|
2020-04-23 12:10:20 +00:00
|
|
|
return err(Asn1Error.Incorrect)
|
|
|
|
|
|
|
|
rawq = ?ib.read()
|
2019-02-24 00:07:13 +00:00
|
|
|
|
|
|
|
if rawq.kind != Asn1Tag.Integer:
|
2020-04-23 12:10:20 +00:00
|
|
|
return err(Asn1Error.Incorrect)
|
|
|
|
|
|
|
|
rawdp = ?ib.read()
|
2019-02-24 00:07:13 +00:00
|
|
|
|
|
|
|
if rawdp.kind != Asn1Tag.Integer:
|
2020-04-23 12:10:20 +00:00
|
|
|
return err(Asn1Error.Incorrect)
|
|
|
|
|
|
|
|
rawdq = ?ib.read()
|
2019-02-24 00:07:13 +00:00
|
|
|
|
|
|
|
if rawdq.kind != Asn1Tag.Integer:
|
2020-04-23 12:10:20 +00:00
|
|
|
return err(Asn1Error.Incorrect)
|
|
|
|
|
|
|
|
rawiq = ?ib.read()
|
2019-02-24 00:07:13 +00:00
|
|
|
|
|
|
|
if rawiq.kind != Asn1Tag.Integer:
|
2020-04-23 12:10:20 +00:00
|
|
|
return err(Asn1Error.Incorrect)
|
2019-02-24 00:07:13 +00:00
|
|
|
|
|
|
|
if len(rawn) >= (MinKeySize shr 3) and len(rawp) > 0 and len(rawq) > 0 and
|
|
|
|
len(rawdp) > 0 and len(rawdq) > 0 and len(rawiq) > 0:
|
|
|
|
key = new RsaPrivateKey
|
|
|
|
key.buffer = @data
|
2022-06-16 08:08:52 +00:00
|
|
|
key.pubk.n = addr key.buffer[rawn.offset]
|
|
|
|
key.pubk.e = addr key.buffer[rawpube.offset]
|
|
|
|
key.seck.p = addr key.buffer[rawp.offset]
|
|
|
|
key.seck.q = addr key.buffer[rawq.offset]
|
|
|
|
key.seck.dp = addr key.buffer[rawdp.offset]
|
|
|
|
key.seck.dq = addr key.buffer[rawdq.offset]
|
|
|
|
key.seck.iq = addr key.buffer[rawiq.offset]
|
|
|
|
key.pexp = addr key.buffer[rawprie.offset]
|
|
|
|
key.pubk.nlen = uint(len(rawn))
|
|
|
|
key.pubk.elen = uint(len(rawpube))
|
|
|
|
key.seck.plen = uint(len(rawp))
|
|
|
|
key.seck.qlen = uint(len(rawq))
|
|
|
|
key.seck.dplen = uint(len(rawdp))
|
|
|
|
key.seck.dqlen = uint(len(rawdq))
|
|
|
|
key.seck.iqlen = uint(len(rawiq))
|
|
|
|
key.pexplen = uint(len(rawprie))
|
2019-02-24 00:07:13 +00:00
|
|
|
key.seck.nBitlen = cast[uint32](len(rawn) shl 3)
|
2020-04-23 12:10:20 +00:00
|
|
|
ok()
|
2019-02-24 00:07:13 +00:00
|
|
|
else:
|
2020-04-23 12:10:20 +00:00
|
|
|
err(Asn1Error.Incorrect)
|
2019-02-24 00:07:13 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc init*(key: var RsaPublicKey, data: openArray[byte]): Result[void, Asn1Error] =
|
2019-02-24 00:07:13 +00:00
|
|
|
## Initialize RSA public key ``key`` from ASN.1 DER binary representation
|
|
|
|
## ``data``.
|
|
|
|
##
|
|
|
|
## Procedure returns ``Asn1Status``.
|
2019-10-29 18:51:48 +00:00
|
|
|
var field, rawn, rawe: Asn1Field
|
2019-02-24 00:07:13 +00:00
|
|
|
var ab = Asn1Buffer.init(data)
|
|
|
|
|
2020-04-23 12:10:20 +00:00
|
|
|
field = ?ab.read()
|
|
|
|
|
2019-02-24 00:07:13 +00:00
|
|
|
if field.kind != Asn1Tag.Sequence:
|
2020-04-23 12:10:20 +00:00
|
|
|
return err(Asn1Error.Incorrect)
|
|
|
|
|
2019-02-24 00:07:13 +00:00
|
|
|
var ib = field.getBuffer()
|
|
|
|
|
2020-04-23 12:10:20 +00:00
|
|
|
field = ?ib.read()
|
|
|
|
|
2019-02-24 00:07:13 +00:00
|
|
|
if field.kind != Asn1Tag.Sequence:
|
2020-04-23 12:10:20 +00:00
|
|
|
return err(Asn1Error.Incorrect)
|
|
|
|
|
2019-02-24 00:07:13 +00:00
|
|
|
var ob = field.getBuffer()
|
|
|
|
|
2020-04-23 12:10:20 +00:00
|
|
|
field = ?ob.read()
|
|
|
|
|
2019-02-24 00:07:13 +00:00
|
|
|
if field.kind != Asn1Tag.Oid:
|
2020-04-23 12:10:20 +00:00
|
|
|
return err(Asn1Error.Incorrect)
|
|
|
|
elif field != Asn1OidRsaEncryption:
|
|
|
|
return err(Asn1Error.Incorrect)
|
|
|
|
|
|
|
|
field = ?ob.read()
|
2019-02-24 00:07:13 +00:00
|
|
|
|
|
|
|
if field.kind != Asn1Tag.Null:
|
2020-04-23 12:10:20 +00:00
|
|
|
return err(Asn1Error.Incorrect)
|
|
|
|
|
|
|
|
field = ?ib.read()
|
2019-02-24 00:07:13 +00:00
|
|
|
|
|
|
|
if field.kind != Asn1Tag.BitString:
|
2020-04-23 12:10:20 +00:00
|
|
|
return err(Asn1Error.Incorrect)
|
|
|
|
|
2019-02-24 00:07:13 +00:00
|
|
|
var vb = field.getBuffer()
|
|
|
|
|
2020-04-23 12:10:20 +00:00
|
|
|
field = ?vb.read()
|
|
|
|
|
2019-02-24 00:07:13 +00:00
|
|
|
if field.kind != Asn1Tag.Sequence:
|
2020-04-23 12:10:20 +00:00
|
|
|
return err(Asn1Error.Incorrect)
|
|
|
|
|
2019-02-24 00:07:13 +00:00
|
|
|
var sb = field.getBuffer()
|
|
|
|
|
2020-04-23 12:10:20 +00:00
|
|
|
rawn = ?sb.read()
|
2019-02-24 00:07:13 +00:00
|
|
|
|
|
|
|
if rawn.kind != Asn1Tag.Integer:
|
2020-04-23 12:10:20 +00:00
|
|
|
return err(Asn1Error.Incorrect)
|
|
|
|
|
|
|
|
rawe = ?sb.read()
|
|
|
|
|
|
|
|
if rawe.kind != Asn1Tag.Integer:
|
|
|
|
return err(Asn1Error.Incorrect)
|
2019-02-24 00:07:13 +00:00
|
|
|
|
|
|
|
if len(rawn) >= (MinKeySize shr 3) and len(rawe) > 0:
|
|
|
|
key = new RsaPublicKey
|
|
|
|
key.buffer = @data
|
2022-06-16 08:08:52 +00:00
|
|
|
key.key.n = addr key.buffer[rawn.offset]
|
|
|
|
key.key.e = addr key.buffer[rawe.offset]
|
|
|
|
key.key.nlen = uint(len(rawn))
|
|
|
|
key.key.elen = uint(len(rawe))
|
2020-04-23 12:10:20 +00:00
|
|
|
ok()
|
2019-02-24 00:07:13 +00:00
|
|
|
else:
|
2020-04-23 12:10:20 +00:00
|
|
|
err(Asn1Error.Incorrect)
|
2019-02-24 00:07:13 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc init*(sig: var RsaSignature, data: openArray[byte]): Result[void, Asn1Error] =
|
2019-02-24 00:07:13 +00:00
|
|
|
## Initialize RSA signature ``sig`` from ASN.1 DER binary representation
|
|
|
|
## ``data``.
|
|
|
|
##
|
2020-04-23 12:10:20 +00:00
|
|
|
## Procedure returns ``Result[void, Asn1Status]``.
|
2019-02-19 22:11:59 +00:00
|
|
|
if len(data) > 0:
|
2019-02-24 00:07:13 +00:00
|
|
|
sig = new RsaSignature
|
|
|
|
sig.buffer = @data
|
2020-04-23 12:10:20 +00:00
|
|
|
ok()
|
|
|
|
else:
|
|
|
|
err(Asn1Error.Incorrect)
|
2019-02-24 00:07:13 +00:00
|
|
|
|
2020-08-11 14:48:21 +00:00
|
|
|
proc init*[T: RsaPKI](sospk: var T, data: string): Result[void, Asn1Error] {.inline.} =
|
2019-02-24 00:07:13 +00:00
|
|
|
## Initialize EC `private key`, `public key` or `scalar` ``sospk`` from
|
|
|
|
## hexadecimal string representation ``data``.
|
|
|
|
##
|
2020-04-23 12:10:20 +00:00
|
|
|
## Procedure returns ``Result[void, Asn1Status]``.
|
2020-08-11 14:48:21 +00:00
|
|
|
sospk.init(ncrutils.fromHex(data))
|
2019-02-24 00:07:13 +00:00
|
|
|
|
2020-08-11 14:48:21 +00:00
|
|
|
proc init*(
|
2021-12-16 10:05:20 +00:00
|
|
|
t: typedesc[RsaPrivateKey], data: openArray[byte]
|
|
|
|
): RsaResult[RsaPrivateKey] =
|
2019-02-24 00:07:13 +00:00
|
|
|
## Initialize RSA private key from ASN.1 DER binary representation ``data``
|
|
|
|
## and return constructed object.
|
2020-05-18 05:25:55 +00:00
|
|
|
var res: RsaPrivateKey
|
|
|
|
if res.init(data).isErr:
|
|
|
|
err(RsaKeyIncorrectError)
|
|
|
|
else:
|
|
|
|
ok(res)
|
2019-02-24 00:07:13 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc init*(t: typedesc[RsaPublicKey], data: openArray[byte]): RsaResult[RsaPublicKey] =
|
2019-02-24 00:07:13 +00:00
|
|
|
## Initialize RSA public key from ASN.1 DER binary representation ``data``
|
|
|
|
## and return constructed object.
|
2020-05-18 05:25:55 +00:00
|
|
|
var res: RsaPublicKey
|
|
|
|
if res.init(data).isErr:
|
|
|
|
err(RsaKeyIncorrectError)
|
|
|
|
else:
|
|
|
|
ok(res)
|
2019-02-24 00:07:13 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc init*(t: typedesc[RsaSignature], data: openArray[byte]): RsaResult[RsaSignature] =
|
2019-02-24 00:07:13 +00:00
|
|
|
## Initialize RSA signature from raw binary representation ``data`` and
|
|
|
|
## return constructed object.
|
2020-05-18 05:25:55 +00:00
|
|
|
var res: RsaSignature
|
|
|
|
if res.init(data).isErr:
|
|
|
|
err(RsaSignatureError)
|
|
|
|
else:
|
|
|
|
ok(res)
|
2019-02-24 00:07:13 +00:00
|
|
|
|
|
|
|
proc init*[T: RsaPKI](t: typedesc[T], data: string): T {.inline.} =
|
|
|
|
## Initialize RSA `private key`, `public key` or `signature` from hexadecimal
|
|
|
|
## string representation ``data`` and return constructed object.
|
2020-08-11 14:48:21 +00:00
|
|
|
result = t.init(ncrutils.fromHex(data))
|
2019-02-24 00:07:13 +00:00
|
|
|
|
|
|
|
proc `$`*(key: RsaPrivateKey): string =
|
|
|
|
## Return string representation of RSA private key.
|
2019-09-11 16:03:39 +00:00
|
|
|
if isNil(key) or len(key.buffer) == 0:
|
|
|
|
result = "Empty or uninitialized RSA key"
|
2019-02-24 00:07:13 +00:00
|
|
|
else:
|
|
|
|
result = "RSA key ("
|
|
|
|
result.add($key.seck.nBitlen)
|
|
|
|
result.add(" bits)\n")
|
|
|
|
result.add("p = ")
|
2020-08-11 14:48:21 +00:00
|
|
|
result.add(ncrutils.toHex(getArray(key.buffer, key.seck.p, key.seck.plen)))
|
2019-02-24 00:07:13 +00:00
|
|
|
result.add("\nq = ")
|
2020-08-11 14:48:21 +00:00
|
|
|
result.add(ncrutils.toHex(getArray(key.buffer, key.seck.q, key.seck.qlen)))
|
2019-02-24 00:07:13 +00:00
|
|
|
result.add("\ndp = ")
|
2020-08-11 14:48:21 +00:00
|
|
|
result.add(ncrutils.toHex(getArray(key.buffer, key.seck.dp, key.seck.dplen)))
|
2019-02-24 00:07:13 +00:00
|
|
|
result.add("\ndq = ")
|
2020-08-11 14:48:21 +00:00
|
|
|
result.add(ncrutils.toHex(getArray(key.buffer, key.seck.dq, key.seck.dqlen)))
|
2019-02-24 00:07:13 +00:00
|
|
|
result.add("\niq = ")
|
2020-08-11 14:48:21 +00:00
|
|
|
result.add(ncrutils.toHex(getArray(key.buffer, key.seck.iq, key.seck.iqlen)))
|
2019-02-24 00:07:13 +00:00
|
|
|
result.add("\npre = ")
|
2020-08-11 14:48:21 +00:00
|
|
|
result.add(ncrutils.toHex(getArray(key.buffer, key.pexp, key.pexplen)))
|
2019-02-24 00:07:13 +00:00
|
|
|
result.add("\nm = ")
|
2020-08-11 14:48:21 +00:00
|
|
|
result.add(ncrutils.toHex(getArray(key.buffer, key.pubk.n, key.pubk.nlen)))
|
2019-02-24 00:07:13 +00:00
|
|
|
result.add("\npue = ")
|
2020-08-11 14:48:21 +00:00
|
|
|
result.add(ncrutils.toHex(getArray(key.buffer, key.pubk.e, key.pubk.elen)))
|
2019-02-24 00:07:13 +00:00
|
|
|
result.add("\n")
|
|
|
|
|
|
|
|
proc `$`*(key: RsaPublicKey): string =
|
|
|
|
## Return string representation of RSA public key.
|
2019-09-11 16:03:39 +00:00
|
|
|
if isNil(key) or len(key.buffer) == 0:
|
|
|
|
result = "Empty or uninitialized RSA key"
|
2019-02-24 00:07:13 +00:00
|
|
|
else:
|
|
|
|
let nbitlen = key.key.nlen shl 3
|
|
|
|
result = "RSA key ("
|
|
|
|
result.add($nbitlen)
|
|
|
|
result.add(" bits)\nn = ")
|
2020-08-11 14:48:21 +00:00
|
|
|
result.add(ncrutils.toHex(getArray(key.buffer, key.key.n, key.key.nlen)))
|
2019-02-24 00:07:13 +00:00
|
|
|
result.add("\ne = ")
|
2020-08-11 14:48:21 +00:00
|
|
|
result.add(ncrutils.toHex(getArray(key.buffer, key.key.e, key.key.elen)))
|
2019-02-24 00:07:13 +00:00
|
|
|
result.add("\n")
|
|
|
|
|
|
|
|
proc `$`*(sig: RsaSignature): string =
|
|
|
|
## Return string representation of RSA signature.
|
2019-09-11 16:03:39 +00:00
|
|
|
if isNil(sig) or len(sig.buffer) == 0:
|
|
|
|
result = "Empty or uninitialized RSA signature"
|
2019-02-24 00:07:13 +00:00
|
|
|
else:
|
|
|
|
result = "RSA signature ("
|
2020-08-11 14:48:21 +00:00
|
|
|
result.add(ncrutils.toHex(sig.buffer))
|
2019-02-24 00:07:13 +00:00
|
|
|
result.add(")")
|
|
|
|
|
2019-02-19 22:11:59 +00:00
|
|
|
proc `==`*(a, b: RsaPrivateKey): bool =
|
|
|
|
## Compare two RSA private keys for equality.
|
2019-09-11 16:03:39 +00:00
|
|
|
##
|
|
|
|
## Result is true if ``a`` and ``b`` are both ``nil`` or ``a`` and ``b`` are
|
|
|
|
## equal by value.
|
|
|
|
if isNil(a) and isNil(b):
|
2020-08-08 06:53:33 +00:00
|
|
|
true
|
2019-09-11 16:03:39 +00:00
|
|
|
elif isNil(a) and (not isNil(b)):
|
2020-08-08 06:53:33 +00:00
|
|
|
false
|
2019-09-11 16:03:39 +00:00
|
|
|
elif isNil(b) and (not isNil(a)):
|
2020-08-08 06:53:33 +00:00
|
|
|
false
|
2019-09-11 16:03:39 +00:00
|
|
|
else:
|
|
|
|
if a.seck.nBitlen == b.seck.nBitlen:
|
2023-05-18 08:24:17 +00:00
|
|
|
if a.seck.nBitlen > 0'u:
|
2020-08-08 06:53:33 +00:00
|
|
|
let r1 = CT.isEqual(
|
|
|
|
getArray(a.buffer, a.seck.p, a.seck.plen),
|
|
|
|
getArray(b.buffer, b.seck.p, b.seck.plen),
|
|
|
|
)
|
|
|
|
let r2 = CT.isEqual(
|
|
|
|
getArray(a.buffer, a.seck.q, a.seck.qlen),
|
|
|
|
getArray(b.buffer, b.seck.q, b.seck.qlen),
|
|
|
|
)
|
|
|
|
let r3 = CT.isEqual(
|
|
|
|
getArray(a.buffer, a.seck.dp, a.seck.dplen),
|
|
|
|
getArray(b.buffer, b.seck.dp, b.seck.dplen),
|
|
|
|
)
|
|
|
|
let r4 = CT.isEqual(
|
|
|
|
getArray(a.buffer, a.seck.dq, a.seck.dqlen),
|
|
|
|
getArray(b.buffer, b.seck.dq, b.seck.dqlen),
|
|
|
|
)
|
|
|
|
let r5 = CT.isEqual(
|
|
|
|
getArray(a.buffer, a.seck.iq, a.seck.iqlen),
|
|
|
|
getArray(b.buffer, b.seck.iq, b.seck.iqlen),
|
|
|
|
)
|
|
|
|
let r6 = CT.isEqual(
|
|
|
|
getArray(a.buffer, a.pexp, a.pexplen), getArray(b.buffer, b.pexp, b.pexplen)
|
|
|
|
)
|
|
|
|
let r7 = CT.isEqual(
|
|
|
|
getArray(a.buffer, a.pubk.n, a.pubk.nlen),
|
|
|
|
getArray(b.buffer, b.pubk.n, b.pubk.nlen),
|
|
|
|
)
|
|
|
|
let r8 = CT.isEqual(
|
|
|
|
getArray(a.buffer, a.pubk.e, a.pubk.elen),
|
|
|
|
getArray(b.buffer, b.pubk.e, b.pubk.elen),
|
|
|
|
)
|
|
|
|
r1 and r2 and r3 and r4 and r5 and r6 and r7 and r8
|
2019-09-11 16:03:39 +00:00
|
|
|
else:
|
2020-08-08 06:53:33 +00:00
|
|
|
true
|
|
|
|
else:
|
|
|
|
false
|
2019-02-19 22:11:59 +00:00
|
|
|
|
2019-02-24 00:07:13 +00:00
|
|
|
proc `==`*(a, b: RsaSignature): bool =
|
|
|
|
## Compare two RSA signatures for equality.
|
2019-09-11 16:03:39 +00:00
|
|
|
if isNil(a) and isNil(b):
|
2020-08-08 06:53:33 +00:00
|
|
|
true
|
2019-09-11 16:03:39 +00:00
|
|
|
elif isNil(a) and (not isNil(b)):
|
2020-08-08 06:53:33 +00:00
|
|
|
false
|
2019-09-11 16:03:39 +00:00
|
|
|
elif isNil(b) and (not isNil(a)):
|
2020-08-08 06:53:33 +00:00
|
|
|
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-24 00:07:13 +00:00
|
|
|
|
|
|
|
proc `==`*(a, b: RsaPublicKey): bool =
|
|
|
|
## Compare two RSA public keys for equality.
|
2019-09-11 16:03:39 +00:00
|
|
|
if isNil(a) and isNil(b):
|
2020-08-08 06:53:33 +00:00
|
|
|
true
|
2019-09-11 16:03:39 +00:00
|
|
|
elif isNil(a) and (not isNil(b)):
|
2020-08-08 06:53:33 +00:00
|
|
|
false
|
2019-09-11 16:03:39 +00:00
|
|
|
elif isNil(b) and (not isNil(a)):
|
2020-08-08 06:53:33 +00:00
|
|
|
false
|
2019-09-11 16:03:39 +00:00
|
|
|
else:
|
2020-08-08 06:53:33 +00:00
|
|
|
let r1 = CT.isEqual(
|
|
|
|
getArray(a.buffer, a.key.n, a.key.nlen), getArray(b.buffer, b.key.n, b.key.nlen)
|
|
|
|
)
|
|
|
|
let r2 = CT.isEqual(
|
|
|
|
getArray(a.buffer, a.key.e, a.key.elen), getArray(b.buffer, b.key.e, b.key.elen)
|
|
|
|
)
|
|
|
|
(r1 and r2)
|
2019-02-24 00:07:13 +00:00
|
|
|
|
|
|
|
proc sign*[T: byte | char](
|
2021-12-16 10:05:20 +00:00
|
|
|
key: RsaPrivateKey, message: openArray[T]
|
|
|
|
): RsaResult[RsaSignature] {.gcsafe.} =
|
2019-02-24 00:07:13 +00:00
|
|
|
## Get RSA PKCS1.5 signature of data ``message`` using SHA256 and private
|
|
|
|
## key ``key``.
|
2020-05-18 05:25:55 +00:00
|
|
|
if isNil(key):
|
|
|
|
return err(RsaKeyIncorrectError)
|
|
|
|
|
2022-06-16 08:08:52 +00:00
|
|
|
var hc: HashCompatContext
|
2019-02-24 00:07:13 +00:00
|
|
|
var hash: array[32, byte]
|
2022-06-16 08:08:52 +00:00
|
|
|
let impl = rsaPkcs1SignGetDefault()
|
2020-05-18 05:25:55 +00:00
|
|
|
var res = new RsaSignature
|
|
|
|
res.buffer = newSeq[byte]((key.seck.nBitlen + 7) shr 3)
|
2019-02-24 00:07:13 +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-24 00:07:13 +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])
|
2019-02-24 00:07:13 +00:00
|
|
|
var oid = RsaOidSha256
|
2022-06-16 08:08:52 +00:00
|
|
|
let implRes =
|
|
|
|
impl(addr oid[0], addr hash[0], uint(len(hash)), addr key.seck, addr res.buffer[0])
|
2020-05-18 05:25:55 +00:00
|
|
|
if implRes == 0:
|
|
|
|
err(RsaSignatureError)
|
|
|
|
else:
|
|
|
|
ok(res)
|
2019-02-19 22:11:59 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
proc verify*[T: byte | char](
|
|
|
|
sig: RsaSignature, message: openArray[T], pubkey: RsaPublicKey
|
2019-02-24 00:07:13 +00:00
|
|
|
): bool {.inline.} =
|
|
|
|
## Verify RSA 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)))
|
2019-02-24 00:07:13 +00:00
|
|
|
if len(sig.buffer) > 0:
|
2022-06-16 08:08:52 +00:00
|
|
|
var hc: HashCompatContext
|
2019-02-24 00:07:13 +00:00
|
|
|
var hash: array[32, byte]
|
|
|
|
var check: array[32, byte]
|
2022-06-16 08:08:52 +00:00
|
|
|
var impl = rsaPkcs1VrfyGetDefault()
|
2019-02-24 00:07:13 +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-24 00:07:13 +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])
|
2019-02-24 00:07:13 +00:00
|
|
|
var oid = RsaOidSha256
|
2022-06-16 08:08:52 +00:00
|
|
|
let res = impl(
|
|
|
|
addr sig.buffer[0],
|
|
|
|
uint(len(sig.buffer)),
|
|
|
|
addr oid[0],
|
|
|
|
uint(len(check)),
|
|
|
|
addr pubkey.key,
|
|
|
|
addr check[0],
|
|
|
|
)
|
2019-02-24 00:07:13 +00:00
|
|
|
if res == 1:
|
|
|
|
result = equalMem(addr check[0], addr hash[0], len(hash))
|