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