Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 1 | /* |
| 2 | * PSA hashing layer on top of Mbed TLS software crypto |
| 3 | */ |
| 4 | /* |
| 5 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 7ff7965 | 2023-11-03 12:04:52 +0000 | [diff] [blame] | 6 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include "common.h" |
| 10 | |
| 11 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
| 12 | |
| 13 | #include <psa/crypto.h> |
| 14 | #include "psa_crypto_core.h" |
| 15 | #include "psa_crypto_hash.h" |
| 16 | |
| 17 | #include <mbedtls/error.h> |
| 18 | #include <string.h> |
| 19 | |
Steven Cooreman | 5f88e77 | 2021-03-15 11:07:12 +0100 | [diff] [blame] | 20 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 21 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \ |
| 22 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \ |
| 23 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 24 | const mbedtls_md_info_t *mbedtls_md_info_from_psa(psa_algorithm_t alg) |
Steven Cooreman | 5f88e77 | 2021-03-15 11:07:12 +0100 | [diff] [blame] | 25 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 26 | switch (alg) { |
Steven Cooreman | 5f88e77 | 2021-03-15 11:07:12 +0100 | [diff] [blame] | 27 | #if defined(MBEDTLS_MD2_C) |
| 28 | case PSA_ALG_MD2: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 29 | return &mbedtls_md2_info; |
Steven Cooreman | 5f88e77 | 2021-03-15 11:07:12 +0100 | [diff] [blame] | 30 | #endif |
| 31 | #if defined(MBEDTLS_MD4_C) |
| 32 | case PSA_ALG_MD4: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 33 | return &mbedtls_md4_info; |
Steven Cooreman | 5f88e77 | 2021-03-15 11:07:12 +0100 | [diff] [blame] | 34 | #endif |
| 35 | #if defined(MBEDTLS_MD5_C) |
| 36 | case PSA_ALG_MD5: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 37 | return &mbedtls_md5_info; |
Steven Cooreman | 5f88e77 | 2021-03-15 11:07:12 +0100 | [diff] [blame] | 38 | #endif |
| 39 | #if defined(MBEDTLS_RIPEMD160_C) |
| 40 | case PSA_ALG_RIPEMD160: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 41 | return &mbedtls_ripemd160_info; |
Steven Cooreman | 5f88e77 | 2021-03-15 11:07:12 +0100 | [diff] [blame] | 42 | #endif |
| 43 | #if defined(MBEDTLS_SHA1_C) |
| 44 | case PSA_ALG_SHA_1: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 45 | return &mbedtls_sha1_info; |
Steven Cooreman | 5f88e77 | 2021-03-15 11:07:12 +0100 | [diff] [blame] | 46 | #endif |
| 47 | #if defined(MBEDTLS_SHA256_C) |
| 48 | case PSA_ALG_SHA_224: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 49 | return &mbedtls_sha224_info; |
Steven Cooreman | 5f88e77 | 2021-03-15 11:07:12 +0100 | [diff] [blame] | 50 | #endif |
| 51 | #if defined(MBEDTLS_SHA256_C) |
| 52 | case PSA_ALG_SHA_256: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 53 | return &mbedtls_sha256_info; |
Steven Cooreman | 5f88e77 | 2021-03-15 11:07:12 +0100 | [diff] [blame] | 54 | #endif |
| 55 | #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384) |
| 56 | case PSA_ALG_SHA_384: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 57 | return &mbedtls_sha384_info; |
Steven Cooreman | 5f88e77 | 2021-03-15 11:07:12 +0100 | [diff] [blame] | 58 | #endif |
| 59 | #if defined(MBEDTLS_SHA512_C) |
| 60 | case PSA_ALG_SHA_512: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 61 | return &mbedtls_sha512_info; |
Steven Cooreman | 5f88e77 | 2021-03-15 11:07:12 +0100 | [diff] [blame] | 62 | #endif |
| 63 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 64 | return NULL; |
Steven Cooreman | 5f88e77 | 2021-03-15 11:07:12 +0100 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 68 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || |
| 69 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || |
| 70 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
| 71 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 72 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 73 | psa_status_t mbedtls_psa_hash_abort( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 74 | mbedtls_psa_hash_operation_t *operation) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 75 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 76 | switch (operation->alg) { |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 77 | case 0: |
| 78 | /* The object has (apparently) been initialized but it is not |
| 79 | * in use. It's ok to call abort on such an object, and there's |
| 80 | * nothing to do. */ |
| 81 | break; |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 82 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 83 | case PSA_ALG_MD2: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 84 | mbedtls_md2_free(&operation->ctx.md2); |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 85 | break; |
| 86 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 87 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 88 | case PSA_ALG_MD4: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 89 | mbedtls_md4_free(&operation->ctx.md4); |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 90 | break; |
| 91 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 92 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 93 | case PSA_ALG_MD5: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 94 | mbedtls_md5_free(&operation->ctx.md5); |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 95 | break; |
| 96 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 97 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 98 | case PSA_ALG_RIPEMD160: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 99 | mbedtls_ripemd160_free(&operation->ctx.ripemd160); |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 100 | break; |
| 101 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 102 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 103 | case PSA_ALG_SHA_1: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 104 | mbedtls_sha1_free(&operation->ctx.sha1); |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 105 | break; |
| 106 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 107 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 108 | case PSA_ALG_SHA_224: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 109 | mbedtls_sha256_free(&operation->ctx.sha256); |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 110 | break; |
| 111 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 112 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 113 | case PSA_ALG_SHA_256: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 114 | mbedtls_sha256_free(&operation->ctx.sha256); |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 115 | break; |
| 116 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 117 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 118 | case PSA_ALG_SHA_384: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 119 | mbedtls_sha512_free(&operation->ctx.sha512); |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 120 | break; |
| 121 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 122 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 123 | case PSA_ALG_SHA_512: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 124 | mbedtls_sha512_free(&operation->ctx.sha512); |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 125 | break; |
| 126 | #endif |
| 127 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 128 | return PSA_ERROR_BAD_STATE; |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 129 | } |
| 130 | operation->alg = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 131 | return PSA_SUCCESS; |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 132 | } |
| 133 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 134 | psa_status_t mbedtls_psa_hash_setup( |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 135 | mbedtls_psa_hash_operation_t *operation, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 136 | psa_algorithm_t alg) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 137 | { |
| 138 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 139 | |
| 140 | /* A context must be freshly initialized before it can be set up. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 141 | if (operation->alg != 0) { |
| 142 | return PSA_ERROR_BAD_STATE; |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 143 | } |
| 144 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 145 | switch (alg) { |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 146 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 147 | case PSA_ALG_MD2: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 148 | mbedtls_md2_init(&operation->ctx.md2); |
| 149 | ret = mbedtls_md2_starts_ret(&operation->ctx.md2); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 150 | break; |
| 151 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 152 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 153 | case PSA_ALG_MD4: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 154 | mbedtls_md4_init(&operation->ctx.md4); |
| 155 | ret = mbedtls_md4_starts_ret(&operation->ctx.md4); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 156 | break; |
| 157 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 158 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 159 | case PSA_ALG_MD5: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 160 | mbedtls_md5_init(&operation->ctx.md5); |
| 161 | ret = mbedtls_md5_starts_ret(&operation->ctx.md5); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 162 | break; |
| 163 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 164 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 165 | case PSA_ALG_RIPEMD160: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 166 | mbedtls_ripemd160_init(&operation->ctx.ripemd160); |
| 167 | ret = mbedtls_ripemd160_starts_ret(&operation->ctx.ripemd160); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 168 | break; |
| 169 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 170 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 171 | case PSA_ALG_SHA_1: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 172 | mbedtls_sha1_init(&operation->ctx.sha1); |
| 173 | ret = mbedtls_sha1_starts_ret(&operation->ctx.sha1); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 174 | break; |
| 175 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 176 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 177 | case PSA_ALG_SHA_224: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 178 | mbedtls_sha256_init(&operation->ctx.sha256); |
| 179 | ret = mbedtls_sha256_starts_ret(&operation->ctx.sha256, 1); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 180 | break; |
| 181 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 182 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 183 | case PSA_ALG_SHA_256: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 184 | mbedtls_sha256_init(&operation->ctx.sha256); |
| 185 | ret = mbedtls_sha256_starts_ret(&operation->ctx.sha256, 0); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 186 | break; |
| 187 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 188 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 189 | case PSA_ALG_SHA_384: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 190 | mbedtls_sha512_init(&operation->ctx.sha512); |
| 191 | ret = mbedtls_sha512_starts_ret(&operation->ctx.sha512, 1); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 192 | break; |
| 193 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 194 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 195 | case PSA_ALG_SHA_512: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 196 | mbedtls_sha512_init(&operation->ctx.sha512); |
| 197 | ret = mbedtls_sha512_starts_ret(&operation->ctx.sha512, 0); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 198 | break; |
| 199 | #endif |
| 200 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 201 | return PSA_ALG_IS_HASH(alg) ? |
| 202 | PSA_ERROR_NOT_SUPPORTED : |
| 203 | PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 204 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 205 | if (ret == 0) { |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 206 | operation->alg = alg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 207 | } else { |
| 208 | mbedtls_psa_hash_abort(operation); |
| 209 | } |
| 210 | return mbedtls_to_psa_error(ret); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 211 | } |
| 212 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 213 | psa_status_t mbedtls_psa_hash_clone( |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 214 | const mbedtls_psa_hash_operation_t *source_operation, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 215 | mbedtls_psa_hash_operation_t *target_operation) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 216 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 217 | switch (source_operation->alg) { |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 218 | case 0: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 219 | return PSA_ERROR_BAD_STATE; |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 220 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 221 | case PSA_ALG_MD2: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 222 | mbedtls_md2_clone(&target_operation->ctx.md2, |
| 223 | &source_operation->ctx.md2); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 224 | break; |
| 225 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 226 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 227 | case PSA_ALG_MD4: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 228 | mbedtls_md4_clone(&target_operation->ctx.md4, |
| 229 | &source_operation->ctx.md4); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 230 | break; |
| 231 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 232 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 233 | case PSA_ALG_MD5: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 234 | mbedtls_md5_clone(&target_operation->ctx.md5, |
| 235 | &source_operation->ctx.md5); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 236 | break; |
| 237 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 238 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 239 | case PSA_ALG_RIPEMD160: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 240 | mbedtls_ripemd160_clone(&target_operation->ctx.ripemd160, |
| 241 | &source_operation->ctx.ripemd160); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 242 | break; |
| 243 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 244 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 245 | case PSA_ALG_SHA_1: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 246 | mbedtls_sha1_clone(&target_operation->ctx.sha1, |
| 247 | &source_operation->ctx.sha1); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 248 | break; |
| 249 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 250 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 251 | case PSA_ALG_SHA_224: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 252 | mbedtls_sha256_clone(&target_operation->ctx.sha256, |
| 253 | &source_operation->ctx.sha256); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 254 | break; |
| 255 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 256 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 257 | case PSA_ALG_SHA_256: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 258 | mbedtls_sha256_clone(&target_operation->ctx.sha256, |
| 259 | &source_operation->ctx.sha256); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 260 | break; |
| 261 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 262 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 263 | case PSA_ALG_SHA_384: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 264 | mbedtls_sha512_clone(&target_operation->ctx.sha512, |
| 265 | &source_operation->ctx.sha512); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 266 | break; |
| 267 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 268 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 269 | case PSA_ALG_SHA_512: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 270 | mbedtls_sha512_clone(&target_operation->ctx.sha512, |
| 271 | &source_operation->ctx.sha512); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 272 | break; |
| 273 | #endif |
| 274 | default: |
Steven Cooreman | 5adf52c | 2021-03-04 18:09:49 +0100 | [diff] [blame] | 275 | (void) source_operation; |
| 276 | (void) target_operation; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 277 | return PSA_ERROR_NOT_SUPPORTED; |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | target_operation->alg = source_operation->alg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 281 | return PSA_SUCCESS; |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 282 | } |
| 283 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 284 | psa_status_t mbedtls_psa_hash_update( |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 285 | mbedtls_psa_hash_operation_t *operation, |
| 286 | const uint8_t *input, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 287 | size_t input_length) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 288 | { |
| 289 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 290 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 291 | switch (operation->alg) { |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 292 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 293 | case PSA_ALG_MD2: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 294 | ret = mbedtls_md2_update_ret(&operation->ctx.md2, |
| 295 | input, input_length); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 296 | break; |
| 297 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 298 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 299 | case PSA_ALG_MD4: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 300 | ret = mbedtls_md4_update_ret(&operation->ctx.md4, |
| 301 | input, input_length); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 302 | break; |
| 303 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 304 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 305 | case PSA_ALG_MD5: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 306 | ret = mbedtls_md5_update_ret(&operation->ctx.md5, |
| 307 | input, input_length); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 308 | break; |
| 309 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 310 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 311 | case PSA_ALG_RIPEMD160: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 312 | ret = mbedtls_ripemd160_update_ret(&operation->ctx.ripemd160, |
| 313 | input, input_length); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 314 | break; |
| 315 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 316 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 317 | case PSA_ALG_SHA_1: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 318 | ret = mbedtls_sha1_update_ret(&operation->ctx.sha1, |
| 319 | input, input_length); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 320 | break; |
| 321 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 322 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 323 | case PSA_ALG_SHA_224: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 324 | ret = mbedtls_sha256_update_ret(&operation->ctx.sha256, |
| 325 | input, input_length); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 326 | break; |
| 327 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 328 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 329 | case PSA_ALG_SHA_256: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 330 | ret = mbedtls_sha256_update_ret(&operation->ctx.sha256, |
| 331 | input, input_length); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 332 | break; |
| 333 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 334 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 335 | case PSA_ALG_SHA_384: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 336 | ret = mbedtls_sha512_update_ret(&operation->ctx.sha512, |
| 337 | input, input_length); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 338 | break; |
| 339 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 340 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 341 | case PSA_ALG_SHA_512: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 342 | ret = mbedtls_sha512_update_ret(&operation->ctx.sha512, |
| 343 | input, input_length); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 344 | break; |
| 345 | #endif |
| 346 | default: |
Steven Cooreman | 5adf52c | 2021-03-04 18:09:49 +0100 | [diff] [blame] | 347 | (void) input; |
| 348 | (void) input_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 349 | return PSA_ERROR_BAD_STATE; |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 350 | } |
| 351 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 352 | return mbedtls_to_psa_error(ret); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 353 | } |
| 354 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 355 | psa_status_t mbedtls_psa_hash_finish( |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 356 | mbedtls_psa_hash_operation_t *operation, |
| 357 | uint8_t *hash, |
| 358 | size_t hash_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 359 | size_t *hash_length) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 360 | { |
| 361 | psa_status_t status; |
| 362 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 363 | size_t actual_hash_length = PSA_HASH_LENGTH(operation->alg); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 364 | |
| 365 | /* Fill the output buffer with something that isn't a valid hash |
| 366 | * (barring an attack on the hash and deliberately-crafted input), |
| 367 | * in case the caller doesn't check the return status properly. */ |
| 368 | *hash_length = hash_size; |
| 369 | /* If hash_size is 0 then hash may be NULL and then the |
| 370 | * call to memset would have undefined behavior. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 371 | if (hash_size != 0) { |
| 372 | memset(hash, '!', hash_size); |
| 373 | } |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 374 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 375 | if (hash_size < actual_hash_length) { |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 376 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 377 | goto exit; |
| 378 | } |
| 379 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 380 | switch (operation->alg) { |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 381 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 382 | case PSA_ALG_MD2: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 383 | ret = mbedtls_md2_finish_ret(&operation->ctx.md2, hash); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 384 | break; |
| 385 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 386 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 387 | case PSA_ALG_MD4: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 388 | ret = mbedtls_md4_finish_ret(&operation->ctx.md4, hash); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 389 | break; |
| 390 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 391 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 392 | case PSA_ALG_MD5: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 393 | ret = mbedtls_md5_finish_ret(&operation->ctx.md5, hash); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 394 | break; |
| 395 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 396 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 397 | case PSA_ALG_RIPEMD160: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 398 | ret = mbedtls_ripemd160_finish_ret(&operation->ctx.ripemd160, hash); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 399 | break; |
| 400 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 401 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 402 | case PSA_ALG_SHA_1: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 403 | ret = mbedtls_sha1_finish_ret(&operation->ctx.sha1, hash); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 404 | break; |
| 405 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 406 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 407 | case PSA_ALG_SHA_224: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 408 | ret = mbedtls_sha256_finish_ret(&operation->ctx.sha256, hash); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 409 | break; |
| 410 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 411 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 412 | case PSA_ALG_SHA_256: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 413 | ret = mbedtls_sha256_finish_ret(&operation->ctx.sha256, hash); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 414 | break; |
| 415 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 416 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 417 | case PSA_ALG_SHA_384: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 418 | ret = mbedtls_sha512_finish_ret(&operation->ctx.sha512, hash); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 419 | break; |
| 420 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 421 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 422 | case PSA_ALG_SHA_512: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 423 | ret = mbedtls_sha512_finish_ret(&operation->ctx.sha512, hash); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 424 | break; |
| 425 | #endif |
| 426 | default: |
Steven Cooreman | 5adf52c | 2021-03-04 18:09:49 +0100 | [diff] [blame] | 427 | (void) hash; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 428 | return PSA_ERROR_BAD_STATE; |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 429 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 430 | status = mbedtls_to_psa_error(ret); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 431 | |
| 432 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 433 | if (status == PSA_SUCCESS) { |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 434 | *hash_length = actual_hash_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 435 | } |
| 436 | return status; |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 437 | } |
| 438 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 439 | psa_status_t mbedtls_psa_hash_compute( |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 440 | psa_algorithm_t alg, |
| 441 | const uint8_t *input, |
| 442 | size_t input_length, |
| 443 | uint8_t *hash, |
| 444 | size_t hash_size, |
| 445 | size_t *hash_length) |
| 446 | { |
| 447 | mbedtls_psa_hash_operation_t operation = MBEDTLS_PSA_HASH_OPERATION_INIT; |
| 448 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 61bb8fc | 2021-03-15 12:32:48 +0100 | [diff] [blame] | 449 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 450 | |
| 451 | *hash_length = hash_size; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 452 | status = mbedtls_psa_hash_setup(&operation, alg); |
| 453 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 454 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 455 | } |
| 456 | status = mbedtls_psa_hash_update(&operation, input, input_length); |
| 457 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 458 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 459 | } |
| 460 | status = mbedtls_psa_hash_finish(&operation, hash, hash_size, hash_length); |
| 461 | if (status != PSA_SUCCESS) { |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 462 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 463 | } |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 464 | |
| 465 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 466 | abort_status = mbedtls_psa_hash_abort(&operation); |
| 467 | if (status == PSA_SUCCESS) { |
| 468 | return abort_status; |
| 469 | } else { |
| 470 | return status; |
| 471 | } |
Steven Cooreman | 61bb8fc | 2021-03-15 12:32:48 +0100 | [diff] [blame] | 472 | |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 473 | } |
Steven Cooreman | 0d58666 | 2021-03-08 20:28:18 +0100 | [diff] [blame] | 474 | #endif /* MBEDTLS_PSA_BUILTIN_HASH */ |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 475 | |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 476 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |