2019-02-05 15:40:29 +00:00
|
|
|
#
|
|
|
|
# Ethereum P2P
|
2024-06-18 16:09:27 +00:00
|
|
|
# (c) Copyright 2018-2024
|
2019-02-05 15:40:29 +00:00
|
|
|
# Status Research & Development GmbH
|
|
|
|
#
|
|
|
|
# Licensed under either of
|
|
|
|
# Apache License, version 2.0, (LICENSE-APACHEv2)
|
|
|
|
# MIT license (LICENSE-MIT)
|
|
|
|
#
|
|
|
|
|
2024-11-02 07:15:40 +00:00
|
|
|
## This module implements Ethereum EIP-8 RLPx authentication - pre-EIP-8
|
|
|
|
## messages are not supported
|
|
|
|
## https://github.com/ethereum/devp2p/blob/5713591d0366da78a913a811c7502d9ca91d29a8/rlpx.md#initial-handshake
|
|
|
|
## https://github.com/ethereum/EIPs/blob/b479473414cf94445b450c266a9dedc079a12158/EIPS/eip-8.md
|
2019-02-05 15:40:29 +00:00
|
|
|
|
2023-05-10 13:50:04 +00:00
|
|
|
{.push raises: [].}
|
2020-04-06 16:24:15 +00:00
|
|
|
|
2021-04-06 11:33:24 +00:00
|
|
|
import
|
2022-06-17 20:45:37 +00:00
|
|
|
nimcrypto/[rijndael, keccak, utils],
|
2024-06-18 16:09:27 +00:00
|
|
|
stew/[arrayops, byteutils, endians2, objects],
|
|
|
|
results,
|
2024-09-29 08:52:19 +00:00
|
|
|
../rlp,
|
|
|
|
../common/keys,
|
2021-04-06 11:33:24 +00:00
|
|
|
./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
|
|
|
|
2024-09-29 08:52:19 +00:00
|
|
|
type keccak256 = keccak.keccak256
|
|
|
|
|
2019-02-05 15:40:29 +00:00
|
|
|
const
|
2022-11-17 07:46:27 +00:00
|
|
|
# Auth message sizes
|
2024-11-02 07:15:40 +00:00
|
|
|
MsgLenLenEIP8* = 2
|
|
|
|
## auth-size = size of enc-auth-body, encoded as a big-endian 16-bit integer
|
|
|
|
## ack-size = size of enc-ack-body, encoded as a big-endian 16-bit integer
|
2022-11-17 07:46:27 +00:00
|
|
|
|
2024-11-02 07:15:40 +00:00
|
|
|
MinPadLenEIP8* = 100
|
|
|
|
MaxPadLenEIP8* = 300
|
|
|
|
## Padding makes message length unpredictable which makes packet filtering
|
|
|
|
## a tiny bit harder - although not necessary any more, we always add at
|
|
|
|
## least 100 bytes of padding to make the message distinguishable from
|
|
|
|
## pre-EIP8 and at most 200 to stay within recommendation
|
2022-11-17 07:46:27 +00:00
|
|
|
|
2024-11-05 15:30:41 +00:00
|
|
|
# signature + pubkey + nonce + version + rlp encoding overhead
|
2022-11-17 07:46:27 +00:00
|
|
|
# 65 + 64 + 32 + 1 + 7 = 169
|
2019-02-05 15:40:29 +00:00
|
|
|
PlainAuthMessageEIP8Length = 169
|
2024-11-02 07:15:40 +00:00
|
|
|
PlainAuthMessageMaxEIP8 = PlainAuthMessageEIP8Length + MaxPadLenEIP8
|
2022-11-17 07:46:27 +00:00
|
|
|
# Min. encrypted message + size prefix = 284
|
2024-11-02 07:15:40 +00:00
|
|
|
AuthMessageEIP8Length* =
|
|
|
|
eciesEncryptedLength(PlainAuthMessageEIP8Length) + MsgLenLenEIP8
|
|
|
|
AuthMessageMaxEIP8* = AuthMessageEIP8Length + MaxPadLenEIP8
|
|
|
|
## Minimal output buffer size to pass into `authMessage`
|
2022-11-17 07:46:27 +00:00
|
|
|
|
|
|
|
# Ack message sizes
|
|
|
|
|
|
|
|
# pubkey + nounce + version + rlp encoding overhead
|
|
|
|
# 64 + 32 + 1 + 5 = 102
|
2024-11-02 07:15:40 +00:00
|
|
|
PlainAckMessageEIP8Length = 102
|
|
|
|
PlainAckMessageMaxEIP8 = PlainAckMessageEIP8Length + MaxPadLenEIP8
|
2022-11-17 07:46:27 +00:00
|
|
|
# Min. encrypted message + size prefix = 217
|
2024-11-05 15:30:41 +00:00
|
|
|
AckMessageEIP8Length* =
|
|
|
|
eciesEncryptedLength(PlainAckMessageEIP8Length) + MsgLenLenEIP8
|
2024-11-02 07:15:40 +00:00
|
|
|
AckMessageMaxEIP8* = AckMessageEIP8Length + MaxPadLenEIP8
|
|
|
|
## Minimal output buffer size to pass into `ackMessage`
|
|
|
|
|
|
|
|
Vsn = [byte 4]
|
|
|
|
## auth-vsn = 4
|
|
|
|
## ack-vsn = 4
|
2019-02-05 15:40:29 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
Nonce* = array[KeyLength, byte]
|
|
|
|
|
|
|
|
HandshakeFlag* = enum
|
2024-11-02 07:15:40 +00:00
|
|
|
Initiator ## `Handshake` owner is connection initiator
|
|
|
|
Responder ## `Handshake` owner is connection responder
|
2019-02-05 15:40:29 +00:00
|
|
|
|
2020-04-06 16:24:15 +00:00
|
|
|
AuthError* = enum
|
2024-11-02 07:15:40 +00:00
|
|
|
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"
|
2020-04-06 16:24:15 +00:00
|
|
|
IncompleteError = "auth: data incomplete"
|
2019-02-05 15:40:29 +00:00
|
|
|
|
|
|
|
Handshake* = object
|
2024-11-02 07:15:40 +00:00
|
|
|
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
|
2019-02-05 15:40:29 +00:00
|
|
|
|
|
|
|
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] =
|
2024-11-02 07:15:40 +00:00
|
|
|
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*(
|
2024-11-02 07:15:40 +00:00
|
|
|
T: type Handshake,
|
|
|
|
rng: var HmacDrbgContext,
|
|
|
|
host: KeyPair,
|
|
|
|
flags: set[HandshakeFlag],
|
|
|
|
): 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
|
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:
|
2022-06-17 20:45:37 +00:00
|
|
|
rng.generate(initiatorNonce)
|
2019-02-05 15:40:29 +00:00
|
|
|
else:
|
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
|
|
|
flags: flags,
|
|
|
|
host: host,
|
|
|
|
ephemeral: ephemeral,
|
|
|
|
initiatorNonce: initiatorNonce,
|
|
|
|
responderNonce: responderNonce,
|
2022-04-04 20:31:09 +00:00
|
|
|
)
|
2019-02-05 15:40:29 +00:00
|
|
|
|
2024-11-02 07:15:40 +00:00
|
|
|
proc authMessage*(
|
|
|
|
h: var Handshake,
|
|
|
|
rng: var HmacDrbgContext,
|
|
|
|
pubkey: PublicKey,
|
|
|
|
output: var openArray[byte],
|
|
|
|
): AuthResult[int] =
|
|
|
|
## Create EIP8 authentication message - returns length of encoded message
|
|
|
|
## The output should be a buffer of AuthMessageMaxEIP8 bytes at least.
|
|
|
|
if len(output) < AuthMessageMaxEIP8:
|
|
|
|
return err(AuthError.BufferOverrun)
|
|
|
|
|
|
|
|
var padsize = int(rng.generate(byte))
|
|
|
|
while padsize > (MaxPadLenEIP8 - MinPadLenEIP8):
|
|
|
|
padsize = int(rng.generate(byte))
|
|
|
|
padsize += MinPadLenEIP8
|
|
|
|
|
|
|
|
let
|
|
|
|
pencsize = eciesEncryptedLength(PlainAuthMessageEIP8Length)
|
|
|
|
wosize = pencsize + padsize
|
|
|
|
fullsize = wosize + 2
|
|
|
|
|
|
|
|
doAssert fullsize <= len(output), "We checked against max possible length above"
|
2020-04-06 16:24:15 +00:00
|
|
|
|
2023-04-21 08:59:44 +00:00
|
|
|
var secret = ecdhSharedSecret(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
|
2024-11-02 07:15:40 +00:00
|
|
|
var payload =
|
|
|
|
rlp.encodeList(signature.toRaw(), h.host.pubkey.toRaw(), h.initiatorNonce, Vsn)
|
|
|
|
doAssert(len(payload) == PlainAuthMessageEIP8Length)
|
2019-02-05 15:40:29 +00:00
|
|
|
|
2024-11-02 07:15:40 +00:00
|
|
|
var buffer {.noinit.}: array[PlainAuthMessageMaxEIP8, byte]
|
|
|
|
copyMem(addr buffer[0], addr payload[0], len(payload))
|
|
|
|
rng.generate(toa(buffer, PlainAuthMessageEIP8Length, padsize))
|
2020-04-06 16:24:15 +00:00
|
|
|
|
2024-11-02 07:15:40 +00:00
|
|
|
let wosizeBE = uint16(wosize).toBytesBE()
|
|
|
|
output[0 ..< 2] = wosizeBE
|
|
|
|
if eciesEncrypt(
|
|
|
|
rng,
|
|
|
|
toa(buffer, 0, len(payload) + padsize),
|
|
|
|
toa(output, 2, wosize),
|
|
|
|
pubkey,
|
|
|
|
toa(output, 0, 2),
|
|
|
|
).isErr:
|
|
|
|
return err(AuthError.EciesError)
|
2020-04-06 16:24:15 +00:00
|
|
|
|
2024-11-02 07:15:40 +00:00
|
|
|
ok(fullsize)
|
2020-04-06 16:24:15 +00:00
|
|
|
|
2024-11-02 07:15:40 +00:00
|
|
|
proc ackMessage*(
|
|
|
|
h: var Handshake, rng: var HmacDrbgContext, output: var openArray[byte]
|
|
|
|
): AuthResult[int] =
|
|
|
|
## Create EIP8 authentication ack message - returns length of encoded message
|
|
|
|
## The output should be a buffer of AckMessageMaxEIP8 bytes at least.
|
|
|
|
if len(output) < AckMessageMaxEIP8:
|
|
|
|
return err(AuthError.BufferOverrun)
|
2020-07-07 08:56:26 +00:00
|
|
|
|
2024-11-02 07:15:40 +00:00
|
|
|
var padsize = int(rng.generate(byte))
|
|
|
|
while padsize > (MaxPadLenEIP8 - MinPadLenEIP8):
|
2022-06-17 20:45:37 +00:00
|
|
|
padsize = int(rng.generate(byte))
|
2024-11-02 07:15:40 +00:00
|
|
|
padsize += MinPadLenEIP8
|
2022-06-17 20:45:37 +00:00
|
|
|
|
|
|
|
let
|
2024-11-02 07:15:40 +00:00
|
|
|
pencsize = eciesEncryptedLength(PlainAckMessageEIP8Length)
|
2022-06-17 20:45:37 +00:00
|
|
|
wosize = pencsize + padsize
|
|
|
|
fullsize = wosize + 2
|
|
|
|
|
2024-11-02 07:15:40 +00:00
|
|
|
doAssert fullsize <= len(output), "We checked against max possible length above"
|
2022-06-17 20:45:37 +00:00
|
|
|
|
2019-02-05 15:40:29 +00:00
|
|
|
var
|
|
|
|
buffer: array[PlainAckMessageMaxEIP8, byte]
|
2024-11-02 07:15:40 +00:00
|
|
|
payload = rlp.encodeList(h.ephemeral.pubkey.toRaw(), h.responderNonce, Vsn)
|
2019-03-13 22:15:26 +00:00
|
|
|
doAssert(len(payload) == PlainAckMessageEIP8Length)
|
2020-04-04 16:44:01 +00:00
|
|
|
|
2024-11-02 07:15:40 +00:00
|
|
|
copyMem(addr buffer[0], addr payload[0], PlainAckMessageEIP8Length)
|
|
|
|
rng.generate(toa(buffer, PlainAckMessageEIP8Length, padsize))
|
2020-04-06 16:24:15 +00:00
|
|
|
|
2024-11-02 07:15:40 +00:00
|
|
|
output[0 ..< MsgLenLenEIP8] = uint16(wosize).toBytesBE()
|
2020-04-06 16:24:15 +00:00
|
|
|
|
2024-11-02 07:15:40 +00:00
|
|
|
if eciesEncrypt(
|
|
|
|
rng,
|
|
|
|
toa(buffer, 0, PlainAckMessageEIP8Length + padsize),
|
|
|
|
toa(output, MsgLenLenEIP8, wosize),
|
|
|
|
h.remoteHPubkey,
|
|
|
|
toa(output, 0, MsgLenLenEIP8),
|
|
|
|
).isErr:
|
|
|
|
return err(AuthError.EciesError)
|
|
|
|
ok(fullsize)
|
2020-04-06 16:24:15 +00:00
|
|
|
|
2024-11-05 15:30:41 +00:00
|
|
|
func decodeMsgLen(input: openArray[byte]): AuthResult[int] =
|
2024-11-02 07:15:40 +00:00
|
|
|
if input.len < 2:
|
|
|
|
return err(AuthError.IncompleteError)
|
2024-11-05 15:30:41 +00:00
|
|
|
ok(int(uint16.fromBytesBE(input)) + 2)
|
|
|
|
|
|
|
|
func decodeAuthMsgLen*(h: Handshake, input: openArray[byte]): AuthResult[int] =
|
|
|
|
let len = ?decodeMsgLen(input)
|
2024-11-02 07:15:40 +00:00
|
|
|
if len < AuthMessageEIP8Length:
|
|
|
|
return err(AuthError.IncompleteError)
|
|
|
|
ok(len)
|
2019-02-05 15:40:29 +00:00
|
|
|
|
2024-11-05 15:30:41 +00:00
|
|
|
func decodeAckMsgLen*(h: Handshake, input: openArray[byte]): AuthResult[int] =
|
|
|
|
let len = ?decodeMsgLen(input)
|
|
|
|
if len < AckMessageEIP8Length:
|
|
|
|
return err(AuthError.IncompleteError)
|
|
|
|
ok(len)
|
|
|
|
|
2024-11-02 07:15:40 +00:00
|
|
|
proc decodeAuthMessage*(h: var Handshake, m: openArray[byte]): AuthResult[void] =
|
2019-02-05 15:40:29 +00:00
|
|
|
## Decodes EIP-8 AuthMessage.
|
2024-11-02 07:15:40 +00:00
|
|
|
let
|
2024-11-05 15:30:41 +00:00
|
|
|
expectedLength = ?h.decodeAuthMsgLen(m)
|
2024-11-02 07:15:40 +00:00
|
|
|
size = expectedLength - MsgLenLenEIP8
|
2022-11-17 07:46:27 +00:00
|
|
|
|
|
|
|
# Check if the prefixed size is => than the minimum
|
2024-11-02 07:15:40 +00:00
|
|
|
if expectedLength < AuthMessageEIP8Length:
|
2022-11-17 07:46:27 +00:00
|
|
|
return err(AuthError.IncompleteError)
|
|
|
|
|
2024-11-02 07:15:40 +00:00
|
|
|
if expectedLength > len(m):
|
2022-09-06 17:38:22 +00:00
|
|
|
return err(AuthError.IncompleteError)
|
2022-11-17 07:46:27 +00:00
|
|
|
|
2024-11-02 07:15:40 +00:00
|
|
|
var buffer = newSeq[byte](eciesDecryptedLength(size))
|
2022-11-17 07:46:27 +00:00
|
|
|
if eciesDecrypt(
|
2024-11-02 07:15:40 +00:00
|
|
|
toa(m, MsgLenLenEIP8, int(size)), buffer, h.host.seckey, toa(m, 0, MsgLenLenEIP8)
|
|
|
|
).isErr:
|
2022-09-06 17:38:22 +00:00
|
|
|
return err(AuthError.EciesError)
|
2024-11-02 07:15:40 +00:00
|
|
|
|
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:
|
2022-09-06 17:38:22 +00:00
|
|
|
return err(AuthError.InvalidAuth)
|
2019-02-05 15:40:29 +00:00
|
|
|
if reader.listElem(0).blobLen != RawSignatureSize:
|
2022-09-06 17:38:22 +00:00
|
|
|
return err(AuthError.InvalidAuth)
|
2019-02-05 15:40:29 +00:00
|
|
|
if reader.listElem(1).blobLen != RawPublicKeySize:
|
2022-09-06 17:38:22 +00:00
|
|
|
return err(AuthError.InvalidAuth)
|
2019-02-05 15:40:29 +00:00
|
|
|
if reader.listElem(2).blobLen != KeyLength:
|
2022-09-06 17:38:22 +00:00
|
|
|
return err(AuthError.InvalidAuth)
|
2019-02-05 15:40:29 +00:00
|
|
|
if reader.listElem(3).blobLen != 1:
|
2022-09-06 17:38:22 +00:00
|
|
|
return err(AuthError.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()
|
2020-04-06 16:24:15 +00:00
|
|
|
|
2024-11-02 07:15:40 +00:00
|
|
|
signature = ?Signature.fromRaw(signatureBr).mapErrTo(SignatureError)
|
|
|
|
pubkey = ?PublicKey.fromRaw(pubkeyBr).mapErrTo(InvalidPubKey)
|
2020-04-20 18:14:39 +00:00
|
|
|
nonce = toArray(KeyLength, nonceBr)
|
2020-04-06 16:24:15 +00:00
|
|
|
|
2023-04-21 08:59:44 +00:00
|
|
|
var secret = ecdhSharedSecret(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()
|
|
|
|
|
2024-11-02 07:15:40 +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
|
|
|
|
ok()
|
2019-04-23 01:31:12 +00:00
|
|
|
except CatchableError:
|
2022-09-06 17:38:22 +00:00
|
|
|
err(AuthError.RlpError)
|
2019-02-05 15:40:29 +00:00
|
|
|
|
2024-11-02 07:15:40 +00:00
|
|
|
proc decodeAckMessage*(h: var Handshake, m: openArray[byte]): AuthResult[void] =
|
2019-02-05 15:40:29 +00:00
|
|
|
## Decodes EIP-8 AckMessage.
|
2024-11-02 07:15:40 +00:00
|
|
|
let
|
2024-11-05 15:30:41 +00:00
|
|
|
expectedLength = ?h.decodeAckMsgLen(m)
|
2024-11-02 07:15:40 +00:00
|
|
|
size = expectedLength - MsgLenLenEIP8
|
2022-11-17 07:46:27 +00:00
|
|
|
|
|
|
|
# Check if the prefixed size is => than the minimum
|
2024-11-02 07:15:40 +00:00
|
|
|
if expectedLength > len(m):
|
2022-11-17 07:46:27 +00:00
|
|
|
return err(AuthError.IncompleteError)
|
|
|
|
|
2024-11-02 07:15:40 +00:00
|
|
|
var buffer = newSeq[byte](eciesDecryptedLength(size))
|
2022-11-17 07:46:27 +00:00
|
|
|
if eciesDecrypt(
|
2024-11-02 07:15:40 +00:00
|
|
|
toa(m, MsgLenLenEIP8, size), buffer, h.host.seckey, toa(m, 0, MsgLenLenEIP8)
|
|
|
|
).isErr:
|
2022-09-06 17:38:22 +00:00
|
|
|
return err(AuthError.EciesError)
|
2019-02-05 15:40:29 +00:00
|
|
|
try:
|
2020-04-20 18:14:39 +00:00
|
|
|
var reader = rlpFromBytes(buffer)
|
2024-11-02 07:15:40 +00:00
|
|
|
# The last element, the version, is ignored
|
2019-02-05 15:40:29 +00:00
|
|
|
if not reader.isList() or reader.listLen() < 3:
|
2022-09-06 17:38:22 +00:00
|
|
|
return err(AuthError.InvalidAck)
|
2019-02-05 15:40:29 +00:00
|
|
|
if reader.listElem(0).blobLen != RawPublicKeySize:
|
2022-09-06 17:38:22 +00:00
|
|
|
return err(AuthError.InvalidAck)
|
2019-02-05 15:40:29 +00:00
|
|
|
if reader.listElem(1).blobLen != KeyLength:
|
2022-09-06 17:38:22 +00:00
|
|
|
return err(AuthError.InvalidAck)
|
2024-11-02 07:15:40 +00:00
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
let
|
|
|
|
pubkeyBr = reader.listElem(0).toBytes()
|
|
|
|
nonceBr = reader.listElem(1).toBytes()
|
|
|
|
|
2024-11-02 07:15:40 +00:00
|
|
|
h.remoteEPubkey = ?PublicKey.fromRaw(pubkeyBr).mapErrTo(InvalidPubKey)
|
2020-04-20 18:14:39 +00:00
|
|
|
h.responderNonce = toArray(KeyLength, nonceBr)
|
2020-04-06 16:24:15 +00:00
|
|
|
|
|
|
|
ok()
|
2019-04-23 01:31:12 +00:00
|
|
|
except CatchableError:
|
2022-09-06 17:38:22 +00:00
|
|
|
err(AuthError.RlpError)
|
2019-02-05 15:40:29 +00:00
|
|
|
|
2020-04-06 16:24:15 +00:00
|
|
|
proc getSecrets*(
|
2024-11-02 07:15:40 +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)
|
2023-04-21 08:59:44 +00:00
|
|
|
var shsec = ecdhSharedSecret(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
|