Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Dec 16 16:27:57 2016 +0000
Revision:
3:1198227e6421
Parent:
0:5c4d7b2438d3
Changed ADC scale for MAX32625 platforms to 1.2V full scale to match MAX32630 platforms

Who changed what in which revision?

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