avoid importing `ecnist` when not needed (#942)
This commit is contained in:
parent
c6aa085e98
commit
b76bac752f
|
@ -65,11 +65,13 @@ when supported(PKScheme.Ed25519):
|
||||||
import ed25519/ed25519
|
import ed25519/ed25519
|
||||||
when supported(PKScheme.Secp256k1):
|
when supported(PKScheme.Secp256k1):
|
||||||
import secp
|
import secp
|
||||||
|
when supported(PKScheme.ECDSA):
|
||||||
|
import ecnist
|
||||||
|
|
||||||
# We are still importing `ecnist` because, it is used for SECIO handshake,
|
# These used to be declared in `crypto` itself
|
||||||
# but it will be impossible to create ECNIST keys or import ECNIST keys.
|
export ecnist.ephemeral, ecnist.ECDHEScheme
|
||||||
|
|
||||||
import ecnist, bearssl/rand, bearssl/hash as bhash
|
import bearssl/rand, bearssl/hash as bhash
|
||||||
import ../protobuf/minprotobuf, ../vbuffer, ../multihash, ../multicodec
|
import ../protobuf/minprotobuf, ../vbuffer, ../multihash, ../multicodec
|
||||||
import nimcrypto/[rijndael, twofish, sha2, hash, hmac]
|
import nimcrypto/[rijndael, twofish, sha2, hash, hmac]
|
||||||
# We use `ncrutils` for constant-time hexadecimal encoding/decoding procedures.
|
# We use `ncrutils` for constant-time hexadecimal encoding/decoding procedures.
|
||||||
|
@ -86,8 +88,6 @@ type
|
||||||
Sha256,
|
Sha256,
|
||||||
Sha512
|
Sha512
|
||||||
|
|
||||||
ECDHEScheme* = EcCurveKind
|
|
||||||
|
|
||||||
PublicKey* = object
|
PublicKey* = object
|
||||||
case scheme*: PKScheme
|
case scheme*: PKScheme
|
||||||
of PKScheme.RSA:
|
of PKScheme.RSA:
|
||||||
|
@ -879,34 +879,6 @@ proc mac*(secret: Secret, id: int): seq[byte] {.inline.} =
|
||||||
offset += secret.ivsize + secret.keysize
|
offset += secret.ivsize + secret.keysize
|
||||||
copyMem(addr result[0], unsafeAddr secret.data[offset], secret.macsize)
|
copyMem(addr result[0], unsafeAddr secret.data[offset], secret.macsize)
|
||||||
|
|
||||||
proc ephemeral*(
|
|
||||||
scheme: ECDHEScheme,
|
|
||||||
rng: var HmacDrbgContext): CryptoResult[EcKeyPair] =
|
|
||||||
## Generate ephemeral keys used to perform ECDHE.
|
|
||||||
var keypair: EcKeyPair
|
|
||||||
if scheme == Secp256r1:
|
|
||||||
keypair = ? EcKeyPair.random(Secp256r1, rng).orError(KeyError)
|
|
||||||
elif scheme == Secp384r1:
|
|
||||||
keypair = ? EcKeyPair.random(Secp384r1, rng).orError(KeyError)
|
|
||||||
elif scheme == Secp521r1:
|
|
||||||
keypair = ? EcKeyPair.random(Secp521r1, rng).orError(KeyError)
|
|
||||||
ok(keypair)
|
|
||||||
|
|
||||||
proc ephemeral*(
|
|
||||||
scheme: string, rng: var HmacDrbgContext): CryptoResult[EcKeyPair] =
|
|
||||||
## 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)
|
|
||||||
|
|
||||||
proc getOrder*(remotePubkey, localNonce: openArray[byte],
|
proc getOrder*(remotePubkey, localNonce: openArray[byte],
|
||||||
localPubkey, remoteNonce: openArray[byte]): CryptoResult[int] =
|
localPubkey, remoteNonce: openArray[byte]): CryptoResult[int] =
|
||||||
## Compare values and calculate `order` parameter.
|
## Compare values and calculate `order` parameter.
|
||||||
|
|
|
@ -994,3 +994,33 @@ proc verify*[T: byte|char](sig: EcSignature, message: openArray[T],
|
||||||
# Clear context with initial value
|
# Clear context with initial value
|
||||||
kv.init(addr hc.vtable)
|
kv.init(addr hc.vtable)
|
||||||
result = (res == 1)
|
result = (res == 1)
|
||||||
|
|
||||||
|
type ECDHEScheme* = EcCurveKind
|
||||||
|
|
||||||
|
proc ephemeral*(
|
||||||
|
scheme: ECDHEScheme,
|
||||||
|
rng: var HmacDrbgContext): EcResult[EcKeyPair] =
|
||||||
|
## Generate ephemeral keys used to perform ECDHE.
|
||||||
|
var keypair: EcKeyPair
|
||||||
|
if scheme == Secp256r1:
|
||||||
|
keypair = ? EcKeyPair.random(Secp256r1, rng)
|
||||||
|
elif scheme == Secp384r1:
|
||||||
|
keypair = ? EcKeyPair.random(Secp384r1, rng)
|
||||||
|
elif scheme == Secp521r1:
|
||||||
|
keypair = ? EcKeyPair.random(Secp521r1, rng)
|
||||||
|
ok(keypair)
|
||||||
|
|
||||||
|
proc ephemeral*(
|
||||||
|
scheme: string, rng: var HmacDrbgContext): EcResult[EcKeyPair] =
|
||||||
|
## 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)
|
||||||
|
|
Loading…
Reference in New Issue