This is a fork due to permission issues

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of 6_songs-from-the-cloud by MakingMusicWorkshop

Committer:
maclobdell
Date:
Wed May 18 19:06:32 2016 +0000
Revision:
0:f7c60d3e7b8a
clean version

Who changed what in which revision?

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