This repository contains an example demonstrating the compilation and use of Mbed Crypto on Mbed OS.

Committer:
mbed_official
Date:
Fri Nov 08 14:02:25 2019 +0000
Revision:
1:59536a7f0430
Parent:
0:f34b7234a060
Merge pull request #49 from Patater/reduce-ram-rsa

getting-started: Align with upstream getting started snippets, comment about pedagogical code, and reduce stack usage
.
Commit copied from https://github.com/ARMmbed/mbed-os-example-mbed-crypto

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:f34b7234a060 1 /*
mbed_official 0:f34b7234a060 2 * Copyright (c) 2018-2019, Arm Limited and affiliates
mbed_official 0:f34b7234a060 3 * SPDX-License-Identifier: Apache-2.0
mbed_official 0:f34b7234a060 4 *
mbed_official 0:f34b7234a060 5 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 0:f34b7234a060 6 * you may not use this file except in compliance with the License.
mbed_official 0:f34b7234a060 7 * You may obtain a copy of the License at
mbed_official 0:f34b7234a060 8 *
mbed_official 0:f34b7234a060 9 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 0:f34b7234a060 10 *
mbed_official 0:f34b7234a060 11 * Unless required by applicable law or agreed to in writing, software
mbed_official 0:f34b7234a060 12 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 0:f34b7234a060 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 0:f34b7234a060 14 * See the License for the specific language governing permissions and
mbed_official 0:f34b7234a060 15 * limitations under the License.
mbed_official 0:f34b7234a060 16 */
mbed_official 0:f34b7234a060 17 #include "psa/crypto.h"
mbed_official 0:f34b7234a060 18 #include "mbedtls/version.h"
mbed_official 0:f34b7234a060 19 #include <string.h>
mbed_official 0:f34b7234a060 20 #include <inttypes.h>
mbed_official 0:f34b7234a060 21 #include <stdio.h>
mbed_official 0:f34b7234a060 22 #include <stdlib.h>
mbed_official 0:f34b7234a060 23
mbed_official 0:f34b7234a060 24 #define SOME_PLAINTEXT "I am plaintext."
mbed_official 0:f34b7234a060 25 #define SOME_CIPHERTEXT \
mbed_official 0:f34b7234a060 26 { \
mbed_official 0:f34b7234a060 27 0x9f, 0xbf, 0x0b, 0x99, 0x70, 0xe0, 0x3d, 0xab, \
mbed_official 0:f34b7234a060 28 0xf7, 0x65, 0x43, 0x88, 0x09, 0x2c, 0xb4, 0x66, \
mbed_official 0:f34b7234a060 29 }
mbed_official 0:f34b7234a060 30 #define ENCRYPTED_WITH_IV \
mbed_official 0:f34b7234a060 31 { \
mbed_official 0:f34b7234a060 32 0x0e, 0x42, 0x75, 0x78, 0xb5, 0x0d, 0x17, 0x4f, \
mbed_official 0:f34b7234a060 33 0x6e, 0x13, 0xf4, 0xfd, 0x16, 0x30, 0x3e, 0xc7, \
mbed_official 0:f34b7234a060 34 }
mbed_official 1:59536a7f0430 35 /* This key is present as a global define for the purpose of example. In
mbed_official 1:59536a7f0430 36 * real-world applications, you would not have a key hardcoded in source like
mbed_official 1:59536a7f0430 37 * this. */
mbed_official 1:59536a7f0430 38 static const uint8_t AES_KEY[] =
mbed_official 1:59536a7f0430 39 {
mbed_official 1:59536a7f0430 40 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
mbed_official 1:59536a7f0430 41 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
mbed_official 1:59536a7f0430 42 };
mbed_official 1:59536a7f0430 43 /* This key is present as a global define for the purpose of example. In
mbed_official 1:59536a7f0430 44 * real-world applications, you would not have a key hardcoded in source like
mbed_official 1:59536a7f0430 45 * this. */
mbed_official 1:59536a7f0430 46 static const uint8_t RSA_KEY[] =
mbed_official 1:59536a7f0430 47 {
mbed_official 1:59536a7f0430 48 0x30, 0x82, 0x02, 0x5e, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xaf,
mbed_official 1:59536a7f0430 49 0x05, 0x7d, 0x39, 0x6e, 0xe8, 0x4f, 0xb7, 0x5f, 0xdb, 0xb5, 0xc2, 0xb1,
mbed_official 1:59536a7f0430 50 0x3c, 0x7f, 0xe5, 0xa6, 0x54, 0xaa, 0x8a, 0xa2, 0x47, 0x0b, 0x54, 0x1e,
mbed_official 1:59536a7f0430 51 0xe1, 0xfe, 0xb0, 0xb1, 0x2d, 0x25, 0xc7, 0x97, 0x11, 0x53, 0x12, 0x49,
mbed_official 1:59536a7f0430 52 0xe1, 0x12, 0x96, 0x28, 0x04, 0x2d, 0xbb, 0xb6, 0xc1, 0x20, 0xd1, 0x44,
mbed_official 1:59536a7f0430 53 0x35, 0x24, 0xef, 0x4c, 0x0e, 0x6e, 0x1d, 0x89, 0x56, 0xee, 0xb2, 0x07,
mbed_official 1:59536a7f0430 54 0x7a, 0xf1, 0x23, 0x49, 0xdd, 0xee, 0xe5, 0x44, 0x83, 0xbc, 0x06, 0xc2,
mbed_official 1:59536a7f0430 55 0xc6, 0x19, 0x48, 0xcd, 0x02, 0xb2, 0x02, 0xe7, 0x96, 0xae, 0xbd, 0x94,
mbed_official 1:59536a7f0430 56 0xd3, 0xa7, 0xcb, 0xf8, 0x59, 0xc2, 0xc1, 0x81, 0x9c, 0x32, 0x4c, 0xb8,
mbed_official 1:59536a7f0430 57 0x2b, 0x9c, 0xd3, 0x4e, 0xde, 0x26, 0x3a, 0x2a, 0xbf, 0xfe, 0x47, 0x33,
mbed_official 1:59536a7f0430 58 0xf0, 0x77, 0x86, 0x9e, 0x86, 0x60, 0xf7, 0xd6, 0x83, 0x4d, 0xa5, 0x3d,
mbed_official 1:59536a7f0430 59 0x69, 0x0e, 0xf7, 0x98, 0x5f, 0x6b, 0xc3, 0x02, 0x03, 0x01, 0x00, 0x01,
mbed_official 1:59536a7f0430 60 0x02, 0x81, 0x81, 0x00, 0x87, 0x4b, 0xf0, 0xff, 0xc2, 0xf2, 0xa7, 0x1d,
mbed_official 1:59536a7f0430 61 0x14, 0x67, 0x1d, 0xdd, 0x01, 0x71, 0xc9, 0x54, 0xd7, 0xfd, 0xbf, 0x50,
mbed_official 1:59536a7f0430 62 0x28, 0x1e, 0x4f, 0x6d, 0x99, 0xea, 0x0e, 0x1e, 0xbc, 0xf8, 0x2f, 0xaa,
mbed_official 1:59536a7f0430 63 0x58, 0xe7, 0xb5, 0x95, 0xff, 0xb2, 0x93, 0xd1, 0xab, 0xe1, 0x7f, 0x11,
mbed_official 1:59536a7f0430 64 0x0b, 0x37, 0xc4, 0x8c, 0xc0, 0xf3, 0x6c, 0x37, 0xe8, 0x4d, 0x87, 0x66,
mbed_official 1:59536a7f0430 65 0x21, 0xd3, 0x27, 0xf6, 0x4b, 0xbe, 0x08, 0x45, 0x7d, 0x3e, 0xc4, 0x09,
mbed_official 1:59536a7f0430 66 0x8b, 0xa2, 0xfa, 0x0a, 0x31, 0x9f, 0xba, 0x41, 0x1c, 0x28, 0x41, 0xed,
mbed_official 1:59536a7f0430 67 0x7b, 0xe8, 0x31, 0x96, 0xa8, 0xcd, 0xf9, 0xda, 0xa5, 0xd0, 0x06, 0x94,
mbed_official 1:59536a7f0430 68 0xbc, 0x33, 0x5f, 0xc4, 0xc3, 0x22, 0x17, 0xfe, 0x04, 0x88, 0xbc, 0xe9,
mbed_official 1:59536a7f0430 69 0xcb, 0x72, 0x02, 0xe5, 0x94, 0x68, 0xb1, 0xea, 0xd1, 0x19, 0x00, 0x04,
mbed_official 1:59536a7f0430 70 0x77, 0xdb, 0x2c, 0xa7, 0x97, 0xfa, 0xc1, 0x9e, 0xda, 0x3f, 0x58, 0xc1,
mbed_official 1:59536a7f0430 71 0x02, 0x41, 0x00, 0xe2, 0xab, 0x76, 0x08, 0x41, 0xbb, 0x9d, 0x30, 0xa8,
mbed_official 1:59536a7f0430 72 0x1d, 0x22, 0x2d, 0xe1, 0xeb, 0x73, 0x81, 0xd8, 0x22, 0x14, 0x40, 0x7f,
mbed_official 1:59536a7f0430 73 0x1b, 0x97, 0x5c, 0xbb, 0xfe, 0x4e, 0x1a, 0x94, 0x67, 0xfd, 0x98, 0xad,
mbed_official 1:59536a7f0430 74 0xbd, 0x78, 0xf6, 0x07, 0x83, 0x6c, 0xa5, 0xbe, 0x19, 0x28, 0xb9, 0xd1,
mbed_official 1:59536a7f0430 75 0x60, 0xd9, 0x7f, 0xd4, 0x5c, 0x12, 0xd6, 0xb5, 0x2e, 0x2c, 0x98, 0x71,
mbed_official 1:59536a7f0430 76 0xa1, 0x74, 0xc6, 0x6b, 0x48, 0x81, 0x13, 0x02, 0x41, 0x00, 0xc5, 0xab,
mbed_official 1:59536a7f0430 77 0x27, 0x60, 0x21, 0x59, 0xae, 0x7d, 0x6f, 0x20, 0xc3, 0xc2, 0xee, 0x85,
mbed_official 1:59536a7f0430 78 0x1e, 0x46, 0xdc, 0x11, 0x2e, 0x68, 0x9e, 0x28, 0xd5, 0xfc, 0xbb, 0xf9,
mbed_official 1:59536a7f0430 79 0x90, 0xa9, 0x9e, 0xf8, 0xa9, 0x0b, 0x8b, 0xb4, 0x4f, 0xd3, 0x64, 0x67,
mbed_official 1:59536a7f0430 80 0xe7, 0xfc, 0x17, 0x89, 0xce, 0xb6, 0x63, 0xab, 0xda, 0x33, 0x86, 0x52,
mbed_official 1:59536a7f0430 81 0xc3, 0xc7, 0x3f, 0x11, 0x17, 0x74, 0x90, 0x2e, 0x84, 0x05, 0x65, 0x92,
mbed_official 1:59536a7f0430 82 0x70, 0x91, 0x02, 0x41, 0x00, 0xb6, 0xcd, 0xbd, 0x35, 0x4f, 0x7d, 0xf5,
mbed_official 1:59536a7f0430 83 0x79, 0xa6, 0x3b, 0x48, 0xb3, 0x64, 0x3e, 0x35, 0x3b, 0x84, 0x89, 0x87,
mbed_official 1:59536a7f0430 84 0x77, 0xb4, 0x8b, 0x15, 0xf9, 0x4e, 0x0b, 0xfc, 0x05, 0x67, 0xa6, 0xae,
mbed_official 1:59536a7f0430 85 0x59, 0x11, 0xd5, 0x7a, 0xd6, 0x40, 0x9c, 0xf7, 0x64, 0x7b, 0xf9, 0x62,
mbed_official 1:59536a7f0430 86 0x64, 0xe9, 0xbd, 0x87, 0xeb, 0x95, 0xe2, 0x63, 0xb7, 0x11, 0x0b, 0x9a,
mbed_official 1:59536a7f0430 87 0x1f, 0x9f, 0x94, 0xac, 0xce, 0xd0, 0xfa, 0xfa, 0x4d, 0x02, 0x40, 0x71,
mbed_official 1:59536a7f0430 88 0x19, 0x5e, 0xec, 0x37, 0xe8, 0xd2, 0x57, 0xde, 0xcf, 0xc6, 0x72, 0xb0,
mbed_official 1:59536a7f0430 89 0x7a, 0xe6, 0x39, 0xf1, 0x0c, 0xbb, 0x9b, 0x0c, 0x73, 0x9d, 0x0c, 0x80,
mbed_official 1:59536a7f0430 90 0x99, 0x68, 0xd6, 0x44, 0xa9, 0x4e, 0x3f, 0xd6, 0xed, 0x92, 0x87, 0x07,
mbed_official 1:59536a7f0430 91 0x7a, 0x14, 0x58, 0x3f, 0x37, 0x90, 0x58, 0xf7, 0x6a, 0x8a, 0xec, 0xd4,
mbed_official 1:59536a7f0430 92 0x3c, 0x62, 0xdc, 0x8c, 0x0f, 0x41, 0x76, 0x66, 0x50, 0xd7, 0x25, 0x27,
mbed_official 1:59536a7f0430 93 0x5a, 0xc4, 0xa1, 0x02, 0x41, 0x00, 0xbb, 0x32, 0xd1, 0x33, 0xed, 0xc2,
mbed_official 1:59536a7f0430 94 0xe0, 0x48, 0xd4, 0x63, 0x38, 0x8b, 0x7b, 0xe9, 0xcb, 0x4b, 0xe2, 0x9f,
mbed_official 1:59536a7f0430 95 0x4b, 0x62, 0x50, 0xbe, 0x60, 0x3e, 0x70, 0xe3, 0x64, 0x75, 0x01, 0xc9,
mbed_official 1:59536a7f0430 96 0x7d, 0xdd, 0xe2, 0x0a, 0x4e, 0x71, 0xbe, 0x95, 0xfd, 0x5e, 0x71, 0x78,
mbed_official 1:59536a7f0430 97 0x4e, 0x25, 0xac, 0xa4, 0xba, 0xf2, 0x5b, 0xe5, 0x73, 0x8a, 0xae, 0x59,
mbed_official 1:59536a7f0430 98 0xbb, 0xfe, 0x1c, 0x99, 0x77, 0x81, 0x44, 0x7a, 0x2b, 0x24,
mbed_official 1:59536a7f0430 99 };
mbed_official 0:f34b7234a060 100
mbed_official 0:f34b7234a060 101 #if !defined(MBEDTLS_PSA_CRYPTO_C) || (MBEDTLS_VERSION_NUMBER < 0x02130000)
mbed_official 0:f34b7234a060 102 int main(void)
mbed_official 0:f34b7234a060 103 {
mbed_official 0:f34b7234a060 104 printf("Not all of the requirements are met:\n"
mbed_official 0:f34b7234a060 105 " - MBEDTLS_PSA_CRYPTO_C\n"
mbed_official 0:f34b7234a060 106 " - PSA Crypto API v1.0b3\n");
mbed_official 0:f34b7234a060 107 return 0;
mbed_official 0:f34b7234a060 108 }
mbed_official 0:f34b7234a060 109 #else
mbed_official 0:f34b7234a060 110
mbed_official 1:59536a7f0430 111 static void import_a_key(const uint8_t *key, size_t key_len)
mbed_official 0:f34b7234a060 112 {
mbed_official 0:f34b7234a060 113 psa_status_t status;
mbed_official 0:f34b7234a060 114 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mbed_official 0:f34b7234a060 115 psa_key_handle_t handle;
mbed_official 0:f34b7234a060 116
mbed_official 0:f34b7234a060 117 printf("Import an AES key...\t");
mbed_official 0:f34b7234a060 118 fflush(stdout);
mbed_official 0:f34b7234a060 119
mbed_official 0:f34b7234a060 120 /* Initialize PSA Crypto */
mbed_official 0:f34b7234a060 121 status = psa_crypto_init();
mbed_official 0:f34b7234a060 122 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 123 printf("Failed to initialize PSA Crypto\n");
mbed_official 0:f34b7234a060 124 return;
mbed_official 0:f34b7234a060 125 }
mbed_official 0:f34b7234a060 126
mbed_official 0:f34b7234a060 127 /* Set key attributes */
mbed_official 0:f34b7234a060 128 psa_set_key_usage_flags(&attributes, 0);
mbed_official 0:f34b7234a060 129 psa_set_key_algorithm(&attributes, 0);
mbed_official 0:f34b7234a060 130 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
mbed_official 0:f34b7234a060 131 psa_set_key_bits(&attributes, 128);
mbed_official 0:f34b7234a060 132
mbed_official 0:f34b7234a060 133 /* Import the key */
mbed_official 1:59536a7f0430 134 status = psa_import_key(&attributes, key, key_len, &handle);
mbed_official 0:f34b7234a060 135 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 136 printf("Failed to import key\n");
mbed_official 0:f34b7234a060 137 return;
mbed_official 0:f34b7234a060 138 }
mbed_official 0:f34b7234a060 139 printf("Imported a key\n");
mbed_official 0:f34b7234a060 140
mbed_official 0:f34b7234a060 141 /* Free the attributes */
mbed_official 0:f34b7234a060 142 psa_reset_key_attributes(&attributes);
mbed_official 0:f34b7234a060 143
mbed_official 0:f34b7234a060 144 /* Destroy the key */
mbed_official 0:f34b7234a060 145 psa_destroy_key(handle);
mbed_official 0:f34b7234a060 146
mbed_official 0:f34b7234a060 147 mbedtls_psa_crypto_free();
mbed_official 0:f34b7234a060 148 }
mbed_official 0:f34b7234a060 149
mbed_official 1:59536a7f0430 150 static void sign_a_message_using_rsa(const uint8_t *key, size_t key_len)
mbed_official 0:f34b7234a060 151 {
mbed_official 0:f34b7234a060 152 psa_status_t status;
mbed_official 0:f34b7234a060 153 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mbed_official 0:f34b7234a060 154 uint8_t hash[] = "INPUT_FOR_SIGN";
mbed_official 0:f34b7234a060 155 uint8_t signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
mbed_official 0:f34b7234a060 156 size_t signature_length;
mbed_official 0:f34b7234a060 157 psa_key_handle_t handle;
mbed_official 0:f34b7234a060 158
mbed_official 0:f34b7234a060 159 printf("Sign a message...\t");
mbed_official 0:f34b7234a060 160 fflush(stdout);
mbed_official 0:f34b7234a060 161
mbed_official 0:f34b7234a060 162 /* Initialize PSA Crypto */
mbed_official 0:f34b7234a060 163 status = psa_crypto_init();
mbed_official 0:f34b7234a060 164 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 165 printf("Failed to initialize PSA Crypto\n");
mbed_official 0:f34b7234a060 166 return;
mbed_official 0:f34b7234a060 167 }
mbed_official 0:f34b7234a060 168
mbed_official 0:f34b7234a060 169 /* Set key attributes */
mbed_official 0:f34b7234a060 170 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN);
mbed_official 0:f34b7234a060 171 psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_SIGN_RAW);
mbed_official 0:f34b7234a060 172 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
mbed_official 0:f34b7234a060 173 psa_set_key_bits(&attributes, 1024);
mbed_official 0:f34b7234a060 174
mbed_official 0:f34b7234a060 175 /* Import the key */
mbed_official 1:59536a7f0430 176 status = psa_import_key(&attributes, key, key_len, &handle);
mbed_official 0:f34b7234a060 177 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 178 printf("Failed to import key\n");
mbed_official 0:f34b7234a060 179 return;
mbed_official 0:f34b7234a060 180 }
mbed_official 0:f34b7234a060 181
mbed_official 0:f34b7234a060 182 /* Sign message using the key */
mbed_official 0:f34b7234a060 183 status = psa_asymmetric_sign(handle, PSA_ALG_RSA_PKCS1V15_SIGN_RAW,
mbed_official 0:f34b7234a060 184 hash, sizeof(hash),
mbed_official 0:f34b7234a060 185 signature, sizeof(signature),
mbed_official 0:f34b7234a060 186 &signature_length);
mbed_official 0:f34b7234a060 187 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 188 printf("Failed to sign\n");
mbed_official 0:f34b7234a060 189 return;
mbed_official 0:f34b7234a060 190 }
mbed_official 0:f34b7234a060 191
mbed_official 0:f34b7234a060 192 printf("Signed a message\n");
mbed_official 0:f34b7234a060 193
mbed_official 0:f34b7234a060 194 /* Free the attributes */
mbed_official 0:f34b7234a060 195 psa_reset_key_attributes(&attributes);
mbed_official 0:f34b7234a060 196
mbed_official 0:f34b7234a060 197 /* Destroy the key */
mbed_official 0:f34b7234a060 198 psa_destroy_key(handle);
mbed_official 0:f34b7234a060 199
mbed_official 0:f34b7234a060 200 mbedtls_psa_crypto_free();
mbed_official 0:f34b7234a060 201 }
mbed_official 0:f34b7234a060 202
mbed_official 1:59536a7f0430 203 static void encrypt_with_symmetric_ciphers(const uint8_t *key, size_t key_len)
mbed_official 0:f34b7234a060 204 {
mbed_official 0:f34b7234a060 205 enum {
mbed_official 0:f34b7234a060 206 block_size = PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES),
mbed_official 0:f34b7234a060 207 };
mbed_official 0:f34b7234a060 208 psa_status_t status;
mbed_official 0:f34b7234a060 209 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mbed_official 0:f34b7234a060 210 psa_algorithm_t alg = PSA_ALG_CBC_NO_PADDING;
mbed_official 0:f34b7234a060 211 uint8_t plaintext[block_size] = SOME_PLAINTEXT;
mbed_official 0:f34b7234a060 212 uint8_t iv[block_size];
mbed_official 0:f34b7234a060 213 size_t iv_len;
mbed_official 0:f34b7234a060 214 uint8_t output[block_size];
mbed_official 0:f34b7234a060 215 size_t output_len;
mbed_official 0:f34b7234a060 216 psa_key_handle_t handle;
mbed_official 0:f34b7234a060 217 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
mbed_official 0:f34b7234a060 218
mbed_official 0:f34b7234a060 219 printf("Encrypt with cipher...\t");
mbed_official 0:f34b7234a060 220 fflush(stdout);
mbed_official 0:f34b7234a060 221
mbed_official 0:f34b7234a060 222 /* Initialize PSA Crypto */
mbed_official 0:f34b7234a060 223 status = psa_crypto_init();
mbed_official 0:f34b7234a060 224 if (status != PSA_SUCCESS)
mbed_official 0:f34b7234a060 225 {
mbed_official 0:f34b7234a060 226 printf("Failed to initialize PSA Crypto\n");
mbed_official 0:f34b7234a060 227 return;
mbed_official 0:f34b7234a060 228 }
mbed_official 0:f34b7234a060 229
mbed_official 0:f34b7234a060 230 /* Import a key */
mbed_official 0:f34b7234a060 231 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
mbed_official 0:f34b7234a060 232 psa_set_key_algorithm(&attributes, alg);
mbed_official 0:f34b7234a060 233 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
mbed_official 0:f34b7234a060 234 psa_set_key_bits(&attributes, 128);
mbed_official 1:59536a7f0430 235 status = psa_import_key(&attributes, key, key_len, &handle);
mbed_official 0:f34b7234a060 236 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 237 printf("Failed to import a key\n");
mbed_official 0:f34b7234a060 238 return;
mbed_official 0:f34b7234a060 239 }
mbed_official 0:f34b7234a060 240 psa_reset_key_attributes(&attributes);
mbed_official 0:f34b7234a060 241
mbed_official 0:f34b7234a060 242 /* Encrypt the plaintext */
mbed_official 0:f34b7234a060 243 status = psa_cipher_encrypt_setup(&operation, handle, alg);
mbed_official 0:f34b7234a060 244 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 245 printf("Failed to begin cipher operation\n");
mbed_official 0:f34b7234a060 246 return;
mbed_official 0:f34b7234a060 247 }
mbed_official 0:f34b7234a060 248 status = psa_cipher_generate_iv(&operation, iv, sizeof(iv), &iv_len);
mbed_official 0:f34b7234a060 249 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 250 printf("Failed to generate IV\n");
mbed_official 0:f34b7234a060 251 return;
mbed_official 0:f34b7234a060 252 }
mbed_official 0:f34b7234a060 253 status = psa_cipher_update(&operation, plaintext, sizeof(plaintext),
mbed_official 0:f34b7234a060 254 output, sizeof(output), &output_len);
mbed_official 0:f34b7234a060 255 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 256 printf("Failed to update cipher operation\n");
mbed_official 0:f34b7234a060 257 return;
mbed_official 0:f34b7234a060 258 }
mbed_official 0:f34b7234a060 259 status = psa_cipher_finish(&operation, output + output_len,
mbed_official 0:f34b7234a060 260 sizeof(output) - output_len, &output_len);
mbed_official 0:f34b7234a060 261 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 262 printf("Failed to finish cipher operation\n");
mbed_official 0:f34b7234a060 263 return;
mbed_official 0:f34b7234a060 264 }
mbed_official 0:f34b7234a060 265 printf("Encrypted plaintext\n");
mbed_official 0:f34b7234a060 266
mbed_official 0:f34b7234a060 267 /* Clean up cipher operation context */
mbed_official 0:f34b7234a060 268 psa_cipher_abort(&operation);
mbed_official 0:f34b7234a060 269
mbed_official 0:f34b7234a060 270 /* Destroy the key */
mbed_official 0:f34b7234a060 271 psa_destroy_key(handle);
mbed_official 0:f34b7234a060 272
mbed_official 0:f34b7234a060 273 mbedtls_psa_crypto_free();
mbed_official 0:f34b7234a060 274 }
mbed_official 0:f34b7234a060 275
mbed_official 1:59536a7f0430 276 static void decrypt_with_symmetric_ciphers(const uint8_t *key, size_t key_len)
mbed_official 0:f34b7234a060 277 {
mbed_official 0:f34b7234a060 278 enum {
mbed_official 0:f34b7234a060 279 block_size = PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES),
mbed_official 0:f34b7234a060 280 };
mbed_official 0:f34b7234a060 281 psa_status_t status;
mbed_official 0:f34b7234a060 282 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mbed_official 0:f34b7234a060 283 psa_algorithm_t alg = PSA_ALG_CBC_NO_PADDING;
mbed_official 0:f34b7234a060 284 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
mbed_official 0:f34b7234a060 285 uint8_t ciphertext[block_size] = SOME_CIPHERTEXT;
mbed_official 0:f34b7234a060 286 uint8_t iv[block_size] = ENCRYPTED_WITH_IV;
mbed_official 0:f34b7234a060 287 uint8_t output[block_size];
mbed_official 0:f34b7234a060 288 size_t output_len;
mbed_official 0:f34b7234a060 289 psa_key_handle_t handle;
mbed_official 0:f34b7234a060 290
mbed_official 0:f34b7234a060 291 printf("Decrypt with cipher...\t");
mbed_official 0:f34b7234a060 292 fflush(stdout);
mbed_official 0:f34b7234a060 293
mbed_official 0:f34b7234a060 294 /* Initialize PSA Crypto */
mbed_official 0:f34b7234a060 295 status = psa_crypto_init();
mbed_official 0:f34b7234a060 296 if (status != PSA_SUCCESS)
mbed_official 0:f34b7234a060 297 {
mbed_official 0:f34b7234a060 298 printf("Failed to initialize PSA Crypto\n");
mbed_official 0:f34b7234a060 299 return;
mbed_official 0:f34b7234a060 300 }
mbed_official 0:f34b7234a060 301
mbed_official 0:f34b7234a060 302 /* Import a key */
mbed_official 0:f34b7234a060 303 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
mbed_official 0:f34b7234a060 304 psa_set_key_algorithm(&attributes, alg);
mbed_official 0:f34b7234a060 305 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
mbed_official 0:f34b7234a060 306 psa_set_key_bits(&attributes, 128);
mbed_official 1:59536a7f0430 307 status = psa_import_key(&attributes, key, key_len, &handle);
mbed_official 0:f34b7234a060 308 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 309 printf("Failed to import a key\n");
mbed_official 0:f34b7234a060 310 return;
mbed_official 0:f34b7234a060 311 }
mbed_official 0:f34b7234a060 312 psa_reset_key_attributes(&attributes);
mbed_official 0:f34b7234a060 313
mbed_official 0:f34b7234a060 314 /* Decrypt the ciphertext */
mbed_official 0:f34b7234a060 315 status = psa_cipher_decrypt_setup(&operation, handle, alg);
mbed_official 0:f34b7234a060 316 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 317 printf("Failed to begin cipher operation\n");
mbed_official 0:f34b7234a060 318 return;
mbed_official 0:f34b7234a060 319 }
mbed_official 0:f34b7234a060 320 status = psa_cipher_set_iv(&operation, iv, sizeof(iv));
mbed_official 0:f34b7234a060 321 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 322 printf("Failed to set IV\n");
mbed_official 0:f34b7234a060 323 return;
mbed_official 0:f34b7234a060 324 }
mbed_official 0:f34b7234a060 325 status = psa_cipher_update(&operation, ciphertext, sizeof(ciphertext),
mbed_official 0:f34b7234a060 326 output, sizeof(output), &output_len);
mbed_official 0:f34b7234a060 327 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 328 printf("Failed to update cipher operation\n");
mbed_official 0:f34b7234a060 329 return;
mbed_official 0:f34b7234a060 330 }
mbed_official 0:f34b7234a060 331 status = psa_cipher_finish(&operation, output + output_len,
mbed_official 0:f34b7234a060 332 sizeof(output) - output_len, &output_len);
mbed_official 0:f34b7234a060 333 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 334 printf("Failed to finish cipher operation\n");
mbed_official 0:f34b7234a060 335 return;
mbed_official 0:f34b7234a060 336 }
mbed_official 0:f34b7234a060 337 printf("Decrypted ciphertext\n");
mbed_official 0:f34b7234a060 338
mbed_official 0:f34b7234a060 339 /* Clean up cipher operation context */
mbed_official 0:f34b7234a060 340 psa_cipher_abort(&operation);
mbed_official 0:f34b7234a060 341
mbed_official 0:f34b7234a060 342 /* Destroy the key */
mbed_official 0:f34b7234a060 343 psa_destroy_key(handle);
mbed_official 0:f34b7234a060 344
mbed_official 0:f34b7234a060 345 mbedtls_psa_crypto_free();
mbed_official 0:f34b7234a060 346 }
mbed_official 0:f34b7234a060 347
mbed_official 0:f34b7234a060 348 static void hash_a_message(void)
mbed_official 0:f34b7234a060 349 {
mbed_official 0:f34b7234a060 350 psa_status_t status;
mbed_official 0:f34b7234a060 351 psa_algorithm_t alg = PSA_ALG_SHA_256;
mbed_official 0:f34b7234a060 352 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
mbed_official 0:f34b7234a060 353 unsigned char input[] = { 'a', 'b', 'c' };
mbed_official 0:f34b7234a060 354 unsigned char actual_hash[PSA_HASH_MAX_SIZE];
mbed_official 0:f34b7234a060 355 size_t actual_hash_len;
mbed_official 0:f34b7234a060 356
mbed_official 0:f34b7234a060 357 printf("Hash a message...\t");
mbed_official 0:f34b7234a060 358 fflush(stdout);
mbed_official 0:f34b7234a060 359
mbed_official 0:f34b7234a060 360 /* Initialize PSA Crypto */
mbed_official 0:f34b7234a060 361 status = psa_crypto_init();
mbed_official 0:f34b7234a060 362 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 363 printf("Failed to initialize PSA Crypto\n");
mbed_official 0:f34b7234a060 364 return;
mbed_official 0:f34b7234a060 365 }
mbed_official 0:f34b7234a060 366
mbed_official 0:f34b7234a060 367 /* Compute hash of message */
mbed_official 0:f34b7234a060 368 status = psa_hash_setup(&operation, alg);
mbed_official 0:f34b7234a060 369 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 370 printf("Failed to begin hash operation\n");
mbed_official 0:f34b7234a060 371 return;
mbed_official 0:f34b7234a060 372 }
mbed_official 0:f34b7234a060 373 status = psa_hash_update(&operation, input, sizeof(input));
mbed_official 0:f34b7234a060 374 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 375 printf("Failed to update hash operation\n");
mbed_official 0:f34b7234a060 376 return;
mbed_official 0:f34b7234a060 377 }
mbed_official 0:f34b7234a060 378 status = psa_hash_finish(&operation, actual_hash, sizeof(actual_hash),
mbed_official 0:f34b7234a060 379 &actual_hash_len);
mbed_official 0:f34b7234a060 380 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 381 printf("Failed to finish hash operation\n");
mbed_official 0:f34b7234a060 382 return;
mbed_official 0:f34b7234a060 383 }
mbed_official 0:f34b7234a060 384
mbed_official 0:f34b7234a060 385 printf("Hashed a message\n");
mbed_official 0:f34b7234a060 386
mbed_official 0:f34b7234a060 387 /* Clean up hash operation context */
mbed_official 0:f34b7234a060 388 psa_hash_abort(&operation);
mbed_official 0:f34b7234a060 389
mbed_official 0:f34b7234a060 390 mbedtls_psa_crypto_free();
mbed_official 0:f34b7234a060 391 }
mbed_official 0:f34b7234a060 392
mbed_official 0:f34b7234a060 393 static void verify_a_hash(void)
mbed_official 0:f34b7234a060 394 {
mbed_official 0:f34b7234a060 395 psa_status_t status;
mbed_official 0:f34b7234a060 396 psa_algorithm_t alg = PSA_ALG_SHA_256;
mbed_official 0:f34b7234a060 397 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
mbed_official 0:f34b7234a060 398 unsigned char input[] = { 'a', 'b', 'c' };
mbed_official 0:f34b7234a060 399 unsigned char expected_hash[] = {
mbed_official 0:f34b7234a060 400 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde,
mbed_official 0:f34b7234a060 401 0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
mbed_official 0:f34b7234a060 402 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
mbed_official 0:f34b7234a060 403 };
mbed_official 0:f34b7234a060 404 size_t expected_hash_len = PSA_HASH_SIZE(alg);
mbed_official 0:f34b7234a060 405
mbed_official 0:f34b7234a060 406 printf("Verify a hash...\t");
mbed_official 0:f34b7234a060 407 fflush(stdout);
mbed_official 0:f34b7234a060 408
mbed_official 0:f34b7234a060 409 /* Initialize PSA Crypto */
mbed_official 0:f34b7234a060 410 status = psa_crypto_init();
mbed_official 0:f34b7234a060 411 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 412 printf("Failed to initialize PSA Crypto\n");
mbed_official 0:f34b7234a060 413 return;
mbed_official 0:f34b7234a060 414 }
mbed_official 0:f34b7234a060 415
mbed_official 0:f34b7234a060 416 /* Verify message hash */
mbed_official 0:f34b7234a060 417 status = psa_hash_setup(&operation, alg);
mbed_official 0:f34b7234a060 418 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 419 printf("Failed to begin hash operation\n");
mbed_official 0:f34b7234a060 420 return;
mbed_official 0:f34b7234a060 421 }
mbed_official 0:f34b7234a060 422 status = psa_hash_update(&operation, input, sizeof(input));
mbed_official 0:f34b7234a060 423 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 424 printf("Failed to update hash operation\n");
mbed_official 0:f34b7234a060 425 return;
mbed_official 0:f34b7234a060 426 }
mbed_official 0:f34b7234a060 427 status = psa_hash_verify(&operation, expected_hash, expected_hash_len);
mbed_official 0:f34b7234a060 428 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 429 printf("Failed to verify hash\n");
mbed_official 0:f34b7234a060 430 return;
mbed_official 0:f34b7234a060 431 }
mbed_official 0:f34b7234a060 432
mbed_official 0:f34b7234a060 433 printf("Verified a hash\n");
mbed_official 0:f34b7234a060 434
mbed_official 0:f34b7234a060 435 /* Clean up hash operation context */
mbed_official 0:f34b7234a060 436 psa_hash_abort(&operation);
mbed_official 0:f34b7234a060 437
mbed_official 0:f34b7234a060 438 mbedtls_psa_crypto_free();
mbed_official 0:f34b7234a060 439 }
mbed_official 0:f34b7234a060 440
mbed_official 0:f34b7234a060 441 static void generate_a_random_value(void)
mbed_official 0:f34b7234a060 442 {
mbed_official 0:f34b7234a060 443 psa_status_t status;
mbed_official 0:f34b7234a060 444 uint8_t random[10] = { 0 };
mbed_official 0:f34b7234a060 445
mbed_official 0:f34b7234a060 446 printf("Generate random...\t");
mbed_official 0:f34b7234a060 447 fflush(stdout);
mbed_official 0:f34b7234a060 448
mbed_official 0:f34b7234a060 449 /* Initialize PSA Crypto */
mbed_official 0:f34b7234a060 450 status = psa_crypto_init();
mbed_official 0:f34b7234a060 451 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 452 printf("Failed to initialize PSA Crypto\n");
mbed_official 0:f34b7234a060 453 return;
mbed_official 0:f34b7234a060 454 }
mbed_official 0:f34b7234a060 455
mbed_official 0:f34b7234a060 456 status = psa_generate_random(random, sizeof(random));
mbed_official 0:f34b7234a060 457 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 458 printf("Failed to generate a random value\n");
mbed_official 0:f34b7234a060 459 return;
mbed_official 0:f34b7234a060 460 }
mbed_official 0:f34b7234a060 461
mbed_official 0:f34b7234a060 462 printf("Generated random data\n");
mbed_official 0:f34b7234a060 463
mbed_official 0:f34b7234a060 464 /* Clean up */
mbed_official 0:f34b7234a060 465 mbedtls_psa_crypto_free();
mbed_official 0:f34b7234a060 466 }
mbed_official 0:f34b7234a060 467
mbed_official 0:f34b7234a060 468 static void derive_a_new_key_from_an_existing_key(void)
mbed_official 0:f34b7234a060 469 {
mbed_official 0:f34b7234a060 470 psa_status_t status;
mbed_official 0:f34b7234a060 471 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mbed_official 0:f34b7234a060 472 static const unsigned char key[] = {
mbed_official 0:f34b7234a060 473 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
mbed_official 0:f34b7234a060 474 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
mbed_official 0:f34b7234a060 475 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
mbed_official 0:f34b7234a060 476 0x0b };
mbed_official 0:f34b7234a060 477 static const unsigned char salt[] = {
mbed_official 0:f34b7234a060 478 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
mbed_official 0:f34b7234a060 479 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c };
mbed_official 0:f34b7234a060 480 static const unsigned char info[] = {
mbed_official 0:f34b7234a060 481 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6,
mbed_official 0:f34b7234a060 482 0xf7, 0xf8, 0xf9 };
mbed_official 0:f34b7234a060 483 psa_algorithm_t alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
mbed_official 0:f34b7234a060 484 psa_key_derivation_operation_t operation =
mbed_official 0:f34b7234a060 485 PSA_KEY_DERIVATION_OPERATION_INIT;
mbed_official 0:f34b7234a060 486 size_t derived_bits = 128;
mbed_official 0:f34b7234a060 487 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
mbed_official 0:f34b7234a060 488 psa_key_handle_t base_key;
mbed_official 0:f34b7234a060 489 psa_key_handle_t derived_key;
mbed_official 0:f34b7234a060 490
mbed_official 0:f34b7234a060 491 printf("Derive a key (HKDF)...\t");
mbed_official 0:f34b7234a060 492 fflush(stdout);
mbed_official 0:f34b7234a060 493
mbed_official 0:f34b7234a060 494 /* Initialize PSA Crypto */
mbed_official 0:f34b7234a060 495 status = psa_crypto_init();
mbed_official 0:f34b7234a060 496 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 497 printf("Failed to initialize PSA Crypto\n");
mbed_official 0:f34b7234a060 498 return;
mbed_official 0:f34b7234a060 499 }
mbed_official 0:f34b7234a060 500
mbed_official 0:f34b7234a060 501 /* Import a key for use in key derivation. If such a key has already been
mbed_official 0:f34b7234a060 502 * generated or imported, you can skip this part. */
mbed_official 0:f34b7234a060 503 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
mbed_official 0:f34b7234a060 504 psa_set_key_algorithm(&attributes, alg);
mbed_official 0:f34b7234a060 505 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
mbed_official 0:f34b7234a060 506 status = psa_import_key(&attributes, key, sizeof(key), &base_key);
mbed_official 0:f34b7234a060 507 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 508 printf("Failed to import a key\n");
mbed_official 0:f34b7234a060 509 return;
mbed_official 0:f34b7234a060 510 }
mbed_official 0:f34b7234a060 511 psa_reset_key_attributes(&attributes);
mbed_official 0:f34b7234a060 512
mbed_official 0:f34b7234a060 513 /* Derive a key */
mbed_official 0:f34b7234a060 514 status = psa_key_derivation_setup(&operation, alg);
mbed_official 0:f34b7234a060 515 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 516 printf("Failed to begin key derivation\n");
mbed_official 0:f34b7234a060 517 return;
mbed_official 0:f34b7234a060 518 }
mbed_official 0:f34b7234a060 519 status = psa_key_derivation_set_capacity(&operation, capacity);
mbed_official 0:f34b7234a060 520 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 521 printf("Failed to set capacity\n");
mbed_official 0:f34b7234a060 522 return;
mbed_official 0:f34b7234a060 523 }
mbed_official 0:f34b7234a060 524 status = psa_key_derivation_input_bytes(&operation,
mbed_official 0:f34b7234a060 525 PSA_KEY_DERIVATION_INPUT_SALT,
mbed_official 0:f34b7234a060 526 salt, sizeof(salt));
mbed_official 0:f34b7234a060 527 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 528 printf("Failed to input salt (extract)\n");
mbed_official 0:f34b7234a060 529 return;
mbed_official 0:f34b7234a060 530 }
mbed_official 0:f34b7234a060 531 status = psa_key_derivation_input_key(&operation,
mbed_official 0:f34b7234a060 532 PSA_KEY_DERIVATION_INPUT_SECRET,
mbed_official 0:f34b7234a060 533 base_key);
mbed_official 0:f34b7234a060 534 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 535 printf("Failed to input key (extract)\n");
mbed_official 0:f34b7234a060 536 return;
mbed_official 0:f34b7234a060 537 }
mbed_official 0:f34b7234a060 538 status = psa_key_derivation_input_bytes(&operation,
mbed_official 0:f34b7234a060 539 PSA_KEY_DERIVATION_INPUT_INFO,
mbed_official 0:f34b7234a060 540 info, sizeof(info));
mbed_official 0:f34b7234a060 541 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 542 printf("Failed to input info (expand)\n");
mbed_official 0:f34b7234a060 543 return;
mbed_official 0:f34b7234a060 544 }
mbed_official 0:f34b7234a060 545 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
mbed_official 0:f34b7234a060 546 psa_set_key_algorithm(&attributes, PSA_ALG_CTR);
mbed_official 0:f34b7234a060 547 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
mbed_official 0:f34b7234a060 548 psa_set_key_bits(&attributes, 128);
mbed_official 0:f34b7234a060 549 status = psa_key_derivation_output_key(&attributes, &operation,
mbed_official 0:f34b7234a060 550 &derived_key);
mbed_official 0:f34b7234a060 551 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 552 printf("Failed to derive key\n");
mbed_official 0:f34b7234a060 553 return;
mbed_official 0:f34b7234a060 554 }
mbed_official 0:f34b7234a060 555 psa_reset_key_attributes(&attributes);
mbed_official 0:f34b7234a060 556
mbed_official 0:f34b7234a060 557 printf("Derived key\n");
mbed_official 0:f34b7234a060 558
mbed_official 0:f34b7234a060 559 /* Clean up key derivation operation */
mbed_official 0:f34b7234a060 560 psa_key_derivation_abort(&operation);
mbed_official 0:f34b7234a060 561
mbed_official 0:f34b7234a060 562 /* Destroy the keys */
mbed_official 0:f34b7234a060 563 psa_destroy_key(derived_key);
mbed_official 0:f34b7234a060 564 psa_destroy_key(base_key);
mbed_official 0:f34b7234a060 565
mbed_official 0:f34b7234a060 566 mbedtls_psa_crypto_free();
mbed_official 0:f34b7234a060 567 }
mbed_official 0:f34b7234a060 568
mbed_official 0:f34b7234a060 569 static void authenticate_and_encrypt_a_message(void)
mbed_official 0:f34b7234a060 570 {
mbed_official 0:f34b7234a060 571 psa_status_t status;
mbed_official 0:f34b7234a060 572 static const uint8_t key[] = {
mbed_official 0:f34b7234a060 573 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
mbed_official 0:f34b7234a060 574 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF };
mbed_official 0:f34b7234a060 575 static const uint8_t nonce[] = {
mbed_official 0:f34b7234a060 576 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
mbed_official 0:f34b7234a060 577 0x08, 0x09, 0x0A, 0x0B };
mbed_official 0:f34b7234a060 578 static const uint8_t additional_data[] = {
mbed_official 0:f34b7234a060 579 0xEC, 0x46, 0xBB, 0x63, 0xB0, 0x25,
mbed_official 0:f34b7234a060 580 0x20, 0xC3, 0x3C, 0x49, 0xFD, 0x70 };
mbed_official 0:f34b7234a060 581 static const uint8_t input_data[] = {
mbed_official 0:f34b7234a060 582 0xB9, 0x6B, 0x49, 0xE2, 0x1D, 0x62, 0x17, 0x41,
mbed_official 0:f34b7234a060 583 0x63, 0x28, 0x75, 0xDB, 0x7F, 0x6C, 0x92, 0x43,
mbed_official 0:f34b7234a060 584 0xD2, 0xD7, 0xC2 };
mbed_official 0:f34b7234a060 585 uint8_t *output_data = NULL;
mbed_official 0:f34b7234a060 586 size_t output_size = 0;
mbed_official 0:f34b7234a060 587 size_t output_length = 0;
mbed_official 0:f34b7234a060 588 size_t tag_length = 16;
mbed_official 0:f34b7234a060 589 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mbed_official 0:f34b7234a060 590 psa_key_handle_t handle;
mbed_official 0:f34b7234a060 591
mbed_official 0:f34b7234a060 592 printf("Authenticate encrypt...\t");
mbed_official 0:f34b7234a060 593 fflush(stdout);
mbed_official 0:f34b7234a060 594
mbed_official 0:f34b7234a060 595 /* Initialize PSA Crypto */
mbed_official 0:f34b7234a060 596 status = psa_crypto_init();
mbed_official 0:f34b7234a060 597 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 598 printf("Failed to initialize PSA Crypto\n");
mbed_official 0:f34b7234a060 599 return;
mbed_official 0:f34b7234a060 600 }
mbed_official 0:f34b7234a060 601
mbed_official 0:f34b7234a060 602 output_size = sizeof(input_data) + tag_length;
mbed_official 0:f34b7234a060 603 output_data = (uint8_t *)malloc(output_size);
mbed_official 0:f34b7234a060 604 if (!output_data) {
mbed_official 0:f34b7234a060 605 printf("Out of memory\n");
mbed_official 0:f34b7234a060 606 return;
mbed_official 0:f34b7234a060 607 }
mbed_official 0:f34b7234a060 608
mbed_official 0:f34b7234a060 609 /* Import a key */
mbed_official 0:f34b7234a060 610 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
mbed_official 0:f34b7234a060 611 psa_set_key_algorithm(&attributes, PSA_ALG_CCM);
mbed_official 0:f34b7234a060 612 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
mbed_official 0:f34b7234a060 613 psa_set_key_bits(&attributes, 128);
mbed_official 0:f34b7234a060 614 status = psa_import_key(&attributes, key, sizeof(key), &handle);
mbed_official 0:f34b7234a060 615 psa_reset_key_attributes(&attributes);
mbed_official 0:f34b7234a060 616
mbed_official 0:f34b7234a060 617 /* Authenticate and encrypt */
mbed_official 0:f34b7234a060 618 status = psa_aead_encrypt(handle, PSA_ALG_CCM,
mbed_official 0:f34b7234a060 619 nonce, sizeof(nonce),
mbed_official 0:f34b7234a060 620 additional_data, sizeof(additional_data),
mbed_official 0:f34b7234a060 621 input_data, sizeof(input_data),
mbed_official 0:f34b7234a060 622 output_data, output_size,
mbed_official 0:f34b7234a060 623 &output_length);
mbed_official 0:f34b7234a060 624 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 625 printf("Failed to authenticate and encrypt\n");
mbed_official 0:f34b7234a060 626 return;
mbed_official 0:f34b7234a060 627 }
mbed_official 0:f34b7234a060 628
mbed_official 0:f34b7234a060 629 printf("Authenticated and encrypted\n");
mbed_official 0:f34b7234a060 630
mbed_official 0:f34b7234a060 631 /* Clean up */
mbed_official 0:f34b7234a060 632 free(output_data);
mbed_official 0:f34b7234a060 633
mbed_official 0:f34b7234a060 634 /* Destroy the key */
mbed_official 0:f34b7234a060 635 psa_destroy_key(handle);
mbed_official 0:f34b7234a060 636
mbed_official 0:f34b7234a060 637 mbedtls_psa_crypto_free();
mbed_official 0:f34b7234a060 638 }
mbed_official 0:f34b7234a060 639
mbed_official 0:f34b7234a060 640 static void authenticate_and_decrypt_a_message(void)
mbed_official 0:f34b7234a060 641 {
mbed_official 0:f34b7234a060 642 psa_status_t status;
mbed_official 0:f34b7234a060 643 static const uint8_t key[] = {
mbed_official 0:f34b7234a060 644 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
mbed_official 0:f34b7234a060 645 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF };
mbed_official 0:f34b7234a060 646 static const uint8_t nonce[] = {
mbed_official 0:f34b7234a060 647 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
mbed_official 0:f34b7234a060 648 0x08, 0x09, 0x0A, 0x0B };
mbed_official 0:f34b7234a060 649 static const uint8_t additional_data[] = {
mbed_official 0:f34b7234a060 650 0xEC, 0x46, 0xBB, 0x63, 0xB0, 0x25,
mbed_official 0:f34b7234a060 651 0x20, 0xC3, 0x3C, 0x49, 0xFD, 0x70 };
mbed_official 0:f34b7234a060 652 static const uint8_t input_data[] = {
mbed_official 0:f34b7234a060 653 0x20, 0x30, 0xE0, 0x36, 0xED, 0x09, 0xA0, 0x45, 0xAF, 0x3C, 0xBA, 0xEE,
mbed_official 0:f34b7234a060 654 0x0F, 0xC8, 0x48, 0xAF, 0xCD, 0x89, 0x54, 0xF4, 0xF6, 0x3F, 0x28, 0x9A,
mbed_official 0:f34b7234a060 655 0xA1, 0xDD, 0xB2, 0xB8, 0x09, 0xCD, 0x7C, 0xE1, 0x46, 0xE9, 0x98 };
mbed_official 0:f34b7234a060 656 uint8_t *output_data = NULL;
mbed_official 0:f34b7234a060 657 size_t output_size = 0;
mbed_official 0:f34b7234a060 658 size_t output_length = 0;
mbed_official 0:f34b7234a060 659 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mbed_official 0:f34b7234a060 660 psa_key_handle_t handle;
mbed_official 0:f34b7234a060 661
mbed_official 0:f34b7234a060 662 printf("Authenticate decrypt...\t");
mbed_official 0:f34b7234a060 663 fflush(stdout);
mbed_official 0:f34b7234a060 664
mbed_official 0:f34b7234a060 665 /* Initialize PSA Crypto */
mbed_official 0:f34b7234a060 666 status = psa_crypto_init();
mbed_official 0:f34b7234a060 667 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 668 printf("Failed to initialize PSA Crypto\n");
mbed_official 0:f34b7234a060 669 return;
mbed_official 0:f34b7234a060 670 }
mbed_official 0:f34b7234a060 671
mbed_official 0:f34b7234a060 672 output_size = sizeof(input_data);
mbed_official 0:f34b7234a060 673 output_data = (uint8_t *)malloc(output_size);
mbed_official 0:f34b7234a060 674 if (!output_data) {
mbed_official 0:f34b7234a060 675 printf("Out of memory\n");
mbed_official 0:f34b7234a060 676 return;
mbed_official 0:f34b7234a060 677 }
mbed_official 0:f34b7234a060 678
mbed_official 0:f34b7234a060 679 /* Import a key */
mbed_official 0:f34b7234a060 680 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
mbed_official 0:f34b7234a060 681 psa_set_key_algorithm(&attributes, PSA_ALG_CCM);
mbed_official 0:f34b7234a060 682 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
mbed_official 0:f34b7234a060 683 psa_set_key_bits(&attributes, 128);
mbed_official 0:f34b7234a060 684 status = psa_import_key(&attributes, key, sizeof(key), &handle);
mbed_official 0:f34b7234a060 685 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 686 printf("Failed to import a key\n");
mbed_official 0:f34b7234a060 687 return;
mbed_official 0:f34b7234a060 688 }
mbed_official 0:f34b7234a060 689 psa_reset_key_attributes(&attributes);
mbed_official 0:f34b7234a060 690
mbed_official 0:f34b7234a060 691 /* Authenticate and decrypt */
mbed_official 0:f34b7234a060 692 status = psa_aead_decrypt(handle, PSA_ALG_CCM,
mbed_official 0:f34b7234a060 693 nonce, sizeof(nonce),
mbed_official 0:f34b7234a060 694 additional_data, sizeof(additional_data),
mbed_official 0:f34b7234a060 695 input_data, sizeof(input_data),
mbed_official 0:f34b7234a060 696 output_data, output_size,
mbed_official 0:f34b7234a060 697 &output_length);
mbed_official 0:f34b7234a060 698 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 699 printf("Failed to authenticate and decrypt\n");
mbed_official 0:f34b7234a060 700 return;
mbed_official 0:f34b7234a060 701 }
mbed_official 0:f34b7234a060 702
mbed_official 0:f34b7234a060 703 printf("Authenticated and decrypted\n");
mbed_official 0:f34b7234a060 704
mbed_official 0:f34b7234a060 705 /* Clean up */
mbed_official 0:f34b7234a060 706 free(output_data);
mbed_official 0:f34b7234a060 707
mbed_official 0:f34b7234a060 708 /* Destroy the key */
mbed_official 0:f34b7234a060 709 psa_destroy_key(handle);
mbed_official 0:f34b7234a060 710
mbed_official 0:f34b7234a060 711 mbedtls_psa_crypto_free();
mbed_official 0:f34b7234a060 712 }
mbed_official 0:f34b7234a060 713
mbed_official 0:f34b7234a060 714 static void generate_and_export_a_public_key()
mbed_official 0:f34b7234a060 715 {
mbed_official 0:f34b7234a060 716 enum {
mbed_official 0:f34b7234a060 717 key_bits = 256,
mbed_official 0:f34b7234a060 718 };
mbed_official 0:f34b7234a060 719 psa_status_t status;
mbed_official 0:f34b7234a060 720 size_t exported_length = 0;
mbed_official 0:f34b7234a060 721 static uint8_t exported[PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits)];
mbed_official 0:f34b7234a060 722 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mbed_official 0:f34b7234a060 723 psa_key_handle_t handle;
mbed_official 0:f34b7234a060 724
mbed_official 0:f34b7234a060 725 printf("Generate a key pair...\t");
mbed_official 0:f34b7234a060 726 fflush(stdout);
mbed_official 0:f34b7234a060 727
mbed_official 0:f34b7234a060 728 /* Initialize PSA Crypto */
mbed_official 0:f34b7234a060 729 status = psa_crypto_init();
mbed_official 0:f34b7234a060 730 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 731 printf("Failed to initialize PSA Crypto\n");
mbed_official 0:f34b7234a060 732 return;
mbed_official 0:f34b7234a060 733 }
mbed_official 0:f34b7234a060 734
mbed_official 0:f34b7234a060 735 /* Generate a key */
mbed_official 0:f34b7234a060 736 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN);
mbed_official 0:f34b7234a060 737 psa_set_key_algorithm(&attributes,
mbed_official 0:f34b7234a060 738 PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_SHA_256));
mbed_official 0:f34b7234a060 739 psa_set_key_type(&attributes,
mbed_official 0:f34b7234a060 740 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_CURVE_SECP256R1));
mbed_official 0:f34b7234a060 741 psa_set_key_bits(&attributes, key_bits);
mbed_official 0:f34b7234a060 742 status = psa_generate_key(&attributes, &handle);
mbed_official 0:f34b7234a060 743 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 744 printf("Failed to generate key\n");
mbed_official 0:f34b7234a060 745 return;
mbed_official 0:f34b7234a060 746 }
mbed_official 0:f34b7234a060 747 psa_reset_key_attributes(&attributes);
mbed_official 0:f34b7234a060 748
mbed_official 0:f34b7234a060 749 status = psa_export_public_key(handle, exported, sizeof(exported),
mbed_official 0:f34b7234a060 750 &exported_length);
mbed_official 0:f34b7234a060 751 if (status != PSA_SUCCESS) {
mbed_official 0:f34b7234a060 752 printf("Failed to export public key %ld\n", status);
mbed_official 0:f34b7234a060 753 return;
mbed_official 0:f34b7234a060 754 }
mbed_official 0:f34b7234a060 755
mbed_official 0:f34b7234a060 756 printf("Exported a public key\n");
mbed_official 0:f34b7234a060 757
mbed_official 0:f34b7234a060 758 /* Destroy the key */
mbed_official 0:f34b7234a060 759 psa_destroy_key(handle);
mbed_official 0:f34b7234a060 760
mbed_official 0:f34b7234a060 761 mbedtls_psa_crypto_free();
mbed_official 0:f34b7234a060 762 }
mbed_official 0:f34b7234a060 763
mbed_official 0:f34b7234a060 764 int main(void)
mbed_official 0:f34b7234a060 765 {
mbed_official 0:f34b7234a060 766 printf("-- Begin Mbed Crypto Getting Started --\n\n");
mbed_official 0:f34b7234a060 767
mbed_official 1:59536a7f0430 768 import_a_key(AES_KEY, sizeof(AES_KEY));
mbed_official 1:59536a7f0430 769 sign_a_message_using_rsa(RSA_KEY, sizeof(RSA_KEY));
mbed_official 1:59536a7f0430 770 encrypt_with_symmetric_ciphers(AES_KEY, sizeof(AES_KEY));
mbed_official 1:59536a7f0430 771 decrypt_with_symmetric_ciphers(AES_KEY, sizeof(AES_KEY));
mbed_official 0:f34b7234a060 772 hash_a_message();
mbed_official 0:f34b7234a060 773 verify_a_hash();
mbed_official 0:f34b7234a060 774 generate_a_random_value();
mbed_official 0:f34b7234a060 775 derive_a_new_key_from_an_existing_key();
mbed_official 0:f34b7234a060 776 authenticate_and_encrypt_a_message();
mbed_official 0:f34b7234a060 777 authenticate_and_decrypt_a_message();
mbed_official 0:f34b7234a060 778 generate_and_export_a_public_key();
mbed_official 0:f34b7234a060 779
mbed_official 0:f34b7234a060 780 printf("\n-- End Mbed Crypto Getting Started --\n");
mbed_official 0:f34b7234a060 781
mbed_official 0:f34b7234a060 782 return 0;
mbed_official 0:f34b7234a060 783 }
mbed_official 0:f34b7234a060 784 #endif /* MBEDTLS_PSA_CRYPTO_C */