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 RLPx cryptography
|
|
|
|
|
2020-04-06 16:24:15 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2021-04-06 11:33:24 +00:00
|
|
|
import
|
2022-09-02 14:57:52 +00:00
|
|
|
nimcrypto/[bcmode, keccak, rijndael, utils], stew/results
|
2019-02-05 15:40:29 +00:00
|
|
|
from auth import ConnectionSecret
|
|
|
|
|
2020-04-07 09:56:25 +00:00
|
|
|
export results
|
2020-04-06 16:24:15 +00:00
|
|
|
|
2019-02-05 15:40:29 +00:00
|
|
|
const
|
|
|
|
RlpHeaderLength* = 16
|
|
|
|
RlpMacLength* = 16
|
|
|
|
maxUInt24 = (not uint32(0)) shl 8
|
|
|
|
|
|
|
|
type
|
|
|
|
SecretState* = object
|
|
|
|
## Object represents current encryption/decryption context.
|
|
|
|
aesenc*: CTR[aes256]
|
|
|
|
aesdec*: CTR[aes256]
|
|
|
|
macenc*: ECB[aes256]
|
|
|
|
emac*: keccak256
|
|
|
|
imac*: keccak256
|
|
|
|
|
2020-04-06 16:24:15 +00:00
|
|
|
RlpxError* = enum
|
|
|
|
IncorrectMac = "rlpx: MAC verification failed"
|
|
|
|
BufferOverrun = "rlpx: buffer overrun"
|
|
|
|
IncompleteError = "rlpx: data incomplete"
|
|
|
|
IncorrectArgs = "rlpx: incorrect arguments"
|
2019-02-05 15:40:29 +00:00
|
|
|
|
|
|
|
RlpxHeader* = array[16, byte]
|
|
|
|
|
2020-04-06 16:24:15 +00:00
|
|
|
RlpxResult*[T] = Result[T, RlpxError]
|
|
|
|
|
2019-02-05 15:40:29 +00:00
|
|
|
proc roundup16*(x: int): int {.inline.} =
|
|
|
|
## Procedure aligns `x` to
|
|
|
|
let rem = x and 15
|
|
|
|
if rem != 0:
|
|
|
|
result = x + 16 - rem
|
|
|
|
else:
|
|
|
|
result = x
|
|
|
|
|
|
|
|
template toa(a, b, c: untyped): untyped =
|
|
|
|
toOpenArray((a), (b), (b) + (c) - 1)
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
proc sxor[T](a: var openArray[T], b: openArray[T]) {.inline.} =
|
2019-03-13 22:15:26 +00:00
|
|
|
doAssert(len(a) == len(b))
|
2019-02-05 15:40:29 +00:00
|
|
|
for i in 0 ..< len(a):
|
|
|
|
a[i] = a[i] xor b[i]
|
|
|
|
|
|
|
|
proc initSecretState*(secrets: ConnectionSecret, context: var SecretState) =
|
|
|
|
## Initialized `context` with values from `secrets`.
|
|
|
|
|
|
|
|
# FIXME: Yes, the encryption is insecure,
|
|
|
|
# see: https://github.com/ethereum/devp2p/issues/32
|
|
|
|
# https://github.com/ethereum/py-evm/blob/master/p2p/peer.py#L159-L160
|
|
|
|
var iv: array[context.aesenc.sizeBlock, byte]
|
|
|
|
context.aesenc.init(secrets.aesKey, iv)
|
|
|
|
context.aesdec = context.aesenc
|
|
|
|
context.macenc.init(secrets.macKey)
|
|
|
|
context.emac = secrets.egressMac
|
|
|
|
context.imac = secrets.ingressMac
|
|
|
|
|
|
|
|
template encryptedLength*(size: int): int =
|
|
|
|
## Returns the number of bytes used by the entire frame of a
|
|
|
|
## message with size `size`:
|
|
|
|
RlpHeaderLength + roundup16(size) + 2 * RlpMacLength
|
|
|
|
|
|
|
|
template decryptedLength*(size: int): int =
|
|
|
|
## Returns size of decrypted message for body with length `size`.
|
|
|
|
roundup16(size)
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
proc encrypt*(c: var SecretState, header: openArray[byte],
|
|
|
|
frame: openArray[byte],
|
|
|
|
output: var openArray[byte]): RlpxResult[void] =
|
2019-02-05 15:40:29 +00:00
|
|
|
## Encrypts `header` and `frame` using SecretState `c` context and store
|
|
|
|
## result into `output`.
|
|
|
|
##
|
|
|
|
## `header` must be exactly `RlpHeaderLength` length.
|
|
|
|
## `frame` must not be zero length.
|
|
|
|
## `output` must be at least `encryptedLength(len(frame))` length.
|
|
|
|
var
|
|
|
|
tmpmac: keccak256
|
|
|
|
aes: array[RlpHeaderLength, byte]
|
|
|
|
let length = encryptedLength(len(frame))
|
|
|
|
let frameLength = roundup16(len(frame))
|
|
|
|
let headerMacPos = RlpHeaderLength
|
|
|
|
let framePos = RlpHeaderLength + RlpMacLength
|
|
|
|
let frameMacPos = RlpHeaderLength * 2 + frameLength
|
|
|
|
if len(header) != RlpHeaderLength or len(frame) == 0 or length != len(output):
|
2020-04-06 16:24:15 +00:00
|
|
|
return err(IncorrectArgs)
|
2019-02-05 15:40:29 +00:00
|
|
|
# header_ciphertext = self.aes_enc.update(header)
|
|
|
|
c.aesenc.encrypt(header, toa(output, 0, RlpHeaderLength))
|
|
|
|
# mac_secret = self.egress_mac.digest()[:HEADER_LEN]
|
|
|
|
tmpmac = c.emac
|
|
|
|
var macsec = tmpmac.finish()
|
|
|
|
# self.egress_mac.update(sxor(self.mac_enc(mac_secret), header_ciphertext))
|
|
|
|
c.macenc.encrypt(toa(macsec.data, 0, RlpHeaderLength), aes)
|
|
|
|
sxor(aes, toa(output, 0, RlpHeaderLength))
|
|
|
|
c.emac.update(aes)
|
|
|
|
burnMem(aes)
|
|
|
|
# header_mac = self.egress_mac.digest()[:HEADER_LEN]
|
|
|
|
tmpmac = c.emac
|
|
|
|
var headerMac = tmpmac.finish()
|
|
|
|
# frame_ciphertext = self.aes_enc.update(frame)
|
|
|
|
copyMem(addr output[framePos], unsafeAddr frame[0], len(frame))
|
|
|
|
c.aesenc.encrypt(toa(output, 32, frameLength), toa(output, 32, frameLength))
|
|
|
|
# self.egress_mac.update(frame_ciphertext)
|
|
|
|
c.emac.update(toa(output, 32, frameLength))
|
|
|
|
# fmac_seed = self.egress_mac.digest()[:HEADER_LEN]
|
|
|
|
tmpmac = c.emac
|
|
|
|
var seed = tmpmac.finish()
|
|
|
|
# mac_secret = self.egress_mac.digest()[:HEADER_LEN]
|
|
|
|
macsec = seed
|
|
|
|
# self.egress_mac.update(sxor(self.mac_enc(mac_secret), fmac_seed))
|
|
|
|
c.macenc.encrypt(toa(macsec.data, 0, RlpHeaderLength), aes)
|
|
|
|
sxor(aes, toa(seed.data, 0, RlpHeaderLength))
|
|
|
|
c.emac.update(aes)
|
|
|
|
burnMem(aes)
|
|
|
|
# frame_mac = self.egress_mac.digest()[:HEADER_LEN]
|
|
|
|
tmpmac = c.emac
|
|
|
|
var frameMac = tmpmac.finish()
|
|
|
|
tmpmac.clear()
|
|
|
|
# return header_ciphertext + header_mac + frame_ciphertext + frame_mac
|
|
|
|
copyMem(addr output[headerMacPos], addr headerMac.data[0], RlpHeaderLength)
|
|
|
|
copyMem(addr output[frameMacPos], addr frameMac.data[0], RlpHeaderLength)
|
2020-04-06 16:24:15 +00:00
|
|
|
ok()
|
2019-02-05 15:40:29 +00:00
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
proc encryptMsg*(msg: openArray[byte], secrets: var SecretState): seq[byte] =
|
2022-11-10 15:32:57 +00:00
|
|
|
doAssert(uint32(msg.len) <= maxUInt24, "RLPx message size exceeds limit")
|
2019-02-05 15:40:29 +00:00
|
|
|
|
2022-11-10 15:32:57 +00:00
|
|
|
var header: RlpxHeader
|
2019-02-05 15:40:29 +00:00
|
|
|
# write the frame size in the first 3 bytes of the header
|
|
|
|
header[0] = byte((msg.len shr 16) and 0xFF)
|
|
|
|
header[1] = byte((msg.len shr 8) and 0xFF)
|
|
|
|
header[2] = byte(msg.len and 0xFF)
|
2019-07-03 16:50:23 +00:00
|
|
|
# This is the [capability-id, context-id] in header-data
|
|
|
|
# While not really used, this is checked in the Parity client.
|
|
|
|
# Same as rlp.encode((0, 0))
|
|
|
|
header[3] = 0xc2
|
|
|
|
header[4] = 0x80
|
|
|
|
header[5] = 0x80
|
|
|
|
|
2022-11-10 15:32:57 +00:00
|
|
|
var res = newSeq[byte](encryptedLength(msg.len))
|
|
|
|
encrypt(secrets, header, msg, res).expect(
|
|
|
|
"always succeeds because we call with correct buffer")
|
|
|
|
res
|
2019-02-05 15:40:29 +00:00
|
|
|
|
|
|
|
proc getBodySize*(a: RlpxHeader): int =
|
|
|
|
(int(a[0]) shl 16) or (int(a[1]) shl 8) or int(a[2])
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
proc decryptHeader*(c: var SecretState, data: openArray[byte],
|
|
|
|
output: var openArray[byte]): RlpxResult[void] =
|
2019-02-05 15:40:29 +00:00
|
|
|
## Decrypts header `data` using SecretState `c` context and store
|
|
|
|
## result into `output`.
|
|
|
|
##
|
|
|
|
## `header` must be exactly `RlpHeaderLength + RlpMacLength` length.
|
|
|
|
## `output` must be at least `RlpHeaderLength` length.
|
|
|
|
var
|
|
|
|
tmpmac: keccak256
|
|
|
|
aes: array[RlpHeaderLength, byte]
|
|
|
|
|
|
|
|
if len(data) != RlpHeaderLength + RlpMacLength:
|
2020-04-06 16:24:15 +00:00
|
|
|
return err(IncompleteError)
|
2019-02-05 15:40:29 +00:00
|
|
|
if len(output) < RlpHeaderLength:
|
2020-04-06 16:24:15 +00:00
|
|
|
return err(IncorrectArgs)
|
2019-02-05 15:40:29 +00:00
|
|
|
# mac_secret = self.ingress_mac.digest()[:HEADER_LEN]
|
|
|
|
tmpmac = c.imac
|
|
|
|
var macsec = tmpmac.finish()
|
|
|
|
# aes = self.mac_enc(mac_secret)[:HEADER_LEN]
|
|
|
|
c.macenc.encrypt(toa(macsec.data, 0, RlpHeaderLength), aes)
|
|
|
|
# self.ingress_mac.update(sxor(aes, header_ciphertext))
|
|
|
|
sxor(aes, toa(data, 0, RlpHeaderLength))
|
|
|
|
c.imac.update(aes)
|
|
|
|
burnMem(aes)
|
|
|
|
# expected_header_mac = self.ingress_mac.digest()[:HEADER_LEN]
|
|
|
|
tmpmac = c.imac
|
|
|
|
var expectMac = tmpmac.finish()
|
|
|
|
# if not bytes_eq(expected_header_mac, header_mac):
|
|
|
|
let headerMacPos = RlpHeaderLength
|
|
|
|
if not equalMem(cast[pointer](unsafeAddr data[headerMacPos]),
|
|
|
|
cast[pointer](addr expectMac.data[0]), RlpMacLength):
|
2020-04-06 16:24:15 +00:00
|
|
|
result = err(IncorrectMac)
|
2019-02-05 15:40:29 +00:00
|
|
|
else:
|
|
|
|
# return self.aes_dec.update(header_ciphertext)
|
|
|
|
c.aesdec.decrypt(toa(data, 0, RlpHeaderLength), output)
|
2020-04-06 16:24:15 +00:00
|
|
|
result = ok()
|
2019-02-05 15:40:29 +00:00
|
|
|
|
2022-02-21 12:50:20 +00:00
|
|
|
proc decryptHeaderAndGetMsgSize*(c: var SecretState,
|
|
|
|
encryptedHeader: openArray[byte],
|
|
|
|
outSize: var int,
|
|
|
|
outHeader: var RlpxHeader): RlpxResult[void] =
|
|
|
|
result = decryptHeader(c, encryptedHeader, outHeader)
|
|
|
|
if result.isOk():
|
|
|
|
outSize = outHeader.getBodySize
|
|
|
|
|
2019-02-05 15:40:29 +00:00
|
|
|
proc decryptHeaderAndGetMsgSize*(c: var SecretState,
|
2021-12-20 12:14:50 +00:00
|
|
|
encryptedHeader: openArray[byte],
|
2020-04-06 16:24:15 +00:00
|
|
|
outSize: var int): RlpxResult[void] =
|
2019-02-05 15:40:29 +00:00
|
|
|
var decryptedHeader: RlpxHeader
|
|
|
|
result = decryptHeader(c, encryptedHeader, decryptedHeader)
|
2020-04-06 16:24:15 +00:00
|
|
|
if result.isOk():
|
2019-02-05 15:40:29 +00:00
|
|
|
outSize = decryptedHeader.getBodySize
|
|
|
|
|
2021-12-20 12:14:50 +00:00
|
|
|
proc decryptBody*(c: var SecretState, data: openArray[byte], bodysize: int,
|
|
|
|
output: var openArray[byte], outlen: var int): RlpxResult[void] =
|
2019-02-05 15:40:29 +00:00
|
|
|
## Decrypts body `data` using SecretState `c` context and store
|
|
|
|
## result into `output`.
|
|
|
|
##
|
|
|
|
## `data` must be at least `roundup16(bodysize) + RlpMacLength` length.
|
|
|
|
## `output` must be at least `roundup16(bodysize)` length.
|
|
|
|
##
|
|
|
|
## On success completion `outlen` will hold actual size of decrypted body.
|
|
|
|
var
|
|
|
|
tmpmac: keccak256
|
|
|
|
aes: array[RlpHeaderLength, byte]
|
|
|
|
outlen = 0
|
|
|
|
let rsize = roundup16(bodysize)
|
|
|
|
if len(data) < rsize + RlpMacLength:
|
2020-04-06 16:24:15 +00:00
|
|
|
return err(IncompleteError)
|
2019-02-05 15:40:29 +00:00
|
|
|
if len(output) < rsize:
|
2020-04-06 16:24:15 +00:00
|
|
|
return err(IncorrectArgs)
|
2019-02-05 15:40:29 +00:00
|
|
|
# self.ingress_mac.update(frame_ciphertext)
|
|
|
|
c.imac.update(toa(data, 0, rsize))
|
|
|
|
tmpmac = c.imac
|
|
|
|
# fmac_seed = self.ingress_mac.digest()[:MAC_LEN]
|
|
|
|
var seed = tmpmac.finish()
|
|
|
|
# self.ingress_mac.update(sxor(self.mac_enc(fmac_seed), fmac_seed))
|
|
|
|
c.macenc.encrypt(toa(seed.data, 0, RlpHeaderLength), aes)
|
|
|
|
sxor(aes, toa(seed.data, 0, RlpHeaderLength))
|
|
|
|
c.imac.update(aes)
|
|
|
|
# expected_frame_mac = self.ingress_mac.digest()[:MAC_LEN]
|
|
|
|
tmpmac = c.imac
|
|
|
|
var expectMac = tmpmac.finish()
|
|
|
|
let bodyMacPos = rsize
|
|
|
|
if not equalMem(cast[pointer](unsafeAddr data[bodyMacPos]),
|
|
|
|
cast[pointer](addr expectMac.data[0]), RlpMacLength):
|
2020-04-06 16:24:15 +00:00
|
|
|
result = err(IncorrectMac)
|
2019-02-05 15:40:29 +00:00
|
|
|
else:
|
|
|
|
c.aesdec.decrypt(toa(data, 0, rsize), output)
|
|
|
|
outlen = bodysize
|
2020-04-06 16:24:15 +00:00
|
|
|
result = ok()
|