-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Add example program for PSA hash #7942
Changes from 6 commits
f8b9ebf
209c9c9
8907815
f7348ae
1db78fa
9520df7
1fd916a
2c87234
6fc4ca2
a79f806
c050037
3071c85
c07fa29
9730cb1
a2b7519
1f98736
606110f
ce14124
fbe742b
c918c32
1ba9744
21fbe4c
5c2dcbd
102033c
a68ef95
86f9795
7605388
3450087
1c2378b
cd79f77
d8453bb
a21c972
2e67781
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Features | ||
* Added an example program showing how to hash with the PSA API. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ set(executables | |
hmac_demo | ||
key_ladder_demo | ||
psa_constant_names | ||
psa_hash | ||
) | ||
|
||
if(GEN_FILES) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's unfortunate that we don't run sample programs on the CI. I would like that at least for new programs we arrange to run them on the CI. #2698 sets up some infrastructure to make that easier. Can we rework this as a part of the epic to get the PSA demo programs in? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At the moment I want to focus on bringing in these PSA programs as a priority, especially since they have been around a while already. I think the suggestion here introduces too much scope creep to the original plan. I am happy to discuss it and potentially work on it after the PSA programs are done, but I don't think we should make #2698 part of this bit of work. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As discussed today, I'm going to split #2698 into two parts: one to add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #7982 is available for review. I would like to merge it and the hash demo independently, but to make a follow-up to run the hash demo on the CI before we merge the other demos. |
||
* Example computing a SHA-256 hash using the PSA Crypto API | ||
* | ||
* The example computes the SHA-256 hash of a test string using the | ||
* one-shot API call psa_hash_compute() and the using multi-part | ||
* operation, which requires psa_hash_setup(), psa_hash_update() and | ||
* psa_hash_finish(). The multi-part operation is popular on embedded | ||
* devices where a rolling hash needs to be computed. | ||
* | ||
* | ||
* Copyright The Mbed TLS Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't normally have two consecutive blank lines in our code There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes we do. And I can't think of a good reason why that would be a problem. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, yes, so we do, but used so inconsistently that they are clearly unintentional (i.e. not clearly separating sections of a file, etc) |
||
#include "psa/crypto.h" | ||
#include <string.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#include "mbedtls/build_info.h" | ||
#include "mbedtls/platform.h" | ||
|
||
#define HASH_ALG PSA_ALG_SHA_256 | ||
|
||
#define TEST_SHA256_HASH { \ | ||
gilles-peskine-arm marked this conversation as resolved.
Show resolved
Hide resolved
gilles-peskine-arm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
0x5a, 0x09, 0xe8, 0xfa, 0x9c, 0x77, 0x80, 0x7b, 0x24, 0xe9, 0x9c, 0x9c, \ | ||
0xf9, 0x99, 0xde, 0xbf, 0xad, 0x84, 0x41, 0xe2, 0x69, 0xeb, 0x96, 0x0e, \ | ||
0x20, 0x1f, 0x61, 0xfc, 0x3d, 0xe2, 0x0d, 0x5a \ | ||
} | ||
|
||
const uint8_t mbedtls_test_sha256_hash[] = TEST_SHA256_HASH; | ||
gilles-peskine-arm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const size_t mbedtls_test_sha256_hash_len = | ||
sizeof(mbedtls_test_sha256_hash); | ||
|
||
#if !defined(MBEDTLS_PSA_CRYPTO_C) || !defined(PSA_WANT_ALG_SHA_256) | ||
int main(void) | ||
{ | ||
mbedtls_printf("MBEDTLS_PSA_CRYPTO_C and MBEDTLS_SHA256_C" | ||
gilles-peskine-arm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"not defined.\r\n"); | ||
return EXIT_SUCCESS; | ||
} | ||
#else | ||
|
||
int main(void) | ||
{ | ||
uint8_t buf[] = "Hello World!"; | ||
gilles-peskine-arm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
psa_status_t status; | ||
uint8_t hash[PSA_HASH_LENGTH(HASH_ALG)]; | ||
size_t hash_size; | ||
psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT; | ||
psa_hash_operation_t cloned_sha256 = PSA_HASH_OPERATION_INIT; | ||
|
||
mbedtls_printf("PSA Crypto API: SHA-256 example\n\n"); | ||
|
||
status = psa_crypto_init(); | ||
if (status != PSA_SUCCESS) { | ||
mbedtls_printf("psa_crypto_init failed\n"); | ||
return EXIT_FAILURE; | ||
} | ||
|
||
|
||
/* Compute hash using multi-part operation */ | ||
|
||
status = psa_hash_setup(&sha256_psa, HASH_ALG); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since technically this can be of any size maybe it's better not to name it sha256_psa? |
||
if (status != PSA_SUCCESS) { | ||
mbedtls_printf("psa_hash_setup failed\n"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like Gilles pointed out There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh whoops yeah good catch. With the clean up labels you want, do you mean so that the calls to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes so at the end you have like: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool, yeah, agree that is better. Will implement now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @waleed-elmelegy-arm I have made the changes. PTAL. |
||
return EXIT_FAILURE; | ||
} | ||
|
||
status = psa_hash_update(&sha256_psa, buf, sizeof(buf)); | ||
if (status != PSA_SUCCESS) { | ||
mbedtls_printf("psa_hash_update failed\n"); | ||
return EXIT_FAILURE; | ||
waleed-elmelegy-arm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
status = psa_hash_clone(&sha256_psa, &cloned_sha256); | ||
if (status != PSA_SUCCESS) { | ||
mbedtls_printf("PSA hash clone failed"); | ||
return EXIT_FAILURE; | ||
} | ||
|
||
status = psa_hash_finish(&sha256_psa, hash, sizeof(hash), &hash_size); | ||
if (status != PSA_SUCCESS) { | ||
mbedtls_printf("psa_hash_finish failed\n"); | ||
return EXIT_FAILURE; | ||
} | ||
gilles-peskine-arm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
status = | ||
psa_hash_verify(&cloned_sha256, mbedtls_test_sha256_hash, mbedtls_test_sha256_hash_len); | ||
if (status != PSA_SUCCESS) { | ||
mbedtls_printf("psa_hash_verify failed\n"); | ||
return EXIT_FAILURE; | ||
} else { | ||
mbedtls_printf("Multi-part hash operation successful!\n"); | ||
} | ||
|
||
/* Compute hash using one-shot function call */ | ||
gilles-peskine-arm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
memset(hash, 0, sizeof(hash)); | ||
hash_size = 0; | ||
|
||
status = psa_hash_compute(HASH_ALG, | ||
buf, sizeof(buf), | ||
hash, sizeof(hash), | ||
&hash_size); | ||
if (status != PSA_SUCCESS) { | ||
mbedtls_printf("psa_hash_compute failed\n"); | ||
return EXIT_FAILURE; | ||
} | ||
|
||
for (size_t j = 0; j < mbedtls_test_sha256_hash_len; j++) { | ||
gilles-peskine-arm marked this conversation as resolved.
Show resolved
Hide resolved
gilles-peskine-arm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (hash[j] != mbedtls_test_sha256_hash[j]) { | ||
waleed-elmelegy-arm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
mbedtls_printf("One-shot hash operation failed!\n\n"); | ||
gilles-peskine-arm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return EXIT_FAILURE; | ||
} | ||
} | ||
|
||
mbedtls_printf("One-shot hash operation successful!\n\n"); | ||
|
||
mbedtls_printf("The SHA-256( '%s' ) is:\n", buf); | ||
|
||
for (size_t j = 0; j < mbedtls_test_sha256_hash_len; j++) { | ||
if (j % 8 == 0) { | ||
mbedtls_printf("\n "); | ||
} | ||
mbedtls_printf("%02x ", hash[j]); | ||
} | ||
gilles-peskine-arm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
mbedtls_printf("\n"); | ||
|
||
mbedtls_psa_crypto_free(); | ||
return EXIT_SUCCESS; | ||
} | ||
#endif /* MBEDTLS_PSA_CRYPTO_C && MBEDTLS_SHA256_C */ | ||
gilles-peskine-arm marked this conversation as resolved.
Show resolved
Hide resolved
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a directory
programs/psa
that contains all programs that are related to the PSA API: examples of simple usage likepsa_hash
andcrypto_examples
andaead_demo
andhmac_demo
, examples of more complex usage likekey_ladder_demo
, and utility programs for developers (just one:psa_constant_names
). This made sense originally when PSA was an experimental feature, but now that it's the preferred API for cryptography, maybe we shouldn't single outpsa
anymore?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Happy to do this as a follow-up PR but in this PR I want to focus on getting the PSA hash program merged.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, let's reorganize later.