mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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