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