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_DES_C)
<> 149:156823d33999 24 #if defined(MBEDTLS_DES_ALT)
<> 149:156823d33999 25
<> 149:156823d33999 26 #include <string.h>
<> 149:156823d33999 27 #include "mbedtls/des.h"
<> 149:156823d33999 28 #include "des_alt.h"
<> 149:156823d33999 29 #include "crypto-misc.h"
<> 149:156823d33999 30 #include "nu_bitutil.h"
<> 149:156823d33999 31 #include "toolchain.h"
<> 149:156823d33999 32
<> 149:156823d33999 33 // Must be a multiple of 64-bit block size
<> 149:156823d33999 34 #define MAXSIZE_DMABUF (8 * 5)
<> 149:156823d33999 35 static uint8_t dmabuf_in[MAXSIZE_DMABUF] MBED_ALIGN(4);
<> 149:156823d33999 36 static uint8_t dmabuf_out[MAXSIZE_DMABUF] MBED_ALIGN(4);
<> 149:156823d33999 37
<> 149:156823d33999 38 static int mbedtls_des_docrypt(uint16_t keyopt, uint8_t key[3][MBEDTLS_DES_KEY_SIZE], int enc, uint32_t tdes_opmode, size_t length,
<> 149:156823d33999 39 unsigned char iv[8], const unsigned char *input, unsigned char *output);
<> 149:156823d33999 40
<> 149:156823d33999 41 void mbedtls_des_init(mbedtls_des_context *ctx)
<> 149:156823d33999 42 {
<> 149:156823d33999 43 crypto_init();
<> 149:156823d33999 44 memset(ctx, 0, sizeof(mbedtls_des_context));
<> 149:156823d33999 45 }
<> 149:156823d33999 46
<> 149:156823d33999 47 void mbedtls_des_free( mbedtls_des_context *ctx )
<> 149:156823d33999 48 {
<> 149:156823d33999 49 if (ctx == NULL) {
<> 149:156823d33999 50 return;
<> 149:156823d33999 51 }
<> 149:156823d33999 52
<> 149:156823d33999 53 crypto_zeroize(ctx, sizeof(mbedtls_des_context));
<> 149:156823d33999 54 }
<> 149:156823d33999 55
<> 149:156823d33999 56 void mbedtls_des3_init( mbedtls_des3_context *ctx )
<> 149:156823d33999 57 {
<> 149:156823d33999 58 crypto_init();
<> 149:156823d33999 59 memset(ctx, 0, sizeof(mbedtls_des3_context));
<> 149:156823d33999 60 }
<> 149:156823d33999 61
<> 149:156823d33999 62 void mbedtls_des3_free( mbedtls_des3_context *ctx )
<> 149:156823d33999 63 {
<> 149:156823d33999 64 if (ctx == NULL) {
<> 149:156823d33999 65 return;
<> 149:156823d33999 66 }
<> 149:156823d33999 67
<> 149:156823d33999 68 crypto_zeroize(ctx, sizeof (mbedtls_des3_context));
<> 149:156823d33999 69 }
<> 149:156823d33999 70
<> 149:156823d33999 71 static const unsigned char odd_parity_table[128] = { 1, 2, 4, 7, 8,
<> 149:156823d33999 72 11, 13, 14, 16, 19, 21, 22, 25, 26, 28, 31, 32, 35, 37, 38, 41, 42, 44,
<> 149:156823d33999 73 47, 49, 50, 52, 55, 56, 59, 61, 62, 64, 67, 69, 70, 73, 74, 76, 79, 81,
<> 149:156823d33999 74 82, 84, 87, 88, 91, 93, 94, 97, 98, 100, 103, 104, 107, 109, 110, 112,
<> 149:156823d33999 75 115, 117, 118, 121, 122, 124, 127, 128, 131, 133, 134, 137, 138, 140,
<> 149:156823d33999 76 143, 145, 146, 148, 151, 152, 155, 157, 158, 161, 162, 164, 167, 168,
<> 149:156823d33999 77 171, 173, 174, 176, 179, 181, 182, 185, 186, 188, 191, 193, 194, 196,
<> 149:156823d33999 78 199, 200, 203, 205, 206, 208, 211, 213, 214, 217, 218, 220, 223, 224,
<> 149:156823d33999 79 227, 229, 230, 233, 234, 236, 239, 241, 242, 244, 247, 248, 251, 253,
<> 149:156823d33999 80 254 };
<> 149:156823d33999 81
<> 149:156823d33999 82 void mbedtls_des_key_set_parity(unsigned char key[MBEDTLS_DES_KEY_SIZE])
<> 149:156823d33999 83 {
<> 149:156823d33999 84 int i;
<> 149:156823d33999 85
<> 149:156823d33999 86 for (i = 0; i < MBEDTLS_DES_KEY_SIZE; i++) {
<> 149:156823d33999 87 key[i] = odd_parity_table[key[i] / 2];
<> 149:156823d33999 88 }
<> 149:156823d33999 89 }
<> 149:156823d33999 90
<> 149:156823d33999 91 /*
<> 149:156823d33999 92 * Check the given key's parity, returns 1 on failure, 0 on SUCCESS
<> 149:156823d33999 93 */
<> 149:156823d33999 94 int mbedtls_des_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
<> 149:156823d33999 95 {
<> 149:156823d33999 96 int i;
<> 149:156823d33999 97
<> 149:156823d33999 98 for( i = 0; i < MBEDTLS_DES_KEY_SIZE; i++ )
<> 149:156823d33999 99 if( key[i] != odd_parity_table[key[i] / 2] )
<> 149:156823d33999 100 return( 1 );
<> 149:156823d33999 101
<> 149:156823d33999 102 return( 0 );
<> 149:156823d33999 103 }
<> 149:156823d33999 104
<> 149:156823d33999 105 /*
<> 149:156823d33999 106 * Table of weak and semi-weak keys
<> 149:156823d33999 107 *
<> 149:156823d33999 108 * Source: http://en.wikipedia.org/wiki/Weak_key
<> 149:156823d33999 109 *
<> 149:156823d33999 110 * Weak:
<> 149:156823d33999 111 * Alternating ones + zeros (0x0101010101010101)
<> 149:156823d33999 112 * Alternating 'F' + 'E' (0xFEFEFEFEFEFEFEFE)
<> 149:156823d33999 113 * '0xE0E0E0E0F1F1F1F1'
<> 149:156823d33999 114 * '0x1F1F1F1F0E0E0E0E'
<> 149:156823d33999 115 *
<> 149:156823d33999 116 * Semi-weak:
<> 149:156823d33999 117 * 0x011F011F010E010E and 0x1F011F010E010E01
<> 149:156823d33999 118 * 0x01E001E001F101F1 and 0xE001E001F101F101
<> 149:156823d33999 119 * 0x01FE01FE01FE01FE and 0xFE01FE01FE01FE01
<> 149:156823d33999 120 * 0x1FE01FE00EF10EF1 and 0xE01FE01FF10EF10E
<> 149:156823d33999 121 * 0x1FFE1FFE0EFE0EFE and 0xFE1FFE1FFE0EFE0E
<> 149:156823d33999 122 * 0xE0FEE0FEF1FEF1FE and 0xFEE0FEE0FEF1FEF1
<> 149:156823d33999 123 *
<> 149:156823d33999 124 */
<> 149:156823d33999 125
<> 149:156823d33999 126 #define WEAK_KEY_COUNT 16
<> 149:156823d33999 127
<> 149:156823d33999 128 static const unsigned char weak_key_table[WEAK_KEY_COUNT][MBEDTLS_DES_KEY_SIZE] =
<> 149:156823d33999 129 {
<> 149:156823d33999 130 { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
<> 149:156823d33999 131 { 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE },
<> 149:156823d33999 132 { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E },
<> 149:156823d33999 133 { 0xE0, 0xE0, 0xE0, 0xE0, 0xF1, 0xF1, 0xF1, 0xF1 },
<> 149:156823d33999 134
<> 149:156823d33999 135 { 0x01, 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E },
<> 149:156823d33999 136 { 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E, 0x01 },
<> 149:156823d33999 137 { 0x01, 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1 },
<> 149:156823d33999 138 { 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1, 0x01 },
<> 149:156823d33999 139 { 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE },
<> 149:156823d33999 140 { 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01 },
<> 149:156823d33999 141 { 0x1F, 0xE0, 0x1F, 0xE0, 0x0E, 0xF1, 0x0E, 0xF1 },
<> 149:156823d33999 142 { 0xE0, 0x1F, 0xE0, 0x1F, 0xF1, 0x0E, 0xF1, 0x0E },
<> 149:156823d33999 143 { 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE },
<> 149:156823d33999 144 { 0xFE, 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E },
<> 149:156823d33999 145 { 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE },
<> 149:156823d33999 146 { 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1 }
<> 149:156823d33999 147 };
<> 149:156823d33999 148
<> 149:156823d33999 149 int mbedtls_des_key_check_weak( const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
<> 149:156823d33999 150 {
<> 149:156823d33999 151 int i;
<> 149:156823d33999 152
<> 149:156823d33999 153 for( i = 0; i < WEAK_KEY_COUNT; i++ )
<> 149:156823d33999 154 if( memcmp( weak_key_table[i], key, MBEDTLS_DES_KEY_SIZE) == 0 )
<> 149:156823d33999 155 return( 1 );
<> 149:156823d33999 156
<> 149:156823d33999 157 return( 0 );
<> 149:156823d33999 158 }
<> 149:156823d33999 159
<> 149:156823d33999 160 /*
<> 149:156823d33999 161 * DES key schedule (56-bit, encryption)
<> 149:156823d33999 162 */
<> 149:156823d33999 163 int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
<> 149:156823d33999 164 {
<> 149:156823d33999 165 ctx->enc = 1;
<> 149:156823d33999 166 // Keying option 3: All three keys are identical, i.e. K1 = K2 = K3.
<> 149:156823d33999 167 ctx->keyopt = 3;
<> 149:156823d33999 168 memcpy(ctx->key[0], key, MBEDTLS_DES_KEY_SIZE);
<> 149:156823d33999 169 memcpy(ctx->key[1], key, MBEDTLS_DES_KEY_SIZE);
<> 149:156823d33999 170 memcpy(ctx->key[2], key, MBEDTLS_DES_KEY_SIZE);
<> 149:156823d33999 171
<> 149:156823d33999 172 return 0;
<> 149:156823d33999 173 }
<> 149:156823d33999 174
<> 149:156823d33999 175 /*
<> 149:156823d33999 176 * DES key schedule (56-bit, decryption)
<> 149:156823d33999 177 */
<> 149:156823d33999 178 int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
<> 149:156823d33999 179 {
<> 149:156823d33999 180 ctx->enc = 0;
<> 149:156823d33999 181 // Keying option 3: All three keys are identical, i.e. K1 = K2 = K3.
<> 149:156823d33999 182 ctx->keyopt = 3;
<> 149:156823d33999 183 memcpy(ctx->key[0], key, MBEDTLS_DES_KEY_SIZE);
<> 149:156823d33999 184 memcpy(ctx->key[1], key, MBEDTLS_DES_KEY_SIZE);
<> 149:156823d33999 185 memcpy(ctx->key[2], key, MBEDTLS_DES_KEY_SIZE);
<> 149:156823d33999 186
<> 149:156823d33999 187 return 0;
<> 149:156823d33999 188 }
<> 149:156823d33999 189
<> 149:156823d33999 190 /*
<> 149:156823d33999 191 * Triple-DES key schedule (112-bit, encryption)
<> 149:156823d33999 192 */
<> 149:156823d33999 193 int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,
<> 149:156823d33999 194 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] )
<> 149:156823d33999 195 {
<> 149:156823d33999 196 ctx->enc = 1;
<> 149:156823d33999 197 // Keying option 2: K1 and K2 are independent, and K3 = K1.
<> 149:156823d33999 198 ctx->keyopt = 2;
<> 149:156823d33999 199 memcpy(ctx->key[0], key, MBEDTLS_DES_KEY_SIZE);
<> 149:156823d33999 200 memcpy(ctx->key[1], key + MBEDTLS_DES_KEY_SIZE, MBEDTLS_DES_KEY_SIZE);
<> 149:156823d33999 201 memcpy(ctx->key[2], key, MBEDTLS_DES_KEY_SIZE);
<> 149:156823d33999 202
<> 149:156823d33999 203 return 0;
<> 149:156823d33999 204 }
<> 149:156823d33999 205
<> 149:156823d33999 206 /*
<> 149:156823d33999 207 * Triple-DES key schedule (112-bit, decryption)
<> 149:156823d33999 208 */
<> 149:156823d33999 209 int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,
<> 149:156823d33999 210 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] )
<> 149:156823d33999 211 {
<> 149:156823d33999 212 ctx->enc = 0;
<> 149:156823d33999 213 // Keying option 2: K1 and K2 are independent, and K3 = K1.
<> 149:156823d33999 214 ctx->keyopt = 2;
<> 149:156823d33999 215 memcpy(ctx->key[0], key, MBEDTLS_DES_KEY_SIZE);
<> 149:156823d33999 216 memcpy(ctx->key[1], key + MBEDTLS_DES_KEY_SIZE, MBEDTLS_DES_KEY_SIZE);
<> 149:156823d33999 217 memcpy(ctx->key[2], key, MBEDTLS_DES_KEY_SIZE);
<> 149:156823d33999 218
<> 149:156823d33999 219 return 0;
<> 149:156823d33999 220 }
<> 149:156823d33999 221
<> 149:156823d33999 222 /*
<> 149:156823d33999 223 * Triple-DES key schedule (168-bit, encryption)
<> 149:156823d33999 224 */
<> 149:156823d33999 225 int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,
<> 149:156823d33999 226 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] )
<> 149:156823d33999 227 {
<> 149:156823d33999 228 ctx->enc = 1;
<> 149:156823d33999 229 // Keying option 1: All three keys are independent.
<> 149:156823d33999 230 ctx->keyopt = 1;
<> 149:156823d33999 231 memcpy(ctx->key[0], key, MBEDTLS_DES_KEY_SIZE);
<> 149:156823d33999 232 memcpy(ctx->key[1], key + MBEDTLS_DES_KEY_SIZE, MBEDTLS_DES_KEY_SIZE);
<> 149:156823d33999 233 memcpy(ctx->key[2], key + MBEDTLS_DES_KEY_SIZE * 2, MBEDTLS_DES_KEY_SIZE);
<> 149:156823d33999 234
<> 149:156823d33999 235 return 0;
<> 149:156823d33999 236 }
<> 149:156823d33999 237
<> 149:156823d33999 238 /*
<> 149:156823d33999 239 * Triple-DES key schedule (168-bit, decryption)
<> 149:156823d33999 240 */
<> 149:156823d33999 241 int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx,
<> 149:156823d33999 242 const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] )
<> 149:156823d33999 243 {
<> 149:156823d33999 244 ctx->enc = 0;
<> 149:156823d33999 245 // Keying option 1: All three keys are independent.
<> 149:156823d33999 246 ctx->keyopt = 1;
<> 149:156823d33999 247 memcpy(ctx->key[0], key, MBEDTLS_DES_KEY_SIZE);
<> 149:156823d33999 248 memcpy(ctx->key[1], key + MBEDTLS_DES_KEY_SIZE, MBEDTLS_DES_KEY_SIZE);
<> 149:156823d33999 249 memcpy(ctx->key[2], key + MBEDTLS_DES_KEY_SIZE * 2, MBEDTLS_DES_KEY_SIZE);
<> 149:156823d33999 250
<> 149:156823d33999 251 return 0;
<> 149:156823d33999 252 }
<> 149:156823d33999 253
<> 149:156823d33999 254 /*
<> 149:156823d33999 255 * DES-ECB block encryption/decryption
<> 149:156823d33999 256 */
<> 149:156823d33999 257 int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,
<> 149:156823d33999 258 const unsigned char input[8],
<> 149:156823d33999 259 unsigned char output[8] )
<> 149:156823d33999 260 {
<> 149:156823d33999 261 unsigned char iv[8] = {0x00};
<> 149:156823d33999 262 return mbedtls_des_docrypt(ctx->keyopt, ctx->key, ctx->enc, DES_MODE_ECB, 8, iv, input, output);
<> 149:156823d33999 263 }
<> 149:156823d33999 264
<> 149:156823d33999 265 #if defined(MBEDTLS_CIPHER_MODE_CBC)
<> 149:156823d33999 266 /*
<> 149:156823d33999 267 * DES-CBC buffer encryption/decryption
<> 149:156823d33999 268 */
<> 149:156823d33999 269 int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx,
<> 149:156823d33999 270 int mode,
<> 149:156823d33999 271 size_t length,
<> 149:156823d33999 272 unsigned char iv[8],
<> 149:156823d33999 273 const unsigned char *input,
<> 149:156823d33999 274 unsigned char *output )
<> 149:156823d33999 275 {
<> 149:156823d33999 276 return mbedtls_des_docrypt(ctx->keyopt, ctx->key, mode == MBEDTLS_DES_ENCRYPT, DES_MODE_CBC, length, iv, input, output);
<> 149:156823d33999 277 }
<> 149:156823d33999 278 #endif /* MBEDTLS_CIPHER_MODE_CBC */
<> 149:156823d33999 279
<> 149:156823d33999 280 /*
<> 149:156823d33999 281 * 3DES-ECB block encryption/decryption
<> 149:156823d33999 282 */
<> 149:156823d33999 283 int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx,
<> 149:156823d33999 284 const unsigned char input[8],
<> 149:156823d33999 285 unsigned char output[8] )
<> 149:156823d33999 286 {
<> 149:156823d33999 287 unsigned char iv[8] = {0x00};
<> 149:156823d33999 288 return mbedtls_des_docrypt(ctx->keyopt, ctx->key, ctx->enc, TDES_MODE_ECB, 8, iv, input, output);
<> 149:156823d33999 289 }
<> 149:156823d33999 290
<> 149:156823d33999 291 #if defined(MBEDTLS_CIPHER_MODE_CBC)
<> 149:156823d33999 292 /*
<> 149:156823d33999 293 * 3DES-CBC buffer encryption/decryption
<> 149:156823d33999 294 */
<> 149:156823d33999 295 int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx,
<> 149:156823d33999 296 int mode,
<> 149:156823d33999 297 size_t length,
<> 149:156823d33999 298 unsigned char iv[8],
<> 149:156823d33999 299 const unsigned char *input,
<> 149:156823d33999 300 unsigned char *output )
<> 149:156823d33999 301 {
<> 149:156823d33999 302 return mbedtls_des_docrypt(ctx->keyopt, ctx->key, mode == MBEDTLS_DES_ENCRYPT, TDES_MODE_CBC, length, iv, input, output);
<> 149:156823d33999 303 }
<> 149:156823d33999 304 #endif /* MBEDTLS_CIPHER_MODE_CBC */
<> 149:156823d33999 305
<> 149:156823d33999 306
<> 149:156823d33999 307
<> 149:156823d33999 308 static int mbedtls_des_docrypt(uint16_t keyopt, uint8_t key[3][MBEDTLS_DES_KEY_SIZE], int enc, uint32_t tdes_opmode, size_t length,
<> 149:156823d33999 309 unsigned char iv[8], const unsigned char *input, unsigned char *output)
<> 149:156823d33999 310 {
<> 149:156823d33999 311 if (length % 8) {
<> 149:156823d33999 312 return MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH;
<> 149:156823d33999 313 }
<> 149:156823d33999 314
<> 149:156823d33999 315 // NOTE: Don't call driver function TDES_Open in BSP because it doesn't support TDES_CTL[3KEYS] setting.
<> 149:156823d33999 316 CRPT->TDES_CTL = (0 << CRPT_TDES_CTL_CHANNEL_Pos) | (enc << CRPT_TDES_CTL_ENCRPT_Pos) |
<> 149:156823d33999 317 tdes_opmode | (TDES_IN_OUT_WHL_SWAP << CRPT_TDES_CTL_BLKSWAP_Pos);
<> 149:156823d33999 318
<> 149:156823d33999 319 // Keying option 1: All three keys are independent.
<> 149:156823d33999 320 // Keying option 2: K1 and K2 are independent, and K3 = K1.
<> 149:156823d33999 321 // Keying option 3: All three keys are identical, i.e. K1 = K2 = K3.
<> 149:156823d33999 322 if (keyopt == 1) {
<> 149:156823d33999 323 CRPT->TDES_CTL |= CRPT_TDES_CTL_3KEYS_Msk;
<> 149:156823d33999 324 }
<> 149:156823d33999 325 else {
<> 149:156823d33999 326 CRPT->TDES_CTL &= ~CRPT_TDES_CTL_3KEYS_Msk;
<> 149:156823d33999 327 }
<> 149:156823d33999 328
<> 149:156823d33999 329 // Set DES/TDES keys
<> 149:156823d33999 330 // NOTE: Don't call driver function TDES_SetKey in BSP because it doesn't support endian swap.
<> 149:156823d33999 331 uint32_t val;
<> 149:156823d33999 332 volatile uint32_t *tdes_key = (uint32_t *) ((uint32_t) &CRPT->TDES0_KEY1H + (0x40 * 0));
<> 149:156823d33999 333 val = nu_get32_be(key[0] + 0);
<> 149:156823d33999 334 *tdes_key ++ = val;
<> 149:156823d33999 335 val = nu_get32_be(key[0] + 4);
<> 149:156823d33999 336 *tdes_key ++ = val;
<> 149:156823d33999 337 val = nu_get32_be(key[1] + 0);
<> 149:156823d33999 338 *tdes_key ++ = val;
<> 149:156823d33999 339 val = nu_get32_be(key[1] + 4);
<> 149:156823d33999 340 *tdes_key ++ = val;
<> 149:156823d33999 341 val = nu_get32_be(key[2] + 0);
<> 149:156823d33999 342 *tdes_key ++ = val;
<> 149:156823d33999 343 val = nu_get32_be(key[2] + 4);
<> 149:156823d33999 344 *tdes_key ++ = val;
<> 149:156823d33999 345
<> 149:156823d33999 346 uint32_t rmn = length;
<> 149:156823d33999 347 const unsigned char *in_pos = input;
<> 149:156823d33999 348 const unsigned char *out_pos = output;
<> 149:156823d33999 349
<> 149:156823d33999 350 while (rmn) {
<> 149:156823d33999 351 uint32_t data_len = (rmn <= MAXSIZE_DMABUF) ? rmn : MAXSIZE_DMABUF;
<> 149:156823d33999 352
<> 149:156823d33999 353 uint32_t ivh, ivl;
<> 149:156823d33999 354 ivh = nu_get32_be(iv);
<> 149:156823d33999 355 ivl = nu_get32_be(iv + 4);
<> 149:156823d33999 356 TDES_SetInitVect(0, ivh, ivl);
<> 149:156823d33999 357
<> 149:156823d33999 358 memcpy(dmabuf_in, in_pos, data_len);
<> 149:156823d33999 359
<> 149:156823d33999 360 TDES_SetDMATransfer(0, (uint32_t) dmabuf_in, (uint32_t) dmabuf_out, data_len);
<> 149:156823d33999 361
<> 149:156823d33999 362 // Start enc/dec.
<> 149:156823d33999 363 // NOTE: Don't call driver function TDES_Start in BSP because it will override TDES_CTL[3KEYS] setting.
<> 149:156823d33999 364 CRPT->TDES_CTL |= CRPT_TDES_CTL_START_Msk | (CRYPTO_DMA_ONE_SHOT << CRPT_TDES_CTL_DMALAST_Pos);
<> 149:156823d33999 365 while (CRPT->TDES_STS & CRPT_TDES_STS_BUSY_Msk);
<> 149:156823d33999 366
<> 149:156823d33999 367 memcpy(out_pos, dmabuf_out, data_len);
<> 149:156823d33999 368 in_pos += data_len;
<> 149:156823d33999 369 out_pos += data_len;
<> 149:156823d33999 370 rmn -= data_len;
<> 149:156823d33999 371
<> 149:156823d33999 372 // Update IV for next block enc/dec in next function call
<> 149:156823d33999 373 switch (tdes_opmode) {
<> 149:156823d33999 374 case DES_MODE_OFB:
<> 149:156823d33999 375 case TDES_MODE_OFB: {
<> 149:156823d33999 376 // OFB: IV (enc/dec) = output block XOR input block
<> 149:156823d33999 377 uint32_t lbh, lbl;
<> 149:156823d33999 378 // Last block of input data
<> 149:156823d33999 379 lbh = nu_get32_be(dmabuf_in + data_len - 8 + 4);
<> 149:156823d33999 380 lbl = nu_get32_be(dmabuf_in + data_len - 8 + 0);
<> 149:156823d33999 381 // Last block of output data
<> 149:156823d33999 382 ivh = nu_get32_be(dmabuf_out + 4);
<> 149:156823d33999 383 ivl = nu_get32_be(dmabuf_out + 0);
<> 149:156823d33999 384 ivh = ivh ^ lbh;
<> 149:156823d33999 385 ivl = ivl ^ lbl;
<> 149:156823d33999 386 nu_set32_be(iv + 4, ivh);
<> 149:156823d33999 387 nu_set32_be(iv, ivl);
<> 149:156823d33999 388 break;
<> 149:156823d33999 389 }
<> 149:156823d33999 390 case DES_MODE_CBC:
<> 149:156823d33999 391 case DES_MODE_CFB:
<> 149:156823d33999 392 case TDES_MODE_CBC:
<> 149:156823d33999 393 case TDES_MODE_CFB: {
<> 149:156823d33999 394 // CBC/CFB: IV (enc) = output block
<> 149:156823d33999 395 // IV (dec) = input block
<> 149:156823d33999 396 if (enc) {
<> 149:156823d33999 397 memcpy(iv, dmabuf_out + data_len - 8, 8);
<> 149:156823d33999 398 }
<> 149:156823d33999 399 else {
<> 149:156823d33999 400 memcpy(iv, dmabuf_in + data_len - 8, 8);
<> 149:156823d33999 401 }
<> 149:156823d33999 402 }
<> 149:156823d33999 403 }
<> 149:156823d33999 404 }
<> 149:156823d33999 405
<> 149:156823d33999 406 return 0;
<> 149:156823d33999 407 }
<> 149:156823d33999 408
<> 149:156823d33999 409 #endif /* MBEDTLS_DES_ALT */
<> 149:156823d33999 410 #endif /* MBEDTLS_DES_C */