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 | 606110f | 2023-07-28 15:57:10 +0100 | [diff] [blame] | 36 | /* The algorithm used by this demo is SHA 256. |
| 37 | * Please see include/psa/crypto_values.h to see the other |
| 38 | * algorithms that are supported. If you switch to a different |
| 39 | * algorithm you will need to update the hash data in the |
| 40 | * SAMPLE_HASH_DATA macro below.*/ |
| 41 | |
Thomas Daubney | 1db78fa | 2023-07-24 16:49:14 +0100 | [diff] [blame] | 42 | #define HASH_ALG PSA_ALG_SHA_256 |
| 43 | |
Thomas Daubney | c918c32 | 2023-07-28 17:15:03 +0100 | [diff] [blame] | 44 | #define SAMPLE_HASH_DATA { \ |
| 45 | 0x7f, 0x83, 0xb1, 0x65, 0x7f, 0xf1, 0xfc, 0x53, 0xb9, 0x2d, 0xc1, 0x81, \ |
| 46 | 0x48, 0xa1, 0xd6, 0x5d, 0xfc, 0x2d, 0x4b, 0x1f, 0xa3, 0xd6, 0x77, 0x28, \ |
| 47 | 0x4a, 0xdd, 0xd2, 0x00, 0x12, 0x6d, 0x90, 0x69 \ |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 48 | } |
| 49 | |
Thomas Daubney | 606110f | 2023-07-28 15:57:10 +0100 | [diff] [blame] | 50 | const uint8_t sample_message[] = "Hello World!"; |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 51 | |
Thomas Daubney | 606110f | 2023-07-28 15:57:10 +0100 | [diff] [blame] | 52 | const uint8_t sample_hash[] = SAMPLE_HASH_DATA; |
| 53 | const size_t sample_hash_len = sizeof(sample_hash); |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 54 | |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 55 | #if !defined(MBEDTLS_PSA_CRYPTO_C) || !defined(PSA_WANT_ALG_SHA_256) |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 56 | int main(void) |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 57 | { |
Thomas Daubney | 2c87234 | 2023-07-28 14:21:38 +0100 | [diff] [blame] | 58 | mbedtls_printf("MBEDTLS_PSA_CRYPTO_C and PSA_WANT_ALG_SHA_256" |
Thomas Daubney | 9520df7 | 2023-07-25 10:56:54 +0100 | [diff] [blame] | 59 | "not defined.\r\n"); |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 60 | return EXIT_SUCCESS; |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 61 | } |
| 62 | #else |
| 63 | |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 64 | int main(void) |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 65 | { |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 66 | psa_status_t status; |
Thomas Daubney | 1db78fa | 2023-07-24 16:49:14 +0100 | [diff] [blame] | 67 | uint8_t hash[PSA_HASH_LENGTH(HASH_ALG)]; |
Thomas Daubney | 6fc4ca2 | 2023-07-28 14:31:06 +0100 | [diff] [blame] | 68 | size_t hash_length; |
Thomas Daubney | c050037 | 2023-07-28 14:44:25 +0100 | [diff] [blame] | 69 | psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; |
| 70 | psa_hash_operation_t cloned_hash_operation = PSA_HASH_OPERATION_INIT; |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 71 | |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 72 | mbedtls_printf("PSA Crypto API: SHA-256 example\n\n"); |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 73 | |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 74 | status = psa_crypto_init(); |
| 75 | if (status != PSA_SUCCESS) { |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 76 | mbedtls_printf("psa_crypto_init failed\n"); |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 77 | return EXIT_FAILURE; |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 80 | /* Compute hash using multi-part operation */ |
| 81 | |
Thomas Daubney | c050037 | 2023-07-28 14:44:25 +0100 | [diff] [blame] | 82 | status = psa_hash_setup(&hash_operation, HASH_ALG); |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 83 | if (status != PSA_SUCCESS) { |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 84 | mbedtls_printf("psa_hash_setup failed\n"); |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 85 | return EXIT_FAILURE; |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 86 | } |
| 87 | |
Thomas Daubney | 1ba9744 | 2023-07-28 17:25:16 +0100 | [diff] [blame] | 88 | /* Note: Here we use sizeof(sample_message) - 1 since we don't wish to |
Thomas Daubney | c918c32 | 2023-07-28 17:15:03 +0100 | [diff] [blame] | 89 | * include the null byte in the hash computation */ |
| 90 | status = psa_hash_update(&hash_operation, sample_message, sizeof(sample_message) - 1); |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 91 | if (status != PSA_SUCCESS) { |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 92 | mbedtls_printf("psa_hash_update failed\n"); |
Thomas Daubney | 5c2dcbd | 2023-08-03 16:03:30 +0100 | [diff] [blame] | 93 | goto cleanup; |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 94 | } |
| 95 | |
Thomas Daubney | c050037 | 2023-07-28 14:44:25 +0100 | [diff] [blame] | 96 | status = psa_hash_clone(&hash_operation, &cloned_hash_operation); |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 97 | if (status != PSA_SUCCESS) { |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 98 | mbedtls_printf("PSA hash clone failed"); |
Thomas Daubney | 5c2dcbd | 2023-08-03 16:03:30 +0100 | [diff] [blame] | 99 | goto cleanup; |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 100 | } |
| 101 | |
Thomas Daubney | c050037 | 2023-07-28 14:44:25 +0100 | [diff] [blame] | 102 | status = psa_hash_finish(&hash_operation, hash, sizeof(hash), &hash_length); |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 103 | if (status != PSA_SUCCESS) { |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 104 | mbedtls_printf("psa_hash_finish failed\n"); |
Thomas Daubney | 5c2dcbd | 2023-08-03 16:03:30 +0100 | [diff] [blame] | 105 | goto cleanup; |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 106 | } |
| 107 | |
Thomas Daubney | ce14124 | 2023-07-28 16:14:20 +0100 | [diff] [blame] | 108 | /* Check the result of the operation against the sample */ |
Thomas Daubney | 1ba9744 | 2023-07-28 17:25:16 +0100 | [diff] [blame] | 109 | if ((memcmp(hash, sample_hash, sample_hash_len) != 0) || hash_length != sample_hash_len) { |
Thomas Daubney | ce14124 | 2023-07-28 16:14:20 +0100 | [diff] [blame] | 110 | mbedtls_printf("Multi-part hash operation gave the wrong result!\n\n"); |
Thomas Daubney | 5c2dcbd | 2023-08-03 16:03:30 +0100 | [diff] [blame] | 111 | goto cleanup; |
Thomas Daubney | ce14124 | 2023-07-28 16:14:20 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 114 | status = |
Thomas Daubney | 606110f | 2023-07-28 15:57:10 +0100 | [diff] [blame] | 115 | psa_hash_verify(&cloned_hash_operation, sample_hash, |
| 116 | sample_hash_len); |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 117 | if (status != PSA_SUCCESS) { |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 118 | mbedtls_printf("psa_hash_verify failed\n"); |
Thomas Daubney | 5c2dcbd | 2023-08-03 16:03:30 +0100 | [diff] [blame] | 119 | goto cleanup; |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 120 | } else { |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 121 | mbedtls_printf("Multi-part hash operation successful!\n"); |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 122 | } |
| 123 | |
Thomas Daubney | 3071c85 | 2023-07-28 14:47:47 +0100 | [diff] [blame] | 124 | /* Clear local variables prior to one-shot hash demo */ |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 125 | memset(hash, 0, sizeof(hash)); |
Thomas Daubney | 6fc4ca2 | 2023-07-28 14:31:06 +0100 | [diff] [blame] | 126 | hash_length = 0; |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 127 | |
Thomas Daubney | 3071c85 | 2023-07-28 14:47:47 +0100 | [diff] [blame] | 128 | /* Compute hash using one-shot function call */ |
Thomas Daubney | 1db78fa | 2023-07-24 16:49:14 +0100 | [diff] [blame] | 129 | status = psa_hash_compute(HASH_ALG, |
Thomas Daubney | c918c32 | 2023-07-28 17:15:03 +0100 | [diff] [blame] | 130 | sample_message, sizeof(sample_message) - 1, |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 131 | hash, sizeof(hash), |
Thomas Daubney | 6fc4ca2 | 2023-07-28 14:31:06 +0100 | [diff] [blame] | 132 | &hash_length); |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 133 | if (status != PSA_SUCCESS) { |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 134 | mbedtls_printf("psa_hash_compute failed\n"); |
Thomas Daubney | 5c2dcbd | 2023-08-03 16:03:30 +0100 | [diff] [blame] | 135 | goto cleanup; |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Thomas Daubney | 1ba9744 | 2023-07-28 17:25:16 +0100 | [diff] [blame] | 138 | if (memcmp(hash, sample_hash, sample_hash_len) != 0 || hash_length != sample_hash_len) { |
Thomas Daubney | a2b7519 | 2023-07-28 15:21:46 +0100 | [diff] [blame] | 139 | mbedtls_printf("One-shot hash operation gave the wrong result!\n\n"); |
Thomas Daubney | 5c2dcbd | 2023-08-03 16:03:30 +0100 | [diff] [blame] | 140 | goto cleanup; |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 141 | } |
| 142 | |
Thomas Daubney | f7348ae | 2023-07-24 12:18:40 +0100 | [diff] [blame] | 143 | mbedtls_printf("One-shot hash operation successful!\n\n"); |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 144 | |
Thomas Daubney | 1f98736 | 2023-07-28 15:23:06 +0100 | [diff] [blame] | 145 | /* Print out result */ |
Thomas Daubney | 606110f | 2023-07-28 15:57:10 +0100 | [diff] [blame] | 146 | mbedtls_printf("The SHA-256( '%s' ) is: ", sample_message); |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 147 | |
Thomas Daubney | 606110f | 2023-07-28 15:57:10 +0100 | [diff] [blame] | 148 | for (size_t j = 0; j < sample_hash_len; j++) { |
Thomas Daubney | 9730cb1 | 2023-07-28 15:07:19 +0100 | [diff] [blame] | 149 | mbedtls_printf("%02x", hash[j]); |
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("\n"); |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 153 | |
Thomas Daubney | 209c9c9 | 2023-07-18 14:59:45 +0100 | [diff] [blame] | 154 | mbedtls_psa_crypto_free(); |
| 155 | return EXIT_SUCCESS; |
Thomas Daubney | 5c2dcbd | 2023-08-03 16:03:30 +0100 | [diff] [blame] | 156 | |
| 157 | cleanup: |
| 158 | psa_hash_abort(&hash_operation); |
| 159 | psa_hash_abort(&cloned_hash_operation); |
| 160 | return EXIT_FAILURE; |
Hannes Tschofenig | f8b9ebf | 2023-07-18 13:46:10 +0100 | [diff] [blame] | 161 | } |
Thomas Daubney | 102033c | 2023-08-03 16:20:09 +0100 | [diff] [blame^] | 162 | #endif /* MBEDTLS_PSA_CRYPTO_C && PSA_WANT_ALG_SHA_256 */ |