Use results and no exceptions in chacha and curve25519 (#182)
This commit is contained in:
parent
f8029e7359
commit
c219100e64
|
@ -15,6 +15,8 @@
|
||||||
|
|
||||||
# RFC @ https://tools.ietf.org/html/rfc7539
|
# RFC @ https://tools.ietf.org/html/rfc7539
|
||||||
|
|
||||||
|
{.push raises: [Defect].}
|
||||||
|
|
||||||
import bearssl
|
import bearssl
|
||||||
|
|
||||||
# have to do this due to a nim bug and raises[] on callbacks
|
# have to do this due to a nim bug and raises[] on callbacks
|
||||||
|
|
|
@ -15,7 +15,11 @@
|
||||||
|
|
||||||
# RFC @ https://tools.ietf.org/html/rfc7748
|
# RFC @ https://tools.ietf.org/html/rfc7748
|
||||||
|
|
||||||
|
{.push raises: [Defect].}
|
||||||
|
|
||||||
import bearssl
|
import bearssl
|
||||||
|
import stew/results
|
||||||
|
export results
|
||||||
|
|
||||||
const
|
const
|
||||||
Curve25519KeySize* = 32
|
Curve25519KeySize* = 32
|
||||||
|
@ -24,7 +28,8 @@ type
|
||||||
Curve25519* = object
|
Curve25519* = object
|
||||||
Curve25519Key* = array[Curve25519KeySize, byte]
|
Curve25519Key* = array[Curve25519KeySize, byte]
|
||||||
pcuchar = ptr cuchar
|
pcuchar = ptr cuchar
|
||||||
Curver25519RngError* = object of CatchableError
|
Curve25519Error* = enum
|
||||||
|
Curver25519RngError
|
||||||
|
|
||||||
proc intoCurve25519Key*(s: openarray[byte]): Curve25519Key =
|
proc intoCurve25519Key*(s: openarray[byte]): Curve25519Key =
|
||||||
assert s.len == Curve25519KeySize
|
assert s.len == Curve25519KeySize
|
||||||
|
@ -100,12 +105,16 @@ proc mulgen*(_: type[Curve25519], dst: var Curve25519Key, point: Curve25519Key)
|
||||||
proc public*(private: Curve25519Key): Curve25519Key =
|
proc public*(private: Curve25519Key): Curve25519Key =
|
||||||
Curve25519.mulgen(result, private)
|
Curve25519.mulgen(result, private)
|
||||||
|
|
||||||
proc random*(_: type[Curve25519Key]): Curve25519Key =
|
proc random*(_: type[Curve25519Key]): Result[Curve25519Key, Curve25519Error] =
|
||||||
var rng: BrHmacDrbgContext
|
var rng: BrHmacDrbgContext
|
||||||
|
var res: Curve25519Key
|
||||||
let seeder = brPrngSeederSystem(nil)
|
let seeder = brPrngSeederSystem(nil)
|
||||||
brHmacDrbgInit(addr rng, addr sha256Vtable, nil, 0)
|
brHmacDrbgInit(addr rng, addr sha256Vtable, nil, 0)
|
||||||
if seeder(addr rng.vtable) == 0:
|
if seeder(addr rng.vtable) == 0:
|
||||||
raise newException(ValueError, "Could not seed RNG")
|
err(Curver25519RngError)
|
||||||
let defaultBrEc = brEcGetDefault()
|
else:
|
||||||
if brEcKeygen(addr rng.vtable, defaultBrEc, nil, addr result[0], EC_curve25519) != Curve25519KeySize:
|
let defaultBrEc = brEcGetDefault()
|
||||||
raise newException(Curver25519RngError, "Could not generate random data")
|
if brEcKeygen(addr rng.vtable, defaultBrEc, nil, addr res[0], EC_curve25519) != Curve25519KeySize:
|
||||||
|
err(Curver25519RngError)
|
||||||
|
else:
|
||||||
|
ok(res)
|
||||||
|
|
|
@ -88,7 +88,7 @@ type
|
||||||
# Utility
|
# Utility
|
||||||
|
|
||||||
proc genKeyPair(): KeyPair =
|
proc genKeyPair(): KeyPair =
|
||||||
result.privateKey = Curve25519Key.random()
|
result.privateKey = Curve25519Key.random().tryGet()
|
||||||
result.publicKey = result.privateKey.public()
|
result.publicKey = result.privateKey.public()
|
||||||
|
|
||||||
proc hashProtocol(name: string): MDigest[256] =
|
proc hashProtocol(name: string): MDigest[256] =
|
||||||
|
|
Loading…
Reference in New Issue