2019-02-05 15:40:29 +00:00
|
|
|
#
|
|
|
|
# Ethereum P2P
|
|
|
|
# (c) Copyright 2018
|
|
|
|
# Status Research & Development GmbH
|
|
|
|
#
|
|
|
|
# Licensed under either of
|
|
|
|
# Apache License, version 2.0, (LICENSE-APACHEv2)
|
|
|
|
# MIT license (LICENSE-MIT)
|
|
|
|
#
|
|
|
|
|
|
|
|
## This module implements Ethereum authentication
|
|
|
|
|
2020-04-06 16:24:15 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2021-04-06 11:33:24 +00:00
|
|
|
import
|
2022-06-17 20:45:37 +00:00
|
|
|
nimcrypto/[rijndael, keccak, utils],
|
|
|
|
stew/[arrayops, byteutils, endians2, objects, results],
|
2021-04-06 11:33:24 +00:00
|
|
|
".."/[keys, rlp],
|
|
|
|
./ecies
|
2020-04-06 16:24:15 +00:00
|
|
|
|
2020-04-07 09:56:25 +00:00
|
|
|
export results
|
2019-02-05 15:40:29 +00:00
|
|
|
|
|
|
|
const
|
2020-04-06 16:24:15 +00:00
|
|
|
SupportedRlpxVersion* = 4'u8
|
2019-02-05 15:40:29 +00:00
|
|
|
PlainAuthMessageV4Length* = 194
|
|
|
|
AuthMessageV4Length* = 307
|
|
|
|
PlainAuthMessageEIP8Length = 169
|
|
|
|
PlainAuthMessageMaxEIP8* = PlainAuthMessageEIP8Length + 255
|
|
|
|
AuthMessageEIP8Length* = 282 + 2
|
|
|
|
AuthMessageMaxEIP8* = AuthMessageEIP8Length + 255
|
|
|
|
PlainAckMessageV4Length* = 97
|
|
|
|
AckMessageV4Length* = 210
|
|
|
|
PlainAckMessageEIP8Length* = 102
|
|
|
|
PlainAckMessageMaxEIP8* = PlainAckMessageEIP8Length + 255
|
|
|
|
AckMessageEIP8Length* = 215 + 2
|
|
|
|
AckMessageMaxEIP8* = AckMessageEIP8Length + 255
|
|
|
|
|
|
|
|
type
|
|
|
|
Nonce* = array[KeyLength, byte]
|
|
|
|
|
2020-04-04 16:44:01 +00:00
|
|
|
AuthMessageV4* {.packed.} = object
|
2019-02-05 15:40:29 +00:00
|
|
|
signature: array[RawSignatureSize, byte]
|
|
|
|
keyhash: array[keccak256.sizeDigest, byte]
|
2020-04-04 16:44:01 +00:00
|
|
|
pubkey: array[RawPublicKeySize, byte]
|
2019-02-05 15:40:29 +00:00
|
|
|
nonce: array[keccak256.sizeDigest, byte]
|
|
|
|
flag: byte
|
|
|
|
|
2020-04-04 16:44:01 +00:00
|
|
|
AckMessageV4* {.packed.} = object
|
2019-02-05 15:40:29 +00:00
|
|
|
pubkey: array[RawPublicKeySize, byte]
|
|
|
|
nonce: array[keccak256.sizeDigest, byte]
|
|
|
|
flag: byte
|
|
|
|
|
|
|
|
HandshakeFlag* = enum
|
|
|
|
Initiator, ## `Handshake` owner is connection initiator
|
|
|
|
Responder, ## `Handshake` owner is connection responder
|
2021-12-20 12:14:50 +00:00
|
|
|
EIP8 ## Flag indicates that EIP-8 handshake is used
|
2019-02-05 15:40:29 +00:00
|
|
|
|
2020-04-06 16:24:15 +00:00
|
|
|
AuthError* = enum
|
|
|
|
EcdhError = "auth: ECDH shared secret could not be calculated"
|
|
|
|
BufferOverrun = "auth: buffer overrun"
|
|
|
|
SignatureError = "auth: signature could not be obtained"
|
|
|
|
EciesError = "auth: ECIES encryption/decryption error"
|
|
|
|
InvalidPubKey = "auth: invalid public key"
|
|
|
|
InvalidAuth = "auth: invalid Authentication message"
|
|
|
|
InvalidAck = "auth: invalid Authentication ACK message"
|
|
|
|
RlpError = "auth: error while decoding RLP stream"
|
|
|
|
IncompleteError = "auth: data incomplete"
|
2019-02-05 15:40:29 +00:00
|
|
|
|
|
|
|
Handshake* = object
|
|
|
|
version*: uint8 ## protocol version
|
|
|
|
flags*: set[HandshakeFlag] ## handshake flags
|
|
|
|
host*: KeyPair ## host keypair
|
|
|
|
ephemeral*: KeyPair ## ephemeral host keypair
|
|
|
|
remoteHPubkey*: PublicKey ## remote host public key
|
|
|
|
remoteEPubkey*: PublicKey ## remote host ephemeral public key
|
|
|
|
initiatorNonce*: Nonce ## initiator nonce
|
|
|
|
responderNonce*: Nonce ## responder nonce
|
|
|
|
expectedLength*: int ## expected incoming message length
|
|
|
|
|
|
|
|
ConnectionSecret* = object
|
|
|
|
aesKey*: array[aes256.sizeKey, byte]
|
|
|
|
macKey*: array[KeyLength, byte]
|
|
|
|
egressMac*: keccak256
|
|
|
|
ingressMac*: keccak256
|
|
|
|
|
2020-04-06 16:24:15 +00:00
|
|
|
AuthResult*[T] = Result[T, AuthError]
|
2019-02-05 15:40:29 +00:00
|
|
|
|
|
|
|
template toa(a, b, c: untyped): untyped =
|
|
|
|
toOpenArray((a), (b), (b) + (c) - 1)
|
|
|
|
|
2020-04-06 16:24:15 +00:00
|
|
|
proc mapErrTo[T, E](r: Result[T, E], v: static AuthError): AuthResult[T] =
|
|
|
|
r.mapErr(proc (e: E): AuthError = v)
|
2019-02-05 15:40:29 +00:00
|
|
|
|
2022-04-04 20:31:09 +00:00
|
|
|
proc init*(
|
2022-06-17 20:45:37 +00:00
|
|
|
T: type Handshake, rng: var HmacDrbgContext, host: KeyPair,
|
2020-07-07 08:56:26 +00:00
|
|
|
flags: set[HandshakeFlag] = {Initiator},
|
2022-04-04 20:31:09 +00:00
|
|
|
version: uint8 = SupportedRlpxVersion): T =
|
2019-02-05 15:40:29 +00:00
|
|
|
## Create new `Handshake` object.
|
2020-04-06 16:24:15 +00:00
|
|
|
var
|
|
|
|
initiatorNonce: Nonce
|
|
|
|
responderNonce: Nonce
|
|
|
|
expectedLength: int
|
2020-07-07 08:56:26 +00:00
|
|
|
ephemeral = KeyPair.random(rng)
|
2020-04-06 16:24:15 +00:00
|
|
|
|
2019-02-05 15:40:29 +00:00
|
|
|
if Initiator in flags:
|
2020-04-06 16:24:15 +00:00
|
|
|
expectedLength = AckMessageV4Length
|
2022-06-17 20:45:37 +00:00
|
|
|
rng.generate(initiatorNonce)
|
2019-02-05 15:40:29 +00:00
|
|
|
else:
|
2020-04-06 16:24:15 +00:00
|
|
|
expectedLength = AuthMessageV4Length
|
2022-06-17 20:45:37 +00:00
|
|
|
rng.generate(responderNonce)
|
2020-04-06 16:24:15 +00:00
|
|
|
|
2022-04-04 20:31:09 +00:00
|
|
|
return T(
|
2020-04-06 16:24:15 +00:00
|
|
|
version: version,
|
|
|
|
flags: flags,
|
|
|
|
host: host,
|
|
|
|
ephemeral: ephemeral,
|
|
|
|
initiatorNonce: initiatorNonce,
|
|
|
|
responderNonce: responderNonce,
|
|
|
|
expectedLength: expectedLength
|
2022-04-04 20:31:09 +00:00
|
|
|
)
|
2019-02-05 15:40:29 +00:00
|
|
|
|
|
|
|
proc authMessagePreEIP8(h: var Handshake,
|
2022-06-17 20:45:37 +00:00
|
|
|
rng: var HmacDrbgContext,
|
2019-02-05 15:40:29 +00:00
|
|
|
pubkey: PublicKey,
|
2021-12-20 12:14:50 +00:00
|
|
|
output: var openArray[byte],
|
2019-02-05 15:40:29 +00:00
|
|
|
outlen: var int,
|
2020-04-06 16:24:15 +00:00
|
|
|
flag: byte = 0,
|
|
|
|
encrypt: bool = true): AuthResult[void] =
|
2019-02-05 15:40:29 +00:00
|
|
|
## Create plain pre-EIP8 authentication message.
|
|
|
|
var
|
|
|
|
buffer: array[PlainAuthMessageV4Length, byte]
|
|
|
|
outlen = 0
|
2020-04-06 16:24:15 +00:00
|
|
|
let header = cast[ptr AuthMessageV4](addr buffer[0])
|
|
|
|
|
2020-06-22 16:07:48 +00:00
|
|
|
var secret = ecdhRaw(h.host.seckey, pubkey)
|
2020-07-07 08:56:26 +00:00
|
|
|
secret.data = secret.data xor h.initiatorNonce
|
2020-04-06 16:24:15 +00:00
|
|
|
|
2020-07-07 08:56:26 +00:00
|
|
|
let signature = sign(h.ephemeral.seckey, SkMessage(secret.data))
|
2020-04-06 16:24:15 +00:00
|
|
|
secret.clear()
|
|
|
|
|
2019-02-05 15:40:29 +00:00
|
|
|
h.remoteHPubkey = pubkey
|
2020-04-06 16:24:15 +00:00
|
|
|
header.signature = signature.toRaw()
|
2020-04-04 16:44:01 +00:00
|
|
|
header.keyhash = keccak256.digest(h.ephemeral.pubkey.toRaw()).data
|
|
|
|
header.pubkey = h.host.pubkey.toRaw()
|
2019-02-05 15:40:29 +00:00
|
|
|
header.nonce = h.initiatorNonce
|
2020-04-06 16:24:15 +00:00
|
|
|
header.flag = flag
|
2019-02-05 15:40:29 +00:00
|
|
|
if encrypt:
|
|
|
|
if len(output) < AuthMessageV4Length:
|
2020-04-06 16:24:15 +00:00
|
|
|
return err(BufferOverrun)
|
2020-07-07 08:56:26 +00:00
|
|
|
if eciesEncrypt(rng, buffer, output, h.remoteHPubkey).isErr:
|
2020-04-06 16:24:15 +00:00
|
|
|
return err(EciesError)
|
2019-02-05 15:40:29 +00:00
|
|
|
outlen = AuthMessageV4Length
|
|
|
|
else:
|
|
|
|
if len(output) < PlainAuthMessageV4Length:
|
2020-04-06 16:24:15 +00:00
|
|
|
return err(BufferOverrun)
|
2019-02-05 15:40:29 +00:00
|
|
|
copyMem(addr output[0], addr buffer[0], PlainAuthMessageV4Length)
|
|
|
|
outlen = PlainAuthMessageV4Length
|
2020-04-06 16:24:15 +00:00
|
|
|
|
|
|
|
ok()
|
2019-02-05 15:40:29 +00:00
|
|
|
|
|
|
|
proc authMessageEIP8(h: var Handshake,
|
2022-06-17 20:45:37 +00:00
|
|
|
rng: var HmacDrbgContext,
|
2019-02-05 15:40:29 +00:00
|
|
|
pubkey: PublicKey,
|
2021-12-20 12:14:50 +00:00
|
|
|
output: var openArray[byte],
|
2019-02-05 15:40:29 +00:00
|
|
|
outlen: var int,
|
2020-04-06 16:24:15 +00:00
|
|
|
flag: byte = 0,
|
|
|
|
encrypt: bool = true): AuthResult[void] =
|
2019-02-05 15:40:29 +00:00
|
|
|
## Create EIP8 authentication message.
|
|
|
|
var
|
|
|
|
buffer: array[PlainAuthMessageMaxEIP8, byte]
|
|
|
|
|
2019-03-13 22:15:26 +00:00
|
|
|
doAssert(EIP8 in h.flags)
|
2019-02-05 15:40:29 +00:00
|
|
|
outlen = 0
|
2020-04-06 16:24:15 +00:00
|
|
|
|
2020-07-07 08:56:26 +00:00
|
|
|
var secret = ecdhRaw(h.host.seckey, pubkey)
|
|
|
|
secret.data = secret.data xor h.initiatorNonce
|
2020-04-06 16:24:15 +00:00
|
|
|
|
2020-07-07 08:56:26 +00:00
|
|
|
let signature = sign(h.ephemeral.seckey, SkMessage(secret.data))
|
|
|
|
secret.clear()
|
2020-04-06 16:24:15 +00:00
|
|
|
|
2019-02-05 15:40:29 +00:00
|
|
|
h.remoteHPubkey = pubkey
|
2020-04-06 16:24:15 +00:00
|
|
|
var payload = rlp.encodeList(signature.toRaw(),
|
2020-04-04 16:44:01 +00:00
|
|
|
h.host.pubkey.toRaw(),
|
2019-02-05 15:40:29 +00:00
|
|
|
h.initiatorNonce,
|
|
|
|
[byte(h.version)])
|
2019-03-13 22:15:26 +00:00
|
|
|
doAssert(len(payload) == PlainAuthMessageEIP8Length)
|
2020-07-07 08:56:26 +00:00
|
|
|
let
|
|
|
|
pencsize = eciesEncryptedLength(len(payload))
|
|
|
|
|
2022-06-17 20:45:37 +00:00
|
|
|
var padsize = int(rng.generate(byte)) # aka rand(max)
|
|
|
|
while padsize <= (AuthMessageV4Length - (pencsize + 2)):
|
|
|
|
padsize = int(rng.generate(byte))
|
|
|
|
|
2019-02-05 15:40:29 +00:00
|
|
|
# It is possible to make packet size constant by uncommenting this line
|
|
|
|
# padsize = 24
|
2022-06-17 20:45:37 +00:00
|
|
|
let
|
|
|
|
wosize = pencsize + padsize
|
|
|
|
fullsize = wosize + 2
|
|
|
|
|
|
|
|
rng.generate(toa(buffer, PlainAuthMessageEIP8Length, padsize))
|
|
|
|
|
2019-02-05 15:40:29 +00:00
|
|
|
if encrypt:
|
|
|
|
if len(output) < fullsize:
|
2020-04-06 16:24:15 +00:00
|
|
|
return err(BufferOverrun)
|
2022-06-17 20:45:37 +00:00
|
|
|
|
|
|
|
copyMem(addr buffer[0], addr payload[0], len(payload))
|
|
|
|
|
2020-03-05 00:25:21 +00:00
|
|
|
let wosizeBE = uint16(wosize).toBytesBE()
|
|
|
|
output[0..<2] = wosizeBE
|
2022-06-17 20:45:37 +00:00
|
|
|
if eciesEncrypt(rng, toa(buffer, 0, len(payload) + padsize),
|
2019-02-05 15:40:29 +00:00
|
|
|
toa(output, 2, wosize), pubkey,
|
2020-04-06 16:24:15 +00:00
|
|
|
toa(output, 0, 2)).isErr:
|
|
|
|
return err(EciesError)
|
2019-02-05 15:40:29 +00:00
|
|
|
outlen = fullsize
|
|
|
|
else:
|
2022-06-17 20:45:37 +00:00
|
|
|
let plainsize = len(payload) + padsize
|
2019-02-05 15:40:29 +00:00
|
|
|
if len(output) < plainsize:
|
2020-04-06 16:24:15 +00:00
|
|
|
return err(BufferOverrun)
|
2019-02-05 15:40:29 +00:00
|
|
|
copyMem(addr output[0], addr buffer[0], plainsize)
|
|
|
|
outlen = plainsize
|
2020-04-06 16:24:15 +00:00
|
|
|
|
|
|
|
ok()
|
2019-02-05 15:40:29 +00:00
|
|
|
|
|
|
|
proc ackMessagePreEIP8(h: var Handshake,
|
2022-06-17 20:45:37 +00:00
|
|
|
rng: var HmacDrbgContext,
|
2021-12-20 12:14:50 +00:00
|
|
|
output: var openArray[byte],
|
2019-02-05 15:40:29 +00:00
|
|
|
outlen: var int,
|
2020-04-06 16:24:15 +00:00
|
|
|
flag: byte = 0,
|
|
|
|
encrypt: bool = true): AuthResult[void] =
|
2019-02-05 15:40:29 +00:00
|
|
|
## Create plain pre-EIP8 authentication ack message.
|
|
|
|
var buffer: array[PlainAckMessageV4Length, byte]
|
|
|
|
outlen = 0
|
2020-04-06 16:24:15 +00:00
|
|
|
let header = cast[ptr AckMessageV4](addr buffer[0])
|
2020-04-04 16:44:01 +00:00
|
|
|
header.pubkey = h.ephemeral.pubkey.toRaw()
|
2019-02-05 15:40:29 +00:00
|
|
|
header.nonce = h.responderNonce
|
2020-04-06 16:24:15 +00:00
|
|
|
header.flag = flag
|
2019-02-05 15:40:29 +00:00
|
|
|
if encrypt:
|
|
|
|
if len(output) < AckMessageV4Length:
|
2020-04-06 16:24:15 +00:00
|
|
|
return err(BufferOverrun)
|
2020-07-07 08:56:26 +00:00
|
|
|
if eciesEncrypt(rng, buffer, output, h.remoteHPubkey).isErr:
|
2020-04-06 16:24:15 +00:00
|
|
|
return err(EciesError)
|
2019-02-05 15:40:29 +00:00
|
|
|
outlen = AckMessageV4Length
|
|
|
|
else:
|
|
|
|
if len(output) < PlainAckMessageV4Length:
|
2020-04-06 16:24:15 +00:00
|
|
|
return err(BufferOverrun)
|
2019-02-05 15:40:29 +00:00
|
|
|
copyMem(addr output[0], addr buffer[0], PlainAckMessageV4Length)
|
|
|
|
outlen = PlainAckMessageV4Length
|
2020-04-06 16:24:15 +00:00
|
|
|
|
|
|
|
ok()
|
2019-02-05 15:40:29 +00:00
|
|
|
|
|
|
|
proc ackMessageEIP8(h: var Handshake,
|
2022-06-17 20:45:37 +00:00
|
|
|
rng: var HmacDrbgContext,
|
2021-12-20 12:14:50 +00:00
|
|
|
output: var openArray[byte],
|
2019-02-05 15:40:29 +00:00
|
|
|
outlen: var int,
|
2020-04-06 16:24:15 +00:00
|
|
|
flag: byte = 0,
|
|
|
|
encrypt: bool = true): AuthResult[void] =
|
2019-02-05 15:40:29 +00:00
|
|
|
## Create EIP8 authentication ack message.
|
|
|
|
var
|
|
|
|
buffer: array[PlainAckMessageMaxEIP8, byte]
|
2020-07-07 08:56:26 +00:00
|
|
|
padsize: array[1, byte]
|
2019-03-13 22:15:26 +00:00
|
|
|
doAssert(EIP8 in h.flags)
|
2020-04-04 16:44:01 +00:00
|
|
|
var payload = rlp.encodeList(h.ephemeral.pubkey.toRaw(),
|
2019-02-05 15:40:29 +00:00
|
|
|
h.responderNonce,
|
|
|
|
[byte(h.version)])
|
2019-03-13 22:15:26 +00:00
|
|
|
doAssert(len(payload) == PlainAckMessageEIP8Length)
|
2019-02-05 15:40:29 +00:00
|
|
|
outlen = 0
|
|
|
|
let pencsize = eciesEncryptedLength(len(payload))
|
|
|
|
while true:
|
2022-06-17 20:45:37 +00:00
|
|
|
generate(rng, padsize)
|
2020-07-07 08:56:26 +00:00
|
|
|
if int(padsize[0]) > (AckMessageV4Length - (pencsize + 2)):
|
2019-02-05 15:40:29 +00:00
|
|
|
break
|
|
|
|
# It is possible to make packet size constant by uncommenting this line
|
|
|
|
# padsize = 0
|
2020-07-07 08:56:26 +00:00
|
|
|
let wosize = pencsize + int(padsize[0])
|
2019-02-05 15:40:29 +00:00
|
|
|
let fullsize = wosize + 2
|
2020-07-07 08:56:26 +00:00
|
|
|
if int(padsize[0]) > 0:
|
2022-06-17 20:45:37 +00:00
|
|
|
rng.generate(toa(buffer, PlainAckMessageEIP8Length, int(padsize[0])))
|
2020-07-07 08:56:26 +00:00
|
|
|
|
2019-02-05 15:40:29 +00:00
|
|
|
copyMem(addr buffer[0], addr payload[0], len(payload))
|
|
|
|
if encrypt:
|
|
|
|
if len(output) < fullsize:
|
2020-04-06 16:24:15 +00:00
|
|
|
return err(BufferOverrun)
|
2020-03-05 00:25:21 +00:00
|
|
|
output[0..<2] = uint16(wosize).toBytesBE()
|
2020-07-07 08:56:26 +00:00
|
|
|
if eciesEncrypt(rng, toa(buffer, 0, len(payload) + int(padsize[0])),
|
2019-02-05 15:40:29 +00:00
|
|
|
toa(output, 2, wosize), h.remoteHPubkey,
|
2020-04-06 16:24:15 +00:00
|
|
|
toa(output, 0, 2)).isErr:
|
|
|
|
return err(EciesError)
|
2019-02-05 15:40:29 +00:00
|
|
|
outlen = fullsize
|
|
|
|
else:
|
2020-07-07 08:56:26 +00:00
|
|
|
let plainsize = len(payload) + int(padsize[0])
|
2019-02-05 15:40:29 +00:00
|
|
|
if len(output) < plainsize:
|
2020-04-06 16:24:15 +00:00
|
|
|
return err(BufferOverrun)
|
2019-02-05 15:40:29 +00:00
|
|
|
copyMem(addr output[0], addr buffer[0], plainsize)
|
|
|
|
outlen = plainsize
|
2020-04-06 16:24:15 +00:00
|
|
|
|
|
|
|
ok()
|
2019-02-05 15:40:29 +00:00
|
|
|
|
|
|
|
template authSize*(h: Handshake, encrypt: bool = true): int =
|
|
|
|
## Get number of bytes needed to store AuthMessage.
|
|
|
|
if EIP8 in h.flags:
|
|
|
|
if encrypt: (AuthMessageMaxEIP8) else: (PlainAuthMessageMaxEIP8)
|
|
|
|
else:
|
|
|
|
if encrypt: (AuthMessageV4Length) else: (PlainAuthMessageV4Length)
|
|
|
|
|
|
|
|
template ackSize*(h: Handshake, encrypt: bool = true): int =
|
|
|
|
## Get number of bytes needed to store AckMessage.
|
|
|
|
if EIP8 in h.flags:
|
|
|
|
if encrypt: (AckMessageMaxEIP8) else: (PlainAckMessageMaxEIP8)
|
|
|
|
else:
|
|
|
|
if encrypt: (AckMessageV4Length) else: (PlainAckMessageV4Length)
|
|
|
|
|
2022-06-17 20:45:37 +00:00
|
|
|
proc authMessage*(h: var Handshake, rng: var HmacDrbgContext,
|
2020-07-07 08:56:26 +00:00
|
|
|
pubkey: PublicKey,
|
2021-12-20 12:14:50 +00:00
|
|
|
output: var openArray[byte],
|
2020-04-06 16:24:15 +00:00
|
|
|
outlen: var int, flag: byte = 0,
|
|
|
|
encrypt: bool = true): AuthResult[void] =
|
2019-02-05 15:40:29 +00:00
|
|
|
## Create new AuthMessage for specified `pubkey` and store it inside
|
|
|
|
## of `output`, size of generated AuthMessage will stored in `outlen`.
|
|
|
|
if EIP8 in h.flags:
|
2020-07-07 08:56:26 +00:00
|
|
|
authMessageEIP8(h, rng, pubkey, output, outlen, flag, encrypt)
|
2019-02-05 15:40:29 +00:00
|
|
|
else:
|
2020-07-07 08:56:26 +00:00
|
|
|
authMessagePreEIP8(h, rng, pubkey, output, outlen, flag, encrypt)
|
2019-02-05 15:40:29 +00:00
|
|
|
|
2022-06-17 20:45:37 +00:00
|
|
|
proc ackMessage*(h: var Handshake, rng: var HmacDrbgContext,
|
2021-12-20 12:14:50 +00:00
|
|
|
output: var openArray[byte],
|
2020-04-06 16:24:15 +00:00
|
|
|
outlen: var int, flag: byte = 0,
|
|
|
|
encrypt: bool = true): AuthResult[void] =
|
2019-02-05 15:40:29 +00:00
|
|
|
## Create new AckMessage and store it inside of `output`, size of generated
|
|
|
|
## AckMessage will stored in `outlen`.
|
|
|
|
if EIP8 in h.flags:
|
2020-07-07 08:56:26 +00:00
|
|
|
ackMessageEIP8(h, rng, output, outlen, flag, encrypt)
|
2019-02-05 15:40:29 +00:00
|
|
|
else:
|
2020-07-07 08:56:26 +00:00
|
|
|
ackMessagePreEIP8(h, rng, output, outlen, flag, encrypt)
|
2019-02-05 15:40:29 +00:00
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
proc decodeAuthMessageV4(h: var Handshake, m: openArray[byte]): AuthResult[void] =
|
2019-02-05 15:40:29 +00:00
|
|
|
## Decodes V4 AuthMessage.
|
|
|
|
var
|
|
|
|
buffer: array[PlainAuthMessageV4Length, byte]
|
2020-04-04 16:44:01 +00:00
|
|
|
|
2019-03-13 22:15:26 +00:00
|
|
|
doAssert(Responder in h.flags)
|
2020-04-06 16:24:15 +00:00
|
|
|
if eciesDecrypt(m, buffer, h.host.seckey).isErr:
|
|
|
|
return err(EciesError)
|
|
|
|
|
|
|
|
let
|
|
|
|
header = cast[ptr AuthMessageV4](addr buffer[0])
|
|
|
|
pubkey = ? PublicKey.fromRaw(header.pubkey).mapErrTo(InvalidPubKey)
|
|
|
|
signature = ? Signature.fromRaw(header.signature).mapErrTo(SignatureError)
|
|
|
|
|
2020-06-22 16:07:48 +00:00
|
|
|
var secret = ecdhRaw(h.host.seckey, pubkey)
|
2020-07-07 08:56:26 +00:00
|
|
|
secret.data = secret.data xor header.nonce
|
2020-04-06 16:24:15 +00:00
|
|
|
|
2020-07-07 08:56:26 +00:00
|
|
|
var recovered = recover(signature, SkMessage(secret.data))
|
2020-04-06 16:24:15 +00:00
|
|
|
secret.clear()
|
|
|
|
|
2020-07-07 08:56:26 +00:00
|
|
|
h.remoteEPubkey = ? recovered.mapErrTo(SignatureError)
|
2019-02-05 15:40:29 +00:00
|
|
|
h.initiatorNonce = header.nonce
|
2020-04-06 16:24:15 +00:00
|
|
|
h.remoteHPubkey = pubkey
|
|
|
|
|
|
|
|
ok()
|
2019-02-05 15:40:29 +00:00
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
proc decodeAuthMessageEIP8(h: var Handshake, m: openArray[byte]): AuthResult[void] =
|
2019-02-05 15:40:29 +00:00
|
|
|
## Decodes EIP-8 AuthMessage.
|
2020-03-05 00:25:21 +00:00
|
|
|
let size = uint16.fromBytesBE(m)
|
2019-02-05 15:40:29 +00:00
|
|
|
h.expectedLength = int(size) + 2
|
|
|
|
if h.expectedLength > len(m):
|
2020-04-06 16:24:15 +00:00
|
|
|
return err(IncompleteError)
|
2019-02-05 15:40:29 +00:00
|
|
|
var buffer = newSeq[byte](eciesDecryptedLength(int(size)))
|
|
|
|
if eciesDecrypt(toa(m, 2, int(size)), buffer, h.host.seckey,
|
2020-04-06 16:24:15 +00:00
|
|
|
toa(m, 0, 2)).isErr:
|
|
|
|
return err(EciesError)
|
2019-02-05 15:40:29 +00:00
|
|
|
try:
|
2020-04-20 18:14:39 +00:00
|
|
|
var reader = rlpFromBytes(buffer)
|
2019-02-05 15:40:29 +00:00
|
|
|
if not reader.isList() or reader.listLen() < 4:
|
2020-04-06 16:24:15 +00:00
|
|
|
return err(InvalidAuth)
|
2019-02-05 15:40:29 +00:00
|
|
|
if reader.listElem(0).blobLen != RawSignatureSize:
|
2020-04-06 16:24:15 +00:00
|
|
|
return err(InvalidAuth)
|
2019-02-05 15:40:29 +00:00
|
|
|
if reader.listElem(1).blobLen != RawPublicKeySize:
|
2020-04-06 16:24:15 +00:00
|
|
|
return err(InvalidAuth)
|
2019-02-05 15:40:29 +00:00
|
|
|
if reader.listElem(2).blobLen != KeyLength:
|
2020-04-06 16:24:15 +00:00
|
|
|
return err(InvalidAuth)
|
2019-02-05 15:40:29 +00:00
|
|
|
if reader.listElem(3).blobLen != 1:
|
2020-04-06 16:24:15 +00:00
|
|
|
return err(InvalidAuth)
|
2020-04-20 18:14:39 +00:00
|
|
|
let
|
|
|
|
signatureBr = reader.listElem(0).toBytes()
|
|
|
|
pubkeyBr = reader.listElem(1).toBytes()
|
|
|
|
nonceBr = reader.listElem(2).toBytes()
|
|
|
|
versionBr = reader.listElem(3).toBytes()
|
2020-04-06 16:24:15 +00:00
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
let
|
|
|
|
signature = ? Signature.fromRaw(signatureBr).mapErrTo(SignatureError)
|
|
|
|
pubkey = ? PublicKey.fromRaw(pubkeyBr).mapErrTo(InvalidPubKey)
|
|
|
|
nonce = toArray(KeyLength, nonceBr)
|
2020-04-06 16:24:15 +00:00
|
|
|
|
2020-06-22 16:07:48 +00:00
|
|
|
var secret = ecdhRaw(h.host.seckey, pubkey)
|
2020-07-07 08:56:26 +00:00
|
|
|
secret.data = secret.data xor nonce
|
2020-04-06 16:24:15 +00:00
|
|
|
|
2020-07-07 08:56:26 +00:00
|
|
|
let recovered = recover(signature, SkMessage(secret.data))
|
2020-04-06 16:24:15 +00:00
|
|
|
secret.clear()
|
|
|
|
|
2020-07-07 08:56:26 +00:00
|
|
|
h.remoteEPubkey = ? recovered.mapErrTo(SignatureError)
|
2019-02-05 15:40:29 +00:00
|
|
|
h.initiatorNonce = nonce
|
2020-04-06 16:24:15 +00:00
|
|
|
h.remoteHPubkey = pubkey
|
2020-04-20 18:14:39 +00:00
|
|
|
h.version = versionBr[0]
|
2020-04-06 16:24:15 +00:00
|
|
|
ok()
|
2019-04-23 01:31:12 +00:00
|
|
|
except CatchableError:
|
2020-04-06 16:24:15 +00:00
|
|
|
err(RlpError)
|
2019-02-05 15:40:29 +00:00
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
proc decodeAckMessageEIP8*(h: var Handshake, m: openArray[byte]): AuthResult[void] =
|
2019-02-05 15:40:29 +00:00
|
|
|
## Decodes EIP-8 AckMessage.
|
2020-03-05 00:25:21 +00:00
|
|
|
let size = uint16.fromBytesBE(m)
|
|
|
|
|
2019-02-05 15:40:29 +00:00
|
|
|
h.expectedLength = 2 + int(size)
|
|
|
|
if h.expectedLength > len(m):
|
2020-04-06 16:24:15 +00:00
|
|
|
return err(IncompleteError)
|
2019-02-05 15:40:29 +00:00
|
|
|
var buffer = newSeq[byte](eciesDecryptedLength(int(size)))
|
|
|
|
if eciesDecrypt(toa(m, 2, int(size)), buffer, h.host.seckey,
|
2020-04-06 16:24:15 +00:00
|
|
|
toa(m, 0, 2)).isErr:
|
|
|
|
return err(EciesError)
|
2019-02-05 15:40:29 +00:00
|
|
|
try:
|
2020-04-20 18:14:39 +00:00
|
|
|
var reader = rlpFromBytes(buffer)
|
2019-02-05 15:40:29 +00:00
|
|
|
if not reader.isList() or reader.listLen() < 3:
|
2020-04-06 16:24:15 +00:00
|
|
|
return err(InvalidAck)
|
2019-02-05 15:40:29 +00:00
|
|
|
if reader.listElem(0).blobLen != RawPublicKeySize:
|
2020-04-06 16:24:15 +00:00
|
|
|
return err(InvalidAck)
|
2019-02-05 15:40:29 +00:00
|
|
|
if reader.listElem(1).blobLen != KeyLength:
|
2020-04-06 16:24:15 +00:00
|
|
|
return err(InvalidAck)
|
2019-02-05 15:40:29 +00:00
|
|
|
if reader.listElem(2).blobLen != 1:
|
2020-04-06 16:24:15 +00:00
|
|
|
return err(InvalidAck)
|
2020-04-20 18:14:39 +00:00
|
|
|
let
|
|
|
|
pubkeyBr = reader.listElem(0).toBytes()
|
|
|
|
nonceBr = reader.listElem(1).toBytes()
|
|
|
|
versionBr = reader.listElem(2).toBytes()
|
|
|
|
|
|
|
|
h.remoteEPubkey = ? PublicKey.fromRaw(pubkeyBr).mapErrTo(InvalidPubKey)
|
|
|
|
h.responderNonce = toArray(KeyLength, nonceBr)
|
|
|
|
h.version = versionBr[0]
|
2020-04-06 16:24:15 +00:00
|
|
|
|
|
|
|
ok()
|
2019-04-23 01:31:12 +00:00
|
|
|
except CatchableError:
|
2020-04-06 16:24:15 +00:00
|
|
|
err(RlpError)
|
2019-02-05 15:40:29 +00:00
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
proc decodeAckMessageV4(h: var Handshake, m: openArray[byte]): AuthResult[void] =
|
2019-02-05 15:40:29 +00:00
|
|
|
## Decodes V4 AckMessage.
|
|
|
|
var
|
|
|
|
buffer: array[PlainAckMessageV4Length, byte]
|
2019-03-13 22:15:26 +00:00
|
|
|
doAssert(Initiator in h.flags)
|
2020-04-06 16:24:15 +00:00
|
|
|
|
|
|
|
if eciesDecrypt(m, buffer, h.host.seckey).isErr:
|
|
|
|
return err(EciesError)
|
2019-02-05 15:40:29 +00:00
|
|
|
var header = cast[ptr AckMessageV4](addr buffer[0])
|
2020-04-06 16:24:15 +00:00
|
|
|
|
|
|
|
h.remoteEPubkey = ? PublicKey.fromRaw(header.pubkey).mapErrTo(InvalidPubKey)
|
2019-02-05 15:40:29 +00:00
|
|
|
h.responderNonce = header.nonce
|
|
|
|
|
2020-04-06 16:24:15 +00:00
|
|
|
ok()
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
proc decodeAuthMessage*(h: var Handshake, input: openArray[byte]): AuthResult[void] =
|
2019-02-05 15:40:29 +00:00
|
|
|
## Decodes AuthMessage from `input`.
|
|
|
|
if len(input) < AuthMessageV4Length:
|
2020-04-06 16:24:15 +00:00
|
|
|
return err(IncompleteError)
|
|
|
|
|
|
|
|
if len(input) == AuthMessageV4Length:
|
|
|
|
let res = h.decodeAuthMessageV4(input)
|
|
|
|
if res.isOk(): return res
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
let res = h.decodeAuthMessageEIP8(input)
|
2020-04-06 16:24:15 +00:00
|
|
|
if res.isOk():
|
|
|
|
h.flags.incl(EIP8)
|
|
|
|
res
|
2019-02-05 15:40:29 +00:00
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
proc decodeAckMessage*(h: var Handshake, input: openArray[byte]): AuthResult[void] =
|
2019-02-05 15:40:29 +00:00
|
|
|
## Decodes AckMessage from `input`.
|
|
|
|
if len(input) < AckMessageV4Length:
|
2020-04-06 16:24:15 +00:00
|
|
|
return err(IncompleteError)
|
|
|
|
if len(input) == AckMessageV4Length:
|
|
|
|
let res = h.decodeAckMessageV4(input)
|
|
|
|
if res.isOk(): return res
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
let res = h.decodeAckMessageEIP8(input)
|
2020-04-06 16:24:15 +00:00
|
|
|
if res.isOk(): h.flags.incl(EIP8)
|
|
|
|
res
|
|
|
|
|
|
|
|
proc getSecrets*(
|
2021-12-20 12:14:50 +00:00
|
|
|
h: Handshake, authmsg: openArray[byte],
|
|
|
|
ackmsg: openArray[byte]): ConnectionSecret =
|
2019-02-05 15:40:29 +00:00
|
|
|
## Derive secrets from handshake `h` using encrypted AuthMessage `authmsg` and
|
|
|
|
## encrypted AckMessage `ackmsg`.
|
|
|
|
var
|
|
|
|
ctx0: keccak256
|
|
|
|
ctx1: keccak256
|
|
|
|
mac1: MDigest[256]
|
2020-04-06 16:24:15 +00:00
|
|
|
secret: ConnectionSecret
|
2019-02-05 15:40:29 +00:00
|
|
|
|
|
|
|
# ecdhe-secret = ecdh.agree(ephemeral-privkey, remote-ephemeral-pubk)
|
2020-06-22 16:07:48 +00:00
|
|
|
var shsec = ecdhRaw(h.ephemeral.seckey, h.remoteEPubkey)
|
2019-02-05 15:40:29 +00:00
|
|
|
|
|
|
|
# shared-secret = keccak(ecdhe-secret || keccak(nonce || initiator-nonce))
|
|
|
|
ctx0.init()
|
|
|
|
ctx1.init()
|
|
|
|
ctx1.update(h.responderNonce)
|
|
|
|
ctx1.update(h.initiatorNonce)
|
|
|
|
mac1 = ctx1.finish()
|
|
|
|
ctx1.clear()
|
2020-04-06 16:24:15 +00:00
|
|
|
ctx0.update(shsec.data)
|
2019-02-05 15:40:29 +00:00
|
|
|
ctx0.update(mac1.data)
|
|
|
|
mac1 = ctx0.finish()
|
|
|
|
|
|
|
|
# aes-secret = keccak(ecdhe-secret || shared-secret)
|
|
|
|
ctx0.init()
|
2020-04-06 16:24:15 +00:00
|
|
|
ctx0.update(shsec.data)
|
2019-02-05 15:40:29 +00:00
|
|
|
ctx0.update(mac1.data)
|
|
|
|
mac1 = ctx0.finish()
|
|
|
|
|
|
|
|
# mac-secret = keccak(ecdhe-secret || aes-secret)
|
|
|
|
ctx0.init()
|
2020-04-06 16:24:15 +00:00
|
|
|
ctx0.update(shsec.data)
|
2019-02-05 15:40:29 +00:00
|
|
|
ctx0.update(mac1.data)
|
|
|
|
secret.aesKey = mac1.data
|
|
|
|
mac1 = ctx0.finish()
|
|
|
|
secret.macKey = mac1.data
|
|
|
|
|
2020-07-07 08:56:26 +00:00
|
|
|
clear(shsec)
|
2020-04-04 16:44:01 +00:00
|
|
|
|
2019-02-05 15:40:29 +00:00
|
|
|
# egress-mac = keccak256(mac-secret ^ recipient-nonce || auth-sent-init)
|
2020-04-06 16:24:15 +00:00
|
|
|
|
|
|
|
var xornonce = mac1.data xor h.responderNonce
|
2019-02-05 15:40:29 +00:00
|
|
|
ctx0.init()
|
|
|
|
ctx0.update(xornonce)
|
|
|
|
ctx0.update(authmsg)
|
|
|
|
|
|
|
|
# ingress-mac = keccak256(mac-secret ^ initiator-nonce || auth-recvd-ack)
|
2020-04-06 16:24:15 +00:00
|
|
|
xornonce = secret.macKey xor h.initiatorNonce
|
|
|
|
|
2019-02-05 15:40:29 +00:00
|
|
|
ctx1.init()
|
|
|
|
ctx1.update(xornonce)
|
|
|
|
ctx1.update(ackmsg)
|
|
|
|
burnMem(xornonce)
|
|
|
|
|
|
|
|
if Initiator in h.flags:
|
|
|
|
secret.egressMac = ctx0
|
|
|
|
secret.ingressMac = ctx1
|
|
|
|
else:
|
|
|
|
secret.ingressMac = ctx0
|
|
|
|
secret.egressMac = ctx1
|
|
|
|
|
|
|
|
ctx0.clear()
|
|
|
|
ctx1.clear()
|
2020-04-06 16:24:15 +00:00
|
|
|
|
2021-07-14 08:35:35 +00:00
|
|
|
secret
|