Bump nim-eth and nim-bearssl and accompanying fixes (#1150)

This commit is contained in:
Kim De Mey 2022-07-04 09:38:02 +02:00 committed by GitHub
parent 0068e3b427
commit fb12793482
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 52 additions and 62 deletions

View File

@ -50,7 +50,7 @@ proc loadBootstrapFile*(bootstrapFile: string,
# However that would require the pull the keystore.nim and parts of # However that would require the pull the keystore.nim and parts of
# keystore_management.nim out of nimbus-eth2. # keystore_management.nim out of nimbus-eth2.
proc getPersistentNetKey*( proc getPersistentNetKey*(
rng: var BrHmacDrbgContext, keyFilePath: string, dataDir: string): rng: var HmacDrbgContext, keyFilePath: string, dataDir: string):
PrivateKey = PrivateKey =
logScope: logScope:
key_file = keyFilePath key_file = keyFilePath

View File

@ -78,7 +78,7 @@ type
contentOffers: seq[ContentOffer] contentOffers: seq[ContentOffer]
connectionTimeout: Duration connectionTimeout: Duration
contentReadTimeout*: Duration contentReadTimeout*: Duration
rng: ref BrHmacDrbgContext rng: ref HmacDrbgContext
udata: pointer udata: pointer
contentHandler: ContentHandlerCallback contentHandler: ContentHandlerCallback
@ -102,7 +102,7 @@ proc addContentOffer*(
# TODO: Should we check if `NodeId` & `connectionId` combo already exists? # TODO: Should we check if `NodeId` & `connectionId` combo already exists?
# What happens if we get duplicates? # What happens if we get duplicates?
var connectionId: Bytes2 var connectionId: Bytes2
brHmacDrbgGenerate(stream.rng[], connectionId) stream.rng[].generate(connectionId)
# uTP protocol uses BE for all values in the header, incl. connection id. # uTP protocol uses BE for all values in the header, incl. connection id.
let id = uint16.fromBytesBE(connectionId) let id = uint16.fromBytesBE(connectionId)
@ -122,7 +122,7 @@ proc addContentRequest*(
# TODO: Should we check if `NodeId` & `connectionId` combo already exists? # TODO: Should we check if `NodeId` & `connectionId` combo already exists?
# What happens if we get duplicates? # What happens if we get duplicates?
var connectionId: Bytes2 var connectionId: Bytes2
brHmacDrbgGenerate(stream.rng[], connectionId) stream.rng[].generate(connectionId)
# uTP protocol uses BE for all values in the header, incl. connection id. # uTP protocol uses BE for all values in the header, incl. connection id.
let id = uint16.fromBytesBE(connectionId) let id = uint16.fromBytesBE(connectionId)

View File

@ -15,12 +15,11 @@ import
../content_db, ../content_db,
./test_helpers ./test_helpers
proc generateNRandomU256(rng: var BrHmacDrbgContext, n: int): seq[UInt256] = proc generateNRandomU256(rng: var HmacDrbgContext, n: int): seq[UInt256] =
var i = 0 var i = 0
var res = newSeq[Uint256]() var res = newSeq[Uint256]()
while i < n: while i < n:
var bytes = newSeq[byte](32) let bytes = rng.generateBytes(32)
brHmacDrbgGenerate(rng, bytes)
let num = Uint256.fromBytesBE(bytes) let num = Uint256.fromBytesBE(bytes)
res.add(num) res.add(num)
inc i inc i

View File

@ -20,7 +20,7 @@ type TestCase = ref object
server: RpcProxy server: RpcProxy
client: RpcHttpClient client: RpcHttpClient
proc setupTest(rng: ref BrHmacDrbgContext): Future[TestCase] {.async.} = proc setupTest(rng: ref HmacDrbgContext): Future[TestCase] {.async.} =
let let
localSrvAddress = "127.0.0.1" localSrvAddress = "127.0.0.1"
localSrvPort = 8545 localSrvPort = 8545

View File

@ -15,7 +15,7 @@ proc localAddress*(port: int): Address =
Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(port)) Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(port))
proc initDiscoveryNode*( proc initDiscoveryNode*(
rng: ref BrHmacDrbgContext, rng: ref HmacDrbgContext,
privKey: PrivateKey, privKey: PrivateKey,
address: Address, address: Address,
bootstrapRecords: openArray[Record] = [], bootstrapRecords: openArray[Record] = [],

View File

@ -29,7 +29,7 @@ proc validateContent(content: openArray[byte], contentKey: ByteList): bool =
true true
proc initPortalProtocol( proc initPortalProtocol(
rng: ref BrHmacDrbgContext, rng: ref HmacDrbgContext,
privKey: PrivateKey, privKey: PrivateKey,
address: Address, address: Address,
bootstrapRecords: openArray[Record] = []): PortalProtocol = bootstrapRecords: openArray[Record] = []): PortalProtocol =
@ -57,7 +57,7 @@ proc stopPortalProtocol(proto: PortalProtocol) {.async.} =
proto.stop() proto.stop()
await proto.baseProtocol.closeWait() await proto.baseProtocol.closeWait()
proc defaultTestSetup(rng: ref BrHmacDrbgContext): proc defaultTestSetup(rng: ref HmacDrbgContext):
(PortalProtocol, PortalProtocol) = (PortalProtocol, PortalProtocol) =
let let
proto1 = proto1 =

View File

@ -268,11 +268,8 @@ proc run(config: PortalCliConf) =
else: else:
echo nodes.error echo nodes.error
of findcontent: of findcontent:
proc random(T: type UInt256, rng: var BrHmacDrbgContext): T = proc random(T: type UInt256, rng: var HmacDrbgContext): T =
var key: UInt256 rng.generate(T)
brHmacDrbgGenerate(addr rng, addr key, csize_t(sizeof(key)))
key
# For now just some random bytes # For now just some random bytes
let contentKey = ByteList.init(@[1'u8]) let contentKey = ByteList.init(@[1'u8])

View File

@ -12,13 +12,8 @@ import
eth/keys, eth/keys,
./utp_test_client ./utp_test_client
proc generateByteSeq(rng: var BrHmacDrbgContext, length: int): seq[byte] = proc generateBytesHex(rng: var HmacDrbgContext, length: int): string =
var bytes = newSeq[byte](length) rng.generateBytes(length).toHex()
brHmacDrbgGenerate(rng, bytes)
return bytes
proc generateByteSeqHex(rng: var BrHmacDrbgContext, length: int): string =
generateByteSeq(rng, length).toHex()
# Before running the suit, there need to be two instances of utp_test_app running # Before running the suit, there need to be two instances of utp_test_app running
# under provided ports (9042, 9041). # under provided ports (9042, 9041).
@ -38,7 +33,7 @@ procSuite "Utp integration tests":
type type
FutureCallback[A] = proc (): Future[A] {.gcsafe, raises: [Defect].} FutureCallback[A] = proc (): Future[A] {.gcsafe, raises: [Defect].}
# combinator which repeatadly calls passed closure until returned future is # combinator which repeatedly calls passed closure until returned future is
# successfull # successfull
# TODO: currently works only for non void types # TODO: currently works only for non void types
proc repeatTillSuccess[A](f: FutureCallback[A], maxTries: int = 20): Future[A] {.async.} = proc repeatTillSuccess[A](f: FutureCallback[A], maxTries: int = 20): Future[A] {.async.} =
@ -100,7 +95,7 @@ procSuite "Utp integration tests":
let serverConnectionKey = maybeServerConnectionKey.unsafeGet() let serverConnectionKey = maybeServerConnectionKey.unsafeGet()
let let
bytesToWrite = generateByteSeqHex(rng[], numOfBytes) bytesToWrite = generateBytesHex(rng[], numOfBytes)
writeRes = await client.utp_write(clientConnectionKey, bytesToWrite) writeRes = await client.utp_write(clientConnectionKey, bytesToWrite)
readData = await server.utp_read(serverConnectionKey, numOfBytes) readData = await server.utp_read(serverConnectionKey, numOfBytes)
@ -125,7 +120,7 @@ procSuite "Utp integration tests":
let serverConnectionKey = maybeServerConnectionKey.unsafeGet() let serverConnectionKey = maybeServerConnectionKey.unsafeGet()
let let
bytesToWrite = generateByteSeqHex(rng[], numOfBytes) bytesToWrite = generateBytesHex(rng[], numOfBytes)
writeRes = await server.utp_write(serverConnectionKey, bytesToWrite) writeRes = await server.utp_write(serverConnectionKey, bytesToWrite)
readData = await client.utp_read(clientConnectionKey, numOfBytes) readData = await client.utp_read(clientConnectionKey, numOfBytes)
@ -147,9 +142,9 @@ procSuite "Utp integration tests":
let serverConnectionKey = maybeServerConnectionKey.unsafeGet() let serverConnectionKey = maybeServerConnectionKey.unsafeGet()
let let
bytesToWrite = generateByteSeqHex(rng[], numOfBytes) bytesToWrite = generateBytesHex(rng[], numOfBytes)
bytesToWrite1 = generateByteSeqHex(rng[], numOfBytes) bytesToWrite1 = generateBytesHex(rng[], numOfBytes)
bytesToWrite2 = generateByteSeqHex(rng[], numOfBytes) bytesToWrite2 = generateBytesHex(rng[], numOfBytes)
writeRes = await client.utp_write(clientConnectionKey, bytesToWrite) writeRes = await client.utp_write(clientConnectionKey, bytesToWrite)
writeRes1 = await client.utp_write(clientConnectionKey, bytesToWrite1) writeRes1 = await client.utp_write(clientConnectionKey, bytesToWrite1)
writeRes2 = await client.utp_write(clientConnectionKey, bytesToWrite2) writeRes2 = await client.utp_write(clientConnectionKey, bytesToWrite2)
@ -186,9 +181,9 @@ procSuite "Utp integration tests":
let serverConnectionKey3 = maybeServerConnectionKey3.unsafeGet() let serverConnectionKey3 = maybeServerConnectionKey3.unsafeGet()
let let
bytesToWrite1 = generateByteSeqHex(rng[], numOfBytes) bytesToWrite1 = generateBytesHex(rng[], numOfBytes)
bytesToWrite2 = generateByteSeqHex(rng[], numOfBytes) bytesToWrite2 = generateBytesHex(rng[], numOfBytes)
bytesToWrite3 = generateByteSeqHex(rng[], numOfBytes) bytesToWrite3 = generateBytesHex(rng[], numOfBytes)
writeRes1 = await client.utp_write(clientConnectionKey1, bytesToWrite1) writeRes1 = await client.utp_write(clientConnectionKey1, bytesToWrite1)
writeRes2 = await client.utp_write(clientConnectionKey2, bytesToWrite2) writeRes2 = await client.utp_write(clientConnectionKey2, bytesToWrite2)

View File

@ -40,7 +40,7 @@ type
# Created accounts are tracked in this map. # Created accounts are tracked in this map.
accounts: Table[EthAddress, PrivateKey] accounts: Table[EthAddress, PrivateKey]
rng: ref BrHmacDrbgContext rng: ref HmacDrbgContext
chainID: ChainID chainID: ChainID
gasPrice: GasInt gasPrice: GasInt
vaultKey: PrivateKey vaultKey: PrivateKey

View File

@ -19,7 +19,7 @@ type
am*: AccountsManager am*: AccountsManager
# You should only create one instance of the RNG per application / library # You should only create one instance of the RNG per application / library
# Ref is used so that it can be shared between components # Ref is used so that it can be shared between components
rng*: ref BrHmacDrbgContext rng*: ref HmacDrbgContext
proc newEthContext*(): EthContext = proc newEthContext*(): EthContext =
result = new(EthContext) result = new(EthContext)

View File

@ -14,7 +14,7 @@
import import
std/[base64, json, options, os, strutils, times], std/[base64, json, options, os, strutils, times],
bearssl, bearssl/rand,
chronicles, chronicles,
chronos, chronos,
chronos/apps/http/httptable, chronos/apps/http/httptable,
@ -54,7 +54,7 @@ type
JwtGenSecret* = ##\ JwtGenSecret* = ##\
## Random generator function producing a shared key. Typically, this\ ## Random generator function producing a shared key. Typically, this\
## will be a wrapper around a random generator type, such as\ ## will be a wrapper around a random generator type, such as\
## `BrHmacDrbgContext`. ## `HmacDrbgContext`.
proc(): JwtSharedKey {.gcsafe.} proc(): JwtSharedKey {.gcsafe.}
JwtExcept* = object of CatchableError JwtExcept* = object of CatchableError
@ -183,7 +183,7 @@ proc fromHex*(key: var JwtSharedKey, src: string): Result[void,JwtError] =
except ValueError: except ValueError:
err(jwtKeyInvalidHexString) err(jwtKeyInvalidHexString)
proc jwtGenSecret*(rng: ref BrHmacDrbgContext): JwtGenSecret = proc jwtGenSecret*(rng: ref HmacDrbgContext): JwtGenSecret =
## Standard shared key random generator. If a fixed key is needed, a ## Standard shared key random generator. If a fixed key is needed, a
## function like ## function like
## :: ## ::
@ -195,7 +195,7 @@ proc jwtGenSecret*(rng: ref BrHmacDrbgContext): JwtGenSecret =
## only. ## only.
result = proc: JwtSharedKey = result = proc: JwtSharedKey =
var data: array[jwtMinSecretLen,byte] var data: array[jwtMinSecretLen,byte]
rng[].brHmacDrbgGenerate(data) rng[].generate(data)
data.JwtSharedKey data.JwtSharedKey
proc jwtSharedSecret*(rndSecret: JwtGenSecret; config: NimbusConf): proc jwtSharedSecret*(rndSecret: JwtGenSecret; config: NimbusConf):
@ -255,7 +255,7 @@ proc jwtSharedSecret*(rndSecret: JwtGenSecret; config: NimbusConf):
except ValueError: except ValueError:
return err(jwtKeyInvalidHexString) return err(jwtKeyInvalidHexString)
proc jwtSharedSecret*(rng: ref BrHmacDrbgContext; config: NimbusConf): proc jwtSharedSecret*(rng: ref HmacDrbgContext; config: NimbusConf):
Result[JwtSharedKey, JwtError] Result[JwtSharedKey, JwtError]
{.gcsafe, raises: [Defect,JwtExcept].} = {.gcsafe, raises: [Defect,JwtExcept].} =
## Variant of `jwtSharedSecret()` with explicit random generator argument. ## Variant of `jwtSharedSecret()` with explicit random generator argument.

View File

@ -16,7 +16,6 @@ import
std/[options, strutils], std/[options, strutils],
../utils, ../utils,
./pow/[pow_cache, pow_dataset], ./pow/[pow_cache, pow_dataset],
bearssl,
eth/[common, keys, p2p, rlp], eth/[common, keys, p2p, rlp],
ethash, ethash,
nimcrypto, nimcrypto,
@ -63,7 +62,7 @@ type
# You should only create one instance of the RNG per application / library # You should only create one instance of the RNG per application / library
# Ref is used so that it can be shared between components # Ref is used so that it can be shared between components
rng: ref BrHmacDrbgContext rng: ref HmacDrbgContext
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Private functions: RLP support # Private functions: RLP support
@ -161,7 +160,7 @@ proc mineFull(tm: PowRef; blockNumber: BlockNumber; powHeaderDigest: Hash256,
# --------------- # ---------------
proc init(tm: PowRef; proc init(tm: PowRef;
rng: Option[ref BrHmacDrbgContext]; rng: Option[ref HmacDrbgContext];
light: Option[PowCacheRef]; light: Option[PowCacheRef];
full: Option[PowDatasetRef]) = full: Option[PowDatasetRef]) =
## Constructor ## Constructor
@ -185,7 +184,7 @@ proc init(tm: PowRef;
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
proc new*(T: type PowRef; proc new*(T: type PowRef;
rng: ref BrHmacDrbgContext; rng: ref HmacDrbgContext;
cache: PowCacheRef; cache: PowCacheRef;
dataset: PowDatasetRef): T = dataset: PowDatasetRef): T =
## Constructor ## Constructor
@ -197,9 +196,9 @@ proc new*(T: type PowRef; cache: PowCacheRef; dataset: PowDatasetRef): T =
## Constructor ## Constructor
new result new result
result.init( result.init(
none(ref BrHmacDrbgContext), some(cache), some(dataset)) none(ref HmacDrbgContext), some(cache), some(dataset))
proc new*(T: type PowRef; rng: ref BrHmacDrbgContext): T = proc new*(T: type PowRef; rng: ref HmacDrbgContext): T =
## Constructor ## Constructor
new result new result
result.init( result.init(
@ -209,7 +208,7 @@ proc new*(T: type PowRef): T =
## Constructor ## Constructor
new result new result
result.init( result.init(
none(ref BrHmacDrbgContext), none(PowCacheRef), none(PowDatasetRef)) none(ref HmacDrbgContext), none(PowCacheRef), none(PowDatasetRef))
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Public functions # Public functions
@ -298,7 +297,7 @@ proc getNonce*(tm: PowRef; number: BlockNumber; powHeaderDigest: Hash256;
{.gcsafe,raises: [Defect,CatchableError].} = {.gcsafe,raises: [Defect,CatchableError].} =
## Variant of `getNonce()` ## Variant of `getNonce()`
var startNonce: array[8,byte] var startNonce: array[8,byte]
tm.rng[].brHmacDrbgGenerate(startNonce) tm.rng[].generate(startNonce)
tm.getNonce(number, powHeaderDigest, difficulty, startNonce) tm.getNonce(number, powHeaderDigest, difficulty, startNonce)
proc getNonce*(tm: PowRef; header: BlockHeader): BlockNonce proc getNonce*(tm: PowRef; header: BlockHeader): BlockNonce

2
vendor/nim-bearssl vendored

@ -1 +1 @@
Subproject commit 65b74302e03912ab5bde64b6da10d05896139007 Subproject commit 32e125015ae4251675763842366380795a91b722

2
vendor/nim-eth vendored

@ -1 +1 @@
Subproject commit 8761ea3222f8d4fbd7ebae6755665e791499d7f2 Subproject commit 2c08626ed8261fa18adc22eaf465b57931b88a65