From f766cb39b1a44a909463e96e018a695e5638cdb8 Mon Sep 17 00:00:00 2001 From: Csaba Kiraly Date: Fri, 2 Jun 2023 19:03:57 +0200 Subject: [PATCH] encoding: introducing type cipher=aes128 Introducing the cipher type to ease changing cipher. No functional change Signed-off-by: Csaba Kiraly --- codexdht/private/eth/p2p/discoveryv5/encoding.nim | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/codexdht/private/eth/p2p/discoveryv5/encoding.nim b/codexdht/private/eth/p2p/discoveryv5/encoding.nim index 2520b28..7fd18e7 100644 --- a/codexdht/private/eth/p2p/discoveryv5/encoding.nim +++ b/codexdht/private/eth/p2p/discoveryv5/encoding.nim @@ -40,6 +40,9 @@ declareCounter discovery_session_decrypt_failures, "Session decrypt failures" logScope: topics = "discv5" +type + cipher = aes128 + const version: uint16 = 1 idSignatureText = "discovery v5 identity proof" @@ -162,7 +165,7 @@ proc deriveKeys*(n1, n2: NodeId, priv: PrivateKey, pub: PublicKey, ok secrets proc encryptGCM*(key: AesKey, nonce, pt, authData: openArray[byte]): seq[byte] = - var ectx: GCM[aes128] + var ectx: GCM[cipher] ectx.init(key, nonce, authData) result = newSeq[byte](pt.len + gcmTagSize) ectx.encrypt(pt, result) @@ -175,7 +178,7 @@ proc decryptGCM*(key: AesKey, nonce, ct, authData: openArray[byte]): debug "cipher is missing tag", len = ct.len return - var dctx: GCM[aes128] + var dctx: GCM[cipher] dctx.init(key, nonce, authData) var res = newSeq[byte](ct.len - gcmTagSize) var tag: array[gcmTagSize, byte] @@ -189,7 +192,7 @@ proc decryptGCM*(key: AesKey, nonce, ct, authData: openArray[byte]): return some(res) proc encryptHeader*(id: NodeId, iv, header: openArray[byte]): seq[byte] = - var ectx: CTR[aes128] + var ectx: CTR[cipher] ectx.init(id.toByteArrayBE().toOpenArray(0, 15), iv) result = newSeq[byte](header.len) ectx.encrypt(header, result) @@ -374,7 +377,7 @@ proc decodeHeader*(id: NodeId, iv, maskedHeader: openArray[byte]): DecodeResult[(StaticHeader, seq[byte])] = # No need to check staticHeader size as that is included in minimum packet # size check in decodePacket - var ectx: CTR[aes128] + var ectx: CTR[cipher] ectx.init(id.toByteArrayBE().toOpenArray(0, aesKeySize - 1), iv) # Decrypt static-header part of the header var staticHeader = newSeq[byte](staticHeaderSize)