2021-02-18 16:18:32 +01:00
|
|
|
/*
|
|
|
|
* PSA hashing layer on top of Mbed TLS software crypto
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* 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 "common.h"
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
|
|
|
|
|
|
|
#include <psa/crypto.h>
|
|
|
|
#include "psa_crypto_core.h"
|
|
|
|
#include "psa_crypto_hash.h"
|
|
|
|
|
|
|
|
#include <mbedtls/error.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_HASH)
|
|
|
|
psa_status_t mbedtls_psa_hash_abort(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_psa_hash_operation_t *operation)
|
2021-03-08 17:09:48 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (operation->alg) {
|
2021-03-08 17:09:48 +01:00
|
|
|
case 0:
|
|
|
|
/* The object has (apparently) been initialized but it is not
|
|
|
|
* in use. It's ok to call abort on such an object, and there's
|
|
|
|
* nothing to do. */
|
|
|
|
break;
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
|
2021-03-08 17:09:48 +01:00
|
|
|
case PSA_ALG_MD5:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_md5_free(&operation->ctx.md5);
|
2021-03-08 17:09:48 +01:00
|
|
|
break;
|
|
|
|
#endif
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
|
2021-03-08 17:09:48 +01:00
|
|
|
case PSA_ALG_RIPEMD160:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ripemd160_free(&operation->ctx.ripemd160);
|
2021-03-08 17:09:48 +01:00
|
|
|
break;
|
|
|
|
#endif
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
|
2021-03-08 17:09:48 +01:00
|
|
|
case PSA_ALG_SHA_1:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_sha1_free(&operation->ctx.sha1);
|
2021-03-08 17:09:48 +01:00
|
|
|
break;
|
|
|
|
#endif
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
|
2021-03-08 17:09:48 +01:00
|
|
|
case PSA_ALG_SHA_224:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_sha256_free(&operation->ctx.sha256);
|
2021-03-08 17:09:48 +01:00
|
|
|
break;
|
|
|
|
#endif
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
|
2021-03-08 17:09:48 +01:00
|
|
|
case PSA_ALG_SHA_256:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_sha256_free(&operation->ctx.sha256);
|
2021-03-08 17:09:48 +01:00
|
|
|
break;
|
|
|
|
#endif
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
|
2021-03-08 17:09:48 +01:00
|
|
|
case PSA_ALG_SHA_384:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_sha512_free(&operation->ctx.sha512);
|
2021-03-08 17:09:48 +01:00
|
|
|
break;
|
|
|
|
#endif
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
|
2021-03-08 17:09:48 +01:00
|
|
|
case PSA_ALG_SHA_512:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_sha512_free(&operation->ctx.sha512);
|
2021-03-08 17:09:48 +01:00
|
|
|
break;
|
2023-06-26 00:27:45 +02:00
|
|
|
#endif
|
2023-06-26 12:02:12 +02:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224)
|
2023-06-26 00:27:45 +02:00
|
|
|
case PSA_ALG_SHA3_224:
|
2023-06-26 12:02:12 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256)
|
2023-06-26 00:27:45 +02:00
|
|
|
case PSA_ALG_SHA3_256:
|
2023-06-26 12:02:12 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384)
|
2023-06-26 00:27:45 +02:00
|
|
|
case PSA_ALG_SHA3_384:
|
2023-06-26 12:02:12 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
|
2023-06-26 00:27:45 +02:00
|
|
|
case PSA_ALG_SHA3_512:
|
2023-06-26 12:02:12 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224) || \
|
2023-06-26 12:11:23 +02:00
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
|
2023-06-26 00:27:45 +02:00
|
|
|
mbedtls_sha3_free(&operation->ctx.sha3);
|
|
|
|
break;
|
2021-03-08 17:09:48 +01:00
|
|
|
#endif
|
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_ERROR_BAD_STATE;
|
2021-03-08 17:09:48 +01:00
|
|
|
}
|
|
|
|
operation->alg = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_SUCCESS;
|
2021-02-18 16:18:32 +01:00
|
|
|
}
|
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
psa_status_t mbedtls_psa_hash_setup(
|
2021-02-18 16:18:32 +01:00
|
|
|
mbedtls_psa_hash_operation_t *operation,
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_algorithm_t alg)
|
2021-02-18 16:18:32 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
|
|
/* A context must be freshly initialized before it can be set up. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (operation->alg != 0) {
|
|
|
|
return PSA_ERROR_BAD_STATE;
|
2021-02-18 16:18:32 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (alg) {
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
|
2021-02-18 16:18:32 +01:00
|
|
|
case PSA_ALG_MD5:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_md5_init(&operation->ctx.md5);
|
|
|
|
ret = mbedtls_md5_starts(&operation->ctx.md5);
|
2021-02-18 16:18:32 +01:00
|
|
|
break;
|
|
|
|
#endif
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
|
2021-02-18 16:18:32 +01:00
|
|
|
case PSA_ALG_RIPEMD160:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ripemd160_init(&operation->ctx.ripemd160);
|
|
|
|
ret = mbedtls_ripemd160_starts(&operation->ctx.ripemd160);
|
2021-02-18 16:18:32 +01:00
|
|
|
break;
|
|
|
|
#endif
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
|
2021-02-18 16:18:32 +01:00
|
|
|
case PSA_ALG_SHA_1:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_sha1_init(&operation->ctx.sha1);
|
|
|
|
ret = mbedtls_sha1_starts(&operation->ctx.sha1);
|
2021-02-18 16:18:32 +01:00
|
|
|
break;
|
|
|
|
#endif
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
|
2021-02-18 16:18:32 +01:00
|
|
|
case PSA_ALG_SHA_224:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_sha256_init(&operation->ctx.sha256);
|
|
|
|
ret = mbedtls_sha256_starts(&operation->ctx.sha256, 1);
|
2021-02-18 16:18:32 +01:00
|
|
|
break;
|
|
|
|
#endif
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
|
2021-02-18 16:18:32 +01:00
|
|
|
case PSA_ALG_SHA_256:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_sha256_init(&operation->ctx.sha256);
|
|
|
|
ret = mbedtls_sha256_starts(&operation->ctx.sha256, 0);
|
2021-02-18 16:18:32 +01:00
|
|
|
break;
|
|
|
|
#endif
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
|
2021-02-18 16:18:32 +01:00
|
|
|
case PSA_ALG_SHA_384:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_sha512_init(&operation->ctx.sha512);
|
|
|
|
ret = mbedtls_sha512_starts(&operation->ctx.sha512, 1);
|
2021-02-18 16:18:32 +01:00
|
|
|
break;
|
|
|
|
#endif
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
|
2021-02-18 16:18:32 +01:00
|
|
|
case PSA_ALG_SHA_512:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_sha512_init(&operation->ctx.sha512);
|
|
|
|
ret = mbedtls_sha512_starts(&operation->ctx.sha512, 0);
|
2021-02-18 16:18:32 +01:00
|
|
|
break;
|
2023-06-26 00:27:45 +02:00
|
|
|
#endif
|
2023-06-26 12:02:12 +02:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224)
|
2023-06-26 00:27:45 +02:00
|
|
|
case PSA_ALG_SHA3_224:
|
|
|
|
mbedtls_sha3_init(&operation->ctx.sha3);
|
|
|
|
ret = mbedtls_sha3_starts(&operation->ctx.sha3, MBEDTLS_SHA3_224);
|
|
|
|
break;
|
2023-06-26 12:02:12 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256)
|
2023-06-26 00:27:45 +02:00
|
|
|
case PSA_ALG_SHA3_256:
|
|
|
|
mbedtls_sha3_init(&operation->ctx.sha3);
|
|
|
|
ret = mbedtls_sha3_starts(&operation->ctx.sha3, MBEDTLS_SHA3_256);
|
|
|
|
break;
|
2023-06-26 12:02:12 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384)
|
2023-06-26 00:27:45 +02:00
|
|
|
case PSA_ALG_SHA3_384:
|
|
|
|
mbedtls_sha3_init(&operation->ctx.sha3);
|
|
|
|
ret = mbedtls_sha3_starts(&operation->ctx.sha3, MBEDTLS_SHA3_384);
|
|
|
|
break;
|
2023-06-26 12:02:12 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
|
2023-06-26 00:27:45 +02:00
|
|
|
case PSA_ALG_SHA3_512:
|
|
|
|
mbedtls_sha3_init(&operation->ctx.sha3);
|
|
|
|
ret = mbedtls_sha3_starts(&operation->ctx.sha3, MBEDTLS_SHA3_512);
|
|
|
|
break;
|
2021-02-18 16:18:32 +01:00
|
|
|
#endif
|
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_ALG_IS_HASH(alg) ?
|
|
|
|
PSA_ERROR_NOT_SUPPORTED :
|
|
|
|
PSA_ERROR_INVALID_ARGUMENT;
|
2021-02-18 16:18:32 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == 0) {
|
2021-02-18 16:18:32 +01:00
|
|
|
operation->alg = alg;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
mbedtls_psa_hash_abort(operation);
|
|
|
|
}
|
|
|
|
return mbedtls_to_psa_error(ret);
|
2021-02-18 16:18:32 +01:00
|
|
|
}
|
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
psa_status_t mbedtls_psa_hash_clone(
|
2021-02-18 16:18:32 +01:00
|
|
|
const mbedtls_psa_hash_operation_t *source_operation,
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_psa_hash_operation_t *target_operation)
|
2021-02-18 16:18:32 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (source_operation->alg) {
|
2021-02-18 16:18:32 +01:00
|
|
|
case 0:
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_ERROR_BAD_STATE;
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
|
2021-02-18 16:18:32 +01:00
|
|
|
case PSA_ALG_MD5:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_md5_clone(&target_operation->ctx.md5,
|
|
|
|
&source_operation->ctx.md5);
|
2021-02-18 16:18:32 +01:00
|
|
|
break;
|
|
|
|
#endif
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
|
2021-02-18 16:18:32 +01:00
|
|
|
case PSA_ALG_RIPEMD160:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ripemd160_clone(&target_operation->ctx.ripemd160,
|
|
|
|
&source_operation->ctx.ripemd160);
|
2021-02-18 16:18:32 +01:00
|
|
|
break;
|
|
|
|
#endif
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
|
2021-02-18 16:18:32 +01:00
|
|
|
case PSA_ALG_SHA_1:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_sha1_clone(&target_operation->ctx.sha1,
|
|
|
|
&source_operation->ctx.sha1);
|
2021-02-18 16:18:32 +01:00
|
|
|
break;
|
|
|
|
#endif
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
|
2021-02-18 16:18:32 +01:00
|
|
|
case PSA_ALG_SHA_224:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_sha256_clone(&target_operation->ctx.sha256,
|
|
|
|
&source_operation->ctx.sha256);
|
2021-02-18 16:18:32 +01:00
|
|
|
break;
|
|
|
|
#endif
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
|
2021-02-18 16:18:32 +01:00
|
|
|
case PSA_ALG_SHA_256:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_sha256_clone(&target_operation->ctx.sha256,
|
|
|
|
&source_operation->ctx.sha256);
|
2021-02-18 16:18:32 +01:00
|
|
|
break;
|
|
|
|
#endif
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
|
2021-02-18 16:18:32 +01:00
|
|
|
case PSA_ALG_SHA_384:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_sha512_clone(&target_operation->ctx.sha512,
|
|
|
|
&source_operation->ctx.sha512);
|
2021-02-18 16:18:32 +01:00
|
|
|
break;
|
|
|
|
#endif
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
|
2021-02-18 16:18:32 +01:00
|
|
|
case PSA_ALG_SHA_512:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_sha512_clone(&target_operation->ctx.sha512,
|
|
|
|
&source_operation->ctx.sha512);
|
2021-02-18 16:18:32 +01:00
|
|
|
break;
|
2023-06-26 00:27:45 +02:00
|
|
|
#endif
|
2023-06-26 12:02:12 +02:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224)
|
2023-06-26 00:27:45 +02:00
|
|
|
case PSA_ALG_SHA3_224:
|
2023-06-26 12:02:12 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256)
|
2023-06-26 00:27:45 +02:00
|
|
|
case PSA_ALG_SHA3_256:
|
2023-06-26 12:02:12 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384)
|
2023-06-26 00:27:45 +02:00
|
|
|
case PSA_ALG_SHA3_384:
|
2023-06-26 12:02:12 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
|
2023-06-26 00:27:45 +02:00
|
|
|
case PSA_ALG_SHA3_512:
|
2023-06-26 12:02:12 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224) || \
|
2023-06-26 12:11:23 +02:00
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
|
2023-06-26 00:27:45 +02:00
|
|
|
mbedtls_sha3_clone(&target_operation->ctx.sha3,
|
|
|
|
&source_operation->ctx.sha3);
|
|
|
|
break;
|
2021-02-18 16:18:32 +01:00
|
|
|
#endif
|
|
|
|
default:
|
2021-03-04 18:09:49 +01:00
|
|
|
(void) source_operation;
|
|
|
|
(void) target_operation;
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
2021-02-18 16:18:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
target_operation->alg = source_operation->alg;
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_SUCCESS;
|
2021-02-18 16:18:32 +01:00
|
|
|
}
|
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
psa_status_t mbedtls_psa_hash_update(
|
2021-02-18 16:18:32 +01:00
|
|
|
mbedtls_psa_hash_operation_t *operation,
|
|
|
|
const uint8_t *input,
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t input_length)
|
2021-02-18 16:18:32 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (operation->alg) {
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
|
2021-02-18 16:18:32 +01:00
|
|
|
case PSA_ALG_MD5:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_md5_update(&operation->ctx.md5,
|
|
|
|
input, input_length);
|
2021-02-18 16:18:32 +01:00
|
|
|
break;
|
|
|
|
#endif
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
|
2021-02-18 16:18:32 +01:00
|
|
|
case PSA_ALG_RIPEMD160:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ripemd160_update(&operation->ctx.ripemd160,
|
|
|
|
input, input_length);
|
2021-02-18 16:18:32 +01:00
|
|
|
break;
|
|
|
|
#endif
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
|
2021-02-18 16:18:32 +01:00
|
|
|
case PSA_ALG_SHA_1:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_sha1_update(&operation->ctx.sha1,
|
|
|
|
input, input_length);
|
2021-02-18 16:18:32 +01:00
|
|
|
break;
|
|
|
|
#endif
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
|
2021-02-18 16:18:32 +01:00
|
|
|
case PSA_ALG_SHA_224:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_sha256_update(&operation->ctx.sha256,
|
|
|
|
input, input_length);
|
2021-02-18 16:18:32 +01:00
|
|
|
break;
|
|
|
|
#endif
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
|
2021-02-18 16:18:32 +01:00
|
|
|
case PSA_ALG_SHA_256:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_sha256_update(&operation->ctx.sha256,
|
|
|
|
input, input_length);
|
2021-02-18 16:18:32 +01:00
|
|
|
break;
|
|
|
|
#endif
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
|
2021-02-18 16:18:32 +01:00
|
|
|
case PSA_ALG_SHA_384:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_sha512_update(&operation->ctx.sha512,
|
|
|
|
input, input_length);
|
2021-02-18 16:18:32 +01:00
|
|
|
break;
|
|
|
|
#endif
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
|
2021-02-18 16:18:32 +01:00
|
|
|
case PSA_ALG_SHA_512:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_sha512_update(&operation->ctx.sha512,
|
|
|
|
input, input_length);
|
2021-02-18 16:18:32 +01:00
|
|
|
break;
|
2023-06-26 00:27:45 +02:00
|
|
|
#endif
|
2023-06-26 12:02:12 +02:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224)
|
2023-06-26 00:27:45 +02:00
|
|
|
case PSA_ALG_SHA3_224:
|
2023-06-26 12:02:12 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256)
|
2023-06-26 00:27:45 +02:00
|
|
|
case PSA_ALG_SHA3_256:
|
2023-06-26 12:02:12 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384)
|
2023-06-26 00:27:45 +02:00
|
|
|
case PSA_ALG_SHA3_384:
|
2023-06-26 12:02:12 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
|
2023-06-26 00:27:45 +02:00
|
|
|
case PSA_ALG_SHA3_512:
|
2023-06-26 12:02:12 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
|
2023-06-26 12:11:23 +02:00
|
|
|
ret = mbedtls_sha3_update(&operation->ctx.sha3,
|
|
|
|
input, input_length);
|
|
|
|
break;
|
2021-02-18 16:18:32 +01:00
|
|
|
#endif
|
|
|
|
default:
|
2021-03-04 18:09:49 +01:00
|
|
|
(void) input;
|
|
|
|
(void) input_length;
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_ERROR_BAD_STATE;
|
2021-02-18 16:18:32 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_to_psa_error(ret);
|
2021-02-18 16:18:32 +01:00
|
|
|
}
|
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
psa_status_t mbedtls_psa_hash_finish(
|
2021-02-18 16:18:32 +01:00
|
|
|
mbedtls_psa_hash_operation_t *operation,
|
|
|
|
uint8_t *hash,
|
|
|
|
size_t hash_size,
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t *hash_length)
|
2021-02-18 16:18:32 +01:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t actual_hash_length = PSA_HASH_LENGTH(operation->alg);
|
2021-02-18 16:18:32 +01:00
|
|
|
|
|
|
|
/* Fill the output buffer with something that isn't a valid hash
|
|
|
|
* (barring an attack on the hash and deliberately-crafted input),
|
|
|
|
* in case the caller doesn't check the return status properly. */
|
|
|
|
*hash_length = hash_size;
|
|
|
|
/* If hash_size is 0 then hash may be NULL and then the
|
|
|
|
* call to memset would have undefined behavior. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (hash_size != 0) {
|
|
|
|
memset(hash, '!', hash_size);
|
|
|
|
}
|
2021-02-18 16:18:32 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (hash_size < actual_hash_length) {
|
2021-02-18 16:18:32 +01:00
|
|
|
status = PSA_ERROR_BUFFER_TOO_SMALL;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (operation->alg) {
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
|
2021-02-18 16:18:32 +01:00
|
|
|
case PSA_ALG_MD5:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_md5_finish(&operation->ctx.md5, hash);
|
2021-02-18 16:18:32 +01:00
|
|
|
break;
|
|
|
|
#endif
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
|
2021-02-18 16:18:32 +01:00
|
|
|
case PSA_ALG_RIPEMD160:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ripemd160_finish(&operation->ctx.ripemd160, hash);
|
2021-02-18 16:18:32 +01:00
|
|
|
break;
|
|
|
|
#endif
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
|
2021-02-18 16:18:32 +01:00
|
|
|
case PSA_ALG_SHA_1:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_sha1_finish(&operation->ctx.sha1, hash);
|
2021-02-18 16:18:32 +01:00
|
|
|
break;
|
|
|
|
#endif
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
|
2021-02-18 16:18:32 +01:00
|
|
|
case PSA_ALG_SHA_224:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_sha256_finish(&operation->ctx.sha256, hash);
|
2021-02-18 16:18:32 +01:00
|
|
|
break;
|
|
|
|
#endif
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
|
2021-02-18 16:18:32 +01:00
|
|
|
case PSA_ALG_SHA_256:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_sha256_finish(&operation->ctx.sha256, hash);
|
2021-02-18 16:18:32 +01:00
|
|
|
break;
|
|
|
|
#endif
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
|
2021-02-18 16:18:32 +01:00
|
|
|
case PSA_ALG_SHA_384:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_sha512_finish(&operation->ctx.sha512, hash);
|
2021-02-18 16:18:32 +01:00
|
|
|
break;
|
|
|
|
#endif
|
2021-03-13 18:50:11 +01:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
|
2021-02-18 16:18:32 +01:00
|
|
|
case PSA_ALG_SHA_512:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_sha512_finish(&operation->ctx.sha512, hash);
|
2021-02-18 16:18:32 +01:00
|
|
|
break;
|
2023-06-26 00:27:45 +02:00
|
|
|
#endif
|
2023-06-26 12:02:12 +02:00
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224)
|
2023-06-26 00:27:45 +02:00
|
|
|
case PSA_ALG_SHA3_224:
|
2023-06-26 12:02:12 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256)
|
2023-06-26 00:27:45 +02:00
|
|
|
case PSA_ALG_SHA3_256:
|
2023-06-26 12:02:12 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384)
|
2023-06-26 00:27:45 +02:00
|
|
|
case PSA_ALG_SHA3_384:
|
2023-06-26 12:02:12 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
|
2023-06-26 00:27:45 +02:00
|
|
|
case PSA_ALG_SHA3_512:
|
2023-06-26 12:02:12 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384) || \
|
|
|
|
defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
|
2023-06-26 12:11:23 +02:00
|
|
|
ret = mbedtls_sha3_finish(&operation->ctx.sha3, hash, hash_size);
|
|
|
|
break;
|
2021-02-18 16:18:32 +01:00
|
|
|
#endif
|
|
|
|
default:
|
2021-03-04 18:09:49 +01:00
|
|
|
(void) hash;
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_ERROR_BAD_STATE;
|
2021-02-18 16:18:32 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
status = mbedtls_to_psa_error(ret);
|
2021-02-18 16:18:32 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status == PSA_SUCCESS) {
|
2021-02-18 16:18:32 +01:00
|
|
|
*hash_length = actual_hash_length;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
return status;
|
2021-02-18 16:18:32 +01:00
|
|
|
}
|
|
|
|
|
2021-03-13 18:50:11 +01:00
|
|
|
psa_status_t mbedtls_psa_hash_compute(
|
2021-03-08 17:09:48 +01:00
|
|
|
psa_algorithm_t alg,
|
|
|
|
const uint8_t *input,
|
|
|
|
size_t input_length,
|
|
|
|
uint8_t *hash,
|
|
|
|
size_t hash_size,
|
|
|
|
size_t *hash_length)
|
|
|
|
{
|
|
|
|
mbedtls_psa_hash_operation_t operation = MBEDTLS_PSA_HASH_OPERATION_INIT;
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2021-03-15 12:32:48 +01:00
|
|
|
psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
|
2021-03-08 17:09:48 +01:00
|
|
|
|
|
|
|
*hash_length = hash_size;
|
2023-01-11 14:50:10 +01:00
|
|
|
status = mbedtls_psa_hash_setup(&operation, alg);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-03-08 17:09:48 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
status = mbedtls_psa_hash_update(&operation, input, input_length);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-03-08 17:09:48 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
status = mbedtls_psa_hash_finish(&operation, hash, hash_size, hash_length);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2021-03-08 17:09:48 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-03-08 17:09:48 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
abort_status = mbedtls_psa_hash_abort(&operation);
|
|
|
|
if (status == PSA_SUCCESS) {
|
|
|
|
return abort_status;
|
|
|
|
} else {
|
|
|
|
return status;
|
|
|
|
}
|
2021-03-15 12:32:48 +01:00
|
|
|
|
2021-03-08 17:09:48 +01:00
|
|
|
}
|
2021-03-08 20:28:18 +01:00
|
|
|
#endif /* MBEDTLS_PSA_BUILTIN_HASH */
|
2021-02-18 16:18:32 +01:00
|
|
|
|
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_C */
|