SharkSSL C/C++ Reference
TLS, cryptography, certificates, and PSA integration
Encrypt and decrypt with EVP AES-GCM

Use incremental AES-128-GCM or AES-256-GCM to encrypt a message and authenticate its contents. The evp_gcm component wraps native SharkSSL without a BIO or networking dependency. On decryption, keep output private until final tag verification succeeds.

Interface and build

Add compat/openssl/include to the include path and compile src/SharkSslOsslEvpCipher.c, src/SharkSslOsslParams.c, src/SharkSslOsslGcmStream.c and src/SharkSslOsslErr.c with the same SharkSSL configuration as the native SharkSslCrypto.c. The selected native backend objects must also be linked. Shared allocator/atomic definitions live in SharkSslOsslPort.h; the BIO implementation need not be linked.

openssl/evp.h supplies the following prefixed source aliases:

Area Implemented family
Fetch and lifetime EVP_CIPHER_fetch, up_ref, free
Borrowed descriptors EVP_aes_128_gcm(), EVP_aes_256_gcm(), EVP_get_cipherbyname(). Descriptors have process lifetime. Disabled ciphers return NULL.
Classic initialization EVP_CipherInit, EVP_EncryptInit, EVP_DecryptInit and their _ex forms. Pass NULL for ENGINE with the _ex forms.
GCM controls EVP_CIPHER_CTX_ctrl accepts EVP_CTRL_GCM_SET_IVLEN with 12 bytes and EVP_CTRL_GCM_GET_TAG or EVP_CTRL_GCM_SET_TAG with 16 bytes. The AEAD aliases use the same controls.
Context lifetime EVP_CIPHER_CTX_new, reset, free
Properties Cipher/context key length, IV length, block size; cipher name; context get0_cipher
Initialization EVP_CipherInit_ex2, EVP_EncryptInit_ex2, EVP_DecryptInit_ex2
Streaming Cipher/Encrypt/Decrypt Update, Final and Final_ex
Parameters EVP_CIPHER_CTX_set_params, get_params; integer, size_t, octet-string and end constructors

EVP_get_cipherbynid accepts the numeric identifiers for the two supported ciphers. The descriptor and context getters expose the identifier, GCM mode and the supported key, IV and block lengths.

Fetch accepts case-insensitive AES-128-GCM and AES-256-GCM when compiled, a NULL library context, and NULL/empty properties. Other algorithms, custom library contexts, and nonempty property queries fail. This does not implement providers or claim FIPS validation. Contexts retain a fetched cipher reference; the caller may free its own reference after initialization.

This subset fixes nonce length at 12 bytes and authentication tag length at 16 bytes. Set parameters accept ivlen=12, the selected keylen, and a 16-byte tag for decryption. Get parameters return those lengths and the encryption tag after successful Final. Init checks the supplied parameters before reading the key or nonce. Other parameters and sizes fail. Unsupported setters invalidate the current message. No legacy low-level AES interface, padding mode, context copying or parameter discovery is provided. The ERR subset covers fetch, allocation, parameter and operation-state failures under its producer/quiet-return rules. Unsupported functions are undeclared.

Streaming and lifetime

Initialize the cipher, key, and nonce together or in stages. An expected decryption tag supplied during staged initialization survives later key/IV setup. Reinitialization with a NULL cipher retains the selected algorithm and key. Starting another message requires an explicitly supplied nonce. The application must ensure nonce uniqueness under each encryption key; the adapter does not keep a nonce history.

Pass NULL output to Update for AAD, before any nonempty payload update. Payload updates immediately emit exactly the input length. Exact in-place encryption and decryption are supported; partial overlap is outside this subset. A zero-length payload update does not end the AAD phase. Final emits zero payload bytes and generates or verifies the tag. Decrypted Update output is unauthenticated until Final succeeds and must be discarded on failure.

Update/Final do not allocate. The native stream retains one partial block, one keystream block, a counter, GHASH state, and 64-bit byte counts. It limits AAD to 2^61-1 bytes and payload to 2^36-32 bytes per message, checking before processing input. The existing one-shot API retains its own documented bounds.

Native finalization, abort, and errors erase the stream state. EVP contexts retain their key for explicit fresh-nonce reinitialization until reset/free; invalid-operation failures clear retained key material. Objects require external serialization. Cipher reference updates use the same host atomic port hook as BIO; supply a suitable hook for the selected toolchain.

Backend limits

The stream uses the common native encryptBlock, authBlocks and cryptBlocks helpers. Their declarations remain private to the adapter. Use a matching native source version and configuration. Software, AES-NI and VAES backends are supported within this subset; the selected assembly objects and runtime feature checks remain the native library's responsibility.

PSA consumer AES is explicitly rejected. Replacement crypto files without the private helpers cannot link. An alternative backend must supply compatible private helpers; a DLL build must export those helpers to its adapter. These EVP message operations are separate from TLS record processing.