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