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