Finished with ECIES review and adopted tests.
This commit is contained in:
parent
1afd8b67e1
commit
f020ff4712
299
ethp2p/ecies.nim
299
ethp2p/ecies.nim
|
@ -21,10 +21,8 @@ type
|
|||
EciesStatus* = enum
|
||||
Success, ## Operation was successful
|
||||
BufferOverrun, ## Output buffer size is too small
|
||||
EmptyMessage, ## Attempt to encrypt/decrypt empty message
|
||||
RandomError, ## Could not obtain random data
|
||||
EcdhError, ## ECDH shared secret could not be calculated
|
||||
IncorrectSize, ## ECIES data has incorrect size (size is too low)
|
||||
WrongHeader, ## ECIES header is incorrect
|
||||
IncorrectKey, ## Recovered public key is invalid
|
||||
IncorrectTag, ## ECIES tag verification failed
|
||||
|
@ -72,11 +70,9 @@ proc kdf*(data: openarray[byte]): array[KeyLength, byte] {.noInit.} =
|
|||
var ctx: sha256
|
||||
var counter: uint32
|
||||
var counterLe: uint32
|
||||
let reps = ((KeyLength + 7) * 8) div (int(ctx.sizeDigest) * 8)
|
||||
let reps = ((KeyLength + 7) * 8) div (int(ctx.sizeBlock) * 8)
|
||||
var offset = 0
|
||||
# REVIEW: There is a relationship between KeyLength and sha256.sizeDigest here
|
||||
# that could be expressed in the code with a static assert.
|
||||
var storage = newSeq[byte](KeyLength * (reps + 1))
|
||||
var storage = newSeq[byte](int(ctx.sizeDigest) * (reps + 1))
|
||||
while counter <= uint32(reps):
|
||||
counter = counter + 1
|
||||
counterLe = LSWAP(counter)
|
||||
|
@ -91,14 +87,14 @@ proc kdf*(data: openarray[byte]): array[KeyLength, byte] {.noInit.} =
|
|||
|
||||
proc eciesEncrypt*(input: openarray[byte], output: var openarray[byte],
|
||||
pubkey: PublicKey,
|
||||
sharedmac: openarray[byte]): EciesStatus =
|
||||
sharedmac: openarray[byte] = emptyMac): EciesStatus =
|
||||
## Encrypt data with ECIES method using given public key `pubkey`.
|
||||
## ``input`` - input data
|
||||
## ``output`` - output data
|
||||
## ``pubkey`` - ECC public key
|
||||
## ``sharedmac`` - additional data used to calculate encrypted message MAC
|
||||
## Length of output data can be calculated using ``eciesEncryptedLength()``
|
||||
## macro.
|
||||
## template.
|
||||
var
|
||||
encKey: array[aes128.sizeKey, byte]
|
||||
cipher: CTR[aes128]
|
||||
|
@ -138,7 +134,7 @@ proc eciesEncrypt*(input: openarray[byte], output: var openarray[byte],
|
|||
cipher.clear()
|
||||
|
||||
so = eciesIvPos()
|
||||
eo = so + aes128.sizeBlock + len(input)
|
||||
eo = so + aes128.sizeBlock + len(input) - 1
|
||||
ctx.init(macKey.data)
|
||||
ctx.update(toOpenArray(output, so, eo))
|
||||
if len(sharedmac) > 0:
|
||||
|
@ -146,7 +142,8 @@ proc eciesEncrypt*(input: openarray[byte], output: var openarray[byte],
|
|||
var tag = ctx.finish()
|
||||
|
||||
so = eciesTagPos(len(input))
|
||||
copyMem(addr output[so], addr tag.data[0], ctx.sizeDigest)
|
||||
# ctx.sizeDigest() crash compiler
|
||||
copyMem(addr output[so], addr tag.data[0], sha256.sizeDigest)
|
||||
ctx.clear()
|
||||
|
||||
result = Success
|
||||
|
@ -154,14 +151,14 @@ proc eciesEncrypt*(input: openarray[byte], output: var openarray[byte],
|
|||
proc eciesDecrypt*(input: openarray[byte],
|
||||
output: var openarray[byte],
|
||||
seckey: PrivateKey,
|
||||
sharedmac: openarray[byte]): EciesStatus =
|
||||
sharedmac: openarray[byte] = emptyMac): EciesStatus =
|
||||
## Decrypt data with ECIES method using given private key `seckey`.
|
||||
## ``input`` - input data
|
||||
## ``output`` - output data
|
||||
## ``pubkey`` - ECC private key
|
||||
## ``sharedmac`` - additional data used to calculate encrypted message MAC
|
||||
## Length of output data can be calculated using ``eciesDecryptedLength()``
|
||||
## macro.
|
||||
## template.
|
||||
var
|
||||
pubkey: PublicKey
|
||||
encKey: array[aes128.sizeKey, byte]
|
||||
|
@ -194,7 +191,7 @@ proc eciesDecrypt*(input: openarray[byte],
|
|||
let datsize = eciesDecryptedLength(len(input))
|
||||
ctx.init(macKey.data)
|
||||
burnMem(macKey)
|
||||
ctx.update(toOpenArray(input, eciesIvPos(), eciesIvPos() + macsize))
|
||||
ctx.update(toOpenArray(input, eciesIvPos(), eciesIvPos() + macsize - 1))
|
||||
if len(sharedmac) > 0:
|
||||
ctx.update(sharedmac)
|
||||
var tag = ctx.finish()
|
||||
|
@ -206,279 +203,7 @@ proc eciesDecrypt*(input: openarray[byte],
|
|||
|
||||
cipher.init(encKey, header.iv)
|
||||
burnMem(encKey)
|
||||
cipher.decrypt(toOpenArray(input, eciesDataPos(), eciesDataPos() + datsize),
|
||||
output)
|
||||
cipher.decrypt(toOpenArray(input, eciesDataPos(),
|
||||
eciesDataPos() + datsize - 1), output)
|
||||
cipher.clear()
|
||||
result = Success
|
||||
|
||||
|
||||
# proc eciesEncrypt*(inp, oup: ptr byte, inl, oul: int, pubkey: PublicKey,
|
||||
# shmac: ptr byte = nil, shlen: int = 0): EciesStatus =
|
||||
# ## Encrypt data with ECIES method to the given public key `pubkey`.
|
||||
# ##
|
||||
# ## `inp` - [INPUT] pointer to input data
|
||||
# ## `oup` - [INPUT] pointer to output data
|
||||
# ## `inl` - [INPUT] input data size
|
||||
# ## `oul` - [INPUT] output data size
|
||||
# ## `pubkey` - [INPUT] Ecc secp256k1 public key
|
||||
# ## `shmac` - [INPUT] additional mac data
|
||||
# ## `shlen` - [INPUT] additional mac data size
|
||||
|
||||
# var
|
||||
# encKey: array[aes128.sizeKey, byte]
|
||||
# cipher: CTR[aes128]
|
||||
# ctx: HMAC[sha256]
|
||||
# iv: array[aes128.sizeBlock, byte]
|
||||
# secret: SharedSecret
|
||||
# material: array[KeyLength, byte]
|
||||
|
||||
# assert(not isNil(inp) and not isNil(oup))
|
||||
# assert(inl > 0 and oul > 0)
|
||||
|
||||
# if oul < eciesEncryptedLength(inl):
|
||||
# return(BufferOverrun)
|
||||
# if randomBytes(addr iv[0], len(iv)) != len(iv):
|
||||
# return(RandomError)
|
||||
|
||||
# var ephemeral = newKeyPair()
|
||||
# var output = cast[ptr UncheckedArray[byte]](oup)
|
||||
# var epub = ephemeral.pubkey.getRaw()
|
||||
|
||||
# if ecdhAgree(ephemeral.seckey, pubkey, secret) != EccStatus.Success:
|
||||
# return(EcdhError)
|
||||
|
||||
# material = kdf(secret)
|
||||
# burnMem(secret)
|
||||
|
||||
# copyMem(addr encKey[0], addr material[0], aes128.sizeKey)
|
||||
# var macKey = sha256.digest(material, ostart = KeyLength div 2)
|
||||
# burnMem(material)
|
||||
|
||||
# var header = cast[ptr EciesHeader](oup)
|
||||
# header.version = 0x04
|
||||
# header.pubkey = epub.data
|
||||
# header.iv = iv
|
||||
|
||||
# cipher.init(addr encKey[0], addr iv[0])
|
||||
# cipher.encrypt(inp, cast[ptr byte](addr header.data), uint(inl))
|
||||
# burnMem(encKey)
|
||||
# cipher.clear()
|
||||
|
||||
# ctx.init(cast[ptr byte](addr macKey.data[0]), uint(sha256.sizeDigest))
|
||||
# burnMem(macKey)
|
||||
# ctx.update(cast[ptr byte](addr header.iv), uint(eciesMacLength(inl)))
|
||||
# if not isNil(shmac) and shlen > 0:
|
||||
# ctx.update(shmac, uint(shlen))
|
||||
# var tag = ctx.finish()
|
||||
# ctx.clear()
|
||||
|
||||
# # echo dump(output, oul)
|
||||
|
||||
# let tagPos = cast[ptr byte](cast[uint](addr header.data) + uint(inl))
|
||||
# copyMem(tagPos, addr tag.data[0], sha256.sizeDigest)
|
||||
# result = Success
|
||||
|
||||
# proc eciesDecrypt*(inp, oup: ptr byte, inl, oul: int, seckey: PrivateKey,
|
||||
# shmac: ptr byte = nil, shlen: int = 0): EciesStatus =
|
||||
# ## Decrypt data with ECIES method using the given private key `seckey`.
|
||||
# ##
|
||||
# ## `inp` - [INPUT] pointer to input data
|
||||
# ## `oup` - [INPUT] pointer to output data
|
||||
# ## `inl` - [INPUT] input data size
|
||||
# ## `oul` - [INPUT] output data size
|
||||
# ## `seckey` - [INPUT] Ecc secp256k1 private key
|
||||
# ## `shmac` - [INPUT] additional mac data (default = nil)
|
||||
# ## `shlen` - [INPUT] additional mac data size (default = 0)
|
||||
|
||||
# var
|
||||
# pubkey: PublicKey
|
||||
# encKey: array[aes128.sizeKey, byte]
|
||||
# cipher: CTR[aes128]
|
||||
# ctx: HMAC[sha256]
|
||||
# secret: SharedSecret
|
||||
|
||||
# assert(not isNil(inp) and not isNil(oup))
|
||||
# assert(inl > 0 and oul > 0)
|
||||
|
||||
# var input = cast[ptr UncheckedArray[byte]](inp)
|
||||
# if inl <= eciesOverheadLength():
|
||||
# return(IncorrectSize)
|
||||
# if inl - eciesOverheadLength() > oul:
|
||||
# return(BufferOverrun)
|
||||
|
||||
# var header = cast[ptr EciesHeader](input)
|
||||
# if header.version != 0x04:
|
||||
# return(WrongHeader)
|
||||
|
||||
# if recoverPublicKey(addr input[1], KeyLength * 2,
|
||||
# pubkey) != EccStatus.Success:
|
||||
# return(IncorrectKey)
|
||||
|
||||
# if ecdhAgree(seckey, pubkey, secret) != EccStatus.Success:
|
||||
# return(EcdhError)
|
||||
|
||||
# var material = kdf(secret)
|
||||
# burnMem(secret)
|
||||
# copyMem(addr encKey[0], addr material[0], aes128.sizeKey)
|
||||
# var macKey = sha256.digest(material, ostart = KeyLength div 2)
|
||||
# burnMem(material)
|
||||
|
||||
# let macsize = eciesMacLength(inl - eciesOverheadLength())
|
||||
# ctx.init(addr macKey.data[0], uint(sha256.sizeDigest))
|
||||
# burnMem(macKey)
|
||||
# ctx.update(cast[ptr byte](addr header.iv), uint(macsize))
|
||||
# if not isNil(shmac) and shlen > 0:
|
||||
# ctx.update(shmac, uint(shlen))
|
||||
# var tag = ctx.finish()
|
||||
# ctx.clear()
|
||||
|
||||
# if not equalMem(addr tag.data[0], addr input[eciesMacPos(inl)],
|
||||
# sha256.sizeDigest):
|
||||
# return(IncorrectTag)
|
||||
|
||||
# cipher.init(addr encKey[0], cast[ptr byte](addr header.iv))
|
||||
# burnMem(encKey)
|
||||
# cipher.decrypt(cast[ptr byte](addr header.data),
|
||||
# cast[ptr byte](oup), uint(inl - eciesOverheadLength()))
|
||||
# cipher.clear()
|
||||
# result = Success
|
||||
|
||||
# proc eciesEncrypt*[A, B](input: openarray[A],
|
||||
# pubkey: PublicKey,
|
||||
# output: var openarray[B],
|
||||
# outlen: var int,
|
||||
# ostart: int = 0,
|
||||
# ofinish: int = -1): EciesStatus =
|
||||
# ## Encrypt data with ECIES method to the given public key `pubkey`.
|
||||
# ##
|
||||
# ## `input` - [INPUT] input data
|
||||
# ## `pubkey` - [INPUT] Ecc secp256k1 public key
|
||||
# ## `output` - [OUTPUT] output data
|
||||
# ## `outlen` - [OUTPUT] output data size
|
||||
# ## `ostart` - [INPUT] starting index in `data` (default = -1, start of input)
|
||||
# ## `ofinish` - [INPUT] ending index in `data` (default = -1, whole input)
|
||||
# ##
|
||||
# ## Encryption is done on `data` with inclusive range [ostart, ofinish]
|
||||
# ## Negative values of `ostart` and `ofinish` are treated as index with value
|
||||
# ## (len(data) + `ostart/ofinish`).
|
||||
|
||||
# let so = if ostart < 0: (len(input) + ostart) else: ostart
|
||||
# let eo = if ofinish < 0: (len(input) + ofinish) else: ofinish
|
||||
# let length = (eo - so + 1) * sizeof(A)
|
||||
# # We don't need to check `so` because compiler will do it for `data[so]`.
|
||||
# if eo >= len(input):
|
||||
# return(BufferOverrun)
|
||||
# if len(input) == 0:
|
||||
# return(EmptyMessage)
|
||||
# let esize = eciesEncryptedLength(length)
|
||||
# if (len(output) * sizeof(B)) < esize:
|
||||
# return(BufferOverrun)
|
||||
# outlen = esize
|
||||
# result = eciesEncrypt(cast[ptr byte](unsafeAddr input[so]), addr output[0],
|
||||
# length, esize, pubkey)
|
||||
|
||||
# proc eciesEncrypt*[A, B, C](input: openarray[A],
|
||||
# pubkey: PublicKey,
|
||||
# output: var openarray[B],
|
||||
# outlen: var int,
|
||||
# shmac: openarray[C],
|
||||
# ostart: int = 0,
|
||||
# ofinish: int = -1): EciesStatus =
|
||||
# ## Encrypt data with ECIES method to the given public key `pubkey`.
|
||||
# ##
|
||||
# ## `input` - [INPUT] input data
|
||||
# ## `pubkey` - [INPUT] Ecc secp256k1 public key
|
||||
# ## `output` - [OUTPUT] output data
|
||||
# ## `outlen` - [OUTPUT] output data size
|
||||
# ## `shmac` - [INPUT] additional mac data
|
||||
# ## `ostart` - [INPUT] starting index in `data` (default = -1, start of input)
|
||||
# ## `ofinish` - [INPUT] ending index in `data` (default = -1, whole input)
|
||||
# ##
|
||||
# ## Encryption is done on `data` with inclusive range [ostart, ofinish]
|
||||
# ## Negative values of `ostart` and `ofinish` are treated as index with value
|
||||
# ## (len(data) + `ostart/ofinish`).
|
||||
|
||||
# let so = if ostart < 0: (len(input) + ostart) else: ostart
|
||||
# let eo = if ofinish < 0: (len(input) + ofinish) else: ofinish
|
||||
# let length = (eo - so + 1) * sizeof(A)
|
||||
# # We don't need to check `so` because compiler will do it for `data[so]`.
|
||||
# if eo >= len(input):
|
||||
# return(BufferOverrun)
|
||||
# if len(input) == 0:
|
||||
# return(EmptyMessage)
|
||||
# let esize = eciesEncryptedLength(length)
|
||||
# if len(output) * sizeof(B) < esize:
|
||||
# return(BufferOverrun)
|
||||
# outlen = esize
|
||||
# result = eciesEncrypt(cast[ptr byte](unsafeAddr input[so]), addr output[0],
|
||||
# length, esize, pubkey,
|
||||
# cast[ptr byte](unsafeAddr shmac[0]),
|
||||
# len(shmac) * sizeof(C))
|
||||
|
||||
# proc eciesDecrypt*[A, B](input: openarray[A],
|
||||
# seckey: PrivateKey,
|
||||
# output: var openarray[B],
|
||||
# outlen: var int,
|
||||
# ostart: int = 0,
|
||||
# ofinish: int = -1): EciesStatus =
|
||||
# ## Decrypt data with ECIES method using given private key `seckey`.
|
||||
# ##
|
||||
# ## `input` - [INPUT] input data
|
||||
# ## `seckey` - [INPUT] Ecc secp256k1 private key
|
||||
# ## `output` - [OUTPUT] output data
|
||||
# ## `outlen` - [OUTPUT] output data size
|
||||
# ## `ostart` - [INPUT] starting index in `data` (default = -1, start of input)
|
||||
# ## `ofinish` - [INPUT] ending index in `data` (default = -1, whole input)
|
||||
# ##
|
||||
# ## Decryption is done on `data` with inclusive range [ostart, ofinish]
|
||||
|
||||
# let so = if ostart < 0: (len(input) + ostart) else: ostart
|
||||
# let eo = if ofinish < 0: (len(input) + ofinish) else: ofinish
|
||||
# let length = (eo - so + 1) * sizeof(A)
|
||||
# # We don't need to check `so` because compiler will do it for `data[so]`.
|
||||
# if eo >= len(input):
|
||||
# return(BufferOverrun)
|
||||
# if len(input) == 0:
|
||||
# return(EmptyMessage)
|
||||
# let dsize = eciesDecryptedLength(length)
|
||||
# if len(output) * sizeof(B) < dsize:
|
||||
# return(BufferOverrun)
|
||||
# outlen = dsize
|
||||
# result = eciesDecrypt(cast[ptr byte](unsafeAddr input[so]), addr output[0],
|
||||
# length, dsize, seckey)
|
||||
|
||||
# proc eciesDecrypt*[A, B, C](input: openarray[A],
|
||||
# seckey: PrivateKey,
|
||||
# output: var openarray[B],
|
||||
# outlen: var int,
|
||||
# shmac: openarray[C],
|
||||
# ostart: int = 0,
|
||||
# ofinish: int = -1): EciesStatus =
|
||||
# ## Decrypt data with ECIES method using given private key `seckey`.
|
||||
# ##
|
||||
# ## `input` - [INPUT] input data
|
||||
# ## `seckey` - [INPUT] Ecc secp256k1 private key
|
||||
# ## `output` - [OUTPUT] output data
|
||||
# ## `outlen` - [OUTPUT] output data size
|
||||
# ## `shmac` - additional mac data
|
||||
# ## `ostart` - starting index in `data` (default = -1, data[0])
|
||||
# ## `ofinish` - ending index in `data` (default = -1, data[len(data) - 1])
|
||||
# ##
|
||||
# ## Decryption is done on `data` with inclusive range [ostart, ofinish]
|
||||
|
||||
# let so = if ostart < 0: (len(input) + ostart) else: ostart
|
||||
# let eo = if ofinish < 0: (len(input) + ofinish) else: ofinish
|
||||
# let length = (eo - so + 1) * sizeof(A)
|
||||
# # We don't need to check `so` because compiler will do it for `data[so]`.
|
||||
# if eo >= len(input):
|
||||
# return(BufferOverrun)
|
||||
# if len(input) == 0:
|
||||
# return(EmptyMessage)
|
||||
# let dsize = eciesDecryptedLength(length)
|
||||
# if len(output) * sizeof(B) < dsize:
|
||||
# return(BufferOverrun)
|
||||
# outlen = dsize
|
||||
# result = eciesDecrypt(cast[ptr byte](unsafeAddr input[so]), addr output[0],
|
||||
# length, dsize, seckey,
|
||||
# cast[ptr byte](unsafeAddr shmac[0]),
|
||||
# len(shmac) * sizeof(C))
|
||||
|
|
|
@ -57,109 +57,103 @@ suite "ECIES test suite":
|
|||
var encr = newSeq[byte](eciesEncryptedLength(len(m)))
|
||||
var decr = newSeq[byte](len(m))
|
||||
var shmac = [0x13'u8, 0x13'u8]
|
||||
var outlen = 0
|
||||
var s = newPrivateKey()
|
||||
var p = s.getPublicKey()
|
||||
check:
|
||||
# Without additional mac data
|
||||
eciesEncrypt(plain, encr, p, [0'u8]) == EciesStatus.Success
|
||||
eciesDecrypt(encr, decr, s, [0'u8]) == EciesStatus.Success
|
||||
equalMem(addr m[0], addr decr[0], outlen) == true
|
||||
# # With additional mac data
|
||||
# eciesEncrypt(m, p, encr, outlen, shmac) == EciesStatus.Success
|
||||
# eciesDecrypt(encr, s, decr, outlen, shmac) == EciesStatus.Success
|
||||
# outlen == len(m)
|
||||
# equalMem(addr m[0], addr decr[0], outlen) == true
|
||||
eciesEncrypt(plain, encr, p) == EciesStatus.Success
|
||||
eciesDecrypt(encr, decr, s) == EciesStatus.Success
|
||||
equalMem(addr m[0], addr decr[0], len(m))
|
||||
# With additional mac data
|
||||
eciesEncrypt(plain, encr, p, shmac) == EciesStatus.Success
|
||||
eciesDecrypt(encr, decr, s, shmac) == EciesStatus.Success
|
||||
equalMem(addr m[0], addr decr[0], len(m))
|
||||
|
||||
# test "ECIES/py-evm/cpp-ethereum test_ecies.py#L43/rlpx.cpp#L187":
|
||||
# # ECIES
|
||||
# # https://github.com/ethereum/py-evm/blob/master/tests/p2p/test_ecies.py#L43
|
||||
# # https://github.com/ethereum/cpp-ethereum/blob/develop/test/unittests/libp2p/rlpx.cpp#L187
|
||||
# const secretKeys = [
|
||||
# "c45f950382d542169ea207959ee0220ec1491755abe405cd7498d6b16adb6df8",
|
||||
# "5e173f6ac3c669587538e7727cf19b782a4f2fda07c1eaa662c593e5e85e3051"
|
||||
# ]
|
||||
# const cipherText = [
|
||||
# """04a0274c5951e32132e7f088c9bdfdc76c9d91f0dc6078e848f8e3361193dbdc
|
||||
# 43b94351ea3d89e4ff33ddcefbc80070498824857f499656c4f79bbd97b6c51a
|
||||
# 514251d69fd1785ef8764bd1d262a883f780964cce6a14ff206daf1206aa073a
|
||||
# 2d35ce2697ebf3514225bef186631b2fd2316a4b7bcdefec8d75a1025ba2c540
|
||||
# 4a34e7795e1dd4bc01c6113ece07b0df13b69d3ba654a36e35e69ff9d482d88d
|
||||
# 2f0228e7d96fe11dccbb465a1831c7d4ad3a026924b182fc2bdfe016a6944312
|
||||
# 021da5cc459713b13b86a686cf34d6fe6615020e4acf26bf0d5b7579ba813e77
|
||||
# 23eb95b3cef9942f01a58bd61baee7c9bdd438956b426a4ffe238e61746a8c93
|
||||
# d5e10680617c82e48d706ac4953f5e1c4c4f7d013c87d34a06626f498f34576d
|
||||
# c017fdd3d581e83cfd26cf125b6d2bda1f1d56""",
|
||||
# """049934a7b2d7f9af8fd9db941d9da281ac9381b5740e1f64f7092f3588d4f87f
|
||||
# 5ce55191a6653e5e80c1c5dd538169aa123e70dc6ffc5af1827e546c0e958e42
|
||||
# dad355bcc1fcb9cdf2cf47ff524d2ad98cbf275e661bf4cf00960e74b5956b79
|
||||
# 9771334f426df007350b46049adb21a6e78ab1408d5e6ccde6fb5e69f0f4c92b
|
||||
# b9c725c02f99fa72b9cdc8dd53cff089e0e73317f61cc5abf6152513cb7d833f
|
||||
# 09d2851603919bf0fbe44d79a09245c6e8338eb502083dc84b846f2fee1cc310
|
||||
# d2cc8b1b9334728f97220bb799376233e113"""
|
||||
# ]
|
||||
# const expectText = [
|
||||
# """884c36f7ae6b406637c1f61b2f57e1d2cab813d24c6559aaf843c3f48962f32f
|
||||
# 46662c066d39669b7b2e3ba14781477417600e7728399278b1b5d801a519aa57
|
||||
# 0034fdb5419558137e0d44cd13d319afe5629eeccb47fd9dfe55cc6089426e46
|
||||
# cc762dd8a0636e07a54b31169eba0c7a20a1ac1ef68596f1f283b5c676bae406
|
||||
# 4abfcce24799d09f67e392632d3ffdc12e3d6430dcb0ea19c318343ffa7aae74
|
||||
# d4cd26fecb93657d1cd9e9eaf4f8be720b56dd1d39f190c4e1c6b7ec66f077bb
|
||||
# 1100""",
|
||||
# """802b052f8b066640bba94a4fc39d63815c377fced6fcb84d27f791c9921ddf3e
|
||||
# 9bf0108e298f490812847109cbd778fae393e80323fd643209841a3b7f110397
|
||||
# f37ec61d84cea03dcc5e8385db93248584e8af4b4d1c832d8c7453c0089687a7
|
||||
# 00"""
|
||||
# ]
|
||||
# var data: array[1024, byte]
|
||||
# var outlen = 0
|
||||
# for i in 0..1:
|
||||
# var s = secretKeys[i].getPrivateKey()
|
||||
# var cipher = fromHex(stripSpaces(cipherText[i]))
|
||||
# var expect = fromHex(stripSpaces(expectText[i]))
|
||||
# check:
|
||||
# eciesDecrypt(cipher, s, data, outlen) == EciesStatus.Success
|
||||
# outlen == len(expect)
|
||||
# compare(data, expect) == true
|
||||
test "ECIES/py-evm/cpp-ethereum test_ecies.py#L43/rlpx.cpp#L187":
|
||||
# ECIES
|
||||
# https://github.com/ethereum/py-evm/blob/master/tests/p2p/test_ecies.py#L43
|
||||
# https://github.com/ethereum/cpp-ethereum/blob/develop/test/unittests/libp2p/rlpx.cpp#L187
|
||||
const secretKeys = [
|
||||
"c45f950382d542169ea207959ee0220ec1491755abe405cd7498d6b16adb6df8",
|
||||
"5e173f6ac3c669587538e7727cf19b782a4f2fda07c1eaa662c593e5e85e3051"
|
||||
]
|
||||
const cipherText = [
|
||||
"""04a0274c5951e32132e7f088c9bdfdc76c9d91f0dc6078e848f8e3361193dbdc
|
||||
43b94351ea3d89e4ff33ddcefbc80070498824857f499656c4f79bbd97b6c51a
|
||||
514251d69fd1785ef8764bd1d262a883f780964cce6a14ff206daf1206aa073a
|
||||
2d35ce2697ebf3514225bef186631b2fd2316a4b7bcdefec8d75a1025ba2c540
|
||||
4a34e7795e1dd4bc01c6113ece07b0df13b69d3ba654a36e35e69ff9d482d88d
|
||||
2f0228e7d96fe11dccbb465a1831c7d4ad3a026924b182fc2bdfe016a6944312
|
||||
021da5cc459713b13b86a686cf34d6fe6615020e4acf26bf0d5b7579ba813e77
|
||||
23eb95b3cef9942f01a58bd61baee7c9bdd438956b426a4ffe238e61746a8c93
|
||||
d5e10680617c82e48d706ac4953f5e1c4c4f7d013c87d34a06626f498f34576d
|
||||
c017fdd3d581e83cfd26cf125b6d2bda1f1d56""",
|
||||
"""049934a7b2d7f9af8fd9db941d9da281ac9381b5740e1f64f7092f3588d4f87f
|
||||
5ce55191a6653e5e80c1c5dd538169aa123e70dc6ffc5af1827e546c0e958e42
|
||||
dad355bcc1fcb9cdf2cf47ff524d2ad98cbf275e661bf4cf00960e74b5956b79
|
||||
9771334f426df007350b46049adb21a6e78ab1408d5e6ccde6fb5e69f0f4c92b
|
||||
b9c725c02f99fa72b9cdc8dd53cff089e0e73317f61cc5abf6152513cb7d833f
|
||||
09d2851603919bf0fbe44d79a09245c6e8338eb502083dc84b846f2fee1cc310
|
||||
d2cc8b1b9334728f97220bb799376233e113"""
|
||||
]
|
||||
const expectText = [
|
||||
"""884c36f7ae6b406637c1f61b2f57e1d2cab813d24c6559aaf843c3f48962f32f
|
||||
46662c066d39669b7b2e3ba14781477417600e7728399278b1b5d801a519aa57
|
||||
0034fdb5419558137e0d44cd13d319afe5629eeccb47fd9dfe55cc6089426e46
|
||||
cc762dd8a0636e07a54b31169eba0c7a20a1ac1ef68596f1f283b5c676bae406
|
||||
4abfcce24799d09f67e392632d3ffdc12e3d6430dcb0ea19c318343ffa7aae74
|
||||
d4cd26fecb93657d1cd9e9eaf4f8be720b56dd1d39f190c4e1c6b7ec66f077bb
|
||||
1100""",
|
||||
"""802b052f8b066640bba94a4fc39d63815c377fced6fcb84d27f791c9921ddf3e
|
||||
9bf0108e298f490812847109cbd778fae393e80323fd643209841a3b7f110397
|
||||
f37ec61d84cea03dcc5e8385db93248584e8af4b4d1c832d8c7453c0089687a7
|
||||
00"""
|
||||
]
|
||||
var data: array[1024, byte]
|
||||
for i in 0..1:
|
||||
var s = secretKeys[i].getPrivateKey()
|
||||
var cipher = fromHex(stripSpaces(cipherText[i]))
|
||||
var expect = fromHex(stripSpaces(expectText[i]))
|
||||
check:
|
||||
eciesDecrypt(cipher, data, s) == EciesStatus.Success
|
||||
compare(data, expect) == true
|
||||
|
||||
# test "ECIES/cpp-ethereum rlpx.cpp#L432-L459":
|
||||
# # ECIES
|
||||
# # https://github.com/ethereum/cpp-ethereum/blob/develop/test/unittests/libp2p/rlpx.cpp#L432-L459
|
||||
# const secretKeys = [
|
||||
# "57baf2c62005ddec64c357d96183ebc90bf9100583280e848aa31d683cad73cb",
|
||||
# "472413e97f1fd58d84e28a559479e6b6902d2e8a0cee672ef38a3a35d263886b",
|
||||
# "472413e97f1fd58d84e28a559479e6b6902d2e8a0cee672ef38a3a35d263886b",
|
||||
# "472413e97f1fd58d84e28a559479e6b6902d2e8a0cee672ef38a3a35d263886b"
|
||||
# ]
|
||||
# const cipherData = [
|
||||
# """04ff2c874d0a47917c84eea0b2a4141ca95233720b5c70f81a8415bae1dc7b74
|
||||
# 6b61df7558811c1d6054333907333ef9bb0cc2fbf8b34abb9730d14e0140f455
|
||||
# 3f4b15d705120af46cf653a1dc5b95b312cf8444714f95a4f7a0425b67fc064d
|
||||
# 18f4d0a528761565ca02d97faffdac23de10""",
|
||||
# """046f647e1bd8a5cd1446d31513bac233e18bdc28ec0e59d46de453137a725995
|
||||
# 33f1e97c98154343420d5f16e171e5107999a7c7f1a6e26f57bcb0d2280655d0
|
||||
# 8fb148d36f1d4b28642d3bb4a136f0e33e3dd2e3cffe4b45a03fb7c5b5ea5e65
|
||||
# 617250fdc89e1a315563c20504b9d3a72555""",
|
||||
# """0443c24d6ccef3ad095140760bb143078b3880557a06392f17c5e368502d7953
|
||||
# 2bc18903d59ced4bbe858e870610ab0d5f8b7963dd5c9c4cf81128d10efd7c7a
|
||||
# a80091563c273e996578403694673581829e25a865191bdc9954db14285b56eb
|
||||
# 0043b6288172e0d003c10f42fe413222e273d1d4340c38a2d8344d7aadcbc846
|
||||
# ee""",
|
||||
# """04c4e40c86bb5324e017e598c6d48c19362ae527af8ab21b077284a4656c8735
|
||||
# e62d73fb3d740acefbec30ca4c024739a1fcdff69ecaf03301eebf156eb5f17c
|
||||
# ca6f9d7a7e214a1f3f6e34d1ee0ec00ce0ef7d2b242fbfec0f276e17941f9f1b
|
||||
# fbe26de10a15a6fac3cda039904ddd1d7e06e7b96b4878f61860e47f0b84c8ce
|
||||
# b64f6a900ff23844f4359ae49b44154980a626d3c73226c19e"""
|
||||
# ]
|
||||
# const expectData = [
|
||||
# "a", "a", "aaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
||||
# ]
|
||||
# var data: array[1024, byte]
|
||||
# var outlen = 0
|
||||
# for i in 0..3:
|
||||
# var s = secretKeys[i].getPrivateKey()
|
||||
# var cipher = fromHex(stripSpaces(cipherData[i]))
|
||||
# check:
|
||||
# eciesDecrypt(cipher, s, data, outlen) == EciesStatus.Success
|
||||
# outlen == len(expectData[i])
|
||||
# compare(data, expectData[i]) == true
|
||||
test "ECIES/cpp-ethereum rlpx.cpp#L432-L459":
|
||||
# ECIES
|
||||
# https://github.com/ethereum/cpp-ethereum/blob/develop/test/unittests/libp2p/rlpx.cpp#L432-L459
|
||||
const secretKeys = [
|
||||
"57baf2c62005ddec64c357d96183ebc90bf9100583280e848aa31d683cad73cb",
|
||||
"472413e97f1fd58d84e28a559479e6b6902d2e8a0cee672ef38a3a35d263886b",
|
||||
"472413e97f1fd58d84e28a559479e6b6902d2e8a0cee672ef38a3a35d263886b",
|
||||
"472413e97f1fd58d84e28a559479e6b6902d2e8a0cee672ef38a3a35d263886b"
|
||||
]
|
||||
const cipherData = [
|
||||
"""04ff2c874d0a47917c84eea0b2a4141ca95233720b5c70f81a8415bae1dc7b74
|
||||
6b61df7558811c1d6054333907333ef9bb0cc2fbf8b34abb9730d14e0140f455
|
||||
3f4b15d705120af46cf653a1dc5b95b312cf8444714f95a4f7a0425b67fc064d
|
||||
18f4d0a528761565ca02d97faffdac23de10""",
|
||||
"""046f647e1bd8a5cd1446d31513bac233e18bdc28ec0e59d46de453137a725995
|
||||
33f1e97c98154343420d5f16e171e5107999a7c7f1a6e26f57bcb0d2280655d0
|
||||
8fb148d36f1d4b28642d3bb4a136f0e33e3dd2e3cffe4b45a03fb7c5b5ea5e65
|
||||
617250fdc89e1a315563c20504b9d3a72555""",
|
||||
"""0443c24d6ccef3ad095140760bb143078b3880557a06392f17c5e368502d7953
|
||||
2bc18903d59ced4bbe858e870610ab0d5f8b7963dd5c9c4cf81128d10efd7c7a
|
||||
a80091563c273e996578403694673581829e25a865191bdc9954db14285b56eb
|
||||
0043b6288172e0d003c10f42fe413222e273d1d4340c38a2d8344d7aadcbc846
|
||||
ee""",
|
||||
"""04c4e40c86bb5324e017e598c6d48c19362ae527af8ab21b077284a4656c8735
|
||||
e62d73fb3d740acefbec30ca4c024739a1fcdff69ecaf03301eebf156eb5f17c
|
||||
ca6f9d7a7e214a1f3f6e34d1ee0ec00ce0ef7d2b242fbfec0f276e17941f9f1b
|
||||
fbe26de10a15a6fac3cda039904ddd1d7e06e7b96b4878f61860e47f0b84c8ce
|
||||
b64f6a900ff23844f4359ae49b44154980a626d3c73226c19e"""
|
||||
]
|
||||
const expectData = [
|
||||
"a", "a", "aaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
||||
]
|
||||
var data: array[1024, byte]
|
||||
for i in 0..3:
|
||||
var s = secretKeys[i].getPrivateKey()
|
||||
var cipher = fromHex(stripSpaces(cipherData[i]))
|
||||
check:
|
||||
eciesDecrypt(cipher, data, s) == EciesStatus.Success
|
||||
compare(data, expectData[i]) == true
|
||||
|
|
Loading…
Reference in New Issue