Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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