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.c@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 This file contains the function implementations for the Advanced |
Anna Bridge |
186:707f6e361f3e | 4 | * Encryption Standard (AES) peripheral module. |
Anna Bridge |
186:707f6e361f3e | 5 | */ |
Anna Bridge |
186:707f6e361f3e | 6 | |
Anna Bridge |
186:707f6e361f3e | 7 | /* **************************************************************************** |
Anna Bridge |
186:707f6e361f3e | 8 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. |
Anna Bridge |
186:707f6e361f3e | 9 | * |
Anna Bridge |
186:707f6e361f3e | 10 | * Permission is hereby granted, free of charge, to any person obtaining a |
Anna Bridge |
186:707f6e361f3e | 11 | * copy of this software and associated documentation files (the "Software"), |
Anna Bridge |
186:707f6e361f3e | 12 | * to deal in the Software without restriction, including without limitation |
Anna Bridge |
186:707f6e361f3e | 13 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
Anna Bridge |
186:707f6e361f3e | 14 | * and/or sell copies of the Software, and to permit persons to whom the |
Anna Bridge |
186:707f6e361f3e | 15 | * Software is furnished to do so, subject to the following conditions: |
Anna Bridge |
186:707f6e361f3e | 16 | * |
Anna Bridge |
186:707f6e361f3e | 17 | * The above copyright notice and this permission notice shall be included |
Anna Bridge |
186:707f6e361f3e | 18 | * in all copies or substantial portions of the Software. |
Anna Bridge |
186:707f6e361f3e | 19 | * |
Anna Bridge |
186:707f6e361f3e | 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
Anna Bridge |
186:707f6e361f3e | 21 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
Anna Bridge |
186:707f6e361f3e | 22 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
Anna Bridge |
186:707f6e361f3e | 23 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
Anna Bridge |
186:707f6e361f3e | 24 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
Anna Bridge |
186:707f6e361f3e | 25 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
Anna Bridge |
186:707f6e361f3e | 26 | * OTHER DEALINGS IN THE SOFTWARE. |
Anna Bridge |
186:707f6e361f3e | 27 | * |
Anna Bridge |
186:707f6e361f3e | 28 | * Except as contained in this notice, the name of Maxim Integrated |
Anna Bridge |
186:707f6e361f3e | 29 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
Anna Bridge |
186:707f6e361f3e | 30 | * Products, Inc. Branding Policy. |
Anna Bridge |
186:707f6e361f3e | 31 | * |
Anna Bridge |
186:707f6e361f3e | 32 | * The mere transfer of this software does not imply any licenses |
Anna Bridge |
186:707f6e361f3e | 33 | * of trade secrets, proprietary technology, copyrights, patents, |
Anna Bridge |
186:707f6e361f3e | 34 | * trademarks, maskwork rights, or any other form of intellectual |
Anna Bridge |
186:707f6e361f3e | 35 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
Anna Bridge |
186:707f6e361f3e | 36 | * ownership rights. |
Anna Bridge |
186:707f6e361f3e | 37 | * |
Anna Bridge |
186:707f6e361f3e | 38 | * $Date: 2016-09-09 12:50:17 -0500 (Fri, 09 Sep 2016) $ |
Anna Bridge |
186:707f6e361f3e | 39 | * $Revision: 24348 $ |
Anna Bridge |
186:707f6e361f3e | 40 | * |
Anna Bridge |
186:707f6e361f3e | 41 | *************************************************************************** */ |
Anna Bridge |
186:707f6e361f3e | 42 | |
Anna Bridge |
186:707f6e361f3e | 43 | /* **** Includes **** */ |
Anna Bridge |
186:707f6e361f3e | 44 | #include <string.h> /* Included for memcpy() & #includes stddef for NULL */ |
Anna Bridge |
186:707f6e361f3e | 45 | |
Anna Bridge |
186:707f6e361f3e | 46 | #include "mxc_config.h" |
Anna Bridge |
186:707f6e361f3e | 47 | #include "aes.h" |
Anna Bridge |
186:707f6e361f3e | 48 | #include "nvic_table.h" |
Anna Bridge |
186:707f6e361f3e | 49 | |
Anna Bridge |
186:707f6e361f3e | 50 | /** |
Anna Bridge |
186:707f6e361f3e | 51 | * @ingroup aes |
Anna Bridge |
186:707f6e361f3e | 52 | * @{ |
Anna Bridge |
186:707f6e361f3e | 53 | */ |
Anna Bridge |
186:707f6e361f3e | 54 | |
Anna Bridge |
186:707f6e361f3e | 55 | /* **** Definitions **** */ |
Anna Bridge |
186:707f6e361f3e | 56 | |
Anna Bridge |
186:707f6e361f3e | 57 | /* **** Globals **** */ |
Anna Bridge |
186:707f6e361f3e | 58 | |
Anna Bridge |
186:707f6e361f3e | 59 | /* **** Local Function Prototypes **** */ |
Anna Bridge |
186:707f6e361f3e | 60 | static int aes_memcpy32(uint32_t *out, uint32_t *in, unsigned int count); |
Anna Bridge |
186:707f6e361f3e | 61 | |
Anna Bridge |
186:707f6e361f3e | 62 | /* **** Functions **** */ |
Anna Bridge |
186:707f6e361f3e | 63 | |
Anna Bridge |
186:707f6e361f3e | 64 | /* ************************************************************************* */ |
Anna Bridge |
186:707f6e361f3e | 65 | int AES_SetKey(const uint8_t *key, mxc_aes_mode_t mode) |
Anna Bridge |
186:707f6e361f3e | 66 | { |
Anna Bridge |
186:707f6e361f3e | 67 | unsigned int len; |
Anna Bridge |
186:707f6e361f3e | 68 | |
Anna Bridge |
186:707f6e361f3e | 69 | /* Erase any existing key */ |
Anna Bridge |
186:707f6e361f3e | 70 | MXC_AES_MEM->key[7] = MXC_AES_MEM->key[6] = MXC_AES_MEM->key[5] = MXC_AES_MEM->key[4] \ |
Anna Bridge |
186:707f6e361f3e | 71 | = MXC_AES_MEM->key[3] = MXC_AES_MEM->key[2] = MXC_AES_MEM->key[1] = MXC_AES_MEM->key[0] \ |
Anna Bridge |
186:707f6e361f3e | 72 | = 0x00000000; |
Anna Bridge |
186:707f6e361f3e | 73 | |
Anna Bridge |
186:707f6e361f3e | 74 | /* Determine length of key */ |
Anna Bridge |
186:707f6e361f3e | 75 | if (mode == MXC_E_AES_MODE_256) { |
Anna Bridge |
186:707f6e361f3e | 76 | len = MXC_AES_KEY_256_LEN; |
Anna Bridge |
186:707f6e361f3e | 77 | } else if (mode == MXC_E_AES_MODE_192) { |
Anna Bridge |
186:707f6e361f3e | 78 | len = MXC_AES_KEY_192_LEN; |
Anna Bridge |
186:707f6e361f3e | 79 | } else if (mode == MXC_E_AES_MODE_128) { |
Anna Bridge |
186:707f6e361f3e | 80 | len = MXC_AES_KEY_128_LEN; |
Anna Bridge |
186:707f6e361f3e | 81 | } else { |
Anna Bridge |
186:707f6e361f3e | 82 | return E_BAD_PARAM; |
Anna Bridge |
186:707f6e361f3e | 83 | } |
Anna Bridge |
186:707f6e361f3e | 84 | |
Anna Bridge |
186:707f6e361f3e | 85 | /* Load new key, based on key mode */ |
Anna Bridge |
186:707f6e361f3e | 86 | if (aes_memcpy32((uint32_t *)MXC_AES_MEM->key, (uint32_t *)key, len / sizeof(uint32_t)) < 0) { |
Anna Bridge |
186:707f6e361f3e | 87 | return E_NULL_PTR; |
Anna Bridge |
186:707f6e361f3e | 88 | } |
Anna Bridge |
186:707f6e361f3e | 89 | |
Anna Bridge |
186:707f6e361f3e | 90 | return E_SUCCESS; |
Anna Bridge |
186:707f6e361f3e | 91 | } |
Anna Bridge |
186:707f6e361f3e | 92 | |
Anna Bridge |
186:707f6e361f3e | 93 | /* ************************************************************************* */ |
Anna Bridge |
186:707f6e361f3e | 94 | int AES_ECBOp(const uint8_t *in, uint8_t *out, mxc_aes_mode_t mode, mxc_aes_dir_t dir) |
Anna Bridge |
186:707f6e361f3e | 95 | { |
Anna Bridge |
186:707f6e361f3e | 96 | /* Output array can't be a NULL, unless we are in _ASYNC mode */ |
Anna Bridge |
186:707f6e361f3e | 97 | if ((out == NULL) |
Anna Bridge |
186:707f6e361f3e | 98 | && ((dir != MXC_E_AES_ENCRYPT_ASYNC) && (dir != MXC_E_AES_DECRYPT_ASYNC))) { |
Anna Bridge |
186:707f6e361f3e | 99 | return E_NULL_PTR; |
Anna Bridge |
186:707f6e361f3e | 100 | } |
Anna Bridge |
186:707f6e361f3e | 101 | |
Anna Bridge |
186:707f6e361f3e | 102 | /* Another encryption is already in progress */ |
Anna Bridge |
186:707f6e361f3e | 103 | if (MXC_AES->ctrl & MXC_F_AES_CTRL_START) { |
Anna Bridge |
186:707f6e361f3e | 104 | return E_BUSY; |
Anna Bridge |
186:707f6e361f3e | 105 | } |
Anna Bridge |
186:707f6e361f3e | 106 | |
Anna Bridge |
186:707f6e361f3e | 107 | /* Clear interrupt flag and any existing configuration*/ |
Anna Bridge |
186:707f6e361f3e | 108 | MXC_AES->ctrl = MXC_F_AES_CTRL_INTFL; |
Anna Bridge |
186:707f6e361f3e | 109 | |
Anna Bridge |
186:707f6e361f3e | 110 | /* Select key size & direction |
Anna Bridge |
186:707f6e361f3e | 111 | * |
Anna Bridge |
186:707f6e361f3e | 112 | * Note: This is done first to detect argument errors, before sensitive data |
Anna Bridge |
186:707f6e361f3e | 113 | * is loaded into AES_MEM block |
Anna Bridge |
186:707f6e361f3e | 114 | * |
Anna Bridge |
186:707f6e361f3e | 115 | */ |
Anna Bridge |
186:707f6e361f3e | 116 | switch (mode) { |
Anna Bridge |
186:707f6e361f3e | 117 | case MXC_E_AES_MODE_128: |
Anna Bridge |
186:707f6e361f3e | 118 | MXC_AES->ctrl |= MXC_S_AES_CTRL_KEY_SIZE_128; |
Anna Bridge |
186:707f6e361f3e | 119 | break; |
Anna Bridge |
186:707f6e361f3e | 120 | |
Anna Bridge |
186:707f6e361f3e | 121 | case MXC_E_AES_MODE_192: |
Anna Bridge |
186:707f6e361f3e | 122 | MXC_AES->ctrl |= MXC_S_AES_CTRL_KEY_SIZE_192; |
Anna Bridge |
186:707f6e361f3e | 123 | break; |
Anna Bridge |
186:707f6e361f3e | 124 | |
Anna Bridge |
186:707f6e361f3e | 125 | case MXC_E_AES_MODE_256: |
Anna Bridge |
186:707f6e361f3e | 126 | MXC_AES->ctrl |= MXC_S_AES_CTRL_KEY_SIZE_256; |
Anna Bridge |
186:707f6e361f3e | 127 | break; |
Anna Bridge |
186:707f6e361f3e | 128 | |
Anna Bridge |
186:707f6e361f3e | 129 | default: |
Anna Bridge |
186:707f6e361f3e | 130 | return E_BAD_PARAM; |
Anna Bridge |
186:707f6e361f3e | 131 | } |
Anna Bridge |
186:707f6e361f3e | 132 | |
Anna Bridge |
186:707f6e361f3e | 133 | switch (dir) { |
Anna Bridge |
186:707f6e361f3e | 134 | case MXC_E_AES_ENCRYPT: |
Anna Bridge |
186:707f6e361f3e | 135 | case MXC_E_AES_ENCRYPT_ASYNC: |
Anna Bridge |
186:707f6e361f3e | 136 | MXC_AES->ctrl |= MXC_S_AES_CTRL_ENCRYPT_MODE; |
Anna Bridge |
186:707f6e361f3e | 137 | break; |
Anna Bridge |
186:707f6e361f3e | 138 | |
Anna Bridge |
186:707f6e361f3e | 139 | case MXC_E_AES_DECRYPT: |
Anna Bridge |
186:707f6e361f3e | 140 | case MXC_E_AES_DECRYPT_ASYNC: |
Anna Bridge |
186:707f6e361f3e | 141 | MXC_AES->ctrl |= MXC_S_AES_CTRL_DECRYPT_MODE; |
Anna Bridge |
186:707f6e361f3e | 142 | break; |
Anna Bridge |
186:707f6e361f3e | 143 | |
Anna Bridge |
186:707f6e361f3e | 144 | default: |
Anna Bridge |
186:707f6e361f3e | 145 | return E_BAD_PARAM; |
Anna Bridge |
186:707f6e361f3e | 146 | } |
Anna Bridge |
186:707f6e361f3e | 147 | |
Anna Bridge |
186:707f6e361f3e | 148 | /* If non-blocking mode has been selected, interrupts are automatically enabled */ |
Anna Bridge |
186:707f6e361f3e | 149 | if ((dir == MXC_E_AES_ENCRYPT_ASYNC) || |
Anna Bridge |
186:707f6e361f3e | 150 | (dir == MXC_E_AES_DECRYPT_ASYNC)) { |
Anna Bridge |
186:707f6e361f3e | 151 | MXC_AES->ctrl |= MXC_F_AES_CTRL_INTEN; |
Anna Bridge |
186:707f6e361f3e | 152 | } |
Anna Bridge |
186:707f6e361f3e | 153 | |
Anna Bridge |
186:707f6e361f3e | 154 | /* Load input into engine */ |
Anna Bridge |
186:707f6e361f3e | 155 | if (aes_memcpy32((uint32_t *)MXC_AES_MEM->inp, (uint32_t *)in, MXC_AES_DATA_LEN / sizeof(uint32_t)) < 0) { |
Anna Bridge |
186:707f6e361f3e | 156 | return E_NULL_PTR; |
Anna Bridge |
186:707f6e361f3e | 157 | } |
Anna Bridge |
186:707f6e361f3e | 158 | |
Anna Bridge |
186:707f6e361f3e | 159 | /* Start operation */ |
Anna Bridge |
186:707f6e361f3e | 160 | MXC_AES->ctrl |= MXC_F_AES_CTRL_START; |
Anna Bridge |
186:707f6e361f3e | 161 | |
Anna Bridge |
186:707f6e361f3e | 162 | /* Block, waiting on engine to complete, or fall through if non-blocking */ |
Anna Bridge |
186:707f6e361f3e | 163 | if ((dir != MXC_E_AES_ENCRYPT_ASYNC) && |
Anna Bridge |
186:707f6e361f3e | 164 | (dir != MXC_E_AES_DECRYPT_ASYNC)) { |
Anna Bridge |
186:707f6e361f3e | 165 | while (MXC_AES->ctrl & MXC_F_AES_CTRL_START) { |
Anna Bridge |
186:707f6e361f3e | 166 | /* Ensure that this wait loop is not optimized out */ |
Anna Bridge |
186:707f6e361f3e | 167 | __NOP(); |
Anna Bridge |
186:707f6e361f3e | 168 | } |
Anna Bridge |
186:707f6e361f3e | 169 | |
Anna Bridge |
186:707f6e361f3e | 170 | /* Get output from engine */ |
Anna Bridge |
186:707f6e361f3e | 171 | return AES_GetOutput(out); |
Anna Bridge |
186:707f6e361f3e | 172 | } |
Anna Bridge |
186:707f6e361f3e | 173 | |
Anna Bridge |
186:707f6e361f3e | 174 | return E_SUCCESS; |
Anna Bridge |
186:707f6e361f3e | 175 | } |
Anna Bridge |
186:707f6e361f3e | 176 | |
Anna Bridge |
186:707f6e361f3e | 177 | /* ************************************************************************* */ |
Anna Bridge |
186:707f6e361f3e | 178 | int AES_GetOutput(uint8_t *out) |
Anna Bridge |
186:707f6e361f3e | 179 | { |
Anna Bridge |
186:707f6e361f3e | 180 | /* Don't read it out of the AES memory unless engine is idle */ |
Anna Bridge |
186:707f6e361f3e | 181 | if (MXC_AES->ctrl & MXC_F_AES_CTRL_START) { |
Anna Bridge |
186:707f6e361f3e | 182 | return E_BUSY; |
Anna Bridge |
186:707f6e361f3e | 183 | } |
Anna Bridge |
186:707f6e361f3e | 184 | |
Anna Bridge |
186:707f6e361f3e | 185 | /* Pull out result */ |
Anna Bridge |
186:707f6e361f3e | 186 | if (aes_memcpy32((uint32_t *)out, (uint32_t *)MXC_AES_MEM->out, MXC_AES_DATA_LEN / sizeof(uint32_t)) < 0) { |
Anna Bridge |
186:707f6e361f3e | 187 | return E_NULL_PTR; |
Anna Bridge |
186:707f6e361f3e | 188 | } |
Anna Bridge |
186:707f6e361f3e | 189 | |
Anna Bridge |
186:707f6e361f3e | 190 | /* Clear interrupt flag, write 1 to clear */ |
Anna Bridge |
186:707f6e361f3e | 191 | MXC_AES->ctrl |= MXC_F_AES_CTRL_INTFL; |
Anna Bridge |
186:707f6e361f3e | 192 | |
Anna Bridge |
186:707f6e361f3e | 193 | return E_SUCCESS; |
Anna Bridge |
186:707f6e361f3e | 194 | } |
Anna Bridge |
186:707f6e361f3e | 195 | |
Anna Bridge |
186:707f6e361f3e | 196 | /** |
Anna Bridge |
186:707f6e361f3e | 197 | * @internal This memory copy is used only by the AES module to avoid data leakage by the standard C library. |
Anna Bridge |
186:707f6e361f3e | 198 | * Copy count number of 32-bit locations from in to out |
Anna Bridge |
186:707f6e361f3e | 199 | */ |
Anna Bridge |
186:707f6e361f3e | 200 | static int aes_memcpy32(uint32_t *out, uint32_t *in, unsigned int count) |
Anna Bridge |
186:707f6e361f3e | 201 | { |
Anna Bridge |
186:707f6e361f3e | 202 | if ((out == NULL) || (in == NULL)) { |
Anna Bridge |
186:707f6e361f3e | 203 | /* Invalid arguments, but is internal-only so don't use error codes */ |
Anna Bridge |
186:707f6e361f3e | 204 | return -1; |
Anna Bridge |
186:707f6e361f3e | 205 | } |
Anna Bridge |
186:707f6e361f3e | 206 | |
Anna Bridge |
186:707f6e361f3e | 207 | while (count--) { |
Anna Bridge |
186:707f6e361f3e | 208 | *out++ = *in++; |
Anna Bridge |
186:707f6e361f3e | 209 | } |
Anna Bridge |
186:707f6e361f3e | 210 | |
Anna Bridge |
186:707f6e361f3e | 211 | return 0; |
Anna Bridge |
186:707f6e361f3e | 212 | } |
Anna Bridge |
186:707f6e361f3e | 213 | |
Anna Bridge |
186:707f6e361f3e | 214 | /**@} end of group aes */ |