|
| 1 | +/* |
| 2 | + * Hello world example of using the authenticated encryption with mbed TLS |
| 3 | + * |
| 4 | + * Copyright (C) 2016, ARM Limited, All Rights Reserved |
| 5 | + * SPDX-License-Identifier: Apache-2.0 |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | + * not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "mbed.h" |
| 21 | + |
| 22 | +#include "mbedtls/cipher.h" |
| 23 | +#include "mbedtls/entropy.h" |
| 24 | +#include "mbedtls/ctr_drbg.h" |
| 25 | +#if DEBUG_LEVEL > 0 |
| 26 | +#include "mbedtls/debug.h" |
| 27 | +#endif |
| 28 | + |
| 29 | +#include "mbedtls/platform.h" |
| 30 | + |
| 31 | +#include <string.h> |
| 32 | + |
| 33 | +static void print_hex(const char *title, const unsigned char buf[], size_t len) |
| 34 | +{ |
| 35 | + mbedtls_printf("%s: ", title); |
| 36 | + |
| 37 | + for (size_t i = 0; i < len; i++) |
| 38 | + mbedtls_printf("%02x", buf[i]); |
| 39 | + |
| 40 | + mbedtls_printf("\r\n"); |
| 41 | +} |
| 42 | + |
| 43 | +/* |
| 44 | + * The pre-shared key. Should be generated randomly and be unique to the |
| 45 | + * device/channel/etc. Just used a fixed on here for simplicity. |
| 46 | + */ |
| 47 | +static const unsigned char secret_key[16] = { |
| 48 | + 0xf4, 0x82, 0xc6, 0x70, 0x3c, 0xc7, 0x61, 0x0a, |
| 49 | + 0xb9, 0xa0, 0xb8, 0xe9, 0x87, 0xb8, 0xc1, 0x72, |
| 50 | +}; |
| 51 | + |
| 52 | +static int example(void) |
| 53 | +{ |
| 54 | + /* message that should be protected */ |
| 55 | + const char message[] = "Some things are better left unread"; |
| 56 | + /* metadata transmitted in the clear but authenticated */ |
| 57 | + const char metadata[] = "eg sequence number, routing info"; |
| 58 | + /* ciphertext buffer large enough to hold message + nonce + tag */ |
| 59 | + unsigned char ciphertext[128] = { 0 }; |
| 60 | + int ret; |
| 61 | + |
| 62 | + mbedtls_printf("\r\n\r\n"); |
| 63 | + print_hex("plaintext message", (unsigned char *) message, sizeof message); |
| 64 | + |
| 65 | + /* |
| 66 | + * Setup random number generator |
| 67 | + * (Note: later this might be done automatically.) |
| 68 | + */ |
| 69 | + mbedtls_entropy_context entropy; /* entropy pool for seeding PRNG */ |
| 70 | + mbedtls_ctr_drbg_context drbg; /* pseudo-random generator */ |
| 71 | + |
| 72 | + mbedtls_entropy_init(&entropy); |
| 73 | + mbedtls_ctr_drbg_init(&drbg); |
| 74 | + |
| 75 | + /* Seed the PRNG using the entropy pool, and throw in our secret key as an |
| 76 | + * additional source of randomness. */ |
| 77 | + ret = mbedtls_ctr_drbg_seed(&drbg, mbedtls_entropy_func, &entropy, |
| 78 | + secret_key, sizeof secret_key); |
| 79 | + if (ret != 0) { |
| 80 | + return 1; |
| 81 | + } |
| 82 | + |
| 83 | + /* |
| 84 | + * Setup AES-CCM contex |
| 85 | + */ |
| 86 | + mbedtls_cipher_context_t ctx; |
| 87 | + |
| 88 | + mbedtls_cipher_init(&ctx); |
| 89 | + |
| 90 | + ret = mbedtls_cipher_setup(&ctx, mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_CCM)); |
| 91 | + if (ret != 0) { |
| 92 | + mbedtls_printf("mbedtls_cipher_setup() returned -0x%04X\r\n", -ret); |
| 93 | + return 1; |
| 94 | + } |
| 95 | + |
| 96 | + ret = mbedtls_cipher_setkey(&ctx, secret_key, 8 * sizeof secret_key, MBEDTLS_ENCRYPT); |
| 97 | + if (ret != 0) { |
| 98 | + mbedtls_printf("mbedtls_cipher_setkey() returned -0x%04X\r\n", -ret); |
| 99 | + return 1; |
| 100 | + } |
| 101 | + |
| 102 | + /* |
| 103 | + * Encrypt-authenticate the message and authenticate additional data |
| 104 | + * |
| 105 | + * First generate a random 8-byte nonce. |
| 106 | + * Put it directly in the output buffer as the recipient will need it. |
| 107 | + * |
| 108 | + * Warning: you must never re-use the same (key, nonce) pair. One of the |
| 109 | + * best ways to ensure this to use a counter for the nonce. However this |
| 110 | + * means you should save the counter accross rebots, if the key is a |
| 111 | + * long-term one. The alternative we choose here is to generate the nonce |
| 112 | + * randomly. However it only works if you have a good source of |
| 113 | + * randomness. |
| 114 | + */ |
| 115 | + const size_t nonce_len = 8; |
| 116 | + mbedtls_ctr_drbg_random(&drbg, ciphertext, nonce_len); |
| 117 | + |
| 118 | + size_t ciphertext_len = 0; |
| 119 | + /* Go for a conservative 16-byte (128-bit) tag |
| 120 | + * and append it to the ciphertext */ |
| 121 | + const size_t tag_len = 16; |
| 122 | + ret = mbedtls_cipher_auth_encrypt(&ctx, ciphertext, nonce_len, |
| 123 | + (const unsigned char *) metadata, sizeof metadata, |
| 124 | + (const unsigned char *) message, sizeof message, |
| 125 | + ciphertext + nonce_len, &ciphertext_len, |
| 126 | + ciphertext + nonce_len + sizeof message, tag_len ); |
| 127 | + if (ret != 0) { |
| 128 | + mbedtls_printf("mbedtls_cipher_auth_encrypt() returned -0x%04X\r\n", -ret); |
| 129 | + return 1; |
| 130 | + } |
| 131 | + ciphertext_len += nonce_len + tag_len; |
| 132 | + |
| 133 | + /* |
| 134 | + * The following information should now be transmitted: |
| 135 | + * - first ciphertext_len bytes of ciphertext buffer |
| 136 | + * - metadata if not already transmitted elsewhere |
| 137 | + */ |
| 138 | + print_hex("ciphertext", ciphertext, ciphertext_len); |
| 139 | + |
| 140 | + /* |
| 141 | + * Decrypt-authenticate |
| 142 | + */ |
| 143 | + unsigned char decrypted[128] = { 0 }; |
| 144 | + size_t decrypted_len = 0; |
| 145 | + |
| 146 | + ret = mbedtls_cipher_setkey(&ctx, secret_key, 8 * sizeof secret_key, MBEDTLS_DECRYPT); |
| 147 | + if (ret != 0) { |
| 148 | + mbedtls_printf("mbedtls_cipher_setkey() returned -0x%04X\r\n", -ret); |
| 149 | + return 1; |
| 150 | + } |
| 151 | + |
| 152 | + ret = mbedtls_cipher_auth_decrypt(&ctx, |
| 153 | + ciphertext, nonce_len, |
| 154 | + (const unsigned char *) metadata, sizeof metadata, |
| 155 | + ciphertext + nonce_len, ciphertext_len - nonce_len - tag_len, |
| 156 | + decrypted, &decrypted_len, |
| 157 | + ciphertext + ciphertext_len - tag_len, tag_len ); |
| 158 | + /* Checking the return code is CRITICAL for security here */ |
| 159 | + if (ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED) { |
| 160 | + mbedtls_printf("Something bad is happening! Data is not authentic!\r\n"); |
| 161 | + return 1; |
| 162 | + } |
| 163 | + if (ret != 0) { |
| 164 | + mbedtls_printf("mbedtls_cipher_authdecrypt() returned -0x%04X\r\n", -ret); |
| 165 | + return 1; |
| 166 | + } |
| 167 | + |
| 168 | + print_hex("decrypted", decrypted, decrypted_len); |
| 169 | + |
| 170 | + mbedtls_printf("\r\nDONE\r\n"); |
| 171 | + |
| 172 | + return 0; |
| 173 | +} |
| 174 | + |
| 175 | +int main() { |
| 176 | + int ret = example(); |
| 177 | + if (ret != 0) { |
| 178 | + mbedtls_printf("Example failed with error %d\r\n", ret); |
| 179 | + } |
| 180 | +} |
0 commit comments