Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Example computing a SHA-256 hash using the PSA Crypto API |
| 3 | * |
| 4 | * The example computes the SHA-256 hash of a test string using the |
| 5 | * one-shot API call psa_hash_compute() and the using multi-part |
| 6 | * operation, which requires psa_hash_setup(), psa_hash_update() and |
| 7 | * psa_hash_finish(). The multi-part operation is popular on embedded |
| 8 | * devices where a rolling hash needs to be computed. |
| 9 | * |
| 10 | * |
| 11 | * Copyright The Mbed TLS Contributors |
| 12 | * SPDX-License-Identifier: Apache-2.0 |
| 13 | * |
| 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 15 | * not use this file except in compliance with the License. |
| 16 | * You may obtain a copy of the License at |
| 17 | * |
| 18 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 19 | * |
| 20 | * Unless required by applicable law or agreed to in writing, software |
| 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 23 | * See the License for the specific language governing permissions and |
| 24 | * limitations under the License. |
| 25 | */ |
| 26 | |
| 27 | |
| 28 | #include "psa/crypto.h" |
| 29 | #include <string.h> |
| 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
| 32 | |
| 33 | #include "mbedtls/build_info.h" |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 34 | #include "mbedtls/platform.h" |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 35 | |
Thomas Daubney | 1db78fa | 2023-07-24 16:49:14 +0100 | [diff] [blame] | 36 | #define HASH_ALG PSA_ALG_SHA_256 |
| 37 | |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 38 | #define TEST_SHA256_HASH { \ |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 39 | 0x5a, 0x09, 0xe8, 0xfa, 0x9c, 0x77, 0x80, 0x7b, 0x24, 0xe9, 0x9c, 0x9c, \ |
| 40 | 0xf9, 0x99, 0xde, 0xbf, 0xad, 0x84, 0x41, 0xe2, 0x69, 0xeb, 0x96, 0x0e, \ |
| 41 | 0x20, 0x1f, 0x61, 0xfc, 0x3d, 0xe2, 0x0d, 0x5a \ |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | const uint8_t mbedtls_test_sha256_hash[] = TEST_SHA256_HASH; |
| 45 | |
| 46 | const size_t mbedtls_test_sha256_hash_len = |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 47 | sizeof(mbedtls_test_sha256_hash); |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 48 | |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 49 | #if !defined(MBEDTLS_PSA_CRYPTO_C) || !defined(PSA_WANT_ALG_SHA_256) |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 50 | int main(void) |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 51 | { |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 52 | mbedtls_printf("MBEDTLS_PSA_CRYPTO_C and MBEDTLS_SHA256_C" |
Thomas Daubney | 9520df7 | 2023-07-25 10:56:54 +0100 | [diff] [blame] | 53 | "not defined.\r\n"); |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 54 | return EXIT_SUCCESS; |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 55 | } |
| 56 | #else |
| 57 | |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 58 | int main(void) |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 59 | { |
| 60 | uint8_t buf[] = "Hello World!"; |
| 61 | psa_status_t status; |
Thomas Daubney | 1db78fa | 2023-07-24 16:49:14 +0100 | [diff] [blame] | 62 | uint8_t hash[PSA_HASH_LENGTH(HASH_ALG)]; |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 63 | size_t hash_size; |
Thomas Daubney | 1fd916a | 2023-07-25 16:10:48 +0100 | [diff] [blame^] | 64 | psa_hash_operation_t psa_hash_operation = PSA_HASH_OPERATION_INIT; |
| 65 | psa_hash_operation_t cloned_psa_hash_operation = PSA_HASH_OPERATION_INIT; |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 66 | |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 67 | mbedtls_printf("PSA Crypto API: SHA-256 example\n\n"); |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 68 | |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 69 | status = psa_crypto_init(); |
| 70 | if (status != PSA_SUCCESS) { |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 71 | mbedtls_printf("psa_crypto_init failed\n"); |
Thomas Daubney | 1fd916a | 2023-07-25 16:10:48 +0100 | [diff] [blame^] | 72 | psa_hash_abort(&psa_hash_operation); |
| 73 | psa_hash_abort(&cloned_psa_hash_operation); |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 74 | return EXIT_FAILURE; |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 75 | } |
| 76 | |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 77 | /* Compute hash using multi-part operation */ |
| 78 | |
Thomas Daubney | 1fd916a | 2023-07-25 16:10:48 +0100 | [diff] [blame^] | 79 | status = psa_hash_setup(&psa_hash_operation, HASH_ALG); |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 80 | if (status != PSA_SUCCESS) { |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 81 | mbedtls_printf("psa_hash_setup failed\n"); |
Thomas Daubney | 1fd916a | 2023-07-25 16:10:48 +0100 | [diff] [blame^] | 82 | psa_hash_abort(&psa_hash_operation); |
| 83 | psa_hash_abort(&cloned_psa_hash_operation); |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 84 | return EXIT_FAILURE; |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 85 | } |
| 86 | |
Thomas Daubney | 1fd916a | 2023-07-25 16:10:48 +0100 | [diff] [blame^] | 87 | status = psa_hash_update(&psa_hash_operation, buf, sizeof(buf)); |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 88 | if (status != PSA_SUCCESS) { |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 89 | mbedtls_printf("psa_hash_update failed\n"); |
Thomas Daubney | 1fd916a | 2023-07-25 16:10:48 +0100 | [diff] [blame^] | 90 | psa_hash_abort(&psa_hash_operation); |
| 91 | psa_hash_abort(&cloned_psa_hash_operation); |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 92 | return EXIT_FAILURE; |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Thomas Daubney | 1fd916a | 2023-07-25 16:10:48 +0100 | [diff] [blame^] | 95 | status = psa_hash_clone(&psa_hash_operation, &cloned_psa_hash_operation); |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 96 | if (status != PSA_SUCCESS) { |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 97 | mbedtls_printf("PSA hash clone failed"); |
Thomas Daubney | 1fd916a | 2023-07-25 16:10:48 +0100 | [diff] [blame^] | 98 | psa_hash_abort(&psa_hash_operation); |
| 99 | psa_hash_abort(&cloned_psa_hash_operation); |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 100 | return EXIT_FAILURE; |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 101 | } |
| 102 | |
Thomas Daubney | 1fd916a | 2023-07-25 16:10:48 +0100 | [diff] [blame^] | 103 | status = psa_hash_finish(&psa_hash_operation, hash, sizeof(hash), &hash_size); |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 104 | if (status != PSA_SUCCESS) { |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 105 | mbedtls_printf("psa_hash_finish failed\n"); |
Thomas Daubney | 1fd916a | 2023-07-25 16:10:48 +0100 | [diff] [blame^] | 106 | psa_hash_abort(&psa_hash_operation); |
| 107 | psa_hash_abort(&cloned_psa_hash_operation); |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 108 | return EXIT_FAILURE; |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 109 | } |
| 110 | |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 111 | status = |
Thomas Daubney | 1fd916a | 2023-07-25 16:10:48 +0100 | [diff] [blame^] | 112 | psa_hash_verify(&cloned_psa_hash_operation, mbedtls_test_sha256_hash, |
| 113 | mbedtls_test_sha256_hash_len); |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 114 | if (status != PSA_SUCCESS) { |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 115 | mbedtls_printf("psa_hash_verify failed\n"); |
Thomas Daubney | 1fd916a | 2023-07-25 16:10:48 +0100 | [diff] [blame^] | 116 | psa_hash_abort(&psa_hash_operation); |
| 117 | psa_hash_abort(&cloned_psa_hash_operation); |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 118 | return EXIT_FAILURE; |
| 119 | } else { |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 120 | mbedtls_printf("Multi-part hash operation successful!\n"); |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /* Compute hash using one-shot function call */ |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 124 | memset(hash, 0, sizeof(hash)); |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 125 | hash_size = 0; |
| 126 | |
Thomas Daubney | 1db78fa | 2023-07-24 16:49:14 +0100 | [diff] [blame] | 127 | status = psa_hash_compute(HASH_ALG, |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 128 | buf, sizeof(buf), |
| 129 | hash, sizeof(hash), |
| 130 | &hash_size); |
| 131 | if (status != PSA_SUCCESS) { |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 132 | mbedtls_printf("psa_hash_compute failed\n"); |
Thomas Daubney | 1fd916a | 2023-07-25 16:10:48 +0100 | [diff] [blame^] | 133 | psa_hash_abort(&psa_hash_operation); |
| 134 | psa_hash_abort(&cloned_psa_hash_operation); |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 135 | return EXIT_FAILURE; |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 138 | for (size_t j = 0; j < mbedtls_test_sha256_hash_len; j++) { |
| 139 | if (hash[j] != mbedtls_test_sha256_hash[j]) { |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 140 | mbedtls_printf("One-shot hash operation failed!\n\n"); |
Thomas Daubney | 1fd916a | 2023-07-25 16:10:48 +0100 | [diff] [blame^] | 141 | psa_hash_abort(&psa_hash_operation); |
| 142 | psa_hash_abort(&cloned_psa_hash_operation); |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 143 | return EXIT_FAILURE; |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 147 | mbedtls_printf("One-shot hash operation successful!\n\n"); |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 148 | |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 149 | mbedtls_printf("The SHA-256( '%s' ) is:\n", buf); |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 150 | |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 151 | for (size_t j = 0; j < mbedtls_test_sha256_hash_len; j++) { |
| 152 | if (j % 8 == 0) { |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 153 | mbedtls_printf("\n "); |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 154 | } |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 155 | mbedtls_printf("%02x ", hash[j]); |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 156 | } |
| 157 | |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 158 | mbedtls_printf("\n"); |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 159 | |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 160 | mbedtls_psa_crypto_free(); |
| 161 | return EXIT_SUCCESS; |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 162 | } |
| 163 | #endif /* MBEDTLS_PSA_CRYPTO_C && MBEDTLS_SHA256_C */ |