Skip to content
Home » Embedded Systems » Encryption and Authentication Techniques for Embedded Systems

Encryption and Authentication Techniques for Embedded Systems

Encryption and Authentication featured image with dark red background, SECURITY badge, AES icon in red circle, and AES ECC TLS for Embedded Systems subtitle by nerdyelectronics.com
Embedded Systems Learning Path
Part 109 of 129 — View Full Path →

KEY TAKEAWAYS

  • Symmetric encryption (AES) is fast and suitable for data protection; asymmetric (RSA, ECC) is used for key exchange
  • TLS/SSL provides encrypted communication between IoT devices and cloud services
  • X.509 certificates enable mutual authentication between devices and servers
  • Hash functions (SHA-256) verify data integrity and are used in digital signatures and secure boot

Why Encryption and Authentication Matter

Embedded and IoT devices collect sensitive data (health metrics, location, industrial readings) and control physical systems (door locks, motors, valves). Without proper encryption and authentication:
  • Anyone can read your sensor data in transit
  • An attacker can impersonate your device or your server
  • Unauthorized users can send commands to your device
  • Credentials stored on the device can be extracted
Encryption protects data confidentiality. Authentication verifies identity. Both are essential for any connected embedded system.

Symmetric Encryption

How It Works

Symmetric encryption uses the same key for both encryption and decryption. Both the sender and receiver must have the same secret key.
Symmetric encryption: the same key encrypts and decryptsTwo rows. Sender: the plaintext and the secret key go into Encrypt, which produces the ciphertext. Receiver: the ciphertext and the same secret key go into Decrypt, which produces the plaintext again. An example below uses the 128-bit key “mysecretkey12345”, the plaintext “Temperature: 23.5C” and the ciphertext “a7f2b9c1e8d4…”, which is unreadable without the key.Symmetric encryption: one shared secret keySenderPlaintextEncryptCiphertextKeythe same key on both sidesReceiverCiphertextDecryptPlaintextKeyExampleKey: “mysecretkey12345” (128-bit)Plaintext: “Temperature: 23.5C”Ciphertext: “a7f2b9c1e8d4…” (unreadable without key)
Figure: Symmetric encryption uses the same secret key to encrypt and to decrypt.

Common Symmetric Algorithms

AlgorithmKey SizeBlock SizeStatus
AES-128128 bits128 bitsStandard, recommended for embedded
AES-256256 bits128 bitsHigher security, slightly slower
ChaCha20256 bitsStreamFast in software (no hardware AES)
DES/3DES56/168 bits64 bitsDeprecated, do NOT use
AES (Advanced Encryption Standard) is the most widely used. Many modern microcontrollers (ESP32, STM32, nRF52) have hardware AES accelerators that encrypt data much faster and with less power than software implementations.

AES Example on ESP32

#include "mbedtls/aes.h"

void encrypt_data(const uint8_t *key, const uint8_t *input,
                  uint8_t *output, size_t length) {
    mbedtls_aes_context aes;
    mbedtls_aes_init(&aes);

    // Set encryption key (128-bit)
    mbedtls_aes_setkey_enc(&aes, key, 128);

    // Encrypt each 16-byte block
    for (size_t i = 0; i < length; i += 16) {
        mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_ENCRYPT,
                               input + i, output + i);
    }

    mbedtls_aes_free(&aes);
}
Important: Never use ECB (Electronic Codebook) mode for real data, as it leaks patterns. Use CBC, CTR, or GCM mode instead. GCM is preferred because it provides both encryption and authentication (AEAD).

Asymmetric Encryption (Public-Key Cryptography)

How It Works

Asymmetric encryption uses a key pair: a public key (shared freely) and a private key (kept secret). Data encrypted with the public key can only be decrypted with the private key.
Asymmetric cryptography: a key pair, encryption and digital signaturesThree groups. Key generation: generate a key pair, giving a public key and a private key. Encryption: the sender combines the plaintext with the receiver’s public key to make the ciphertext; the receiver combines the ciphertext with the receiver’s private key to get the plaintext. Digital signature: the signer combines the hash of the data with the signer’s private key to make the signature; the verifier combines the hash of the data with the signer’s public key and gets Valid or Invalid. Public keys are drawn green, private keys red.Key generationGenerate key pairPublic key+Private keyEncryptionSenderPlaintext+Receiver’s PUBLIC keyEncryptCiphertextReceiverCiphertext+Receiver’s PRIVATE keyDecryptPlaintextDigital signatureSignerHash(data)+Signer’s PRIVATE keySignSignatureVerifierHash(data)+Signer’s PUBLIC keyVerifyValid or Invalid
Figure: A key pair, encryption with the receiver’s keys, and a signature with the signer’s keys.

Common Asymmetric Algorithms

AlgorithmKey SizeUse CaseEmbedded Suitability
RSA2048-4096 bitsEncryption, signaturesSlow, large keys (not ideal for constrained devices)
ECDSA256 bitsDigital signaturesFast, small keys, ideal for embedded
ECDH256 bitsKey exchangeFast, small keys
Ed25519256 bitsDigital signaturesVery fast, simple, modern
ECC (Elliptic Curve Cryptography) is preferred for embedded systems because it provides equivalent security to RSA with much smaller keys and faster computation. A 256-bit ECC key provides roughly the same security as a 3072-bit RSA key.

Hashing

A hash function takes any input and produces a fixed-size output (the hash or digest). Hashes are one-way: you cannot recover the original data from the hash.
SHA-256("Hello") → "185f8db32271fe25f561a6fc938b2e26..."
SHA-256("hello") → "2cf24dba5fb0a30e26e83b2ac5b9e29e..."
  (completely different output for a tiny change)
Uses in embedded systems:
  • Verifying firmware integrity (secure boot)
  • Password storage (store hash, not plaintext)
  • Data integrity checks (detect corruption)
  • HMAC (Hash-based Message Authentication Code) for message authentication
AlgorithmOutput SizeStatus
SHA-256256 bits (32 bytes)Recommended standard
SHA-384/512384/512 bitsHigher security, more computation
MD5128 bitsBroken, do NOT use for security
SHA-1160 bitsDeprecated, do NOT use for security

TLS (Transport Layer Security)

TLS is the protocol that puts the “S” in HTTPS. It provides encrypted, authenticated communication between your device and a server. TLS combines symmetric encryption, asymmetric encryption, and hashing into a complete secure communication channel.

TLS Handshake (Simplified)

TLS 1.2 handshake with an ECDHE key exchangeA sequence diagram with two lifelines, Device on the left and Server on the right. The device sends ClientHello with its supported ciphers. The server answers with ServerHello (the chosen cipher), Certificate (the server’s X.509 certificate), ServerKeyExchange (its ECDHE key) and ServerHelloDone. The device checks the certificate against its trusted CA certificate. The device then sends ClientKeyExchange (its ECDHE key) and ChangeCipherSpec with Finished; the server replies with ChangeCipherSpec and Finished. Both sides now have the shared secret, and all further data is encrypted with AES: encrypted application data flows in both directions.DeviceServerClientHellosupported ciphersServerHellochosen cipherCertificateserver’s X.509 certificateServerKeyExchangeECDHE keyServerHelloDoneDevice checks the certificateagainst its trusted CA certificateClientKeyExchangeECDHE keyChangeCipherSpec, FinishedChangeCipherSpec, FinishedBoth sides now have the shared secret.All further data is encrypted with AESEncrypted application data
Figure: The TLS 1.2 handshake with an ECDHE key exchange, as seen between a device and a server.
This is the TLS 1.2 full handshake (RFC 5246, section 7.3) with an ECDHE key exchange. The two Finished messages are the first ones protected with the new session keys. For mutual TLS, the server also sends a CertificateRequest, and the device answers with its own Certificate plus a CertificateVerify signed with its private key. TLS 1.3 (RFC 8446) cuts this to one round trip: the device sends its key share in the ClientHello, and everything after the ServerHello is encrypted.

TLS on Embedded Devices

Most embedded TLS implementations use mbedTLS (formerly PolarSSL). It is lightweight and runs on microcontrollers with as little as 64KB ROM and 16KB RAM.
// ESP32 HTTPS request with TLS (ESP-IDF)
esp_http_client_config_t config = {
    .url = "https://api.example.com/data",
    .cert_pem = server_root_ca_cert,  // CA certificate to verify server
    .client_cert_pem = client_cert,   // Optional: mutual TLS
    .client_key_pem = client_key,
};

esp_http_client_handle_t client = esp_http_client_init(&config);
esp_http_client_perform(client);
esp_http_client_cleanup(client);

Mutual TLS (mTLS)

Standard TLS only verifies the server. Mutual TLS also verifies the device. The device presents its own certificate, proving its identity to the server. This is the standard authentication method for AWS IoT, Azure IoT, and Google Cloud IoT.
Standard TLS:   Device verifies Server (one-way)
Mutual TLS:     Device verifies Server AND Server verifies Device (two-way)

Authentication Methods

1. Pre-Shared Key (PSK)

A secret key is loaded onto the device during manufacturing. Simple but does not scale well (each device needs a unique key).

2. Certificate-Based (X.509)

Each device has a unique certificate signed by a Certificate Authority (CA). The server verifies the certificate to authenticate the device. This is the most secure and scalable approach.
Giving a device its certificate (manufacturing) and using it (runtime)Manufacturing, four steps in order: 1 generate a key pair on the device or externally; 2 create a Certificate Signing Request (CSR); 3 the CA signs the CSR, producing the device certificate; 4 store the certificate and the private key on the device. Runtime: the device presents its certificate, the server verifies it with the CA, and the device is authenticated.Manufacturing1Generate key pair on the device(or externally)2Create a Certificate SigningRequest (CSR)3The CA signs the CSR,giving the device certificate4Store the certificate and theprivate key on the deviceRuntimeDevice presents its certificateServer verifies it with the CAAuthenticated
Figure: Manufacturing gives the device a certificate; at runtime the device presents it and the server checks it with the CA.

3. Token-Based (JWT, API Key)

The device uses a token (like a JSON Web Token) to authenticate with cloud services. Simpler than certificates but tokens can expire and need refreshing.

Storing Secrets on Embedded Devices

Where you store keys and certificates matters:
MethodSecurityNotes
Hardcoded in source codeVery poorAnyone with the binary can extract them
Separate flash partitionLowBetter but still readable with physical access
Encrypted flashMediumProtected if flash encryption is enabled
Secure element (ATECC608)HighHardware crypto chip, keys never leave the chip
TrustZone secure storageHighHardware-isolated secure world (Cortex-M33+)
For production IoT devices, consider using a secure element like the Microchip ATECC608A/B. It stores private keys in tamper-resistant hardware and performs crypto operations internally. The private key never leaves the chip, making extraction virtually impossible.

Security Best Practices

  1. Use TLS for all network communication. Never send data in plaintext.
  2. Use AES-GCM or ChaCha20-Poly1305 for symmetric encryption (they provide both encryption and integrity).
  3. Use ECC (ECDSA/Ed25519) instead of RSA for embedded devices. Smaller keys, faster operations.
  4. Never hardcode secrets in source code. Use secure storage or a secure element.
  5. Use unique credentials per device. If one device is compromised, only that device is affected.
  6. Rotate keys and certificates. Plan for how to update credentials if they are compromised.
  7. Validate all input. Never trust data from external sources without validation.
  8. Keep crypto libraries updated. Vulnerabilities in crypto implementations are regularly discovered and patched.

Summary

Security in embedded systems is built on three pillars: encryption (protecting data), authentication (verifying identity), and integrity (detecting tampering). Use AES for symmetric encryption, ECC for asymmetric operations, SHA-256 for hashing, and TLS for secure communication. Store secrets in hardware secure elements when possible. Every connected device should use TLS with at minimum server authentication, and production IoT devices should implement mutual TLS with per-device certificates. Security is not optional; it is a core requirement for any device that connects to a network.

Leave a Reply

Your email address will not be published. Required fields are marked *