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