mbed library sources. Supersedes mbed-src. Edited target satm32f446 for user USART3 pins
Fork of mbed-dev by
targets/TARGET_Maxim/TARGET_MAX32620C/mxc/aes.h@186:707f6e361f3e, 2018-06-22 (annotated)
- Committer:
- Anna Bridge
- Date:
- Fri Jun 22 16:45:37 2018 +0100
- Revision:
- 186:707f6e361f3e
mbed-dev library. Release version 162
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Anna Bridge |
186:707f6e361f3e | 1 | /** |
Anna Bridge |
186:707f6e361f3e | 2 | * @file |
Anna Bridge |
186:707f6e361f3e | 3 | * @brief Advanced Encryption Standard (AES) function prototypes and data |
Anna Bridge |
186:707f6e361f3e | 4 | * types. |
Anna Bridge |
186:707f6e361f3e | 5 | */ |
Anna Bridge |
186:707f6e361f3e | 6 | /* **************************************************************************** |
Anna Bridge |
186:707f6e361f3e | 7 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. |
Anna Bridge |
186:707f6e361f3e | 8 | * |
Anna Bridge |
186:707f6e361f3e | 9 | * Permission is hereby granted, free of charge, to any person obtaining a |
Anna Bridge |
186:707f6e361f3e | 10 | * copy of this software and associated documentation files (the "Software"), |
Anna Bridge |
186:707f6e361f3e | 11 | * to deal in the Software without restriction, including without limitation |
Anna Bridge |
186:707f6e361f3e | 12 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
Anna Bridge |
186:707f6e361f3e | 13 | * and/or sell copies of the Software, and to permit persons to whom the |
Anna Bridge |
186:707f6e361f3e | 14 | * Software is furnished to do so, subject to the following conditions: |
Anna Bridge |
186:707f6e361f3e | 15 | * |
Anna Bridge |
186:707f6e361f3e | 16 | * The above copyright notice and this permission notice shall be included |
Anna Bridge |
186:707f6e361f3e | 17 | * in all copies or substantial portions of the Software. |
Anna Bridge |
186:707f6e361f3e | 18 | * |
Anna Bridge |
186:707f6e361f3e | 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
Anna Bridge |
186:707f6e361f3e | 20 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
Anna Bridge |
186:707f6e361f3e | 21 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
Anna Bridge |
186:707f6e361f3e | 22 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
Anna Bridge |
186:707f6e361f3e | 23 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
Anna Bridge |
186:707f6e361f3e | 24 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
Anna Bridge |
186:707f6e361f3e | 25 | * OTHER DEALINGS IN THE SOFTWARE. |
Anna Bridge |
186:707f6e361f3e | 26 | * |
Anna Bridge |
186:707f6e361f3e | 27 | * Except as contained in this notice, the name of Maxim Integrated |
Anna Bridge |
186:707f6e361f3e | 28 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
Anna Bridge |
186:707f6e361f3e | 29 | * Products, Inc. Branding Policy. |
Anna Bridge |
186:707f6e361f3e | 30 | * |
Anna Bridge |
186:707f6e361f3e | 31 | * The mere transfer of this software does not imply any licenses |
Anna Bridge |
186:707f6e361f3e | 32 | * of trade secrets, proprietary technology, copyrights, patents, |
Anna Bridge |
186:707f6e361f3e | 33 | * trademarks, maskwork rights, or any other form of intellectual |
Anna Bridge |
186:707f6e361f3e | 34 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
Anna Bridge |
186:707f6e361f3e | 35 | * ownership rights. |
Anna Bridge |
186:707f6e361f3e | 36 | * |
Anna Bridge |
186:707f6e361f3e | 37 | * $Date: 2016-10-10 16:51:05 -0500 (Mon, 10 Oct 2016) $ |
Anna Bridge |
186:707f6e361f3e | 38 | * $Revision: 24655 $ |
Anna Bridge |
186:707f6e361f3e | 39 | * |
Anna Bridge |
186:707f6e361f3e | 40 | *************************************************************************** */ |
Anna Bridge |
186:707f6e361f3e | 41 | |
Anna Bridge |
186:707f6e361f3e | 42 | /* Define to prevent redundant inclusion */ |
Anna Bridge |
186:707f6e361f3e | 43 | #ifndef _AES_H |
Anna Bridge |
186:707f6e361f3e | 44 | #define _AES_H |
Anna Bridge |
186:707f6e361f3e | 45 | /* **** Includes **** */ |
Anna Bridge |
186:707f6e361f3e | 46 | #include <stdint.h> |
Anna Bridge |
186:707f6e361f3e | 47 | #include "aes_regs.h" |
Anna Bridge |
186:707f6e361f3e | 48 | |
Anna Bridge |
186:707f6e361f3e | 49 | #ifdef __cplusplus |
Anna Bridge |
186:707f6e361f3e | 50 | extern "C" { |
Anna Bridge |
186:707f6e361f3e | 51 | #endif |
Anna Bridge |
186:707f6e361f3e | 52 | |
Anna Bridge |
186:707f6e361f3e | 53 | /** |
Anna Bridge |
186:707f6e361f3e | 54 | * @ingroup periphlibs |
Anna Bridge |
186:707f6e361f3e | 55 | * @defgroup aes Advanced Encryption Standard (AES) |
Anna Bridge |
186:707f6e361f3e | 56 | * @brief High-level API for AES encryption engine |
Anna Bridge |
186:707f6e361f3e | 57 | */ |
Anna Bridge |
186:707f6e361f3e | 58 | |
Anna Bridge |
186:707f6e361f3e | 59 | /** |
Anna Bridge |
186:707f6e361f3e | 60 | * @ingroup aes |
Anna Bridge |
186:707f6e361f3e | 61 | * @defgroup aes_overview Overview and Usage |
Anna Bridge |
186:707f6e361f3e | 62 | * @brief Advanced Encryption Standard API public include file. |
Anna Bridge |
186:707f6e361f3e | 63 | * @details |
Anna Bridge |
186:707f6e361f3e | 64 | * <b>Key/data format in memory</b> |
Anna Bridge |
186:707f6e361f3e | 65 | * The API functions require that key and plain/ciphertext will be stored as a |
Anna Bridge |
186:707f6e361f3e | 66 | * byte array in LSB .. MSB format. |
Anna Bridge |
186:707f6e361f3e | 67 | * @par |
Anna Bridge |
186:707f6e361f3e | 68 | * As an example, given the key @a 0x139A35422F1D61DE3C91787FE0507AFD, the proper storage order is: |
Anna Bridge |
186:707f6e361f3e | 69 | * ~~~~~ |
Anna Bridge |
186:707f6e361f3e | 70 | * uint8_t key[16] = { 0xFD, 0x7A, 0x50, 0xE0, |
Anna Bridge |
186:707f6e361f3e | 71 | * 0x7F, 0x78, 0x91, 0x3C, |
Anna Bridge |
186:707f6e361f3e | 72 | * 0xDE, 0x61, 0x1D, 0x2F, |
Anna Bridge |
186:707f6e361f3e | 73 | * 0x42, 0x35, 0x9A, 0x13 }; |
Anna Bridge |
186:707f6e361f3e | 74 | * ~~~~~ |
Anna Bridge |
186:707f6e361f3e | 75 | * This is the same order expected by the underlying hardware. |
Anna Bridge |
186:707f6e361f3e | 76 | */ |
Anna Bridge |
186:707f6e361f3e | 77 | |
Anna Bridge |
186:707f6e361f3e | 78 | /* **** Definitions **** */ |
Anna Bridge |
186:707f6e361f3e | 79 | /** |
Anna Bridge |
186:707f6e361f3e | 80 | * @ingroup aes |
Anna Bridge |
186:707f6e361f3e | 81 | * @{ |
Anna Bridge |
186:707f6e361f3e | 82 | */ |
Anna Bridge |
186:707f6e361f3e | 83 | #define MXC_AES_DATA_LEN (128 / 8) /**< Number of bytes in an AES plaintext or cyphertext block, which are always 128-bits long. */ |
Anna Bridge |
186:707f6e361f3e | 84 | #define MXC_AES_KEY_128_LEN (128 / 8) /**< Number of bytes in a AES-128 key. */ |
Anna Bridge |
186:707f6e361f3e | 85 | #define MXC_AES_KEY_192_LEN (192 / 8) /**< Number of bytes in a AES-192 key. */ |
Anna Bridge |
186:707f6e361f3e | 86 | #define MXC_AES_KEY_256_LEN (256 / 8) /**< Number of bytes in a AES-256 key. */ |
Anna Bridge |
186:707f6e361f3e | 87 | |
Anna Bridge |
186:707f6e361f3e | 88 | /** |
Anna Bridge |
186:707f6e361f3e | 89 | * Enumeration type for AES key size selection (bits). |
Anna Bridge |
186:707f6e361f3e | 90 | */ |
Anna Bridge |
186:707f6e361f3e | 91 | typedef enum { |
Anna Bridge |
186:707f6e361f3e | 92 | MXC_E_AES_MODE_128 = MXC_V_AES_CTRL_KEY_SIZE_128, /**< 128-bit key. */ |
Anna Bridge |
186:707f6e361f3e | 93 | MXC_E_AES_MODE_192 = MXC_V_AES_CTRL_KEY_SIZE_192, /**< 192-bit key. */ |
Anna Bridge |
186:707f6e361f3e | 94 | MXC_E_AES_MODE_256 = MXC_V_AES_CTRL_KEY_SIZE_256 /**< 256-bit key. */ |
Anna Bridge |
186:707f6e361f3e | 95 | } mxc_aes_mode_t; |
Anna Bridge |
186:707f6e361f3e | 96 | |
Anna Bridge |
186:707f6e361f3e | 97 | /** |
Anna Bridge |
186:707f6e361f3e | 98 | * Enumeration type for specifying encryption/decrytion and asynchronous or blocking behavior. |
Anna Bridge |
186:707f6e361f3e | 99 | */ |
Anna Bridge |
186:707f6e361f3e | 100 | typedef enum { |
Anna Bridge |
186:707f6e361f3e | 101 | MXC_E_AES_ENCRYPT = 0, /**< Encrypt (synchronous/blocking). */ |
Anna Bridge |
186:707f6e361f3e | 102 | MXC_E_AES_ENCRYPT_ASYNC = 1, /**< Encrypt (aynchronous/interrupt-driven). */ |
Anna Bridge |
186:707f6e361f3e | 103 | MXC_E_AES_DECRYPT = 2, /**< Decrypt (synchronous/blocking). */ |
Anna Bridge |
186:707f6e361f3e | 104 | MXC_E_AES_DECRYPT_ASYNC = 3 /**< Decrypt (aynchronous/interrupt-driven). */ |
Anna Bridge |
186:707f6e361f3e | 105 | } mxc_aes_dir_t; |
Anna Bridge |
186:707f6e361f3e | 106 | |
Anna Bridge |
186:707f6e361f3e | 107 | /* **** Function Prototypes **** */ |
Anna Bridge |
186:707f6e361f3e | 108 | |
Anna Bridge |
186:707f6e361f3e | 109 | /** |
Anna Bridge |
186:707f6e361f3e | 110 | * @brief Configure AES block with keying material |
Anna Bridge |
186:707f6e361f3e | 111 | * |
Anna Bridge |
186:707f6e361f3e | 112 | * @param key 128, 192, or 256 bit keying material |
Anna Bridge |
186:707f6e361f3e | 113 | * @param mode The key length, see #mxc_aes_mode_t for supported lengths. |
Anna Bridge |
186:707f6e361f3e | 114 | * |
Anna Bridge |
186:707f6e361f3e | 115 | * @return #E_BAD_PARAM Specified @a mode is invalid, see #mxc_aes_mode_t. |
Anna Bridge |
186:707f6e361f3e | 116 | * @return #E_NULL_PTR Invalid/Null pointer for parameter @a key. |
Anna Bridge |
186:707f6e361f3e | 117 | * @return #E_SUCCESS Key and mode set up correctly. |
Anna Bridge |
186:707f6e361f3e | 118 | */ |
Anna Bridge |
186:707f6e361f3e | 119 | int AES_SetKey(const uint8_t *key, mxc_aes_mode_t mode); |
Anna Bridge |
186:707f6e361f3e | 120 | |
Anna Bridge |
186:707f6e361f3e | 121 | |
Anna Bridge |
186:707f6e361f3e | 122 | /** |
Anna Bridge |
186:707f6e361f3e | 123 | * @brief Encrypt/decrypt an input block with the loaded AES key. |
Anna Bridge |
186:707f6e361f3e | 124 | * @note The parameters @a in and @a out must be 16 bytes. |
Anna Bridge |
186:707f6e361f3e | 125 | * |
Anna Bridge |
186:707f6e361f3e | 126 | * @param in Pointer to input array of 16 bytes. |
Anna Bridge |
186:707f6e361f3e | 127 | * @param out Pointer to output array of 16 bytes. |
Anna Bridge |
186:707f6e361f3e | 128 | * @param mode AES key size to use for the transaction, see #mxc_aes_mode_t for supported key sizes. |
Anna Bridge |
186:707f6e361f3e | 129 | * @param dir Operation to perform, see #mxc_aes_dir_t for supported operations. |
Anna Bridge |
186:707f6e361f3e | 130 | * |
Anna Bridge |
186:707f6e361f3e | 131 | * @return #E_SUCCESS Operation completed successfully, output data is stored in @a *out. |
Anna Bridge |
186:707f6e361f3e | 132 | * @return ErrorCode An @ref MXC_Error_Codes "Error Code" if an error occured. |
Anna Bridge |
186:707f6e361f3e | 133 | */ |
Anna Bridge |
186:707f6e361f3e | 134 | int AES_ECBOp(const uint8_t *in, uint8_t *out, mxc_aes_mode_t mode, mxc_aes_dir_t dir); |
Anna Bridge |
186:707f6e361f3e | 135 | |
Anna Bridge |
186:707f6e361f3e | 136 | /** |
Anna Bridge |
186:707f6e361f3e | 137 | * @brief Read the AES output memory, used for asynchronous encryption, and |
Anna Bridge |
186:707f6e361f3e | 138 | * clears interrupt flag. |
Anna Bridge |
186:707f6e361f3e | 139 | * @note The parameter @a out must always be 16 bytes. |
Anna Bridge |
186:707f6e361f3e | 140 | * |
Anna Bridge |
186:707f6e361f3e | 141 | * @param out Pointer to a 16-byte array to store the output from the AES operation. |
Anna Bridge |
186:707f6e361f3e | 142 | * |
Anna Bridge |
186:707f6e361f3e | 143 | * @return #E_SUCCESS Output data was written to the location pointed |
Anna Bridge |
186:707f6e361f3e | 144 | * to by @a *out. |
Anna Bridge |
186:707f6e361f3e | 145 | * @return A @ref MXC_Error_Codes "Error Code" indicating the error that |
Anna Bridge |
186:707f6e361f3e | 146 | * occured. |
Anna Bridge |
186:707f6e361f3e | 147 | */ |
Anna Bridge |
186:707f6e361f3e | 148 | int AES_GetOutput(uint8_t *out); |
Anna Bridge |
186:707f6e361f3e | 149 | |
Anna Bridge |
186:707f6e361f3e | 150 | /** |
Anna Bridge |
186:707f6e361f3e | 151 | * @def AES_ECBEncrypt(ptxt, ctxt, mode) |
Anna Bridge |
186:707f6e361f3e | 152 | * @brief Encrypt a block of plaintext with the loaded AES key, blocks |
Anna Bridge |
186:707f6e361f3e | 153 | * until complete. |
Anna Bridge |
186:707f6e361f3e | 154 | * @hideinitializer |
Anna Bridge |
186:707f6e361f3e | 155 | * |
Anna Bridge |
186:707f6e361f3e | 156 | * @param ptxt Pointer to plaintext input array (always 16 bytes) |
Anna Bridge |
186:707f6e361f3e | 157 | * @param ctxt Pointer to ciphertext output array (always 16 bytes) |
Anna Bridge |
186:707f6e361f3e | 158 | * @param mode Selects key length, valid modes found in mxc_aes_mode_t |
Anna Bridge |
186:707f6e361f3e | 159 | */ |
Anna Bridge |
186:707f6e361f3e | 160 | #define AES_ECBEncrypt(ptxt, ctxt, mode) AES_ECBOp(ptxt, ctxt, mode, MXC_E_AES_ENCRYPT) |
Anna Bridge |
186:707f6e361f3e | 161 | |
Anna Bridge |
186:707f6e361f3e | 162 | |
Anna Bridge |
186:707f6e361f3e | 163 | /** |
Anna Bridge |
186:707f6e361f3e | 164 | * @def AES_ECBDecrypt(ctxt, ptxt, mode) |
Anna Bridge |
186:707f6e361f3e | 165 | * @hideinitializer |
Anna Bridge |
186:707f6e361f3e | 166 | * @brief Decrypt a block of ciphertext with the loaded AES key, blocks |
Anna Bridge |
186:707f6e361f3e | 167 | * until complete. |
Anna Bridge |
186:707f6e361f3e | 168 | * |
Anna Bridge |
186:707f6e361f3e | 169 | * @param ctxt Pointer to ciphertext output array (always 16 bytes) |
Anna Bridge |
186:707f6e361f3e | 170 | * @param ptxt Pointer to plaintext input array (always 16 bytes) |
Anna Bridge |
186:707f6e361f3e | 171 | * @param mode Selects key length, valid modes found in mxc_aes_mode_t |
Anna Bridge |
186:707f6e361f3e | 172 | */ |
Anna Bridge |
186:707f6e361f3e | 173 | #define AES_ECBDecrypt(ctxt, ptxt, mode) AES_ECBOp(ctxt, ptxt, mode, MXC_E_AES_DECRYPT) |
Anna Bridge |
186:707f6e361f3e | 174 | |
Anna Bridge |
186:707f6e361f3e | 175 | /** |
Anna Bridge |
186:707f6e361f3e | 176 | * @def AES_ECBEncryptAsync(ptxt, mode) |
Anna Bridge |
186:707f6e361f3e | 177 | * @hideinitializer |
Anna Bridge |
186:707f6e361f3e | 178 | * @brief Starts encryption of a block, enables interrupt, and returns |
Anna Bridge |
186:707f6e361f3e | 179 | * immediately. Use AES_GetOuput() to retrieve result after |
Anna Bridge |
186:707f6e361f3e | 180 | * interrupt fires |
Anna Bridge |
186:707f6e361f3e | 181 | * |
Anna Bridge |
186:707f6e361f3e | 182 | * |
Anna Bridge |
186:707f6e361f3e | 183 | * @param ptxt Pointer to plaintext input array (always 16 bytes) |
Anna Bridge |
186:707f6e361f3e | 184 | * @param mode Selects key length, valid modes found in mxc_aes_mode_t |
Anna Bridge |
186:707f6e361f3e | 185 | */ |
Anna Bridge |
186:707f6e361f3e | 186 | #define AES_ECBEncryptAsync(ptxt, mode) AES_ECBOp(ptxt, NULL, mode, MXC_E_AES_ENCRYPT_ASYNC) |
Anna Bridge |
186:707f6e361f3e | 187 | |
Anna Bridge |
186:707f6e361f3e | 188 | /** |
Anna Bridge |
186:707f6e361f3e | 189 | * @def AES_ECBDecryptAsync(ctxt, mode) |
Anna Bridge |
186:707f6e361f3e | 190 | * @hideinitializer |
Anna Bridge |
186:707f6e361f3e | 191 | * @brief Starts encryption of a block, enables interrupt, and returns |
Anna Bridge |
186:707f6e361f3e | 192 | * immediately. Use AES_GetOuput() to retrieve result after |
Anna Bridge |
186:707f6e361f3e | 193 | * interrupt fires |
Anna Bridge |
186:707f6e361f3e | 194 | * |
Anna Bridge |
186:707f6e361f3e | 195 | * @param ctxt Pointer to ciphertext output array (always 16 bytes) |
Anna Bridge |
186:707f6e361f3e | 196 | * @param mode Selects key length, valid modes found in mxc_aes_mode_t |
Anna Bridge |
186:707f6e361f3e | 197 | */ |
Anna Bridge |
186:707f6e361f3e | 198 | #define AES_ECBDecryptAsync(ctxt, mode) AES_ECBOp(ctxt, NULL, mode, MXC_E_AES_DECRYPT_ASYNC) |
Anna Bridge |
186:707f6e361f3e | 199 | |
Anna Bridge |
186:707f6e361f3e | 200 | /**@} end of group aes*/ |
Anna Bridge |
186:707f6e361f3e | 201 | |
Anna Bridge |
186:707f6e361f3e | 202 | #ifdef __cplusplus |
Anna Bridge |
186:707f6e361f3e | 203 | } |
Anna Bridge |
186:707f6e361f3e | 204 | #endif |
Anna Bridge |
186:707f6e361f3e | 205 | |
Anna Bridge |
186:707f6e361f3e | 206 | #endif |