test

Committer:
elijahsj
Date:
Mon Nov 09 00:33:19 2020 -0500
Revision:
2:4364577b5ad8
Parent:
1:8a094db1347f
copied mbed library

Who changed what in which revision?

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