blob: 4be9c3839b4649afb8a67b67f9372b0f82d5f18f [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"
34
35#define TEST_SHA256_HASH { \
Thomas Daubney209c9c92023-07-18 14:59:45 +010036 0x5a, 0x09, 0xe8, 0xfa, 0x9c, 0x77, 0x80, 0x7b, 0x24, 0xe9, 0x9c, 0x9c, \
37 0xf9, 0x99, 0xde, 0xbf, 0xad, 0x84, 0x41, 0xe2, 0x69, 0xeb, 0x96, 0x0e, \
38 0x20, 0x1f, 0x61, 0xfc, 0x3d, 0xe2, 0x0d, 0x5a \
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010039}
40
41const uint8_t mbedtls_test_sha256_hash[] = TEST_SHA256_HASH;
42
43const size_t mbedtls_test_sha256_hash_len =
Thomas Daubney209c9c92023-07-18 14:59:45 +010044 sizeof(mbedtls_test_sha256_hash);
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010045
46#if !defined(MBEDTLS_PSA_CRYPTO_C) || !defined(MBEDTLS_SHA256_C)
Thomas Daubney209c9c92023-07-18 14:59:45 +010047int main(void)
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010048{
Thomas Daubney209c9c92023-07-18 14:59:45 +010049 printf("MBEDTLS_PSA_CRYPTO_C and MBEDTLS_SHA256_C"
50 "not defined.\r\n");
51 return EXIT_SUCCESS;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010052}
53#else
54
Thomas Daubney209c9c92023-07-18 14:59:45 +010055int main(void)
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010056{
57 uint8_t buf[] = "Hello World!";
58 psa_status_t status;
59 uint8_t hash[PSA_HASH_MAX_SIZE];
60 size_t hash_size;
61 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
62 psa_hash_operation_t cloned_sha256 = PSA_HASH_OPERATION_INIT;
63
Thomas Daubney209c9c92023-07-18 14:59:45 +010064 printf("PSA Crypto API: SHA-256 example\n\n");
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010065
Thomas Daubney209c9c92023-07-18 14:59:45 +010066 status = psa_crypto_init();
67 if (status != PSA_SUCCESS) {
68 printf("psa_crypto_init failed\n");
69 return EXIT_FAILURE;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010070 }
71
72
73 /* Compute hash using multi-part operation */
74
Thomas Daubney209c9c92023-07-18 14:59:45 +010075 status = psa_hash_setup(&sha256_psa, PSA_ALG_SHA_256);
76 if (status != PSA_SUCCESS) {
77 printf("psa_hash_setup failed\n");
78 return EXIT_FAILURE;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010079 }
80
Thomas Daubney209c9c92023-07-18 14:59:45 +010081 status = psa_hash_update(&sha256_psa, buf, sizeof(buf));
82 if (status != PSA_SUCCESS) {
83 printf("psa_hash_update failed\n");
84 return EXIT_FAILURE;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010085 }
86
Thomas Daubney209c9c92023-07-18 14:59:45 +010087 status = psa_hash_clone(&sha256_psa, &cloned_sha256);
88 if (status != PSA_SUCCESS) {
89 printf("PSA hash clone failed");
90 return EXIT_FAILURE;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010091 }
92
Thomas Daubney209c9c92023-07-18 14:59:45 +010093 status = psa_hash_finish(&sha256_psa, hash, sizeof(hash), &hash_size);
94 if (status != PSA_SUCCESS) {
95 printf("psa_hash_finish failed\n");
96 return EXIT_FAILURE;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +010097 }
98
Thomas Daubney209c9c92023-07-18 14:59:45 +010099 status =
100 psa_hash_verify(&cloned_sha256, mbedtls_test_sha256_hash, mbedtls_test_sha256_hash_len);
101 if (status != PSA_SUCCESS) {
102 printf("psa_hash_verify failed\n");
103 return EXIT_FAILURE;
104 } else {
105 printf("Multi-part hash operation successful!\n");
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100106 }
107
108 /* Compute hash using one-shot function call */
Thomas Daubney209c9c92023-07-18 14:59:45 +0100109 memset(hash, 0, sizeof(hash));
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100110 hash_size = 0;
111
Thomas Daubney209c9c92023-07-18 14:59:45 +0100112 status = psa_hash_compute(PSA_ALG_SHA_256,
113 buf, sizeof(buf),
114 hash, sizeof(hash),
115 &hash_size);
116 if (status != PSA_SUCCESS) {
117 printf("psa_hash_compute failed\n");
118 return EXIT_FAILURE;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100119 }
120
Thomas Daubney209c9c92023-07-18 14:59:45 +0100121 for (size_t j = 0; j < mbedtls_test_sha256_hash_len; j++) {
122 if (hash[j] != mbedtls_test_sha256_hash[j]) {
123 printf("One-shot hash operation failed!\n\n");
124 return EXIT_FAILURE;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100125 }
126 }
127
Thomas Daubney209c9c92023-07-18 14:59:45 +0100128 printf("One-shot hash operation successful!\n\n");
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100129
Thomas Daubney209c9c92023-07-18 14:59:45 +0100130 printf("The SHA-256( '%s' ) is:\n", buf);
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100131
Thomas Daubney209c9c92023-07-18 14:59:45 +0100132 for (size_t j = 0; j < mbedtls_test_sha256_hash_len; j++) {
133 if (j % 8 == 0) {
134 printf("\n ");
135 }
136 printf("%02x ", hash[j]);
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100137 }
138
Thomas Daubney209c9c92023-07-18 14:59:45 +0100139 printf("\n");
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100140
Thomas Daubney209c9c92023-07-18 14:59:45 +0100141 mbedtls_psa_crypto_free();
142 return EXIT_SUCCESS;
Hannes Tschofenigf8b9ebf2023-07-18 13:46:10 +0100143}
144#endif /* MBEDTLS_PSA_CRYPTO_C && MBEDTLS_SHA256_C */