mbed-os5 only for TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 17 23:23:45 2019 +0000
Revision:
0:5b88d5760320
Child:
1:9db0e321a9f4
mbed-os5 only for TYBLE16

Who changed what in which revision?

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