Revert "track nim-libp2p's unstable branch and nim-bearssl's master branch"

This reverts commit c480fb5732.
This commit is contained in:
Dmitriy Ryajov 2022-09-23 15:59:31 -06:00 committed by GitHub
parent c480fb5732
commit 9dfa86767d
12 changed files with 48 additions and 48 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -76,7 +76,7 @@
import
std/[tables, sets, options, math, sequtils, algorithm, strutils],
stew/shims/net as stewNet, json_serialization/std/net,
stew/[base64, endians2, results], chronicles, chronicles/chronos_tools, chronos, chronos/timer, stint, bearssl/rand,
stew/[base64, endians2, results], chronicles, chronicles/chronos_tools, chronos, chronos/timer, stint, bearssl,
metrics,
libp2p/[crypto/crypto, routing_record],
"."/[transport, messages, messages_encoding, node, routing_table, spr, random2, ip_vote, nodes_verification, lru]
@ -159,7 +159,7 @@ type
enrAutoUpdate: bool
talkProtocols*: Table[seq[byte], TalkProtocol] # TODO: Table is a bit of
# overkill here, use sequence
rng*: ref HmacDrbgContext
rng*: ref BrHmacDrbgContext
providers: ItemsCache
TalkProtocolHandler* = proc(p: TalkProtocol, request: seq[byte], fromId: NodeId, fromUdpAddress: Address): seq[byte]

View File

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

View File

@ -9,7 +9,7 @@
import
std/[algorithm, times, sequtils, bitops, sets, options, tables],
stint, chronicles, metrics, bearssl/rand, chronos, stew/shims/net as stewNet,
stint, chronicles, metrics, bearssl, chronos, stew/shims/net as stewNet,
"."/[node, random2, spr]
export options
@ -46,7 +46,7 @@ type
ipLimits: IpLimits ## IP limits for total routing table: all buckets and
## replacement caches.
distanceCalculator: DistanceCalculator
rng: ref HmacDrbgContext
rng: ref BrHmacDrbgContext
KBucket = ref object
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")
proc init*(T: type RoutingTable, localNode: Node, bitsPerHop = DefaultBitsPerHop,
ipLimits = DefaultTableIpLimits, rng: ref HmacDrbgContext,
ipLimits = DefaultTableIpLimits, rng: ref BrHmacDrbgContext,
distanceCalculator = XorDistanceCalculator): T =
## Initialize the routing table for provided `Node` and bitsPerHop value.
## `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
import
std/[tables, options],
bearssl/rand,
bearssl,
chronos,
chronicles,
libp2p/crypto/crypto,
@ -27,7 +27,7 @@ type
transp: DatagramTransport
pendingRequests: Table[AESGCMNonce, PendingRequest]
codec*: Codec
rng: ref HmacDrbgContext
rng: ref BrHmacDrbgContext
PendingRequest = object
node: Node

View File

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

View File

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

View File

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

View File

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