blob: a3923628fa169c84306a17aad13cdaf17a7b82e1 [file] [log] [blame]
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +01001/*
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 Daubneyf7348ae2023-07-24 12:18:40 +010034#include "mbedtls/platform.h"
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010035
Thomas Daubney86f97952023-10-10 16:50:49 +010036/* Information about hashing with the PSA API can be
37 * found here:
38 * https://arm-software.github.io/psa-api/crypto/1.1/api/ops/hashes.html
Thomas Daubney34500872023-10-11 10:04:54 +010039 *
Thomas Daubney86f97952023-10-10 16:50:49 +010040 * The algorithm used by this demo is SHA 256.
Thomas Daubney606110f2023-07-28 15:57:10 +010041 * Please see include/psa/crypto_values.h to see the other
Thomas Daubney86f97952023-10-10 16:50:49 +010042 * algorithms that are supported by Mbed TLS.
43 * If you switch to a different algorithm you will need to update
44 * the hash data in the SAMPLE_HASH_DATA macro below. */
Thomas Daubney606110f2023-07-28 15:57:10 +010045
Thomas Daubney1db78fa2023-07-24 16:49:14 +010046#define HASH_ALG PSA_ALG_SHA_256
47
Thomas Daubneyc918c322023-07-28 17:15:03 +010048#define SAMPLE_HASH_DATA { \
49 0x7f, 0x83, 0xb1, 0x65, 0x7f, 0xf1, 0xfc, 0x53, 0xb9, 0x2d, 0xc1, 0x81, \
50 0x48, 0xa1, 0xd6, 0x5d, 0xfc, 0x2d, 0x4b, 0x1f, 0xa3, 0xd6, 0x77, 0x28, \
51 0x4a, 0xdd, 0xd2, 0x00, 0x12, 0x6d, 0x90, 0x69 \
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010052}
53
Thomas Daubney606110f2023-07-28 15:57:10 +010054const uint8_t sample_message[] = "Hello World!";
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010055
Thomas Daubney606110f2023-07-28 15:57:10 +010056const uint8_t sample_hash[] = SAMPLE_HASH_DATA;
57const size_t sample_hash_len = sizeof(sample_hash);
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010058
Thomas Daubneyf7348ae2023-07-24 12:18:40 +010059#if !defined(MBEDTLS_PSA_CRYPTO_C) || !defined(PSA_WANT_ALG_SHA_256)
Thomas Daubney209c9c92023-07-18 14:59:45 +010060int main(void)
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010061{
Thomas Daubney2c872342023-07-28 14:21:38 +010062 mbedtls_printf("MBEDTLS_PSA_CRYPTO_C and PSA_WANT_ALG_SHA_256"
Thomas Daubney9520df72023-07-25 10:56:54 +010063 "not defined.\r\n");
Thomas Daubney209c9c92023-07-18 14:59:45 +010064 return EXIT_SUCCESS;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010065}
66#else
67
Thomas Daubney209c9c92023-07-18 14:59:45 +010068int main(void)
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010069{
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010070 psa_status_t status;
Thomas Daubney1db78fa2023-07-24 16:49:14 +010071 uint8_t hash[PSA_HASH_LENGTH(HASH_ALG)];
Thomas Daubney6fc4ca22023-07-28 14:31:06 +010072 size_t hash_length;
Thomas Daubneyc0500372023-07-28 14:44:25 +010073 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
74 psa_hash_operation_t cloned_hash_operation = PSA_HASH_OPERATION_INIT;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010075
Thomas Daubneyf7348ae2023-07-24 12:18:40 +010076 mbedtls_printf("PSA Crypto API: SHA-256 example\n\n");
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010077
Thomas Daubney209c9c92023-07-18 14:59:45 +010078 status = psa_crypto_init();
79 if (status != PSA_SUCCESS) {
Thomas Daubneyf7348ae2023-07-24 12:18:40 +010080 mbedtls_printf("psa_crypto_init failed\n");
Thomas Daubney209c9c92023-07-18 14:59:45 +010081 return EXIT_FAILURE;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010082 }
83
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010084 /* Compute hash using multi-part operation */
85
Thomas Daubneyc0500372023-07-28 14:44:25 +010086 status = psa_hash_setup(&hash_operation, HASH_ALG);
Thomas Daubney76053882023-10-10 17:38:53 +010087 if (status == PSA_ERROR_NOT_SUPPORTED){
88 mbedtls_printf("unknown hash algorithm supplied\n");
89 return EXIT_FAILURE;
90 }
91 else if (status != PSA_SUCCESS) {
Thomas Daubneyf7348ae2023-07-24 12:18:40 +010092 mbedtls_printf("psa_hash_setup failed\n");
Thomas Daubney209c9c92023-07-18 14:59:45 +010093 return EXIT_FAILURE;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010094 }
95
Thomas Daubney1ba97442023-07-28 17:25:16 +010096 /* Note: Here we use sizeof(sample_message) - 1 since we don't wish to
Thomas Daubneyc918c322023-07-28 17:15:03 +010097 * include the null byte in the hash computation */
98 status = psa_hash_update(&hash_operation, sample_message, sizeof(sample_message) - 1);
Thomas Daubney209c9c92023-07-18 14:59:45 +010099 if (status != PSA_SUCCESS) {
Thomas Daubneyf7348ae2023-07-24 12:18:40 +0100100 mbedtls_printf("psa_hash_update failed\n");
Thomas Daubney5c2dcbd2023-08-03 16:03:30 +0100101 goto cleanup;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100102 }
103
Thomas Daubneyc0500372023-07-28 14:44:25 +0100104 status = psa_hash_clone(&hash_operation, &cloned_hash_operation);
Thomas Daubney209c9c92023-07-18 14:59:45 +0100105 if (status != PSA_SUCCESS) {
Thomas Daubneyf7348ae2023-07-24 12:18:40 +0100106 mbedtls_printf("PSA hash clone failed");
Thomas Daubney5c2dcbd2023-08-03 16:03:30 +0100107 goto cleanup;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100108 }
109
Thomas Daubneyc0500372023-07-28 14:44:25 +0100110 status = psa_hash_finish(&hash_operation, hash, sizeof(hash), &hash_length);
Thomas Daubney209c9c92023-07-18 14:59:45 +0100111 if (status != PSA_SUCCESS) {
Thomas Daubneyf7348ae2023-07-24 12:18:40 +0100112 mbedtls_printf("psa_hash_finish failed\n");
Thomas Daubney5c2dcbd2023-08-03 16:03:30 +0100113 goto cleanup;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100114 }
115
Thomas Daubneyce141242023-07-28 16:14:20 +0100116 /* Check the result of the operation against the sample */
Thomas Daubneya68ef952023-08-07 11:09:51 +0100117 if (hash_length != sample_hash_len || (memcmp(hash, sample_hash, sample_hash_len) != 0)) {
Thomas Daubneyce141242023-07-28 16:14:20 +0100118 mbedtls_printf("Multi-part hash operation gave the wrong result!\n\n");
Thomas Daubney5c2dcbd2023-08-03 16:03:30 +0100119 goto cleanup;
Thomas Daubneyce141242023-07-28 16:14:20 +0100120 }
121
Thomas Daubney209c9c92023-07-18 14:59:45 +0100122 status =
Thomas Daubney606110f2023-07-28 15:57:10 +0100123 psa_hash_verify(&cloned_hash_operation, sample_hash,
124 sample_hash_len);
Thomas Daubney209c9c92023-07-18 14:59:45 +0100125 if (status != PSA_SUCCESS) {
Thomas Daubneyf7348ae2023-07-24 12:18:40 +0100126 mbedtls_printf("psa_hash_verify failed\n");
Thomas Daubney5c2dcbd2023-08-03 16:03:30 +0100127 goto cleanup;
Thomas Daubney209c9c92023-07-18 14:59:45 +0100128 } else {
Thomas Daubneyf7348ae2023-07-24 12:18:40 +0100129 mbedtls_printf("Multi-part hash operation successful!\n");
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100130 }
131
Thomas Daubney3071c852023-07-28 14:47:47 +0100132 /* Clear local variables prior to one-shot hash demo */
Thomas Daubney209c9c92023-07-18 14:59:45 +0100133 memset(hash, 0, sizeof(hash));
Thomas Daubney6fc4ca22023-07-28 14:31:06 +0100134 hash_length = 0;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100135
Thomas Daubney3071c852023-07-28 14:47:47 +0100136 /* Compute hash using one-shot function call */
Thomas Daubney1db78fa2023-07-24 16:49:14 +0100137 status = psa_hash_compute(HASH_ALG,
Thomas Daubneyc918c322023-07-28 17:15:03 +0100138 sample_message, sizeof(sample_message) - 1,
Thomas Daubney209c9c92023-07-18 14:59:45 +0100139 hash, sizeof(hash),
Thomas Daubney6fc4ca22023-07-28 14:31:06 +0100140 &hash_length);
Thomas Daubney209c9c92023-07-18 14:59:45 +0100141 if (status != PSA_SUCCESS) {
Thomas Daubneyf7348ae2023-07-24 12:18:40 +0100142 mbedtls_printf("psa_hash_compute failed\n");
Thomas Daubney5c2dcbd2023-08-03 16:03:30 +0100143 goto cleanup;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100144 }
145
Thomas Daubneya68ef952023-08-07 11:09:51 +0100146 if (hash_length != sample_hash_len || (memcmp(hash, sample_hash, sample_hash_len) != 0)) {
Thomas Daubneya2b75192023-07-28 15:21:46 +0100147 mbedtls_printf("One-shot hash operation gave the wrong result!\n\n");
Thomas Daubney5c2dcbd2023-08-03 16:03:30 +0100148 goto cleanup;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100149 }
150
Thomas Daubneyf7348ae2023-07-24 12:18:40 +0100151 mbedtls_printf("One-shot hash operation successful!\n\n");
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100152
Thomas Daubney1f987362023-07-28 15:23:06 +0100153 /* Print out result */
Thomas Daubney606110f2023-07-28 15:57:10 +0100154 mbedtls_printf("The SHA-256( '%s' ) is: ", sample_message);
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100155
Thomas Daubney606110f2023-07-28 15:57:10 +0100156 for (size_t j = 0; j < sample_hash_len; j++) {
Thomas Daubney9730cb12023-07-28 15:07:19 +0100157 mbedtls_printf("%02x", hash[j]);
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100158 }
159
Thomas Daubneyf7348ae2023-07-24 12:18:40 +0100160 mbedtls_printf("\n");
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100161
Thomas Daubney209c9c92023-07-18 14:59:45 +0100162 mbedtls_psa_crypto_free();
163 return EXIT_SUCCESS;
Thomas Daubney5c2dcbd2023-08-03 16:03:30 +0100164
165cleanup:
166 psa_hash_abort(&hash_operation);
167 psa_hash_abort(&cloned_hash_operation);
168 return EXIT_FAILURE;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100169}
Thomas Daubney102033c2023-08-03 16:20:09 +0100170#endif /* MBEDTLS_PSA_CRYPTO_C && PSA_WANT_ALG_SHA_256 */