Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:54:50 2016 +0000
Revision:
1:d96dbedaebdb
Parent:
0:6c56fb4bc5f0
Removed extra directories for other platforms

Who changed what in which revision?

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