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