Path: blob/main/crypto/openssl/providers/implementations/ciphers/cipher_aes_wrp.c
106879 views
/*1* Copyright 2019-2023 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/*10* This file uses the low level AES functions (which are deprecated for11* non-internal use) in order to implement provider AES ciphers.12*/13#include "internal/deprecated.h"1415#include <openssl/proverr.h>16#include "cipher_aes.h"17#include "prov/providercommon.h"18#include "prov/implementations.h"1920/* AES wrap with padding has IV length of 4, without padding 8 */21#define AES_WRAP_PAD_IVLEN 422#define AES_WRAP_NOPAD_IVLEN 82324#define WRAP_FLAGS (PROV_CIPHER_FLAG_CUSTOM_IV)25#define WRAP_FLAGS_INV (WRAP_FLAGS | PROV_CIPHER_FLAG_INVERSE_CIPHER)2627typedef size_t (*aeswrap_fn)(void *key, const unsigned char *iv,28unsigned char *out, const unsigned char *in,29size_t inlen, block128_f block);3031static OSSL_FUNC_cipher_encrypt_init_fn aes_wrap_einit;32static OSSL_FUNC_cipher_decrypt_init_fn aes_wrap_dinit;33static OSSL_FUNC_cipher_update_fn aes_wrap_cipher;34static OSSL_FUNC_cipher_final_fn aes_wrap_final;35static OSSL_FUNC_cipher_freectx_fn aes_wrap_freectx;36static OSSL_FUNC_cipher_set_ctx_params_fn aes_wrap_set_ctx_params;3738typedef struct prov_aes_wrap_ctx_st {39PROV_CIPHER_CTX base;40union {41OSSL_UNION_ALIGN;42AES_KEY ks;43} ks;44aeswrap_fn wrapfn;4546} PROV_AES_WRAP_CTX;4748static void *aes_wrap_newctx(size_t kbits, size_t blkbits,49size_t ivbits, unsigned int mode, uint64_t flags)50{51PROV_AES_WRAP_CTX *wctx;52PROV_CIPHER_CTX *ctx;5354if (!ossl_prov_is_running())55return NULL;5657wctx = OPENSSL_zalloc(sizeof(*wctx));58ctx = (PROV_CIPHER_CTX *)wctx;59if (ctx != NULL) {60ossl_cipher_generic_initkey(ctx, kbits, blkbits, ivbits, mode, flags,61NULL, NULL);62ctx->pad = (ctx->ivlen == AES_WRAP_PAD_IVLEN);63}64return wctx;65}6667static void *aes_wrap_dupctx(void *wctx)68{69PROV_AES_WRAP_CTX *ctx = wctx;70PROV_AES_WRAP_CTX *dctx = wctx;7172if (!ossl_prov_is_running())73return NULL;7475if (ctx == NULL)76return NULL;77dctx = OPENSSL_memdup(ctx, sizeof(*ctx));7879if (dctx != NULL && dctx->base.tlsmac != NULL && dctx->base.alloced) {80dctx->base.tlsmac = OPENSSL_memdup(dctx->base.tlsmac,81dctx->base.tlsmacsize);82if (dctx->base.tlsmac == NULL) {83OPENSSL_free(dctx);84dctx = NULL;85}86}87return dctx;88}8990static void aes_wrap_freectx(void *vctx)91{92PROV_AES_WRAP_CTX *wctx = (PROV_AES_WRAP_CTX *)vctx;9394ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);95OPENSSL_clear_free(wctx, sizeof(*wctx));96}9798static int aes_wrap_init(void *vctx, const unsigned char *key,99size_t keylen, const unsigned char *iv,100size_t ivlen, const OSSL_PARAM params[], int enc)101{102PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;103PROV_AES_WRAP_CTX *wctx = (PROV_AES_WRAP_CTX *)vctx;104105if (!ossl_prov_is_running())106return 0;107108ctx->enc = enc;109if (ctx->pad)110wctx->wrapfn = enc ? CRYPTO_128_wrap_pad : CRYPTO_128_unwrap_pad;111else112wctx->wrapfn = enc ? CRYPTO_128_wrap : CRYPTO_128_unwrap;113114if (iv != NULL) {115if (!ossl_cipher_generic_initiv(ctx, iv, ivlen))116return 0;117}118if (key != NULL) {119int use_forward_transform;120121if (keylen != ctx->keylen) {122ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);123return 0;124}125/*126* See SP800-38F : Section 5.1127* The forward and inverse transformations for the AES block128* cipher—called “cipher” and “inverse cipher” are informally known as129* the AES encryption and AES decryption functions, respectively.130* If the designated cipher function for a key-wrap algorithm is chosen131* to be the AES decryption function, then CIPH-1K will be the AES132* encryption function.133*/134if (ctx->inverse_cipher == 0)135use_forward_transform = ctx->enc;136else137use_forward_transform = !ctx->enc;138if (use_forward_transform) {139AES_set_encrypt_key(key, keylen * 8, &wctx->ks.ks);140ctx->block = (block128_f)AES_encrypt;141} else {142AES_set_decrypt_key(key, keylen * 8, &wctx->ks.ks);143ctx->block = (block128_f)AES_decrypt;144}145}146return aes_wrap_set_ctx_params(ctx, params);147}148149static int aes_wrap_einit(void *ctx, const unsigned char *key, size_t keylen,150const unsigned char *iv, size_t ivlen,151const OSSL_PARAM params[])152{153return aes_wrap_init(ctx, key, keylen, iv, ivlen, params, 1);154}155156static int aes_wrap_dinit(void *ctx, const unsigned char *key, size_t keylen,157const unsigned char *iv, size_t ivlen,158const OSSL_PARAM params[])159{160return aes_wrap_init(ctx, key, keylen, iv, ivlen, params, 0);161}162163static int aes_wrap_cipher_internal(void *vctx, unsigned char *out,164const unsigned char *in, size_t inlen)165{166PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;167PROV_AES_WRAP_CTX *wctx = (PROV_AES_WRAP_CTX *)vctx;168size_t rv;169int pad = ctx->pad;170171/* No final operation so always return zero length */172if (in == NULL)173return 0;174175/* Input length must always be non-zero */176if (inlen == 0) {177ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_INPUT_LENGTH);178return -1;179}180181/* If decrypting need at least 16 bytes and multiple of 8 */182if (!ctx->enc && (inlen < 16 || inlen & 0x7)) {183ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_INPUT_LENGTH);184return -1;185}186187/* If not padding input must be multiple of 8 */188if (!pad && inlen & 0x7) {189ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_INPUT_LENGTH);190return -1;191}192193if (out == NULL) {194if (ctx->enc) {195/* If padding round up to multiple of 8 */196if (pad)197inlen = (inlen + 7) / 8 * 8;198/* 8 byte prefix */199return inlen + 8;200} else {201/*202* If not padding output will be exactly 8 bytes smaller than203* input. If padding it will be at least 8 bytes smaller but we204* don't know how much.205*/206return inlen - 8;207}208}209210rv = wctx->wrapfn(&wctx->ks.ks, ctx->iv_set ? ctx->iv : NULL, out, in,211inlen, ctx->block);212if (!rv) {213ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);214return -1;215}216if (rv > INT_MAX) {217ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_OUTPUT_LENGTH);218return -1;219}220return (int)rv;221}222223static int aes_wrap_final(void *vctx, unsigned char *out, size_t *outl,224size_t outsize)225{226if (!ossl_prov_is_running())227return 0;228229*outl = 0;230return 1;231}232233static int aes_wrap_cipher(void *vctx,234unsigned char *out, size_t *outl, size_t outsize,235const unsigned char *in, size_t inl)236{237PROV_AES_WRAP_CTX *ctx = (PROV_AES_WRAP_CTX *)vctx;238size_t len;239240if (!ossl_prov_is_running())241return 0;242243if (inl == 0) {244*outl = 0;245return 1;246}247248if (outsize < inl) {249ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);250return 0;251}252253len = aes_wrap_cipher_internal(ctx, out, in, inl);254if (len <= 0)255return 0;256257*outl = len;258return 1;259}260261static int aes_wrap_set_ctx_params(void *vctx, const OSSL_PARAM params[])262{263PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;264const OSSL_PARAM *p;265size_t keylen = 0;266267if (ossl_param_is_empty(params))268return 1;269270p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);271if (p != NULL) {272if (!OSSL_PARAM_get_size_t(p, &keylen)) {273ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);274return 0;275}276if (ctx->keylen != keylen) {277ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);278return 0;279}280}281return 1;282}283284#define IMPLEMENT_cipher(mode, fname, UCMODE, flags, kbits, blkbits, ivbits) \285static OSSL_FUNC_cipher_get_params_fn aes_##kbits##_##fname##_get_params; \286static int aes_##kbits##_##fname##_get_params(OSSL_PARAM params[]) \287{ \288return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \289flags, kbits, blkbits, ivbits); \290} \291static OSSL_FUNC_cipher_newctx_fn aes_##kbits##fname##_newctx; \292static void *aes_##kbits##fname##_newctx(void *provctx) \293{ \294return aes_##mode##_newctx(kbits, blkbits, ivbits, \295EVP_CIPH_##UCMODE##_MODE, flags); \296} \297const OSSL_DISPATCH ossl_##aes##kbits##fname##_functions[] = { \298{ OSSL_FUNC_CIPHER_NEWCTX, \299(void (*)(void))aes_##kbits##fname##_newctx }, \300{ OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))aes_##mode##_einit }, \301{ OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))aes_##mode##_dinit }, \302{ OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))aes_##mode##_cipher }, \303{ OSSL_FUNC_CIPHER_FINAL, (void (*)(void))aes_##mode##_final }, \304{ OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))aes_##mode##_freectx }, \305{ OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))aes_##mode##_dupctx }, \306{ OSSL_FUNC_CIPHER_GET_PARAMS, \307(void (*)(void))aes_##kbits##_##fname##_get_params }, \308{ OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \309(void (*)(void))ossl_cipher_generic_gettable_params }, \310{ OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \311(void (*)(void))ossl_cipher_generic_get_ctx_params }, \312{ OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \313(void (*)(void))aes_wrap_set_ctx_params }, \314{ OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \315(void (*)(void))ossl_cipher_generic_gettable_ctx_params }, \316{ OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \317(void (*)(void))ossl_cipher_generic_settable_ctx_params }, \318OSSL_DISPATCH_END \319}320321IMPLEMENT_cipher(wrap, wrap, WRAP, WRAP_FLAGS, 256, 64, AES_WRAP_NOPAD_IVLEN * 8);322IMPLEMENT_cipher(wrap, wrap, WRAP, WRAP_FLAGS, 192, 64, AES_WRAP_NOPAD_IVLEN * 8);323IMPLEMENT_cipher(wrap, wrap, WRAP, WRAP_FLAGS, 128, 64, AES_WRAP_NOPAD_IVLEN * 8);324IMPLEMENT_cipher(wrap, wrappad, WRAP, WRAP_FLAGS, 256, 64, AES_WRAP_PAD_IVLEN * 8);325IMPLEMENT_cipher(wrap, wrappad, WRAP, WRAP_FLAGS, 192, 64, AES_WRAP_PAD_IVLEN * 8);326IMPLEMENT_cipher(wrap, wrappad, WRAP, WRAP_FLAGS, 128, 64, AES_WRAP_PAD_IVLEN * 8);327328IMPLEMENT_cipher(wrap, wrapinv, WRAP, WRAP_FLAGS_INV, 256, 64, AES_WRAP_NOPAD_IVLEN * 8);329IMPLEMENT_cipher(wrap, wrapinv, WRAP, WRAP_FLAGS_INV, 192, 64, AES_WRAP_NOPAD_IVLEN * 8);330IMPLEMENT_cipher(wrap, wrapinv, WRAP, WRAP_FLAGS_INV, 128, 64, AES_WRAP_NOPAD_IVLEN * 8);331IMPLEMENT_cipher(wrap, wrappadinv, WRAP, WRAP_FLAGS_INV, 256, 64, AES_WRAP_PAD_IVLEN * 8);332IMPLEMENT_cipher(wrap, wrappadinv, WRAP, WRAP_FLAGS_INV, 192, 64, AES_WRAP_PAD_IVLEN * 8);333IMPLEMENT_cipher(wrap, wrappadinv, WRAP, WRAP_FLAGS_INV, 128, 64, AES_WRAP_PAD_IVLEN * 8);334335336