mbed library sources. Supersedes mbed-src.

Fork of mbed-dev by mbed official

Committer:
<>
Date:
Fri Oct 28 11:17:30 2016 +0100
Revision:
149:156823d33999
This updates the lib to the mbed lib v128

NOTE: This release includes a restructuring of the file and directory locations and thus some
include paths in your code may need updating accordingly.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 149:156823d33999 1 /* mbed Microcontroller Library
<> 149:156823d33999 2 * Copyright (c) 2015-2016 Nuvoton
<> 149:156823d33999 3 *
<> 149:156823d33999 4 * Licensed under the Apache License, Version 2.0 (the "License");
<> 149:156823d33999 5 * you may not use this file except in compliance with the License.
<> 149:156823d33999 6 * You may obtain a copy of the License at
<> 149:156823d33999 7 *
<> 149:156823d33999 8 * http://www.apache.org/licenses/LICENSE-2.0
<> 149:156823d33999 9 *
<> 149:156823d33999 10 * Unless required by applicable law or agreed to in writing, software
<> 149:156823d33999 11 * distributed under the License is distributed on an "AS IS" BASIS,
<> 149:156823d33999 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<> 149:156823d33999 13 * See the License for the specific language governing permissions and
<> 149:156823d33999 14 * limitations under the License.
<> 149:156823d33999 15 */
<> 149:156823d33999 16
<> 149:156823d33999 17 #if !defined(MBEDTLS_CONFIG_FILE)
<> 149:156823d33999 18 #include "mbedtls/config.h"
<> 149:156823d33999 19 #else
<> 149:156823d33999 20 #include MBEDTLS_CONFIG_FILE
<> 149:156823d33999 21 #endif
<> 149:156823d33999 22
<> 149:156823d33999 23 #if defined(MBEDTLS_SHA1_C) || defined(MBEDTLS_SHA256_C) || defined(MBEDTLS_SHA512_C)
<> 149:156823d33999 24
<> 149:156823d33999 25 #if defined(MBEDTLS_SHA1_ALT) || defined(MBEDTLS_SHA256_ALT) || defined(MBEDTLS_SHA512_ALT)
<> 149:156823d33999 26
<> 149:156823d33999 27 #if defined(MBEDTLS_SHA1_ALT)
<> 149:156823d33999 28 #include "sha1_alt.h"
<> 149:156823d33999 29 #endif /* MBEDTLS_SHA1_ALT */
<> 149:156823d33999 30
<> 149:156823d33999 31 #if defined(MBEDTLS_SHA256_ALT)
<> 149:156823d33999 32 #include "sha256_alt.h"
<> 149:156823d33999 33 #endif /* MBEDTLS_SHA256_ALT */
<> 149:156823d33999 34
<> 149:156823d33999 35 #if defined(MBEDTLS_SHA512_ALT)
<> 149:156823d33999 36 #include "sha512_alt.h"
<> 149:156823d33999 37 #endif /* MBEDTLS_SHA512_ALT */
<> 149:156823d33999 38
<> 149:156823d33999 39 #include "nu_bitutil.h"
<> 149:156823d33999 40 #include "mbed_assert.h"
<> 149:156823d33999 41 #include "crypto-misc.h"
<> 149:156823d33999 42
<> 149:156823d33999 43 #include <string.h>
<> 149:156823d33999 44
<> 149:156823d33999 45 void crypto_sha_update(crypto_sha_context *ctx, const unsigned char *input, size_t ilen);
<> 149:156823d33999 46 void crypto_sha_update_nobuf(crypto_sha_context *ctx, const unsigned char *input, size_t ilen, int islast);
<> 149:156823d33999 47 void crypto_sha_getinternstate(unsigned char output[], size_t olen);
<> 149:156823d33999 48
<> 149:156823d33999 49 #endif /* MBEDTLS_SHA1_ALT || MBEDTLS_SHA256_ALT || MBEDTLS_SHA512_ALT */
<> 149:156823d33999 50
<> 149:156823d33999 51 #if defined(MBEDTLS_SHA1_ALT)
<> 149:156823d33999 52
<> 149:156823d33999 53 void mbedtls_sha1_hw_init(crypto_sha_context *ctx)
<> 149:156823d33999 54 {
<> 149:156823d33999 55 crypto_init();
<> 149:156823d33999 56 memset(ctx, 0, sizeof(crypto_sha_context));
<> 149:156823d33999 57 }
<> 149:156823d33999 58
<> 149:156823d33999 59 void mbedtls_sha1_hw_free(crypto_sha_context *ctx)
<> 149:156823d33999 60 {
<> 149:156823d33999 61 if (ctx == NULL) {
<> 149:156823d33999 62 return;
<> 149:156823d33999 63 }
<> 149:156823d33999 64
<> 149:156823d33999 65 crypto_zeroize(ctx, sizeof(crypto_sha_context));
<> 149:156823d33999 66 }
<> 149:156823d33999 67
<> 149:156823d33999 68 void mbedtls_sha1_hw_clone(crypto_sha_context *dst,
<> 149:156823d33999 69 const crypto_sha_context *src)
<> 149:156823d33999 70 {
<> 149:156823d33999 71 *dst = *src;
<> 149:156823d33999 72 }
<> 149:156823d33999 73
<> 149:156823d33999 74 void mbedtls_sha1_hw_starts(crypto_sha_context *ctx)
<> 149:156823d33999 75 {
<> 149:156823d33999 76 // NOTE: mbedtls may call mbedtls_shaXXX_starts multiple times and then call the ending mbedtls_shaXXX_finish. Guard from it.
<> 149:156823d33999 77 CRPT->SHA_CTL |= CRPT_SHA_CTL_STOP_Msk;
<> 149:156823d33999 78
<> 149:156823d33999 79 ctx->total = 0;
<> 149:156823d33999 80 ctx->buffer_left = 0;
<> 149:156823d33999 81 ctx->blocksize = 64;
<> 149:156823d33999 82 ctx->blocksize_mask = 0x3F;
<> 149:156823d33999 83
<> 149:156823d33999 84 SHA_Open(SHA_MODE_SHA1, SHA_NO_SWAP);
<> 149:156823d33999 85
<> 149:156823d33999 86 // Ensure we have correct initial inernal states in SHA_DGST registers even though SHA H/W is not actually started.
<> 149:156823d33999 87 CRPT->SHA_CTL |= CRPT_SHA_CTL_START_Msk;
<> 149:156823d33999 88
<> 149:156823d33999 89 return;
<> 149:156823d33999 90 }
<> 149:156823d33999 91
<> 149:156823d33999 92 void mbedtls_sha1_hw_update(crypto_sha_context *ctx, const unsigned char *input, size_t ilen)
<> 149:156823d33999 93 {
<> 149:156823d33999 94 crypto_sha_update(ctx, input, ilen);
<> 149:156823d33999 95 }
<> 149:156823d33999 96
<> 149:156823d33999 97 void mbedtls_sha1_hw_finish(crypto_sha_context *ctx, unsigned char output[20])
<> 149:156823d33999 98 {
<> 149:156823d33999 99 // H/W SHA cannot handle zero data well. Fall back to S/W SHA.
<> 149:156823d33999 100 if (ctx->total) {
<> 149:156823d33999 101 crypto_sha_update_nobuf(ctx, ctx->buffer, ctx->buffer_left, 1);
<> 149:156823d33999 102 ctx->buffer_left = 0;
<> 149:156823d33999 103 crypto_sha_getinternstate(output, 20);
<> 149:156823d33999 104
<> 149:156823d33999 105 CRPT->SHA_CTL |= CRPT_SHA_CTL_STOP_Msk;
<> 149:156823d33999 106 }
<> 149:156823d33999 107 else {
<> 149:156823d33999 108 mbedtls_sha1_sw_context ctx_sw;
<> 149:156823d33999 109
<> 149:156823d33999 110 mbedtls_sha1_sw_init(&ctx_sw);
<> 149:156823d33999 111 mbedtls_sha1_sw_starts(&ctx_sw);
<> 149:156823d33999 112 mbedtls_sha1_sw_finish(&ctx_sw, output);
<> 149:156823d33999 113 mbedtls_sha1_sw_free(&ctx_sw);
<> 149:156823d33999 114 }
<> 149:156823d33999 115 }
<> 149:156823d33999 116
<> 149:156823d33999 117 void mbedtls_sha1_hw_process(crypto_sha_context *ctx, const unsigned char data[64])
<> 149:156823d33999 118 {
<> 149:156823d33999 119 mbedtls_sha1_hw_update(ctx, data, 64);
<> 149:156823d33999 120 }
<> 149:156823d33999 121
<> 149:156823d33999 122 #endif /* MBEDTLS_SHA1_ALT */
<> 149:156823d33999 123
<> 149:156823d33999 124 #if defined(MBEDTLS_SHA256_ALT)
<> 149:156823d33999 125
<> 149:156823d33999 126 void mbedtls_sha256_hw_init(crypto_sha_context *ctx)
<> 149:156823d33999 127 {
<> 149:156823d33999 128 crypto_init();
<> 149:156823d33999 129 memset(ctx, 0, sizeof(crypto_sha_context));
<> 149:156823d33999 130 }
<> 149:156823d33999 131
<> 149:156823d33999 132 void mbedtls_sha256_hw_free(crypto_sha_context *ctx)
<> 149:156823d33999 133 {
<> 149:156823d33999 134 if (ctx == NULL) {
<> 149:156823d33999 135 return;
<> 149:156823d33999 136 }
<> 149:156823d33999 137
<> 149:156823d33999 138 crypto_zeroize(ctx, sizeof(crypto_sha_context));
<> 149:156823d33999 139 }
<> 149:156823d33999 140
<> 149:156823d33999 141 void mbedtls_sha256_hw_clone(crypto_sha_context *dst,
<> 149:156823d33999 142 const crypto_sha_context *src)
<> 149:156823d33999 143 {
<> 149:156823d33999 144 *dst = *src;
<> 149:156823d33999 145 }
<> 149:156823d33999 146
<> 149:156823d33999 147 void mbedtls_sha256_hw_starts( crypto_sha_context *ctx, int is224)
<> 149:156823d33999 148 {
<> 149:156823d33999 149 // NOTE: mbedtls may call mbedtls_shaXXX_starts multiple times and then call the ending mbedtls_shaXXX_finish. Guard from it.
<> 149:156823d33999 150 CRPT->SHA_CTL |= CRPT_SHA_CTL_STOP_Msk;
<> 149:156823d33999 151
<> 149:156823d33999 152 ctx->total = 0;
<> 149:156823d33999 153 ctx->buffer_left = 0;
<> 149:156823d33999 154 ctx->blocksize = 64;
<> 149:156823d33999 155 ctx->blocksize_mask = 0x3F;
<> 149:156823d33999 156 ctx->is224 = is224;
<> 149:156823d33999 157
<> 149:156823d33999 158 SHA_Open(is224 ? SHA_MODE_SHA224 : SHA_MODE_SHA256, SHA_NO_SWAP);
<> 149:156823d33999 159
<> 149:156823d33999 160 // Ensure we have correct initial inernal states in SHA_DGST registers even though SHA H/W is not actually started.
<> 149:156823d33999 161 CRPT->SHA_CTL |= CRPT_SHA_CTL_START_Msk;
<> 149:156823d33999 162
<> 149:156823d33999 163 return;
<> 149:156823d33999 164 }
<> 149:156823d33999 165
<> 149:156823d33999 166 void mbedtls_sha256_hw_update(crypto_sha_context *ctx, const unsigned char *input, size_t ilen)
<> 149:156823d33999 167 {
<> 149:156823d33999 168 crypto_sha_update(ctx, input, ilen);
<> 149:156823d33999 169 }
<> 149:156823d33999 170
<> 149:156823d33999 171 void mbedtls_sha256_hw_finish(crypto_sha_context *ctx, unsigned char output[32])
<> 149:156823d33999 172 {
<> 149:156823d33999 173 // H/W SHA cannot handle zero data well. Fall back to S/W SHA.
<> 149:156823d33999 174 if (ctx->total) {
<> 149:156823d33999 175 crypto_sha_update_nobuf(ctx, ctx->buffer, ctx->buffer_left, 1);
<> 149:156823d33999 176 ctx->buffer_left = 0;
<> 149:156823d33999 177 crypto_sha_getinternstate(output, ctx->is224 ? 28 : 32);
<> 149:156823d33999 178
<> 149:156823d33999 179 CRPT->SHA_CTL |= CRPT_SHA_CTL_STOP_Msk;
<> 149:156823d33999 180 }
<> 149:156823d33999 181 else {
<> 149:156823d33999 182 mbedtls_sha256_sw_context ctx_sw;
<> 149:156823d33999 183
<> 149:156823d33999 184 mbedtls_sha256_sw_init(&ctx_sw);
<> 149:156823d33999 185 mbedtls_sha256_sw_starts(&ctx_sw, ctx->is224);
<> 149:156823d33999 186 mbedtls_sha256_sw_finish(&ctx_sw, output);
<> 149:156823d33999 187 mbedtls_sha256_sw_free(&ctx_sw);
<> 149:156823d33999 188 }
<> 149:156823d33999 189 }
<> 149:156823d33999 190
<> 149:156823d33999 191 void mbedtls_sha256_hw_process(crypto_sha_context *ctx, const unsigned char data[64])
<> 149:156823d33999 192 {
<> 149:156823d33999 193 mbedtls_sha256_hw_update(ctx, data, 64);
<> 149:156823d33999 194 }
<> 149:156823d33999 195
<> 149:156823d33999 196 #endif /* MBEDTLS_SHA256_ALT */
<> 149:156823d33999 197
<> 149:156823d33999 198 #if defined(MBEDTLS_SHA1_ALT) || defined(MBEDTLS_SHA256_ALT) || defined(MBEDTLS_SHA512_ALT)
<> 149:156823d33999 199
<> 149:156823d33999 200 void crypto_sha_update(crypto_sha_context *ctx, const unsigned char *input, size_t ilen)
<> 149:156823d33999 201 {
<> 149:156823d33999 202 if (ilen == 0) {
<> 149:156823d33999 203 return;
<> 149:156823d33999 204 }
<> 149:156823d33999 205
<> 149:156823d33999 206 size_t fill = ctx->blocksize - ctx->buffer_left;
<> 149:156823d33999 207
<> 149:156823d33999 208 ctx->total += (uint32_t) ilen;
<> 149:156823d33999 209
<> 149:156823d33999 210 if (ctx->buffer_left && ilen >= fill) {
<> 149:156823d33999 211 memcpy((void *) (ctx->buffer + ctx->buffer_left), input, fill);
<> 149:156823d33999 212 input += fill;
<> 149:156823d33999 213 ilen -= fill;
<> 149:156823d33999 214 ctx->buffer_left += fill;
<> 149:156823d33999 215 if (ilen) {
<> 149:156823d33999 216 crypto_sha_update_nobuf(ctx, ctx->buffer, ctx->buffer_left, 0);
<> 149:156823d33999 217 ctx->buffer_left = 0;
<> 149:156823d33999 218 }
<> 149:156823d33999 219 }
<> 149:156823d33999 220
<> 149:156823d33999 221 while (ilen > ctx->blocksize) {
<> 149:156823d33999 222 crypto_sha_update_nobuf(ctx, input, ctx->blocksize, 0);
<> 149:156823d33999 223 input += ctx->blocksize;
<> 149:156823d33999 224 ilen -= ctx->blocksize;
<> 149:156823d33999 225 }
<> 149:156823d33999 226
<> 149:156823d33999 227 if (ilen > 0) {
<> 149:156823d33999 228 memcpy((void *) (ctx->buffer + ctx->buffer_left), input, ilen);
<> 149:156823d33999 229 ctx->buffer_left += ilen;
<> 149:156823d33999 230 }
<> 149:156823d33999 231 }
<> 149:156823d33999 232
<> 149:156823d33999 233 void crypto_sha_update_nobuf(crypto_sha_context *ctx, const unsigned char *input, size_t ilen, int islast)
<> 149:156823d33999 234 {
<> 149:156823d33999 235 // Accept only:
<> 149:156823d33999 236 // 1. Last block which may be incomplete
<> 149:156823d33999 237 // 2. Non-last block which is complete
<> 149:156823d33999 238 MBED_ASSERT(islast || ilen == ctx->blocksize);
<> 149:156823d33999 239
<> 149:156823d33999 240 const unsigned char *in_pos = input;
<> 149:156823d33999 241 int rmn = ilen;
<> 149:156823d33999 242 uint32_t sha_ctl_start = (CRPT->SHA_CTL & ~(CRPT_SHA_CTL_DMALAST_Msk | CRPT_SHA_CTL_DMAEN_Msk)) | CRPT_SHA_CTL_START_Msk;
<> 149:156823d33999 243 uint32_t sha_opmode = (CRPT->SHA_CTL & CRPT_SHA_CTL_OPMODE_Msk) >> CRPT_SHA_CTL_OPMODE_Pos;
<> 149:156823d33999 244 uint32_t DGST0_old, DGST1_old, DGST2_old, DGST3_old, DGST4_old, DGST5_old, DGST6_old, DGST7_old;
<> 149:156823d33999 245
<> 149:156823d33999 246 while (rmn > 0) {
<> 149:156823d33999 247 CRPT->SHA_CTL = sha_ctl_start;
<> 149:156823d33999 248
<> 149:156823d33999 249 uint32_t data = nu_get32_be(in_pos);
<> 149:156823d33999 250 if (rmn <= 4) { // Last word of a (in)complete block
<> 149:156823d33999 251 if (islast) {
<> 149:156823d33999 252 uint32_t lastblock_size = ctx->total & ctx->blocksize_mask;
<> 149:156823d33999 253 if (lastblock_size == 0) {
<> 149:156823d33999 254 lastblock_size = ctx->blocksize;
<> 149:156823d33999 255 }
<> 149:156823d33999 256 CRPT->SHA_DMACNT = lastblock_size;
<> 149:156823d33999 257 CRPT->SHA_CTL = sha_ctl_start | CRPT_SHA_CTL_DMALAST_Msk;
<> 149:156823d33999 258 }
<> 149:156823d33999 259 else {
<> 149:156823d33999 260 switch (sha_opmode) {
<> 149:156823d33999 261 case SHA_MODE_SHA256:
<> 149:156823d33999 262 DGST7_old = CRPT->SHA_DGST7;
<> 149:156823d33999 263 case SHA_MODE_SHA224:
<> 149:156823d33999 264 DGST5_old = CRPT->SHA_DGST5;
<> 149:156823d33999 265 DGST6_old = CRPT->SHA_DGST6;
<> 149:156823d33999 266 case SHA_MODE_SHA1:
<> 149:156823d33999 267 DGST0_old = CRPT->SHA_DGST0;
<> 149:156823d33999 268 DGST1_old = CRPT->SHA_DGST1;
<> 149:156823d33999 269 DGST2_old = CRPT->SHA_DGST2;
<> 149:156823d33999 270 DGST3_old = CRPT->SHA_DGST3;
<> 149:156823d33999 271 DGST4_old = CRPT->SHA_DGST4;
<> 149:156823d33999 272 }
<> 149:156823d33999 273
<> 149:156823d33999 274 CRPT->SHA_CTL = sha_ctl_start;
<> 149:156823d33999 275 }
<> 149:156823d33999 276 }
<> 149:156823d33999 277 else { // Non-last word of a complete block
<> 149:156823d33999 278 CRPT->SHA_CTL = sha_ctl_start;
<> 149:156823d33999 279 }
<> 149:156823d33999 280 while (! (CRPT->SHA_STS & CRPT_SHA_STS_DATINREQ_Msk));
<> 149:156823d33999 281 CRPT->SHA_DATIN = data;
<> 149:156823d33999 282
<> 149:156823d33999 283 in_pos += 4;
<> 149:156823d33999 284 rmn -= 4;
<> 149:156823d33999 285 }
<> 149:156823d33999 286
<> 149:156823d33999 287 if (islast) { // Finish of last block
<> 149:156823d33999 288 while (CRPT->SHA_STS & CRPT_SHA_STS_BUSY_Msk);
<> 149:156823d33999 289 }
<> 149:156823d33999 290 else { // Finish of non-last block
<> 149:156823d33999 291 // No H/W flag to indicate finish of non-last block process.
<> 149:156823d33999 292 // Values of SHA_DGSTx registers will change as last word of the block is input, so use it for judgement.
<> 149:156823d33999 293 int isfinish = 0;
<> 149:156823d33999 294 while (! isfinish) {
<> 149:156823d33999 295 switch (sha_opmode) {
<> 149:156823d33999 296 case SHA_MODE_SHA256:
<> 149:156823d33999 297 if (DGST7_old != CRPT->SHA_DGST7) {
<> 149:156823d33999 298 isfinish = 1;
<> 149:156823d33999 299 break;
<> 149:156823d33999 300 }
<> 149:156823d33999 301 case SHA_MODE_SHA224:
<> 149:156823d33999 302 if (DGST5_old != CRPT->SHA_DGST5 || DGST6_old != CRPT->SHA_DGST6) {
<> 149:156823d33999 303 isfinish = 1;
<> 149:156823d33999 304 break;
<> 149:156823d33999 305 }
<> 149:156823d33999 306 case SHA_MODE_SHA1:
<> 149:156823d33999 307 if (DGST0_old != CRPT->SHA_DGST0 || DGST1_old != CRPT->SHA_DGST1 || DGST2_old != CRPT->SHA_DGST2 ||
<> 149:156823d33999 308 DGST3_old != CRPT->SHA_DGST3 || DGST4_old != CRPT->SHA_DGST4) {
<> 149:156823d33999 309 isfinish = 1;
<> 149:156823d33999 310 break;
<> 149:156823d33999 311 }
<> 149:156823d33999 312 }
<> 149:156823d33999 313 }
<> 149:156823d33999 314 }
<> 149:156823d33999 315 }
<> 149:156823d33999 316
<> 149:156823d33999 317 void crypto_sha_getinternstate(unsigned char output[], size_t olen)
<> 149:156823d33999 318 {
<> 149:156823d33999 319 uint32_t *in_pos = (uint32_t *) &CRPT->SHA_DGST0;
<> 149:156823d33999 320 unsigned char *out_pos = output;
<> 149:156823d33999 321 uint32_t rmn = olen;
<> 149:156823d33999 322
<> 149:156823d33999 323 while (rmn) {
<> 149:156823d33999 324 uint32_t val = *in_pos ++;
<> 149:156823d33999 325 nu_set32_be(out_pos, val);
<> 149:156823d33999 326 out_pos += 4;
<> 149:156823d33999 327 rmn -= 4;
<> 149:156823d33999 328 }
<> 149:156823d33999 329 }
<> 149:156823d33999 330
<> 149:156823d33999 331 #endif /* MBEDTLS_SHA1_ALT || MBEDTLS_SHA256_ALT || MBEDTLS_SHA512_ALT */
<> 149:156823d33999 332
<> 149:156823d33999 333 #endif /* MBEDTLS_SHA1_C || MBEDTLS_SHA256_C || MBEDTLS_SHA512_C */