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