I added functionality to get the RSSI, BER, and Cell Neighbor for reporting connection issues to M2X
Dependencies: WncControllerK64F
mbedtls/source/des.c@18:198e9b0acf11, 2016-11-17 (annotated)
- Committer:
- JMF
- Date:
- Thu Nov 17 16:13:29 2016 +0000
- Revision:
- 18:198e9b0acf11
- Parent:
- 12:0071cb144c7a
Updates to mbed os resulted in mutex.h going away and rtos.h needed to be used; This fixes the Mutex typedef failure. Also cast data buffers from 'char *' to (const std::uint8_t*) to conform with Fred's changes in WncController
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JMF | 12:0071cb144c7a | 1 | /* |
JMF | 12:0071cb144c7a | 2 | * FIPS-46-3 compliant Triple-DES implementation |
JMF | 12:0071cb144c7a | 3 | * |
JMF | 12:0071cb144c7a | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
JMF | 12:0071cb144c7a | 5 | * SPDX-License-Identifier: Apache-2.0 |
JMF | 12:0071cb144c7a | 6 | * |
JMF | 12:0071cb144c7a | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
JMF | 12:0071cb144c7a | 8 | * not use this file except in compliance with the License. |
JMF | 12:0071cb144c7a | 9 | * You may obtain a copy of the License at |
JMF | 12:0071cb144c7a | 10 | * |
JMF | 12:0071cb144c7a | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
JMF | 12:0071cb144c7a | 12 | * |
JMF | 12:0071cb144c7a | 13 | * Unless required by applicable law or agreed to in writing, software |
JMF | 12:0071cb144c7a | 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
JMF | 12:0071cb144c7a | 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
JMF | 12:0071cb144c7a | 16 | * See the License for the specific language governing permissions and |
JMF | 12:0071cb144c7a | 17 | * limitations under the License. |
JMF | 12:0071cb144c7a | 18 | * |
JMF | 12:0071cb144c7a | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
JMF | 12:0071cb144c7a | 20 | */ |
JMF | 12:0071cb144c7a | 21 | /* |
JMF | 12:0071cb144c7a | 22 | * DES, on which TDES is based, was originally designed by Horst Feistel |
JMF | 12:0071cb144c7a | 23 | * at IBM in 1974, and was adopted as a standard by NIST (formerly NBS). |
JMF | 12:0071cb144c7a | 24 | * |
JMF | 12:0071cb144c7a | 25 | * http://csrc.nist.gov/publications/fips/fips46-3/fips46-3.pdf |
JMF | 12:0071cb144c7a | 26 | */ |
JMF | 12:0071cb144c7a | 27 | |
JMF | 12:0071cb144c7a | 28 | #if !defined(MBEDTLS_CONFIG_FILE) |
JMF | 12:0071cb144c7a | 29 | #include "mbedtls/config.h" |
JMF | 12:0071cb144c7a | 30 | #else |
JMF | 12:0071cb144c7a | 31 | #include MBEDTLS_CONFIG_FILE |
JMF | 12:0071cb144c7a | 32 | #endif |
JMF | 12:0071cb144c7a | 33 | |
JMF | 12:0071cb144c7a | 34 | #if defined(MBEDTLS_DES_C) |
JMF | 12:0071cb144c7a | 35 | |
JMF | 12:0071cb144c7a | 36 | #include "mbedtls/des.h" |
JMF | 12:0071cb144c7a | 37 | |
JMF | 12:0071cb144c7a | 38 | #include <string.h> |
JMF | 12:0071cb144c7a | 39 | |
JMF | 12:0071cb144c7a | 40 | #if defined(MBEDTLS_SELF_TEST) |
JMF | 12:0071cb144c7a | 41 | #if defined(MBEDTLS_PLATFORM_C) |
JMF | 12:0071cb144c7a | 42 | #include "mbedtls/platform.h" |
JMF | 12:0071cb144c7a | 43 | #else |
JMF | 12:0071cb144c7a | 44 | #include <stdio.h> |
JMF | 12:0071cb144c7a | 45 | #define mbedtls_printf pc.printf |
JMF | 12:0071cb144c7a | 46 | #endif /* MBEDTLS_PLATFORM_C */ |
JMF | 12:0071cb144c7a | 47 | #endif /* MBEDTLS_SELF_TEST */ |
JMF | 12:0071cb144c7a | 48 | |
JMF | 12:0071cb144c7a | 49 | |
JMF | 12:0071cb144c7a | 50 | #if !defined(MBEDTLS_DES_ALT) |
JMF | 12:0071cb144c7a | 51 | |
JMF | 12:0071cb144c7a | 52 | /* Implementation that should never be optimized out by the compiler */ |
JMF | 12:0071cb144c7a | 53 | static void mbedtls_zeroize( void *v, size_t n ) { |
JMF | 12:0071cb144c7a | 54 | volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0; |
JMF | 12:0071cb144c7a | 55 | } |
JMF | 12:0071cb144c7a | 56 | |
JMF | 12:0071cb144c7a | 57 | /* |
JMF | 12:0071cb144c7a | 58 | * 32-bit integer manipulation macros (big endian) |
JMF | 12:0071cb144c7a | 59 | */ |
JMF | 12:0071cb144c7a | 60 | #ifndef GET_UINT32_BE |
JMF | 12:0071cb144c7a | 61 | #define GET_UINT32_BE(n,b,i) \ |
JMF | 12:0071cb144c7a | 62 | { \ |
JMF | 12:0071cb144c7a | 63 | (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ |
JMF | 12:0071cb144c7a | 64 | | ( (uint32_t) (b)[(i) + 1] << 16 ) \ |
JMF | 12:0071cb144c7a | 65 | | ( (uint32_t) (b)[(i) + 2] << 8 ) \ |
JMF | 12:0071cb144c7a | 66 | | ( (uint32_t) (b)[(i) + 3] ); \ |
JMF | 12:0071cb144c7a | 67 | } |
JMF | 12:0071cb144c7a | 68 | #endif |
JMF | 12:0071cb144c7a | 69 | |
JMF | 12:0071cb144c7a | 70 | #ifndef PUT_UINT32_BE |
JMF | 12:0071cb144c7a | 71 | #define PUT_UINT32_BE(n,b,i) \ |
JMF | 12:0071cb144c7a | 72 | { \ |
JMF | 12:0071cb144c7a | 73 | (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ |
JMF | 12:0071cb144c7a | 74 | (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ |
JMF | 12:0071cb144c7a | 75 | (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ |
JMF | 12:0071cb144c7a | 76 | (b)[(i) + 3] = (unsigned char) ( (n) ); \ |
JMF | 12:0071cb144c7a | 77 | } |
JMF | 12:0071cb144c7a | 78 | #endif |
JMF | 12:0071cb144c7a | 79 | |
JMF | 12:0071cb144c7a | 80 | /* |
JMF | 12:0071cb144c7a | 81 | * Expanded DES S-boxes |
JMF | 12:0071cb144c7a | 82 | */ |
JMF | 12:0071cb144c7a | 83 | static const uint32_t SB1[64] = |
JMF | 12:0071cb144c7a | 84 | { |
JMF | 12:0071cb144c7a | 85 | 0x01010400, 0x00000000, 0x00010000, 0x01010404, |
JMF | 12:0071cb144c7a | 86 | 0x01010004, 0x00010404, 0x00000004, 0x00010000, |
JMF | 12:0071cb144c7a | 87 | 0x00000400, 0x01010400, 0x01010404, 0x00000400, |
JMF | 12:0071cb144c7a | 88 | 0x01000404, 0x01010004, 0x01000000, 0x00000004, |
JMF | 12:0071cb144c7a | 89 | 0x00000404, 0x01000400, 0x01000400, 0x00010400, |
JMF | 12:0071cb144c7a | 90 | 0x00010400, 0x01010000, 0x01010000, 0x01000404, |
JMF | 12:0071cb144c7a | 91 | 0x00010004, 0x01000004, 0x01000004, 0x00010004, |
JMF | 12:0071cb144c7a | 92 | 0x00000000, 0x00000404, 0x00010404, 0x01000000, |
JMF | 12:0071cb144c7a | 93 | 0x00010000, 0x01010404, 0x00000004, 0x01010000, |
JMF | 12:0071cb144c7a | 94 | 0x01010400, 0x01000000, 0x01000000, 0x00000400, |
JMF | 12:0071cb144c7a | 95 | 0x01010004, 0x00010000, 0x00010400, 0x01000004, |
JMF | 12:0071cb144c7a | 96 | 0x00000400, 0x00000004, 0x01000404, 0x00010404, |
JMF | 12:0071cb144c7a | 97 | 0x01010404, 0x00010004, 0x01010000, 0x01000404, |
JMF | 12:0071cb144c7a | 98 | 0x01000004, 0x00000404, 0x00010404, 0x01010400, |
JMF | 12:0071cb144c7a | 99 | 0x00000404, 0x01000400, 0x01000400, 0x00000000, |
JMF | 12:0071cb144c7a | 100 | 0x00010004, 0x00010400, 0x00000000, 0x01010004 |
JMF | 12:0071cb144c7a | 101 | }; |
JMF | 12:0071cb144c7a | 102 | |
JMF | 12:0071cb144c7a | 103 | static const uint32_t SB2[64] = |
JMF | 12:0071cb144c7a | 104 | { |
JMF | 12:0071cb144c7a | 105 | 0x80108020, 0x80008000, 0x00008000, 0x00108020, |
JMF | 12:0071cb144c7a | 106 | 0x00100000, 0x00000020, 0x80100020, 0x80008020, |
JMF | 12:0071cb144c7a | 107 | 0x80000020, 0x80108020, 0x80108000, 0x80000000, |
JMF | 12:0071cb144c7a | 108 | 0x80008000, 0x00100000, 0x00000020, 0x80100020, |
JMF | 12:0071cb144c7a | 109 | 0x00108000, 0x00100020, 0x80008020, 0x00000000, |
JMF | 12:0071cb144c7a | 110 | 0x80000000, 0x00008000, 0x00108020, 0x80100000, |
JMF | 12:0071cb144c7a | 111 | 0x00100020, 0x80000020, 0x00000000, 0x00108000, |
JMF | 12:0071cb144c7a | 112 | 0x00008020, 0x80108000, 0x80100000, 0x00008020, |
JMF | 12:0071cb144c7a | 113 | 0x00000000, 0x00108020, 0x80100020, 0x00100000, |
JMF | 12:0071cb144c7a | 114 | 0x80008020, 0x80100000, 0x80108000, 0x00008000, |
JMF | 12:0071cb144c7a | 115 | 0x80100000, 0x80008000, 0x00000020, 0x80108020, |
JMF | 12:0071cb144c7a | 116 | 0x00108020, 0x00000020, 0x00008000, 0x80000000, |
JMF | 12:0071cb144c7a | 117 | 0x00008020, 0x80108000, 0x00100000, 0x80000020, |
JMF | 12:0071cb144c7a | 118 | 0x00100020, 0x80008020, 0x80000020, 0x00100020, |
JMF | 12:0071cb144c7a | 119 | 0x00108000, 0x00000000, 0x80008000, 0x00008020, |
JMF | 12:0071cb144c7a | 120 | 0x80000000, 0x80100020, 0x80108020, 0x00108000 |
JMF | 12:0071cb144c7a | 121 | }; |
JMF | 12:0071cb144c7a | 122 | |
JMF | 12:0071cb144c7a | 123 | static const uint32_t SB3[64] = |
JMF | 12:0071cb144c7a | 124 | { |
JMF | 12:0071cb144c7a | 125 | 0x00000208, 0x08020200, 0x00000000, 0x08020008, |
JMF | 12:0071cb144c7a | 126 | 0x08000200, 0x00000000, 0x00020208, 0x08000200, |
JMF | 12:0071cb144c7a | 127 | 0x00020008, 0x08000008, 0x08000008, 0x00020000, |
JMF | 12:0071cb144c7a | 128 | 0x08020208, 0x00020008, 0x08020000, 0x00000208, |
JMF | 12:0071cb144c7a | 129 | 0x08000000, 0x00000008, 0x08020200, 0x00000200, |
JMF | 12:0071cb144c7a | 130 | 0x00020200, 0x08020000, 0x08020008, 0x00020208, |
JMF | 12:0071cb144c7a | 131 | 0x08000208, 0x00020200, 0x00020000, 0x08000208, |
JMF | 12:0071cb144c7a | 132 | 0x00000008, 0x08020208, 0x00000200, 0x08000000, |
JMF | 12:0071cb144c7a | 133 | 0x08020200, 0x08000000, 0x00020008, 0x00000208, |
JMF | 12:0071cb144c7a | 134 | 0x00020000, 0x08020200, 0x08000200, 0x00000000, |
JMF | 12:0071cb144c7a | 135 | 0x00000200, 0x00020008, 0x08020208, 0x08000200, |
JMF | 12:0071cb144c7a | 136 | 0x08000008, 0x00000200, 0x00000000, 0x08020008, |
JMF | 12:0071cb144c7a | 137 | 0x08000208, 0x00020000, 0x08000000, 0x08020208, |
JMF | 12:0071cb144c7a | 138 | 0x00000008, 0x00020208, 0x00020200, 0x08000008, |
JMF | 12:0071cb144c7a | 139 | 0x08020000, 0x08000208, 0x00000208, 0x08020000, |
JMF | 12:0071cb144c7a | 140 | 0x00020208, 0x00000008, 0x08020008, 0x00020200 |
JMF | 12:0071cb144c7a | 141 | }; |
JMF | 12:0071cb144c7a | 142 | |
JMF | 12:0071cb144c7a | 143 | static const uint32_t SB4[64] = |
JMF | 12:0071cb144c7a | 144 | { |
JMF | 12:0071cb144c7a | 145 | 0x00802001, 0x00002081, 0x00002081, 0x00000080, |
JMF | 12:0071cb144c7a | 146 | 0x00802080, 0x00800081, 0x00800001, 0x00002001, |
JMF | 12:0071cb144c7a | 147 | 0x00000000, 0x00802000, 0x00802000, 0x00802081, |
JMF | 12:0071cb144c7a | 148 | 0x00000081, 0x00000000, 0x00800080, 0x00800001, |
JMF | 12:0071cb144c7a | 149 | 0x00000001, 0x00002000, 0x00800000, 0x00802001, |
JMF | 12:0071cb144c7a | 150 | 0x00000080, 0x00800000, 0x00002001, 0x00002080, |
JMF | 12:0071cb144c7a | 151 | 0x00800081, 0x00000001, 0x00002080, 0x00800080, |
JMF | 12:0071cb144c7a | 152 | 0x00002000, 0x00802080, 0x00802081, 0x00000081, |
JMF | 12:0071cb144c7a | 153 | 0x00800080, 0x00800001, 0x00802000, 0x00802081, |
JMF | 12:0071cb144c7a | 154 | 0x00000081, 0x00000000, 0x00000000, 0x00802000, |
JMF | 12:0071cb144c7a | 155 | 0x00002080, 0x00800080, 0x00800081, 0x00000001, |
JMF | 12:0071cb144c7a | 156 | 0x00802001, 0x00002081, 0x00002081, 0x00000080, |
JMF | 12:0071cb144c7a | 157 | 0x00802081, 0x00000081, 0x00000001, 0x00002000, |
JMF | 12:0071cb144c7a | 158 | 0x00800001, 0x00002001, 0x00802080, 0x00800081, |
JMF | 12:0071cb144c7a | 159 | 0x00002001, 0x00002080, 0x00800000, 0x00802001, |
JMF | 12:0071cb144c7a | 160 | 0x00000080, 0x00800000, 0x00002000, 0x00802080 |
JMF | 12:0071cb144c7a | 161 | }; |
JMF | 12:0071cb144c7a | 162 | |
JMF | 12:0071cb144c7a | 163 | static const uint32_t SB5[64] = |
JMF | 12:0071cb144c7a | 164 | { |
JMF | 12:0071cb144c7a | 165 | 0x00000100, 0x02080100, 0x02080000, 0x42000100, |
JMF | 12:0071cb144c7a | 166 | 0x00080000, 0x00000100, 0x40000000, 0x02080000, |
JMF | 12:0071cb144c7a | 167 | 0x40080100, 0x00080000, 0x02000100, 0x40080100, |
JMF | 12:0071cb144c7a | 168 | 0x42000100, 0x42080000, 0x00080100, 0x40000000, |
JMF | 12:0071cb144c7a | 169 | 0x02000000, 0x40080000, 0x40080000, 0x00000000, |
JMF | 12:0071cb144c7a | 170 | 0x40000100, 0x42080100, 0x42080100, 0x02000100, |
JMF | 12:0071cb144c7a | 171 | 0x42080000, 0x40000100, 0x00000000, 0x42000000, |
JMF | 12:0071cb144c7a | 172 | 0x02080100, 0x02000000, 0x42000000, 0x00080100, |
JMF | 12:0071cb144c7a | 173 | 0x00080000, 0x42000100, 0x00000100, 0x02000000, |
JMF | 12:0071cb144c7a | 174 | 0x40000000, 0x02080000, 0x42000100, 0x40080100, |
JMF | 12:0071cb144c7a | 175 | 0x02000100, 0x40000000, 0x42080000, 0x02080100, |
JMF | 12:0071cb144c7a | 176 | 0x40080100, 0x00000100, 0x02000000, 0x42080000, |
JMF | 12:0071cb144c7a | 177 | 0x42080100, 0x00080100, 0x42000000, 0x42080100, |
JMF | 12:0071cb144c7a | 178 | 0x02080000, 0x00000000, 0x40080000, 0x42000000, |
JMF | 12:0071cb144c7a | 179 | 0x00080100, 0x02000100, 0x40000100, 0x00080000, |
JMF | 12:0071cb144c7a | 180 | 0x00000000, 0x40080000, 0x02080100, 0x40000100 |
JMF | 12:0071cb144c7a | 181 | }; |
JMF | 12:0071cb144c7a | 182 | |
JMF | 12:0071cb144c7a | 183 | static const uint32_t SB6[64] = |
JMF | 12:0071cb144c7a | 184 | { |
JMF | 12:0071cb144c7a | 185 | 0x20000010, 0x20400000, 0x00004000, 0x20404010, |
JMF | 12:0071cb144c7a | 186 | 0x20400000, 0x00000010, 0x20404010, 0x00400000, |
JMF | 12:0071cb144c7a | 187 | 0x20004000, 0x00404010, 0x00400000, 0x20000010, |
JMF | 12:0071cb144c7a | 188 | 0x00400010, 0x20004000, 0x20000000, 0x00004010, |
JMF | 12:0071cb144c7a | 189 | 0x00000000, 0x00400010, 0x20004010, 0x00004000, |
JMF | 12:0071cb144c7a | 190 | 0x00404000, 0x20004010, 0x00000010, 0x20400010, |
JMF | 12:0071cb144c7a | 191 | 0x20400010, 0x00000000, 0x00404010, 0x20404000, |
JMF | 12:0071cb144c7a | 192 | 0x00004010, 0x00404000, 0x20404000, 0x20000000, |
JMF | 12:0071cb144c7a | 193 | 0x20004000, 0x00000010, 0x20400010, 0x00404000, |
JMF | 12:0071cb144c7a | 194 | 0x20404010, 0x00400000, 0x00004010, 0x20000010, |
JMF | 12:0071cb144c7a | 195 | 0x00400000, 0x20004000, 0x20000000, 0x00004010, |
JMF | 12:0071cb144c7a | 196 | 0x20000010, 0x20404010, 0x00404000, 0x20400000, |
JMF | 12:0071cb144c7a | 197 | 0x00404010, 0x20404000, 0x00000000, 0x20400010, |
JMF | 12:0071cb144c7a | 198 | 0x00000010, 0x00004000, 0x20400000, 0x00404010, |
JMF | 12:0071cb144c7a | 199 | 0x00004000, 0x00400010, 0x20004010, 0x00000000, |
JMF | 12:0071cb144c7a | 200 | 0x20404000, 0x20000000, 0x00400010, 0x20004010 |
JMF | 12:0071cb144c7a | 201 | }; |
JMF | 12:0071cb144c7a | 202 | |
JMF | 12:0071cb144c7a | 203 | static const uint32_t SB7[64] = |
JMF | 12:0071cb144c7a | 204 | { |
JMF | 12:0071cb144c7a | 205 | 0x00200000, 0x04200002, 0x04000802, 0x00000000, |
JMF | 12:0071cb144c7a | 206 | 0x00000800, 0x04000802, 0x00200802, 0x04200800, |
JMF | 12:0071cb144c7a | 207 | 0x04200802, 0x00200000, 0x00000000, 0x04000002, |
JMF | 12:0071cb144c7a | 208 | 0x00000002, 0x04000000, 0x04200002, 0x00000802, |
JMF | 12:0071cb144c7a | 209 | 0x04000800, 0x00200802, 0x00200002, 0x04000800, |
JMF | 12:0071cb144c7a | 210 | 0x04000002, 0x04200000, 0x04200800, 0x00200002, |
JMF | 12:0071cb144c7a | 211 | 0x04200000, 0x00000800, 0x00000802, 0x04200802, |
JMF | 12:0071cb144c7a | 212 | 0x00200800, 0x00000002, 0x04000000, 0x00200800, |
JMF | 12:0071cb144c7a | 213 | 0x04000000, 0x00200800, 0x00200000, 0x04000802, |
JMF | 12:0071cb144c7a | 214 | 0x04000802, 0x04200002, 0x04200002, 0x00000002, |
JMF | 12:0071cb144c7a | 215 | 0x00200002, 0x04000000, 0x04000800, 0x00200000, |
JMF | 12:0071cb144c7a | 216 | 0x04200800, 0x00000802, 0x00200802, 0x04200800, |
JMF | 12:0071cb144c7a | 217 | 0x00000802, 0x04000002, 0x04200802, 0x04200000, |
JMF | 12:0071cb144c7a | 218 | 0x00200800, 0x00000000, 0x00000002, 0x04200802, |
JMF | 12:0071cb144c7a | 219 | 0x00000000, 0x00200802, 0x04200000, 0x00000800, |
JMF | 12:0071cb144c7a | 220 | 0x04000002, 0x04000800, 0x00000800, 0x00200002 |
JMF | 12:0071cb144c7a | 221 | }; |
JMF | 12:0071cb144c7a | 222 | |
JMF | 12:0071cb144c7a | 223 | static const uint32_t SB8[64] = |
JMF | 12:0071cb144c7a | 224 | { |
JMF | 12:0071cb144c7a | 225 | 0x10001040, 0x00001000, 0x00040000, 0x10041040, |
JMF | 12:0071cb144c7a | 226 | 0x10000000, 0x10001040, 0x00000040, 0x10000000, |
JMF | 12:0071cb144c7a | 227 | 0x00040040, 0x10040000, 0x10041040, 0x00041000, |
JMF | 12:0071cb144c7a | 228 | 0x10041000, 0x00041040, 0x00001000, 0x00000040, |
JMF | 12:0071cb144c7a | 229 | 0x10040000, 0x10000040, 0x10001000, 0x00001040, |
JMF | 12:0071cb144c7a | 230 | 0x00041000, 0x00040040, 0x10040040, 0x10041000, |
JMF | 12:0071cb144c7a | 231 | 0x00001040, 0x00000000, 0x00000000, 0x10040040, |
JMF | 12:0071cb144c7a | 232 | 0x10000040, 0x10001000, 0x00041040, 0x00040000, |
JMF | 12:0071cb144c7a | 233 | 0x00041040, 0x00040000, 0x10041000, 0x00001000, |
JMF | 12:0071cb144c7a | 234 | 0x00000040, 0x10040040, 0x00001000, 0x00041040, |
JMF | 12:0071cb144c7a | 235 | 0x10001000, 0x00000040, 0x10000040, 0x10040000, |
JMF | 12:0071cb144c7a | 236 | 0x10040040, 0x10000000, 0x00040000, 0x10001040, |
JMF | 12:0071cb144c7a | 237 | 0x00000000, 0x10041040, 0x00040040, 0x10000040, |
JMF | 12:0071cb144c7a | 238 | 0x10040000, 0x10001000, 0x10001040, 0x00000000, |
JMF | 12:0071cb144c7a | 239 | 0x10041040, 0x00041000, 0x00041000, 0x00001040, |
JMF | 12:0071cb144c7a | 240 | 0x00001040, 0x00040040, 0x10000000, 0x10041000 |
JMF | 12:0071cb144c7a | 241 | }; |
JMF | 12:0071cb144c7a | 242 | |
JMF | 12:0071cb144c7a | 243 | /* |
JMF | 12:0071cb144c7a | 244 | * PC1: left and right halves bit-swap |
JMF | 12:0071cb144c7a | 245 | */ |
JMF | 12:0071cb144c7a | 246 | static const uint32_t LHs[16] = |
JMF | 12:0071cb144c7a | 247 | { |
JMF | 12:0071cb144c7a | 248 | 0x00000000, 0x00000001, 0x00000100, 0x00000101, |
JMF | 12:0071cb144c7a | 249 | 0x00010000, 0x00010001, 0x00010100, 0x00010101, |
JMF | 12:0071cb144c7a | 250 | 0x01000000, 0x01000001, 0x01000100, 0x01000101, |
JMF | 12:0071cb144c7a | 251 | 0x01010000, 0x01010001, 0x01010100, 0x01010101 |
JMF | 12:0071cb144c7a | 252 | }; |
JMF | 12:0071cb144c7a | 253 | |
JMF | 12:0071cb144c7a | 254 | static const uint32_t RHs[16] = |
JMF | 12:0071cb144c7a | 255 | { |
JMF | 12:0071cb144c7a | 256 | 0x00000000, 0x01000000, 0x00010000, 0x01010000, |
JMF | 12:0071cb144c7a | 257 | 0x00000100, 0x01000100, 0x00010100, 0x01010100, |
JMF | 12:0071cb144c7a | 258 | 0x00000001, 0x01000001, 0x00010001, 0x01010001, |
JMF | 12:0071cb144c7a | 259 | 0x00000101, 0x01000101, 0x00010101, 0x01010101, |
JMF | 12:0071cb144c7a | 260 | }; |
JMF | 12:0071cb144c7a | 261 | |
JMF | 12:0071cb144c7a | 262 | /* |
JMF | 12:0071cb144c7a | 263 | * Initial Permutation macro |
JMF | 12:0071cb144c7a | 264 | */ |
JMF | 12:0071cb144c7a | 265 | #define DES_IP(X,Y) \ |
JMF | 12:0071cb144c7a | 266 | { \ |
JMF | 12:0071cb144c7a | 267 | T = ((X >> 4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T << 4); \ |
JMF | 12:0071cb144c7a | 268 | T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16); \ |
JMF | 12:0071cb144c7a | 269 | T = ((Y >> 2) ^ X) & 0x33333333; X ^= T; Y ^= (T << 2); \ |
JMF | 12:0071cb144c7a | 270 | T = ((Y >> 8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T << 8); \ |
JMF | 12:0071cb144c7a | 271 | Y = ((Y << 1) | (Y >> 31)) & 0xFFFFFFFF; \ |
JMF | 12:0071cb144c7a | 272 | T = (X ^ Y) & 0xAAAAAAAA; Y ^= T; X ^= T; \ |
JMF | 12:0071cb144c7a | 273 | X = ((X << 1) | (X >> 31)) & 0xFFFFFFFF; \ |
JMF | 12:0071cb144c7a | 274 | } |
JMF | 12:0071cb144c7a | 275 | |
JMF | 12:0071cb144c7a | 276 | /* |
JMF | 12:0071cb144c7a | 277 | * Final Permutation macro |
JMF | 12:0071cb144c7a | 278 | */ |
JMF | 12:0071cb144c7a | 279 | #define DES_FP(X,Y) \ |
JMF | 12:0071cb144c7a | 280 | { \ |
JMF | 12:0071cb144c7a | 281 | X = ((X << 31) | (X >> 1)) & 0xFFFFFFFF; \ |
JMF | 12:0071cb144c7a | 282 | T = (X ^ Y) & 0xAAAAAAAA; X ^= T; Y ^= T; \ |
JMF | 12:0071cb144c7a | 283 | Y = ((Y << 31) | (Y >> 1)) & 0xFFFFFFFF; \ |
JMF | 12:0071cb144c7a | 284 | T = ((Y >> 8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T << 8); \ |
JMF | 12:0071cb144c7a | 285 | T = ((Y >> 2) ^ X) & 0x33333333; X ^= T; Y ^= (T << 2); \ |
JMF | 12:0071cb144c7a | 286 | T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16); \ |
JMF | 12:0071cb144c7a | 287 | T = ((X >> 4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T << 4); \ |
JMF | 12:0071cb144c7a | 288 | } |
JMF | 12:0071cb144c7a | 289 | |
JMF | 12:0071cb144c7a | 290 | /* |
JMF | 12:0071cb144c7a | 291 | * DES round macro |
JMF | 12:0071cb144c7a | 292 | */ |
JMF | 12:0071cb144c7a | 293 | #define DES_ROUND(X,Y) \ |
JMF | 12:0071cb144c7a | 294 | { \ |
JMF | 12:0071cb144c7a | 295 | T = *SK++ ^ X; \ |
JMF | 12:0071cb144c7a | 296 | Y ^= SB8[ (T ) & 0x3F ] ^ \ |
JMF | 12:0071cb144c7a | 297 | SB6[ (T >> 8) & 0x3F ] ^ \ |
JMF | 12:0071cb144c7a | 298 | SB4[ (T >> 16) & 0x3F ] ^ \ |
JMF | 12:0071cb144c7a | 299 | SB2[ (T >> 24) & 0x3F ]; \ |
JMF | 12:0071cb144c7a | 300 | \ |
JMF | 12:0071cb144c7a | 301 | T = *SK++ ^ ((X << 28) | (X >> 4)); \ |
JMF | 12:0071cb144c7a | 302 | Y ^= SB7[ (T ) & 0x3F ] ^ \ |
JMF | 12:0071cb144c7a | 303 | SB5[ (T >> 8) & 0x3F ] ^ \ |
JMF | 12:0071cb144c7a | 304 | SB3[ (T >> 16) & 0x3F ] ^ \ |
JMF | 12:0071cb144c7a | 305 | SB1[ (T >> 24) & 0x3F ]; \ |
JMF | 12:0071cb144c7a | 306 | } |
JMF | 12:0071cb144c7a | 307 | |
JMF | 12:0071cb144c7a | 308 | #define SWAP(a,b) { uint32_t t = a; a = b; b = t; t = 0; } |
JMF | 12:0071cb144c7a | 309 | |
JMF | 12:0071cb144c7a | 310 | void mbedtls_des_init( mbedtls_des_context *ctx ) |
JMF | 12:0071cb144c7a | 311 | { |
JMF | 12:0071cb144c7a | 312 | memset( ctx, 0, sizeof( mbedtls_des_context ) ); |
JMF | 12:0071cb144c7a | 313 | } |
JMF | 12:0071cb144c7a | 314 | |
JMF | 12:0071cb144c7a | 315 | void mbedtls_des_free( mbedtls_des_context *ctx ) |
JMF | 12:0071cb144c7a | 316 | { |
JMF | 12:0071cb144c7a | 317 | if( ctx == NULL ) |
JMF | 12:0071cb144c7a | 318 | return; |
JMF | 12:0071cb144c7a | 319 | |
JMF | 12:0071cb144c7a | 320 | mbedtls_zeroize( ctx, sizeof( mbedtls_des_context ) ); |
JMF | 12:0071cb144c7a | 321 | } |
JMF | 12:0071cb144c7a | 322 | |
JMF | 12:0071cb144c7a | 323 | void mbedtls_des3_init( mbedtls_des3_context *ctx ) |
JMF | 12:0071cb144c7a | 324 | { |
JMF | 12:0071cb144c7a | 325 | memset( ctx, 0, sizeof( mbedtls_des3_context ) ); |
JMF | 12:0071cb144c7a | 326 | } |
JMF | 12:0071cb144c7a | 327 | |
JMF | 12:0071cb144c7a | 328 | void mbedtls_des3_free( mbedtls_des3_context *ctx ) |
JMF | 12:0071cb144c7a | 329 | { |
JMF | 12:0071cb144c7a | 330 | if( ctx == NULL ) |
JMF | 12:0071cb144c7a | 331 | return; |
JMF | 12:0071cb144c7a | 332 | |
JMF | 12:0071cb144c7a | 333 | mbedtls_zeroize( ctx, sizeof( mbedtls_des3_context ) ); |
JMF | 12:0071cb144c7a | 334 | } |
JMF | 12:0071cb144c7a | 335 | |
JMF | 12:0071cb144c7a | 336 | static const unsigned char odd_parity_table[128] = { 1, 2, 4, 7, 8, |
JMF | 12:0071cb144c7a | 337 | 11, 13, 14, 16, 19, 21, 22, 25, 26, 28, 31, 32, 35, 37, 38, 41, 42, 44, |
JMF | 12:0071cb144c7a | 338 | 47, 49, 50, 52, 55, 56, 59, 61, 62, 64, 67, 69, 70, 73, 74, 76, 79, 81, |
JMF | 12:0071cb144c7a | 339 | 82, 84, 87, 88, 91, 93, 94, 97, 98, 100, 103, 104, 107, 109, 110, 112, |
JMF | 12:0071cb144c7a | 340 | 115, 117, 118, 121, 122, 124, 127, 128, 131, 133, 134, 137, 138, 140, |
JMF | 12:0071cb144c7a | 341 | 143, 145, 146, 148, 151, 152, 155, 157, 158, 161, 162, 164, 167, 168, |
JMF | 12:0071cb144c7a | 342 | 171, 173, 174, 176, 179, 181, 182, 185, 186, 188, 191, 193, 194, 196, |
JMF | 12:0071cb144c7a | 343 | 199, 200, 203, 205, 206, 208, 211, 213, 214, 217, 218, 220, 223, 224, |
JMF | 12:0071cb144c7a | 344 | 227, 229, 230, 233, 234, 236, 239, 241, 242, 244, 247, 248, 251, 253, |
JMF | 12:0071cb144c7a | 345 | 254 }; |
JMF | 12:0071cb144c7a | 346 | |
JMF | 12:0071cb144c7a | 347 | void mbedtls_des_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] ) |
JMF | 12:0071cb144c7a | 348 | { |
JMF | 12:0071cb144c7a | 349 | int i; |
JMF | 12:0071cb144c7a | 350 | |
JMF | 12:0071cb144c7a | 351 | for( i = 0; i < MBEDTLS_DES_KEY_SIZE; i++ ) |
JMF | 12:0071cb144c7a | 352 | key[i] = odd_parity_table[key[i] / 2]; |
JMF | 12:0071cb144c7a | 353 | } |
JMF | 12:0071cb144c7a | 354 | |
JMF | 12:0071cb144c7a | 355 | /* |
JMF | 12:0071cb144c7a | 356 | * Check the given key's parity, returns 1 on failure, 0 on SUCCESS |
JMF | 12:0071cb144c7a | 357 | */ |
JMF | 12:0071cb144c7a | 358 | int mbedtls_des_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SIZE] ) |
JMF | 12:0071cb144c7a | 359 | { |
JMF | 12:0071cb144c7a | 360 | int i; |
JMF | 12:0071cb144c7a | 361 | |
JMF | 12:0071cb144c7a | 362 | for( i = 0; i < MBEDTLS_DES_KEY_SIZE; i++ ) |
JMF | 12:0071cb144c7a | 363 | if( key[i] != odd_parity_table[key[i] / 2] ) |
JMF | 12:0071cb144c7a | 364 | return( 1 ); |
JMF | 12:0071cb144c7a | 365 | |
JMF | 12:0071cb144c7a | 366 | return( 0 ); |
JMF | 12:0071cb144c7a | 367 | } |
JMF | 12:0071cb144c7a | 368 | |
JMF | 12:0071cb144c7a | 369 | /* |
JMF | 12:0071cb144c7a | 370 | * Table of weak and semi-weak keys |
JMF | 12:0071cb144c7a | 371 | * |
JMF | 12:0071cb144c7a | 372 | * Source: http://en.wikipedia.org/wiki/Weak_key |
JMF | 12:0071cb144c7a | 373 | * |
JMF | 12:0071cb144c7a | 374 | * Weak: |
JMF | 12:0071cb144c7a | 375 | * Alternating ones + zeros (0x0101010101010101) |
JMF | 12:0071cb144c7a | 376 | * Alternating 'F' + 'E' (0xFEFEFEFEFEFEFEFE) |
JMF | 12:0071cb144c7a | 377 | * '0xE0E0E0E0F1F1F1F1' |
JMF | 12:0071cb144c7a | 378 | * '0x1F1F1F1F0E0E0E0E' |
JMF | 12:0071cb144c7a | 379 | * |
JMF | 12:0071cb144c7a | 380 | * Semi-weak: |
JMF | 12:0071cb144c7a | 381 | * 0x011F011F010E010E and 0x1F011F010E010E01 |
JMF | 12:0071cb144c7a | 382 | * 0x01E001E001F101F1 and 0xE001E001F101F101 |
JMF | 12:0071cb144c7a | 383 | * 0x01FE01FE01FE01FE and 0xFE01FE01FE01FE01 |
JMF | 12:0071cb144c7a | 384 | * 0x1FE01FE00EF10EF1 and 0xE01FE01FF10EF10E |
JMF | 12:0071cb144c7a | 385 | * 0x1FFE1FFE0EFE0EFE and 0xFE1FFE1FFE0EFE0E |
JMF | 12:0071cb144c7a | 386 | * 0xE0FEE0FEF1FEF1FE and 0xFEE0FEE0FEF1FEF1 |
JMF | 12:0071cb144c7a | 387 | * |
JMF | 12:0071cb144c7a | 388 | */ |
JMF | 12:0071cb144c7a | 389 | |
JMF | 12:0071cb144c7a | 390 | #define WEAK_KEY_COUNT 16 |
JMF | 12:0071cb144c7a | 391 | |
JMF | 12:0071cb144c7a | 392 | static const unsigned char weak_key_table[WEAK_KEY_COUNT][MBEDTLS_DES_KEY_SIZE] = |
JMF | 12:0071cb144c7a | 393 | { |
JMF | 12:0071cb144c7a | 394 | { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 }, |
JMF | 12:0071cb144c7a | 395 | { 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE }, |
JMF | 12:0071cb144c7a | 396 | { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E }, |
JMF | 12:0071cb144c7a | 397 | { 0xE0, 0xE0, 0xE0, 0xE0, 0xF1, 0xF1, 0xF1, 0xF1 }, |
JMF | 12:0071cb144c7a | 398 | |
JMF | 12:0071cb144c7a | 399 | { 0x01, 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E }, |
JMF | 12:0071cb144c7a | 400 | { 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E, 0x01 }, |
JMF | 12:0071cb144c7a | 401 | { 0x01, 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1 }, |
JMF | 12:0071cb144c7a | 402 | { 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1, 0x01 }, |
JMF | 12:0071cb144c7a | 403 | { 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE }, |
JMF | 12:0071cb144c7a | 404 | { 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01 }, |
JMF | 12:0071cb144c7a | 405 | { 0x1F, 0xE0, 0x1F, 0xE0, 0x0E, 0xF1, 0x0E, 0xF1 }, |
JMF | 12:0071cb144c7a | 406 | { 0xE0, 0x1F, 0xE0, 0x1F, 0xF1, 0x0E, 0xF1, 0x0E }, |
JMF | 12:0071cb144c7a | 407 | { 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE }, |
JMF | 12:0071cb144c7a | 408 | { 0xFE, 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E }, |
JMF | 12:0071cb144c7a | 409 | { 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE }, |
JMF | 12:0071cb144c7a | 410 | { 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1 } |
JMF | 12:0071cb144c7a | 411 | }; |
JMF | 12:0071cb144c7a | 412 | |
JMF | 12:0071cb144c7a | 413 | int mbedtls_des_key_check_weak( const unsigned char key[MBEDTLS_DES_KEY_SIZE] ) |
JMF | 12:0071cb144c7a | 414 | { |
JMF | 12:0071cb144c7a | 415 | int i; |
JMF | 12:0071cb144c7a | 416 | |
JMF | 12:0071cb144c7a | 417 | for( i = 0; i < WEAK_KEY_COUNT; i++ ) |
JMF | 12:0071cb144c7a | 418 | if( memcmp( weak_key_table[i], key, MBEDTLS_DES_KEY_SIZE) == 0 ) |
JMF | 12:0071cb144c7a | 419 | return( 1 ); |
JMF | 12:0071cb144c7a | 420 | |
JMF | 12:0071cb144c7a | 421 | return( 0 ); |
JMF | 12:0071cb144c7a | 422 | } |
JMF | 12:0071cb144c7a | 423 | |
JMF | 12:0071cb144c7a | 424 | #if !defined(MBEDTLS_DES_SETKEY_ALT) |
JMF | 12:0071cb144c7a | 425 | void mbedtls_des_setkey( uint32_t SK[32], const unsigned char key[MBEDTLS_DES_KEY_SIZE] ) |
JMF | 12:0071cb144c7a | 426 | { |
JMF | 12:0071cb144c7a | 427 | int i; |
JMF | 12:0071cb144c7a | 428 | uint32_t X, Y, T; |
JMF | 12:0071cb144c7a | 429 | |
JMF | 12:0071cb144c7a | 430 | GET_UINT32_BE( X, key, 0 ); |
JMF | 12:0071cb144c7a | 431 | GET_UINT32_BE( Y, key, 4 ); |
JMF | 12:0071cb144c7a | 432 | |
JMF | 12:0071cb144c7a | 433 | /* |
JMF | 12:0071cb144c7a | 434 | * Permuted Choice 1 |
JMF | 12:0071cb144c7a | 435 | */ |
JMF | 12:0071cb144c7a | 436 | T = ((Y >> 4) ^ X) & 0x0F0F0F0F; X ^= T; Y ^= (T << 4); |
JMF | 12:0071cb144c7a | 437 | T = ((Y ) ^ X) & 0x10101010; X ^= T; Y ^= (T ); |
JMF | 12:0071cb144c7a | 438 | |
JMF | 12:0071cb144c7a | 439 | X = (LHs[ (X ) & 0xF] << 3) | (LHs[ (X >> 8) & 0xF ] << 2) |
JMF | 12:0071cb144c7a | 440 | | (LHs[ (X >> 16) & 0xF] << 1) | (LHs[ (X >> 24) & 0xF ] ) |
JMF | 12:0071cb144c7a | 441 | | (LHs[ (X >> 5) & 0xF] << 7) | (LHs[ (X >> 13) & 0xF ] << 6) |
JMF | 12:0071cb144c7a | 442 | | (LHs[ (X >> 21) & 0xF] << 5) | (LHs[ (X >> 29) & 0xF ] << 4); |
JMF | 12:0071cb144c7a | 443 | |
JMF | 12:0071cb144c7a | 444 | Y = (RHs[ (Y >> 1) & 0xF] << 3) | (RHs[ (Y >> 9) & 0xF ] << 2) |
JMF | 12:0071cb144c7a | 445 | | (RHs[ (Y >> 17) & 0xF] << 1) | (RHs[ (Y >> 25) & 0xF ] ) |
JMF | 12:0071cb144c7a | 446 | | (RHs[ (Y >> 4) & 0xF] << 7) | (RHs[ (Y >> 12) & 0xF ] << 6) |
JMF | 12:0071cb144c7a | 447 | | (RHs[ (Y >> 20) & 0xF] << 5) | (RHs[ (Y >> 28) & 0xF ] << 4); |
JMF | 12:0071cb144c7a | 448 | |
JMF | 12:0071cb144c7a | 449 | X &= 0x0FFFFFFF; |
JMF | 12:0071cb144c7a | 450 | Y &= 0x0FFFFFFF; |
JMF | 12:0071cb144c7a | 451 | |
JMF | 12:0071cb144c7a | 452 | /* |
JMF | 12:0071cb144c7a | 453 | * calculate subkeys |
JMF | 12:0071cb144c7a | 454 | */ |
JMF | 12:0071cb144c7a | 455 | for( i = 0; i < 16; i++ ) |
JMF | 12:0071cb144c7a | 456 | { |
JMF | 12:0071cb144c7a | 457 | if( i < 2 || i == 8 || i == 15 ) |
JMF | 12:0071cb144c7a | 458 | { |
JMF | 12:0071cb144c7a | 459 | X = ((X << 1) | (X >> 27)) & 0x0FFFFFFF; |
JMF | 12:0071cb144c7a | 460 | Y = ((Y << 1) | (Y >> 27)) & 0x0FFFFFFF; |
JMF | 12:0071cb144c7a | 461 | } |
JMF | 12:0071cb144c7a | 462 | else |
JMF | 12:0071cb144c7a | 463 | { |
JMF | 12:0071cb144c7a | 464 | X = ((X << 2) | (X >> 26)) & 0x0FFFFFFF; |
JMF | 12:0071cb144c7a | 465 | Y = ((Y << 2) | (Y >> 26)) & 0x0FFFFFFF; |
JMF | 12:0071cb144c7a | 466 | } |
JMF | 12:0071cb144c7a | 467 | |
JMF | 12:0071cb144c7a | 468 | *SK++ = ((X << 4) & 0x24000000) | ((X << 28) & 0x10000000) |
JMF | 12:0071cb144c7a | 469 | | ((X << 14) & 0x08000000) | ((X << 18) & 0x02080000) |
JMF | 12:0071cb144c7a | 470 | | ((X << 6) & 0x01000000) | ((X << 9) & 0x00200000) |
JMF | 12:0071cb144c7a | 471 | | ((X >> 1) & 0x00100000) | ((X << 10) & 0x00040000) |
JMF | 12:0071cb144c7a | 472 | | ((X << 2) & 0x00020000) | ((X >> 10) & 0x00010000) |
JMF | 12:0071cb144c7a | 473 | | ((Y >> 13) & 0x00002000) | ((Y >> 4) & 0x00001000) |
JMF | 12:0071cb144c7a | 474 | | ((Y << 6) & 0x00000800) | ((Y >> 1) & 0x00000400) |
JMF | 12:0071cb144c7a | 475 | | ((Y >> 14) & 0x00000200) | ((Y ) & 0x00000100) |
JMF | 12:0071cb144c7a | 476 | | ((Y >> 5) & 0x00000020) | ((Y >> 10) & 0x00000010) |
JMF | 12:0071cb144c7a | 477 | | ((Y >> 3) & 0x00000008) | ((Y >> 18) & 0x00000004) |
JMF | 12:0071cb144c7a | 478 | | ((Y >> 26) & 0x00000002) | ((Y >> 24) & 0x00000001); |
JMF | 12:0071cb144c7a | 479 | |
JMF | 12:0071cb144c7a | 480 | *SK++ = ((X << 15) & 0x20000000) | ((X << 17) & 0x10000000) |
JMF | 12:0071cb144c7a | 481 | | ((X << 10) & 0x08000000) | ((X << 22) & 0x04000000) |
JMF | 12:0071cb144c7a | 482 | | ((X >> 2) & 0x02000000) | ((X << 1) & 0x01000000) |
JMF | 12:0071cb144c7a | 483 | | ((X << 16) & 0x00200000) | ((X << 11) & 0x00100000) |
JMF | 12:0071cb144c7a | 484 | | ((X << 3) & 0x00080000) | ((X >> 6) & 0x00040000) |
JMF | 12:0071cb144c7a | 485 | | ((X << 15) & 0x00020000) | ((X >> 4) & 0x00010000) |
JMF | 12:0071cb144c7a | 486 | | ((Y >> 2) & 0x00002000) | ((Y << 8) & 0x00001000) |
JMF | 12:0071cb144c7a | 487 | | ((Y >> 14) & 0x00000808) | ((Y >> 9) & 0x00000400) |
JMF | 12:0071cb144c7a | 488 | | ((Y ) & 0x00000200) | ((Y << 7) & 0x00000100) |
JMF | 12:0071cb144c7a | 489 | | ((Y >> 7) & 0x00000020) | ((Y >> 3) & 0x00000011) |
JMF | 12:0071cb144c7a | 490 | | ((Y << 2) & 0x00000004) | ((Y >> 21) & 0x00000002); |
JMF | 12:0071cb144c7a | 491 | } |
JMF | 12:0071cb144c7a | 492 | } |
JMF | 12:0071cb144c7a | 493 | #endif /* !MBEDTLS_DES_SETKEY_ALT */ |
JMF | 12:0071cb144c7a | 494 | |
JMF | 12:0071cb144c7a | 495 | /* |
JMF | 12:0071cb144c7a | 496 | * DES key schedule (56-bit, encryption) |
JMF | 12:0071cb144c7a | 497 | */ |
JMF | 12:0071cb144c7a | 498 | int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] ) |
JMF | 12:0071cb144c7a | 499 | { |
JMF | 12:0071cb144c7a | 500 | mbedtls_des_setkey( ctx->sk, key ); |
JMF | 12:0071cb144c7a | 501 | |
JMF | 12:0071cb144c7a | 502 | return( 0 ); |
JMF | 12:0071cb144c7a | 503 | } |
JMF | 12:0071cb144c7a | 504 | |
JMF | 12:0071cb144c7a | 505 | /* |
JMF | 12:0071cb144c7a | 506 | * DES key schedule (56-bit, decryption) |
JMF | 12:0071cb144c7a | 507 | */ |
JMF | 12:0071cb144c7a | 508 | int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] ) |
JMF | 12:0071cb144c7a | 509 | { |
JMF | 12:0071cb144c7a | 510 | int i; |
JMF | 12:0071cb144c7a | 511 | |
JMF | 12:0071cb144c7a | 512 | mbedtls_des_setkey( ctx->sk, key ); |
JMF | 12:0071cb144c7a | 513 | |
JMF | 12:0071cb144c7a | 514 | for( i = 0; i < 16; i += 2 ) |
JMF | 12:0071cb144c7a | 515 | { |
JMF | 12:0071cb144c7a | 516 | SWAP( ctx->sk[i ], ctx->sk[30 - i] ); |
JMF | 12:0071cb144c7a | 517 | SWAP( ctx->sk[i + 1], ctx->sk[31 - i] ); |
JMF | 12:0071cb144c7a | 518 | } |
JMF | 12:0071cb144c7a | 519 | |
JMF | 12:0071cb144c7a | 520 | return( 0 ); |
JMF | 12:0071cb144c7a | 521 | } |
JMF | 12:0071cb144c7a | 522 | |
JMF | 12:0071cb144c7a | 523 | static void des3_set2key( uint32_t esk[96], |
JMF | 12:0071cb144c7a | 524 | uint32_t dsk[96], |
JMF | 12:0071cb144c7a | 525 | const unsigned char key[MBEDTLS_DES_KEY_SIZE*2] ) |
JMF | 12:0071cb144c7a | 526 | { |
JMF | 12:0071cb144c7a | 527 | int i; |
JMF | 12:0071cb144c7a | 528 | |
JMF | 12:0071cb144c7a | 529 | mbedtls_des_setkey( esk, key ); |
JMF | 12:0071cb144c7a | 530 | mbedtls_des_setkey( dsk + 32, key + 8 ); |
JMF | 12:0071cb144c7a | 531 | |
JMF | 12:0071cb144c7a | 532 | for( i = 0; i < 32; i += 2 ) |
JMF | 12:0071cb144c7a | 533 | { |
JMF | 12:0071cb144c7a | 534 | dsk[i ] = esk[30 - i]; |
JMF | 12:0071cb144c7a | 535 | dsk[i + 1] = esk[31 - i]; |
JMF | 12:0071cb144c7a | 536 | |
JMF | 12:0071cb144c7a | 537 | esk[i + 32] = dsk[62 - i]; |
JMF | 12:0071cb144c7a | 538 | esk[i + 33] = dsk[63 - i]; |
JMF | 12:0071cb144c7a | 539 | |
JMF | 12:0071cb144c7a | 540 | esk[i + 64] = esk[i ]; |
JMF | 12:0071cb144c7a | 541 | esk[i + 65] = esk[i + 1]; |
JMF | 12:0071cb144c7a | 542 | |
JMF | 12:0071cb144c7a | 543 | dsk[i + 64] = dsk[i ]; |
JMF | 12:0071cb144c7a | 544 | dsk[i + 65] = dsk[i + 1]; |
JMF | 12:0071cb144c7a | 545 | } |
JMF | 12:0071cb144c7a | 546 | } |
JMF | 12:0071cb144c7a | 547 | |
JMF | 12:0071cb144c7a | 548 | /* |
JMF | 12:0071cb144c7a | 549 | * Triple-DES key schedule (112-bit, encryption) |
JMF | 12:0071cb144c7a | 550 | */ |
JMF | 12:0071cb144c7a | 551 | int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx, |
JMF | 12:0071cb144c7a | 552 | const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] ) |
JMF | 12:0071cb144c7a | 553 | { |
JMF | 12:0071cb144c7a | 554 | uint32_t sk[96]; |
JMF | 12:0071cb144c7a | 555 | |
JMF | 12:0071cb144c7a | 556 | des3_set2key( ctx->sk, sk, key ); |
JMF | 12:0071cb144c7a | 557 | mbedtls_zeroize( sk, sizeof( sk ) ); |
JMF | 12:0071cb144c7a | 558 | |
JMF | 12:0071cb144c7a | 559 | return( 0 ); |
JMF | 12:0071cb144c7a | 560 | } |
JMF | 12:0071cb144c7a | 561 | |
JMF | 12:0071cb144c7a | 562 | /* |
JMF | 12:0071cb144c7a | 563 | * Triple-DES key schedule (112-bit, decryption) |
JMF | 12:0071cb144c7a | 564 | */ |
JMF | 12:0071cb144c7a | 565 | int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx, |
JMF | 12:0071cb144c7a | 566 | const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] ) |
JMF | 12:0071cb144c7a | 567 | { |
JMF | 12:0071cb144c7a | 568 | uint32_t sk[96]; |
JMF | 12:0071cb144c7a | 569 | |
JMF | 12:0071cb144c7a | 570 | des3_set2key( sk, ctx->sk, key ); |
JMF | 12:0071cb144c7a | 571 | mbedtls_zeroize( sk, sizeof( sk ) ); |
JMF | 12:0071cb144c7a | 572 | |
JMF | 12:0071cb144c7a | 573 | return( 0 ); |
JMF | 12:0071cb144c7a | 574 | } |
JMF | 12:0071cb144c7a | 575 | |
JMF | 12:0071cb144c7a | 576 | static void des3_set3key( uint32_t esk[96], |
JMF | 12:0071cb144c7a | 577 | uint32_t dsk[96], |
JMF | 12:0071cb144c7a | 578 | const unsigned char key[24] ) |
JMF | 12:0071cb144c7a | 579 | { |
JMF | 12:0071cb144c7a | 580 | int i; |
JMF | 12:0071cb144c7a | 581 | |
JMF | 12:0071cb144c7a | 582 | mbedtls_des_setkey( esk, key ); |
JMF | 12:0071cb144c7a | 583 | mbedtls_des_setkey( dsk + 32, key + 8 ); |
JMF | 12:0071cb144c7a | 584 | mbedtls_des_setkey( esk + 64, key + 16 ); |
JMF | 12:0071cb144c7a | 585 | |
JMF | 12:0071cb144c7a | 586 | for( i = 0; i < 32; i += 2 ) |
JMF | 12:0071cb144c7a | 587 | { |
JMF | 12:0071cb144c7a | 588 | dsk[i ] = esk[94 - i]; |
JMF | 12:0071cb144c7a | 589 | dsk[i + 1] = esk[95 - i]; |
JMF | 12:0071cb144c7a | 590 | |
JMF | 12:0071cb144c7a | 591 | esk[i + 32] = dsk[62 - i]; |
JMF | 12:0071cb144c7a | 592 | esk[i + 33] = dsk[63 - i]; |
JMF | 12:0071cb144c7a | 593 | |
JMF | 12:0071cb144c7a | 594 | dsk[i + 64] = esk[30 - i]; |
JMF | 12:0071cb144c7a | 595 | dsk[i + 65] = esk[31 - i]; |
JMF | 12:0071cb144c7a | 596 | } |
JMF | 12:0071cb144c7a | 597 | } |
JMF | 12:0071cb144c7a | 598 | |
JMF | 12:0071cb144c7a | 599 | /* |
JMF | 12:0071cb144c7a | 600 | * Triple-DES key schedule (168-bit, encryption) |
JMF | 12:0071cb144c7a | 601 | */ |
JMF | 12:0071cb144c7a | 602 | int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx, |
JMF | 12:0071cb144c7a | 603 | const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] ) |
JMF | 12:0071cb144c7a | 604 | { |
JMF | 12:0071cb144c7a | 605 | uint32_t sk[96]; |
JMF | 12:0071cb144c7a | 606 | |
JMF | 12:0071cb144c7a | 607 | des3_set3key( ctx->sk, sk, key ); |
JMF | 12:0071cb144c7a | 608 | mbedtls_zeroize( sk, sizeof( sk ) ); |
JMF | 12:0071cb144c7a | 609 | |
JMF | 12:0071cb144c7a | 610 | return( 0 ); |
JMF | 12:0071cb144c7a | 611 | } |
JMF | 12:0071cb144c7a | 612 | |
JMF | 12:0071cb144c7a | 613 | /* |
JMF | 12:0071cb144c7a | 614 | * Triple-DES key schedule (168-bit, decryption) |
JMF | 12:0071cb144c7a | 615 | */ |
JMF | 12:0071cb144c7a | 616 | int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx, |
JMF | 12:0071cb144c7a | 617 | const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] ) |
JMF | 12:0071cb144c7a | 618 | { |
JMF | 12:0071cb144c7a | 619 | uint32_t sk[96]; |
JMF | 12:0071cb144c7a | 620 | |
JMF | 12:0071cb144c7a | 621 | des3_set3key( sk, ctx->sk, key ); |
JMF | 12:0071cb144c7a | 622 | mbedtls_zeroize( sk, sizeof( sk ) ); |
JMF | 12:0071cb144c7a | 623 | |
JMF | 12:0071cb144c7a | 624 | return( 0 ); |
JMF | 12:0071cb144c7a | 625 | } |
JMF | 12:0071cb144c7a | 626 | |
JMF | 12:0071cb144c7a | 627 | /* |
JMF | 12:0071cb144c7a | 628 | * DES-ECB block encryption/decryption |
JMF | 12:0071cb144c7a | 629 | */ |
JMF | 12:0071cb144c7a | 630 | #if !defined(MBEDTLS_DES_CRYPT_ECB_ALT) |
JMF | 12:0071cb144c7a | 631 | int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx, |
JMF | 12:0071cb144c7a | 632 | const unsigned char input[8], |
JMF | 12:0071cb144c7a | 633 | unsigned char output[8] ) |
JMF | 12:0071cb144c7a | 634 | { |
JMF | 12:0071cb144c7a | 635 | int i; |
JMF | 12:0071cb144c7a | 636 | uint32_t X, Y, T, *SK; |
JMF | 12:0071cb144c7a | 637 | |
JMF | 12:0071cb144c7a | 638 | SK = ctx->sk; |
JMF | 12:0071cb144c7a | 639 | |
JMF | 12:0071cb144c7a | 640 | GET_UINT32_BE( X, input, 0 ); |
JMF | 12:0071cb144c7a | 641 | GET_UINT32_BE( Y, input, 4 ); |
JMF | 12:0071cb144c7a | 642 | |
JMF | 12:0071cb144c7a | 643 | DES_IP( X, Y ); |
JMF | 12:0071cb144c7a | 644 | |
JMF | 12:0071cb144c7a | 645 | for( i = 0; i < 8; i++ ) |
JMF | 12:0071cb144c7a | 646 | { |
JMF | 12:0071cb144c7a | 647 | DES_ROUND( Y, X ); |
JMF | 12:0071cb144c7a | 648 | DES_ROUND( X, Y ); |
JMF | 12:0071cb144c7a | 649 | } |
JMF | 12:0071cb144c7a | 650 | |
JMF | 12:0071cb144c7a | 651 | DES_FP( Y, X ); |
JMF | 12:0071cb144c7a | 652 | |
JMF | 12:0071cb144c7a | 653 | PUT_UINT32_BE( Y, output, 0 ); |
JMF | 12:0071cb144c7a | 654 | PUT_UINT32_BE( X, output, 4 ); |
JMF | 12:0071cb144c7a | 655 | |
JMF | 12:0071cb144c7a | 656 | return( 0 ); |
JMF | 12:0071cb144c7a | 657 | } |
JMF | 12:0071cb144c7a | 658 | #endif /* !MBEDTLS_DES_CRYPT_ECB_ALT */ |
JMF | 12:0071cb144c7a | 659 | |
JMF | 12:0071cb144c7a | 660 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
JMF | 12:0071cb144c7a | 661 | /* |
JMF | 12:0071cb144c7a | 662 | * DES-CBC buffer encryption/decryption |
JMF | 12:0071cb144c7a | 663 | */ |
JMF | 12:0071cb144c7a | 664 | int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx, |
JMF | 12:0071cb144c7a | 665 | int mode, |
JMF | 12:0071cb144c7a | 666 | size_t length, |
JMF | 12:0071cb144c7a | 667 | unsigned char iv[8], |
JMF | 12:0071cb144c7a | 668 | const unsigned char *input, |
JMF | 12:0071cb144c7a | 669 | unsigned char *output ) |
JMF | 12:0071cb144c7a | 670 | { |
JMF | 12:0071cb144c7a | 671 | int i; |
JMF | 12:0071cb144c7a | 672 | unsigned char temp[8]; |
JMF | 12:0071cb144c7a | 673 | |
JMF | 12:0071cb144c7a | 674 | if( length % 8 ) |
JMF | 12:0071cb144c7a | 675 | return( MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH ); |
JMF | 12:0071cb144c7a | 676 | |
JMF | 12:0071cb144c7a | 677 | if( mode == MBEDTLS_DES_ENCRYPT ) |
JMF | 12:0071cb144c7a | 678 | { |
JMF | 12:0071cb144c7a | 679 | while( length > 0 ) |
JMF | 12:0071cb144c7a | 680 | { |
JMF | 12:0071cb144c7a | 681 | for( i = 0; i < 8; i++ ) |
JMF | 12:0071cb144c7a | 682 | output[i] = (unsigned char)( input[i] ^ iv[i] ); |
JMF | 12:0071cb144c7a | 683 | |
JMF | 12:0071cb144c7a | 684 | mbedtls_des_crypt_ecb( ctx, output, output ); |
JMF | 12:0071cb144c7a | 685 | memcpy( iv, output, 8 ); |
JMF | 12:0071cb144c7a | 686 | |
JMF | 12:0071cb144c7a | 687 | input += 8; |
JMF | 12:0071cb144c7a | 688 | output += 8; |
JMF | 12:0071cb144c7a | 689 | length -= 8; |
JMF | 12:0071cb144c7a | 690 | } |
JMF | 12:0071cb144c7a | 691 | } |
JMF | 12:0071cb144c7a | 692 | else /* MBEDTLS_DES_DECRYPT */ |
JMF | 12:0071cb144c7a | 693 | { |
JMF | 12:0071cb144c7a | 694 | while( length > 0 ) |
JMF | 12:0071cb144c7a | 695 | { |
JMF | 12:0071cb144c7a | 696 | memcpy( temp, input, 8 ); |
JMF | 12:0071cb144c7a | 697 | mbedtls_des_crypt_ecb( ctx, input, output ); |
JMF | 12:0071cb144c7a | 698 | |
JMF | 12:0071cb144c7a | 699 | for( i = 0; i < 8; i++ ) |
JMF | 12:0071cb144c7a | 700 | output[i] = (unsigned char)( output[i] ^ iv[i] ); |
JMF | 12:0071cb144c7a | 701 | |
JMF | 12:0071cb144c7a | 702 | memcpy( iv, temp, 8 ); |
JMF | 12:0071cb144c7a | 703 | |
JMF | 12:0071cb144c7a | 704 | input += 8; |
JMF | 12:0071cb144c7a | 705 | output += 8; |
JMF | 12:0071cb144c7a | 706 | length -= 8; |
JMF | 12:0071cb144c7a | 707 | } |
JMF | 12:0071cb144c7a | 708 | } |
JMF | 12:0071cb144c7a | 709 | |
JMF | 12:0071cb144c7a | 710 | return( 0 ); |
JMF | 12:0071cb144c7a | 711 | } |
JMF | 12:0071cb144c7a | 712 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
JMF | 12:0071cb144c7a | 713 | |
JMF | 12:0071cb144c7a | 714 | /* |
JMF | 12:0071cb144c7a | 715 | * 3DES-ECB block encryption/decryption |
JMF | 12:0071cb144c7a | 716 | */ |
JMF | 12:0071cb144c7a | 717 | #if !defined(MBEDTLS_DES3_CRYPT_ECB_ALT) |
JMF | 12:0071cb144c7a | 718 | int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx, |
JMF | 12:0071cb144c7a | 719 | const unsigned char input[8], |
JMF | 12:0071cb144c7a | 720 | unsigned char output[8] ) |
JMF | 12:0071cb144c7a | 721 | { |
JMF | 12:0071cb144c7a | 722 | int i; |
JMF | 12:0071cb144c7a | 723 | uint32_t X, Y, T, *SK; |
JMF | 12:0071cb144c7a | 724 | |
JMF | 12:0071cb144c7a | 725 | SK = ctx->sk; |
JMF | 12:0071cb144c7a | 726 | |
JMF | 12:0071cb144c7a | 727 | GET_UINT32_BE( X, input, 0 ); |
JMF | 12:0071cb144c7a | 728 | GET_UINT32_BE( Y, input, 4 ); |
JMF | 12:0071cb144c7a | 729 | |
JMF | 12:0071cb144c7a | 730 | DES_IP( X, Y ); |
JMF | 12:0071cb144c7a | 731 | |
JMF | 12:0071cb144c7a | 732 | for( i = 0; i < 8; i++ ) |
JMF | 12:0071cb144c7a | 733 | { |
JMF | 12:0071cb144c7a | 734 | DES_ROUND( Y, X ); |
JMF | 12:0071cb144c7a | 735 | DES_ROUND( X, Y ); |
JMF | 12:0071cb144c7a | 736 | } |
JMF | 12:0071cb144c7a | 737 | |
JMF | 12:0071cb144c7a | 738 | for( i = 0; i < 8; i++ ) |
JMF | 12:0071cb144c7a | 739 | { |
JMF | 12:0071cb144c7a | 740 | DES_ROUND( X, Y ); |
JMF | 12:0071cb144c7a | 741 | DES_ROUND( Y, X ); |
JMF | 12:0071cb144c7a | 742 | } |
JMF | 12:0071cb144c7a | 743 | |
JMF | 12:0071cb144c7a | 744 | for( i = 0; i < 8; i++ ) |
JMF | 12:0071cb144c7a | 745 | { |
JMF | 12:0071cb144c7a | 746 | DES_ROUND( Y, X ); |
JMF | 12:0071cb144c7a | 747 | DES_ROUND( X, Y ); |
JMF | 12:0071cb144c7a | 748 | } |
JMF | 12:0071cb144c7a | 749 | |
JMF | 12:0071cb144c7a | 750 | DES_FP( Y, X ); |
JMF | 12:0071cb144c7a | 751 | |
JMF | 12:0071cb144c7a | 752 | PUT_UINT32_BE( Y, output, 0 ); |
JMF | 12:0071cb144c7a | 753 | PUT_UINT32_BE( X, output, 4 ); |
JMF | 12:0071cb144c7a | 754 | |
JMF | 12:0071cb144c7a | 755 | return( 0 ); |
JMF | 12:0071cb144c7a | 756 | } |
JMF | 12:0071cb144c7a | 757 | #endif /* !MBEDTLS_DES3_CRYPT_ECB_ALT */ |
JMF | 12:0071cb144c7a | 758 | |
JMF | 12:0071cb144c7a | 759 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
JMF | 12:0071cb144c7a | 760 | /* |
JMF | 12:0071cb144c7a | 761 | * 3DES-CBC buffer encryption/decryption |
JMF | 12:0071cb144c7a | 762 | */ |
JMF | 12:0071cb144c7a | 763 | int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx, |
JMF | 12:0071cb144c7a | 764 | int mode, |
JMF | 12:0071cb144c7a | 765 | size_t length, |
JMF | 12:0071cb144c7a | 766 | unsigned char iv[8], |
JMF | 12:0071cb144c7a | 767 | const unsigned char *input, |
JMF | 12:0071cb144c7a | 768 | unsigned char *output ) |
JMF | 12:0071cb144c7a | 769 | { |
JMF | 12:0071cb144c7a | 770 | int i; |
JMF | 12:0071cb144c7a | 771 | unsigned char temp[8]; |
JMF | 12:0071cb144c7a | 772 | |
JMF | 12:0071cb144c7a | 773 | if( length % 8 ) |
JMF | 12:0071cb144c7a | 774 | return( MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH ); |
JMF | 12:0071cb144c7a | 775 | |
JMF | 12:0071cb144c7a | 776 | if( mode == MBEDTLS_DES_ENCRYPT ) |
JMF | 12:0071cb144c7a | 777 | { |
JMF | 12:0071cb144c7a | 778 | while( length > 0 ) |
JMF | 12:0071cb144c7a | 779 | { |
JMF | 12:0071cb144c7a | 780 | for( i = 0; i < 8; i++ ) |
JMF | 12:0071cb144c7a | 781 | output[i] = (unsigned char)( input[i] ^ iv[i] ); |
JMF | 12:0071cb144c7a | 782 | |
JMF | 12:0071cb144c7a | 783 | mbedtls_des3_crypt_ecb( ctx, output, output ); |
JMF | 12:0071cb144c7a | 784 | memcpy( iv, output, 8 ); |
JMF | 12:0071cb144c7a | 785 | |
JMF | 12:0071cb144c7a | 786 | input += 8; |
JMF | 12:0071cb144c7a | 787 | output += 8; |
JMF | 12:0071cb144c7a | 788 | length -= 8; |
JMF | 12:0071cb144c7a | 789 | } |
JMF | 12:0071cb144c7a | 790 | } |
JMF | 12:0071cb144c7a | 791 | else /* MBEDTLS_DES_DECRYPT */ |
JMF | 12:0071cb144c7a | 792 | { |
JMF | 12:0071cb144c7a | 793 | while( length > 0 ) |
JMF | 12:0071cb144c7a | 794 | { |
JMF | 12:0071cb144c7a | 795 | memcpy( temp, input, 8 ); |
JMF | 12:0071cb144c7a | 796 | mbedtls_des3_crypt_ecb( ctx, input, output ); |
JMF | 12:0071cb144c7a | 797 | |
JMF | 12:0071cb144c7a | 798 | for( i = 0; i < 8; i++ ) |
JMF | 12:0071cb144c7a | 799 | output[i] = (unsigned char)( output[i] ^ iv[i] ); |
JMF | 12:0071cb144c7a | 800 | |
JMF | 12:0071cb144c7a | 801 | memcpy( iv, temp, 8 ); |
JMF | 12:0071cb144c7a | 802 | |
JMF | 12:0071cb144c7a | 803 | input += 8; |
JMF | 12:0071cb144c7a | 804 | output += 8; |
JMF | 12:0071cb144c7a | 805 | length -= 8; |
JMF | 12:0071cb144c7a | 806 | } |
JMF | 12:0071cb144c7a | 807 | } |
JMF | 12:0071cb144c7a | 808 | |
JMF | 12:0071cb144c7a | 809 | return( 0 ); |
JMF | 12:0071cb144c7a | 810 | } |
JMF | 12:0071cb144c7a | 811 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
JMF | 12:0071cb144c7a | 812 | |
JMF | 12:0071cb144c7a | 813 | #endif /* !MBEDTLS_DES_ALT */ |
JMF | 12:0071cb144c7a | 814 | |
JMF | 12:0071cb144c7a | 815 | #if defined(MBEDTLS_SELF_TEST) |
JMF | 12:0071cb144c7a | 816 | /* |
JMF | 12:0071cb144c7a | 817 | * DES and 3DES test vectors from: |
JMF | 12:0071cb144c7a | 818 | * |
JMF | 12:0071cb144c7a | 819 | * http://csrc.nist.gov/groups/STM/cavp/documents/des/tripledes-vectors.zip |
JMF | 12:0071cb144c7a | 820 | */ |
JMF | 12:0071cb144c7a | 821 | static const unsigned char des3_test_keys[24] = |
JMF | 12:0071cb144c7a | 822 | { |
JMF | 12:0071cb144c7a | 823 | 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, |
JMF | 12:0071cb144c7a | 824 | 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, |
JMF | 12:0071cb144c7a | 825 | 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23 |
JMF | 12:0071cb144c7a | 826 | }; |
JMF | 12:0071cb144c7a | 827 | |
JMF | 12:0071cb144c7a | 828 | static const unsigned char des3_test_buf[8] = |
JMF | 12:0071cb144c7a | 829 | { |
JMF | 12:0071cb144c7a | 830 | 0x4E, 0x6F, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74 |
JMF | 12:0071cb144c7a | 831 | }; |
JMF | 12:0071cb144c7a | 832 | |
JMF | 12:0071cb144c7a | 833 | static const unsigned char des3_test_ecb_dec[3][8] = |
JMF | 12:0071cb144c7a | 834 | { |
JMF | 12:0071cb144c7a | 835 | { 0xCD, 0xD6, 0x4F, 0x2F, 0x94, 0x27, 0xC1, 0x5D }, |
JMF | 12:0071cb144c7a | 836 | { 0x69, 0x96, 0xC8, 0xFA, 0x47, 0xA2, 0xAB, 0xEB }, |
JMF | 12:0071cb144c7a | 837 | { 0x83, 0x25, 0x39, 0x76, 0x44, 0x09, 0x1A, 0x0A } |
JMF | 12:0071cb144c7a | 838 | }; |
JMF | 12:0071cb144c7a | 839 | |
JMF | 12:0071cb144c7a | 840 | static const unsigned char des3_test_ecb_enc[3][8] = |
JMF | 12:0071cb144c7a | 841 | { |
JMF | 12:0071cb144c7a | 842 | { 0x6A, 0x2A, 0x19, 0xF4, 0x1E, 0xCA, 0x85, 0x4B }, |
JMF | 12:0071cb144c7a | 843 | { 0x03, 0xE6, 0x9F, 0x5B, 0xFA, 0x58, 0xEB, 0x42 }, |
JMF | 12:0071cb144c7a | 844 | { 0xDD, 0x17, 0xE8, 0xB8, 0xB4, 0x37, 0xD2, 0x32 } |
JMF | 12:0071cb144c7a | 845 | }; |
JMF | 12:0071cb144c7a | 846 | |
JMF | 12:0071cb144c7a | 847 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
JMF | 12:0071cb144c7a | 848 | static const unsigned char des3_test_iv[8] = |
JMF | 12:0071cb144c7a | 849 | { |
JMF | 12:0071cb144c7a | 850 | 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, |
JMF | 12:0071cb144c7a | 851 | }; |
JMF | 12:0071cb144c7a | 852 | |
JMF | 12:0071cb144c7a | 853 | static const unsigned char des3_test_cbc_dec[3][8] = |
JMF | 12:0071cb144c7a | 854 | { |
JMF | 12:0071cb144c7a | 855 | { 0x12, 0x9F, 0x40, 0xB9, 0xD2, 0x00, 0x56, 0xB3 }, |
JMF | 12:0071cb144c7a | 856 | { 0x47, 0x0E, 0xFC, 0x9A, 0x6B, 0x8E, 0xE3, 0x93 }, |
JMF | 12:0071cb144c7a | 857 | { 0xC5, 0xCE, 0xCF, 0x63, 0xEC, 0xEC, 0x51, 0x4C } |
JMF | 12:0071cb144c7a | 858 | }; |
JMF | 12:0071cb144c7a | 859 | |
JMF | 12:0071cb144c7a | 860 | static const unsigned char des3_test_cbc_enc[3][8] = |
JMF | 12:0071cb144c7a | 861 | { |
JMF | 12:0071cb144c7a | 862 | { 0x54, 0xF1, 0x5A, 0xF6, 0xEB, 0xE3, 0xA4, 0xB4 }, |
JMF | 12:0071cb144c7a | 863 | { 0x35, 0x76, 0x11, 0x56, 0x5F, 0xA1, 0x8E, 0x4D }, |
JMF | 12:0071cb144c7a | 864 | { 0xCB, 0x19, 0x1F, 0x85, 0xD1, 0xED, 0x84, 0x39 } |
JMF | 12:0071cb144c7a | 865 | }; |
JMF | 12:0071cb144c7a | 866 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
JMF | 12:0071cb144c7a | 867 | |
JMF | 12:0071cb144c7a | 868 | /* |
JMF | 12:0071cb144c7a | 869 | * Checkup routine |
JMF | 12:0071cb144c7a | 870 | */ |
JMF | 12:0071cb144c7a | 871 | int mbedtls_des_self_test( int verbose ) |
JMF | 12:0071cb144c7a | 872 | { |
JMF | 12:0071cb144c7a | 873 | int i, j, u, v, ret = 0; |
JMF | 12:0071cb144c7a | 874 | mbedtls_des_context ctx; |
JMF | 12:0071cb144c7a | 875 | mbedtls_des3_context ctx3; |
JMF | 12:0071cb144c7a | 876 | unsigned char buf[8]; |
JMF | 12:0071cb144c7a | 877 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
JMF | 12:0071cb144c7a | 878 | unsigned char prv[8]; |
JMF | 12:0071cb144c7a | 879 | unsigned char iv[8]; |
JMF | 12:0071cb144c7a | 880 | #endif |
JMF | 12:0071cb144c7a | 881 | |
JMF | 12:0071cb144c7a | 882 | mbedtls_des_init( &ctx ); |
JMF | 12:0071cb144c7a | 883 | mbedtls_des3_init( &ctx3 ); |
JMF | 12:0071cb144c7a | 884 | /* |
JMF | 12:0071cb144c7a | 885 | * ECB mode |
JMF | 12:0071cb144c7a | 886 | */ |
JMF | 12:0071cb144c7a | 887 | for( i = 0; i < 6; i++ ) |
JMF | 12:0071cb144c7a | 888 | { |
JMF | 12:0071cb144c7a | 889 | u = i >> 1; |
JMF | 12:0071cb144c7a | 890 | v = i & 1; |
JMF | 12:0071cb144c7a | 891 | |
JMF | 12:0071cb144c7a | 892 | if( verbose != 0 ) |
JMF | 12:0071cb144c7a | 893 | mbedtls_printf( " DES%c-ECB-%3d (%s): ", |
JMF | 12:0071cb144c7a | 894 | ( u == 0 ) ? ' ' : '3', 56 + u * 56, |
JMF | 12:0071cb144c7a | 895 | ( v == MBEDTLS_DES_DECRYPT ) ? "dec" : "enc" ); |
JMF | 12:0071cb144c7a | 896 | |
JMF | 12:0071cb144c7a | 897 | memcpy( buf, des3_test_buf, 8 ); |
JMF | 12:0071cb144c7a | 898 | |
JMF | 12:0071cb144c7a | 899 | switch( i ) |
JMF | 12:0071cb144c7a | 900 | { |
JMF | 12:0071cb144c7a | 901 | case 0: |
JMF | 12:0071cb144c7a | 902 | mbedtls_des_setkey_dec( &ctx, des3_test_keys ); |
JMF | 12:0071cb144c7a | 903 | break; |
JMF | 12:0071cb144c7a | 904 | |
JMF | 12:0071cb144c7a | 905 | case 1: |
JMF | 12:0071cb144c7a | 906 | mbedtls_des_setkey_enc( &ctx, des3_test_keys ); |
JMF | 12:0071cb144c7a | 907 | break; |
JMF | 12:0071cb144c7a | 908 | |
JMF | 12:0071cb144c7a | 909 | case 2: |
JMF | 12:0071cb144c7a | 910 | mbedtls_des3_set2key_dec( &ctx3, des3_test_keys ); |
JMF | 12:0071cb144c7a | 911 | break; |
JMF | 12:0071cb144c7a | 912 | |
JMF | 12:0071cb144c7a | 913 | case 3: |
JMF | 12:0071cb144c7a | 914 | mbedtls_des3_set2key_enc( &ctx3, des3_test_keys ); |
JMF | 12:0071cb144c7a | 915 | break; |
JMF | 12:0071cb144c7a | 916 | |
JMF | 12:0071cb144c7a | 917 | case 4: |
JMF | 12:0071cb144c7a | 918 | mbedtls_des3_set3key_dec( &ctx3, des3_test_keys ); |
JMF | 12:0071cb144c7a | 919 | break; |
JMF | 12:0071cb144c7a | 920 | |
JMF | 12:0071cb144c7a | 921 | case 5: |
JMF | 12:0071cb144c7a | 922 | mbedtls_des3_set3key_enc( &ctx3, des3_test_keys ); |
JMF | 12:0071cb144c7a | 923 | break; |
JMF | 12:0071cb144c7a | 924 | |
JMF | 12:0071cb144c7a | 925 | default: |
JMF | 12:0071cb144c7a | 926 | return( 1 ); |
JMF | 12:0071cb144c7a | 927 | } |
JMF | 12:0071cb144c7a | 928 | |
JMF | 12:0071cb144c7a | 929 | for( j = 0; j < 10000; j++ ) |
JMF | 12:0071cb144c7a | 930 | { |
JMF | 12:0071cb144c7a | 931 | if( u == 0 ) |
JMF | 12:0071cb144c7a | 932 | mbedtls_des_crypt_ecb( &ctx, buf, buf ); |
JMF | 12:0071cb144c7a | 933 | else |
JMF | 12:0071cb144c7a | 934 | mbedtls_des3_crypt_ecb( &ctx3, buf, buf ); |
JMF | 12:0071cb144c7a | 935 | } |
JMF | 12:0071cb144c7a | 936 | |
JMF | 12:0071cb144c7a | 937 | if( ( v == MBEDTLS_DES_DECRYPT && |
JMF | 12:0071cb144c7a | 938 | memcmp( buf, des3_test_ecb_dec[u], 8 ) != 0 ) || |
JMF | 12:0071cb144c7a | 939 | ( v != MBEDTLS_DES_DECRYPT && |
JMF | 12:0071cb144c7a | 940 | memcmp( buf, des3_test_ecb_enc[u], 8 ) != 0 ) ) |
JMF | 12:0071cb144c7a | 941 | { |
JMF | 12:0071cb144c7a | 942 | if( verbose != 0 ) |
JMF | 12:0071cb144c7a | 943 | mbedtls_printf( "failed\n" ); |
JMF | 12:0071cb144c7a | 944 | |
JMF | 12:0071cb144c7a | 945 | ret = 1; |
JMF | 12:0071cb144c7a | 946 | goto exit; |
JMF | 12:0071cb144c7a | 947 | } |
JMF | 12:0071cb144c7a | 948 | |
JMF | 12:0071cb144c7a | 949 | if( verbose != 0 ) |
JMF | 12:0071cb144c7a | 950 | mbedtls_printf( "passed\n" ); |
JMF | 12:0071cb144c7a | 951 | } |
JMF | 12:0071cb144c7a | 952 | |
JMF | 12:0071cb144c7a | 953 | if( verbose != 0 ) |
JMF | 12:0071cb144c7a | 954 | mbedtls_printf( "\n" ); |
JMF | 12:0071cb144c7a | 955 | |
JMF | 12:0071cb144c7a | 956 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
JMF | 12:0071cb144c7a | 957 | /* |
JMF | 12:0071cb144c7a | 958 | * CBC mode |
JMF | 12:0071cb144c7a | 959 | */ |
JMF | 12:0071cb144c7a | 960 | for( i = 0; i < 6; i++ ) |
JMF | 12:0071cb144c7a | 961 | { |
JMF | 12:0071cb144c7a | 962 | u = i >> 1; |
JMF | 12:0071cb144c7a | 963 | v = i & 1; |
JMF | 12:0071cb144c7a | 964 | |
JMF | 12:0071cb144c7a | 965 | if( verbose != 0 ) |
JMF | 12:0071cb144c7a | 966 | mbedtls_printf( " DES%c-CBC-%3d (%s): ", |
JMF | 12:0071cb144c7a | 967 | ( u == 0 ) ? ' ' : '3', 56 + u * 56, |
JMF | 12:0071cb144c7a | 968 | ( v == MBEDTLS_DES_DECRYPT ) ? "dec" : "enc" ); |
JMF | 12:0071cb144c7a | 969 | |
JMF | 12:0071cb144c7a | 970 | memcpy( iv, des3_test_iv, 8 ); |
JMF | 12:0071cb144c7a | 971 | memcpy( prv, des3_test_iv, 8 ); |
JMF | 12:0071cb144c7a | 972 | memcpy( buf, des3_test_buf, 8 ); |
JMF | 12:0071cb144c7a | 973 | |
JMF | 12:0071cb144c7a | 974 | switch( i ) |
JMF | 12:0071cb144c7a | 975 | { |
JMF | 12:0071cb144c7a | 976 | case 0: |
JMF | 12:0071cb144c7a | 977 | mbedtls_des_setkey_dec( &ctx, des3_test_keys ); |
JMF | 12:0071cb144c7a | 978 | break; |
JMF | 12:0071cb144c7a | 979 | |
JMF | 12:0071cb144c7a | 980 | case 1: |
JMF | 12:0071cb144c7a | 981 | mbedtls_des_setkey_enc( &ctx, des3_test_keys ); |
JMF | 12:0071cb144c7a | 982 | break; |
JMF | 12:0071cb144c7a | 983 | |
JMF | 12:0071cb144c7a | 984 | case 2: |
JMF | 12:0071cb144c7a | 985 | mbedtls_des3_set2key_dec( &ctx3, des3_test_keys ); |
JMF | 12:0071cb144c7a | 986 | break; |
JMF | 12:0071cb144c7a | 987 | |
JMF | 12:0071cb144c7a | 988 | case 3: |
JMF | 12:0071cb144c7a | 989 | mbedtls_des3_set2key_enc( &ctx3, des3_test_keys ); |
JMF | 12:0071cb144c7a | 990 | break; |
JMF | 12:0071cb144c7a | 991 | |
JMF | 12:0071cb144c7a | 992 | case 4: |
JMF | 12:0071cb144c7a | 993 | mbedtls_des3_set3key_dec( &ctx3, des3_test_keys ); |
JMF | 12:0071cb144c7a | 994 | break; |
JMF | 12:0071cb144c7a | 995 | |
JMF | 12:0071cb144c7a | 996 | case 5: |
JMF | 12:0071cb144c7a | 997 | mbedtls_des3_set3key_enc( &ctx3, des3_test_keys ); |
JMF | 12:0071cb144c7a | 998 | break; |
JMF | 12:0071cb144c7a | 999 | |
JMF | 12:0071cb144c7a | 1000 | default: |
JMF | 12:0071cb144c7a | 1001 | return( 1 ); |
JMF | 12:0071cb144c7a | 1002 | } |
JMF | 12:0071cb144c7a | 1003 | |
JMF | 12:0071cb144c7a | 1004 | if( v == MBEDTLS_DES_DECRYPT ) |
JMF | 12:0071cb144c7a | 1005 | { |
JMF | 12:0071cb144c7a | 1006 | for( j = 0; j < 10000; j++ ) |
JMF | 12:0071cb144c7a | 1007 | { |
JMF | 12:0071cb144c7a | 1008 | if( u == 0 ) |
JMF | 12:0071cb144c7a | 1009 | mbedtls_des_crypt_cbc( &ctx, v, 8, iv, buf, buf ); |
JMF | 12:0071cb144c7a | 1010 | else |
JMF | 12:0071cb144c7a | 1011 | mbedtls_des3_crypt_cbc( &ctx3, v, 8, iv, buf, buf ); |
JMF | 12:0071cb144c7a | 1012 | } |
JMF | 12:0071cb144c7a | 1013 | } |
JMF | 12:0071cb144c7a | 1014 | else |
JMF | 12:0071cb144c7a | 1015 | { |
JMF | 12:0071cb144c7a | 1016 | for( j = 0; j < 10000; j++ ) |
JMF | 12:0071cb144c7a | 1017 | { |
JMF | 12:0071cb144c7a | 1018 | unsigned char tmp[8]; |
JMF | 12:0071cb144c7a | 1019 | |
JMF | 12:0071cb144c7a | 1020 | if( u == 0 ) |
JMF | 12:0071cb144c7a | 1021 | mbedtls_des_crypt_cbc( &ctx, v, 8, iv, buf, buf ); |
JMF | 12:0071cb144c7a | 1022 | else |
JMF | 12:0071cb144c7a | 1023 | mbedtls_des3_crypt_cbc( &ctx3, v, 8, iv, buf, buf ); |
JMF | 12:0071cb144c7a | 1024 | |
JMF | 12:0071cb144c7a | 1025 | memcpy( tmp, prv, 8 ); |
JMF | 12:0071cb144c7a | 1026 | memcpy( prv, buf, 8 ); |
JMF | 12:0071cb144c7a | 1027 | memcpy( buf, tmp, 8 ); |
JMF | 12:0071cb144c7a | 1028 | } |
JMF | 12:0071cb144c7a | 1029 | |
JMF | 12:0071cb144c7a | 1030 | memcpy( buf, prv, 8 ); |
JMF | 12:0071cb144c7a | 1031 | } |
JMF | 12:0071cb144c7a | 1032 | |
JMF | 12:0071cb144c7a | 1033 | if( ( v == MBEDTLS_DES_DECRYPT && |
JMF | 12:0071cb144c7a | 1034 | memcmp( buf, des3_test_cbc_dec[u], 8 ) != 0 ) || |
JMF | 12:0071cb144c7a | 1035 | ( v != MBEDTLS_DES_DECRYPT && |
JMF | 12:0071cb144c7a | 1036 | memcmp( buf, des3_test_cbc_enc[u], 8 ) != 0 ) ) |
JMF | 12:0071cb144c7a | 1037 | { |
JMF | 12:0071cb144c7a | 1038 | if( verbose != 0 ) |
JMF | 12:0071cb144c7a | 1039 | mbedtls_printf( "failed\n" ); |
JMF | 12:0071cb144c7a | 1040 | |
JMF | 12:0071cb144c7a | 1041 | ret = 1; |
JMF | 12:0071cb144c7a | 1042 | goto exit; |
JMF | 12:0071cb144c7a | 1043 | } |
JMF | 12:0071cb144c7a | 1044 | |
JMF | 12:0071cb144c7a | 1045 | if( verbose != 0 ) |
JMF | 12:0071cb144c7a | 1046 | mbedtls_printf( "passed\n" ); |
JMF | 12:0071cb144c7a | 1047 | } |
JMF | 12:0071cb144c7a | 1048 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
JMF | 12:0071cb144c7a | 1049 | |
JMF | 12:0071cb144c7a | 1050 | if( verbose != 0 ) |
JMF | 12:0071cb144c7a | 1051 | mbedtls_printf( "\n" ); |
JMF | 12:0071cb144c7a | 1052 | |
JMF | 12:0071cb144c7a | 1053 | exit: |
JMF | 12:0071cb144c7a | 1054 | mbedtls_des_free( &ctx ); |
JMF | 12:0071cb144c7a | 1055 | mbedtls_des3_free( &ctx3 ); |
JMF | 12:0071cb144c7a | 1056 | |
JMF | 12:0071cb144c7a | 1057 | return( ret ); |
JMF | 12:0071cb144c7a | 1058 | } |
JMF | 12:0071cb144c7a | 1059 | |
JMF | 12:0071cb144c7a | 1060 | #endif /* MBEDTLS_SELF_TEST */ |
JMF | 12:0071cb144c7a | 1061 | |
JMF | 12:0071cb144c7a | 1062 | #endif /* MBEDTLS_DES_C */ |