1 /** @file 2 * @brief Bluetooth subsystem crypto APIs. 3 */ 4 5 /* 6 * Copyright (c) 2017 Nordic Semiconductor ASA 7 * Copyright (c) 2015-2017 Intel Corporation 8 * 9 * SPDX-License-Identifier: Apache-2.0 10 */ 11 #ifndef ZEPHYR_INCLUDE_BLUETOOTH_CRYPTO_H_ 12 #define ZEPHYR_INCLUDE_BLUETOOTH_CRYPTO_H_ 13 14 /** 15 * @brief Cryptography 16 * @defgroup bt_crypto Cryptography 17 * @ingroup bluetooth 18 * @{ 19 */ 20 21 #include <stdbool.h> 22 23 #ifdef __cplusplus 24 extern "C" { 25 #endif 26 27 /** @brief Generate random data. 28 * 29 * A random number generation helper which utilizes the Bluetooth 30 * controller's own RNG. 31 * 32 * @param buf Buffer to insert the random data 33 * @param len Length of random data to generate 34 * 35 * @return Zero on success or error code otherwise, positive in case 36 * of protocol error or negative (POSIX) in case of stack internal error 37 */ 38 int bt_rand(void *buf, size_t len); 39 40 /** @brief AES encrypt little-endian data. 41 * 42 * An AES encrypt helper is used to request the Bluetooth controller's own 43 * hardware to encrypt the plaintext using the key and returns the encrypted 44 * data. 45 * 46 * @param key 128 bit LS byte first key for the encryption of the plaintext 47 * @param plaintext 128 bit LS byte first plaintext data block to be encrypted 48 * @param enc_data 128 bit LS byte first encrypted data block 49 * 50 * @return Zero on success or error code otherwise. 51 */ 52 int bt_encrypt_le(const u8_t key[16], const u8_t plaintext[16], 53 u8_t enc_data[16]); 54 55 /** @brief AES encrypt big-endian data. 56 * 57 * An AES encrypt helper is used to request the Bluetooth controller's own 58 * hardware to encrypt the plaintext using the key and returns the encrypted 59 * data. 60 * 61 * @param key 128 bit MS byte first key for the encryption of the plaintext 62 * @param plaintext 128 bit MS byte first plaintext data block to be encrypted 63 * @param enc_data 128 bit MS byte first encrypted data block 64 * 65 * @return Zero on success or error code otherwise. 66 */ 67 int bt_encrypt_be(const u8_t key[16], const u8_t plaintext[16], 68 u8_t enc_data[16]); 69 70 int bt_decrypt_be(const u8_t key[16], const u8_t plaintext[16], 71 u8_t enc_data[16]); 72 73 /** @brief Decrypt big-endian data with AES-CCM. 74 * 75 * Decrypts and authorizes @c enc_data with AES-CCM, as described in 76 * https://tools.ietf.org/html/rfc3610. 77 * 78 * Assumes that the MIC follows directly after the encrypted data. 79 * 80 * @param key 128 bit MS byte first key 81 * @param nonce 13 byte MS byte first nonce 82 * @param enc_data Encrypted data 83 * @param len Length of the encrypted data 84 * @param aad Additional input data 85 * @param aad_len Additional input data length 86 * @param plaintext Plaintext buffer to place result in 87 * @param mic_size Size of the trailing MIC (in bytes) 88 * 89 * @retval 0 Successfully decrypted the data. 90 * @retval -EINVAL Invalid parameters. 91 * @retval -EBADMSG Authentication failed. 92 */ 93 int bt_ccm_decrypt(const u8_t key[16], u8_t nonce[13], const u8_t *enc_data, 94 size_t len, const u8_t *aad, size_t aad_len, 95 u8_t *plaintext, size_t mic_size); 96 97 98 /** @brief Encrypt big-endian data with AES-CCM. 99 * 100 * Encrypts and generates a MIC from @c plaintext with AES-CCM, as described in 101 * https://tools.ietf.org/html/rfc3610. 102 * 103 * Places the MIC directly after the encrypted data. 104 * 105 * @param key 128 bit MS byte first key 106 * @param nonce 13 byte MS byte first nonce 107 * @param enc_data Buffer to place encrypted data in 108 * @param len Length of the encrypted data 109 * @param aad Additional input data 110 * @param aad_len Additional input data length 111 * @param plaintext Plaintext buffer to encrypt 112 * @param mic_size Size of the trailing MIC (in bytes) 113 * 114 * @retval 0 Successfully encrypted the data. 115 * @retval -EINVAL Invalid parameters. 116 */ 117 int bt_ccm_encrypt(const u8_t key[16], u8_t nonce[13], const u8_t *enc_data, 118 size_t len, const u8_t *aad, size_t aad_len, 119 u8_t *plaintext, size_t mic_size); 120 121 #ifdef __cplusplus 122 } 123 #endif 124 /** 125 * @} 126 */ 127 128 #endif /* ZEPHYR_INCLUDE_BLUETOOTH_CRYPTO_H_ */ 129