blob: a2e2731a9800aa5e600809cc8fe1d4260a623fa3 [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 Daubney1db78fa2023-07-24 16:49:14 +010036#define HASH_ALG PSA_ALG_SHA_256
37
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010038#define TEST_SHA256_HASH { \
Thomas Daubney209c9c92023-07-18 14:59:45 +010039 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 Tschofenigf8b9ebf2023-07-18 13:46:10 +010042}
43
Thomas Daubneyc0500372023-07-28 14:44:25 +010044const uint8_t test_sha256_hash[] = TEST_SHA256_HASH;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010045
Thomas Daubneyc0500372023-07-28 14:44:25 +010046const size_t test_sha256_hash_len =
47 sizeof(test_sha256_hash);
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010048
Thomas Daubneyf7348ae2023-07-24 12:18:40 +010049#if !defined(MBEDTLS_PSA_CRYPTO_C) || !defined(PSA_WANT_ALG_SHA_256)
Thomas Daubney209c9c92023-07-18 14:59:45 +010050int main(void)
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010051{
Thomas Daubney2c872342023-07-28 14:21:38 +010052 mbedtls_printf("MBEDTLS_PSA_CRYPTO_C and PSA_WANT_ALG_SHA_256"
Thomas Daubney9520df72023-07-25 10:56:54 +010053 "not defined.\r\n");
Thomas Daubney209c9c92023-07-18 14:59:45 +010054 return EXIT_SUCCESS;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010055}
56#else
57
Thomas Daubney209c9c92023-07-18 14:59:45 +010058int main(void)
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010059{
60 uint8_t buf[] = "Hello World!";
61 psa_status_t status;
Thomas Daubney1db78fa2023-07-24 16:49:14 +010062 uint8_t hash[PSA_HASH_LENGTH(HASH_ALG)];
Thomas Daubney6fc4ca22023-07-28 14:31:06 +010063 size_t hash_length;
Thomas Daubneyc0500372023-07-28 14:44:25 +010064 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
65 psa_hash_operation_t cloned_hash_operation = PSA_HASH_OPERATION_INIT;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010066
Thomas Daubneyf7348ae2023-07-24 12:18:40 +010067 mbedtls_printf("PSA Crypto API: SHA-256 example\n\n");
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010068
Thomas Daubney209c9c92023-07-18 14:59:45 +010069 status = psa_crypto_init();
70 if (status != PSA_SUCCESS) {
Thomas Daubneyf7348ae2023-07-24 12:18:40 +010071 mbedtls_printf("psa_crypto_init failed\n");
Thomas Daubney209c9c92023-07-18 14:59:45 +010072 return EXIT_FAILURE;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010073 }
74
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010075 /* Compute hash using multi-part operation */
76
Thomas Daubneyc0500372023-07-28 14:44:25 +010077 status = psa_hash_setup(&hash_operation, HASH_ALG);
Thomas Daubney209c9c92023-07-18 14:59:45 +010078 if (status != PSA_SUCCESS) {
Thomas Daubneyf7348ae2023-07-24 12:18:40 +010079 mbedtls_printf("psa_hash_setup failed\n");
Thomas Daubneyc0500372023-07-28 14:44:25 +010080 psa_hash_abort(&hash_operation);
81 psa_hash_abort(&cloned_hash_operation);
Thomas Daubney209c9c92023-07-18 14:59:45 +010082 return EXIT_FAILURE;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010083 }
84
Thomas Daubneyc0500372023-07-28 14:44:25 +010085 status = psa_hash_update(&hash_operation, buf, sizeof(buf));
Thomas Daubney209c9c92023-07-18 14:59:45 +010086 if (status != PSA_SUCCESS) {
Thomas Daubneyf7348ae2023-07-24 12:18:40 +010087 mbedtls_printf("psa_hash_update failed\n");
Thomas Daubneyc0500372023-07-28 14:44:25 +010088 psa_hash_abort(&hash_operation);
89 psa_hash_abort(&cloned_hash_operation);
Thomas Daubney209c9c92023-07-18 14:59:45 +010090 return EXIT_FAILURE;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010091 }
92
Thomas Daubneyc0500372023-07-28 14:44:25 +010093 status = psa_hash_clone(&hash_operation, &cloned_hash_operation);
Thomas Daubney209c9c92023-07-18 14:59:45 +010094 if (status != PSA_SUCCESS) {
Thomas Daubneyf7348ae2023-07-24 12:18:40 +010095 mbedtls_printf("PSA hash clone failed");
Thomas Daubneyc0500372023-07-28 14:44:25 +010096 psa_hash_abort(&hash_operation);
97 psa_hash_abort(&cloned_hash_operation);
Thomas Daubney209c9c92023-07-18 14:59:45 +010098 return EXIT_FAILURE;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010099 }
100
Thomas Daubneyc0500372023-07-28 14:44:25 +0100101 status = psa_hash_finish(&hash_operation, hash, sizeof(hash), &hash_length);
Thomas Daubney209c9c92023-07-18 14:59:45 +0100102 if (status != PSA_SUCCESS) {
Thomas Daubneyf7348ae2023-07-24 12:18:40 +0100103 mbedtls_printf("psa_hash_finish failed\n");
Thomas Daubneyc0500372023-07-28 14:44:25 +0100104 psa_hash_abort(&hash_operation);
105 psa_hash_abort(&cloned_hash_operation);
Thomas Daubney209c9c92023-07-18 14:59:45 +0100106 return EXIT_FAILURE;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100107 }
108
Thomas Daubney209c9c92023-07-18 14:59:45 +0100109 status =
Thomas Daubneyc0500372023-07-28 14:44:25 +0100110 psa_hash_verify(&cloned_hash_operation, test_sha256_hash,
111 test_sha256_hash_len);
Thomas Daubney209c9c92023-07-18 14:59:45 +0100112 if (status != PSA_SUCCESS) {
Thomas Daubneyf7348ae2023-07-24 12:18:40 +0100113 mbedtls_printf("psa_hash_verify failed\n");
Thomas Daubneyc0500372023-07-28 14:44:25 +0100114 psa_hash_abort(&hash_operation);
115 psa_hash_abort(&cloned_hash_operation);
Thomas Daubney209c9c92023-07-18 14:59:45 +0100116 return EXIT_FAILURE;
117 } else {
Thomas Daubneyf7348ae2023-07-24 12:18:40 +0100118 mbedtls_printf("Multi-part hash operation successful!\n");
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100119 }
120
Thomas Daubney3071c852023-07-28 14:47:47 +0100121 /* Clear local variables prior to one-shot hash demo */
Thomas Daubney209c9c92023-07-18 14:59:45 +0100122 memset(hash, 0, sizeof(hash));
Thomas Daubney6fc4ca22023-07-28 14:31:06 +0100123 hash_length = 0;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100124
Thomas Daubney3071c852023-07-28 14:47:47 +0100125 /* Compute hash using one-shot function call */
Thomas Daubney1db78fa2023-07-24 16:49:14 +0100126 status = psa_hash_compute(HASH_ALG,
Thomas Daubney209c9c92023-07-18 14:59:45 +0100127 buf, sizeof(buf),
128 hash, sizeof(hash),
Thomas Daubney6fc4ca22023-07-28 14:31:06 +0100129 &hash_length);
Thomas Daubney209c9c92023-07-18 14:59:45 +0100130 if (status != PSA_SUCCESS) {
Thomas Daubneyf7348ae2023-07-24 12:18:40 +0100131 mbedtls_printf("psa_hash_compute failed\n");
Thomas Daubneyc0500372023-07-28 14:44:25 +0100132 psa_hash_abort(&hash_operation);
133 psa_hash_abort(&cloned_hash_operation);
Thomas Daubney209c9c92023-07-18 14:59:45 +0100134 return EXIT_FAILURE;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100135 }
136
Thomas Daubneya2b75192023-07-28 15:21:46 +0100137 if (memcmp(hash, test_sha256_hash, test_sha256_hash_len) != 0)
138 {
139 mbedtls_printf("One-shot hash operation gave the wrong result!\n\n");
140 psa_hash_abort(&hash_operation);
141 psa_hash_abort(&cloned_hash_operation);
142 return EXIT_FAILURE;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100143 }
144
Thomas Daubneyf7348ae2023-07-24 12:18:40 +0100145 mbedtls_printf("One-shot hash operation successful!\n\n");
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100146
Thomas Daubney9730cb12023-07-28 15:07:19 +0100147 mbedtls_printf("The SHA-256( '%s' ) is: ", buf);
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100148
Thomas Daubneyc0500372023-07-28 14:44:25 +0100149 for (size_t j = 0; j < test_sha256_hash_len; j++) {
Thomas Daubney9730cb12023-07-28 15:07:19 +0100150 mbedtls_printf("%02x", hash[j]);
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100151 }
152
Thomas Daubneyf7348ae2023-07-24 12:18:40 +0100153 mbedtls_printf("\n");
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100154
Thomas Daubney209c9c92023-07-18 14:59:45 +0100155 mbedtls_psa_crypto_free();
156 return EXIT_SUCCESS;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100157}
Thomas Daubneyc0500372023-07-28 14:44:25 +0100158#endif /* MBEDTLS_PSA_CRYPTO_C && PSA_WANT_ALG_SHA_256 */