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