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