Path: blob/main/crypto/openssl/providers/implementations/ciphers/cipher_aes_wrp.c
48383 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;474849static void *aes_wrap_newctx(size_t kbits, size_t blkbits,50size_t ivbits, unsigned int mode, uint64_t flags)51{52PROV_AES_WRAP_CTX *wctx;53PROV_CIPHER_CTX *ctx;5455if (!ossl_prov_is_running())56return NULL;5758wctx = OPENSSL_zalloc(sizeof(*wctx));59ctx = (PROV_CIPHER_CTX *)wctx;60if (ctx != NULL) {61ossl_cipher_generic_initkey(ctx, kbits, blkbits, ivbits, mode, flags,62NULL, NULL);63ctx->pad = (ctx->ivlen == AES_WRAP_PAD_IVLEN);64}65return wctx;66}6768static void *aes_wrap_dupctx(void *wctx)69{70PROV_AES_WRAP_CTX *ctx = wctx;71PROV_AES_WRAP_CTX *dctx = wctx;7273if (!ossl_prov_is_running())74return NULL;7576if (ctx == NULL)77return NULL;78dctx = OPENSSL_memdup(ctx, sizeof(*ctx));7980if (dctx != NULL && dctx->base.tlsmac != NULL && dctx->base.alloced) {81dctx->base.tlsmac = OPENSSL_memdup(dctx->base.tlsmac,82dctx->base.tlsmacsize);83if (dctx->base.tlsmac == NULL) {84OPENSSL_free(dctx);85dctx = NULL;86}87}88return dctx;89}9091static void aes_wrap_freectx(void *vctx)92{93PROV_AES_WRAP_CTX *wctx = (PROV_AES_WRAP_CTX *)vctx;9495ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);96OPENSSL_clear_free(wctx, sizeof(*wctx));97}9899static int aes_wrap_init(void *vctx, const unsigned char *key,100size_t keylen, const unsigned char *iv,101size_t ivlen, const OSSL_PARAM params[], int enc)102{103PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;104PROV_AES_WRAP_CTX *wctx = (PROV_AES_WRAP_CTX *)vctx;105106if (!ossl_prov_is_running())107return 0;108109ctx->enc = enc;110if (ctx->pad)111wctx->wrapfn = enc ? CRYPTO_128_wrap_pad : CRYPTO_128_unwrap_pad;112else113wctx->wrapfn = enc ? CRYPTO_128_wrap : CRYPTO_128_unwrap;114115if (iv != NULL) {116if (!ossl_cipher_generic_initiv(ctx, iv, ivlen))117return 0;118}119if (key != NULL) {120int use_forward_transform;121122if (keylen != ctx->keylen) {123ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);124return 0;125}126/*127* See SP800-38F : Section 5.1128* The forward and inverse transformations for the AES block129* cipher—called “cipher” and “inverse cipher” are informally known as130* the AES encryption and AES decryption functions, respectively.131* If the designated cipher function for a key-wrap algorithm is chosen132* to be the AES decryption function, then CIPH-1K will be the AES133* encryption function.134*/135if (ctx->inverse_cipher == 0)136use_forward_transform = ctx->enc;137else138use_forward_transform = !ctx->enc;139if (use_forward_transform) {140AES_set_encrypt_key(key, keylen * 8, &wctx->ks.ks);141ctx->block = (block128_f)AES_encrypt;142} else {143AES_set_decrypt_key(key, keylen * 8, &wctx->ks.ks);144ctx->block = (block128_f)AES_decrypt;145}146}147return aes_wrap_set_ctx_params(ctx, params);148}149150static int aes_wrap_einit(void *ctx, const unsigned char *key, size_t keylen,151const unsigned char *iv, size_t ivlen,152const OSSL_PARAM params[])153{154return aes_wrap_init(ctx, key, keylen, iv, ivlen, params, 1);155}156157static int aes_wrap_dinit(void *ctx, const unsigned char *key, size_t keylen,158const unsigned char *iv, size_t ivlen,159const OSSL_PARAM params[])160{161return aes_wrap_init(ctx, key, keylen, iv, ivlen, params, 0);162}163164static int aes_wrap_cipher_internal(void *vctx, unsigned char *out,165const unsigned char *in, size_t inlen)166{167PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;168PROV_AES_WRAP_CTX *wctx = (PROV_AES_WRAP_CTX *)vctx;169size_t rv;170int pad = ctx->pad;171172/* No final operation so always return zero length */173if (in == NULL)174return 0;175176/* Input length must always be non-zero */177if (inlen == 0) {178ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_INPUT_LENGTH);179return -1;180}181182/* If decrypting need at least 16 bytes and multiple of 8 */183if (!ctx->enc && (inlen < 16 || inlen & 0x7)) {184ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_INPUT_LENGTH);185return -1;186}187188/* If not padding input must be multiple of 8 */189if (!pad && inlen & 0x7) {190ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_INPUT_LENGTH);191return -1;192}193194if (out == NULL) {195if (ctx->enc) {196/* If padding round up to multiple of 8 */197if (pad)198inlen = (inlen + 7) / 8 * 8;199/* 8 byte prefix */200return inlen + 8;201} else {202/*203* If not padding output will be exactly 8 bytes smaller than204* input. If padding it will be at least 8 bytes smaller but we205* don't know how much.206*/207return inlen - 8;208}209}210211rv = wctx->wrapfn(&wctx->ks.ks, ctx->iv_set ? ctx->iv : NULL, out, in,212inlen, ctx->block);213if (!rv) {214ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);215return -1;216}217if (rv > INT_MAX) {218ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_OUTPUT_LENGTH);219return -1;220}221return (int)rv;222}223224static int aes_wrap_final(void *vctx, unsigned char *out, size_t *outl,225size_t outsize)226{227if (!ossl_prov_is_running())228return 0;229230*outl = 0;231return 1;232}233234static int aes_wrap_cipher(void *vctx,235unsigned char *out, size_t *outl, size_t outsize,236const unsigned char *in, size_t inl)237{238PROV_AES_WRAP_CTX *ctx = (PROV_AES_WRAP_CTX *)vctx;239size_t len;240241if (!ossl_prov_is_running())242return 0;243244if (inl == 0) {245*outl = 0;246return 1;247}248249if (outsize < inl) {250ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);251return 0;252}253254len = aes_wrap_cipher_internal(ctx, out, in, inl);255if (len <= 0)256return 0;257258*outl = len;259return 1;260}261262static int aes_wrap_set_ctx_params(void *vctx, const OSSL_PARAM params[])263{264PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;265const OSSL_PARAM *p;266size_t keylen = 0;267268if (ossl_param_is_empty(params))269return 1;270271p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);272if (p != NULL) {273if (!OSSL_PARAM_get_size_t(p, &keylen)) {274ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);275return 0;276}277if (ctx->keylen != keylen) {278ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);279return 0;280}281}282return 1;283}284285#define IMPLEMENT_cipher(mode, fname, UCMODE, flags, kbits, blkbits, ivbits) \286static OSSL_FUNC_cipher_get_params_fn aes_##kbits##_##fname##_get_params; \287static int aes_##kbits##_##fname##_get_params(OSSL_PARAM params[]) \288{ \289return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,\290flags, kbits, blkbits, ivbits); \291} \292static OSSL_FUNC_cipher_newctx_fn aes_##kbits##fname##_newctx; \293static void *aes_##kbits##fname##_newctx(void *provctx) \294{ \295return aes_##mode##_newctx(kbits, blkbits, ivbits, \296EVP_CIPH_##UCMODE##_MODE, flags); \297} \298const OSSL_DISPATCH ossl_##aes##kbits##fname##_functions[] = { \299{ OSSL_FUNC_CIPHER_NEWCTX, \300(void (*)(void))aes_##kbits##fname##_newctx }, \301{ OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))aes_##mode##_einit }, \302{ OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))aes_##mode##_dinit }, \303{ OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))aes_##mode##_cipher }, \304{ OSSL_FUNC_CIPHER_FINAL, (void (*)(void))aes_##mode##_final }, \305{ OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))aes_##mode##_freectx }, \306{ OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))aes_##mode##_dupctx }, \307{ OSSL_FUNC_CIPHER_GET_PARAMS, \308(void (*)(void))aes_##kbits##_##fname##_get_params }, \309{ OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \310(void (*)(void))ossl_cipher_generic_gettable_params }, \311{ OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \312(void (*)(void))ossl_cipher_generic_get_ctx_params }, \313{ OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \314(void (*)(void))aes_wrap_set_ctx_params }, \315{ OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \316(void (*)(void))ossl_cipher_generic_gettable_ctx_params }, \317{ OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \318(void (*)(void))ossl_cipher_generic_settable_ctx_params }, \319OSSL_DISPATCH_END \320}321322IMPLEMENT_cipher(wrap, wrap, WRAP, WRAP_FLAGS, 256, 64, AES_WRAP_NOPAD_IVLEN * 8);323IMPLEMENT_cipher(wrap, wrap, WRAP, WRAP_FLAGS, 192, 64, AES_WRAP_NOPAD_IVLEN * 8);324IMPLEMENT_cipher(wrap, wrap, WRAP, WRAP_FLAGS, 128, 64, AES_WRAP_NOPAD_IVLEN * 8);325IMPLEMENT_cipher(wrap, wrappad, WRAP, WRAP_FLAGS, 256, 64, AES_WRAP_PAD_IVLEN * 8);326IMPLEMENT_cipher(wrap, wrappad, WRAP, WRAP_FLAGS, 192, 64, AES_WRAP_PAD_IVLEN * 8);327IMPLEMENT_cipher(wrap, wrappad, WRAP, WRAP_FLAGS, 128, 64, AES_WRAP_PAD_IVLEN * 8);328329IMPLEMENT_cipher(wrap, wrapinv, WRAP, WRAP_FLAGS_INV, 256, 64, AES_WRAP_NOPAD_IVLEN * 8);330IMPLEMENT_cipher(wrap, wrapinv, WRAP, WRAP_FLAGS_INV, 192, 64, AES_WRAP_NOPAD_IVLEN * 8);331IMPLEMENT_cipher(wrap, wrapinv, WRAP, WRAP_FLAGS_INV, 128, 64, AES_WRAP_NOPAD_IVLEN * 8);332IMPLEMENT_cipher(wrap, wrappadinv, WRAP, WRAP_FLAGS_INV, 256, 64, AES_WRAP_PAD_IVLEN * 8);333IMPLEMENT_cipher(wrap, wrappadinv, WRAP, WRAP_FLAGS_INV, 192, 64, AES_WRAP_PAD_IVLEN * 8);334IMPLEMENT_cipher(wrap, wrappadinv, WRAP, WRAP_FLAGS_INV, 128, 64, AES_WRAP_PAD_IVLEN * 8);335336337