Fix overflow problem.

Fix compilation warnings.
This commit is contained in:
cheatfate 2019-10-29 20:51:48 +02:00
parent 400218ba01
commit 833e8c9406
No known key found for this signature in database
GPG Key ID: 46ADD633A7201F95
32 changed files with 166 additions and 136 deletions

View File

@ -114,7 +114,7 @@ proc checkPublic(key: openarray[byte], curve: cint): uint32 =
var x = [0x00'u8, 0x01'u8]
var impl = brEcGetDefault()
var orderlen = 0
var order = impl.order(curve, addr orderlen)
discard impl.order(curve, addr orderlen)
result = impl.mul(cast[ptr cuchar](unsafeAddr ckey[0]), len(ckey),
cast[ptr cuchar](addr x[0]), len(x), curve)
@ -766,7 +766,6 @@ proc initRaw*(sig: var EcSignature, data: openarray[byte]): bool =
## or ``Sig521Length``.
##
## Procedure returns ``true`` on success, ``false`` otherwise.
var curve: cint
let length = len(data)
if (length == Sig256Length) or (length == Sig384Length) or
(length == Sig521Length):
@ -815,7 +814,6 @@ proc scalarMul*(pub: EcPublicKey, sec: EcPrivateKey): EcPublicKey =
if pub.key.curve == sec.key.curve:
var key = new EcPublicKey
if key.copy(pub):
var slength = cint(0)
let poffset = key.getOffset()
let soffset = sec.getOffset()
if poffset >= 0 and soffset >= 0:
@ -840,7 +838,6 @@ proc toSecret*(pubkey: EcPublicKey, seckey: EcPrivateKey,
## for `secp384r1` and 66 bytes for `secp521r1`.
doAssert((not isNil(pubkey)) and (not isNil(seckey)))
var mult = scalarMul(pubkey, seckey)
var length = 0
if not isNil(mult):
if seckey.key.curve == BR_EC_SECP256R1:
result = Secret256Length

View File

@ -472,7 +472,6 @@ proc read*(ab: var Asn1Buffer, field: var Asn1Field): Asn1Status =
tag, ttag, offset: int
length, tlength: uint64
klass: Asn1Class
res: Asn1Status
inclass: bool
inclass = false

View File

@ -386,7 +386,6 @@ proc init*(key: var RsaPrivateKey, data: openarray[byte]): Asn1Status =
## Procedure returns ``Asn1Status``.
var
field, rawn, rawpube, rawprie, rawp, rawq, rawdp, rawdq, rawiq: Asn1Field
version: uint64
var ab = Asn1Buffer.init(data)
result = ab.read(field)
@ -484,7 +483,7 @@ proc init*(key: var RsaPublicKey, data: openarray[byte]): Asn1Status =
## ``data``.
##
## Procedure returns ``Asn1Status``.
var field, rawn, rawe, oid: Asn1Field
var field, rawn, rawe: Asn1Field
var ab = Asn1Buffer.init(data)
result = ab.read(field)

View File

@ -6,7 +6,6 @@
## at your option.
## This file may not be copied, modified, or distributed except according to
## those terms.
import strutils
import secp256k1, nimcrypto/sysrand, nimcrypto/utils, nimcrypto/hash,
nimcrypto/sha2
export sha2
@ -268,10 +267,10 @@ proc toBytes*(key: SkPublicKey, data: var openarray[byte]): int =
var length = csize(len(data))
result = SkRawPublicKeySize
if len(data) >= SkRawPublicKeySize:
let res = secp256k1_ec_pubkey_serialize(ctx.context,
cast[ptr cuchar](addr data[0]),
addr length, unsafeAddr key,
SECP256K1_EC_COMPRESSED)
discard secp256k1_ec_pubkey_serialize(ctx.context,
cast[ptr cuchar](addr data[0]),
addr length, unsafeAddr key,
SECP256K1_EC_COMPRESSED)
proc toBytes*(sig: SkSignature, data: var openarray[byte]): int =
## Serialize Secp256k1 `signature` ``sig`` to raw binary form and store it

View File

@ -572,7 +572,6 @@ proc `$`*(value: MultiAddress): string =
## Return string representation of MultiAddress ``value``.
var header: uint64
var vb = value
var data = newSeq[byte]()
var parts = newSeq[string]()
var part: string
while true:

View File

@ -11,7 +11,7 @@
##
## TODO:
## 1. base32z
import tables, strutils
import tables
import base32, base58, base64
type
@ -20,18 +20,14 @@ type
MultiBase* = object
MBDecoder = proc(inbytes: openarray[char],
outbytes: var openarray[byte],
outlen: var int): MultibaseStatus {.nimcall.}
MBEncoder = proc(inbytes: openarray[byte],
outbytes: var openarray[char],
outlen: var int): MultibaseStatus {.nimcall.}
MBCodeSize = proc(length: int): int {.nimcall.}
MBCodec = object
code: char
name: string
encr: MBEncoder
encr: proc(inbytes: openarray[byte],
outbytes: var openarray[char],
outlen: var int): MultibaseStatus {.nimcall.}
decr: proc(inbytes: openarray[char],
outbytes: var openarray[byte],
outlen: var int): MultibaseStatus {.nimcall.}

View File

@ -165,7 +165,6 @@ proc init*(t: typedesc[PeerID], pubkey: PublicKey): PeerID =
## Create new peer id from public key ``pubkey``.
var pubraw = pubkey.getBytes()
var mh: MultiHash
var codec: MultiCodec
if len(pubraw) <= maxInlineKeyLength:
mh = MultiHash.digest("identity", pubraw)
else:

View File

@ -156,13 +156,13 @@ proc write*(pb: var ProtoBuffer, field: ProtoField) =
doAssert(pb.isEnough(8))
var value = cast[uint64](field.vfloat64)
pb.buffer[pb.offset] = byte(value and 0xFF'u32)
pb.buffer[pb.offset + 1] = byte((value shr 8) and 0xFF'u32)
pb.buffer[pb.offset + 2] = byte((value shr 16) and 0xFF'u32)
pb.buffer[pb.offset + 3] = byte((value shr 24) and 0xFF'u32)
pb.buffer[pb.offset + 4] = byte((value shr 32) and 0xFF'u32)
pb.buffer[pb.offset + 5] = byte((value shr 40) and 0xFF'u32)
pb.buffer[pb.offset + 6] = byte((value shr 48) and 0xFF'u32)
pb.buffer[pb.offset + 7] = byte((value shr 56) and 0xFF'u32)
pb.buffer[pb.offset + 1] = byte((value shr 8) and 0xFF'u64)
pb.buffer[pb.offset + 2] = byte((value shr 16) and 0xFF'u64)
pb.buffer[pb.offset + 3] = byte((value shr 24) and 0xFF'u64)
pb.buffer[pb.offset + 4] = byte((value shr 32) and 0xFF'u64)
pb.buffer[pb.offset + 5] = byte((value shr 40) and 0xFF'u64)
pb.buffer[pb.offset + 6] = byte((value shr 48) and 0xFF'u64)
pb.buffer[pb.offset + 7] = byte((value shr 56) and 0xFF'u64)
pb.offset += 8
of ProtoFieldKind.Fixed32:
doAssert(pb.isEnough(4))
@ -196,17 +196,17 @@ proc finish*(pb: var ProtoBuffer) =
pb.offset = pos
elif WithUint32BeLength in pb.options:
let size = uint(len(pb.buffer) - 4)
pb.buffer[0] = byte((size shr 24) and 0xFF)
pb.buffer[1] = byte((size shr 16) and 0xFF)
pb.buffer[2] = byte((size shr 8) and 0xFF)
pb.buffer[3] = byte(size and 0xFF)
pb.buffer[0] = byte((size shr 24) and 0xFF'u)
pb.buffer[1] = byte((size shr 16) and 0xFF'u)
pb.buffer[2] = byte((size shr 8) and 0xFF'u)
pb.buffer[3] = byte(size and 0xFF'u)
pb.offset = 4
elif WithUint32LeLength in pb.options:
let size = uint(len(pb.buffer) - 4)
pb.buffer[0] = byte(size and 0xFF)
pb.buffer[1] = byte((size shr 8) and 0xFF)
pb.buffer[2] = byte((size shr 16) and 0xFF)
pb.buffer[3] = byte((size shr 24) and 0xFF)
pb.buffer[0] = byte(size and 0xFF'u)
pb.buffer[1] = byte((size shr 8) and 0xFF'u)
pb.buffer[2] = byte((size shr 16) and 0xFF'u)
pb.buffer[3] = byte((size shr 24) and 0xFF'u)
pb.offset = 4
else:
pb.offset = 0

View File

@ -208,9 +208,9 @@ proc writeMessage*(sconn: SecureConnection, message: seq[byte]) {.async.} =
sconn.writerMac.finish(msg.toOpenArray(mo, mo + macsize - 1))
sconn.writerMac.reset()
let length = len(message) + macsize
msg[0] = byte(length shr 24)
msg[1] = byte(length shr 16)
msg[2] = byte(length shr 8)
msg[0] = byte((length shr 24) and 0xFF)
msg[1] = byte((length shr 16) and 0xFF)
msg[2] = byte((length shr 8) and 0xFF)
msg[3] = byte(length and 0xFF)
trace "Writing message", message = toHex(msg)
try:

View File

@ -80,7 +80,6 @@ proc writeSeq*[T: byte|char](vb: var VBuffer, value: openarray[T]) =
proc writeArray*[T: byte|char](vb: var VBuffer, value: openarray[T]) =
## Write array ``value`` to buffer ``vb``, value will NOT be prefixed with
## varint length of the array.
var length = 0
if len(value) > 0:
vb.buffer.setLen(len(vb.buffer) + len(value))
copyMem(addr vb.buffer[vb.offset], unsafeAddr value[0], len(value))

View File

@ -1,6 +1,8 @@
import unittest
import ../libp2p/base32
when defined(nimHasUsed): {.used.}
const TVBaseUpperPadding = [
["f", "MY======"],
["fo", "MZXQ===="],

View File

@ -1,6 +1,8 @@
import unittest
import ../libp2p/base58
when defined(nimHasUsed): {.used.}
proc hexToBytes*(a: string, result: var openarray[byte]) =
doAssert(len(a) == 2 * len(result))
var i = 0

View File

@ -1,6 +1,8 @@
import unittest
import ../libp2p/base64
when defined(nimHasUsed): {.used.}
const TVBasePadding = [
["f", "Zg=="],
["fo", "Zm8="],

View File

@ -2,6 +2,8 @@ import unittest, deques, sequtils, strformat
import chronos
import ../libp2p/stream/bufferstream
when defined(nimHasUsed): {.used.}
suite "BufferStream":
test "push data to buffer":
proc testPushTo(): Future[bool] {.async.} =

View File

@ -1,6 +1,8 @@
import unittest
import ../libp2p/[cid, multihash, multicodec]
when defined(nimHasUsed): {.used.}
suite "Content identifier CID test suite":
test "CIDv0 test vector":
@ -13,7 +15,7 @@ suite "Content identifier CID test suite":
cid0.mhash().mcodec == multiCodec("sha2-256")
var res = 0
try:
var cidb0 = Cid.init("QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zIII")
discard Cid.init("QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zIII")
except CidError:
res = 1
check res == 1

View File

@ -13,6 +13,8 @@ import unittest
import nimcrypto/utils
import ../libp2p/crypto/crypto
when defined(nimHasUsed): {.used.}
const
PrivateKeys = [
"""080012BE023082013A020100024100AD8A7D5B0EB7C852C1464E4567651F412C

View File

@ -3,6 +3,8 @@ import chronos
import ../libp2p/daemon/daemonapi, ../libp2p/multiaddress, ../libp2p/multicodec,
../libp2p/cid, ../libp2p/multihash, ../libp2p/peer
when defined(nimHasUsed): {.used.}
proc identitySpawnTest(): Future[bool] {.async.} =
var api = await newDaemonApi()
var data = await api.identity()

View File

@ -10,6 +10,8 @@ import unittest
import nimcrypto/utils
import ../libp2p/crypto/ecnist
when defined(nimHasUsed): {.used.}
const
TestsCount = 10 # number of random tests
@ -392,7 +394,7 @@ suite "EC NIST-P256/384/521 test suite":
var sersk = kp.seckey.getBytes()
var serpk = kp.pubkey.getBytes()
var sersig = sig.getBytes()
var seckey = EcPrivateKey.init(sersk)
discard EcPrivateKey.init(sersk)
var pubkey = EcPublicKey.init(serpk)
var csig = EcSignature.init(sersig)
check csig.verify(message, pubkey) == true
@ -499,7 +501,7 @@ suite "EC NIST-P256/384/521 test suite":
var sersk = kp.seckey.getBytes()
var serpk = kp.pubkey.getBytes()
var sersig = sig.getBytes()
var seckey = EcPrivateKey.init(sersk)
discard EcPrivateKey.init(sersk)
var pubkey = EcPublicKey.init(serpk)
var csig = EcSignature.init(sersig)
check csig.verify(message, pubkey) == true
@ -606,7 +608,7 @@ suite "EC NIST-P256/384/521 test suite":
var sersk = kp.seckey.getBytes()
var serpk = kp.pubkey.getBytes()
var sersig = sig.getBytes()
var seckey = EcPrivateKey.init(sersk)
discard EcPrivateKey.init(sersk)
var pubkey = EcPublicKey.init(serpk)
var csig = EcSignature.init(sersig)
check csig.verify(message, pubkey) == true

View File

@ -12,6 +12,8 @@ import unittest
import nimcrypto/utils
import ../libp2p/crypto/ed25519/ed25519
when defined(nimHasUsed): {.used.}
const TestsCount = 20
const SecretKeys = [
@ -174,7 +176,7 @@ suite "Ed25519 test suite":
var sersk = kp.seckey.getBytes()
var serpk = kp.pubkey.getBytes()
var sersig = sig.getBytes()
var seckey = EdPrivateKey.init(sersk)
discard EdPrivateKey.init(sersk)
var pubkey = EdPublicKey.init(serpk)
var csig = EdSignature.init(sersig)
check csig.verify(message, pubkey) == true

View File

@ -11,6 +11,8 @@ import ../libp2p/[protocols/identify,
protocols/protocol,
crypto/crypto]
when defined(nimHasUsed): {.used.}
suite "Identify":
test "handle identify message":
proc testHandle(): Future[bool] {.async.} =

View File

@ -12,6 +12,8 @@ import ../libp2p/[connection,
muxers/mplex/types,
muxers/mplex/lpchannel]
when defined(nimHasUsed): {.used.}
suite "Mplex":
test "encode header with channel id 0":
proc testEncodeHeader(): Future[bool] {.async.} =

View File

@ -1,6 +1,8 @@
import unittest
import ../libp2p/multiaddress
when defined(nimHasUsed): {.used.}
type
PatternVector = object
pattern: MaPattern
@ -303,7 +305,7 @@ suite "MultiAddress test suite":
for item in FailureVectors:
var r = false
try:
var a = MultiAddress.init(item)
discard MultiAddress.init(item)
except:
r = true
check r == true
@ -319,7 +321,7 @@ suite "MultiAddress test suite":
for item in RustFailureVectors:
var r = false
try:
var a = MultiAddress.init(item)
discard MultiAddress.init(item)
except:
r = true
check r == true

View File

@ -1,6 +1,8 @@
import unittest
import ../libp2p/multibase
when defined(nimHasUsed): {.used.}
const GoTestVectors = [
[
"identity",
@ -292,12 +294,12 @@ suite "MultiBase test suite":
var r1 = false
var r2 = false
try:
var enc = MultiBase.encode("unknwon", data)
discard MultiBase.encode("unknwon", data)
except MultiBaseError:
r1 = true
try:
var dec = MultiBase.decode("\x01\x00")
discard MultiBase.decode("\x01\x00")
except MultiBaseError:
r2 = true

View File

@ -2,6 +2,8 @@ import unittest
import nimcrypto/utils
import ../libp2p/multihash
when defined(nimHasUsed): {.used.}
const
RustTestVectors = [
[

View File

@ -12,6 +12,8 @@ import ../libp2p/connection,
../libp2p/peerinfo,
../libp2p/peer
when defined(nimHasUsed): {.used.}
## Mock stream for select test
type
TestSelectStream = ref object of LPStream

View File

@ -13,6 +13,8 @@ import unittest
import nimcrypto/utils
import ../libp2p/crypto/crypto, ../libp2p/peer, ../libp2p/base58
when defined(nimHasUsed): {.used.}
const
# Test vectors are generated using
# https://github.com/libp2p/go-libp2p-crypto/blob/master/key.go

View File

@ -6,7 +6,6 @@
## at your option.
## This file may not be copied, modified, or distributed except according to
## those terms.
import unittest, options, tables, sugar, sequtils
import chronos, chronicles
import ../libp2p/[switch,
@ -28,6 +27,8 @@ import ../libp2p/[switch,
protocols/pubsub/pubsub,
protocols/pubsub/floodsub]
when defined(nimHasUsed): {.used.}
proc createMplex(conn: Connection): Muxer =
result = newMplex(conn)

View File

@ -10,6 +10,8 @@ import unittest
import nimcrypto/utils
import ../libp2p/crypto/rsa
when defined(nimHasUsed): {.used.}
const
TestsCount = 20 # number of random tests
@ -418,7 +420,7 @@ suite "RSA 512/1024/2048/4096 test suite":
var sersk = kp.seckey.getBytes()
var serpk = kp.pubkey.getBytes()
var sersig = sig.getBytes()
var seckey = RsaPrivateKey.init(sersk)
discard RsaPrivateKey.init(sersk)
var pubkey = RsaPublicKey.init(serpk)
var csig = RsaSignature.init(sersig)
check csig.verify(message, pubkey) == true
@ -434,7 +436,7 @@ suite "RSA 512/1024/2048/4096 test suite":
var sersk = kp.seckey.getBytes()
var serpk = kp.pubkey.getBytes()
var sersig = sig.getBytes()
var seckey = RsaPrivateKey.init(sersk)
discard RsaPrivateKey.init(sersk)
var pubkey = RsaPublicKey.init(serpk)
var csig = RsaSignature.init(sersig)
check csig.verify(message, pubkey) == true
@ -449,7 +451,7 @@ suite "RSA 512/1024/2048/4096 test suite":
var sersk = kp.seckey.getBytes()
var serpk = kp.pubkey.getBytes()
var sersig = sig.getBytes()
var seckey = RsaPrivateKey.init(sersk)
discard RsaPrivateKey.init(sersk)
var pubkey = RsaPublicKey.init(serpk)
var csig = RsaSignature.init(sersig)
check csig.verify(message, pubkey) == true
@ -465,7 +467,7 @@ suite "RSA 512/1024/2048/4096 test suite":
var sersk = kp.seckey.getBytes()
var serpk = kp.pubkey.getBytes()
var sersig = sig.getBytes()
var seckey = RsaPrivateKey.init(sersk)
discard RsaPrivateKey.init(sersk)
var pubkey = RsaPublicKey.init(serpk)
var csig = RsaSignature.init(sersig)
check csig.verify(message, pubkey) == true

View File

@ -10,6 +10,8 @@ import unittest
import ../libp2p/crypto/secp
import nimcrypto/utils
when defined(nimHasUsed): {.used.}
suite "Secp256k1 testing suite":
const TestsCount = 20
test "Private key serialize/deserialize test":
@ -62,7 +64,7 @@ suite "Secp256k1 testing suite":
var sersk = kp.seckey.getBytes()
var serpk = kp.pubkey.getBytes()
var sersig = sig.getBytes()
var seckey = SkPrivateKey.init(sersk)
discard SkPrivateKey.init(sersk)
var pubkey = SkPublicKey.init(serpk)
var csig = SkSignature.init(sersig)
check csig.verify(message, pubkey) == true

View File

@ -17,6 +17,8 @@ import ../libp2p/[switch,
protocols/secure/secio,
protocols/secure/secure]
when defined(nimHasUsed): {.used.}
const TestCodec = "/test/proto/1.0.0"
type

View File

@ -6,6 +6,8 @@ import ../libp2p/[connection,
multiaddress,
wire]
when defined(nimHasUsed): {.used.}
suite "TCP transport":
test "test listener: handle write":
proc testListener(): Future[bool] {.async, gcsafe.} =

View File

@ -1,6 +1,8 @@
import unittest
import ../libp2p/varint
when defined(nimHasUsed): {.used.}
const PBedgeValues = [
0'u64, (1'u64 shl 7) - 1'u64,
(1'u64 shl 7), (1'u64 shl 14) - 1'u64,