track nim-libp2p's unstable branch and nim-bearssl's master branch (#51)

* track nim-libp2p's unstable branch and nim-bearssl's master branch

refactor accordingly: mainly switching from `import bearssl` to
`import bearssl/rand`, `BrHmacDrbgContext` to `HmacDrbgContext`, and related
changes

* fix ambiguous identifier

* nim 1.4 is deprecated

Co-authored-by: Michael Bradley, Jr <michaelsbradleyjr@gmail.com>
This commit is contained in:
Dmitriy Ryajov 2022-11-02 10:21:05 -06:00 committed by GitHub
parent d6d255b4b5
commit e4e7a3e11f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 51 additions and 51 deletions

View File

@ -23,7 +23,7 @@ jobs:
cpu: amd64 cpu: amd64
#- os: windows #- os: windows
#cpu: i386 #cpu: i386
branch: [version-1-2, version-1-4, version-1-6] branch: [version-1-2, version-1-6]
include: include:
- target: - target:
os: linux os: linux

View File

@ -10,10 +10,10 @@ skipDirs = @["tests"]
# Dependencies # Dependencies
requires "nim >= 1.2.0", requires "nim >= 1.2.0",
"nimcrypto >= 0.5.4 & < 0.6.0", "nimcrypto >= 0.5.4 & < 0.6.0",
"bearssl >= 0.1.5 & < 0.2.0", "bearssl#head",
"chronicles >= 0.10.2 & < 0.11.0", "chronicles >= 0.10.2 & < 0.11.0",
"chronos >= 3.0.11 & < 3.1.0", "chronos >= 3.0.11 & < 3.1.0",
"libp2p#c7504d2446717a48a79c8b15e0f21bbfc84957ba", "libp2p#unstable",
"metrics", "metrics",
"protobufserialization >= 0.2.0 & < 0.3.0", "protobufserialization >= 0.2.0 & < 0.3.0",
"secp256k1 >= 0.5.2 & < 0.6.0", "secp256k1 >= 0.5.2 & < 0.6.0",
@ -59,4 +59,4 @@ task coverage, "generates code coverage report":
exec("genhtml coverage/coverage.f.info --output-directory coverage/report") exec("genhtml coverage/coverage.f.info --output-directory coverage/report")
echo "Opening HTML coverage report in browser..." echo "Opening HTML coverage report in browser..."
exec("open coverage/report/index.html") exec("open coverage/report/index.html")

View File

@ -15,7 +15,7 @@
import import
std/[hashes, net, options, sugar, tables], std/[hashes, net, options, sugar, tables],
bearssl, bearssl/rand,
chronicles, chronicles,
stew/[results, byteutils], stew/[results, byteutils],
stint, stint,
@ -206,13 +206,13 @@ proc encodeStaticHeader*(flag: Flag, nonce: AESGCMNonce, authSize: int):
# TODO: assert on authSize of > 2^16? # TODO: assert on authSize of > 2^16?
result.add((uint16(authSize)).toBytesBE()) result.add((uint16(authSize)).toBytesBE())
proc encodeMessagePacket*(rng: var BrHmacDrbgContext, c: var Codec, proc encodeMessagePacket*(rng: var HmacDrbgContext, c: var Codec,
toId: NodeId, toAddr: Address, message: openArray[byte]): toId: NodeId, toAddr: Address, message: openArray[byte]):
(seq[byte], AESGCMNonce) = (seq[byte], AESGCMNonce) =
var nonce: AESGCMNonce var nonce: AESGCMNonce
brHmacDrbgGenerate(rng, nonce) # Random AESGCM nonce hmacDrbgGenerate(rng, nonce) # Random AESGCM nonce
var iv: array[ivSize, byte] var iv: array[ivSize, byte]
brHmacDrbgGenerate(rng, iv) # Random IV hmacDrbgGenerate(rng, iv) # Random IV
# static-header # static-header
let authdata = c.localNode.id.toByteArrayBE() let authdata = c.localNode.id.toByteArrayBE()
@ -238,7 +238,7 @@ proc encodeMessagePacket*(rng: var BrHmacDrbgContext, c: var Codec,
# 1 byte (e.g "01c20101"). Could increase to 27 for 8 bytes requestId in # 1 byte (e.g "01c20101"). Could increase to 27 for 8 bytes requestId in
# case this must not look like a random packet. # case this must not look like a random packet.
var randomData: array[gcmTagSize + 4, byte] var randomData: array[gcmTagSize + 4, byte]
brHmacDrbgGenerate(rng, randomData) hmacDrbgGenerate(rng, randomData)
messageEncrypted.add(randomData) messageEncrypted.add(randomData)
discovery_session_lru_cache_misses.inc() discovery_session_lru_cache_misses.inc()
@ -251,11 +251,11 @@ proc encodeMessagePacket*(rng: var BrHmacDrbgContext, c: var Codec,
return (packet, nonce) return (packet, nonce)
proc encodeWhoareyouPacket*(rng: var BrHmacDrbgContext, c: var Codec, proc encodeWhoareyouPacket*(rng: var HmacDrbgContext, c: var Codec,
toId: NodeId, toAddr: Address, requestNonce: AESGCMNonce, recordSeq: uint64, toId: NodeId, toAddr: Address, requestNonce: AESGCMNonce, recordSeq: uint64,
pubkey: Option[PublicKey]): seq[byte] = pubkey: Option[PublicKey]): seq[byte] =
var idNonce: IdNonce var idNonce: IdNonce
brHmacDrbgGenerate(rng, idNonce) hmacDrbgGenerate(rng, idNonce)
# authdata # authdata
var authdata: seq[byte] var authdata: seq[byte]
@ -272,7 +272,7 @@ proc encodeWhoareyouPacket*(rng: var BrHmacDrbgContext, c: var Codec,
header.add(authdata) header.add(authdata)
var iv: array[ivSize, byte] var iv: array[ivSize, byte]
brHmacDrbgGenerate(rng, iv) # Random IV hmacDrbgGenerate(rng, iv) # Random IV
let maskedHeader = encryptHeader(toId, iv, header) let maskedHeader = encryptHeader(toId, iv, header)
@ -293,14 +293,14 @@ proc encodeWhoareyouPacket*(rng: var BrHmacDrbgContext, c: var Codec,
return packet return packet
proc encodeHandshakePacket*(rng: var BrHmacDrbgContext, c: var Codec, proc encodeHandshakePacket*(rng: var HmacDrbgContext, c: var Codec,
toId: NodeId, toAddr: Address, message: openArray[byte], toId: NodeId, toAddr: Address, message: openArray[byte],
whoareyouData: WhoareyouData, pubkey: PublicKey): EncodeResult[seq[byte]] = whoareyouData: WhoareyouData, pubkey: PublicKey): EncodeResult[seq[byte]] =
var header: seq[byte] var header: seq[byte]
var nonce: AESGCMNonce var nonce: AESGCMNonce
brHmacDrbgGenerate(rng, nonce) hmacDrbgGenerate(rng, nonce)
var iv: array[ivSize, byte] var iv: array[ivSize, byte]
brHmacDrbgGenerate(rng, iv) # Random IV hmacDrbgGenerate(rng, iv) # Random IV
var authdata: seq[byte] var authdata: seq[byte]
var authdataHead: seq[byte] var authdataHead: seq[byte]

View File

@ -14,7 +14,7 @@
import import
std/[hashes, net], std/[hashes, net],
bearssl, bearssl/rand,
./spr, ./spr,
./node, ./node,
../../../../dht/providers_messages ../../../../dht/providers_messages
@ -130,7 +130,7 @@ template messageKind*(T: typedesc[SomeMessage]): MessageKind =
proc hash*(reqId: RequestId): Hash = proc hash*(reqId: RequestId): Hash =
hash(reqId.id) hash(reqId.id)
proc init*(T: type RequestId, rng: var BrHmacDrbgContext): T = proc init*(T: type RequestId, rng: var HmacDrbgContext): T =
var reqId = RequestId(id: newSeq[byte](8)) # RequestId must be <= 8 bytes var reqId = RequestId(id: newSeq[byte](8)) # RequestId must be <= 8 bytes
brHmacDrbgGenerate(rng, reqId.id) hmacDrbgGenerate(rng, reqId.id)
reqId reqId

View File

@ -9,7 +9,7 @@
import import
std/hashes, std/hashes,
bearssl, bearssl/rand,
chronicles, chronicles,
chronos, chronos,
nimcrypto, nimcrypto,
@ -119,9 +119,9 @@ func `==`*(a, b: Node): bool =
func hash*(id: NodeId): Hash = func hash*(id: NodeId): Hash =
hash(id.toByteArrayBE) hash(id.toByteArrayBE)
proc random*(T: type NodeId, rng: var BrHmacDrbgContext): T = proc random*(T: type NodeId, rng: var HmacDrbgContext): T =
var id: NodeId var id: NodeId
brHmacDrbgGenerate(addr rng, addr id, csize_t(sizeof(id))) hmacDrbgGenerate(rng, addr id, csize_t(sizeof(id)))
id id

View File

@ -54,11 +54,11 @@
## The result is that in an implementation which just stores buckets per ## The result is that in an implementation which just stores buckets per
## logarithmic distance, it simply needs to return the right bucket. In our ## logarithmic distance, it simply needs to return the right bucket. In our
## split-bucket implementation, this cannot be done as such and thus the closest ## split-bucket implementation, this cannot be done as such and thus the closest
## neighbours search is still done. And to do this, a reverse calculation of an ## neighbors search is still done. And to do this, a reverse calculation of an
## id at given logarithmic distance is needed (which is why there is the ## id at given logarithmic distance is needed (which is why there is the
## `idAtDistance` proc). Next, nodes with invalid distances need to be filtered ## `idAtDistance` proc). Next, nodes with invalid distances need to be filtered
## out to be compliant to the specification. This can most likely get further ## out to be compliant to the specification. This can most likely get further
## optimised, but it sounds likely better to switch away from the split-bucket ## optimized, but it sounds likely better to switch away from the split-bucket
## approach. I believe that the main benefit it has is improved lookups ## approach. I believe that the main benefit it has is improved lookups
## (due to no unbalanced branches), and it looks like this will be negated by ## (due to no unbalanced branches), and it looks like this will be negated by
## limiting the returned nodes to only the ones of the requested logarithmic ## limiting the returned nodes to only the ones of the requested logarithmic
@ -81,7 +81,7 @@ import
pkg/[chronicles, chronicles/chronos_tools], pkg/[chronicles, chronicles/chronos_tools],
pkg/chronos, pkg/chronos,
pkg/stint, pkg/stint,
pkg/bearssl, pkg/bearssl/rand,
pkg/metrics pkg/metrics
import "."/[ import "."/[
@ -170,7 +170,7 @@ type
ipVote: IpVote ipVote: IpVote
enrAutoUpdate: bool enrAutoUpdate: bool
talkProtocols*: Table[seq[byte], TalkProtocol] # TODO: Table is a bit of talkProtocols*: Table[seq[byte], TalkProtocol] # TODO: Table is a bit of
rng*: ref BrHmacDrbgContext rng*: ref HmacDrbgContext
providers: ProvidersManager providers: ProvidersManager
TalkProtocolHandler* = proc(p: TalkProtocol, request: seq[byte], fromId: NodeId, fromUdpAddress: Address): seq[byte] TalkProtocolHandler* = proc(p: TalkProtocol, request: seq[byte], fromId: NodeId, fromUdpAddress: Address): seq[byte]

View File

@ -1,22 +1,22 @@
import bearssl import bearssl/rand
## Random helpers: similar as in stdlib, but with BrHmacDrbgContext rng ## Random helpers: similar as in stdlib, but with HmacDrbgContext rng
# TODO: Move these somewhere else? # TODO: Move these somewhere else?
const randMax = 18_446_744_073_709_551_615'u64 const randMax = 18_446_744_073_709_551_615'u64
proc rand*(rng: var BrHmacDrbgContext, max: Natural): int = proc rand*(rng: var HmacDrbgContext, max: Natural): int =
if max == 0: return 0 if max == 0: return 0
var x: uint64 var x: uint64
while true: while true:
brHmacDrbgGenerate(addr rng, addr x, csize_t(sizeof(x))) hmacDrbgGenerate(rng, addr x, csize_t(sizeof(x)))
if x < randMax - (randMax mod (uint64(max) + 1'u64)): # against modulo bias if x < randMax - (randMax mod (uint64(max) + 1'u64)): # against modulo bias
return int(x mod (uint64(max) + 1'u64)) return int(x mod (uint64(max) + 1'u64))
proc sample*[T](rng: var BrHmacDrbgContext, a: openArray[T]): T = proc sample*[T](rng: var HmacDrbgContext, a: openArray[T]): T =
result = a[rng.rand(a.high)] result = a[rng.rand(a.high)]
proc shuffle*[T](rng: var BrHmacDrbgContext, a: var openArray[T]) = proc shuffle*[T](rng: var HmacDrbgContext, a: var openArray[T]) =
for i in countdown(a.high, 1): for i in countdown(a.high, 1):
let j = rng.rand(i) let j = rng.rand(i)
swap(a[i], a[j]) swap(a[i], a[j])

View File

@ -9,7 +9,7 @@
import import
std/[algorithm, times, sequtils, bitops, sets, options, tables], std/[algorithm, times, sequtils, bitops, sets, options, tables],
stint, chronicles, metrics, bearssl, chronos, stew/shims/net as stewNet, stint, chronicles, metrics, bearssl/rand, chronos, stew/shims/net as stewNet,
"."/[node, random2, spr] "."/[node, random2, spr]
export options export options
@ -46,7 +46,7 @@ type
ipLimits: IpLimits ## IP limits for total routing table: all buckets and ipLimits: IpLimits ## IP limits for total routing table: all buckets and
## replacement caches. ## replacement caches.
distanceCalculator: DistanceCalculator distanceCalculator: DistanceCalculator
rng: ref BrHmacDrbgContext rng: ref HmacDrbgContext
KBucket = ref object KBucket = ref object
istart, iend: NodeId ## Range of NodeIds this KBucket covers. This is not a istart, iend: NodeId ## Range of NodeIds this KBucket covers. This is not a
@ -278,7 +278,7 @@ proc computeSharedPrefixBits(nodes: openArray[NodeId]): int =
doAssert(false, "Unable to calculate number of shared prefix bits") doAssert(false, "Unable to calculate number of shared prefix bits")
proc init*(T: type RoutingTable, localNode: Node, bitsPerHop = DefaultBitsPerHop, proc init*(T: type RoutingTable, localNode: Node, bitsPerHop = DefaultBitsPerHop,
ipLimits = DefaultTableIpLimits, rng: ref BrHmacDrbgContext, ipLimits = DefaultTableIpLimits, rng: ref HmacDrbgContext,
distanceCalculator = XorDistanceCalculator): T = distanceCalculator = XorDistanceCalculator): T =
## Initialize the routing table for provided `Node` and bitsPerHop value. ## Initialize the routing table for provided `Node` and bitsPerHop value.
## `bitsPerHop` is default set to 5 as recommended by original Kademlia paper. ## `bitsPerHop` is default set to 5 as recommended by original Kademlia paper.

View File

@ -7,7 +7,7 @@
# Everything below the handling of ordinary messages # Everything below the handling of ordinary messages
import import
std/[tables, options], std/[tables, options],
bearssl, bearssl/rand,
chronos, chronos,
chronicles, chronicles,
libp2p/crypto/crypto, libp2p/crypto/crypto,
@ -27,7 +27,7 @@ type
transp: DatagramTransport transp: DatagramTransport
pendingRequests: Table[AESGCMNonce, PendingRequest] pendingRequests: Table[AESGCMNonce, PendingRequest]
codec*: Codec codec*: Codec
rng: ref BrHmacDrbgContext rng: ref HmacDrbgContext
PendingRequest = object PendingRequest = object
node: Node node: Node

View File

@ -1,5 +1,5 @@
import import
bearssl, bearssl/rand,
chronos, chronos,
libp2p/crypto/[crypto, secp], libp2p/crypto/[crypto, secp],
libp2p/multiaddress, libp2p/multiaddress,
@ -23,7 +23,7 @@ proc example*(T: type NodeId, rng: ref HmacDrbgContext): NodeId =
pubKey.toNodeId().expect("Public key valid for node id") pubKey.toNodeId().expect("Public key valid for node id")
proc initDiscoveryNode*( proc initDiscoveryNode*(
rng: ref BrHmacDrbgContext, rng: ref HmacDrbgContext,
privKey: PrivateKey, privKey: PrivateKey,
address: Address, address: Address,
bootstrapRecords: openArray[SignedPeerRecord] = [], bootstrapRecords: openArray[SignedPeerRecord] = [],
@ -62,7 +62,7 @@ proc generateNode*(privKey: PrivateKey, port: int = 20302,
.expect("Properly intialized private key") .expect("Properly intialized private key")
result = newNode(spr).expect("Properly initialized node") result = newNode(spr).expect("Properly initialized node")
proc generateNRandomNodes*(rng: ref BrHmacDrbgContext, n: int): seq[Node] = proc generateNRandomNodes*(rng: ref HmacDrbgContext, n: int): seq[Node] =
var res = newSeq[Node]() var res = newSeq[Node]()
for i in 1..n: for i in 1..n:
let let
@ -71,7 +71,7 @@ proc generateNRandomNodes*(rng: ref BrHmacDrbgContext, n: int): seq[Node] =
res.add(node) res.add(node)
res res
proc nodeAndPrivKeyAtDistance*(n: Node, rng: var BrHmacDrbgContext, d: uint32, proc nodeAndPrivKeyAtDistance*(n: Node, rng: var HmacDrbgContext, d: uint32,
ip: ValidIpAddress = ValidIpAddress.init("127.0.0.1")): (Node, PrivateKey) = ip: ValidIpAddress = ValidIpAddress.init("127.0.0.1")): (Node, PrivateKey) =
while true: while true:
let let
@ -80,19 +80,19 @@ proc nodeAndPrivKeyAtDistance*(n: Node, rng: var BrHmacDrbgContext, d: uint32,
if logDistance(n.id, node.id) == d: if logDistance(n.id, node.id) == d:
return (node, privKey) return (node, privKey)
proc nodeAtDistance*(n: Node, rng: var BrHmacDrbgContext, d: uint32, proc nodeAtDistance*(n: Node, rng: var HmacDrbgContext, d: uint32,
ip: ValidIpAddress = ValidIpAddress.init("127.0.0.1")): Node = ip: ValidIpAddress = ValidIpAddress.init("127.0.0.1")): Node =
let (node, _) = n.nodeAndPrivKeyAtDistance(rng, d, ip) let (node, _) = n.nodeAndPrivKeyAtDistance(rng, d, ip)
node node
proc nodesAtDistance*( proc nodesAtDistance*(
n: Node, rng: var BrHmacDrbgContext, d: uint32, amount: int, n: Node, rng: var HmacDrbgContext, d: uint32, amount: int,
ip: ValidIpAddress = ValidIpAddress.init("127.0.0.1")): seq[Node] = ip: ValidIpAddress = ValidIpAddress.init("127.0.0.1")): seq[Node] =
for i in 0..<amount: for i in 0..<amount:
result.add(nodeAtDistance(n, rng, d, ip)) result.add(nodeAtDistance(n, rng, d, ip))
proc nodesAtDistanceUniqueIp*( proc nodesAtDistanceUniqueIp*(
n: Node, rng: var BrHmacDrbgContext, d: uint32, amount: int, n: Node, rng: var HmacDrbgContext, d: uint32, amount: int,
ip: ValidIpAddress = ValidIpAddress.init("127.0.0.1")): seq[Node] = ip: ValidIpAddress = ValidIpAddress.init("127.0.0.1")): seq[Node] =
var ta = initTAddress(ip, Port(0)) var ta = initTAddress(ip, Port(0))
for i in 0..<amount: for i in 0..<amount:

View File

@ -12,7 +12,7 @@
import import
std/[options, sequtils], std/[options, sequtils],
asynctest, asynctest,
bearssl, bearssl/rand,
chronicles, chronicles,
chronos, chronos,
nimcrypto, nimcrypto,

View File

@ -3,7 +3,7 @@
import import
std/tables, std/tables,
chronos, chronicles, stint, asynctest, stew/shims/net, chronos, chronicles, stint, asynctest, stew/shims/net,
stew/byteutils, bearssl, stew/byteutils, bearssl/rand,
libp2p/crypto/crypto, libp2p/crypto/crypto,
libp2pdht/discv5/[transport, spr, node, routing_table, encoding, sessions, nodes_verification], libp2pdht/discv5/[transport, spr, node, routing_table, encoding, sessions, nodes_verification],
libp2pdht/discv5/crypto as dhtcrypto, libp2pdht/discv5/crypto as dhtcrypto,

View File

@ -3,7 +3,7 @@
import import
std/[options, sequtils, tables], std/[options, sequtils, tables],
asynctest/unittest2, asynctest/unittest2,
bearssl, bearssl/rand,
chronos, chronos,
libp2p/crypto/secp, libp2p/crypto/secp,
libp2pdht/discv5/[messages, messages_encoding, encoding, spr, node, sessions], libp2pdht/discv5/[messages, messages_encoding, encoding, spr, node, sessions],
@ -480,7 +480,7 @@ suite "Discovery v5.1 Additional Encode/Decode":
test "Encrypt / Decrypt header": test "Encrypt / Decrypt header":
var nonce: AESGCMNonce var nonce: AESGCMNonce
brHmacDrbgGenerate(rng[], nonce) hmacDrbgGenerate(rng[], nonce)
let let
nodeId = NodeId.example(rng) nodeId = NodeId.example(rng)
authdata = newSeq[byte](32) authdata = newSeq[byte](32)
@ -489,7 +489,7 @@ suite "Discovery v5.1 Additional Encode/Decode":
header = staticHeader & authdata header = staticHeader & authdata
var iv: array[128 div 8, byte] var iv: array[128 div 8, byte]
brHmacDrbgGenerate(rng[], iv) hmacDrbgGenerate(rng[], iv)
let let
encrypted = encryptHeader(nodeId, iv, header) encrypted = encryptHeader(nodeId, iv, header)
@ -538,7 +538,7 @@ suite "Discovery v5.1 Additional Encode/Decode":
test "Encode / Decode Whoareyou Packet": test "Encode / Decode Whoareyou Packet":
var requestNonce: AESGCMNonce var requestNonce: AESGCMNonce
brHmacDrbgGenerate(rng[], requestNonce) hmacDrbgGenerate(rng[], requestNonce)
let recordSeq = 0'u64 let recordSeq = 0'u64
let data = encodeWhoareyouPacket(rng[], codecA, nodeB.id, let data = encodeWhoareyouPacket(rng[], codecA, nodeB.id,
@ -559,7 +559,7 @@ suite "Discovery v5.1 Additional Encode/Decode":
test "Encode / Decode Handshake Message Packet": test "Encode / Decode Handshake Message Packet":
var requestNonce: AESGCMNonce var requestNonce: AESGCMNonce
brHmacDrbgGenerate(rng[], requestNonce) hmacDrbgGenerate(rng[], requestNonce)
let let
recordSeq = 1'u64 recordSeq = 1'u64
m = PingMessage(sprSeq: 0) m = PingMessage(sprSeq: 0)
@ -595,7 +595,7 @@ suite "Discovery v5.1 Additional Encode/Decode":
test "Encode / Decode Handshake Message Packet with SPR": test "Encode / Decode Handshake Message Packet with SPR":
var requestNonce: AESGCMNonce var requestNonce: AESGCMNonce
brHmacDrbgGenerate(rng[], requestNonce) hmacDrbgGenerate(rng[], requestNonce)
let let
recordSeq = 0'u64 recordSeq = 0'u64
m = PingMessage(sprSeq: 0) m = PingMessage(sprSeq: 0)