Storage & Security
Hardware Cryptography
SHA-256, AES encryption, HMAC, and hardware random number generation.
14. Hardware Cryptographic Acceleration Engine (Crypto / System.crypto)
KryonOS harnesses the dedicated hardware cryptography accelerators embedded inside the ESP32 silicon alongside mbedTLS. These APIs compute hashes, cryptographic signatures, symmetric cipher blocks, and secure random entropy with hardware speed and zero CPU bit-shifting overhead.
Both the global Crypto object and System.crypto namespace are supported.
14.1 Hardware Hash & HMAC Functions
Crypto.sha256(data)
- Parameters:
data(String) - Plaintext or payload string to hash. - Returns:
String- 64-character lowercase hexadecimal string (256 bits). - Description: Computes SHA-256 hash using the ESP32 hardware SHA engine in silicon.
Crypto.sha512(data)
- Parameters:
data(String) - Plaintext or payload string to hash. - Returns:
String- 128-character lowercase hexadecimal string (512 bits). - Description: Computes high-entropy 512-bit SHA digest using hardware accelerators.
Crypto.hmacSha256(key, message)
- Parameters:
key(String) - Secret HMAC key.message(String) - Payload or data to authenticate.
- Returns:
String- 64-character lowercase hexadecimal string. - Description: Generates hardware-assisted HMAC-SHA256 signature for API tokens, AWS signature v4, webhooks, and JWT verification.
14.2 Hardware Symmetric Ciphers (AES-128 / AES-256)
Crypto.aesEncrypt(plainText, keyHex, ivHex)
- Parameters:
plainText(String) - UTF-8 plaintext string to encrypt.keyHex(String) - Hexadecimal key string: must be strictly 32 characters (16 bytes → AES-128) or 64 characters (32 bytes → AES-256).ivHex(String) - Hexadecimal IV string: must be strictly 32 characters (16 bytes / 128-bit Initialization Vector).
- Returns:
String- Base64-encoded ciphertext string, or""if parameters are invalid. - Description: Performs hardware-accelerated AES-CBC encryption with automatic PKCS#7 block alignment padding.
Crypto.aesDecrypt(base64Cipher, keyHex, ivHex)
- Parameters:
base64Cipher(String) - Base64-encoded ciphertext string.keyHex(String) - Hexadecimal key string (32 or 64 hex chars).ivHex(String) - Hexadecimal IV string (32 hex chars).
- Returns:
String- Decrypted UTF-8 plaintext string, or""if authentication/padding verification fails. - Description: Decodes Base64, verifies 16-byte block alignment, executes hardware AES-CBC decryption, and strictly validates PKCS#7 unpadding to protect against padding oracle vulnerabilities.
14.3 True Random Number Generator (TRNG)
Crypto.randomBytes(length)
- Parameters:
length(Integer) - Number of random bytes requested (1..4096). - Returns:
String- Lowercase hexadecimal string of lengthlength * 2. - Description: Reads cryptographically secure random bytes directly from the ESP32 silicon True Random Number Generator (TRNG) driven by internal RF thermal noise.
14.4 Asymmetric Cryptography (RSA Verification)
Crypto.rsaVerify(pubKeyPem, message, sigBase64)
- Parameters:
pubKeyPem(String) - RSA Public Key in standard PEM format ("-----BEGIN PUBLIC KEY-----...").message(String) - Original message string that was signed.sigBase64(String) - Base64-encoded RSA signature.
- Returns:
Boolean-trueif the RSA PKCS#1 v1.5 (SHA-256) signature is authentic,falseotherwise. - Description: Verifies RSA digital signatures using hardware MPI (Modular Exponentiation Accelerator) in silicon.
14.5 Practical Cryptographic Examples
1. AES-256 Symmetric Encryption & Decryption Roundtrip
var key = "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"; // 256-bit key
var iv = "a0b0c0d0e0f0102030405060708090a0"; // 128-bit IV
var message = "KryonOS Top-Secret Config Payload";
// Encrypt
var cipher = Crypto.aesEncrypt(message, key, iv);
console.log("Encrypted Base64:", cipher);
// Decrypt
var original = Crypto.aesDecrypt(cipher, key, iv);
console.log("Decrypted Text:", original);
2. AWS / Webhook HMAC-SHA256 Signature Generation
var secretKey = "my_super_secret_webhook_key";
var payload = JSON.stringify({ deviceId: "esp32-s3-node-01", temp: 24.5 });
var signature = Crypto.hmacSha256(secretKey, payload);
console.log("X-Hub-Signature-256:", signature);
3. Cryptographically Secure Token Generation
// Generate 32 bytes (256 bits) of hardware silicon entropy
var sessionToken = Crypto.randomBytes(32);
console.log("Secure Session Token:", sessionToken);