mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-08-20 11:21:22 +03:00
.github
3rdparty
ChangeLog.d
cmake
configs
docs
doxygen
include
library
.gitignore
CMakeLists.txt
Makefile
aes.c
aesni.c
aesni.h
aria.c
asn1parse.c
asn1write.c
base64.c
bignum.c
bignum_core.c
bignum_core.h
bignum_mod.c
bignum_mod.h
bignum_mod_raw.c
bignum_mod_raw.h
bn_mul.h
camellia.c
ccm.c
chacha20.c
chachapoly.c
check_crypto_config.h
cipher.c
cipher_wrap.c
cipher_wrap.h
cmac.c
common.h
constant_time.c
constant_time_internal.h
constant_time_invasive.h
ctr_drbg.c
debug.c
des.c
dhm.c
ecdh.c
ecdsa.c
ecjpake.c
ecp.c
ecp_curves.c
ecp_internal_alt.h
ecp_invasive.h
entropy.c
entropy_poll.c
entropy_poll.h
gcm.c
hash_info.c
hash_info.h
hkdf.c
hmac_drbg.c
lmots.c
lmots.h
lms.c
md.c
md5.c
md_wrap.h
memory_buffer_alloc.c
mps_common.h
mps_error.h
mps_reader.c
mps_reader.h
mps_trace.c
mps_trace.h
net_sockets.c
nist_kw.c
oid.c
padlock.c
padlock.h
pem.c
pk.c
pk_wrap.c
pk_wrap.h
pkcs12.c
pkcs5.c
pkparse.c
pkwrite.c
pkwrite.h
platform.c
platform_util.c
poly1305.c
psa_crypto.c
psa_crypto_aead.c
psa_crypto_aead.h
psa_crypto_cipher.c
psa_crypto_cipher.h
psa_crypto_client.c
psa_crypto_core.h
psa_crypto_driver_wrappers.h
psa_crypto_ecp.c
psa_crypto_ecp.h
psa_crypto_hash.c
psa_crypto_hash.h
psa_crypto_invasive.h
psa_crypto_its.h
psa_crypto_mac.c
psa_crypto_mac.h
psa_crypto_pake.c
psa_crypto_random_impl.h
psa_crypto_rsa.c
psa_crypto_rsa.h
psa_crypto_se.c
psa_crypto_se.h
psa_crypto_slot_management.c
psa_crypto_slot_management.h
psa_crypto_storage.c
psa_crypto_storage.h
psa_its_file.c
ripemd160.c
rsa.c
rsa_alt_helpers.c
rsa_alt_helpers.h
sha1.c
sha256.c
sha512.c
ssl_cache.c
ssl_ciphersuites.c
ssl_client.c
ssl_client.h
ssl_cookie.c
ssl_debug_helpers.h
ssl_misc.h
ssl_msg.c
ssl_ticket.c
ssl_tls.c
ssl_tls12_client.c
ssl_tls12_server.c
ssl_tls13_client.c
ssl_tls13_generic.c
ssl_tls13_invasive.h
ssl_tls13_keys.c
ssl_tls13_keys.h
ssl_tls13_server.c
threading.c
timing.c
version.c
x509.c
x509_create.c
x509_crl.c
x509_crt.c
x509_csr.c
x509write_crt.c
x509write_csr.c
programs
scripts
tests
visualc
.gitattributes
.gitignore
.globalrc
.mypy.ini
.pylintrc
.travis.yml
BRANCHES.md
BUGS.md
CMakeLists.txt
CONTRIBUTING.md
ChangeLog
DartConfiguration.tcl
LICENSE
Makefile
README.md
SECURITY.md
SUPPORT.md
dco.txt
As a public header, it should no longer include common.h, just use build_info.h which is what we actually need anyway. Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
128 lines
3.5 KiB
C
128 lines
3.5 KiB
C
/*
|
|
* Hash information that's independent from the crypto implementation.
|
|
*
|
|
* (See the corresponding header file for usage notes.)
|
|
*/
|
|
/*
|
|
* Copyright The Mbed TLS Contributors
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
* not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
#include "hash_info.h"
|
|
#include "mbedtls/legacy_or_psa.h"
|
|
#include "mbedtls/error.h"
|
|
|
|
typedef struct
|
|
{
|
|
psa_algorithm_t psa_alg;
|
|
mbedtls_md_type_t md_type;
|
|
unsigned char size;
|
|
unsigned char block_size;
|
|
} hash_entry;
|
|
|
|
static const hash_entry hash_table[] = {
|
|
#if defined(MBEDTLS_HAS_ALG_MD5_VIA_LOWLEVEL_OR_PSA)
|
|
{ PSA_ALG_MD5, MBEDTLS_MD_MD5, 16, 64 },
|
|
#endif
|
|
#if defined(MBEDTLS_HAS_ALG_RIPEMD160_VIA_LOWLEVEL_OR_PSA)
|
|
{ PSA_ALG_RIPEMD160, MBEDTLS_MD_RIPEMD160, 20, 64 },
|
|
#endif
|
|
#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_LOWLEVEL_OR_PSA)
|
|
{ PSA_ALG_SHA_1, MBEDTLS_MD_SHA1, 20, 64 },
|
|
#endif
|
|
#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_LOWLEVEL_OR_PSA)
|
|
{ PSA_ALG_SHA_224, MBEDTLS_MD_SHA224, 28, 64 },
|
|
#endif
|
|
#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_LOWLEVEL_OR_PSA)
|
|
{ PSA_ALG_SHA_256, MBEDTLS_MD_SHA256, 32, 64 },
|
|
#endif
|
|
#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_LOWLEVEL_OR_PSA)
|
|
{ PSA_ALG_SHA_384, MBEDTLS_MD_SHA384, 48, 128 },
|
|
#endif
|
|
#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_LOWLEVEL_OR_PSA)
|
|
{ PSA_ALG_SHA_512, MBEDTLS_MD_SHA512, 64, 128 },
|
|
#endif
|
|
{ PSA_ALG_NONE, MBEDTLS_MD_NONE, 0, 0 },
|
|
};
|
|
|
|
/* Get size from MD type */
|
|
unsigned char mbedtls_hash_info_get_size( mbedtls_md_type_t md_type )
|
|
{
|
|
const hash_entry *entry = hash_table;
|
|
while( entry->md_type != MBEDTLS_MD_NONE &&
|
|
entry->md_type != md_type )
|
|
{
|
|
entry++;
|
|
}
|
|
|
|
return entry->size;
|
|
}
|
|
|
|
/* Get block size from MD type */
|
|
unsigned char mbedtls_hash_info_get_block_size( mbedtls_md_type_t md_type )
|
|
{
|
|
const hash_entry *entry = hash_table;
|
|
while( entry->md_type != MBEDTLS_MD_NONE &&
|
|
entry->md_type != md_type )
|
|
{
|
|
entry++;
|
|
}
|
|
|
|
return entry->block_size;
|
|
}
|
|
|
|
/* Get PSA from MD */
|
|
psa_algorithm_t mbedtls_hash_info_psa_from_md( mbedtls_md_type_t md_type )
|
|
{
|
|
const hash_entry *entry = hash_table;
|
|
while( entry->md_type != MBEDTLS_MD_NONE &&
|
|
entry->md_type != md_type )
|
|
{
|
|
entry++;
|
|
}
|
|
|
|
return entry->psa_alg;
|
|
}
|
|
|
|
/* Get MD from PSA */
|
|
mbedtls_md_type_t mbedtls_hash_info_md_from_psa( psa_algorithm_t psa_alg )
|
|
{
|
|
const hash_entry *entry = hash_table;
|
|
while( entry->md_type != MBEDTLS_MD_NONE &&
|
|
entry->psa_alg != psa_alg )
|
|
{
|
|
entry++;
|
|
}
|
|
|
|
return entry->md_type;
|
|
}
|
|
|
|
int mbedtls_md_error_from_psa( psa_status_t status )
|
|
{
|
|
switch( status )
|
|
{
|
|
case PSA_SUCCESS:
|
|
return( 0 );
|
|
case PSA_ERROR_NOT_SUPPORTED:
|
|
return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
|
|
case PSA_ERROR_INVALID_ARGUMENT:
|
|
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
|
case PSA_ERROR_INSUFFICIENT_MEMORY:
|
|
return( MBEDTLS_ERR_MD_ALLOC_FAILED );
|
|
default:
|
|
return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
|
|
}
|
|
}
|