2019-03-13 17:26:17 +00:00
|
|
|
/*
|
|
|
|
* 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>
|
|
|
|
|
2019-04-15 09:40:22 +02:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
2019-04-15 09:37:51 +02:00
|
|
|
static int sha256_final(void *ctx,
|
|
|
|
unsigned char *md, size_t *mdl, size_t mdsz)
|
2019-03-13 17:26:17 +00:00
|
|
|
{
|
2019-04-15 09:37:51 +02:00
|
|
|
if (mdsz >= SHA256_DIGEST_LENGTH
|
|
|
|
&& SHA256_Final(md, ctx)) {
|
|
|
|
*mdl = SHA256_DIGEST_LENGTH;
|
2019-03-13 17:26:17 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *sha256_newctx(void)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-03-28 17:22:20 +00:00
|
|
|
static size_t sha256_block_size(void)
|
|
|
|
{
|
|
|
|
return SHA256_CBLOCK;
|
|
|
|
}
|
|
|
|
|
2019-03-13 17:26:17 +00:00
|
|
|
extern const OSSL_DISPATCH sha256_functions[];
|
|
|
|
const OSSL_DISPATCH sha256_functions[] = {
|
|
|
|
{ OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))sha256_newctx },
|
|
|
|
{ OSSL_FUNC_DIGEST_INIT, (void (*)(void))SHA256_Init },
|
2019-04-03 15:38:07 +01:00
|
|
|
{ OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))SHA256_Update },
|
2019-03-13 17:26:17 +00:00
|
|
|
{ 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 },
|
2019-03-28 17:22:20 +00:00
|
|
|
{ OSSL_FUNC_DIGEST_BLOCK_SIZE, (void (*)(void))sha256_block_size },
|
2019-03-13 17:26:17 +00:00
|
|
|
{ 0, NULL }
|
|
|
|
};
|