Merge pull request #7120 from mpg/md-light
Define "MD light" subset of MD
This commit is contained in:
commit
228a30d16c
7 changed files with 246 additions and 239 deletions
292
library/md.c
292
library/md.c
|
@ -23,7 +23,23 @@
|
|||
|
||||
#include "common.h"
|
||||
|
||||
#if defined(MBEDTLS_MD_C)
|
||||
/*
|
||||
* Availability of functions in this module is controlled by two
|
||||
* feature macros:
|
||||
* - MBEDTLS_MD_C enables the whole module;
|
||||
* - MBEDTLS_MD_LIGHT enables only functions for hashing and accessing
|
||||
* most hash metadata (everything except string names); is it
|
||||
* automatically set whenever MBEDTLS_MD_C is defined.
|
||||
*
|
||||
* In this file, functions from MD_LIGHT are at the top, MD_C at the end.
|
||||
*
|
||||
* In the future we may want to change the contract of some functions
|
||||
* (behaviour with NULL arguments) depending on whether MD_C is defined or
|
||||
* only MD_LIGHT. Also, the exact scope of MD_LIGHT might vary.
|
||||
*
|
||||
* For these reasons, we're keeping MD_LIGHT internal for now.
|
||||
*/
|
||||
#if defined(MBEDTLS_MD_LIGHT)
|
||||
|
||||
#include "mbedtls/md.h"
|
||||
#include "md_wrap.h"
|
||||
|
@ -107,91 +123,6 @@ const mbedtls_md_info_t mbedtls_sha512_info = {
|
|||
};
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Reminder: update profiles in x509_crt.c when adding a new hash!
|
||||
*/
|
||||
static const int supported_digests[] = {
|
||||
|
||||
#if defined(MBEDTLS_SHA512_C)
|
||||
MBEDTLS_MD_SHA512,
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SHA384_C)
|
||||
MBEDTLS_MD_SHA384,
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SHA256_C)
|
||||
MBEDTLS_MD_SHA256,
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA224_C)
|
||||
MBEDTLS_MD_SHA224,
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SHA1_C)
|
||||
MBEDTLS_MD_SHA1,
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_RIPEMD160_C)
|
||||
MBEDTLS_MD_RIPEMD160,
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_MD5_C)
|
||||
MBEDTLS_MD_MD5,
|
||||
#endif
|
||||
|
||||
MBEDTLS_MD_NONE
|
||||
};
|
||||
|
||||
const int *mbedtls_md_list(void)
|
||||
{
|
||||
return supported_digests;
|
||||
}
|
||||
|
||||
const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name)
|
||||
{
|
||||
if (NULL == md_name) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Get the appropriate digest information */
|
||||
#if defined(MBEDTLS_MD5_C)
|
||||
if (!strcmp("MD5", md_name)) {
|
||||
return mbedtls_md_info_from_type(MBEDTLS_MD_MD5);
|
||||
}
|
||||
#endif
|
||||
#if defined(MBEDTLS_RIPEMD160_C)
|
||||
if (!strcmp("RIPEMD160", md_name)) {
|
||||
return mbedtls_md_info_from_type(MBEDTLS_MD_RIPEMD160);
|
||||
}
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA1_C)
|
||||
if (!strcmp("SHA1", md_name) || !strcmp("SHA", md_name)) {
|
||||
return mbedtls_md_info_from_type(MBEDTLS_MD_SHA1);
|
||||
}
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA224_C)
|
||||
if (!strcmp("SHA224", md_name)) {
|
||||
return mbedtls_md_info_from_type(MBEDTLS_MD_SHA224);
|
||||
}
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA256_C)
|
||||
if (!strcmp("SHA256", md_name)) {
|
||||
return mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
|
||||
}
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA384_C)
|
||||
if (!strcmp("SHA384", md_name)) {
|
||||
return mbedtls_md_info_from_type(MBEDTLS_MD_SHA384);
|
||||
}
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA512_C)
|
||||
if (!strcmp("SHA512", md_name)) {
|
||||
return mbedtls_md_info_from_type(MBEDTLS_MD_SHA512);
|
||||
}
|
||||
#endif
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const mbedtls_md_info_t *mbedtls_md_info_from_type(mbedtls_md_type_t md_type)
|
||||
{
|
||||
switch (md_type) {
|
||||
|
@ -228,16 +159,6 @@ const mbedtls_md_info_t *mbedtls_md_info_from_type(mbedtls_md_type_t md_type)
|
|||
}
|
||||
}
|
||||
|
||||
const mbedtls_md_info_t *mbedtls_md_info_from_ctx(
|
||||
const mbedtls_md_context_t *ctx)
|
||||
{
|
||||
if (ctx == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return ctx->MBEDTLS_PRIVATE(md_info);
|
||||
}
|
||||
|
||||
void mbedtls_md_init(mbedtls_md_context_t *ctx)
|
||||
{
|
||||
memset(ctx, 0, sizeof(mbedtls_md_context_t));
|
||||
|
@ -586,6 +507,125 @@ int mbedtls_md(const mbedtls_md_info_t *md_info, const unsigned char *input, siz
|
|||
}
|
||||
}
|
||||
|
||||
unsigned char mbedtls_md_get_size(const mbedtls_md_info_t *md_info)
|
||||
{
|
||||
if (md_info == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return md_info->size;
|
||||
}
|
||||
|
||||
mbedtls_md_type_t mbedtls_md_get_type(const mbedtls_md_info_t *md_info)
|
||||
{
|
||||
if (md_info == NULL) {
|
||||
return MBEDTLS_MD_NONE;
|
||||
}
|
||||
|
||||
return md_info->type;
|
||||
}
|
||||
|
||||
/************************************************************************
|
||||
* Functions above this separator are part of MBEDTLS_MD_LIGHT, *
|
||||
* functions below are only available when MBEDTLS_MD_C is set. *
|
||||
************************************************************************/
|
||||
#if defined(MBEDTLS_MD_C)
|
||||
|
||||
/*
|
||||
* Reminder: update profiles in x509_crt.c when adding a new hash!
|
||||
*/
|
||||
static const int supported_digests[] = {
|
||||
|
||||
#if defined(MBEDTLS_SHA512_C)
|
||||
MBEDTLS_MD_SHA512,
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SHA384_C)
|
||||
MBEDTLS_MD_SHA384,
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SHA256_C)
|
||||
MBEDTLS_MD_SHA256,
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA224_C)
|
||||
MBEDTLS_MD_SHA224,
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SHA1_C)
|
||||
MBEDTLS_MD_SHA1,
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_RIPEMD160_C)
|
||||
MBEDTLS_MD_RIPEMD160,
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_MD5_C)
|
||||
MBEDTLS_MD_MD5,
|
||||
#endif
|
||||
|
||||
MBEDTLS_MD_NONE
|
||||
};
|
||||
|
||||
const int *mbedtls_md_list(void)
|
||||
{
|
||||
return supported_digests;
|
||||
}
|
||||
|
||||
const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name)
|
||||
{
|
||||
if (NULL == md_name) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Get the appropriate digest information */
|
||||
#if defined(MBEDTLS_MD5_C)
|
||||
if (!strcmp("MD5", md_name)) {
|
||||
return mbedtls_md_info_from_type(MBEDTLS_MD_MD5);
|
||||
}
|
||||
#endif
|
||||
#if defined(MBEDTLS_RIPEMD160_C)
|
||||
if (!strcmp("RIPEMD160", md_name)) {
|
||||
return mbedtls_md_info_from_type(MBEDTLS_MD_RIPEMD160);
|
||||
}
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA1_C)
|
||||
if (!strcmp("SHA1", md_name) || !strcmp("SHA", md_name)) {
|
||||
return mbedtls_md_info_from_type(MBEDTLS_MD_SHA1);
|
||||
}
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA224_C)
|
||||
if (!strcmp("SHA224", md_name)) {
|
||||
return mbedtls_md_info_from_type(MBEDTLS_MD_SHA224);
|
||||
}
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA256_C)
|
||||
if (!strcmp("SHA256", md_name)) {
|
||||
return mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
|
||||
}
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA384_C)
|
||||
if (!strcmp("SHA384", md_name)) {
|
||||
return mbedtls_md_info_from_type(MBEDTLS_MD_SHA384);
|
||||
}
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA512_C)
|
||||
if (!strcmp("SHA512", md_name)) {
|
||||
return mbedtls_md_info_from_type(MBEDTLS_MD_SHA512);
|
||||
}
|
||||
#endif
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const mbedtls_md_info_t *mbedtls_md_info_from_ctx(
|
||||
const mbedtls_md_context_t *ctx)
|
||||
{
|
||||
if (ctx == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return ctx->MBEDTLS_PRIVATE(md_info);
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_FS_IO)
|
||||
int mbedtls_md_file(const mbedtls_md_info_t *md_info, const char *path, unsigned char *output)
|
||||
{
|
||||
|
@ -774,64 +814,6 @@ cleanup:
|
|||
return ret;
|
||||
}
|
||||
|
||||
int mbedtls_md_process(mbedtls_md_context_t *ctx, const unsigned char *data)
|
||||
{
|
||||
if (ctx == NULL || ctx->md_info == NULL) {
|
||||
return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
switch (ctx->md_info->type) {
|
||||
#if defined(MBEDTLS_MD5_C)
|
||||
case MBEDTLS_MD_MD5:
|
||||
return mbedtls_internal_md5_process(ctx->md_ctx, data);
|
||||
#endif
|
||||
#if defined(MBEDTLS_RIPEMD160_C)
|
||||
case MBEDTLS_MD_RIPEMD160:
|
||||
return mbedtls_internal_ripemd160_process(ctx->md_ctx, data);
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA1_C)
|
||||
case MBEDTLS_MD_SHA1:
|
||||
return mbedtls_internal_sha1_process(ctx->md_ctx, data);
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA224_C)
|
||||
case MBEDTLS_MD_SHA224:
|
||||
return mbedtls_internal_sha256_process(ctx->md_ctx, data);
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA256_C)
|
||||
case MBEDTLS_MD_SHA256:
|
||||
return mbedtls_internal_sha256_process(ctx->md_ctx, data);
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA384_C)
|
||||
case MBEDTLS_MD_SHA384:
|
||||
return mbedtls_internal_sha512_process(ctx->md_ctx, data);
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA512_C)
|
||||
case MBEDTLS_MD_SHA512:
|
||||
return mbedtls_internal_sha512_process(ctx->md_ctx, data);
|
||||
#endif
|
||||
default:
|
||||
return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned char mbedtls_md_get_size(const mbedtls_md_info_t *md_info)
|
||||
{
|
||||
if (md_info == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return md_info->size;
|
||||
}
|
||||
|
||||
mbedtls_md_type_t mbedtls_md_get_type(const mbedtls_md_info_t *md_info)
|
||||
{
|
||||
if (md_info == NULL) {
|
||||
return MBEDTLS_MD_NONE;
|
||||
}
|
||||
|
||||
return md_info->type;
|
||||
}
|
||||
|
||||
const char *mbedtls_md_get_name(const mbedtls_md_info_t *md_info)
|
||||
{
|
||||
if (md_info == NULL) {
|
||||
|
@ -842,3 +824,5 @@ const char *mbedtls_md_get_name(const mbedtls_md_info_t *md_info)
|
|||
}
|
||||
|
||||
#endif /* MBEDTLS_MD_C */
|
||||
|
||||
#endif /* MBEDTLS_MD_LIGHT */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue