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