|
SharkSSL C/C++ Reference
TLS, cryptography, certificates, and PSA integration
|
Use the evp_hash component to hash data, authenticate messages, derive keys or request random bytes through native SharkSSL. It does not require sockets, BIO objects, or a linked OpenSSL library. The source aliases use SharkSslOssl_ symbols; mixing these headers with system OpenSSL headers in one translation unit is unsupported.
The crypto example shows SHA-256, HMAC-SHA256 and HKDF-SHA256 with commented setup and cleanup. For public-key signatures, use the separate PEM key guide.
| Family | Implemented subset |
|---|---|
| SHA-2 | EVP_MD_fetch/up_ref/free, size/block-size/name getters; EVP_MD_CTX_new/free/reset/copy_ex/get0_md; EVP_DigestInit_ex2/Update/Final_ex |
| HMAC | EVP_MAC_fetch/up_ref/free/get0_name, context new/free/dup and size/block-size getters, set_params, init/update/final |
| HKDF | OpenSSL 3 EVP_KDF and the EVP_PKEY_HKDF controls for extract, expand and combined derivation |
| Password derivation | PKCS5_PBKDF2_HMAC with the native PBKDF2 feature enabled |
| Sensitive buffers | CRYPTO_memcmp and OPENSSL_cleanse from <openssl/crypto.h> |
| RNG | RAND_bytes, RAND_bytes_ex |
| Typed parameters | UTF-8 strings, integer/size_t values, octet strings and end markers |
Digests are SHA-256, SHA-384 and SHA-512 when enabled in the native build. Fetch recognizes SHA256, SHA-256, SHA2-256, and corresponding 384/512 names without case sensitivity. HMAC and HKDF support those compiled digests. Disabled or other algorithms fail explicitly. Enable SHA-512 explicitly when the application needs it.
Fetch accepts a NULL library context and NULL/empty properties. Custom library contexts, nonempty property queries, providers, and parameter discovery are not implemented. The ERR subset defines fetch, context, parameter, operation-state and RNG diagnostics, including intentional quiet returns. SHA-2 initialization accepts no nonempty parameter list. Unsupported MAC/KDF parameters fail and invalidate the affected operation.
For a borrowed digest descriptor use EVP_sha256(), EVP_sha384(), EVP_sha512() or EVP_get_digestbyname(). The descriptor lives for the process lifetime. A disabled digest returns NULL. Classic streaming calls include EVP_DigestInit, EVP_DigestInit_ex and EVP_DigestFinal. EVP_DigestFinal resets the context, while EVP_DigestFinal_ex retains the algorithm for reuse. EVP_Digest and EVP_Q_digest compute a one-shot digest. Pass NULL for the ENGINE argument.
Include <openssl/hmac.h> for one-shot HMAC. Pass writable output storage of at least the digest size. A NULL output selects a shared static buffer which a subsequent call can overwrite. Concurrent callers must supply their own buffers.
For streaming HMAC, allocate with HMAC_CTX_new, select a compiled SHA-2 digest and key with HMAC_Init_ex, then call HMAC_Update and HMAC_Final. Use HMAC_CTX_copy to branch an in-progress message. The context retains the digest and the keyed initial state. A NULL key and NULL digest restart with the previous key and algorithm. To select an empty key, pass a non-NULL pointer with length zero. HMAC_CTX_reset clears both states, and HMAC_CTX_free clears them before releasing storage. Pass NULL for ENGINE. The SHA-2 digest descriptors also support lookup by numeric identifier and the classic size, block size and type getters.
Digest updates accept size_t lengths and split native calls at the U32 bound. The adapter caps messages at 2^61-1 bytes for all three digests. This is a declared subset limit; SHA-384/512 have larger theoretical bounds. Final clears the native context; the selected algorithm remains for reinitialization with a NULL digest. Copying active contexts is supported for the native backend.
HMAC parameters are digest, key, and empty properties. Supply a key for each new operation, either directly to init or through a newly supplied key parameter. Empty keys are supported, using a non-NULL pointer with length zero or an explicit zero-length key parameter. Key parameters and direct keys are copied into owned storage. Context duplication copies both prepared key data and active native state; copies have independent lifetimes.
For EVP_MAC, NULL-key reuse after Final is unsupported and fails. Resubmit the key, directly or through parameters, for each operation. This differs from the classic HMAC_Init_ex reuse behavior described above.
Long HMAC keys are hashed incrementally before the native constructor, avoiding its U16 key-length limit. Updates account for the inner pad in the 2^61-1-byte hash limit. Final supports a non-consuming size query and a retry after an undersized output buffer. It clears native state; the owned key is cleared on replacement, error, or context free. Digest/HMAC primitive errors propagate without returning a partial digest as valid output.
Applications using the OpenSSL 1.1 interface can include <openssl/kdf.h>, create an EVP_PKEY_CTX with EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL), and call EVP_PKEY_derive_init. Set the digest, key, optional salt and info before calling EVP_PKEY_derive. Key and salt setters copy and replace their inputs. Each EVP_PKEY_CTX_add1_hkdf_info appends to the previous bytes. The 1024-byte total info limit applies to both interfaces. The hash component alone supplies these calls.
The default mode extracts and expands. For extraction alone select EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY and use exactly one digest of output. A NULL output pointer queries its size. For expansion choose the desired output length yourself. Calling EVP_PKEY_derive_init again clears the previous inputs. Release the context with EVP_PKEY_CTX_free.
The supported parameters are digest, key, salt, info, mode, and empty properties. Mode accepts the standard integer values or strings for EXTRACT_AND_EXPAND, EXTRACT_ONLY and EXPAND_ONLY. The default is combined mode. Key and salt are copied; missing salt and info are empty. An explicit empty key is distinct from never setting a key.
Multiple info entries within one parameter call concatenate in order, up to 1024 bytes total. Supplying info in a later call replaces the previous value; omitting it retains the value. Derive retains the configured parameters for another call. Reset/free and parameter/native failures clear retained secrets.
Extract-only output must equal the selected digest size. Other modes accept 1 through 255 times that size. Invalid sizes fail before accessing output. If a native operation fails during derivation, the requested output is erased, including blocks produced before the failure. Intermediate PRK and HMAC blocks are always cleared. The implementation follows RFC 5869 using native HMAC, with no imported third-party implementation code.
PKCS5_PBKDF2_HMAC calls the native sharkssl_PEM_PBKDF2Ex implementation. Link that implementation and enable SHARKSSL_ENABLE_PEM_API and SHARKSSL_ENABLE_ENCRYPTED_PKCS8_SUPPORT in the native and adapter builds. The SDK disables encrypted PKCS#8 by default. In that configuration the call returns zero and raises an unsupported-operation error. Applications that only use HKDF can leave these options off.
Supply explicit byte counts for binary passwords and salts. A password length of -1 selects a NUL-terminated string. A NULL password represents an empty password. The native interface accepts up to 65,535 password bytes and derives 1 through 65,535 output bytes. Use separate output storage, a positive iteration count and a compiled SHA-2 digest. A NULL salt requires length zero.
CRYPTO_memcmp examines all bytes for the specified length and returns zero for equality. OPENSSL_cleanse clears the specified byte range through the native erasure routine. Both are available in hash-only builds.
The current wrapper uses SharkSSL's global native DRBG. The application must seed it through sharkssl_entropyEx before requesting random data or starting threads that use SharkSSL. First initialization requires at least 48 suitable entropy bytes; later reseeding requires at least 32. Length alone does not establish entropy quality. An application that already initializes SharkSSL can reuse that startup.
The wrapper does not collect entropy or reseed automatically. An unseeded DRBG or native reseed-required/error result returns failure. Arbitrary output sizes are adapted to the native multiple-of-four/U16 call contract. Small tails use a cleared four-byte temporary. A failure erases the entire requested output, including any previously generated prefix. Zero-byte requests succeed without invoking the native generator.
RAND_bytes_ex accepts only a NULL library context and requested strength through 256 bits, subject to correctly seeded native Hash_DRBG. Unsupported strength/context and negative integer sizes fail. This subset does not expose RAND_priv_bytes: there is no separate private DRBG instance. Native mutex configuration and startup ordering remain the application's responsibility.
Seeding and reseeding remain application-owned on every platform. The adapter returns failure when the native generator is unseeded or needs reseeding. It adds no automatic OS entropy collection or startup callback.
Compile src/SharkSslOsslHash.c, src/SharkSslOsslEvpHash.c, src/SharkSslOsslEvpKdf.c, src/SharkSslOsslRand.c and src/SharkSslOsslParams.c plus src/SharkSslOsslErr.c alongside native SharkSslCrypto.c, with matching config. Select only the wrapper objects needed by the application. Shared allocation and atomic hooks live in SharkSslOsslPort.h; no BIO implementation is required. PSA consumer builds are rejected: the wrapper depends on native context copy, abort and ownership behavior. Use a matching native implementation; a replacement backend must provide those same operations.
References: digest API, MAC API, HKDF parameters, RAND API, and RFC 5869.