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