SharkSSL C/C++ Reference
TLS, cryptography, certificates, and PSA integration
PEM keys and message signatures

Use the pkey component to load a software key and sign or verify a message through OpenSSL-style APIs. It supports RSA PKCS#1 v1.5, RSA-PSS and DER-encoded ECDSA with the algorithms enabled in your native SharkSSL build.

Load a key and sign a message

Select the optional pkey component to sign messages with a software private key or verify them with a public key. Seed native SharkSSL before signing. Use the same component mask when compiling the adapter and the application.

  1. Wrap the PEM bytes in a memory BIO with BIO_new_mem_buf. Include <openssl/pem.h> and call PEM_read_bio_PrivateKey or PEM_read_bio_PUBKEY. The reader consumes one complete matching object, including any preceding text, within a 64 KiB input limit. The next object stays in the BIO. On success the caller owns one EVP_PKEY* and can free the BIO. If an existing key is supplied through EVP_PKEY**, initialize the pointer to NULL or a valid owned reference. Failure preserves the previous key.
  2. Create an EVP_MD_CTX* and call EVP_DigestSignInit or EVP_DigestVerifyInit with a compiled SHA-2 digest, NULL for ENGINE and the key. The digest context retains the key. An optional EVP_PKEY_CTX** receives a borrowed operation pointer valid until reset or reinitialization.
  3. Pass message bytes to EVP_DigestSignUpdate. Call EVP_DigestSignFinal with NULL output to get the required capacity in bytes, then call it again with that capacity and a writable signature buffer. The returned length is the actual signature size. The one-shot spelling is EVP_DigestSign. Verification uses the corresponding Verify calls. Accept only a return value of 1. Free the context with EVP_MD_CTX_free and the caller's key reference with EVP_PKEY_free.

The readers accept unencrypted PKCS#8 or traditional RSA/EC private keys and SubjectPublicKeyInfo public keys. Supported signatures use RSA PKCS#1 v1.5, RSA-PSS or DER-encoded ECDSA. Password callbacks, provider selection and hardware key handles are outside this component. The native build selects the available algorithms and curves. OpenSSL message signature API

Inspect a key and choose it after hashing

Use EVP_PKEY_is_a(key, "RSA") or EVP_PKEY_is_a(key, "EC") to select an operation for an imported key. The adapter recognizes the OpenSSL algorithm names case-insensitively and accepts their object identifiers. EVP_PKEY_get0_type_name returns a borrowed const char*. EVP_PKEY_get_id returns the numeric type. EVP_PKEY_get_bits returns the RSA modulus or EC order length in bits. Use EVP_PKEY_get_size for signature buffer capacity in bytes. When SHA-256 is enabled, EVP_PKEY_get_default_digest_nid(key, &nid) returns 1 and writes NID_sha256 to the caller's int.

When the key becomes available after hashing begins, use EVP_SignInit_ex and EVP_SignUpdate with a digest context. Allocate at least EVP_PKEY_size(key) bytes, then call EVP_SignFinal(ctx, sig, &len, key). Its unsigned int* length is output only. The adapter signs with RSA PKCS#1 v1.5 or ECDSA. EVP_VerifyInit_ex, EVP_VerifyUpdate and EVP_VerifyFinal verify the same message with the public key. Finalizing uses a copy of the digest, so you can append bytes and sign or verify the longer message. The OpenSSL 3 finalizers EVP_SignFinal_ex and EVP_VerifyFinal_ex accept a NULL library context and a NULL or empty property query. OpenSSL signing contract · OpenSSL verification contract

RSA-PSS message and digest signatures

Use a generic RSA key and include <openssl/rsa.h>. RSA signatures default to PKCS#1 v1.5. To sign or verify a message with PSS, request the borrowed EVP_PKEY_CTX* at digest initialization and pass it to EVP_PKEY_CTX_set_rsa_padding(op, RSA_PKCS1_PSS_PADDING) before setting the salt or mask options. Check each control for a positive return. PSS signing requires a native RSA modulus at least 256 bytes wide. Import of keys encoded with the restricted RSASSA-PSS key algorithm is outside this component.

The optional EVP_PKEY_CTX_set_rsa_mgf1_md(op, md) selects the SHA-2 digest used by the MGF1 mask generation function. Its default is the message digest. The adapter retains the algorithm choice, so the caller can release a fetched EVP_MD* descriptor after setting it. The corresponding getters report the padding, salt policy and borrowed digest descriptor.

Salt policy passed as an int to EVP_PKEY_CTX_set_rsa_pss_saltlen Signing Verification
Nonnegative byte count, including zero Use that many bytes. Signing fails if the encoding cannot fit. Require that exact length.
RSA_PSS_SALTLEN_DIGEST Use the message digest length. Require the digest length.
RSA_PSS_SALTLEN_MAX Use the largest salt the encoding permits. Require that maximum.
RSA_PSS_SALTLEN_AUTO (also RSA_PSS_SALTLEN_MAX_SIGN) Use the maximum. Discover the salt length.
RSA_PSS_SALTLEN_AUTO_DIGEST_MAX Use the smaller of the maximum and the digest length. Discover the salt length.

The adapter defaults to AUTO for compatibility with OpenSSL 3.0.2. OpenSSL 3.5 defaults to AUTO_DIGEST_MAX. Set a policy explicitly when another implementation must enforce a particular salt length. OpenSSL RSA controls · OpenSSL 3.5 default

For an already computed RSA or EC digest, create an owned context with EVP_PKEY_CTX_new(key, NULL) or EVP_PKEY_CTX_new_from_pkey(NULL, key, NULL), then call EVP_PKEY_sign_init or EVP_PKEY_verify_init. Select the compiled SHA-2 digest with EVP_PKEY_CTX_set_signature_md. Set the RSA controls above when using PSS. For EC the signature uses DER encoding. Pass the exact digest bytes and length to EVP_PKEY_sign or EVP_PKEY_verify. For signing, a NULL output returns the required capacity through the size_t* argument. Verification succeeds only on a return value of 1. The context retains the key. EVP_PKEY_CTX_get0_pkey borrows that key. EVP_MD_CTX_get_pkey_ctx borrows the active message-signature operation or returns NULL for a plain digest. Free only an owned context with EVP_PKEY_CTX_free. OpenSSL prehashed signature API

Build requirements

Select pkey, including its bio and evp_hash dependencies, and compile src/SharkSslOsslPkey.c with the selected sources. Native SharkSSL must enable the PEM API and the RSA or ECDSA signature APIs you use. RSA-PSS also requires SHARKSSL_ENABLE_RSASSA_PSS. Initialize the native RNG before signing.

These loaders operate on software keys. Encrypted private keys, password callbacks, hardware key handles and OpenSSL provider selection are outside this component. Use TLS credential loaders to configure a TLS identity; importing an EVP_PKEY does not install it into an SSL_CTX.