Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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