blob: 07d0b4fa943dfe5fa4fa6eba345eb79e2fdc1a8c [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
36#define TEST_SHA256_HASH { \
Thomas Daubney209c9c92023-07-18 14:59:45 +010037 0x5a, 0x09, 0xe8, 0xfa, 0x9c, 0x77, 0x80, 0x7b, 0x24, 0xe9, 0x9c, 0x9c, \
38 0xf9, 0x99, 0xde, 0xbf, 0xad, 0x84, 0x41, 0xe2, 0x69, 0xeb, 0x96, 0x0e, \
39 0x20, 0x1f, 0x61, 0xfc, 0x3d, 0xe2, 0x0d, 0x5a \
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010040}
41
42const uint8_t mbedtls_test_sha256_hash[] = TEST_SHA256_HASH;
43
44const size_t mbedtls_test_sha256_hash_len =
Thomas Daubney209c9c92023-07-18 14:59:45 +010045 sizeof(mbedtls_test_sha256_hash);
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010046
Thomas Daubneyf7348ae2023-07-24 12:18:40 +010047#if !defined(MBEDTLS_PSA_CRYPTO_C) || !defined(PSA_WANT_ALG_SHA_256)
Thomas Daubney209c9c92023-07-18 14:59:45 +010048int main(void)
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010049{
Thomas Daubneyf7348ae2023-07-24 12:18:40 +010050 mbedtls_printf("MBEDTLS_PSA_CRYPTO_C and MBEDTLS_SHA256_C"
Thomas Daubney209c9c92023-07-18 14:59:45 +010051 "not defined.\r\n");
52 return EXIT_SUCCESS;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010053}
54#else
55
Thomas Daubney209c9c92023-07-18 14:59:45 +010056int main(void)
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010057{
58 uint8_t buf[] = "Hello World!";
59 psa_status_t status;
Thomas Daubneyf7348ae2023-07-24 12:18:40 +010060 uint8_t hash[PSA_HASH_LENGTH(PSA_ALG_SHA_256)];
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010061 size_t hash_size;
62 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
63 psa_hash_operation_t cloned_sha256 = PSA_HASH_OPERATION_INIT;
64
Thomas Daubneyf7348ae2023-07-24 12:18:40 +010065 mbedtls_printf("PSA Crypto API: SHA-256 example\n\n");
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010066
Thomas Daubney209c9c92023-07-18 14:59:45 +010067 status = psa_crypto_init();
68 if (status != PSA_SUCCESS) {
Thomas Daubneyf7348ae2023-07-24 12:18:40 +010069 mbedtls_printf("psa_crypto_init failed\n");
Thomas Daubney209c9c92023-07-18 14:59:45 +010070 return EXIT_FAILURE;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010071 }
72
73
74 /* Compute hash using multi-part operation */
75
Thomas Daubney209c9c92023-07-18 14:59:45 +010076 status = psa_hash_setup(&sha256_psa, PSA_ALG_SHA_256);
77 if (status != PSA_SUCCESS) {
Thomas Daubneyf7348ae2023-07-24 12:18:40 +010078 mbedtls_printf("psa_hash_setup failed\n");
Thomas Daubney209c9c92023-07-18 14:59:45 +010079 return EXIT_FAILURE;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010080 }
81
Thomas Daubney209c9c92023-07-18 14:59:45 +010082 status = psa_hash_update(&sha256_psa, buf, sizeof(buf));
83 if (status != PSA_SUCCESS) {
Thomas Daubneyf7348ae2023-07-24 12:18:40 +010084 mbedtls_printf("psa_hash_update failed\n");
Thomas Daubney209c9c92023-07-18 14:59:45 +010085 return EXIT_FAILURE;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010086 }
87
Thomas Daubney209c9c92023-07-18 14:59:45 +010088 status = psa_hash_clone(&sha256_psa, &cloned_sha256);
89 if (status != PSA_SUCCESS) {
Thomas Daubneyf7348ae2023-07-24 12:18:40 +010090 mbedtls_printf("PSA hash clone failed");
Thomas Daubney209c9c92023-07-18 14:59:45 +010091 return EXIT_FAILURE;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010092 }
93
Thomas Daubney209c9c92023-07-18 14:59:45 +010094 status = psa_hash_finish(&sha256_psa, hash, sizeof(hash), &hash_size);
95 if (status != PSA_SUCCESS) {
Thomas Daubneyf7348ae2023-07-24 12:18:40 +010096 mbedtls_printf("psa_hash_finish failed\n");
Thomas Daubney209c9c92023-07-18 14:59:45 +010097 return EXIT_FAILURE;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010098 }
99
Thomas Daubney209c9c92023-07-18 14:59:45 +0100100 status =
101 psa_hash_verify(&cloned_sha256, mbedtls_test_sha256_hash, mbedtls_test_sha256_hash_len);
102 if (status != PSA_SUCCESS) {
Thomas Daubneyf7348ae2023-07-24 12:18:40 +0100103 mbedtls_printf("psa_hash_verify failed\n");
Thomas Daubney209c9c92023-07-18 14:59:45 +0100104 return EXIT_FAILURE;
105 } else {
Thomas Daubneyf7348ae2023-07-24 12:18:40 +0100106 mbedtls_printf("Multi-part hash operation successful!\n");
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100107 }
108
109 /* Compute hash using one-shot function call */
Thomas Daubney209c9c92023-07-18 14:59:45 +0100110 memset(hash, 0, sizeof(hash));
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100111 hash_size = 0;
112
Thomas Daubney209c9c92023-07-18 14:59:45 +0100113 status = psa_hash_compute(PSA_ALG_SHA_256,
114 buf, sizeof(buf),
115 hash, sizeof(hash),
116 &hash_size);
117 if (status != PSA_SUCCESS) {
Thomas Daubneyf7348ae2023-07-24 12:18:40 +0100118 mbedtls_printf("psa_hash_compute failed\n");
Thomas Daubney209c9c92023-07-18 14:59:45 +0100119 return EXIT_FAILURE;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100120 }
121
Thomas Daubney209c9c92023-07-18 14:59:45 +0100122 for (size_t j = 0; j < mbedtls_test_sha256_hash_len; j++) {
123 if (hash[j] != mbedtls_test_sha256_hash[j]) {
Thomas Daubneyf7348ae2023-07-24 12:18:40 +0100124 mbedtls_printf("One-shot hash operation failed!\n\n");
Thomas Daubney209c9c92023-07-18 14:59:45 +0100125 return EXIT_FAILURE;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100126 }
127 }
128
Thomas Daubneyf7348ae2023-07-24 12:18:40 +0100129 mbedtls_printf("One-shot hash operation successful!\n\n");
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100130
Thomas Daubneyf7348ae2023-07-24 12:18:40 +0100131 mbedtls_printf("The SHA-256( '%s' ) is:\n", buf);
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100132
Thomas Daubney209c9c92023-07-18 14:59:45 +0100133 for (size_t j = 0; j < mbedtls_test_sha256_hash_len; j++) {
134 if (j % 8 == 0) {
Thomas Daubneyf7348ae2023-07-24 12:18:40 +0100135 mbedtls_printf("\n ");
Thomas Daubney209c9c92023-07-18 14:59:45 +0100136 }
Thomas Daubneyf7348ae2023-07-24 12:18:40 +0100137 mbedtls_printf("%02x ", hash[j]);
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100138 }
139
Thomas Daubneyf7348ae2023-07-24 12:18:40 +0100140 mbedtls_printf("\n");
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100141
Thomas Daubney209c9c92023-07-18 14:59:45 +0100142 mbedtls_psa_crypto_free();
143 return EXIT_SUCCESS;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100144}
145#endif /* MBEDTLS_PSA_CRYPTO_C && MBEDTLS_SHA256_C */