/* * ____ _________ __ _ * / __ \___ ____ _/ /_ __(_)___ ___ ___ / / ____ ____ _(_)____ * / /_/ / _ \/ __ `/ / / / / / __ `__ \/ _ \/ / / __ \/ __ `/ / ___/ * / _, _/ __/ /_/ / / / / / / / / / / / __/ /___/ /_/ / /_/ / / /__ * /_/ |_|\___/\__,_/_/ /_/ /_/_/ /_/ /_/\___/_____/\____/\__, /_/\___/ * /____/ * * SharkSSL Embedded SSL/TLS Stack **************************************************************************** * PROGRAM MODULE * Modern EVP example. * * $Id: SharkSslOsslCryptoExample.c 6928 2026-11-25 23:04:29Z wini $ * * COPYRIGHT: Real Time Logic LLC, 2026 * * This software is copyrighted by and is the sole property of Real * Time Logic LLC. All rights, title, ownership, or other interests in * the software remain the property of Real Time Logic LLC. This * software may only be used in accordance with the terms and * conditions stipulated in the corresponding license agreement under * which the software has been supplied. Any unauthorized use, * duplication, transmission, distribution, or disclosure of this * software is expressly forbidden. * * This Copyright notice may not be removed or modified without prior * written consent of Real Time Logic LLC. * * Real Time Logic LLC. reserves the right to modify this software * without notice. * * http://www.realtimelogic.com * http://www.sharkssl.com **************************************************************************** * */ /* [crypto-example] */ #include "SharkSslOsslExample.h" #include #include #include #include int main(void) { const unsigned char message[] = "abc"; /* Demonstration inputs only. Supply your application's key material. */ unsigned char key[] = "example key"; unsigned char salt[] = "example salt"; unsigned char info[] = "example context"; char digestName[] = "SHA256"; unsigned char output[32]; unsigned int digestSize = 0; size_t macSize = 0; OSSL_PARAM params[5]; EVP_MD* digest = NULL; EVP_MD_CTX* digestCtx = NULL; EVP_MAC* mac = NULL; EVP_MAC_CTX* macCtx = NULL; EVP_KDF* kdf = NULL; EVP_KDF_CTX* kdfCtx = NULL; const char* operation = "SHA-256"; int result = 1; /* Fetch returns an owned descriptor; its context has a separate lifetime. */ digest = EVP_MD_fetch(NULL, digestName, NULL); digestCtx = EVP_MD_CTX_new(); if (!digest || !digestCtx || !EVP_DigestInit_ex2(digestCtx, digest, NULL) || !EVP_DigestUpdate(digestCtx, message, sizeof(message) - 1) || !EVP_DigestFinal_ex(digestCtx, output, &digestSize) || digestSize != sizeof(output)) { goto done; } SharkSslOsslExample_hex("SHA256", output, digestSize); operation = "HMAC-SHA256"; mac = EVP_MAC_fetch(NULL, "HMAC", NULL); macCtx = mac ? EVP_MAC_CTX_new(mac) : NULL; /* Each parameter list ends with an end marker. Zero selects a C string. */ params[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, digestName, 0); params[1] = OSSL_PARAM_construct_end(); /* Example key bytes are supplied on every initialization. */ if (!macCtx || !EVP_MAC_init(macCtx, key, sizeof(key) - 1, params) || !EVP_MAC_update(macCtx, message, sizeof(message) - 1) || !EVP_MAC_final(macCtx, output, &macSize, sizeof(output)) || macSize != sizeof(output)) { goto done; } SharkSslOsslExample_hex("HMAC", output, macSize); /* HKDF's default mode extracts and expands. 'info' binds the derived key to this application's intended use; it is not secret key material. */ operation = "HKDF-SHA256"; kdf = EVP_KDF_fetch(NULL, "HKDF", NULL); kdfCtx = kdf ? EVP_KDF_CTX_new(kdf) : NULL; params[0] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, digestName, 0); params[1] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, key, sizeof(key) - 1); params[2] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, salt, sizeof(salt) - 1); params[3] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO, info, sizeof(info) - 1); params[4] = OSSL_PARAM_construct_end(); if (!kdfCtx || !EVP_KDF_derive(kdfCtx, output, sizeof(output), params)) { goto done; } /* Show this demonstration result only. Do not log production derived keys. */ SharkSslOsslExample_hex("HKDF", output, sizeof(output)); result = 0; done: if (result) { SharkSslOsslExample_error(operation); } /* Release contexts before their caller-owned descriptors, on every exit. */ EVP_KDF_CTX_free(kdfCtx); EVP_KDF_free(kdf); EVP_MAC_CTX_free(macCtx); EVP_MAC_free(mac); EVP_MD_CTX_free(digestCtx); EVP_MD_free(digest); return result; } /* [crypto-example] */