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 | |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 27 | #include "psa/crypto.h" |
| 28 | #include <string.h> |
| 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | |
| 32 | #include "mbedtls/build_info.h" |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 33 | #include "mbedtls/platform.h" |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 34 | |
Thomas Daubney | 86f9795 | 2023-10-10 16:50:49 +0100 | [diff] [blame] | 35 | /* Information about hashing with the PSA API can be |
| 36 | * found here: |
| 37 | * https://arm-software.github.io/psa-api/crypto/1.1/api/ops/hashes.html |
Thomas Daubney | 3450087 | 2023-10-11 10:04:54 +0100 | [diff] [blame] | 38 | * |
Thomas Daubney | 86f9795 | 2023-10-10 16:50:49 +0100 | [diff] [blame] | 39 | * The algorithm used by this demo is SHA 256. |
Thomas Daubney | 606110f | 2023-07-28 15:57:10 +0100 | [diff] [blame] | 40 | * Please see include/psa/crypto_values.h to see the other |
Thomas Daubney | 86f9795 | 2023-10-10 16:50:49 +0100 | [diff] [blame] | 41 | * algorithms that are supported by Mbed TLS. |
| 42 | * If you switch to a different algorithm you will need to update |
Thomas Daubney | 2e67781 | 2023-10-12 10:46:43 +0100 | [diff] [blame^] | 43 | * the hash data in the EXAMPLE_HASH_VALUE macro below. */ |
Thomas Daubney | 1c2378b | 2023-10-11 15:19:38 +0100 | [diff] [blame] | 44 | |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 45 | #if !defined(MBEDTLS_PSA_CRYPTO_C) || !defined(PSA_WANT_ALG_SHA_256) |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 46 | int main(void) |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 47 | { |
Thomas Daubney | 2c87234 | 2023-07-28 14:21:38 +0100 | [diff] [blame] | 48 | mbedtls_printf("MBEDTLS_PSA_CRYPTO_C and PSA_WANT_ALG_SHA_256" |
Thomas Daubney | 9520df7 | 2023-07-25 10:56:54 +0100 | [diff] [blame] | 49 | "not defined.\r\n"); |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 50 | return EXIT_SUCCESS; |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 51 | } |
| 52 | #else |
| 53 | |
Thomas Daubney | 2e67781 | 2023-10-12 10:46:43 +0100 | [diff] [blame^] | 54 | #define HASH_ALG PSA_ALG_SHA_256 |
| 55 | |
| 56 | const uint8_t sample_message[] = "Hello World!"; |
| 57 | /* sample_message is terminated with a null byte which is not part of |
| 58 | * the message itself so we make sure to subtract it in order to get |
| 59 | * the message length. */ |
| 60 | const size_t sample_message_length = sizeof(sample_message) - 1; |
| 61 | |
| 62 | #define EXPECTED_HASH_VALUE { \ |
| 63 | 0x7f, 0x83, 0xb1, 0x65, 0x7f, 0xf1, 0xfc, 0x53, 0xb9, 0x2d, 0xc1, 0x81, \ |
| 64 | 0x48, 0xa1, 0xd6, 0x5d, 0xfc, 0x2d, 0x4b, 0x1f, 0xa3, 0xd6, 0x77, 0x28, \ |
| 65 | 0x4a, 0xdd, 0xd2, 0x00, 0x12, 0x6d, 0x90, 0x69 \ |
| 66 | } |
| 67 | |
| 68 | const uint8_t expected_hash[] = EXPECTED_HASH_VALUE; |
| 69 | const size_t expected_hash_len = sizeof(expected_hash); |
| 70 | |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 71 | int main(void) |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 72 | { |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 73 | psa_status_t status; |
Thomas Daubney | 1db78fa | 2023-07-24 16:49:14 +0100 | [diff] [blame] | 74 | uint8_t hash[PSA_HASH_LENGTH(HASH_ALG)]; |
Thomas Daubney | 6fc4ca2 | 2023-07-28 14:31:06 +0100 | [diff] [blame] | 75 | size_t hash_length; |
Thomas Daubney | c050037 | 2023-07-28 14:44:25 +0100 | [diff] [blame] | 76 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; |
| 77 | psa_hash_operation_t cloned_hash_operation = PSA_HASH_OPERATION_INIT; |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 78 | |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 79 | mbedtls_printf("PSA Crypto API: SHA-256 example\n\n"); |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 80 | |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 81 | status = psa_crypto_init(); |
| 82 | if (status != PSA_SUCCESS) { |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 83 | mbedtls_printf("psa_crypto_init failed\n"); |
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 | |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 87 | /* Compute hash using multi-part operation */ |
Thomas Daubney | c050037 | 2023-07-28 14:44:25 +0100 | [diff] [blame] | 88 | status = psa_hash_setup(&hash_operation, HASH_ALG); |
Thomas Daubney | 2e67781 | 2023-10-12 10:46:43 +0100 | [diff] [blame^] | 89 | if (status == PSA_ERROR_NOT_SUPPORTED) { |
Thomas Daubney | 7605388 | 2023-10-10 17:38:53 +0100 | [diff] [blame] | 90 | mbedtls_printf("unknown hash algorithm supplied\n"); |
| 91 | return EXIT_FAILURE; |
Thomas Daubney | 2e67781 | 2023-10-12 10:46:43 +0100 | [diff] [blame^] | 92 | } else if (status != PSA_SUCCESS) { |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 93 | mbedtls_printf("psa_hash_setup failed\n"); |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 94 | return EXIT_FAILURE; |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 95 | } |
| 96 | |
Thomas Daubney | 1c2378b | 2023-10-11 15:19:38 +0100 | [diff] [blame] | 97 | status = psa_hash_update(&hash_operation, sample_message, sample_message_length); |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 98 | if (status != PSA_SUCCESS) { |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 99 | mbedtls_printf("psa_hash_update failed\n"); |
Thomas Daubney | 5c2dcbd | 2023-08-03 16:03:30 +0100 | [diff] [blame] | 100 | goto cleanup; |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 101 | } |
| 102 | |
Thomas Daubney | c050037 | 2023-07-28 14:44:25 +0100 | [diff] [blame] | 103 | status = psa_hash_clone(&hash_operation, &cloned_hash_operation); |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 104 | if (status != PSA_SUCCESS) { |
Thomas Daubney | cd79f77 | 2023-10-11 15:28:13 +0100 | [diff] [blame] | 105 | mbedtls_printf("PSA hash clone failed\n"); |
Thomas Daubney | 5c2dcbd | 2023-08-03 16:03:30 +0100 | [diff] [blame] | 106 | goto cleanup; |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 107 | } |
| 108 | |
Thomas Daubney | c050037 | 2023-07-28 14:44:25 +0100 | [diff] [blame] | 109 | status = psa_hash_finish(&hash_operation, hash, sizeof(hash), &hash_length); |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 110 | if (status != PSA_SUCCESS) { |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 111 | mbedtls_printf("psa_hash_finish failed\n"); |
Thomas Daubney | 5c2dcbd | 2023-08-03 16:03:30 +0100 | [diff] [blame] | 112 | goto cleanup; |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 113 | } |
| 114 | |
Thomas Daubney | ce14124 | 2023-07-28 16:14:20 +0100 | [diff] [blame] | 115 | /* Check the result of the operation against the sample */ |
Thomas Daubney | 2e67781 | 2023-10-12 10:46:43 +0100 | [diff] [blame^] | 116 | if (hash_length != expected_hash_len || |
| 117 | (memcmp(hash, expected_hash, expected_hash_len) != 0)) { |
Thomas Daubney | ce14124 | 2023-07-28 16:14:20 +0100 | [diff] [blame] | 118 | mbedtls_printf("Multi-part hash operation gave the wrong result!\n\n"); |
Thomas Daubney | 5c2dcbd | 2023-08-03 16:03:30 +0100 | [diff] [blame] | 119 | goto cleanup; |
Thomas Daubney | ce14124 | 2023-07-28 16:14:20 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 122 | status = |
Thomas Daubney | 2e67781 | 2023-10-12 10:46:43 +0100 | [diff] [blame^] | 123 | psa_hash_verify(&cloned_hash_operation, expected_hash, |
| 124 | expected_hash_len); |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 125 | if (status != PSA_SUCCESS) { |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 126 | mbedtls_printf("psa_hash_verify failed\n"); |
Thomas Daubney | 5c2dcbd | 2023-08-03 16:03:30 +0100 | [diff] [blame] | 127 | goto cleanup; |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 128 | } else { |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 129 | mbedtls_printf("Multi-part hash operation successful!\n"); |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 130 | } |
| 131 | |
Thomas Daubney | 3071c85 | 2023-07-28 14:47:47 +0100 | [diff] [blame] | 132 | /* Clear local variables prior to one-shot hash demo */ |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 133 | memset(hash, 0, sizeof(hash)); |
Thomas Daubney | 6fc4ca2 | 2023-07-28 14:31:06 +0100 | [diff] [blame] | 134 | hash_length = 0; |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 135 | |
Thomas Daubney | 3071c85 | 2023-07-28 14:47:47 +0100 | [diff] [blame] | 136 | /* Compute hash using one-shot function call */ |
Thomas Daubney | 1db78fa | 2023-07-24 16:49:14 +0100 | [diff] [blame] | 137 | status = psa_hash_compute(HASH_ALG, |
Thomas Daubney | 1c2378b | 2023-10-11 15:19:38 +0100 | [diff] [blame] | 138 | sample_message, sample_message_length, |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 139 | hash, sizeof(hash), |
Thomas Daubney | 6fc4ca2 | 2023-07-28 14:31:06 +0100 | [diff] [blame] | 140 | &hash_length); |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 141 | if (status != PSA_SUCCESS) { |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 142 | mbedtls_printf("psa_hash_compute failed\n"); |
Thomas Daubney | 5c2dcbd | 2023-08-03 16:03:30 +0100 | [diff] [blame] | 143 | goto cleanup; |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 144 | } |
| 145 | |
Thomas Daubney | 2e67781 | 2023-10-12 10:46:43 +0100 | [diff] [blame^] | 146 | if (hash_length != expected_hash_len || |
| 147 | (memcmp(hash, expected_hash, expected_hash_len) != 0)) { |
Thomas Daubney | a2b7519 | 2023-07-28 15:21:46 +0100 | [diff] [blame] | 148 | mbedtls_printf("One-shot hash operation gave the wrong result!\n\n"); |
Thomas Daubney | 5c2dcbd | 2023-08-03 16:03:30 +0100 | [diff] [blame] | 149 | goto cleanup; |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 150 | } |
| 151 | |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 152 | mbedtls_printf("One-shot hash operation successful!\n\n"); |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 153 | |
Thomas Daubney | 1f98736 | 2023-07-28 15:23:06 +0100 | [diff] [blame] | 154 | /* Print out result */ |
Thomas Daubney | 606110f | 2023-07-28 15:57:10 +0100 | [diff] [blame] | 155 | mbedtls_printf("The SHA-256( '%s' ) is: ", sample_message); |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 156 | |
Thomas Daubney | 2e67781 | 2023-10-12 10:46:43 +0100 | [diff] [blame^] | 157 | for (size_t j = 0; j < expected_hash_len; j++) { |
Thomas Daubney | 9730cb1 | 2023-07-28 15:07:19 +0100 | [diff] [blame] | 158 | mbedtls_printf("%02x", hash[j]); |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 159 | } |
| 160 | |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 161 | mbedtls_printf("\n"); |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 162 | |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 163 | mbedtls_psa_crypto_free(); |
| 164 | return EXIT_SUCCESS; |
Thomas Daubney | 5c2dcbd | 2023-08-03 16:03:30 +0100 | [diff] [blame] | 165 | |
| 166 | cleanup: |
| 167 | psa_hash_abort(&hash_operation); |
| 168 | psa_hash_abort(&cloned_hash_operation); |
| 169 | return EXIT_FAILURE; |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 170 | } |
Thomas Daubney | 2e67781 | 2023-10-12 10:46:43 +0100 | [diff] [blame^] | 171 | #endif /* !MBEDTLS_PSA_CRYPTO_C || !PSA_WANT_ALG_SHA_256 */ |