Path: blob/main/crypto/openssl/providers/implementations/kem/template_kem.c
48383 views
/*1* Copyright 2024-2025 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89#include <string.h>10#include <openssl/crypto.h>11#include <openssl/evp.h>12#include <openssl/core_dispatch.h>13#include <openssl/core_names.h>14#include <openssl/params.h>15#include <openssl/err.h>16#include <openssl/proverr.h>17#include "prov/provider_ctx.h"18#include "prov/implementations.h"19#include "prov/securitycheck.h"20#include "prov/providercommon.h"2122extern const OSSL_DISPATCH ossl_template_asym_kem_functions[];2324#define BUFSIZE 100025#if defined(NDEBUG) || defined(OPENSSL_NO_STDIO)26static void debug_print(char *fmt, ...)27{28}2930#else31static void debug_print(char *fmt, ...)32{33char out[BUFSIZE];34va_list argptr;3536va_start(argptr, fmt);37vsnprintf(out, BUFSIZE, fmt, argptr);38va_end(argptr);39if (getenv("TEMPLATEKEM"))40fprintf(stderr, "TEMPLATE_KEM: %s", out);41}42#endif4344typedef struct {45OSSL_LIB_CTX *libctx;46/* some algorithm-specific key struct */47int op;48} PROV_TEMPLATE_CTX;4950static OSSL_FUNC_kem_newctx_fn template_newctx;51static OSSL_FUNC_kem_encapsulate_init_fn template_encapsulate_init;52static OSSL_FUNC_kem_encapsulate_fn template_encapsulate;53static OSSL_FUNC_kem_decapsulate_init_fn template_decapsulate_init;54static OSSL_FUNC_kem_decapsulate_fn template_decapsulate;55static OSSL_FUNC_kem_freectx_fn template_freectx;56static OSSL_FUNC_kem_set_ctx_params_fn template_set_ctx_params;57static OSSL_FUNC_kem_settable_ctx_params_fn template_settable_ctx_params;5859static void *template_newctx(void *provctx)60{61PROV_TEMPLATE_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));6263debug_print("newctx called\n");64if (ctx == NULL)65return NULL;66ctx->libctx = PROV_LIBCTX_OF(provctx);6768debug_print("newctx returns %p\n", ctx);69return ctx;70}7172static void template_freectx(void *vctx)73{74PROV_TEMPLATE_CTX *ctx = (PROV_TEMPLATE_CTX *)vctx;7576debug_print("freectx %p\n", ctx);77OPENSSL_free(ctx);78}7980static int template_init(void *vctx, int operation, void *vkey, void *vauth,81ossl_unused const OSSL_PARAM params[])82{83PROV_TEMPLATE_CTX *ctx = (PROV_TEMPLATE_CTX *)vctx;8485debug_print("init %p / %p\n", ctx, vkey);86if (!ossl_prov_is_running())87return 0;8889/* check and fill in reference to key */90ctx->op = operation;91debug_print("init OK\n");92return 1;93}9495static int template_encapsulate_init(void *vctx, void *vkey,96const OSSL_PARAM params[])97{98return template_init(vctx, EVP_PKEY_OP_ENCAPSULATE, vkey, NULL, params);99}100101static int template_decapsulate_init(void *vctx, void *vkey,102const OSSL_PARAM params[])103{104return template_init(vctx, EVP_PKEY_OP_DECAPSULATE, vkey, NULL, params);105}106107static int template_set_ctx_params(void *vctx, const OSSL_PARAM params[])108{109PROV_TEMPLATE_CTX *ctx = (PROV_TEMPLATE_CTX *)vctx;110111debug_print("set ctx params %p\n", ctx);112if (ctx == NULL)113return 0;114if (ossl_param_is_empty(params))115return 1;116117debug_print("set ctx params OK\n");118return 1;119}120121static const OSSL_PARAM known_settable_template_ctx_params[] = {122/* possibly more params */123OSSL_PARAM_END124};125126static const OSSL_PARAM *template_settable_ctx_params(ossl_unused void *vctx,127ossl_unused void *provctx)128{129return known_settable_template_ctx_params;130}131132static int template_encapsulate(void *vctx, unsigned char *out, size_t *outlen,133unsigned char *secret, size_t *secretlen)134{135debug_print("encaps %p to %p\n", vctx, out);136137/* add algorithm-specific length checks */138139if (outlen != NULL)140*outlen = 0; /* replace with real encapsulated data length */141if (secretlen != NULL)142*secretlen = 0; /* replace with real shared secret length */143144if (out == NULL) {145if (outlen != NULL && secretlen != NULL)146debug_print("encaps outlens set to %zu and %zu\n", *outlen, *secretlen);147return 1;148}149150/* check key and perform real KEM operation */151152debug_print("encaps OK\n");153return 1;154}155156static int template_decapsulate(void *vctx, unsigned char *out, size_t *outlen,157const unsigned char *in, size_t inlen)158{159debug_print("decaps %p to %p inlen at %zu\n", vctx, out, inlen);160161/* add algorithm-specific length checks */162163if (outlen != NULL)164*outlen = 0; /* replace with shared secret length */165166if (out == NULL) {167if (outlen != NULL)168debug_print("decaps outlen set to %zu \n", *outlen);169return 1;170}171172/* check key and perform real decaps operation */173174debug_print("decaps OK\n");175return 1;176}177178const OSSL_DISPATCH ossl_template_asym_kem_functions[] = {179{ OSSL_FUNC_KEM_NEWCTX, (void (*)(void))template_newctx },180{ OSSL_FUNC_KEM_ENCAPSULATE_INIT,181(void (*)(void))template_encapsulate_init },182{ OSSL_FUNC_KEM_ENCAPSULATE, (void (*)(void))template_encapsulate },183{ OSSL_FUNC_KEM_DECAPSULATE_INIT,184(void (*)(void))template_decapsulate_init },185{ OSSL_FUNC_KEM_DECAPSULATE, (void (*)(void))template_decapsulate },186{ OSSL_FUNC_KEM_FREECTX, (void (*)(void))template_freectx },187{ OSSL_FUNC_KEM_SET_CTX_PARAMS,188(void (*)(void))template_set_ctx_params },189{ OSSL_FUNC_KEM_SETTABLE_CTX_PARAMS,190(void (*)(void))template_settable_ctx_params },191OSSL_DISPATCH_END192};193194195