mbed library sources. Supersedes mbed-src. Edited target satm32f446 for user USART3 pins
Fork of mbed-dev by
targets/TARGET_NUVOTON/TARGET_M480/crypto/crypto-misc.c@188:3f10722804f9, 2018-07-30 (annotated)
- Committer:
- ua1arn
- Date:
- Mon Jul 30 12:31:10 2018 +0000
- Revision:
- 188:3f10722804f9
- Parent:
- 182:a56a73fd2a6f
before add multi-configuration USB descriptors
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Anna Bridge |
180:96ed750bd169 | 1 | /* mbed Microcontroller Library |
Anna Bridge |
180:96ed750bd169 | 2 | * Copyright (c) 2015-2016 Nuvoton |
Anna Bridge |
180:96ed750bd169 | 3 | * |
Anna Bridge |
180:96ed750bd169 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Anna Bridge |
180:96ed750bd169 | 5 | * you may not use this file except in compliance with the License. |
Anna Bridge |
180:96ed750bd169 | 6 | * You may obtain a copy of the License at |
Anna Bridge |
180:96ed750bd169 | 7 | * |
Anna Bridge |
180:96ed750bd169 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Anna Bridge |
180:96ed750bd169 | 9 | * |
Anna Bridge |
180:96ed750bd169 | 10 | * Unless required by applicable law or agreed to in writing, software |
Anna Bridge |
180:96ed750bd169 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Anna Bridge |
180:96ed750bd169 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Anna Bridge |
180:96ed750bd169 | 13 | * See the License for the specific language governing permissions and |
Anna Bridge |
180:96ed750bd169 | 14 | * limitations under the License. |
Anna Bridge |
180:96ed750bd169 | 15 | */ |
Anna Bridge |
180:96ed750bd169 | 16 | |
Anna Bridge |
180:96ed750bd169 | 17 | |
Anna Bridge |
180:96ed750bd169 | 18 | #include "cmsis.h" |
Anna Bridge |
180:96ed750bd169 | 19 | #include "mbed_assert.h" |
Anna Bridge |
180:96ed750bd169 | 20 | #include "mbed_critical.h" |
Anna Bridge |
180:96ed750bd169 | 21 | #include "mbed_error.h" |
Anna Bridge |
180:96ed750bd169 | 22 | #include <limits.h> |
Anna Bridge |
180:96ed750bd169 | 23 | #include "nu_modutil.h" |
Anna Bridge |
180:96ed750bd169 | 24 | #include "nu_bitutil.h" |
Anna Bridge |
180:96ed750bd169 | 25 | #include "crypto-misc.h" |
Anna Bridge |
180:96ed750bd169 | 26 | |
Anna Bridge |
180:96ed750bd169 | 27 | /* Track if AES H/W is available */ |
Anna Bridge |
180:96ed750bd169 | 28 | static uint16_t crypto_aes_avail = 1; |
Anna Bridge |
180:96ed750bd169 | 29 | /* Track if DES H/W is available */ |
Anna Bridge |
180:96ed750bd169 | 30 | static uint16_t crypto_des_avail = 1; |
Anna Bridge |
180:96ed750bd169 | 31 | /* Track if SHA H/W is available */ |
Anna Bridge |
180:96ed750bd169 | 32 | static uint16_t crypto_sha_avail = 1; |
AnnaBridge | 182:a56a73fd2a6f | 33 | /* Track if ECC H/W is available */ |
AnnaBridge | 182:a56a73fd2a6f | 34 | static uint16_t crypto_ecc_avail = 1; |
Anna Bridge |
180:96ed750bd169 | 35 | |
Anna Bridge |
180:96ed750bd169 | 36 | /* Crypto (AES, DES, SHA, etc.) init counter. Crypto's keeps active as it is non-zero. */ |
Anna Bridge |
180:96ed750bd169 | 37 | static uint16_t crypto_init_counter = 0U; |
Anna Bridge |
180:96ed750bd169 | 38 | |
Anna Bridge |
180:96ed750bd169 | 39 | static bool crypto_submodule_acquire(uint16_t *submodule_avail); |
Anna Bridge |
180:96ed750bd169 | 40 | static void crypto_submodule_release(uint16_t *submodule_avail); |
Anna Bridge |
180:96ed750bd169 | 41 | |
AnnaBridge | 182:a56a73fd2a6f | 42 | /* Crypto done flags */ |
AnnaBridge | 182:a56a73fd2a6f | 43 | #define CRYPTO_DONE_OK BIT0 /* Done with OK */ |
AnnaBridge | 182:a56a73fd2a6f | 44 | #define CRYPTO_DONE_ERR BIT1 /* Done with error */ |
AnnaBridge | 182:a56a73fd2a6f | 45 | |
Anna Bridge |
180:96ed750bd169 | 46 | /* Track if PRNG H/W operation is done */ |
Anna Bridge |
180:96ed750bd169 | 47 | static volatile uint16_t crypto_prng_done; |
Anna Bridge |
180:96ed750bd169 | 48 | /* Track if AES H/W operation is done */ |
Anna Bridge |
180:96ed750bd169 | 49 | static volatile uint16_t crypto_aes_done; |
Anna Bridge |
180:96ed750bd169 | 50 | /* Track if DES H/W operation is done */ |
Anna Bridge |
180:96ed750bd169 | 51 | static volatile uint16_t crypto_des_done; |
AnnaBridge | 182:a56a73fd2a6f | 52 | /* Track if ECC H/W operation is done */ |
AnnaBridge | 182:a56a73fd2a6f | 53 | static volatile uint16_t crypto_ecc_done; |
Anna Bridge |
180:96ed750bd169 | 54 | |
Anna Bridge |
180:96ed750bd169 | 55 | static void crypto_submodule_prestart(volatile uint16_t *submodule_done); |
Anna Bridge |
180:96ed750bd169 | 56 | static bool crypto_submodule_wait(volatile uint16_t *submodule_done); |
Anna Bridge |
180:96ed750bd169 | 57 | |
Anna Bridge |
180:96ed750bd169 | 58 | /* As crypto init counter changes from 0 to 1: |
Anna Bridge |
180:96ed750bd169 | 59 | * |
Anna Bridge |
180:96ed750bd169 | 60 | * 1. Enable crypto clock |
Anna Bridge |
180:96ed750bd169 | 61 | * 2. Enable crypto interrupt |
Anna Bridge |
180:96ed750bd169 | 62 | */ |
Anna Bridge |
180:96ed750bd169 | 63 | void crypto_init(void) |
Anna Bridge |
180:96ed750bd169 | 64 | { |
Anna Bridge |
180:96ed750bd169 | 65 | core_util_critical_section_enter(); |
Anna Bridge |
180:96ed750bd169 | 66 | if (crypto_init_counter == USHRT_MAX) { |
Anna Bridge |
180:96ed750bd169 | 67 | core_util_critical_section_exit(); |
Anna Bridge |
180:96ed750bd169 | 68 | error("Crypto clock enable counter would overflow (> USHRT_MAX)"); |
Anna Bridge |
180:96ed750bd169 | 69 | } |
Anna Bridge |
180:96ed750bd169 | 70 | core_util_atomic_incr_u16(&crypto_init_counter, 1); |
Anna Bridge |
180:96ed750bd169 | 71 | if (crypto_init_counter == 1) { |
Anna Bridge |
180:96ed750bd169 | 72 | SYS_UnlockReg(); // Unlock protected register |
Anna Bridge |
180:96ed750bd169 | 73 | CLK_EnableModuleClock(CRPT_MODULE); |
Anna Bridge |
180:96ed750bd169 | 74 | SYS_LockReg(); // Lock protected register |
Anna Bridge |
180:96ed750bd169 | 75 | |
Anna Bridge |
180:96ed750bd169 | 76 | NVIC_EnableIRQ(CRPT_IRQn); |
Anna Bridge |
180:96ed750bd169 | 77 | } |
Anna Bridge |
180:96ed750bd169 | 78 | core_util_critical_section_exit(); |
Anna Bridge |
180:96ed750bd169 | 79 | } |
Anna Bridge |
180:96ed750bd169 | 80 | |
Anna Bridge |
180:96ed750bd169 | 81 | /* As crypto init counter changes from 1 to 0: |
Anna Bridge |
180:96ed750bd169 | 82 | * |
Anna Bridge |
180:96ed750bd169 | 83 | * 1. Disable crypto interrupt |
Anna Bridge |
180:96ed750bd169 | 84 | * 2. Disable crypto clock |
Anna Bridge |
180:96ed750bd169 | 85 | */ |
Anna Bridge |
180:96ed750bd169 | 86 | void crypto_uninit(void) |
Anna Bridge |
180:96ed750bd169 | 87 | { |
Anna Bridge |
180:96ed750bd169 | 88 | core_util_critical_section_enter(); |
Anna Bridge |
180:96ed750bd169 | 89 | if (crypto_init_counter == 0) { |
Anna Bridge |
180:96ed750bd169 | 90 | core_util_critical_section_exit(); |
Anna Bridge |
180:96ed750bd169 | 91 | error("Crypto clock enable counter would underflow (< 0)"); |
Anna Bridge |
180:96ed750bd169 | 92 | } |
Anna Bridge |
180:96ed750bd169 | 93 | core_util_atomic_decr_u16(&crypto_init_counter, 1); |
Anna Bridge |
180:96ed750bd169 | 94 | if (crypto_init_counter == 0) { |
Anna Bridge |
180:96ed750bd169 | 95 | NVIC_DisableIRQ(CRPT_IRQn); |
Anna Bridge |
180:96ed750bd169 | 96 | |
Anna Bridge |
180:96ed750bd169 | 97 | SYS_UnlockReg(); // Unlock protected register |
Anna Bridge |
180:96ed750bd169 | 98 | CLK_DisableModuleClock(CRPT_MODULE); |
Anna Bridge |
180:96ed750bd169 | 99 | SYS_LockReg(); // Lock protected register |
Anna Bridge |
180:96ed750bd169 | 100 | } |
Anna Bridge |
180:96ed750bd169 | 101 | core_util_critical_section_exit(); |
Anna Bridge |
180:96ed750bd169 | 102 | } |
Anna Bridge |
180:96ed750bd169 | 103 | |
Anna Bridge |
180:96ed750bd169 | 104 | /* Implementation that should never be optimized out by the compiler */ |
Anna Bridge |
180:96ed750bd169 | 105 | void crypto_zeroize(void *v, size_t n) |
Anna Bridge |
180:96ed750bd169 | 106 | { |
AnnaBridge | 182:a56a73fd2a6f | 107 | volatile unsigned char *p = (volatile unsigned char*) v; |
AnnaBridge | 182:a56a73fd2a6f | 108 | while (n--) { |
AnnaBridge | 182:a56a73fd2a6f | 109 | *p++ = 0; |
AnnaBridge | 182:a56a73fd2a6f | 110 | } |
AnnaBridge | 182:a56a73fd2a6f | 111 | } |
AnnaBridge | 182:a56a73fd2a6f | 112 | |
AnnaBridge | 182:a56a73fd2a6f | 113 | /* Implementation that should never be optimized out by the compiler */ |
AnnaBridge | 182:a56a73fd2a6f | 114 | void crypto_zeroize32(uint32_t *v, size_t n) |
AnnaBridge | 182:a56a73fd2a6f | 115 | { |
AnnaBridge | 182:a56a73fd2a6f | 116 | volatile uint32_t *p = (volatile uint32_t*) v; |
Anna Bridge |
180:96ed750bd169 | 117 | while (n--) { |
Anna Bridge |
180:96ed750bd169 | 118 | *p++ = 0; |
Anna Bridge |
180:96ed750bd169 | 119 | } |
Anna Bridge |
180:96ed750bd169 | 120 | } |
Anna Bridge |
180:96ed750bd169 | 121 | |
Anna Bridge |
180:96ed750bd169 | 122 | bool crypto_aes_acquire(void) |
Anna Bridge |
180:96ed750bd169 | 123 | { |
Anna Bridge |
180:96ed750bd169 | 124 | return crypto_submodule_acquire(&crypto_aes_avail); |
Anna Bridge |
180:96ed750bd169 | 125 | } |
Anna Bridge |
180:96ed750bd169 | 126 | |
Anna Bridge |
180:96ed750bd169 | 127 | void crypto_aes_release(void) |
Anna Bridge |
180:96ed750bd169 | 128 | { |
Anna Bridge |
180:96ed750bd169 | 129 | crypto_submodule_release(&crypto_aes_avail); |
Anna Bridge |
180:96ed750bd169 | 130 | } |
Anna Bridge |
180:96ed750bd169 | 131 | |
Anna Bridge |
180:96ed750bd169 | 132 | bool crypto_des_acquire(void) |
Anna Bridge |
180:96ed750bd169 | 133 | { |
Anna Bridge |
180:96ed750bd169 | 134 | return crypto_submodule_acquire(&crypto_des_avail); |
Anna Bridge |
180:96ed750bd169 | 135 | } |
Anna Bridge |
180:96ed750bd169 | 136 | |
Anna Bridge |
180:96ed750bd169 | 137 | void crypto_des_release(void) |
Anna Bridge |
180:96ed750bd169 | 138 | { |
Anna Bridge |
180:96ed750bd169 | 139 | crypto_submodule_release(&crypto_des_avail); |
Anna Bridge |
180:96ed750bd169 | 140 | } |
Anna Bridge |
180:96ed750bd169 | 141 | |
Anna Bridge |
180:96ed750bd169 | 142 | bool crypto_sha_acquire(void) |
Anna Bridge |
180:96ed750bd169 | 143 | { |
Anna Bridge |
180:96ed750bd169 | 144 | return crypto_submodule_acquire(&crypto_sha_avail); |
Anna Bridge |
180:96ed750bd169 | 145 | } |
Anna Bridge |
180:96ed750bd169 | 146 | |
Anna Bridge |
180:96ed750bd169 | 147 | void crypto_sha_release(void) |
Anna Bridge |
180:96ed750bd169 | 148 | { |
Anna Bridge |
180:96ed750bd169 | 149 | crypto_submodule_release(&crypto_sha_avail); |
Anna Bridge |
180:96ed750bd169 | 150 | } |
Anna Bridge |
180:96ed750bd169 | 151 | |
AnnaBridge | 182:a56a73fd2a6f | 152 | bool crypto_ecc_acquire(void) |
AnnaBridge | 182:a56a73fd2a6f | 153 | { |
AnnaBridge | 182:a56a73fd2a6f | 154 | return crypto_submodule_acquire(&crypto_ecc_avail); |
AnnaBridge | 182:a56a73fd2a6f | 155 | } |
AnnaBridge | 182:a56a73fd2a6f | 156 | |
AnnaBridge | 182:a56a73fd2a6f | 157 | void crypto_ecc_release(void) |
AnnaBridge | 182:a56a73fd2a6f | 158 | { |
AnnaBridge | 182:a56a73fd2a6f | 159 | crypto_submodule_release(&crypto_ecc_avail); |
AnnaBridge | 182:a56a73fd2a6f | 160 | } |
AnnaBridge | 182:a56a73fd2a6f | 161 | |
Anna Bridge |
180:96ed750bd169 | 162 | void crypto_prng_prestart(void) |
Anna Bridge |
180:96ed750bd169 | 163 | { |
Anna Bridge |
180:96ed750bd169 | 164 | crypto_submodule_prestart(&crypto_prng_done); |
Anna Bridge |
180:96ed750bd169 | 165 | } |
Anna Bridge |
180:96ed750bd169 | 166 | |
Anna Bridge |
180:96ed750bd169 | 167 | bool crypto_prng_wait(void) |
Anna Bridge |
180:96ed750bd169 | 168 | { |
Anna Bridge |
180:96ed750bd169 | 169 | return crypto_submodule_wait(&crypto_prng_done); |
Anna Bridge |
180:96ed750bd169 | 170 | } |
Anna Bridge |
180:96ed750bd169 | 171 | |
Anna Bridge |
180:96ed750bd169 | 172 | void crypto_aes_prestart(void) |
Anna Bridge |
180:96ed750bd169 | 173 | { |
Anna Bridge |
180:96ed750bd169 | 174 | crypto_submodule_prestart(&crypto_aes_done); |
Anna Bridge |
180:96ed750bd169 | 175 | } |
Anna Bridge |
180:96ed750bd169 | 176 | |
Anna Bridge |
180:96ed750bd169 | 177 | bool crypto_aes_wait(void) |
Anna Bridge |
180:96ed750bd169 | 178 | { |
Anna Bridge |
180:96ed750bd169 | 179 | return crypto_submodule_wait(&crypto_aes_done); |
Anna Bridge |
180:96ed750bd169 | 180 | } |
Anna Bridge |
180:96ed750bd169 | 181 | |
Anna Bridge |
180:96ed750bd169 | 182 | void crypto_des_prestart(void) |
Anna Bridge |
180:96ed750bd169 | 183 | { |
Anna Bridge |
180:96ed750bd169 | 184 | crypto_submodule_prestart(&crypto_des_done); |
Anna Bridge |
180:96ed750bd169 | 185 | } |
Anna Bridge |
180:96ed750bd169 | 186 | |
Anna Bridge |
180:96ed750bd169 | 187 | bool crypto_des_wait(void) |
Anna Bridge |
180:96ed750bd169 | 188 | { |
Anna Bridge |
180:96ed750bd169 | 189 | return crypto_submodule_wait(&crypto_des_done); |
Anna Bridge |
180:96ed750bd169 | 190 | } |
Anna Bridge |
180:96ed750bd169 | 191 | |
AnnaBridge | 182:a56a73fd2a6f | 192 | void crypto_ecc_prestart(void) |
AnnaBridge | 182:a56a73fd2a6f | 193 | { |
AnnaBridge | 182:a56a73fd2a6f | 194 | crypto_submodule_prestart(&crypto_ecc_done); |
AnnaBridge | 182:a56a73fd2a6f | 195 | } |
AnnaBridge | 182:a56a73fd2a6f | 196 | |
AnnaBridge | 182:a56a73fd2a6f | 197 | bool crypto_ecc_wait(void) |
AnnaBridge | 182:a56a73fd2a6f | 198 | { |
AnnaBridge | 182:a56a73fd2a6f | 199 | return crypto_submodule_wait(&crypto_ecc_done); |
AnnaBridge | 182:a56a73fd2a6f | 200 | } |
AnnaBridge | 182:a56a73fd2a6f | 201 | |
Anna Bridge |
180:96ed750bd169 | 202 | bool crypto_dma_buff_compat(const void *buff, size_t buff_size, size_t size_aligned_to) |
Anna Bridge |
180:96ed750bd169 | 203 | { |
Anna Bridge |
180:96ed750bd169 | 204 | uint32_t buff_ = (uint32_t) buff; |
Anna Bridge |
180:96ed750bd169 | 205 | |
Anna Bridge |
180:96ed750bd169 | 206 | return (((buff_ & 0x03) == 0) && /* Word-aligned buffer base address */ |
Anna Bridge |
180:96ed750bd169 | 207 | ((buff_size & (size_aligned_to - 1)) == 0) && /* Crypto submodule dependent buffer size alignment */ |
Anna Bridge |
180:96ed750bd169 | 208 | (((buff_ >> 28) == 0x2) && (buff_size <= (0x30000000 - buff_)))); /* 0x20000000-0x2FFFFFFF */ |
Anna Bridge |
180:96ed750bd169 | 209 | } |
Anna Bridge |
180:96ed750bd169 | 210 | |
Anna Bridge |
180:96ed750bd169 | 211 | /* Overlap cases |
Anna Bridge |
180:96ed750bd169 | 212 | * |
Anna Bridge |
180:96ed750bd169 | 213 | * 1. in_buff in front of out_buff: |
Anna Bridge |
180:96ed750bd169 | 214 | * |
Anna Bridge |
180:96ed750bd169 | 215 | * in in_end |
Anna Bridge |
180:96ed750bd169 | 216 | * | | |
Anna Bridge |
180:96ed750bd169 | 217 | * |||||||||||||||| |
Anna Bridge |
180:96ed750bd169 | 218 | * |||||||||||||||| |
Anna Bridge |
180:96ed750bd169 | 219 | * | | |
Anna Bridge |
180:96ed750bd169 | 220 | * out out_end |
Anna Bridge |
180:96ed750bd169 | 221 | * |
Anna Bridge |
180:96ed750bd169 | 222 | * 2. out_buff in front of in_buff: |
Anna Bridge |
180:96ed750bd169 | 223 | * |
Anna Bridge |
180:96ed750bd169 | 224 | * in in_end |
Anna Bridge |
180:96ed750bd169 | 225 | * | | |
Anna Bridge |
180:96ed750bd169 | 226 | * |||||||||||||||| |
Anna Bridge |
180:96ed750bd169 | 227 | * |||||||||||||||| |
Anna Bridge |
180:96ed750bd169 | 228 | * | | |
Anna Bridge |
180:96ed750bd169 | 229 | * out out_end |
Anna Bridge |
180:96ed750bd169 | 230 | */ |
Anna Bridge |
180:96ed750bd169 | 231 | bool crypto_dma_buffs_overlap(const void *in_buff, size_t in_buff_size, const void *out_buff, size_t out_buff_size) |
Anna Bridge |
180:96ed750bd169 | 232 | { |
Anna Bridge |
180:96ed750bd169 | 233 | uint32_t in = (uint32_t) in_buff; |
Anna Bridge |
180:96ed750bd169 | 234 | uint32_t in_end = in + in_buff_size; |
Anna Bridge |
180:96ed750bd169 | 235 | uint32_t out = (uint32_t) out_buff; |
Anna Bridge |
180:96ed750bd169 | 236 | uint32_t out_end = out + out_buff_size; |
Anna Bridge |
180:96ed750bd169 | 237 | |
Anna Bridge |
180:96ed750bd169 | 238 | bool overlap = (in <= out && out < in_end) || (out <= in && in < out_end); |
Anna Bridge |
180:96ed750bd169 | 239 | |
Anna Bridge |
180:96ed750bd169 | 240 | return overlap; |
Anna Bridge |
180:96ed750bd169 | 241 | } |
Anna Bridge |
180:96ed750bd169 | 242 | |
Anna Bridge |
180:96ed750bd169 | 243 | static bool crypto_submodule_acquire(uint16_t *submodule_avail) |
Anna Bridge |
180:96ed750bd169 | 244 | { |
Anna Bridge |
180:96ed750bd169 | 245 | uint16_t expectedCurrentValue = 1; |
Anna Bridge |
180:96ed750bd169 | 246 | return core_util_atomic_cas_u16(submodule_avail, &expectedCurrentValue, 0); |
Anna Bridge |
180:96ed750bd169 | 247 | } |
Anna Bridge |
180:96ed750bd169 | 248 | |
Anna Bridge |
180:96ed750bd169 | 249 | static void crypto_submodule_release(uint16_t *submodule_avail) |
Anna Bridge |
180:96ed750bd169 | 250 | { |
Anna Bridge |
180:96ed750bd169 | 251 | uint16_t expectedCurrentValue = 0; |
Anna Bridge |
180:96ed750bd169 | 252 | while (! core_util_atomic_cas_u16(submodule_avail, &expectedCurrentValue, 1)); |
Anna Bridge |
180:96ed750bd169 | 253 | } |
Anna Bridge |
180:96ed750bd169 | 254 | |
Anna Bridge |
180:96ed750bd169 | 255 | static void crypto_submodule_prestart(volatile uint16_t *submodule_done) |
Anna Bridge |
180:96ed750bd169 | 256 | { |
Anna Bridge |
180:96ed750bd169 | 257 | *submodule_done = 0; |
Anna Bridge |
180:96ed750bd169 | 258 | |
Anna Bridge |
180:96ed750bd169 | 259 | /* Ensure memory accesses above are completed before DMA is started |
Anna Bridge |
180:96ed750bd169 | 260 | * |
Anna Bridge |
180:96ed750bd169 | 261 | * Replacing __DSB() with __DMB() is also OK in this case. |
Anna Bridge |
180:96ed750bd169 | 262 | * |
Anna Bridge |
180:96ed750bd169 | 263 | * Refer to "multi-master systems" section with DMA in: |
Anna Bridge |
180:96ed750bd169 | 264 | * https://static.docs.arm.com/dai0321/a/DAI0321A_programming_guide_memory_barriers_for_m_profile.pdf |
Anna Bridge |
180:96ed750bd169 | 265 | */ |
Anna Bridge |
180:96ed750bd169 | 266 | __DSB(); |
Anna Bridge |
180:96ed750bd169 | 267 | } |
Anna Bridge |
180:96ed750bd169 | 268 | |
Anna Bridge |
180:96ed750bd169 | 269 | static bool crypto_submodule_wait(volatile uint16_t *submodule_done) |
Anna Bridge |
180:96ed750bd169 | 270 | { |
Anna Bridge |
180:96ed750bd169 | 271 | while (! *submodule_done); |
Anna Bridge |
180:96ed750bd169 | 272 | |
Anna Bridge |
180:96ed750bd169 | 273 | /* Ensure while loop above and subsequent code are not reordered */ |
Anna Bridge |
180:96ed750bd169 | 274 | __DSB(); |
Anna Bridge |
180:96ed750bd169 | 275 | |
AnnaBridge | 182:a56a73fd2a6f | 276 | if ((*submodule_done & CRYPTO_DONE_OK)) { |
AnnaBridge | 182:a56a73fd2a6f | 277 | /* Done with OK */ |
AnnaBridge | 182:a56a73fd2a6f | 278 | return true; |
AnnaBridge | 182:a56a73fd2a6f | 279 | } else if ((*submodule_done & CRYPTO_DONE_ERR)) { |
AnnaBridge | 182:a56a73fd2a6f | 280 | /* Done with error */ |
AnnaBridge | 182:a56a73fd2a6f | 281 | return false; |
AnnaBridge | 182:a56a73fd2a6f | 282 | } |
AnnaBridge | 182:a56a73fd2a6f | 283 | |
AnnaBridge | 182:a56a73fd2a6f | 284 | return false; |
Anna Bridge |
180:96ed750bd169 | 285 | } |
Anna Bridge |
180:96ed750bd169 | 286 | |
Anna Bridge |
180:96ed750bd169 | 287 | /* Crypto interrupt handler */ |
Anna Bridge |
180:96ed750bd169 | 288 | void CRYPTO_IRQHandler() |
Anna Bridge |
180:96ed750bd169 | 289 | { |
AnnaBridge | 182:a56a73fd2a6f | 290 | uint32_t intsts; |
AnnaBridge | 182:a56a73fd2a6f | 291 | |
AnnaBridge | 182:a56a73fd2a6f | 292 | if ((intsts = PRNG_GET_INT_FLAG()) != 0) { |
AnnaBridge | 182:a56a73fd2a6f | 293 | /* Done with OK */ |
AnnaBridge | 182:a56a73fd2a6f | 294 | crypto_prng_done |= CRYPTO_DONE_OK; |
AnnaBridge | 182:a56a73fd2a6f | 295 | /* Clear interrupt flag */ |
Anna Bridge |
180:96ed750bd169 | 296 | PRNG_CLR_INT_FLAG(); |
AnnaBridge | 182:a56a73fd2a6f | 297 | } else if ((intsts = AES_GET_INT_FLAG()) != 0) { |
AnnaBridge | 182:a56a73fd2a6f | 298 | /* Done with OK */ |
AnnaBridge | 182:a56a73fd2a6f | 299 | crypto_aes_done |= CRYPTO_DONE_OK; |
AnnaBridge | 182:a56a73fd2a6f | 300 | /* Clear interrupt flag */ |
Anna Bridge |
180:96ed750bd169 | 301 | AES_CLR_INT_FLAG(); |
AnnaBridge | 182:a56a73fd2a6f | 302 | } else if ((intsts = TDES_GET_INT_FLAG()) != 0) { |
AnnaBridge | 182:a56a73fd2a6f | 303 | /* Done with OK */ |
AnnaBridge | 182:a56a73fd2a6f | 304 | crypto_des_done |= CRYPTO_DONE_OK; |
AnnaBridge | 182:a56a73fd2a6f | 305 | /* Clear interrupt flag */ |
Anna Bridge |
180:96ed750bd169 | 306 | TDES_CLR_INT_FLAG(); |
AnnaBridge | 182:a56a73fd2a6f | 307 | } else if ((intsts = ECC_GET_INT_FLAG()) != 0) { |
AnnaBridge | 182:a56a73fd2a6f | 308 | /* Check interrupt flags */ |
AnnaBridge | 182:a56a73fd2a6f | 309 | if (intsts & CRPT_INTSTS_ECCIF_Msk) { |
AnnaBridge | 182:a56a73fd2a6f | 310 | /* Done with OK */ |
AnnaBridge | 182:a56a73fd2a6f | 311 | crypto_ecc_done |= CRYPTO_DONE_OK; |
AnnaBridge | 182:a56a73fd2a6f | 312 | } else if (intsts & CRPT_INTSTS_ECCEIF_Msk) { |
AnnaBridge | 182:a56a73fd2a6f | 313 | /* Done with error */ |
AnnaBridge | 182:a56a73fd2a6f | 314 | crypto_ecc_done |= CRYPTO_DONE_ERR; |
AnnaBridge | 182:a56a73fd2a6f | 315 | } |
AnnaBridge | 182:a56a73fd2a6f | 316 | /* Clear interrupt flag */ |
AnnaBridge | 182:a56a73fd2a6f | 317 | ECC_CLR_INT_FLAG(); |
Anna Bridge |
180:96ed750bd169 | 318 | } |
Anna Bridge |
180:96ed750bd169 | 319 | } |