Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Hello world example of using the authenticated encryption with mbed TLS |
| 3 | * |
| 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame^] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 6 | * |
| 7 | * This file is provided under the Apache License 2.0, or the |
| 8 | * GNU General Public License v2.0 or later. |
| 9 | * |
| 10 | * ********** |
| 11 | * Apache License 2.0: |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 12 | * |
| 13 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 14 | * not use this file except in compliance with the License. |
| 15 | * You may obtain a copy of the License at |
| 16 | * |
| 17 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | * |
| 19 | * Unless required by applicable law or agreed to in writing, software |
| 20 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 21 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | * See the License for the specific language governing permissions and |
| 23 | * limitations under the License. |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 24 | * |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame^] | 25 | * ********** |
| 26 | * |
| 27 | * ********** |
| 28 | * GNU General Public License v2.0 or later: |
| 29 | * |
| 30 | * This program is free software; you can redistribute it and/or modify |
| 31 | * it under the terms of the GNU General Public License as published by |
| 32 | * the Free Software Foundation; either version 2 of the License, or |
| 33 | * (at your option) any later version. |
| 34 | * |
| 35 | * This program is distributed in the hope that it will be useful, |
| 36 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 37 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 38 | * GNU General Public License for more details. |
| 39 | * |
| 40 | * You should have received a copy of the GNU General Public License along |
| 41 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 42 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 43 | * |
| 44 | * ********** |
| 45 | * |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 46 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 47 | */ |
| 48 | |
| 49 | #include "mbedtls/cipher.h" |
| 50 | #include "mbedtls/entropy.h" |
| 51 | #include "mbedtls/ctr_drbg.h" |
| 52 | |
| 53 | #include <stdio.h> |
| 54 | #include <string.h> |
| 55 | |
| 56 | static void print_hex(const char *title, const unsigned char buf[], size_t len) |
| 57 | { |
| 58 | printf("%s: ", title); |
| 59 | |
| 60 | for (size_t i = 0; i < len; i++) |
| 61 | printf("%02x", buf[i]); |
| 62 | |
| 63 | printf("\r\n"); |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * The pre-shared key. Should be generated randomly and be unique to the |
| 68 | * device/channel/etc. Just used a fixed on here for simplicity. |
| 69 | */ |
| 70 | static const unsigned char secret_key[16] = { |
| 71 | 0xf4, 0x82, 0xc6, 0x70, 0x3c, 0xc7, 0x61, 0x0a, |
| 72 | 0xb9, 0xa0, 0xb8, 0xe9, 0x87, 0xb8, 0xc1, 0x72, |
| 73 | }; |
| 74 | |
| 75 | static int example(void) |
| 76 | { |
| 77 | /* message that should be protected */ |
| 78 | const char message[] = "Some things are better left unread"; |
| 79 | /* metadata transmitted in the clear but authenticated */ |
| 80 | const char metadata[] = "eg sequence number, routing info"; |
| 81 | /* ciphertext buffer large enough to hold message + nonce + tag */ |
| 82 | unsigned char ciphertext[128] = { 0 }; |
| 83 | int ret; |
| 84 | |
| 85 | printf("\r\n\r\n"); |
| 86 | print_hex("plaintext message", (unsigned char *) message, sizeof message); |
| 87 | |
| 88 | /* |
| 89 | * Setup random number generator |
| 90 | * (Note: later this might be done automatically.) |
| 91 | */ |
| 92 | mbedtls_entropy_context entropy; /* entropy pool for seeding PRNG */ |
| 93 | mbedtls_ctr_drbg_context drbg; /* pseudo-random generator */ |
| 94 | |
| 95 | mbedtls_entropy_init(&entropy); |
| 96 | mbedtls_ctr_drbg_init(&drbg); |
| 97 | |
| 98 | /* Seed the PRNG using the entropy pool, and throw in our secret key as an |
| 99 | * additional source of randomness. */ |
| 100 | ret = mbedtls_ctr_drbg_seed(&drbg, mbedtls_entropy_func, &entropy, |
| 101 | secret_key, sizeof (secret_key)); |
| 102 | if (ret != 0) { |
| 103 | printf("mbedtls_ctr_drbg_init() returned -0x%04X\r\n", -ret); |
| 104 | return 1; |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Setup AES-CCM contex |
| 109 | */ |
| 110 | mbedtls_cipher_context_t ctx; |
| 111 | |
| 112 | mbedtls_cipher_init(&ctx); |
| 113 | |
| 114 | ret = mbedtls_cipher_setup(&ctx, mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_CCM)); |
| 115 | if (ret != 0) { |
| 116 | printf("mbedtls_cipher_setup() returned -0x%04X\r\n", -ret); |
| 117 | return 1; |
| 118 | } |
| 119 | |
| 120 | ret = mbedtls_cipher_setkey(&ctx, secret_key, 8 * sizeof secret_key, MBEDTLS_ENCRYPT); |
| 121 | if (ret != 0) { |
| 122 | printf("mbedtls_cipher_setkey() returned -0x%04X\r\n", -ret); |
| 123 | return 1; |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * Encrypt-authenticate the message and authenticate additional data |
| 128 | * |
| 129 | * First generate a random 8-byte nonce. |
| 130 | * Put it directly in the output buffer as the recipient will need it. |
| 131 | * |
| 132 | * Warning: you must never re-use the same (key, nonce) pair. One of the |
| 133 | * best ways to ensure this to use a counter for the nonce. However this |
| 134 | * means you should save the counter accross rebots, if the key is a |
| 135 | * long-term one. The alternative we choose here is to generate the nonce |
| 136 | * randomly. However it only works if you have a good source of |
| 137 | * randomness. |
| 138 | */ |
| 139 | const size_t nonce_len = 8; |
| 140 | mbedtls_ctr_drbg_random(&drbg, ciphertext, nonce_len); |
| 141 | |
| 142 | size_t ciphertext_len = 0; |
| 143 | /* Go for a conservative 16-byte (128-bit) tag |
| 144 | * and append it to the ciphertext */ |
| 145 | const size_t tag_len = 16; |
| 146 | ret = mbedtls_cipher_auth_encrypt(&ctx, ciphertext, nonce_len, |
| 147 | (const unsigned char *) metadata, sizeof metadata, |
| 148 | (const unsigned char *) message, sizeof message, |
| 149 | ciphertext + nonce_len, &ciphertext_len, |
| 150 | ciphertext + nonce_len + sizeof message, tag_len ); |
| 151 | if (ret != 0) { |
| 152 | printf("mbedtls_cipher_auth_encrypt() returned -0x%04X\r\n", -ret); |
| 153 | return 1; |
| 154 | } |
| 155 | ciphertext_len += nonce_len + tag_len; |
| 156 | |
| 157 | /* |
| 158 | * The following information should now be transmitted: |
| 159 | * - first ciphertext_len bytes of ciphertext buffer |
| 160 | * - metadata if not already transmitted elsewhere |
| 161 | */ |
| 162 | print_hex("ciphertext", ciphertext, ciphertext_len); |
| 163 | |
| 164 | /* |
| 165 | * Decrypt-authenticate |
| 166 | */ |
| 167 | unsigned char decrypted[128] = { 0 }; |
| 168 | size_t decrypted_len = 0; |
| 169 | |
| 170 | ret = mbedtls_cipher_setkey(&ctx, secret_key, 8 * sizeof secret_key, MBEDTLS_DECRYPT); |
| 171 | if (ret != 0) { |
| 172 | printf("mbedtls_cipher_setkey() returned -0x%04X\r\n", -ret); |
| 173 | return 1; |
| 174 | } |
| 175 | |
| 176 | ret = mbedtls_cipher_auth_decrypt(&ctx, |
| 177 | ciphertext, nonce_len, |
| 178 | (const unsigned char *) metadata, sizeof metadata, |
| 179 | ciphertext + nonce_len, ciphertext_len - nonce_len - tag_len, |
| 180 | decrypted, &decrypted_len, |
| 181 | ciphertext + ciphertext_len - tag_len, tag_len ); |
| 182 | /* Checking the return code is CRITICAL for security here */ |
| 183 | if (ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED) { |
| 184 | printf("Something bad is happening! Data is not authentic!\r\n"); |
| 185 | return 1; |
| 186 | } |
| 187 | if (ret != 0) { |
| 188 | printf("mbedtls_cipher_authdecrypt() returned -0x%04X\r\n", -ret); |
| 189 | return 1; |
| 190 | } |
| 191 | |
| 192 | print_hex("decrypted", decrypted, decrypted_len); |
| 193 | |
| 194 | printf("\r\nDONE\r\n"); |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | #if defined(TARGET_LIKE_MBED) |
| 200 | |
Manuel Pégourié-Gonnard | 71956c9 | 2015-10-21 17:59:05 +0200 | [diff] [blame] | 201 | #include "mbed-drivers/test_env.h" |
Manuel Pégourié-Gonnard | e87b04c | 2015-08-11 04:09:44 +0200 | [diff] [blame] | 202 | #include "minar/minar.h" |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 203 | |
Manuel Pégourié-Gonnard | e87b04c | 2015-08-11 04:09:44 +0200 | [diff] [blame] | 204 | static void run() { |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 205 | MBED_HOSTTEST_TIMEOUT(10); |
| 206 | MBED_HOSTTEST_SELECT(default); |
| 207 | MBED_HOSTTEST_DESCRIPTION(mbed TLS example authcrypt); |
| 208 | MBED_HOSTTEST_START("MBEDTLS_EX_AUTHCRYPT"); |
| 209 | MBED_HOSTTEST_RESULT(example() == 0); |
| 210 | } |
| 211 | |
Manuel Pégourié-Gonnard | e87b04c | 2015-08-11 04:09:44 +0200 | [diff] [blame] | 212 | void app_start(int, char*[]) { |
Janos Follath | 60ddf16 | 2016-03-17 13:55:07 +0000 | [diff] [blame] | 213 | /* Use 115200 bps for consistency with other examples */ |
| 214 | get_stdio_serial().baud(115200); |
Manuel Pégourié-Gonnard | ca4fb71 | 2015-09-18 14:36:57 +0200 | [diff] [blame] | 215 | minar::Scheduler::postCallback(mbed::util::FunctionPointer0<void>(run).bind()); |
Manuel Pégourié-Gonnard | e87b04c | 2015-08-11 04:09:44 +0200 | [diff] [blame] | 216 | } |
| 217 | |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 218 | #else |
| 219 | |
| 220 | int main() { |
| 221 | return example(); |
| 222 | } |
| 223 | |
| 224 | #endif |