Implemented encryption for embedded profile.
This commit is contained in:
@@ -10,11 +10,17 @@ import ./[utils, types]
|
||||
proc generateIV*(): Iv =
|
||||
# Generate a random 98-bit (12-byte) initialization vector for AES-256 GCM mode
|
||||
var iv: Iv
|
||||
if randomBytes(iv) != 12:
|
||||
if randomBytes(iv) != sizeof(Iv):
|
||||
raise newException(CatchableError, "Failed to generate IV.")
|
||||
return iv
|
||||
|
||||
proc encrypt*(key: Key, iv: Iv, data: seq[byte], sequenceNumber: uint32): (seq[byte], AuthenticationTag) =
|
||||
proc generateKey*(): Key =
|
||||
var key: Key
|
||||
if randomBytes(key) != sizeof(Key):
|
||||
raise newException(CatchableError, "Failed to generate IV.")
|
||||
return key
|
||||
|
||||
proc encrypt*(key: Key, iv: Iv, data: seq[byte], sequenceNumber: uint32 = 0): (seq[byte], AuthenticationTag) =
|
||||
|
||||
# Encrypt data using AES-256 GCM
|
||||
var encData = newSeq[byte](data.len)
|
||||
@@ -29,7 +35,7 @@ proc encrypt*(key: Key, iv: Iv, data: seq[byte], sequenceNumber: uint32): (seq[b
|
||||
|
||||
return (encData, tag)
|
||||
|
||||
proc decrypt*(key: Key, iv: Iv, encData: seq[byte], sequenceNumber: uint32): (seq[byte], AuthenticationTag) =
|
||||
proc decrypt*(key: Key, iv: Iv, encData: seq[byte], sequenceNumber: uint32 = 0): (seq[byte], AuthenticationTag) =
|
||||
|
||||
# Decrypt data using AES-256 GCM
|
||||
var data = newSeq[byte](encData.len)
|
||||
@@ -91,10 +97,7 @@ proc wipeKey*(data: var openArray[byte]) =
|
||||
|
||||
# Key pair generation
|
||||
proc generateKeyPair*(): KeyPair =
|
||||
var privateKey: Key
|
||||
if randomBytes(privateKey) != sizeof(Key):
|
||||
raise newException(ValueError, "Failed to generate key.")
|
||||
|
||||
let privateKey = generateKey()
|
||||
return KeyPair(
|
||||
privateKey: privateKey,
|
||||
publicKey: getPublicKey(privateKey)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import parsetoml, strutils, sequtils, random
|
||||
import ./[types, utils]
|
||||
|
||||
import ./types
|
||||
|
||||
proc findKey(profile: Profile, path: string): TomlValueRef =
|
||||
let keys = path.split(".")
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import tables
|
||||
import ./[types, utils]
|
||||
import ./types
|
||||
|
||||
var sequenceTable {.global.}: Table[uint32, uint32]
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import streams, strutils, tables
|
||||
import ./[types, utils, crypto, sequence]
|
||||
import streams, tables
|
||||
import ./[types, utils, crypto]
|
||||
|
||||
#[
|
||||
Packer
|
||||
@@ -129,8 +129,6 @@ proc getArgument*(unpacker: Unpacker): TaskArg =
|
||||
result.data = unpacker.getBytes(8)
|
||||
of BOOL:
|
||||
result.data = unpacker.getBytes(1)
|
||||
else:
|
||||
discard
|
||||
|
||||
proc getDataWithLengthPrefix*(unpacker: Unpacker): string =
|
||||
# Read length of variable-length field
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import prompt
|
||||
import tables
|
||||
import times
|
||||
import streams
|
||||
import parsetoml
|
||||
|
||||
# Custom Binary Task structure
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import strutils, sequtils, strformat
|
||||
import nimcrypto
|
||||
import strutils, nimcrypto
|
||||
|
||||
import ./types
|
||||
|
||||
|
||||
Reference in New Issue
Block a user