2019-10-16 14:18:42 +08:00
|
|
|
/*
|
2021-02-18 22:57:13 +08:00
|
|
|
* Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
|
2019-10-16 14:18:42 +08:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Dispatch functions for chacha20 cipher */
|
|
|
|
|
2021-02-06 00:40:42 +08:00
|
|
|
#include <openssl/proverr.h>
|
2019-10-16 14:18:42 +08:00
|
|
|
#include "cipher_chacha20.h"
|
|
|
|
#include "prov/implementations.h"
|
2020-09-08 10:56:34 +08:00
|
|
|
#include "prov/providercommon.h"
|
2019-10-16 14:18:42 +08:00
|
|
|
|
|
|
|
#define CHACHA20_KEYLEN (CHACHA_KEY_SIZE)
|
|
|
|
#define CHACHA20_BLKLEN (1)
|
|
|
|
#define CHACHA20_IVLEN (CHACHA_CTR_SIZE)
|
2020-12-17 14:39:57 +08:00
|
|
|
#define CHACHA20_FLAGS (PROV_CIPHER_FLAG_CUSTOM_IV)
|
2019-10-16 14:18:42 +08:00
|
|
|
|
2020-06-21 07:19:16 +08:00
|
|
|
static OSSL_FUNC_cipher_newctx_fn chacha20_newctx;
|
|
|
|
static OSSL_FUNC_cipher_freectx_fn chacha20_freectx;
|
|
|
|
static OSSL_FUNC_cipher_get_params_fn chacha20_get_params;
|
|
|
|
static OSSL_FUNC_cipher_get_ctx_params_fn chacha20_get_ctx_params;
|
|
|
|
static OSSL_FUNC_cipher_set_ctx_params_fn chacha20_set_ctx_params;
|
|
|
|
static OSSL_FUNC_cipher_gettable_ctx_params_fn chacha20_gettable_ctx_params;
|
|
|
|
static OSSL_FUNC_cipher_settable_ctx_params_fn chacha20_settable_ctx_params;
|
2020-09-29 15:40:26 +08:00
|
|
|
#define chacha20_cipher ossl_cipher_generic_cipher
|
|
|
|
#define chacha20_update ossl_cipher_generic_stream_update
|
|
|
|
#define chacha20_final ossl_cipher_generic_stream_final
|
|
|
|
#define chacha20_gettable_params ossl_cipher_generic_gettable_params
|
2019-10-16 14:18:42 +08:00
|
|
|
|
Fix external symbols in the provider cipher implementations.
Partial fix for #12964
This add ossl_ names for the following symbols.
chacha20_dinit, chacha20_einit, chacha20_initctx,
ccm_cipher, ccm_dinit, ccm_einit, ccm_generic_auth_decrypt, ccm_generic_auth_encrypt,
ccm_generic_gettag, ccm_generic_setaad, ccm_generic_setiv, ccm_get_ctx_params,
ccm_initctx, ccm_set_ctx_params, ccm_stream_final, ccm_stream_update
gcm_aad_update, gcm_cipher, gcm_cipher_final, gcm_cipher_update
gcm_dinit, gcm_einit, gcm_get_ctx_params, gcm_initctx, gcm_one_shot
gcm_set_ctx_params, gcm_setiv, gcm_stream_final, gcm_stream_update
tdes_dinit, tdes_dupctx, tdes_einit, tdes_freectx
tdes_get_ctx_params, tdes_gettable_ctx_params, tdes_newctx
PROV_CIPHER_HW_des_*,
padblock, unpadblock, tlsunpadblock, fillblock, trailingdata
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14209)
2021-02-17 15:54:29 +08:00
|
|
|
void ossl_chacha20_initctx(PROV_CHACHA20_CTX *ctx)
|
2019-10-16 14:18:42 +08:00
|
|
|
{
|
2020-09-29 15:40:26 +08:00
|
|
|
ossl_cipher_generic_initkey(ctx, CHACHA20_KEYLEN * 8,
|
|
|
|
CHACHA20_BLKLEN * 8,
|
|
|
|
CHACHA20_IVLEN * 8,
|
|
|
|
0, CHACHA20_FLAGS,
|
|
|
|
ossl_prov_cipher_hw_chacha20(CHACHA20_KEYLEN * 8),
|
|
|
|
NULL);
|
2019-10-16 14:18:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *chacha20_newctx(void *provctx)
|
|
|
|
{
|
2020-09-08 10:56:34 +08:00
|
|
|
PROV_CHACHA20_CTX *ctx;
|
2019-10-16 14:18:42 +08:00
|
|
|
|
2020-09-08 10:56:34 +08:00
|
|
|
if (!ossl_prov_is_running())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ctx = OPENSSL_zalloc(sizeof(*ctx));
|
|
|
|
if (ctx != NULL)
|
Fix external symbols in the provider cipher implementations.
Partial fix for #12964
This add ossl_ names for the following symbols.
chacha20_dinit, chacha20_einit, chacha20_initctx,
ccm_cipher, ccm_dinit, ccm_einit, ccm_generic_auth_decrypt, ccm_generic_auth_encrypt,
ccm_generic_gettag, ccm_generic_setaad, ccm_generic_setiv, ccm_get_ctx_params,
ccm_initctx, ccm_set_ctx_params, ccm_stream_final, ccm_stream_update
gcm_aad_update, gcm_cipher, gcm_cipher_final, gcm_cipher_update
gcm_dinit, gcm_einit, gcm_get_ctx_params, gcm_initctx, gcm_one_shot
gcm_set_ctx_params, gcm_setiv, gcm_stream_final, gcm_stream_update
tdes_dinit, tdes_dupctx, tdes_einit, tdes_freectx
tdes_get_ctx_params, tdes_gettable_ctx_params, tdes_newctx
PROV_CIPHER_HW_des_*,
padblock, unpadblock, tlsunpadblock, fillblock, trailingdata
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14209)
2021-02-17 15:54:29 +08:00
|
|
|
ossl_chacha20_initctx(ctx);
|
2020-09-08 10:56:34 +08:00
|
|
|
return ctx;
|
2019-10-16 14:18:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void chacha20_freectx(void *vctx)
|
|
|
|
{
|
|
|
|
PROV_CHACHA20_CTX *ctx = (PROV_CHACHA20_CTX *)vctx;
|
|
|
|
|
|
|
|
if (ctx != NULL) {
|
2020-09-29 15:40:26 +08:00
|
|
|
ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
|
2019-10-20 19:10:38 +08:00
|
|
|
OPENSSL_clear_free(ctx, sizeof(*ctx));
|
2019-10-16 14:18:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int chacha20_get_params(OSSL_PARAM params[])
|
|
|
|
{
|
2020-09-29 15:40:26 +08:00
|
|
|
return ossl_cipher_generic_get_params(params, 0, CHACHA20_FLAGS,
|
|
|
|
CHACHA20_KEYLEN * 8,
|
|
|
|
CHACHA20_BLKLEN * 8,
|
|
|
|
CHACHA20_IVLEN * 8);
|
2019-10-16 14:18:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int chacha20_get_ctx_params(void *vctx, OSSL_PARAM params[])
|
|
|
|
{
|
|
|
|
OSSL_PARAM *p;
|
|
|
|
|
|
|
|
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
|
|
|
|
if (p != NULL && !OSSL_PARAM_set_size_t(p, CHACHA20_IVLEN)) {
|
|
|
|
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
|
|
|
|
if (p != NULL && !OSSL_PARAM_set_size_t(p, CHACHA20_KEYLEN)) {
|
|
|
|
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const OSSL_PARAM chacha20_known_gettable_ctx_params[] = {
|
|
|
|
OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
|
|
|
|
OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL),
|
|
|
|
OSSL_PARAM_END
|
|
|
|
};
|
2021-02-23 09:48:35 +08:00
|
|
|
const OSSL_PARAM *chacha20_gettable_ctx_params(ossl_unused void *cctx,
|
|
|
|
ossl_unused void *provctx)
|
2019-10-16 14:18:42 +08:00
|
|
|
{
|
|
|
|
return chacha20_known_gettable_ctx_params;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int chacha20_set_ctx_params(void *vctx, const OSSL_PARAM params[])
|
|
|
|
{
|
|
|
|
const OSSL_PARAM *p;
|
|
|
|
size_t len;
|
|
|
|
|
2021-03-02 20:44:53 +08:00
|
|
|
if (params == NULL)
|
|
|
|
return 1;
|
|
|
|
|
2019-10-16 14:18:42 +08:00
|
|
|
p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
|
|
|
|
if (p != NULL) {
|
|
|
|
if (!OSSL_PARAM_get_size_t(p, &len)) {
|
|
|
|
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (len != CHACHA20_KEYLEN) {
|
|
|
|
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_IVLEN);
|
|
|
|
if (p != NULL) {
|
|
|
|
if (!OSSL_PARAM_get_size_t(p, &len)) {
|
|
|
|
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (len != CHACHA20_IVLEN) {
|
|
|
|
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const OSSL_PARAM chacha20_known_settable_ctx_params[] = {
|
|
|
|
OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
|
|
|
|
OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL),
|
|
|
|
OSSL_PARAM_END
|
|
|
|
};
|
2021-02-23 09:48:35 +08:00
|
|
|
const OSSL_PARAM *chacha20_settable_ctx_params(ossl_unused void *cctx,
|
|
|
|
ossl_unused void *provctx)
|
2019-10-16 14:18:42 +08:00
|
|
|
{
|
|
|
|
return chacha20_known_settable_ctx_params;
|
|
|
|
}
|
|
|
|
|
Fix external symbols in the provider cipher implementations.
Partial fix for #12964
This add ossl_ names for the following symbols.
chacha20_dinit, chacha20_einit, chacha20_initctx,
ccm_cipher, ccm_dinit, ccm_einit, ccm_generic_auth_decrypt, ccm_generic_auth_encrypt,
ccm_generic_gettag, ccm_generic_setaad, ccm_generic_setiv, ccm_get_ctx_params,
ccm_initctx, ccm_set_ctx_params, ccm_stream_final, ccm_stream_update
gcm_aad_update, gcm_cipher, gcm_cipher_final, gcm_cipher_update
gcm_dinit, gcm_einit, gcm_get_ctx_params, gcm_initctx, gcm_one_shot
gcm_set_ctx_params, gcm_setiv, gcm_stream_final, gcm_stream_update
tdes_dinit, tdes_dupctx, tdes_einit, tdes_freectx
tdes_get_ctx_params, tdes_gettable_ctx_params, tdes_newctx
PROV_CIPHER_HW_des_*,
padblock, unpadblock, tlsunpadblock, fillblock, trailingdata
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14209)
2021-02-17 15:54:29 +08:00
|
|
|
int ossl_chacha20_einit(void *vctx, const unsigned char *key, size_t keylen,
|
2021-03-02 20:44:53 +08:00
|
|
|
const unsigned char *iv, size_t ivlen,
|
|
|
|
const OSSL_PARAM params[])
|
2019-10-16 14:18:42 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2020-09-08 10:56:34 +08:00
|
|
|
/* The generic function checks for ossl_prov_is_running() */
|
2021-03-02 20:44:53 +08:00
|
|
|
ret = ossl_cipher_generic_einit(vctx, key, keylen, iv, ivlen, NULL);
|
2019-10-16 14:18:42 +08:00
|
|
|
if (ret && iv != NULL) {
|
|
|
|
PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
|
|
|
|
PROV_CIPHER_HW_CHACHA20 *hw = (PROV_CIPHER_HW_CHACHA20 *)ctx->hw;
|
|
|
|
|
|
|
|
hw->initiv(ctx);
|
|
|
|
}
|
2021-03-02 20:44:53 +08:00
|
|
|
if (ret && !chacha20_set_ctx_params(vctx, params))
|
|
|
|
ret = 0;
|
2019-10-16 14:18:42 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
Fix external symbols in the provider cipher implementations.
Partial fix for #12964
This add ossl_ names for the following symbols.
chacha20_dinit, chacha20_einit, chacha20_initctx,
ccm_cipher, ccm_dinit, ccm_einit, ccm_generic_auth_decrypt, ccm_generic_auth_encrypt,
ccm_generic_gettag, ccm_generic_setaad, ccm_generic_setiv, ccm_get_ctx_params,
ccm_initctx, ccm_set_ctx_params, ccm_stream_final, ccm_stream_update
gcm_aad_update, gcm_cipher, gcm_cipher_final, gcm_cipher_update
gcm_dinit, gcm_einit, gcm_get_ctx_params, gcm_initctx, gcm_one_shot
gcm_set_ctx_params, gcm_setiv, gcm_stream_final, gcm_stream_update
tdes_dinit, tdes_dupctx, tdes_einit, tdes_freectx
tdes_get_ctx_params, tdes_gettable_ctx_params, tdes_newctx
PROV_CIPHER_HW_des_*,
padblock, unpadblock, tlsunpadblock, fillblock, trailingdata
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14209)
2021-02-17 15:54:29 +08:00
|
|
|
int ossl_chacha20_dinit(void *vctx, const unsigned char *key, size_t keylen,
|
2021-03-02 20:44:53 +08:00
|
|
|
const unsigned char *iv, size_t ivlen,
|
|
|
|
const OSSL_PARAM params[])
|
2019-10-16 14:18:42 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2020-09-08 10:56:34 +08:00
|
|
|
/* The generic function checks for ossl_prov_is_running() */
|
2021-03-02 20:44:53 +08:00
|
|
|
ret = ossl_cipher_generic_dinit(vctx, key, keylen, iv, ivlen, NULL);
|
2019-10-16 14:18:42 +08:00
|
|
|
if (ret && iv != NULL) {
|
|
|
|
PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
|
|
|
|
PROV_CIPHER_HW_CHACHA20 *hw = (PROV_CIPHER_HW_CHACHA20 *)ctx->hw;
|
|
|
|
|
|
|
|
hw->initiv(ctx);
|
|
|
|
}
|
2021-03-02 20:44:53 +08:00
|
|
|
if (ret && !chacha20_set_ctx_params(vctx, params))
|
|
|
|
ret = 0;
|
2019-10-16 14:18:42 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-09-28 10:28:29 +08:00
|
|
|
/* ossl_chacha20_functions */
|
|
|
|
const OSSL_DISPATCH ossl_chacha20_functions[] = {
|
2019-10-16 14:18:42 +08:00
|
|
|
{ OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))chacha20_newctx },
|
|
|
|
{ OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))chacha20_freectx },
|
Fix external symbols in the provider cipher implementations.
Partial fix for #12964
This add ossl_ names for the following symbols.
chacha20_dinit, chacha20_einit, chacha20_initctx,
ccm_cipher, ccm_dinit, ccm_einit, ccm_generic_auth_decrypt, ccm_generic_auth_encrypt,
ccm_generic_gettag, ccm_generic_setaad, ccm_generic_setiv, ccm_get_ctx_params,
ccm_initctx, ccm_set_ctx_params, ccm_stream_final, ccm_stream_update
gcm_aad_update, gcm_cipher, gcm_cipher_final, gcm_cipher_update
gcm_dinit, gcm_einit, gcm_get_ctx_params, gcm_initctx, gcm_one_shot
gcm_set_ctx_params, gcm_setiv, gcm_stream_final, gcm_stream_update
tdes_dinit, tdes_dupctx, tdes_einit, tdes_freectx
tdes_get_ctx_params, tdes_gettable_ctx_params, tdes_newctx
PROV_CIPHER_HW_des_*,
padblock, unpadblock, tlsunpadblock, fillblock, trailingdata
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14209)
2021-02-17 15:54:29 +08:00
|
|
|
{ OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))ossl_chacha20_einit },
|
|
|
|
{ OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))ossl_chacha20_dinit },
|
2019-10-16 14:18:42 +08:00
|
|
|
{ OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))chacha20_update },
|
|
|
|
{ OSSL_FUNC_CIPHER_FINAL, (void (*)(void))chacha20_final },
|
|
|
|
{ OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))chacha20_cipher},
|
|
|
|
{ OSSL_FUNC_CIPHER_GET_PARAMS, (void (*)(void))chacha20_get_params },
|
2021-10-26 15:16:18 +08:00
|
|
|
{ OSSL_FUNC_CIPHER_GETTABLE_PARAMS, (void (*)(void))chacha20_gettable_params },
|
2019-10-16 14:18:42 +08:00
|
|
|
{ OSSL_FUNC_CIPHER_GET_CTX_PARAMS, (void (*)(void))chacha20_get_ctx_params },
|
|
|
|
{ OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,
|
|
|
|
(void (*)(void))chacha20_gettable_ctx_params },
|
|
|
|
{ OSSL_FUNC_CIPHER_SET_CTX_PARAMS, (void (*)(void))chacha20_set_ctx_params },
|
|
|
|
{ OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,
|
|
|
|
(void (*)(void))chacha20_settable_ctx_params },
|
|
|
|
{ 0, NULL }
|
|
|
|
};
|
|
|
|
|