Mouse code for the MacroRat
mbed-dev/targets/TARGET_NUVOTON/TARGET_NUC472/crypto/aes/aes_alt.c@46:b156ef445742, 2017-06-03 (annotated)
- Committer:
- sahilmgandhi
- Date:
- Sat Jun 03 00:22:44 2017 +0000
- Revision:
- 46:b156ef445742
- Parent:
- 18:6a4db94011d3
Final code for internal battlebot competition.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sahilmgandhi | 18:6a4db94011d3 | 1 | /* mbed Microcontroller Library |
sahilmgandhi | 18:6a4db94011d3 | 2 | * Copyright (c) 2015-2016 Nuvoton |
sahilmgandhi | 18:6a4db94011d3 | 3 | * |
sahilmgandhi | 18:6a4db94011d3 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sahilmgandhi | 18:6a4db94011d3 | 5 | * you may not use this file except in compliance with the License. |
sahilmgandhi | 18:6a4db94011d3 | 6 | * You may obtain a copy of the License at |
sahilmgandhi | 18:6a4db94011d3 | 7 | * |
sahilmgandhi | 18:6a4db94011d3 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
sahilmgandhi | 18:6a4db94011d3 | 9 | * |
sahilmgandhi | 18:6a4db94011d3 | 10 | * Unless required by applicable law or agreed to in writing, software |
sahilmgandhi | 18:6a4db94011d3 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
sahilmgandhi | 18:6a4db94011d3 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sahilmgandhi | 18:6a4db94011d3 | 13 | * See the License for the specific language governing permissions and |
sahilmgandhi | 18:6a4db94011d3 | 14 | * limitations under the License. |
sahilmgandhi | 18:6a4db94011d3 | 15 | */ |
sahilmgandhi | 18:6a4db94011d3 | 16 | |
sahilmgandhi | 18:6a4db94011d3 | 17 | /* |
sahilmgandhi | 18:6a4db94011d3 | 18 | * The AES block cipher was designed by Vincent Rijmen and Joan Daemen. |
sahilmgandhi | 18:6a4db94011d3 | 19 | * |
sahilmgandhi | 18:6a4db94011d3 | 20 | * http://csrc.nist.gov/encryption/aes/rijndael/Rijndael.pdf |
sahilmgandhi | 18:6a4db94011d3 | 21 | * http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf |
sahilmgandhi | 18:6a4db94011d3 | 22 | */ |
sahilmgandhi | 18:6a4db94011d3 | 23 | |
sahilmgandhi | 18:6a4db94011d3 | 24 | /* Compatible with mbed OS 2 which doesn't support mbedtls */ |
sahilmgandhi | 18:6a4db94011d3 | 25 | #if MBED_CONF_RTOS_PRESENT |
sahilmgandhi | 18:6a4db94011d3 | 26 | |
sahilmgandhi | 18:6a4db94011d3 | 27 | #if !defined(MBEDTLS_CONFIG_FILE) |
sahilmgandhi | 18:6a4db94011d3 | 28 | #include "mbedtls/config.h" |
sahilmgandhi | 18:6a4db94011d3 | 29 | #else |
sahilmgandhi | 18:6a4db94011d3 | 30 | #include MBEDTLS_CONFIG_FILE |
sahilmgandhi | 18:6a4db94011d3 | 31 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 32 | |
sahilmgandhi | 18:6a4db94011d3 | 33 | #if defined(MBEDTLS_AES_C) |
sahilmgandhi | 18:6a4db94011d3 | 34 | #if defined(MBEDTLS_AES_ALT) |
sahilmgandhi | 18:6a4db94011d3 | 35 | |
sahilmgandhi | 18:6a4db94011d3 | 36 | #include <string.h> |
sahilmgandhi | 18:6a4db94011d3 | 37 | |
sahilmgandhi | 18:6a4db94011d3 | 38 | #include "mbedtls/aes.h" |
sahilmgandhi | 18:6a4db94011d3 | 39 | |
sahilmgandhi | 18:6a4db94011d3 | 40 | #include "NUC472_442.h" |
sahilmgandhi | 18:6a4db94011d3 | 41 | #include "mbed_toolchain.h" |
sahilmgandhi | 18:6a4db94011d3 | 42 | #include "mbed_assert.h" |
sahilmgandhi | 18:6a4db94011d3 | 43 | |
sahilmgandhi | 18:6a4db94011d3 | 44 | //static int aes_init_done = 0; |
sahilmgandhi | 18:6a4db94011d3 | 45 | |
sahilmgandhi | 18:6a4db94011d3 | 46 | |
sahilmgandhi | 18:6a4db94011d3 | 47 | #define mbedtls_trace(...) //printf(__VA_ARGS__) |
sahilmgandhi | 18:6a4db94011d3 | 48 | |
sahilmgandhi | 18:6a4db94011d3 | 49 | /* Implementation that should never be optimized out by the compiler */ |
sahilmgandhi | 18:6a4db94011d3 | 50 | static void mbedtls_zeroize( void *v, size_t n ) { |
sahilmgandhi | 18:6a4db94011d3 | 51 | volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0; |
sahilmgandhi | 18:6a4db94011d3 | 52 | } |
sahilmgandhi | 18:6a4db94011d3 | 53 | |
sahilmgandhi | 18:6a4db94011d3 | 54 | |
sahilmgandhi | 18:6a4db94011d3 | 55 | static uint32_t au32MyAESIV[4] = { |
sahilmgandhi | 18:6a4db94011d3 | 56 | 0x00000000, 0x00000000, 0x00000000, 0x00000000 |
sahilmgandhi | 18:6a4db94011d3 | 57 | }; |
sahilmgandhi | 18:6a4db94011d3 | 58 | |
sahilmgandhi | 18:6a4db94011d3 | 59 | extern volatile int g_AES_done; |
sahilmgandhi | 18:6a4db94011d3 | 60 | |
sahilmgandhi | 18:6a4db94011d3 | 61 | // Must be a multiple of 16 bytes block size |
sahilmgandhi | 18:6a4db94011d3 | 62 | #define MAX_DMA_CHAIN_SIZE (16*6) |
sahilmgandhi | 18:6a4db94011d3 | 63 | static uint8_t au8OutputData[MAX_DMA_CHAIN_SIZE] MBED_ALIGN(4); |
sahilmgandhi | 18:6a4db94011d3 | 64 | static uint8_t au8InputData[MAX_DMA_CHAIN_SIZE] MBED_ALIGN(4); |
sahilmgandhi | 18:6a4db94011d3 | 65 | |
sahilmgandhi | 18:6a4db94011d3 | 66 | static void dumpHex(const unsigned char au8Data[], int len) |
sahilmgandhi | 18:6a4db94011d3 | 67 | { |
sahilmgandhi | 18:6a4db94011d3 | 68 | int j; |
sahilmgandhi | 18:6a4db94011d3 | 69 | for (j = 0; j < len; j++) mbedtls_trace("%02x ", au8Data[j]); |
sahilmgandhi | 18:6a4db94011d3 | 70 | mbedtls_trace("\r\n"); |
sahilmgandhi | 18:6a4db94011d3 | 71 | } |
sahilmgandhi | 18:6a4db94011d3 | 72 | |
sahilmgandhi | 18:6a4db94011d3 | 73 | static void swapInitVector(unsigned char iv[16]) |
sahilmgandhi | 18:6a4db94011d3 | 74 | { |
sahilmgandhi | 18:6a4db94011d3 | 75 | unsigned int* piv; |
sahilmgandhi | 18:6a4db94011d3 | 76 | int i; |
sahilmgandhi | 18:6a4db94011d3 | 77 | // iv SWAP |
sahilmgandhi | 18:6a4db94011d3 | 78 | piv = (unsigned int*)iv; |
sahilmgandhi | 18:6a4db94011d3 | 79 | for( i=0; i< 4; i++) |
sahilmgandhi | 18:6a4db94011d3 | 80 | { |
sahilmgandhi | 18:6a4db94011d3 | 81 | *piv = (((*piv) & 0x000000FF) << 24) | |
sahilmgandhi | 18:6a4db94011d3 | 82 | (((*piv) & 0x0000FF00) << 8) | |
sahilmgandhi | 18:6a4db94011d3 | 83 | (((*piv) & 0x00FF0000) >> 8) | |
sahilmgandhi | 18:6a4db94011d3 | 84 | (((*piv) & 0xFF000000) >> 24); |
sahilmgandhi | 18:6a4db94011d3 | 85 | piv++; |
sahilmgandhi | 18:6a4db94011d3 | 86 | } |
sahilmgandhi | 18:6a4db94011d3 | 87 | } |
sahilmgandhi | 18:6a4db94011d3 | 88 | |
sahilmgandhi | 18:6a4db94011d3 | 89 | //volatile void CRYPTO_IRQHandler() |
sahilmgandhi | 18:6a4db94011d3 | 90 | //{ |
sahilmgandhi | 18:6a4db94011d3 | 91 | // if (AES_GET_INT_FLAG()) { |
sahilmgandhi | 18:6a4db94011d3 | 92 | // g_AES_done = 1; |
sahilmgandhi | 18:6a4db94011d3 | 93 | // AES_CLR_INT_FLAG(); |
sahilmgandhi | 18:6a4db94011d3 | 94 | // } |
sahilmgandhi | 18:6a4db94011d3 | 95 | //} |
sahilmgandhi | 18:6a4db94011d3 | 96 | |
sahilmgandhi | 18:6a4db94011d3 | 97 | // AES available channel 0~3 |
sahilmgandhi | 18:6a4db94011d3 | 98 | static unsigned char channel_flag[4]={0x00,0x00,0x00,0x00}; // 0: idle, 1: busy |
sahilmgandhi | 18:6a4db94011d3 | 99 | static int channel_alloc() |
sahilmgandhi | 18:6a4db94011d3 | 100 | { |
sahilmgandhi | 18:6a4db94011d3 | 101 | int i; |
sahilmgandhi | 18:6a4db94011d3 | 102 | for(i=0; i< (int)sizeof(channel_flag); i++) |
sahilmgandhi | 18:6a4db94011d3 | 103 | { |
sahilmgandhi | 18:6a4db94011d3 | 104 | if( channel_flag[i] == 0x00 ) |
sahilmgandhi | 18:6a4db94011d3 | 105 | { |
sahilmgandhi | 18:6a4db94011d3 | 106 | channel_flag[i] = 0x01; |
sahilmgandhi | 18:6a4db94011d3 | 107 | return i; |
sahilmgandhi | 18:6a4db94011d3 | 108 | } |
sahilmgandhi | 18:6a4db94011d3 | 109 | } |
sahilmgandhi | 18:6a4db94011d3 | 110 | return(-1); |
sahilmgandhi | 18:6a4db94011d3 | 111 | } |
sahilmgandhi | 18:6a4db94011d3 | 112 | |
sahilmgandhi | 18:6a4db94011d3 | 113 | static void channel_free(int i) |
sahilmgandhi | 18:6a4db94011d3 | 114 | { |
sahilmgandhi | 18:6a4db94011d3 | 115 | if( i >=0 && i < (int)sizeof(channel_flag) ) |
sahilmgandhi | 18:6a4db94011d3 | 116 | channel_flag[i] = 0x00; |
sahilmgandhi | 18:6a4db94011d3 | 117 | } |
sahilmgandhi | 18:6a4db94011d3 | 118 | |
sahilmgandhi | 18:6a4db94011d3 | 119 | |
sahilmgandhi | 18:6a4db94011d3 | 120 | void mbedtls_aes_init( mbedtls_aes_context *ctx ) |
sahilmgandhi | 18:6a4db94011d3 | 121 | { |
sahilmgandhi | 18:6a4db94011d3 | 122 | int i =-1; |
sahilmgandhi | 18:6a4db94011d3 | 123 | |
sahilmgandhi | 18:6a4db94011d3 | 124 | // sw_mbedtls_aes_init(ctx); |
sahilmgandhi | 18:6a4db94011d3 | 125 | // return; |
sahilmgandhi | 18:6a4db94011d3 | 126 | |
sahilmgandhi | 18:6a4db94011d3 | 127 | mbedtls_trace("=== %s \r\n", __FUNCTION__); |
sahilmgandhi | 18:6a4db94011d3 | 128 | memset( ctx, 0, sizeof( mbedtls_aes_context ) ); |
sahilmgandhi | 18:6a4db94011d3 | 129 | |
sahilmgandhi | 18:6a4db94011d3 | 130 | ctx->swapType = AES_IN_OUT_SWAP; |
sahilmgandhi | 18:6a4db94011d3 | 131 | while( (i = channel_alloc()) < 0 ) |
sahilmgandhi | 18:6a4db94011d3 | 132 | { |
sahilmgandhi | 18:6a4db94011d3 | 133 | mbed_assert_internal("No available AES channel", __FILE__, __LINE__); |
sahilmgandhi | 18:6a4db94011d3 | 134 | //osDelay(300); |
sahilmgandhi | 18:6a4db94011d3 | 135 | } |
sahilmgandhi | 18:6a4db94011d3 | 136 | ctx->channel = i; |
sahilmgandhi | 18:6a4db94011d3 | 137 | ctx->iv = au32MyAESIV; |
sahilmgandhi | 18:6a4db94011d3 | 138 | |
sahilmgandhi | 18:6a4db94011d3 | 139 | /* Unlock protected registers */ |
sahilmgandhi | 18:6a4db94011d3 | 140 | SYS_UnlockReg(); |
sahilmgandhi | 18:6a4db94011d3 | 141 | CLK_EnableModuleClock(CRPT_MODULE); |
sahilmgandhi | 18:6a4db94011d3 | 142 | /* Lock protected registers */ |
sahilmgandhi | 18:6a4db94011d3 | 143 | SYS_LockReg(); |
sahilmgandhi | 18:6a4db94011d3 | 144 | |
sahilmgandhi | 18:6a4db94011d3 | 145 | NVIC_EnableIRQ(CRPT_IRQn); |
sahilmgandhi | 18:6a4db94011d3 | 146 | AES_ENABLE_INT(); |
sahilmgandhi | 18:6a4db94011d3 | 147 | mbedtls_trace("=== %s channel[%d]\r\n", __FUNCTION__, (int)ctx->channel); |
sahilmgandhi | 18:6a4db94011d3 | 148 | } |
sahilmgandhi | 18:6a4db94011d3 | 149 | |
sahilmgandhi | 18:6a4db94011d3 | 150 | void mbedtls_aes_free( mbedtls_aes_context *ctx ) |
sahilmgandhi | 18:6a4db94011d3 | 151 | { |
sahilmgandhi | 18:6a4db94011d3 | 152 | |
sahilmgandhi | 18:6a4db94011d3 | 153 | mbedtls_trace("=== %s channel[%d]\r\n", __FUNCTION__,(int)ctx->channel); |
sahilmgandhi | 18:6a4db94011d3 | 154 | |
sahilmgandhi | 18:6a4db94011d3 | 155 | if( ctx == NULL ) |
sahilmgandhi | 18:6a4db94011d3 | 156 | return; |
sahilmgandhi | 18:6a4db94011d3 | 157 | |
sahilmgandhi | 18:6a4db94011d3 | 158 | /* Unlock protected registers */ |
sahilmgandhi | 18:6a4db94011d3 | 159 | // SYS_UnlockReg(); |
sahilmgandhi | 18:6a4db94011d3 | 160 | // CLK_DisableModuleClock(CRPT_MODULE); |
sahilmgandhi | 18:6a4db94011d3 | 161 | /* Lock protected registers */ |
sahilmgandhi | 18:6a4db94011d3 | 162 | // SYS_LockReg(); |
sahilmgandhi | 18:6a4db94011d3 | 163 | |
sahilmgandhi | 18:6a4db94011d3 | 164 | // NVIC_DisableIRQ(CRPT_IRQn); |
sahilmgandhi | 18:6a4db94011d3 | 165 | // AES_DISABLE_INT(); |
sahilmgandhi | 18:6a4db94011d3 | 166 | channel_free(ctx->channel); |
sahilmgandhi | 18:6a4db94011d3 | 167 | mbedtls_zeroize( ctx, sizeof( mbedtls_aes_context ) ); |
sahilmgandhi | 18:6a4db94011d3 | 168 | } |
sahilmgandhi | 18:6a4db94011d3 | 169 | |
sahilmgandhi | 18:6a4db94011d3 | 170 | /* |
sahilmgandhi | 18:6a4db94011d3 | 171 | * AES key schedule (encryption) |
sahilmgandhi | 18:6a4db94011d3 | 172 | */ |
sahilmgandhi | 18:6a4db94011d3 | 173 | #if defined(MBEDTLS_AES_SETKEY_ENC_ALT) |
sahilmgandhi | 18:6a4db94011d3 | 174 | int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key, |
sahilmgandhi | 18:6a4db94011d3 | 175 | unsigned int keybits ) |
sahilmgandhi | 18:6a4db94011d3 | 176 | { |
sahilmgandhi | 18:6a4db94011d3 | 177 | unsigned int i; |
sahilmgandhi | 18:6a4db94011d3 | 178 | |
sahilmgandhi | 18:6a4db94011d3 | 179 | mbedtls_trace("=== %s keybits[%d]\r\n", __FUNCTION__, keybits); |
sahilmgandhi | 18:6a4db94011d3 | 180 | dumpHex(key,keybits/8); |
sahilmgandhi | 18:6a4db94011d3 | 181 | |
sahilmgandhi | 18:6a4db94011d3 | 182 | switch( keybits ) |
sahilmgandhi | 18:6a4db94011d3 | 183 | { |
sahilmgandhi | 18:6a4db94011d3 | 184 | case 128: |
sahilmgandhi | 18:6a4db94011d3 | 185 | ctx->keySize = AES_KEY_SIZE_128; |
sahilmgandhi | 18:6a4db94011d3 | 186 | break; |
sahilmgandhi | 18:6a4db94011d3 | 187 | case 192: |
sahilmgandhi | 18:6a4db94011d3 | 188 | ctx->keySize = AES_KEY_SIZE_192; |
sahilmgandhi | 18:6a4db94011d3 | 189 | break; |
sahilmgandhi | 18:6a4db94011d3 | 190 | case 256: |
sahilmgandhi | 18:6a4db94011d3 | 191 | ctx->keySize = AES_KEY_SIZE_256; |
sahilmgandhi | 18:6a4db94011d3 | 192 | break; |
sahilmgandhi | 18:6a4db94011d3 | 193 | default : return( MBEDTLS_ERR_AES_INVALID_KEY_LENGTH ); |
sahilmgandhi | 18:6a4db94011d3 | 194 | } |
sahilmgandhi | 18:6a4db94011d3 | 195 | |
sahilmgandhi | 18:6a4db94011d3 | 196 | |
sahilmgandhi | 18:6a4db94011d3 | 197 | |
sahilmgandhi | 18:6a4db94011d3 | 198 | // key swap |
sahilmgandhi | 18:6a4db94011d3 | 199 | for( i = 0; i < ( keybits >> 5 ); i++ ) |
sahilmgandhi | 18:6a4db94011d3 | 200 | { |
sahilmgandhi | 18:6a4db94011d3 | 201 | ctx->buf[i] = (*(key+i*4) << 24) | |
sahilmgandhi | 18:6a4db94011d3 | 202 | (*(key+1+i*4) << 16) | |
sahilmgandhi | 18:6a4db94011d3 | 203 | (*(key+2+i*4) << 8) | |
sahilmgandhi | 18:6a4db94011d3 | 204 | (*(key+3+i*4) ); |
sahilmgandhi | 18:6a4db94011d3 | 205 | } |
sahilmgandhi | 18:6a4db94011d3 | 206 | AES_SetKey(ctx->channel, ctx->buf, ctx->keySize); |
sahilmgandhi | 18:6a4db94011d3 | 207 | |
sahilmgandhi | 18:6a4db94011d3 | 208 | |
sahilmgandhi | 18:6a4db94011d3 | 209 | return( 0 ); |
sahilmgandhi | 18:6a4db94011d3 | 210 | } |
sahilmgandhi | 18:6a4db94011d3 | 211 | #endif /* MBEDTLS_AES_SETKEY_ENC_ALT */ |
sahilmgandhi | 18:6a4db94011d3 | 212 | |
sahilmgandhi | 18:6a4db94011d3 | 213 | /* |
sahilmgandhi | 18:6a4db94011d3 | 214 | * AES key schedule (decryption) |
sahilmgandhi | 18:6a4db94011d3 | 215 | */ |
sahilmgandhi | 18:6a4db94011d3 | 216 | #if defined(MBEDTLS_AES_SETKEY_DEC_ALT) |
sahilmgandhi | 18:6a4db94011d3 | 217 | int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key, |
sahilmgandhi | 18:6a4db94011d3 | 218 | unsigned int keybits ) |
sahilmgandhi | 18:6a4db94011d3 | 219 | { |
sahilmgandhi | 18:6a4db94011d3 | 220 | int ret; |
sahilmgandhi | 18:6a4db94011d3 | 221 | |
sahilmgandhi | 18:6a4db94011d3 | 222 | mbedtls_trace("=== %s keybits[%d]\r\n", __FUNCTION__, keybits); |
sahilmgandhi | 18:6a4db94011d3 | 223 | dumpHex((uint8_t *)key,keybits/8); |
sahilmgandhi | 18:6a4db94011d3 | 224 | |
sahilmgandhi | 18:6a4db94011d3 | 225 | /* Also checks keybits */ |
sahilmgandhi | 18:6a4db94011d3 | 226 | if( ( ret = mbedtls_aes_setkey_enc( ctx, key, keybits ) ) != 0 ) |
sahilmgandhi | 18:6a4db94011d3 | 227 | goto exit; |
sahilmgandhi | 18:6a4db94011d3 | 228 | |
sahilmgandhi | 18:6a4db94011d3 | 229 | exit: |
sahilmgandhi | 18:6a4db94011d3 | 230 | |
sahilmgandhi | 18:6a4db94011d3 | 231 | return( ret ); |
sahilmgandhi | 18:6a4db94011d3 | 232 | } |
sahilmgandhi | 18:6a4db94011d3 | 233 | #endif /* MBEDTLS_AES_SETKEY_DEC_ALT */ |
sahilmgandhi | 18:6a4db94011d3 | 234 | |
sahilmgandhi | 18:6a4db94011d3 | 235 | |
sahilmgandhi | 18:6a4db94011d3 | 236 | static void __nvt_aes_crypt( mbedtls_aes_context *ctx, |
sahilmgandhi | 18:6a4db94011d3 | 237 | const unsigned char input[16], |
sahilmgandhi | 18:6a4db94011d3 | 238 | unsigned char output[16], int dataSize) |
sahilmgandhi | 18:6a4db94011d3 | 239 | { |
sahilmgandhi | 18:6a4db94011d3 | 240 | unsigned char* pIn; |
sahilmgandhi | 18:6a4db94011d3 | 241 | unsigned char* pOut; |
sahilmgandhi | 18:6a4db94011d3 | 242 | |
sahilmgandhi | 18:6a4db94011d3 | 243 | // mbedtls_trace("=== %s \r\n", __FUNCTION__); |
sahilmgandhi | 18:6a4db94011d3 | 244 | dumpHex(input,16); |
sahilmgandhi | 18:6a4db94011d3 | 245 | |
sahilmgandhi | 18:6a4db94011d3 | 246 | AES_Open(ctx->channel, ctx->encDec, ctx->opMode, ctx->keySize, ctx->swapType); |
sahilmgandhi | 18:6a4db94011d3 | 247 | AES_SetInitVect(ctx->channel, ctx->iv); |
sahilmgandhi | 18:6a4db94011d3 | 248 | if( ((uint32_t)input) & 0x03 ) |
sahilmgandhi | 18:6a4db94011d3 | 249 | { |
sahilmgandhi | 18:6a4db94011d3 | 250 | memcpy(au8InputData, input, dataSize); |
sahilmgandhi | 18:6a4db94011d3 | 251 | pIn = au8InputData; |
sahilmgandhi | 18:6a4db94011d3 | 252 | }else{ |
sahilmgandhi | 18:6a4db94011d3 | 253 | pIn = (unsigned char*)input; |
sahilmgandhi | 18:6a4db94011d3 | 254 | } |
sahilmgandhi | 18:6a4db94011d3 | 255 | if( (((uint32_t)output) & 0x03) || (dataSize%4)) // HW CFB output byte count must be multiple of word |
sahilmgandhi | 18:6a4db94011d3 | 256 | { |
sahilmgandhi | 18:6a4db94011d3 | 257 | pOut = au8OutputData; |
sahilmgandhi | 18:6a4db94011d3 | 258 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 259 | pOut = output; |
sahilmgandhi | 18:6a4db94011d3 | 260 | } |
sahilmgandhi | 18:6a4db94011d3 | 261 | |
sahilmgandhi | 18:6a4db94011d3 | 262 | AES_SetDMATransfer(ctx->channel, (uint32_t)pIn, (uint32_t)pOut, dataSize); |
sahilmgandhi | 18:6a4db94011d3 | 263 | |
sahilmgandhi | 18:6a4db94011d3 | 264 | g_AES_done = 0; |
sahilmgandhi | 18:6a4db94011d3 | 265 | AES_Start(ctx->channel, CRYPTO_DMA_ONE_SHOT); |
sahilmgandhi | 18:6a4db94011d3 | 266 | while (!g_AES_done); |
sahilmgandhi | 18:6a4db94011d3 | 267 | |
sahilmgandhi | 18:6a4db94011d3 | 268 | if( pOut != output ) memcpy(output, au8OutputData, dataSize); |
sahilmgandhi | 18:6a4db94011d3 | 269 | dumpHex(output,16); |
sahilmgandhi | 18:6a4db94011d3 | 270 | |
sahilmgandhi | 18:6a4db94011d3 | 271 | } |
sahilmgandhi | 18:6a4db94011d3 | 272 | |
sahilmgandhi | 18:6a4db94011d3 | 273 | /* |
sahilmgandhi | 18:6a4db94011d3 | 274 | * AES-ECB block encryption |
sahilmgandhi | 18:6a4db94011d3 | 275 | */ |
sahilmgandhi | 18:6a4db94011d3 | 276 | #if defined(MBEDTLS_AES_ENCRYPT_ALT) |
sahilmgandhi | 18:6a4db94011d3 | 277 | void mbedtls_aes_encrypt( mbedtls_aes_context *ctx, |
sahilmgandhi | 18:6a4db94011d3 | 278 | const unsigned char input[16], |
sahilmgandhi | 18:6a4db94011d3 | 279 | unsigned char output[16] ) |
sahilmgandhi | 18:6a4db94011d3 | 280 | { |
sahilmgandhi | 18:6a4db94011d3 | 281 | |
sahilmgandhi | 18:6a4db94011d3 | 282 | mbedtls_trace("=== %s \r\n", __FUNCTION__); |
sahilmgandhi | 18:6a4db94011d3 | 283 | |
sahilmgandhi | 18:6a4db94011d3 | 284 | ctx->encDec = 1; |
sahilmgandhi | 18:6a4db94011d3 | 285 | __nvt_aes_crypt(ctx, input, output, 16); |
sahilmgandhi | 18:6a4db94011d3 | 286 | |
sahilmgandhi | 18:6a4db94011d3 | 287 | } |
sahilmgandhi | 18:6a4db94011d3 | 288 | #endif /* MBEDTLS_AES_ENCRYPT_ALT */ |
sahilmgandhi | 18:6a4db94011d3 | 289 | |
sahilmgandhi | 18:6a4db94011d3 | 290 | /* |
sahilmgandhi | 18:6a4db94011d3 | 291 | * AES-ECB block decryption |
sahilmgandhi | 18:6a4db94011d3 | 292 | */ |
sahilmgandhi | 18:6a4db94011d3 | 293 | #if defined(MBEDTLS_AES_DECRYPT_ALT) |
sahilmgandhi | 18:6a4db94011d3 | 294 | void mbedtls_aes_decrypt( mbedtls_aes_context *ctx, |
sahilmgandhi | 18:6a4db94011d3 | 295 | const unsigned char input[16], |
sahilmgandhi | 18:6a4db94011d3 | 296 | unsigned char output[16] ) |
sahilmgandhi | 18:6a4db94011d3 | 297 | { |
sahilmgandhi | 18:6a4db94011d3 | 298 | |
sahilmgandhi | 18:6a4db94011d3 | 299 | mbedtls_trace("=== %s \r\n", __FUNCTION__); |
sahilmgandhi | 18:6a4db94011d3 | 300 | |
sahilmgandhi | 18:6a4db94011d3 | 301 | ctx->encDec = 0; |
sahilmgandhi | 18:6a4db94011d3 | 302 | __nvt_aes_crypt(ctx, input, output, 16); |
sahilmgandhi | 18:6a4db94011d3 | 303 | |
sahilmgandhi | 18:6a4db94011d3 | 304 | |
sahilmgandhi | 18:6a4db94011d3 | 305 | } |
sahilmgandhi | 18:6a4db94011d3 | 306 | #endif /* MBEDTLS_AES_DECRYPT_ALT */ |
sahilmgandhi | 18:6a4db94011d3 | 307 | |
sahilmgandhi | 18:6a4db94011d3 | 308 | /* |
sahilmgandhi | 18:6a4db94011d3 | 309 | * AES-ECB block encryption/decryption |
sahilmgandhi | 18:6a4db94011d3 | 310 | */ |
sahilmgandhi | 18:6a4db94011d3 | 311 | int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx, |
sahilmgandhi | 18:6a4db94011d3 | 312 | int mode, |
sahilmgandhi | 18:6a4db94011d3 | 313 | const unsigned char input[16], |
sahilmgandhi | 18:6a4db94011d3 | 314 | unsigned char output[16] ) |
sahilmgandhi | 18:6a4db94011d3 | 315 | { |
sahilmgandhi | 18:6a4db94011d3 | 316 | |
sahilmgandhi | 18:6a4db94011d3 | 317 | mbedtls_trace("=== %s \r\n", __FUNCTION__); |
sahilmgandhi | 18:6a4db94011d3 | 318 | |
sahilmgandhi | 18:6a4db94011d3 | 319 | ctx->opMode = AES_MODE_ECB; |
sahilmgandhi | 18:6a4db94011d3 | 320 | if( mode == MBEDTLS_AES_ENCRYPT ) |
sahilmgandhi | 18:6a4db94011d3 | 321 | mbedtls_aes_encrypt( ctx, input, output ); |
sahilmgandhi | 18:6a4db94011d3 | 322 | else |
sahilmgandhi | 18:6a4db94011d3 | 323 | mbedtls_aes_decrypt( ctx, input, output ); |
sahilmgandhi | 18:6a4db94011d3 | 324 | |
sahilmgandhi | 18:6a4db94011d3 | 325 | |
sahilmgandhi | 18:6a4db94011d3 | 326 | return( 0 ); |
sahilmgandhi | 18:6a4db94011d3 | 327 | } |
sahilmgandhi | 18:6a4db94011d3 | 328 | |
sahilmgandhi | 18:6a4db94011d3 | 329 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
sahilmgandhi | 18:6a4db94011d3 | 330 | /* |
sahilmgandhi | 18:6a4db94011d3 | 331 | * AES-CBC buffer encryption/decryption |
sahilmgandhi | 18:6a4db94011d3 | 332 | */ |
sahilmgandhi | 18:6a4db94011d3 | 333 | int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx, |
sahilmgandhi | 18:6a4db94011d3 | 334 | int mode, |
sahilmgandhi | 18:6a4db94011d3 | 335 | size_t len, |
sahilmgandhi | 18:6a4db94011d3 | 336 | unsigned char iv[16], |
sahilmgandhi | 18:6a4db94011d3 | 337 | const unsigned char *input, |
sahilmgandhi | 18:6a4db94011d3 | 338 | unsigned char *output ) |
sahilmgandhi | 18:6a4db94011d3 | 339 | { |
sahilmgandhi | 18:6a4db94011d3 | 340 | unsigned char temp[16]; |
sahilmgandhi | 18:6a4db94011d3 | 341 | int length = len; |
sahilmgandhi | 18:6a4db94011d3 | 342 | int blockChainLen; |
sahilmgandhi | 18:6a4db94011d3 | 343 | mbedtls_trace("=== %s [0x%x]\r\n", __FUNCTION__,length); |
sahilmgandhi | 18:6a4db94011d3 | 344 | if( length % 16 ) |
sahilmgandhi | 18:6a4db94011d3 | 345 | return( MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH ); |
sahilmgandhi | 18:6a4db94011d3 | 346 | |
sahilmgandhi | 18:6a4db94011d3 | 347 | if( (((uint32_t)input) & 0x03) || (((uint32_t)output) & 0x03) ) |
sahilmgandhi | 18:6a4db94011d3 | 348 | { |
sahilmgandhi | 18:6a4db94011d3 | 349 | blockChainLen = (( length > MAX_DMA_CHAIN_SIZE ) ? MAX_DMA_CHAIN_SIZE : length ); |
sahilmgandhi | 18:6a4db94011d3 | 350 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 351 | blockChainLen = length; |
sahilmgandhi | 18:6a4db94011d3 | 352 | } |
sahilmgandhi | 18:6a4db94011d3 | 353 | |
sahilmgandhi | 18:6a4db94011d3 | 354 | while( length > 0 ) |
sahilmgandhi | 18:6a4db94011d3 | 355 | { |
sahilmgandhi | 18:6a4db94011d3 | 356 | ctx->opMode = AES_MODE_CBC; |
sahilmgandhi | 18:6a4db94011d3 | 357 | swapInitVector(iv); // iv SWAP |
sahilmgandhi | 18:6a4db94011d3 | 358 | ctx->iv = (uint32_t *)iv; |
sahilmgandhi | 18:6a4db94011d3 | 359 | |
sahilmgandhi | 18:6a4db94011d3 | 360 | if( mode == MBEDTLS_AES_ENCRYPT ) |
sahilmgandhi | 18:6a4db94011d3 | 361 | { |
sahilmgandhi | 18:6a4db94011d3 | 362 | ctx->encDec = 1; |
sahilmgandhi | 18:6a4db94011d3 | 363 | __nvt_aes_crypt(ctx, input, output, blockChainLen); |
sahilmgandhi | 18:6a4db94011d3 | 364 | // if( blockChainLen == length ) break; // finish last block chain but still need to prepare next iv for mbedtls_aes_self_test() |
sahilmgandhi | 18:6a4db94011d3 | 365 | memcpy( iv, output+blockChainLen-16, 16 ); |
sahilmgandhi | 18:6a4db94011d3 | 366 | }else{ |
sahilmgandhi | 18:6a4db94011d3 | 367 | memcpy( temp, input+blockChainLen-16, 16 ); |
sahilmgandhi | 18:6a4db94011d3 | 368 | ctx->encDec = 0; |
sahilmgandhi | 18:6a4db94011d3 | 369 | __nvt_aes_crypt(ctx, input, output, blockChainLen); |
sahilmgandhi | 18:6a4db94011d3 | 370 | // if( blockChainLen == length ) break; // finish last block chain but still need to prepare next iv for mbedtls_aes_self_test() |
sahilmgandhi | 18:6a4db94011d3 | 371 | memcpy( iv, temp, 16 ); |
sahilmgandhi | 18:6a4db94011d3 | 372 | } |
sahilmgandhi | 18:6a4db94011d3 | 373 | length -= blockChainLen; |
sahilmgandhi | 18:6a4db94011d3 | 374 | input += blockChainLen; |
sahilmgandhi | 18:6a4db94011d3 | 375 | output += blockChainLen; |
sahilmgandhi | 18:6a4db94011d3 | 376 | if(length < MAX_DMA_CHAIN_SIZE ) blockChainLen = length; // For last remainder block chain |
sahilmgandhi | 18:6a4db94011d3 | 377 | |
sahilmgandhi | 18:6a4db94011d3 | 378 | } |
sahilmgandhi | 18:6a4db94011d3 | 379 | |
sahilmgandhi | 18:6a4db94011d3 | 380 | return( 0 ); |
sahilmgandhi | 18:6a4db94011d3 | 381 | } |
sahilmgandhi | 18:6a4db94011d3 | 382 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
sahilmgandhi | 18:6a4db94011d3 | 383 | |
sahilmgandhi | 18:6a4db94011d3 | 384 | #if defined(MBEDTLS_CIPHER_MODE_CFB) |
sahilmgandhi | 18:6a4db94011d3 | 385 | /* |
sahilmgandhi | 18:6a4db94011d3 | 386 | * AES-CFB128 buffer encryption/decryption |
sahilmgandhi | 18:6a4db94011d3 | 387 | */ |
sahilmgandhi | 18:6a4db94011d3 | 388 | /* Support partial block encryption/decryption */ |
sahilmgandhi | 18:6a4db94011d3 | 389 | static int __nvt_aes_crypt_partial_block_cfb128( mbedtls_aes_context *ctx, |
sahilmgandhi | 18:6a4db94011d3 | 390 | int mode, |
sahilmgandhi | 18:6a4db94011d3 | 391 | size_t length, |
sahilmgandhi | 18:6a4db94011d3 | 392 | size_t *iv_off, |
sahilmgandhi | 18:6a4db94011d3 | 393 | unsigned char iv[16], |
sahilmgandhi | 18:6a4db94011d3 | 394 | const unsigned char *input, |
sahilmgandhi | 18:6a4db94011d3 | 395 | unsigned char *output ) |
sahilmgandhi | 18:6a4db94011d3 | 396 | { |
sahilmgandhi | 18:6a4db94011d3 | 397 | int c; |
sahilmgandhi | 18:6a4db94011d3 | 398 | size_t n = *iv_off; |
sahilmgandhi | 18:6a4db94011d3 | 399 | unsigned char iv_tmp[16]; |
sahilmgandhi | 18:6a4db94011d3 | 400 | mbedtls_trace("=== %s \r\n", __FUNCTION__); |
sahilmgandhi | 18:6a4db94011d3 | 401 | if( mode == MBEDTLS_AES_DECRYPT ) |
sahilmgandhi | 18:6a4db94011d3 | 402 | { |
sahilmgandhi | 18:6a4db94011d3 | 403 | while( length-- ) |
sahilmgandhi | 18:6a4db94011d3 | 404 | { |
sahilmgandhi | 18:6a4db94011d3 | 405 | if( n == 0) |
sahilmgandhi | 18:6a4db94011d3 | 406 | mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv ); |
sahilmgandhi | 18:6a4db94011d3 | 407 | else if( ctx->opMode == AES_MODE_CFB) // For previous cryption is CFB mode |
sahilmgandhi | 18:6a4db94011d3 | 408 | { |
sahilmgandhi | 18:6a4db94011d3 | 409 | memcpy(iv_tmp, iv, n); |
sahilmgandhi | 18:6a4db94011d3 | 410 | mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, ctx->prv_iv, iv ); |
sahilmgandhi | 18:6a4db94011d3 | 411 | memcpy(iv, iv_tmp, n); |
sahilmgandhi | 18:6a4db94011d3 | 412 | } |
sahilmgandhi | 18:6a4db94011d3 | 413 | |
sahilmgandhi | 18:6a4db94011d3 | 414 | c = *input++; |
sahilmgandhi | 18:6a4db94011d3 | 415 | *output++ = (unsigned char)( c ^ iv[n] ); |
sahilmgandhi | 18:6a4db94011d3 | 416 | iv[n] = (unsigned char) c; |
sahilmgandhi | 18:6a4db94011d3 | 417 | |
sahilmgandhi | 18:6a4db94011d3 | 418 | n = ( n + 1 ) & 0x0F; |
sahilmgandhi | 18:6a4db94011d3 | 419 | } |
sahilmgandhi | 18:6a4db94011d3 | 420 | } |
sahilmgandhi | 18:6a4db94011d3 | 421 | else |
sahilmgandhi | 18:6a4db94011d3 | 422 | { |
sahilmgandhi | 18:6a4db94011d3 | 423 | while( length-- ) |
sahilmgandhi | 18:6a4db94011d3 | 424 | { |
sahilmgandhi | 18:6a4db94011d3 | 425 | if( n == 0 ) |
sahilmgandhi | 18:6a4db94011d3 | 426 | mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv ); |
sahilmgandhi | 18:6a4db94011d3 | 427 | else if( ctx->opMode == AES_MODE_CFB) // For previous cryption is CFB mode |
sahilmgandhi | 18:6a4db94011d3 | 428 | { |
sahilmgandhi | 18:6a4db94011d3 | 429 | memcpy(iv_tmp, iv, n); |
sahilmgandhi | 18:6a4db94011d3 | 430 | mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, ctx->prv_iv, iv ); |
sahilmgandhi | 18:6a4db94011d3 | 431 | memcpy(iv, iv_tmp, n); |
sahilmgandhi | 18:6a4db94011d3 | 432 | } |
sahilmgandhi | 18:6a4db94011d3 | 433 | |
sahilmgandhi | 18:6a4db94011d3 | 434 | iv[n] = *output++ = (unsigned char)( iv[n] ^ *input++ ); |
sahilmgandhi | 18:6a4db94011d3 | 435 | |
sahilmgandhi | 18:6a4db94011d3 | 436 | n = ( n + 1 ) & 0x0F; |
sahilmgandhi | 18:6a4db94011d3 | 437 | } |
sahilmgandhi | 18:6a4db94011d3 | 438 | } |
sahilmgandhi | 18:6a4db94011d3 | 439 | |
sahilmgandhi | 18:6a4db94011d3 | 440 | *iv_off = n; |
sahilmgandhi | 18:6a4db94011d3 | 441 | |
sahilmgandhi | 18:6a4db94011d3 | 442 | return( 0 ); |
sahilmgandhi | 18:6a4db94011d3 | 443 | } |
sahilmgandhi | 18:6a4db94011d3 | 444 | |
sahilmgandhi | 18:6a4db94011d3 | 445 | int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx, |
sahilmgandhi | 18:6a4db94011d3 | 446 | int mode, |
sahilmgandhi | 18:6a4db94011d3 | 447 | size_t len, |
sahilmgandhi | 18:6a4db94011d3 | 448 | size_t *iv_off, |
sahilmgandhi | 18:6a4db94011d3 | 449 | unsigned char iv[16], |
sahilmgandhi | 18:6a4db94011d3 | 450 | const unsigned char *input, |
sahilmgandhi | 18:6a4db94011d3 | 451 | unsigned char *output ) |
sahilmgandhi | 18:6a4db94011d3 | 452 | { |
sahilmgandhi | 18:6a4db94011d3 | 453 | size_t n = *iv_off; |
sahilmgandhi | 18:6a4db94011d3 | 454 | unsigned char temp[16]; |
sahilmgandhi | 18:6a4db94011d3 | 455 | int length=len; |
sahilmgandhi | 18:6a4db94011d3 | 456 | int blockChainLen; |
sahilmgandhi | 18:6a4db94011d3 | 457 | int remLen=0; |
sahilmgandhi | 18:6a4db94011d3 | 458 | int ivLen; |
sahilmgandhi | 18:6a4db94011d3 | 459 | |
sahilmgandhi | 18:6a4db94011d3 | 460 | mbedtls_trace("=== %s \r\n", __FUNCTION__); |
sahilmgandhi | 18:6a4db94011d3 | 461 | |
sahilmgandhi | 18:6a4db94011d3 | 462 | // proceed: start with partial block by ECB mode first |
sahilmgandhi | 18:6a4db94011d3 | 463 | if( n !=0 ) { |
sahilmgandhi | 18:6a4db94011d3 | 464 | __nvt_aes_crypt_partial_block_cfb128(ctx, mode, 16 - n , iv_off, iv, input, output); |
sahilmgandhi | 18:6a4db94011d3 | 465 | input += (16 - n); |
sahilmgandhi | 18:6a4db94011d3 | 466 | output += (16 - n); |
sahilmgandhi | 18:6a4db94011d3 | 467 | length -= (16 - n); |
sahilmgandhi | 18:6a4db94011d3 | 468 | } |
sahilmgandhi | 18:6a4db94011d3 | 469 | |
sahilmgandhi | 18:6a4db94011d3 | 470 | // For address or byte count non-word alignment, go through reserved DMA buffer. |
sahilmgandhi | 18:6a4db94011d3 | 471 | if( (((uint32_t)input) & 0x03) || (((uint32_t)output) & 0x03) ) // Must reserved DMA buffer for each block |
sahilmgandhi | 18:6a4db94011d3 | 472 | { |
sahilmgandhi | 18:6a4db94011d3 | 473 | blockChainLen = (( length > MAX_DMA_CHAIN_SIZE ) ? MAX_DMA_CHAIN_SIZE : length ); |
sahilmgandhi | 18:6a4db94011d3 | 474 | } else if(length%4) { // Need reserved DMA buffer once for last chain |
sahilmgandhi | 18:6a4db94011d3 | 475 | blockChainLen = (( length > MAX_DMA_CHAIN_SIZE ) ? (length - length%16) : length ); |
sahilmgandhi | 18:6a4db94011d3 | 476 | } else { // Not need reserved DMA buffer |
sahilmgandhi | 18:6a4db94011d3 | 477 | blockChainLen = length; |
sahilmgandhi | 18:6a4db94011d3 | 478 | } |
sahilmgandhi | 18:6a4db94011d3 | 479 | |
sahilmgandhi | 18:6a4db94011d3 | 480 | // proceed: start with block alignment |
sahilmgandhi | 18:6a4db94011d3 | 481 | while( length > 0 ) |
sahilmgandhi | 18:6a4db94011d3 | 482 | { |
sahilmgandhi | 18:6a4db94011d3 | 483 | |
sahilmgandhi | 18:6a4db94011d3 | 484 | ctx->opMode = AES_MODE_CFB; |
sahilmgandhi | 18:6a4db94011d3 | 485 | |
sahilmgandhi | 18:6a4db94011d3 | 486 | swapInitVector(iv); // iv SWAP |
sahilmgandhi | 18:6a4db94011d3 | 487 | |
sahilmgandhi | 18:6a4db94011d3 | 488 | ctx->iv = (uint32_t *)iv; |
sahilmgandhi | 18:6a4db94011d3 | 489 | remLen = blockChainLen%16; |
sahilmgandhi | 18:6a4db94011d3 | 490 | ivLen = (( remLen > 0) ? remLen: 16 ); |
sahilmgandhi | 18:6a4db94011d3 | 491 | |
sahilmgandhi | 18:6a4db94011d3 | 492 | if( mode == MBEDTLS_AES_DECRYPT ) |
sahilmgandhi | 18:6a4db94011d3 | 493 | { |
sahilmgandhi | 18:6a4db94011d3 | 494 | memcpy(temp, input+blockChainLen - ivLen, ivLen); |
sahilmgandhi | 18:6a4db94011d3 | 495 | if(blockChainLen >= 16) memcpy(ctx->prv_iv, input+blockChainLen-remLen-16 , 16); |
sahilmgandhi | 18:6a4db94011d3 | 496 | ctx->encDec = 0; |
sahilmgandhi | 18:6a4db94011d3 | 497 | __nvt_aes_crypt(ctx, input, output, blockChainLen); |
sahilmgandhi | 18:6a4db94011d3 | 498 | memcpy(iv,temp, ivLen); |
sahilmgandhi | 18:6a4db94011d3 | 499 | } |
sahilmgandhi | 18:6a4db94011d3 | 500 | else |
sahilmgandhi | 18:6a4db94011d3 | 501 | { |
sahilmgandhi | 18:6a4db94011d3 | 502 | ctx->encDec = 1; |
sahilmgandhi | 18:6a4db94011d3 | 503 | __nvt_aes_crypt(ctx, input, output, blockChainLen); |
sahilmgandhi | 18:6a4db94011d3 | 504 | if(blockChainLen >= 16) memcpy(ctx->prv_iv, output+blockChainLen-remLen-16 , 16); |
sahilmgandhi | 18:6a4db94011d3 | 505 | memcpy(iv,output+blockChainLen-ivLen,ivLen); |
sahilmgandhi | 18:6a4db94011d3 | 506 | } |
sahilmgandhi | 18:6a4db94011d3 | 507 | length -= blockChainLen; |
sahilmgandhi | 18:6a4db94011d3 | 508 | input += blockChainLen; |
sahilmgandhi | 18:6a4db94011d3 | 509 | output += blockChainLen; |
sahilmgandhi | 18:6a4db94011d3 | 510 | if(length < MAX_DMA_CHAIN_SIZE ) blockChainLen = length; // For last remainder block chain |
sahilmgandhi | 18:6a4db94011d3 | 511 | } |
sahilmgandhi | 18:6a4db94011d3 | 512 | |
sahilmgandhi | 18:6a4db94011d3 | 513 | *iv_off = remLen; |
sahilmgandhi | 18:6a4db94011d3 | 514 | |
sahilmgandhi | 18:6a4db94011d3 | 515 | return( 0 ); |
sahilmgandhi | 18:6a4db94011d3 | 516 | } |
sahilmgandhi | 18:6a4db94011d3 | 517 | |
sahilmgandhi | 18:6a4db94011d3 | 518 | |
sahilmgandhi | 18:6a4db94011d3 | 519 | /* |
sahilmgandhi | 18:6a4db94011d3 | 520 | * AES-CFB8 buffer encryption/decryption |
sahilmgandhi | 18:6a4db94011d3 | 521 | */ |
sahilmgandhi | 18:6a4db94011d3 | 522 | int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx, |
sahilmgandhi | 18:6a4db94011d3 | 523 | int mode, |
sahilmgandhi | 18:6a4db94011d3 | 524 | size_t length, |
sahilmgandhi | 18:6a4db94011d3 | 525 | unsigned char iv[16], |
sahilmgandhi | 18:6a4db94011d3 | 526 | const unsigned char *input, |
sahilmgandhi | 18:6a4db94011d3 | 527 | unsigned char *output ) |
sahilmgandhi | 18:6a4db94011d3 | 528 | { |
sahilmgandhi | 18:6a4db94011d3 | 529 | unsigned char c; |
sahilmgandhi | 18:6a4db94011d3 | 530 | unsigned char ov[17]; |
sahilmgandhi | 18:6a4db94011d3 | 531 | |
sahilmgandhi | 18:6a4db94011d3 | 532 | mbedtls_trace("=== %s \r\n", __FUNCTION__); |
sahilmgandhi | 18:6a4db94011d3 | 533 | while( length-- ) |
sahilmgandhi | 18:6a4db94011d3 | 534 | { |
sahilmgandhi | 18:6a4db94011d3 | 535 | memcpy( ov, iv, 16 ); |
sahilmgandhi | 18:6a4db94011d3 | 536 | mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv ); |
sahilmgandhi | 18:6a4db94011d3 | 537 | |
sahilmgandhi | 18:6a4db94011d3 | 538 | if( mode == MBEDTLS_AES_DECRYPT ) |
sahilmgandhi | 18:6a4db94011d3 | 539 | ov[16] = *input; |
sahilmgandhi | 18:6a4db94011d3 | 540 | |
sahilmgandhi | 18:6a4db94011d3 | 541 | c = *output++ = (unsigned char)( iv[0] ^ *input++ ); |
sahilmgandhi | 18:6a4db94011d3 | 542 | |
sahilmgandhi | 18:6a4db94011d3 | 543 | if( mode == MBEDTLS_AES_ENCRYPT ) |
sahilmgandhi | 18:6a4db94011d3 | 544 | ov[16] = c; |
sahilmgandhi | 18:6a4db94011d3 | 545 | |
sahilmgandhi | 18:6a4db94011d3 | 546 | memcpy( iv, ov + 1, 16 ); |
sahilmgandhi | 18:6a4db94011d3 | 547 | } |
sahilmgandhi | 18:6a4db94011d3 | 548 | |
sahilmgandhi | 18:6a4db94011d3 | 549 | return( 0 ); |
sahilmgandhi | 18:6a4db94011d3 | 550 | } |
sahilmgandhi | 18:6a4db94011d3 | 551 | #endif /*MBEDTLS_CIPHER_MODE_CFB */ |
sahilmgandhi | 18:6a4db94011d3 | 552 | |
sahilmgandhi | 18:6a4db94011d3 | 553 | #if defined(MBEDTLS_CIPHER_MODE_CTR) |
sahilmgandhi | 18:6a4db94011d3 | 554 | /* |
sahilmgandhi | 18:6a4db94011d3 | 555 | * AES-CTR buffer encryption/decryption |
sahilmgandhi | 18:6a4db94011d3 | 556 | */ |
sahilmgandhi | 18:6a4db94011d3 | 557 | int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx, |
sahilmgandhi | 18:6a4db94011d3 | 558 | size_t length, |
sahilmgandhi | 18:6a4db94011d3 | 559 | size_t *nc_off, |
sahilmgandhi | 18:6a4db94011d3 | 560 | unsigned char nonce_counter[16], |
sahilmgandhi | 18:6a4db94011d3 | 561 | unsigned char stream_block[16], |
sahilmgandhi | 18:6a4db94011d3 | 562 | const unsigned char *input, |
sahilmgandhi | 18:6a4db94011d3 | 563 | unsigned char *output ) |
sahilmgandhi | 18:6a4db94011d3 | 564 | { |
sahilmgandhi | 18:6a4db94011d3 | 565 | int c, i; |
sahilmgandhi | 18:6a4db94011d3 | 566 | size_t n = *nc_off; |
sahilmgandhi | 18:6a4db94011d3 | 567 | |
sahilmgandhi | 18:6a4db94011d3 | 568 | mbedtls_trace("=== %s \r\n", __FUNCTION__); |
sahilmgandhi | 18:6a4db94011d3 | 569 | while( length-- ) |
sahilmgandhi | 18:6a4db94011d3 | 570 | { |
sahilmgandhi | 18:6a4db94011d3 | 571 | if( n == 0 ) { |
sahilmgandhi | 18:6a4db94011d3 | 572 | mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, nonce_counter, stream_block ); |
sahilmgandhi | 18:6a4db94011d3 | 573 | |
sahilmgandhi | 18:6a4db94011d3 | 574 | for( i = 16; i > 0; i-- ) |
sahilmgandhi | 18:6a4db94011d3 | 575 | if( ++nonce_counter[i - 1] != 0 ) |
sahilmgandhi | 18:6a4db94011d3 | 576 | break; |
sahilmgandhi | 18:6a4db94011d3 | 577 | } |
sahilmgandhi | 18:6a4db94011d3 | 578 | c = *input++; |
sahilmgandhi | 18:6a4db94011d3 | 579 | *output++ = (unsigned char)( c ^ stream_block[n] ); |
sahilmgandhi | 18:6a4db94011d3 | 580 | |
sahilmgandhi | 18:6a4db94011d3 | 581 | n = ( n + 1 ) & 0x0F; |
sahilmgandhi | 18:6a4db94011d3 | 582 | } |
sahilmgandhi | 18:6a4db94011d3 | 583 | |
sahilmgandhi | 18:6a4db94011d3 | 584 | *nc_off = n; |
sahilmgandhi | 18:6a4db94011d3 | 585 | |
sahilmgandhi | 18:6a4db94011d3 | 586 | return( 0 ); |
sahilmgandhi | 18:6a4db94011d3 | 587 | } |
sahilmgandhi | 18:6a4db94011d3 | 588 | #endif /* MBEDTLS_CIPHER_MODE_CTR */ |
sahilmgandhi | 18:6a4db94011d3 | 589 | |
sahilmgandhi | 18:6a4db94011d3 | 590 | #endif /* MBEDTLS_AES_ALT */ |
sahilmgandhi | 18:6a4db94011d3 | 591 | |
sahilmgandhi | 18:6a4db94011d3 | 592 | |
sahilmgandhi | 18:6a4db94011d3 | 593 | #endif /* MBEDTLS_AES_C */ |
sahilmgandhi | 18:6a4db94011d3 | 594 | |
sahilmgandhi | 18:6a4db94011d3 | 595 | #endif /* MBED_CONF_RTOS_PRESENT */ |