Latest update

This commit is contained in:
2019-07-13 19:13:22 +09:00
parent 9a23f88dc2
commit 2e57f602ae
542 changed files with 17287 additions and 6184 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
SUBDIRS=digests ciphers
SOURCE[../../libcrypto]=\
provider_err.c
provider_err.c provlib.c
+131 -123
View File
@@ -27,13 +27,6 @@ static OSSL_OP_cipher_final_fn aes_stream_final;
static OSSL_OP_cipher_cipher_fn aes_cipher;
static OSSL_OP_cipher_freectx_fn aes_freectx;
static OSSL_OP_cipher_dupctx_fn aes_dupctx;
static OSSL_OP_cipher_key_length_fn key_length_256;
static OSSL_OP_cipher_key_length_fn key_length_192;
static OSSL_OP_cipher_key_length_fn key_length_128;
static OSSL_OP_cipher_iv_length_fn iv_length_16;
static OSSL_OP_cipher_iv_length_fn iv_length_0;
static OSSL_OP_cipher_block_size_fn block_size_16;
static OSSL_OP_cipher_block_size_fn block_size_1;
static OSSL_OP_cipher_ctx_get_params_fn aes_ctx_get_params;
static OSSL_OP_cipher_ctx_set_params_fn aes_ctx_set_params;
@@ -255,69 +248,82 @@ static int aes_cipher(void *vctx,
return 1;
}
#define IMPLEMENT_new_params(lcmode, UCMODE) \
static OSSL_OP_cipher_get_params_fn aes_##lcmode##_get_params; \
static int aes_##lcmode##_get_params(const OSSL_PARAM params[]) \
#define IMPLEMENT_cipher(lcmode, UCMODE, flags, kbits, blkbits, ivbits) \
static OSSL_OP_cipher_get_params_fn aes_##kbits##_##lcmode##_get_params; \
static int aes_##kbits##_##lcmode##_get_params(OSSL_PARAM params[]) \
{ \
const OSSL_PARAM *p; \
\
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_MODE); \
if (p != NULL && !OSSL_PARAM_set_int(p, EVP_CIPH_##UCMODE##_MODE)) \
return 0; \
OSSL_PARAM *p; \
\
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_MODE); \
if (p != NULL) { \
if (!OSSL_PARAM_set_int(p, EVP_CIPH_##UCMODE##_MODE)) \
return 0; \
} \
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_FLAGS); \
if (p != NULL) { \
if (!OSSL_PARAM_set_ulong(p, (flags))) \
return 0; \
} \
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN); \
if (p != NULL) { \
if (!OSSL_PARAM_set_int(p, (kbits) / 8)) \
return 0; \
} \
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_BLOCK_SIZE); \
if (p != NULL) { \
if (!OSSL_PARAM_set_int(p, (blkbits) / 8)) \
return 0; \
} \
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN); \
if (p != NULL) { \
if (!OSSL_PARAM_set_int(p, (ivbits) / 8)) \
return 0; \
} \
\
return 1; \
}
#define IMPLEMENT_new_ctx(lcmode, UCMODE, len) \
static OSSL_OP_cipher_newctx_fn aes_##len##_##lcmode##_newctx; \
static void *aes_##len##_##lcmode##_newctx(void *provctx) \
} \
static OSSL_OP_cipher_newctx_fn aes_##kbits##_##lcmode##_newctx; \
static void *aes_##kbits##_##lcmode##_newctx(void *provctx) \
{ \
PROV_AES_KEY *ctx = OPENSSL_zalloc(sizeof(*ctx)); \
\
ctx->pad = 1; \
ctx->keylen = (len / 8); \
ctx->keylen = ((kbits) / 8); \
ctx->ciph = PROV_AES_CIPHER_##lcmode(ctx->keylen); \
ctx->mode = EVP_CIPH_##UCMODE##_MODE; \
return ctx; \
}
/* ECB */
IMPLEMENT_new_params(ecb, ECB)
IMPLEMENT_new_ctx(ecb, ECB, 256)
IMPLEMENT_new_ctx(ecb, ECB, 192)
IMPLEMENT_new_ctx(ecb, ECB, 128)
IMPLEMENT_cipher(ecb, ECB, 0, 256, 128, 0)
IMPLEMENT_cipher(ecb, ECB, 0, 192, 128, 0)
IMPLEMENT_cipher(ecb, ECB, 0, 128, 128, 0)
/* CBC */
IMPLEMENT_new_params(cbc, CBC)
IMPLEMENT_new_ctx(cbc, CBC, 256)
IMPLEMENT_new_ctx(cbc, CBC, 192)
IMPLEMENT_new_ctx(cbc, CBC, 128)
IMPLEMENT_cipher(cbc, CBC, 0, 256, 128, 128)
IMPLEMENT_cipher(cbc, CBC, 0, 192, 128, 128)
IMPLEMENT_cipher(cbc, CBC, 0, 128, 128, 128)
/* OFB */
IMPLEMENT_new_params(ofb, OFB)
IMPLEMENT_new_ctx(ofb, OFB, 256)
IMPLEMENT_new_ctx(ofb, OFB, 192)
IMPLEMENT_new_ctx(ofb, OFB, 128)
IMPLEMENT_cipher(ofb, OFB, 0, 256, 8, 128)
IMPLEMENT_cipher(ofb, OFB, 0, 192, 8, 128)
IMPLEMENT_cipher(ofb, OFB, 0, 128, 8, 128)
/* CFB */
IMPLEMENT_new_params(cfb, CFB)
IMPLEMENT_new_params(cfb1, CFB)
IMPLEMENT_new_params(cfb8, CFB)
IMPLEMENT_new_ctx(cfb, CFB, 256)
IMPLEMENT_new_ctx(cfb, CFB, 192)
IMPLEMENT_new_ctx(cfb, CFB, 128)
IMPLEMENT_new_ctx(cfb1, CFB, 256)
IMPLEMENT_new_ctx(cfb1, CFB, 192)
IMPLEMENT_new_ctx(cfb1, CFB, 128)
IMPLEMENT_new_ctx(cfb8, CFB, 256)
IMPLEMENT_new_ctx(cfb8, CFB, 192)
IMPLEMENT_new_ctx(cfb8, CFB, 128)
IMPLEMENT_cipher(cfb, CFB, 0, 256, 8, 128)
IMPLEMENT_cipher(cfb, CFB, 0, 192, 8, 128)
IMPLEMENT_cipher(cfb, CFB, 0, 128, 8, 128)
IMPLEMENT_cipher(cfb1, CFB, 0, 256, 8, 128)
IMPLEMENT_cipher(cfb1, CFB, 0, 192, 8, 128)
IMPLEMENT_cipher(cfb1, CFB, 0, 128, 8, 128)
IMPLEMENT_cipher(cfb8, CFB, 0, 256, 8, 128)
IMPLEMENT_cipher(cfb8, CFB, 0, 192, 8, 128)
IMPLEMENT_cipher(cfb8, CFB, 0, 128, 8, 128)
/* CTR */
IMPLEMENT_new_params(ctr, CTR)
IMPLEMENT_new_ctx(ctr, CTR, 256)
IMPLEMENT_new_ctx(ctr, CTR, 192)
IMPLEMENT_new_ctx(ctr, CTR, 128)
IMPLEMENT_cipher(ctr, CTR, 0, 256, 8, 128)
IMPLEMENT_cipher(ctr, CTR, 0, 192, 8, 128)
IMPLEMENT_cipher(ctr, CTR, 0, 128, 8, 128)
static void aes_freectx(void *vctx)
{
@@ -340,51 +346,36 @@ static void *aes_dupctx(void *ctx)
return ret;
}
static size_t key_length_256(void)
{
return 256 / 8;
}
static size_t key_length_192(void)
{
return 192 / 8;
}
static size_t key_length_128(void)
{
return 128 / 8;
}
static size_t iv_length_16(void)
{
return 16;
}
static size_t iv_length_0(void)
{
return 0;
}
static size_t block_size_16(void)
{
return 16;
}
static size_t block_size_1(void)
{
return 1;
}
static int aes_ctx_get_params(void *vctx, const OSSL_PARAM params[])
static int aes_ctx_get_params(void *vctx, OSSL_PARAM params[])
{
PROV_AES_KEY *ctx = (PROV_AES_KEY *)vctx;
const OSSL_PARAM *p;
OSSL_PARAM *p;
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_PADDING);
if (p != NULL && !OSSL_PARAM_set_int(p, ctx->pad)) {
PROVerr(PROV_F_AES_CTX_GET_PARAMS, PROV_R_FAILED_TO_SET_PARAMETER);
return 0;
}
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IV);
if (p != NULL
&& !OSSL_PARAM_set_octet_ptr(p, &ctx->iv, AES_BLOCK_SIZE)
&& !OSSL_PARAM_set_octet_string(p, &ctx->iv, AES_BLOCK_SIZE)) {
PROVerr(PROV_F_AES_CTX_GET_PARAMS,
PROV_R_FAILED_TO_SET_PARAMETER);
return 0;
}
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_NUM);
if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->num)) {
PROVerr(PROV_F_AES_CTX_GET_PARAMS,
PROV_R_FAILED_TO_SET_PARAMETER);
return 0;
}
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
if (p != NULL && !OSSL_PARAM_set_int(p, ctx->keylen)) {
PROVerr(PROV_F_AES_CTX_GET_PARAMS,
PROV_R_FAILED_TO_SET_PARAMETER);
return 0;
}
return 1;
}
@@ -394,22 +385,45 @@ static int aes_ctx_set_params(void *vctx, const OSSL_PARAM params[])
PROV_AES_KEY *ctx = (PROV_AES_KEY *)vctx;
const OSSL_PARAM *p;
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_PADDING);
p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_PADDING);
if (p != NULL) {
int pad;
if (!OSSL_PARAM_get_int(p, &pad)) {
PROVerr(PROV_F_AES_CTX_SET_PARAMS, PROV_R_FAILED_TO_GET_PARAMETER);
PROVerr(PROV_F_AES_CTX_SET_PARAMS,
PROV_R_FAILED_TO_GET_PARAMETER);
return 0;
}
ctx->pad = pad ? 1 : 0;
}
p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_NUM);
if (p != NULL) {
int num;
if (!OSSL_PARAM_get_int(p, &num)) {
PROVerr(PROV_F_AES_CTX_SET_PARAMS,
PROV_R_FAILED_TO_GET_PARAMETER);
return 0;
}
ctx->num = num;
}
p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
if (p != NULL) {
int keylen;
if (!OSSL_PARAM_get_int(p, &keylen)) {
PROVerr(PROV_F_AES_CTX_SET_PARAMS,
PROV_R_FAILED_TO_GET_PARAMETER);
return 0;
}
ctx->keylen = keylen;
}
return 1;
}
#define IMPLEMENT_block_funcs(mode, keylen, ivlen) \
const OSSL_DISPATCH aes##keylen##mode##_functions[] = { \
{ OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))aes_##keylen##_##mode##_newctx }, \
#define IMPLEMENT_block_funcs(mode, kbits) \
const OSSL_DISPATCH aes##kbits##mode##_functions[] = { \
{ OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))aes_##kbits##_##mode##_newctx }, \
{ OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))aes_einit }, \
{ OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))aes_dinit }, \
{ OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))aes_block_update }, \
@@ -417,18 +431,15 @@ static int aes_ctx_set_params(void *vctx, const OSSL_PARAM params[])
{ OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))aes_cipher }, \
{ OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))aes_freectx }, \
{ OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))aes_dupctx }, \
{ OSSL_FUNC_CIPHER_KEY_LENGTH, (void (*)(void))key_length_##keylen }, \
{ OSSL_FUNC_CIPHER_IV_LENGTH, (void (*)(void))iv_length_##ivlen }, \
{ OSSL_FUNC_CIPHER_BLOCK_SIZE, (void (*)(void))block_size_16 }, \
{ OSSL_FUNC_CIPHER_GET_PARAMS, (void (*)(void))aes_##mode##_get_params }, \
{ OSSL_FUNC_CIPHER_GET_PARAMS, (void (*)(void))aes_##kbits##_##mode##_get_params }, \
{ OSSL_FUNC_CIPHER_CTX_GET_PARAMS, (void (*)(void))aes_ctx_get_params }, \
{ OSSL_FUNC_CIPHER_CTX_SET_PARAMS, (void (*)(void))aes_ctx_set_params }, \
{ 0, NULL } \
};
#define IMPLEMENT_stream_funcs(mode, keylen, ivlen) \
const OSSL_DISPATCH aes##keylen##mode##_functions[] = { \
{ OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))aes_##keylen##_##mode##_newctx }, \
#define IMPLEMENT_stream_funcs(mode, kbits) \
const OSSL_DISPATCH aes##kbits##mode##_functions[] = { \
{ OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))aes_##kbits##_##mode##_newctx }, \
{ OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))aes_einit }, \
{ OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))aes_dinit }, \
{ OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))aes_stream_update }, \
@@ -436,42 +447,39 @@ static int aes_ctx_set_params(void *vctx, const OSSL_PARAM params[])
{ OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))aes_cipher }, \
{ OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))aes_freectx }, \
{ OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))aes_dupctx }, \
{ OSSL_FUNC_CIPHER_KEY_LENGTH, (void (*)(void))key_length_##keylen }, \
{ OSSL_FUNC_CIPHER_IV_LENGTH, (void (*)(void))iv_length_##ivlen }, \
{ OSSL_FUNC_CIPHER_BLOCK_SIZE, (void (*)(void))block_size_1 }, \
{ OSSL_FUNC_CIPHER_GET_PARAMS, (void (*)(void))aes_##mode##_get_params }, \
{ OSSL_FUNC_CIPHER_GET_PARAMS, (void (*)(void))aes_##kbits##_##mode##_get_params }, \
{ OSSL_FUNC_CIPHER_CTX_GET_PARAMS, (void (*)(void))aes_ctx_get_params }, \
{ OSSL_FUNC_CIPHER_CTX_SET_PARAMS, (void (*)(void))aes_ctx_set_params }, \
{ 0, NULL } \
};
/* ECB */
IMPLEMENT_block_funcs(ecb, 256, 0)
IMPLEMENT_block_funcs(ecb, 192, 0)
IMPLEMENT_block_funcs(ecb, 128, 0)
IMPLEMENT_block_funcs(ecb, 256)
IMPLEMENT_block_funcs(ecb, 192)
IMPLEMENT_block_funcs(ecb, 128)
/* CBC */
IMPLEMENT_block_funcs(cbc, 256, 16)
IMPLEMENT_block_funcs(cbc, 192, 16)
IMPLEMENT_block_funcs(cbc, 128, 16)
IMPLEMENT_block_funcs(cbc, 256)
IMPLEMENT_block_funcs(cbc, 192)
IMPLEMENT_block_funcs(cbc, 128)
/* OFB */
IMPLEMENT_stream_funcs(ofb, 256, 16)
IMPLEMENT_stream_funcs(ofb, 192, 16)
IMPLEMENT_stream_funcs(ofb, 128, 16)
IMPLEMENT_stream_funcs(ofb, 256)
IMPLEMENT_stream_funcs(ofb, 192)
IMPLEMENT_stream_funcs(ofb, 128)
/* CFB */
IMPLEMENT_stream_funcs(cfb, 256, 16)
IMPLEMENT_stream_funcs(cfb, 192, 16)
IMPLEMENT_stream_funcs(cfb, 128, 16)
IMPLEMENT_stream_funcs(cfb1, 256, 16)
IMPLEMENT_stream_funcs(cfb1, 192, 16)
IMPLEMENT_stream_funcs(cfb1, 128, 16)
IMPLEMENT_stream_funcs(cfb8, 256, 16)
IMPLEMENT_stream_funcs(cfb8, 192, 16)
IMPLEMENT_stream_funcs(cfb8, 128, 16)
IMPLEMENT_stream_funcs(cfb, 256)
IMPLEMENT_stream_funcs(cfb, 192)
IMPLEMENT_stream_funcs(cfb, 128)
IMPLEMENT_stream_funcs(cfb1, 256)
IMPLEMENT_stream_funcs(cfb1, 192)
IMPLEMENT_stream_funcs(cfb1, 128)
IMPLEMENT_stream_funcs(cfb8, 256)
IMPLEMENT_stream_funcs(cfb8, 192)
IMPLEMENT_stream_funcs(cfb8, 128)
/* CTR */
IMPLEMENT_stream_funcs(ctr, 256, 16)
IMPLEMENT_stream_funcs(ctr, 192, 16)
IMPLEMENT_stream_funcs(ctr, 128, 16)
IMPLEMENT_stream_funcs(ctr, 256)
IMPLEMENT_stream_funcs(ctr, 192)
IMPLEMENT_stream_funcs(ctr, 128)
+3 -3
View File
@@ -606,9 +606,9 @@ static const PROV_AES_CIPHER aes_##mode = { \
}; \
const PROV_AES_CIPHER *PROV_AES_CIPHER_##mode(size_t keylen) \
{ \
if ((keylen == 128 && S390X_aes_128_##mode##_CAPABLE) \
|| (keylen == 192 && S390X_aes_192_##mode##_CAPABLE) \
|| (keylen == 256 && S390X_aes_256_##mode##_CAPABLE)) \
if ((keylen == 16 && S390X_aes_128_##mode##_CAPABLE) \
|| (keylen == 24 && S390X_aes_192_##mode##_CAPABLE) \
|| (keylen == 32 && S390X_aes_256_##mode##_CAPABLE)) \
return &s390x_aes_##mode; \
\
return &aes_##mode; \
+4
View File
@@ -2,3 +2,7 @@ LIBS=../../../libcrypto
SOURCE[../../../libcrypto]=\
block.c aes.c aes_basic.c
INCLUDE[../../../libcrypto]=. ../../../crypto
SOURCE[../../fips]=\
block.c aes.c aes_basic.c
INCLUDE[../../fips]=. ../../../crypto
+2 -2
View File
@@ -1,5 +1,5 @@
SOURCE[../../../libcrypto]=\
sha2.c
sha2_prov.c sha3_prov.c
SOURCE[../../fips]=\
sha2.c
sha2_prov.c sha3_prov.c
-87
View File
@@ -1,87 +0,0 @@
/*
* Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
*
* 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
*/
#include <openssl/sha.h>
#include <openssl/crypto.h>
#include <openssl/core_numbers.h>
#include "internal/provider_algs.h"
/*
* Forward declaration of everything implemented here. This is not strictly
* necessary for the compiler, but provides an assurance that the signatures
* of the functions in the dispatch table are correct.
*/
static OSSL_OP_digest_newctx_fn sha256_newctx;
#if 0 /* Not defined here */
static OSSL_OP_digest_init_fn sha256_init;
static OSSL_OP_digest_update_fn sha256_update;
#endif
static OSSL_OP_digest_final_fn sha256_final;
static OSSL_OP_digest_freectx_fn sha256_freectx;
static OSSL_OP_digest_dupctx_fn sha256_dupctx;
static OSSL_OP_digest_size_fn sha256_size;
static OSSL_OP_digest_block_size_fn sha256_size;
static int sha256_final(void *ctx,
unsigned char *md, size_t *mdl, size_t mdsz)
{
if (mdsz >= SHA256_DIGEST_LENGTH
&& SHA256_Final(md, ctx)) {
*mdl = SHA256_DIGEST_LENGTH;
return 1;
}
return 0;
}
static void *sha256_newctx(void *provctx)
{
SHA256_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
return ctx;
}
static void sha256_freectx(void *vctx)
{
SHA256_CTX *ctx = (SHA256_CTX *)vctx;
OPENSSL_clear_free(ctx, sizeof(*ctx));
}
static void *sha256_dupctx(void *ctx)
{
SHA256_CTX *in = (SHA256_CTX *)ctx;
SHA256_CTX *ret = OPENSSL_malloc(sizeof(*ret));
*ret = *in;
return ret;
}
static size_t sha256_size(void)
{
return SHA256_DIGEST_LENGTH;
}
static size_t sha256_block_size(void)
{
return SHA256_CBLOCK;
}
const OSSL_DISPATCH sha256_functions[] = {
{ OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))sha256_newctx },
{ OSSL_FUNC_DIGEST_INIT, (void (*)(void))SHA256_Init },
{ OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))SHA256_Update },
{ OSSL_FUNC_DIGEST_FINAL, (void (*)(void))sha256_final },
{ OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))sha256_freectx },
{ OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))sha256_dupctx },
{ OSSL_FUNC_DIGEST_SIZE, (void (*)(void))sha256_size },
{ OSSL_FUNC_DIGEST_BLOCK_SIZE, (void (*)(void))sha256_block_size },
{ 0, NULL }
};
+65
View File
@@ -0,0 +1,65 @@
/*
* Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
*
* 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
*/
#include <openssl/crypto.h>
#include <openssl/core_numbers.h>
#include <openssl/sha.h>
#include <openssl/evp.h>
#include <openssl/params.h>
#include <openssl/core_names.h>
#include "internal/core_mkdigest.h"
#include "internal/provider_algs.h"
#include "internal/sha.h"
static OSSL_OP_digest_set_params_fn sha1_set_params;
/* Special set_params method for SSL3 */
static int sha1_set_params(void *vctx, const OSSL_PARAM params[])
{
const OSSL_PARAM *p;
SHA_CTX *ctx = (SHA_CTX *)vctx;
if (ctx != NULL && params != NULL) {
p = OSSL_PARAM_locate_const(params, OSSL_DIGEST_PARAM_SSL3_MS);
if (p != NULL && p->data_type == OSSL_PARAM_OCTET_STRING)
return sha1_ctrl(ctx, EVP_CTRL_SSL3_MASTER_SECRET, p->data_size,
p->data);
}
return 0;
}
OSSL_FUNC_DIGEST_CONSTRUCT_PARAMS(sha1, SHA_CTX,
SHA_CBLOCK, SHA_DIGEST_LENGTH,
SHA1_Init, SHA1_Update, SHA1_Final,
sha1_set_params)
OSSL_FUNC_DIGEST_CONSTRUCT(sha224, SHA256_CTX,
SHA256_CBLOCK, SHA224_DIGEST_LENGTH,
SHA224_Init, SHA224_Update, SHA224_Final)
OSSL_FUNC_DIGEST_CONSTRUCT(sha256, SHA256_CTX,
SHA256_CBLOCK, SHA256_DIGEST_LENGTH,
SHA256_Init, SHA256_Update, SHA256_Final)
OSSL_FUNC_DIGEST_CONSTRUCT(sha384, SHA512_CTX,
SHA512_CBLOCK, SHA384_DIGEST_LENGTH,
SHA384_Init, SHA384_Update, SHA384_Final)
OSSL_FUNC_DIGEST_CONSTRUCT(sha512, SHA512_CTX,
SHA512_CBLOCK, SHA512_DIGEST_LENGTH,
SHA512_Init, SHA512_Update, SHA512_Final)
OSSL_FUNC_DIGEST_CONSTRUCT(sha512_224, SHA512_CTX,
SHA512_CBLOCK, SHA224_DIGEST_LENGTH,
sha512_224_init, SHA512_Update, SHA512_Final)
OSSL_FUNC_DIGEST_CONSTRUCT(sha512_256, SHA512_CTX,
SHA512_CBLOCK, SHA256_DIGEST_LENGTH,
sha512_256_init, SHA512_Update, SHA512_Final)
+278
View File
@@ -0,0 +1,278 @@
/*
* Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
*
* 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
*/
#include <openssl/core_names.h>
#include <string.h>
#include <openssl/crypto.h>
#include <openssl/evp.h>
#include <openssl/params.h>
#include "internal/sha3.h"
#include "internal/core_mkdigest.h"
#include "internal/provider_algs.h"
/*
* Forward declaration of any unique methods implemented here. This is not strictly
* necessary for the compiler, but provides an assurance that the signatures
* of the functions in the dispatch table are correct.
*/
static OSSL_OP_digest_init_fn keccak_init;
static OSSL_OP_digest_update_fn keccak_update;
static OSSL_OP_digest_final_fn keccak_final;
static OSSL_OP_digest_freectx_fn keccak_freectx;
static OSSL_OP_digest_dupctx_fn keccak_dupctx;
static OSSL_OP_digest_set_params_fn shake_set_params;
static sha3_absorb_fn generic_sha3_absorb;
static sha3_final_fn generic_sha3_final;
#if defined(OPENSSL_CPUID_OBJ) && defined(__s390__) && defined(KECCAK1600_ASM)
/*
* IBM S390X support
*/
# include "s390x_arch.h"
# define S390_SHA3 1
# define S390_SHA3_CAPABLE(name) \
((OPENSSL_s390xcap_P.kimd[0] & S390X_CAPBIT(S390X_##name)) && \
(OPENSSL_s390xcap_P.klmd[0] & S390X_CAPBIT(S390X_##name)))
#endif
static int keccak_init(void *vctx)
{
/* The newctx() handles most of the ctx fixed setup. */
sha3_reset((KECCAK1600_CTX *)vctx);
return 1;
}
static int keccak_update(void *vctx, const unsigned char *inp, size_t len)
{
KECCAK1600_CTX *ctx = vctx;
const size_t bsz = ctx->block_size;
size_t num, rem;
if (len == 0)
return 1;
/* Is there anything in the buffer already ? */
if ((num = ctx->bufsz) != 0) {
/* Calculate how much space is left in the buffer */
rem = bsz - num;
/* If the new input does not fill the buffer then just add it */
if (len < rem) {
memcpy(ctx->buf + num, inp, len);
ctx->bufsz += len;
return 1;
}
/* otherwise fill up the buffer and absorb the buffer */
memcpy(ctx->buf + num, inp, rem);
/* Update the input pointer */
inp += rem;
len -= rem;
ctx->meth.absorb(ctx, ctx->buf, bsz);
ctx->bufsz = 0;
}
/* Absorb the input - rem = leftover part of the input < blocksize) */
rem = ctx->meth.absorb(ctx, inp, len);
/* Copy the leftover bit of the input into the buffer */
if (rem) {
memcpy(ctx->buf, inp + len - rem, rem);
ctx->bufsz = rem;
}
return 1;
}
static int keccak_final(void *vctx, unsigned char *out, size_t *outl,
size_t outsz)
{
int ret;
KECCAK1600_CTX *ctx = vctx;
ret = ctx->meth.final(out, ctx);
*outl = ctx->md_size;
return ret;
}
/*-
* Generic software version of the absorb() and final().
*/
static size_t generic_sha3_absorb(void *vctx, const void *inp, size_t len)
{
KECCAK1600_CTX *ctx = vctx;
return SHA3_absorb(ctx->A, inp, len, ctx->block_size);
}
static int generic_sha3_final(unsigned char *md, void *vctx)
{
return sha3_final(md, (KECCAK1600_CTX *)vctx);
}
static PROV_SHA3_METHOD sha3_generic_md =
{
generic_sha3_absorb,
generic_sha3_final
};
#if defined(S390_SHA3)
static sha3_absorb_fn s390x_sha3_absorb;
static sha3_final_fn s390x_sha3_final;
static sha3_final_fn s390x_shake_final;
/*-
* The platform specific parts of the absorb() and final() for S390X.
*/
static size_t s390x_sha3_absorb(void *vctx, const void *inp, size_t len)
{
KECCAK1600_CTX *ctx = vctx;
size_t rem = len % ctx->block_size;
s390x_kimd(inp, len - rem, ctx->pad, ctx->A);
return rem;
}
static int s390x_sha3_final(unsigned char *md, void *vctx)
{
KECCAK1600_CTX *ctx = vctx;
s390x_klmd(ctx->buf, ctx->bufsz, NULL, 0, ctx->pad, ctx->A);
memcpy(md, ctx->A, ctx->md_size);
return 1;
}
static int s390x_shake_final(unsigned char *md, void *vctx)
{
KECCAK1600_CTX *ctx = vctx;
s390x_klmd(ctx->buf, ctx->bufsz, md, ctx->md_size, ctx->pad, ctx->A);
return 1;
}
static PROV_SHA3_METHOD sha3_s390x_md =
{
s390x_sha3_absorb,
s390x_sha3_final
};
static PROV_SHA3_METHOD shake_s390x_md =
{
s390x_sha3_absorb,
s390x_shake_final
};
# define SHA3_SET_MD(uname, typ) \
if (S390_SHA3_CAPABLE(uname)) { \
ctx->pad = S390X_##uname; \
ctx->meth = typ##_s390x_md; \
} else { \
ctx->meth = sha3_generic_md; \
}
#else
# define SHA3_SET_MD(uname, typ) ctx->meth = sha3_generic_md;
#endif /* S390_SHA3 */
#define SHA3_newctx(typ, uname, name, bitlen, pad) \
static OSSL_OP_digest_newctx_fn name##_newctx; \
static void *name##_newctx(void *provctx) \
{ \
KECCAK1600_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); \
\
if (ctx == NULL) \
return NULL; \
sha3_init(ctx, pad, bitlen); \
SHA3_SET_MD(uname, typ) \
return ctx; \
}
#define KMAC_newctx(uname, bitlen, pad) \
static OSSL_OP_digest_newctx_fn uname##_newctx; \
static void *uname##_newctx(void *provctx) \
{ \
KECCAK1600_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); \
\
if (ctx == NULL) \
return NULL; \
keccak_kmac_init(ctx, pad, bitlen); \
ctx->meth = sha3_generic_md; \
return ctx; \
}
#define OSSL_FUNC_SHA3_DIGEST(name, bitlen, dgstsize, stparams) \
static OSSL_OP_digest_size_fn name##_size; \
static OSSL_OP_digest_block_size_fn name##_block_size; \
static size_t name##_block_size(void) \
{ \
return SHA3_BLOCKSIZE(bitlen); \
} \
static size_t name##_size(void) \
{ \
return dgstsize; \
} \
const OSSL_DISPATCH name##_functions[] = { \
{ OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))name##_newctx }, \
{ OSSL_FUNC_DIGEST_INIT, (void (*)(void))keccak_init }, \
{ OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))keccak_update }, \
{ OSSL_FUNC_DIGEST_FINAL, (void (*)(void))keccak_final }, \
{ OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))keccak_freectx }, \
{ OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))keccak_dupctx }, \
{ OSSL_FUNC_DIGEST_SIZE, (void (*)(void))name##_size }, \
{ OSSL_FUNC_DIGEST_BLOCK_SIZE, (void (*)(void))name##_block_size }, \
{ OSSL_FUNC_DIGEST_SET_PARAMS, (void (*)(void))stparams },\
OSSL_FUNC_DIGEST_CONSTRUCT_END
static void keccak_freectx(void *vctx)
{
KECCAK1600_CTX *ctx = (KECCAK1600_CTX *)vctx;
OPENSSL_clear_free(ctx, sizeof(*ctx));
}
static void *keccak_dupctx(void *ctx)
{
KECCAK1600_CTX *in = (KECCAK1600_CTX *)ctx;
KECCAK1600_CTX *ret = OPENSSL_malloc(sizeof(*ret));
*ret = *in;
return ret;
}
static int shake_set_params(void *vctx, const OSSL_PARAM params[])
{
const OSSL_PARAM *p;
KECCAK1600_CTX *ctx = (KECCAK1600_CTX *)vctx;
if (ctx != NULL && params != NULL) {
p = OSSL_PARAM_locate_const(params, OSSL_DIGEST_PARAM_XOFLEN);
if (p != NULL && !OSSL_PARAM_get_size_t(p, &ctx->md_size))
return 0;
return 1;
}
return 0; /* Null Parameter */
}
#define SHA3(bitlen) \
SHA3_newctx(sha3, SHA3_##bitlen, sha3_##bitlen, bitlen, '\x06') \
OSSL_FUNC_SHA3_DIGEST(sha3_##bitlen, bitlen, SHA3_MDSIZE(bitlen), NULL)
#define SHAKE(bitlen) \
SHA3_newctx(shake, SHAKE_##bitlen, shake_##bitlen, bitlen, '\x1f') \
OSSL_FUNC_SHA3_DIGEST(shake_##bitlen, bitlen, SHA3_MDSIZE(bitlen), \
shake_set_params)
#define KMAC(bitlen) \
KMAC_newctx(keccak_kmac_##bitlen, bitlen, '\x04') \
OSSL_FUNC_SHA3_DIGEST(keccak_kmac_##bitlen, bitlen, KMAC_MDSIZE(bitlen), \
shake_set_params)
SHA3(224)
SHA3(256)
SHA3(384)
SHA3(512)
SHAKE(128)
SHAKE(256)
KMAC(128)
KMAC(256)
@@ -0,0 +1,95 @@
/*
* Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
*
* 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
*/
#ifndef OSSL_CORE_MKDIGEST_H
# define OSSL_CORE_MKDIGEST_H
# include <openssl/core_numbers.h>
# ifdef __cplusplus
extern "C" {
# endif
# define OSSL_FUNC_DIGEST_ALLOC_METHODS(name, CTX_NAME) \
static OSSL_OP_digest_newctx_fn name##_newctx; \
static OSSL_OP_digest_freectx_fn name##_freectx; \
static OSSL_OP_digest_dupctx_fn name##_dupctx; \
static void *name##_newctx(void *prov_ctx) \
{ \
CTX_NAME *ctx = OPENSSL_zalloc(sizeof(*ctx)); \
return ctx; \
} \
static void name##_freectx(void *vctx) \
{ \
CTX_NAME *ctx = (CTX_NAME *)vctx; \
OPENSSL_clear_free(ctx, sizeof(*ctx)); \
} \
static void *name##_dupctx(void *ctx) \
{ \
CTX_NAME *in = (CTX_NAME *)ctx; \
CTX_NAME *ret = OPENSSL_malloc(sizeof(*ret)); \
*ret = *in; \
return ret; \
}
# define OSSL_FUNC_DIGEST_SET_FINAL(name, dgstsize, fin) \
static OSSL_OP_digest_final_fn name##_wrapfinal; \
static int name##_wrapfinal(void *ctx, unsigned char *out, size_t *outl, size_t outsz) \
{ \
if (outsz >= dgstsize && fin(out, ctx)) { \
*outl = dgstsize; \
return 1; \
} \
return 0; \
}
# define OSSL_FUNC_DIGEST_COMMON(name, blksize, dgstsize, init, upd) \
static OSSL_OP_digest_block_size_fn name##_block_size; \
static OSSL_OP_digest_size_fn name##_size; \
static size_t name##_block_size(void) \
{ \
return blksize; \
} \
static size_t name##_size(void) \
{ \
return dgstsize; \
} \
const OSSL_DISPATCH name##_functions[] = { \
{ OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))name##_newctx }, \
{ OSSL_FUNC_DIGEST_INIT, (void (*)(void))init }, \
{ OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))upd }, \
{ OSSL_FUNC_DIGEST_FINAL, (void (*)(void))name##_wrapfinal }, \
{ OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))name##_freectx }, \
{ OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))name##_dupctx }, \
{ OSSL_FUNC_DIGEST_SIZE, (void (*)(void))name##_size }, \
{ OSSL_FUNC_DIGEST_BLOCK_SIZE, (void (*)(void))name##_block_size },
# define OSSL_FUNC_DIGEST_CONSTRUCT_START(name, CTX, blksize, dgstsize, init, upd, fin) \
OSSL_FUNC_DIGEST_ALLOC_METHODS(name, CTX) \
OSSL_FUNC_DIGEST_SET_FINAL(name, dgstsize, fin) \
OSSL_FUNC_DIGEST_COMMON(name, blksize, dgstsize, init, upd)
# define OSSL_FUNC_DIGEST_CONSTRUCT_END \
{ 0, NULL } \
};
# define OSSL_FUNC_DIGEST_CONSTRUCT(name, CTX, blksize, dgstsize, init, upd, fin) \
OSSL_FUNC_DIGEST_CONSTRUCT_START(name, CTX, blksize, dgstsize, init, upd, fin) \
OSSL_FUNC_DIGEST_CONSTRUCT_END
# define OSSL_FUNC_DIGEST_CONSTRUCT_PARAMS(name, CTX, blksize, dgstsize, init, upd, fin, setparams) \
OSSL_FUNC_DIGEST_CONSTRUCT_START(name, CTX, blksize, dgstsize, init, upd, fin) \
{ OSSL_FUNC_DIGEST_SET_PARAMS, (void (*)(void))setparams }, \
OSSL_FUNC_DIGEST_CONSTRUCT_END
# ifdef __cplusplus
}
# endif
#endif /* OSSL_CORE_MKDIGEST_H */
@@ -8,7 +8,32 @@
*/
/* Digests */
extern const OSSL_DISPATCH sha1_functions[];
extern const OSSL_DISPATCH sha224_functions[];
extern const OSSL_DISPATCH sha256_functions[];
extern const OSSL_DISPATCH sha384_functions[];
extern const OSSL_DISPATCH sha512_functions[];
extern const OSSL_DISPATCH sha512_224_functions[];
extern const OSSL_DISPATCH sha512_256_functions[];
extern const OSSL_DISPATCH sha3_224_functions[];
extern const OSSL_DISPATCH sha3_256_functions[];
extern const OSSL_DISPATCH sha3_384_functions[];
extern const OSSL_DISPATCH sha3_512_functions[];
extern const OSSL_DISPATCH keccak_kmac_128_functions[];
extern const OSSL_DISPATCH keccak_kmac_256_functions[];
extern const OSSL_DISPATCH shake_128_functions[];
extern const OSSL_DISPATCH shake_256_functions[];
extern const OSSL_DISPATCH blake2s256_functions[];
extern const OSSL_DISPATCH blake2b512_functions[];
extern const OSSL_DISPATCH md5_functions[];
extern const OSSL_DISPATCH md5_sha1_functions[];
extern const OSSL_DISPATCH sm3_functions[];
extern const OSSL_DISPATCH nullmd_functions[];
extern const OSSL_DISPATCH md2_functions[];
extern const OSSL_DISPATCH md4_functions[];
extern const OSSL_DISPATCH mdc2_functions[];
extern const OSSL_DISPATCH wp_functions[];
extern const OSSL_DISPATCH ripemd160_functions[];
/* Ciphers */
extern const OSSL_DISPATCH aes256ecb_functions[];
@@ -0,0 +1,14 @@
/*
* Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
*
* 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
*/
/*
* To be used anywhere the library context needs to be passed, such as to
* fetching functions.
*/
#define PROV_LIBRARY_CONTEXT_OF(provctx) (provctx)
@@ -0,0 +1,14 @@
/*
* Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
*
* 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
*/
#include <openssl/provider.h>
const OSSL_PROVIDER *FIPS_get_provider(OPENSSL_CTX *ctx);
const char *ossl_prov_util_nid_to_name(int nid);
+21
View File
@@ -0,0 +1,21 @@
/*
* Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
*
* 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
*/
#include <openssl/objects.h>
#include "internal/providercommon.h"
/*
* The FIPS provider has its own version of this in fipsprov.c because it does
* not have OBJ_nid2sn();
*/
const char *ossl_prov_util_nid_to_name(int nid)
{
return OBJ_nid2sn(nid);
}