Fork of mbed-dev build 137, last build before FAT file system appears to be broken. Also reduced HSE timeout time in STM4XX HAL

Fork of mbed-dev by mbed official

Committer:
<>
Date:
Tue Nov 08 17:45:16 2016 +0000
Revision:
150:02e0a0aed4ec
This updates the lib to the mbed lib v129

Who changed what in which revision?

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