Mouse code for the MacroRat
mbed-dev/targets/TARGET_NUVOTON/TARGET_NUC472/crypto/des/des_alt_sw.c@18:6a4db94011d3, 2017-05-14 (annotated)
- Committer:
- sahilmgandhi
- Date:
- Sun May 14 23:18:57 2017 +0000
- Revision:
- 18:6a4db94011d3
Publishing again
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sahilmgandhi | 18:6a4db94011d3 | 1 | /* |
sahilmgandhi | 18:6a4db94011d3 | 2 | * FIPS-46-3 compliant Triple-DES implementation |
sahilmgandhi | 18:6a4db94011d3 | 3 | * |
sahilmgandhi | 18:6a4db94011d3 | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
sahilmgandhi | 18:6a4db94011d3 | 5 | * SPDX-License-Identifier: Apache-2.0 |
sahilmgandhi | 18:6a4db94011d3 | 6 | * |
sahilmgandhi | 18:6a4db94011d3 | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
sahilmgandhi | 18:6a4db94011d3 | 8 | * not use this file except in compliance with the License. |
sahilmgandhi | 18:6a4db94011d3 | 9 | * You may obtain a copy of the License at |
sahilmgandhi | 18:6a4db94011d3 | 10 | * |
sahilmgandhi | 18:6a4db94011d3 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
sahilmgandhi | 18:6a4db94011d3 | 12 | * |
sahilmgandhi | 18:6a4db94011d3 | 13 | * Unless required by applicable law or agreed to in writing, software |
sahilmgandhi | 18:6a4db94011d3 | 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
sahilmgandhi | 18:6a4db94011d3 | 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sahilmgandhi | 18:6a4db94011d3 | 16 | * See the License for the specific language governing permissions and |
sahilmgandhi | 18:6a4db94011d3 | 17 | * limitations under the License. |
sahilmgandhi | 18:6a4db94011d3 | 18 | * |
sahilmgandhi | 18:6a4db94011d3 | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
sahilmgandhi | 18:6a4db94011d3 | 20 | */ |
sahilmgandhi | 18:6a4db94011d3 | 21 | /* |
sahilmgandhi | 18:6a4db94011d3 | 22 | * DES, on which TDES is based, was originally designed by Horst Feistel |
sahilmgandhi | 18:6a4db94011d3 | 23 | * at IBM in 1974, and was adopted as a standard by NIST (formerly NBS). |
sahilmgandhi | 18:6a4db94011d3 | 24 | * |
sahilmgandhi | 18:6a4db94011d3 | 25 | * http://csrc.nist.gov/publications/fips/fips46-3/fips46-3.pdf |
sahilmgandhi | 18:6a4db94011d3 | 26 | */ |
sahilmgandhi | 18:6a4db94011d3 | 27 | |
sahilmgandhi | 18:6a4db94011d3 | 28 | /* Compatible with mbed OS 2 which doesn't support mbedtls */ |
sahilmgandhi | 18:6a4db94011d3 | 29 | #if MBED_CONF_RTOS_PRESENT |
sahilmgandhi | 18:6a4db94011d3 | 30 | |
sahilmgandhi | 18:6a4db94011d3 | 31 | #if !defined(MBEDTLS_CONFIG_FILE) |
sahilmgandhi | 18:6a4db94011d3 | 32 | #include "mbedtls/config.h" |
sahilmgandhi | 18:6a4db94011d3 | 33 | #else |
sahilmgandhi | 18:6a4db94011d3 | 34 | #include MBEDTLS_CONFIG_FILE |
sahilmgandhi | 18:6a4db94011d3 | 35 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 36 | |
sahilmgandhi | 18:6a4db94011d3 | 37 | #if defined(MBEDTLS_DES_C) |
sahilmgandhi | 18:6a4db94011d3 | 38 | #if defined(MBEDTLS_DES_ALT) |
sahilmgandhi | 18:6a4db94011d3 | 39 | |
sahilmgandhi | 18:6a4db94011d3 | 40 | #include "mbedtls/des.h" |
sahilmgandhi | 18:6a4db94011d3 | 41 | |
sahilmgandhi | 18:6a4db94011d3 | 42 | #include <string.h> |
sahilmgandhi | 18:6a4db94011d3 | 43 | |
sahilmgandhi | 18:6a4db94011d3 | 44 | /* Implementation that should never be optimized out by the compiler */ |
sahilmgandhi | 18:6a4db94011d3 | 45 | static void mbedtls_zeroize( void *v, size_t n ) { |
sahilmgandhi | 18:6a4db94011d3 | 46 | volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0; |
sahilmgandhi | 18:6a4db94011d3 | 47 | } |
sahilmgandhi | 18:6a4db94011d3 | 48 | |
sahilmgandhi | 18:6a4db94011d3 | 49 | /* |
sahilmgandhi | 18:6a4db94011d3 | 50 | * 32-bit integer manipulation macros (big endian) |
sahilmgandhi | 18:6a4db94011d3 | 51 | */ |
sahilmgandhi | 18:6a4db94011d3 | 52 | #ifndef GET_UINT32_BE |
sahilmgandhi | 18:6a4db94011d3 | 53 | #define GET_UINT32_BE(n,b,i) \ |
sahilmgandhi | 18:6a4db94011d3 | 54 | { \ |
sahilmgandhi | 18:6a4db94011d3 | 55 | (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ |
sahilmgandhi | 18:6a4db94011d3 | 56 | | ( (uint32_t) (b)[(i) + 1] << 16 ) \ |
sahilmgandhi | 18:6a4db94011d3 | 57 | | ( (uint32_t) (b)[(i) + 2] << 8 ) \ |
sahilmgandhi | 18:6a4db94011d3 | 58 | | ( (uint32_t) (b)[(i) + 3] ); \ |
sahilmgandhi | 18:6a4db94011d3 | 59 | } |
sahilmgandhi | 18:6a4db94011d3 | 60 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 61 | |
sahilmgandhi | 18:6a4db94011d3 | 62 | #ifndef PUT_UINT32_BE |
sahilmgandhi | 18:6a4db94011d3 | 63 | #define PUT_UINT32_BE(n,b,i) \ |
sahilmgandhi | 18:6a4db94011d3 | 64 | { \ |
sahilmgandhi | 18:6a4db94011d3 | 65 | (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ |
sahilmgandhi | 18:6a4db94011d3 | 66 | (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ |
sahilmgandhi | 18:6a4db94011d3 | 67 | (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ |
sahilmgandhi | 18:6a4db94011d3 | 68 | (b)[(i) + 3] = (unsigned char) ( (n) ); \ |
sahilmgandhi | 18:6a4db94011d3 | 69 | } |
sahilmgandhi | 18:6a4db94011d3 | 70 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 71 | |
sahilmgandhi | 18:6a4db94011d3 | 72 | /* |
sahilmgandhi | 18:6a4db94011d3 | 73 | * Expanded DES S-boxes |
sahilmgandhi | 18:6a4db94011d3 | 74 | */ |
sahilmgandhi | 18:6a4db94011d3 | 75 | static const uint32_t SB1[64] = |
sahilmgandhi | 18:6a4db94011d3 | 76 | { |
sahilmgandhi | 18:6a4db94011d3 | 77 | 0x01010400, 0x00000000, 0x00010000, 0x01010404, |
sahilmgandhi | 18:6a4db94011d3 | 78 | 0x01010004, 0x00010404, 0x00000004, 0x00010000, |
sahilmgandhi | 18:6a4db94011d3 | 79 | 0x00000400, 0x01010400, 0x01010404, 0x00000400, |
sahilmgandhi | 18:6a4db94011d3 | 80 | 0x01000404, 0x01010004, 0x01000000, 0x00000004, |
sahilmgandhi | 18:6a4db94011d3 | 81 | 0x00000404, 0x01000400, 0x01000400, 0x00010400, |
sahilmgandhi | 18:6a4db94011d3 | 82 | 0x00010400, 0x01010000, 0x01010000, 0x01000404, |
sahilmgandhi | 18:6a4db94011d3 | 83 | 0x00010004, 0x01000004, 0x01000004, 0x00010004, |
sahilmgandhi | 18:6a4db94011d3 | 84 | 0x00000000, 0x00000404, 0x00010404, 0x01000000, |
sahilmgandhi | 18:6a4db94011d3 | 85 | 0x00010000, 0x01010404, 0x00000004, 0x01010000, |
sahilmgandhi | 18:6a4db94011d3 | 86 | 0x01010400, 0x01000000, 0x01000000, 0x00000400, |
sahilmgandhi | 18:6a4db94011d3 | 87 | 0x01010004, 0x00010000, 0x00010400, 0x01000004, |
sahilmgandhi | 18:6a4db94011d3 | 88 | 0x00000400, 0x00000004, 0x01000404, 0x00010404, |
sahilmgandhi | 18:6a4db94011d3 | 89 | 0x01010404, 0x00010004, 0x01010000, 0x01000404, |
sahilmgandhi | 18:6a4db94011d3 | 90 | 0x01000004, 0x00000404, 0x00010404, 0x01010400, |
sahilmgandhi | 18:6a4db94011d3 | 91 | 0x00000404, 0x01000400, 0x01000400, 0x00000000, |
sahilmgandhi | 18:6a4db94011d3 | 92 | 0x00010004, 0x00010400, 0x00000000, 0x01010004 |
sahilmgandhi | 18:6a4db94011d3 | 93 | }; |
sahilmgandhi | 18:6a4db94011d3 | 94 | |
sahilmgandhi | 18:6a4db94011d3 | 95 | static const uint32_t SB2[64] = |
sahilmgandhi | 18:6a4db94011d3 | 96 | { |
sahilmgandhi | 18:6a4db94011d3 | 97 | 0x80108020, 0x80008000, 0x00008000, 0x00108020, |
sahilmgandhi | 18:6a4db94011d3 | 98 | 0x00100000, 0x00000020, 0x80100020, 0x80008020, |
sahilmgandhi | 18:6a4db94011d3 | 99 | 0x80000020, 0x80108020, 0x80108000, 0x80000000, |
sahilmgandhi | 18:6a4db94011d3 | 100 | 0x80008000, 0x00100000, 0x00000020, 0x80100020, |
sahilmgandhi | 18:6a4db94011d3 | 101 | 0x00108000, 0x00100020, 0x80008020, 0x00000000, |
sahilmgandhi | 18:6a4db94011d3 | 102 | 0x80000000, 0x00008000, 0x00108020, 0x80100000, |
sahilmgandhi | 18:6a4db94011d3 | 103 | 0x00100020, 0x80000020, 0x00000000, 0x00108000, |
sahilmgandhi | 18:6a4db94011d3 | 104 | 0x00008020, 0x80108000, 0x80100000, 0x00008020, |
sahilmgandhi | 18:6a4db94011d3 | 105 | 0x00000000, 0x00108020, 0x80100020, 0x00100000, |
sahilmgandhi | 18:6a4db94011d3 | 106 | 0x80008020, 0x80100000, 0x80108000, 0x00008000, |
sahilmgandhi | 18:6a4db94011d3 | 107 | 0x80100000, 0x80008000, 0x00000020, 0x80108020, |
sahilmgandhi | 18:6a4db94011d3 | 108 | 0x00108020, 0x00000020, 0x00008000, 0x80000000, |
sahilmgandhi | 18:6a4db94011d3 | 109 | 0x00008020, 0x80108000, 0x00100000, 0x80000020, |
sahilmgandhi | 18:6a4db94011d3 | 110 | 0x00100020, 0x80008020, 0x80000020, 0x00100020, |
sahilmgandhi | 18:6a4db94011d3 | 111 | 0x00108000, 0x00000000, 0x80008000, 0x00008020, |
sahilmgandhi | 18:6a4db94011d3 | 112 | 0x80000000, 0x80100020, 0x80108020, 0x00108000 |
sahilmgandhi | 18:6a4db94011d3 | 113 | }; |
sahilmgandhi | 18:6a4db94011d3 | 114 | |
sahilmgandhi | 18:6a4db94011d3 | 115 | static const uint32_t SB3[64] = |
sahilmgandhi | 18:6a4db94011d3 | 116 | { |
sahilmgandhi | 18:6a4db94011d3 | 117 | 0x00000208, 0x08020200, 0x00000000, 0x08020008, |
sahilmgandhi | 18:6a4db94011d3 | 118 | 0x08000200, 0x00000000, 0x00020208, 0x08000200, |
sahilmgandhi | 18:6a4db94011d3 | 119 | 0x00020008, 0x08000008, 0x08000008, 0x00020000, |
sahilmgandhi | 18:6a4db94011d3 | 120 | 0x08020208, 0x00020008, 0x08020000, 0x00000208, |
sahilmgandhi | 18:6a4db94011d3 | 121 | 0x08000000, 0x00000008, 0x08020200, 0x00000200, |
sahilmgandhi | 18:6a4db94011d3 | 122 | 0x00020200, 0x08020000, 0x08020008, 0x00020208, |
sahilmgandhi | 18:6a4db94011d3 | 123 | 0x08000208, 0x00020200, 0x00020000, 0x08000208, |
sahilmgandhi | 18:6a4db94011d3 | 124 | 0x00000008, 0x08020208, 0x00000200, 0x08000000, |
sahilmgandhi | 18:6a4db94011d3 | 125 | 0x08020200, 0x08000000, 0x00020008, 0x00000208, |
sahilmgandhi | 18:6a4db94011d3 | 126 | 0x00020000, 0x08020200, 0x08000200, 0x00000000, |
sahilmgandhi | 18:6a4db94011d3 | 127 | 0x00000200, 0x00020008, 0x08020208, 0x08000200, |
sahilmgandhi | 18:6a4db94011d3 | 128 | 0x08000008, 0x00000200, 0x00000000, 0x08020008, |
sahilmgandhi | 18:6a4db94011d3 | 129 | 0x08000208, 0x00020000, 0x08000000, 0x08020208, |
sahilmgandhi | 18:6a4db94011d3 | 130 | 0x00000008, 0x00020208, 0x00020200, 0x08000008, |
sahilmgandhi | 18:6a4db94011d3 | 131 | 0x08020000, 0x08000208, 0x00000208, 0x08020000, |
sahilmgandhi | 18:6a4db94011d3 | 132 | 0x00020208, 0x00000008, 0x08020008, 0x00020200 |
sahilmgandhi | 18:6a4db94011d3 | 133 | }; |
sahilmgandhi | 18:6a4db94011d3 | 134 | |
sahilmgandhi | 18:6a4db94011d3 | 135 | static const uint32_t SB4[64] = |
sahilmgandhi | 18:6a4db94011d3 | 136 | { |
sahilmgandhi | 18:6a4db94011d3 | 137 | 0x00802001, 0x00002081, 0x00002081, 0x00000080, |
sahilmgandhi | 18:6a4db94011d3 | 138 | 0x00802080, 0x00800081, 0x00800001, 0x00002001, |
sahilmgandhi | 18:6a4db94011d3 | 139 | 0x00000000, 0x00802000, 0x00802000, 0x00802081, |
sahilmgandhi | 18:6a4db94011d3 | 140 | 0x00000081, 0x00000000, 0x00800080, 0x00800001, |
sahilmgandhi | 18:6a4db94011d3 | 141 | 0x00000001, 0x00002000, 0x00800000, 0x00802001, |
sahilmgandhi | 18:6a4db94011d3 | 142 | 0x00000080, 0x00800000, 0x00002001, 0x00002080, |
sahilmgandhi | 18:6a4db94011d3 | 143 | 0x00800081, 0x00000001, 0x00002080, 0x00800080, |
sahilmgandhi | 18:6a4db94011d3 | 144 | 0x00002000, 0x00802080, 0x00802081, 0x00000081, |
sahilmgandhi | 18:6a4db94011d3 | 145 | 0x00800080, 0x00800001, 0x00802000, 0x00802081, |
sahilmgandhi | 18:6a4db94011d3 | 146 | 0x00000081, 0x00000000, 0x00000000, 0x00802000, |
sahilmgandhi | 18:6a4db94011d3 | 147 | 0x00002080, 0x00800080, 0x00800081, 0x00000001, |
sahilmgandhi | 18:6a4db94011d3 | 148 | 0x00802001, 0x00002081, 0x00002081, 0x00000080, |
sahilmgandhi | 18:6a4db94011d3 | 149 | 0x00802081, 0x00000081, 0x00000001, 0x00002000, |
sahilmgandhi | 18:6a4db94011d3 | 150 | 0x00800001, 0x00002001, 0x00802080, 0x00800081, |
sahilmgandhi | 18:6a4db94011d3 | 151 | 0x00002001, 0x00002080, 0x00800000, 0x00802001, |
sahilmgandhi | 18:6a4db94011d3 | 152 | 0x00000080, 0x00800000, 0x00002000, 0x00802080 |
sahilmgandhi | 18:6a4db94011d3 | 153 | }; |
sahilmgandhi | 18:6a4db94011d3 | 154 | |
sahilmgandhi | 18:6a4db94011d3 | 155 | static const uint32_t SB5[64] = |
sahilmgandhi | 18:6a4db94011d3 | 156 | { |
sahilmgandhi | 18:6a4db94011d3 | 157 | 0x00000100, 0x02080100, 0x02080000, 0x42000100, |
sahilmgandhi | 18:6a4db94011d3 | 158 | 0x00080000, 0x00000100, 0x40000000, 0x02080000, |
sahilmgandhi | 18:6a4db94011d3 | 159 | 0x40080100, 0x00080000, 0x02000100, 0x40080100, |
sahilmgandhi | 18:6a4db94011d3 | 160 | 0x42000100, 0x42080000, 0x00080100, 0x40000000, |
sahilmgandhi | 18:6a4db94011d3 | 161 | 0x02000000, 0x40080000, 0x40080000, 0x00000000, |
sahilmgandhi | 18:6a4db94011d3 | 162 | 0x40000100, 0x42080100, 0x42080100, 0x02000100, |
sahilmgandhi | 18:6a4db94011d3 | 163 | 0x42080000, 0x40000100, 0x00000000, 0x42000000, |
sahilmgandhi | 18:6a4db94011d3 | 164 | 0x02080100, 0x02000000, 0x42000000, 0x00080100, |
sahilmgandhi | 18:6a4db94011d3 | 165 | 0x00080000, 0x42000100, 0x00000100, 0x02000000, |
sahilmgandhi | 18:6a4db94011d3 | 166 | 0x40000000, 0x02080000, 0x42000100, 0x40080100, |
sahilmgandhi | 18:6a4db94011d3 | 167 | 0x02000100, 0x40000000, 0x42080000, 0x02080100, |
sahilmgandhi | 18:6a4db94011d3 | 168 | 0x40080100, 0x00000100, 0x02000000, 0x42080000, |
sahilmgandhi | 18:6a4db94011d3 | 169 | 0x42080100, 0x00080100, 0x42000000, 0x42080100, |
sahilmgandhi | 18:6a4db94011d3 | 170 | 0x02080000, 0x00000000, 0x40080000, 0x42000000, |
sahilmgandhi | 18:6a4db94011d3 | 171 | 0x00080100, 0x02000100, 0x40000100, 0x00080000, |
sahilmgandhi | 18:6a4db94011d3 | 172 | 0x00000000, 0x40080000, 0x02080100, 0x40000100 |
sahilmgandhi | 18:6a4db94011d3 | 173 | }; |
sahilmgandhi | 18:6a4db94011d3 | 174 | |
sahilmgandhi | 18:6a4db94011d3 | 175 | static const uint32_t SB6[64] = |
sahilmgandhi | 18:6a4db94011d3 | 176 | { |
sahilmgandhi | 18:6a4db94011d3 | 177 | 0x20000010, 0x20400000, 0x00004000, 0x20404010, |
sahilmgandhi | 18:6a4db94011d3 | 178 | 0x20400000, 0x00000010, 0x20404010, 0x00400000, |
sahilmgandhi | 18:6a4db94011d3 | 179 | 0x20004000, 0x00404010, 0x00400000, 0x20000010, |
sahilmgandhi | 18:6a4db94011d3 | 180 | 0x00400010, 0x20004000, 0x20000000, 0x00004010, |
sahilmgandhi | 18:6a4db94011d3 | 181 | 0x00000000, 0x00400010, 0x20004010, 0x00004000, |
sahilmgandhi | 18:6a4db94011d3 | 182 | 0x00404000, 0x20004010, 0x00000010, 0x20400010, |
sahilmgandhi | 18:6a4db94011d3 | 183 | 0x20400010, 0x00000000, 0x00404010, 0x20404000, |
sahilmgandhi | 18:6a4db94011d3 | 184 | 0x00004010, 0x00404000, 0x20404000, 0x20000000, |
sahilmgandhi | 18:6a4db94011d3 | 185 | 0x20004000, 0x00000010, 0x20400010, 0x00404000, |
sahilmgandhi | 18:6a4db94011d3 | 186 | 0x20404010, 0x00400000, 0x00004010, 0x20000010, |
sahilmgandhi | 18:6a4db94011d3 | 187 | 0x00400000, 0x20004000, 0x20000000, 0x00004010, |
sahilmgandhi | 18:6a4db94011d3 | 188 | 0x20000010, 0x20404010, 0x00404000, 0x20400000, |
sahilmgandhi | 18:6a4db94011d3 | 189 | 0x00404010, 0x20404000, 0x00000000, 0x20400010, |
sahilmgandhi | 18:6a4db94011d3 | 190 | 0x00000010, 0x00004000, 0x20400000, 0x00404010, |
sahilmgandhi | 18:6a4db94011d3 | 191 | 0x00004000, 0x00400010, 0x20004010, 0x00000000, |
sahilmgandhi | 18:6a4db94011d3 | 192 | 0x20404000, 0x20000000, 0x00400010, 0x20004010 |
sahilmgandhi | 18:6a4db94011d3 | 193 | }; |
sahilmgandhi | 18:6a4db94011d3 | 194 | |
sahilmgandhi | 18:6a4db94011d3 | 195 | static const uint32_t SB7[64] = |
sahilmgandhi | 18:6a4db94011d3 | 196 | { |
sahilmgandhi | 18:6a4db94011d3 | 197 | 0x00200000, 0x04200002, 0x04000802, 0x00000000, |
sahilmgandhi | 18:6a4db94011d3 | 198 | 0x00000800, 0x04000802, 0x00200802, 0x04200800, |
sahilmgandhi | 18:6a4db94011d3 | 199 | 0x04200802, 0x00200000, 0x00000000, 0x04000002, |
sahilmgandhi | 18:6a4db94011d3 | 200 | 0x00000002, 0x04000000, 0x04200002, 0x00000802, |
sahilmgandhi | 18:6a4db94011d3 | 201 | 0x04000800, 0x00200802, 0x00200002, 0x04000800, |
sahilmgandhi | 18:6a4db94011d3 | 202 | 0x04000002, 0x04200000, 0x04200800, 0x00200002, |
sahilmgandhi | 18:6a4db94011d3 | 203 | 0x04200000, 0x00000800, 0x00000802, 0x04200802, |
sahilmgandhi | 18:6a4db94011d3 | 204 | 0x00200800, 0x00000002, 0x04000000, 0x00200800, |
sahilmgandhi | 18:6a4db94011d3 | 205 | 0x04000000, 0x00200800, 0x00200000, 0x04000802, |
sahilmgandhi | 18:6a4db94011d3 | 206 | 0x04000802, 0x04200002, 0x04200002, 0x00000002, |
sahilmgandhi | 18:6a4db94011d3 | 207 | 0x00200002, 0x04000000, 0x04000800, 0x00200000, |
sahilmgandhi | 18:6a4db94011d3 | 208 | 0x04200800, 0x00000802, 0x00200802, 0x04200800, |
sahilmgandhi | 18:6a4db94011d3 | 209 | 0x00000802, 0x04000002, 0x04200802, 0x04200000, |
sahilmgandhi | 18:6a4db94011d3 | 210 | 0x00200800, 0x00000000, 0x00000002, 0x04200802, |
sahilmgandhi | 18:6a4db94011d3 | 211 | 0x00000000, 0x00200802, 0x04200000, 0x00000800, |
sahilmgandhi | 18:6a4db94011d3 | 212 | 0x04000002, 0x04000800, 0x00000800, 0x00200002 |
sahilmgandhi | 18:6a4db94011d3 | 213 | }; |
sahilmgandhi | 18:6a4db94011d3 | 214 | |
sahilmgandhi | 18:6a4db94011d3 | 215 | static const uint32_t SB8[64] = |
sahilmgandhi | 18:6a4db94011d3 | 216 | { |
sahilmgandhi | 18:6a4db94011d3 | 217 | 0x10001040, 0x00001000, 0x00040000, 0x10041040, |
sahilmgandhi | 18:6a4db94011d3 | 218 | 0x10000000, 0x10001040, 0x00000040, 0x10000000, |
sahilmgandhi | 18:6a4db94011d3 | 219 | 0x00040040, 0x10040000, 0x10041040, 0x00041000, |
sahilmgandhi | 18:6a4db94011d3 | 220 | 0x10041000, 0x00041040, 0x00001000, 0x00000040, |
sahilmgandhi | 18:6a4db94011d3 | 221 | 0x10040000, 0x10000040, 0x10001000, 0x00001040, |
sahilmgandhi | 18:6a4db94011d3 | 222 | 0x00041000, 0x00040040, 0x10040040, 0x10041000, |
sahilmgandhi | 18:6a4db94011d3 | 223 | 0x00001040, 0x00000000, 0x00000000, 0x10040040, |
sahilmgandhi | 18:6a4db94011d3 | 224 | 0x10000040, 0x10001000, 0x00041040, 0x00040000, |
sahilmgandhi | 18:6a4db94011d3 | 225 | 0x00041040, 0x00040000, 0x10041000, 0x00001000, |
sahilmgandhi | 18:6a4db94011d3 | 226 | 0x00000040, 0x10040040, 0x00001000, 0x00041040, |
sahilmgandhi | 18:6a4db94011d3 | 227 | 0x10001000, 0x00000040, 0x10000040, 0x10040000, |
sahilmgandhi | 18:6a4db94011d3 | 228 | 0x10040040, 0x10000000, 0x00040000, 0x10001040, |
sahilmgandhi | 18:6a4db94011d3 | 229 | 0x00000000, 0x10041040, 0x00040040, 0x10000040, |
sahilmgandhi | 18:6a4db94011d3 | 230 | 0x10040000, 0x10001000, 0x10001040, 0x00000000, |
sahilmgandhi | 18:6a4db94011d3 | 231 | 0x10041040, 0x00041000, 0x00041000, 0x00001040, |
sahilmgandhi | 18:6a4db94011d3 | 232 | 0x00001040, 0x00040040, 0x10000000, 0x10041000 |
sahilmgandhi | 18:6a4db94011d3 | 233 | }; |
sahilmgandhi | 18:6a4db94011d3 | 234 | |
sahilmgandhi | 18:6a4db94011d3 | 235 | /* |
sahilmgandhi | 18:6a4db94011d3 | 236 | * PC1: left and right halves bit-swap |
sahilmgandhi | 18:6a4db94011d3 | 237 | */ |
sahilmgandhi | 18:6a4db94011d3 | 238 | static const uint32_t LHs[16] = |
sahilmgandhi | 18:6a4db94011d3 | 239 | { |
sahilmgandhi | 18:6a4db94011d3 | 240 | 0x00000000, 0x00000001, 0x00000100, 0x00000101, |
sahilmgandhi | 18:6a4db94011d3 | 241 | 0x00010000, 0x00010001, 0x00010100, 0x00010101, |
sahilmgandhi | 18:6a4db94011d3 | 242 | 0x01000000, 0x01000001, 0x01000100, 0x01000101, |
sahilmgandhi | 18:6a4db94011d3 | 243 | 0x01010000, 0x01010001, 0x01010100, 0x01010101 |
sahilmgandhi | 18:6a4db94011d3 | 244 | }; |
sahilmgandhi | 18:6a4db94011d3 | 245 | |
sahilmgandhi | 18:6a4db94011d3 | 246 | static const uint32_t RHs[16] = |
sahilmgandhi | 18:6a4db94011d3 | 247 | { |
sahilmgandhi | 18:6a4db94011d3 | 248 | 0x00000000, 0x01000000, 0x00010000, 0x01010000, |
sahilmgandhi | 18:6a4db94011d3 | 249 | 0x00000100, 0x01000100, 0x00010100, 0x01010100, |
sahilmgandhi | 18:6a4db94011d3 | 250 | 0x00000001, 0x01000001, 0x00010001, 0x01010001, |
sahilmgandhi | 18:6a4db94011d3 | 251 | 0x00000101, 0x01000101, 0x00010101, 0x01010101, |
sahilmgandhi | 18:6a4db94011d3 | 252 | }; |
sahilmgandhi | 18:6a4db94011d3 | 253 | |
sahilmgandhi | 18:6a4db94011d3 | 254 | /* |
sahilmgandhi | 18:6a4db94011d3 | 255 | * Initial Permutation macro |
sahilmgandhi | 18:6a4db94011d3 | 256 | */ |
sahilmgandhi | 18:6a4db94011d3 | 257 | #define DES_IP(X,Y) \ |
sahilmgandhi | 18:6a4db94011d3 | 258 | { \ |
sahilmgandhi | 18:6a4db94011d3 | 259 | T = ((X >> 4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T << 4); \ |
sahilmgandhi | 18:6a4db94011d3 | 260 | T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16); \ |
sahilmgandhi | 18:6a4db94011d3 | 261 | T = ((Y >> 2) ^ X) & 0x33333333; X ^= T; Y ^= (T << 2); \ |
sahilmgandhi | 18:6a4db94011d3 | 262 | T = ((Y >> 8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T << 8); \ |
sahilmgandhi | 18:6a4db94011d3 | 263 | Y = ((Y << 1) | (Y >> 31)) & 0xFFFFFFFF; \ |
sahilmgandhi | 18:6a4db94011d3 | 264 | T = (X ^ Y) & 0xAAAAAAAA; Y ^= T; X ^= T; \ |
sahilmgandhi | 18:6a4db94011d3 | 265 | X = ((X << 1) | (X >> 31)) & 0xFFFFFFFF; \ |
sahilmgandhi | 18:6a4db94011d3 | 266 | } |
sahilmgandhi | 18:6a4db94011d3 | 267 | |
sahilmgandhi | 18:6a4db94011d3 | 268 | /* |
sahilmgandhi | 18:6a4db94011d3 | 269 | * Final Permutation macro |
sahilmgandhi | 18:6a4db94011d3 | 270 | */ |
sahilmgandhi | 18:6a4db94011d3 | 271 | #define DES_FP(X,Y) \ |
sahilmgandhi | 18:6a4db94011d3 | 272 | { \ |
sahilmgandhi | 18:6a4db94011d3 | 273 | X = ((X << 31) | (X >> 1)) & 0xFFFFFFFF; \ |
sahilmgandhi | 18:6a4db94011d3 | 274 | T = (X ^ Y) & 0xAAAAAAAA; X ^= T; Y ^= T; \ |
sahilmgandhi | 18:6a4db94011d3 | 275 | Y = ((Y << 31) | (Y >> 1)) & 0xFFFFFFFF; \ |
sahilmgandhi | 18:6a4db94011d3 | 276 | T = ((Y >> 8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T << 8); \ |
sahilmgandhi | 18:6a4db94011d3 | 277 | T = ((Y >> 2) ^ X) & 0x33333333; X ^= T; Y ^= (T << 2); \ |
sahilmgandhi | 18:6a4db94011d3 | 278 | T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16); \ |
sahilmgandhi | 18:6a4db94011d3 | 279 | T = ((X >> 4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T << 4); \ |
sahilmgandhi | 18:6a4db94011d3 | 280 | } |
sahilmgandhi | 18:6a4db94011d3 | 281 | |
sahilmgandhi | 18:6a4db94011d3 | 282 | /* |
sahilmgandhi | 18:6a4db94011d3 | 283 | * DES round macro |
sahilmgandhi | 18:6a4db94011d3 | 284 | */ |
sahilmgandhi | 18:6a4db94011d3 | 285 | #define DES_ROUND(X,Y) \ |
sahilmgandhi | 18:6a4db94011d3 | 286 | { \ |
sahilmgandhi | 18:6a4db94011d3 | 287 | T = *SK++ ^ X; \ |
sahilmgandhi | 18:6a4db94011d3 | 288 | Y ^= SB8[ (T ) & 0x3F ] ^ \ |
sahilmgandhi | 18:6a4db94011d3 | 289 | SB6[ (T >> 8) & 0x3F ] ^ \ |
sahilmgandhi | 18:6a4db94011d3 | 290 | SB4[ (T >> 16) & 0x3F ] ^ \ |
sahilmgandhi | 18:6a4db94011d3 | 291 | SB2[ (T >> 24) & 0x3F ]; \ |
sahilmgandhi | 18:6a4db94011d3 | 292 | \ |
sahilmgandhi | 18:6a4db94011d3 | 293 | T = *SK++ ^ ((X << 28) | (X >> 4)); \ |
sahilmgandhi | 18:6a4db94011d3 | 294 | Y ^= SB7[ (T ) & 0x3F ] ^ \ |
sahilmgandhi | 18:6a4db94011d3 | 295 | SB5[ (T >> 8) & 0x3F ] ^ \ |
sahilmgandhi | 18:6a4db94011d3 | 296 | SB3[ (T >> 16) & 0x3F ] ^ \ |
sahilmgandhi | 18:6a4db94011d3 | 297 | SB1[ (T >> 24) & 0x3F ]; \ |
sahilmgandhi | 18:6a4db94011d3 | 298 | } |
sahilmgandhi | 18:6a4db94011d3 | 299 | |
sahilmgandhi | 18:6a4db94011d3 | 300 | #define SWAP(a,b) { uint32_t t = a; a = b; b = t; t = 0; } |
sahilmgandhi | 18:6a4db94011d3 | 301 | |
sahilmgandhi | 18:6a4db94011d3 | 302 | void mbedtls_des_sw_init( mbedtls_des_sw_context *ctx ) |
sahilmgandhi | 18:6a4db94011d3 | 303 | { |
sahilmgandhi | 18:6a4db94011d3 | 304 | memset( ctx, 0, sizeof( mbedtls_des_sw_context ) ); |
sahilmgandhi | 18:6a4db94011d3 | 305 | } |
sahilmgandhi | 18:6a4db94011d3 | 306 | |
sahilmgandhi | 18:6a4db94011d3 | 307 | void mbedtls_des_sw_free( mbedtls_des_sw_context *ctx ) |
sahilmgandhi | 18:6a4db94011d3 | 308 | { |
sahilmgandhi | 18:6a4db94011d3 | 309 | if( ctx == NULL ) |
sahilmgandhi | 18:6a4db94011d3 | 310 | return; |
sahilmgandhi | 18:6a4db94011d3 | 311 | |
sahilmgandhi | 18:6a4db94011d3 | 312 | mbedtls_zeroize( ctx, sizeof( mbedtls_des_sw_context ) ); |
sahilmgandhi | 18:6a4db94011d3 | 313 | } |
sahilmgandhi | 18:6a4db94011d3 | 314 | |
sahilmgandhi | 18:6a4db94011d3 | 315 | void mbedtls_des3_sw_init( mbedtls_des3_sw_context *ctx ) |
sahilmgandhi | 18:6a4db94011d3 | 316 | { |
sahilmgandhi | 18:6a4db94011d3 | 317 | memset( ctx, 0, sizeof( mbedtls_des3_sw_context ) ); |
sahilmgandhi | 18:6a4db94011d3 | 318 | } |
sahilmgandhi | 18:6a4db94011d3 | 319 | |
sahilmgandhi | 18:6a4db94011d3 | 320 | void mbedtls_des3_sw_free( mbedtls_des3_sw_context *ctx ) |
sahilmgandhi | 18:6a4db94011d3 | 321 | { |
sahilmgandhi | 18:6a4db94011d3 | 322 | if( ctx == NULL ) |
sahilmgandhi | 18:6a4db94011d3 | 323 | return; |
sahilmgandhi | 18:6a4db94011d3 | 324 | |
sahilmgandhi | 18:6a4db94011d3 | 325 | mbedtls_zeroize( ctx, sizeof( mbedtls_des3_sw_context ) ); |
sahilmgandhi | 18:6a4db94011d3 | 326 | } |
sahilmgandhi | 18:6a4db94011d3 | 327 | |
sahilmgandhi | 18:6a4db94011d3 | 328 | static const unsigned char odd_parity_table[128] = { 1, 2, 4, 7, 8, |
sahilmgandhi | 18:6a4db94011d3 | 329 | 11, 13, 14, 16, 19, 21, 22, 25, 26, 28, 31, 32, 35, 37, 38, 41, 42, 44, |
sahilmgandhi | 18:6a4db94011d3 | 330 | 47, 49, 50, 52, 55, 56, 59, 61, 62, 64, 67, 69, 70, 73, 74, 76, 79, 81, |
sahilmgandhi | 18:6a4db94011d3 | 331 | 82, 84, 87, 88, 91, 93, 94, 97, 98, 100, 103, 104, 107, 109, 110, 112, |
sahilmgandhi | 18:6a4db94011d3 | 332 | 115, 117, 118, 121, 122, 124, 127, 128, 131, 133, 134, 137, 138, 140, |
sahilmgandhi | 18:6a4db94011d3 | 333 | 143, 145, 146, 148, 151, 152, 155, 157, 158, 161, 162, 164, 167, 168, |
sahilmgandhi | 18:6a4db94011d3 | 334 | 171, 173, 174, 176, 179, 181, 182, 185, 186, 188, 191, 193, 194, 196, |
sahilmgandhi | 18:6a4db94011d3 | 335 | 199, 200, 203, 205, 206, 208, 211, 213, 214, 217, 218, 220, 223, 224, |
sahilmgandhi | 18:6a4db94011d3 | 336 | 227, 229, 230, 233, 234, 236, 239, 241, 242, 244, 247, 248, 251, 253, |
sahilmgandhi | 18:6a4db94011d3 | 337 | 254 }; |
sahilmgandhi | 18:6a4db94011d3 | 338 | |
sahilmgandhi | 18:6a4db94011d3 | 339 | void mbedtls_des_sw_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] ) |
sahilmgandhi | 18:6a4db94011d3 | 340 | { |
sahilmgandhi | 18:6a4db94011d3 | 341 | int i; |
sahilmgandhi | 18:6a4db94011d3 | 342 | |
sahilmgandhi | 18:6a4db94011d3 | 343 | for( i = 0; i < MBEDTLS_DES_KEY_SIZE; i++ ) |
sahilmgandhi | 18:6a4db94011d3 | 344 | key[i] = odd_parity_table[key[i] / 2]; |
sahilmgandhi | 18:6a4db94011d3 | 345 | } |
sahilmgandhi | 18:6a4db94011d3 | 346 | |
sahilmgandhi | 18:6a4db94011d3 | 347 | /* |
sahilmgandhi | 18:6a4db94011d3 | 348 | * Check the given key's parity, returns 1 on failure, 0 on SUCCESS |
sahilmgandhi | 18:6a4db94011d3 | 349 | */ |
sahilmgandhi | 18:6a4db94011d3 | 350 | int mbedtls_des_sw_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SIZE] ) |
sahilmgandhi | 18:6a4db94011d3 | 351 | { |
sahilmgandhi | 18:6a4db94011d3 | 352 | int i; |
sahilmgandhi | 18:6a4db94011d3 | 353 | |
sahilmgandhi | 18:6a4db94011d3 | 354 | for( i = 0; i < MBEDTLS_DES_KEY_SIZE; i++ ) |
sahilmgandhi | 18:6a4db94011d3 | 355 | if( key[i] != odd_parity_table[key[i] / 2] ) |
sahilmgandhi | 18:6a4db94011d3 | 356 | return( 1 ); |
sahilmgandhi | 18:6a4db94011d3 | 357 | |
sahilmgandhi | 18:6a4db94011d3 | 358 | return( 0 ); |
sahilmgandhi | 18:6a4db94011d3 | 359 | } |
sahilmgandhi | 18:6a4db94011d3 | 360 | |
sahilmgandhi | 18:6a4db94011d3 | 361 | /* |
sahilmgandhi | 18:6a4db94011d3 | 362 | * Table of weak and semi-weak keys |
sahilmgandhi | 18:6a4db94011d3 | 363 | * |
sahilmgandhi | 18:6a4db94011d3 | 364 | * Source: http://en.wikipedia.org/wiki/Weak_key |
sahilmgandhi | 18:6a4db94011d3 | 365 | * |
sahilmgandhi | 18:6a4db94011d3 | 366 | * Weak: |
sahilmgandhi | 18:6a4db94011d3 | 367 | * Alternating ones + zeros (0x0101010101010101) |
sahilmgandhi | 18:6a4db94011d3 | 368 | * Alternating 'F' + 'E' (0xFEFEFEFEFEFEFEFE) |
sahilmgandhi | 18:6a4db94011d3 | 369 | * '0xE0E0E0E0F1F1F1F1' |
sahilmgandhi | 18:6a4db94011d3 | 370 | * '0x1F1F1F1F0E0E0E0E' |
sahilmgandhi | 18:6a4db94011d3 | 371 | * |
sahilmgandhi | 18:6a4db94011d3 | 372 | * Semi-weak: |
sahilmgandhi | 18:6a4db94011d3 | 373 | * 0x011F011F010E010E and 0x1F011F010E010E01 |
sahilmgandhi | 18:6a4db94011d3 | 374 | * 0x01E001E001F101F1 and 0xE001E001F101F101 |
sahilmgandhi | 18:6a4db94011d3 | 375 | * 0x01FE01FE01FE01FE and 0xFE01FE01FE01FE01 |
sahilmgandhi | 18:6a4db94011d3 | 376 | * 0x1FE01FE00EF10EF1 and 0xE01FE01FF10EF10E |
sahilmgandhi | 18:6a4db94011d3 | 377 | * 0x1FFE1FFE0EFE0EFE and 0xFE1FFE1FFE0EFE0E |
sahilmgandhi | 18:6a4db94011d3 | 378 | * 0xE0FEE0FEF1FEF1FE and 0xFEE0FEE0FEF1FEF1 |
sahilmgandhi | 18:6a4db94011d3 | 379 | * |
sahilmgandhi | 18:6a4db94011d3 | 380 | */ |
sahilmgandhi | 18:6a4db94011d3 | 381 | |
sahilmgandhi | 18:6a4db94011d3 | 382 | #define WEAK_KEY_COUNT 16 |
sahilmgandhi | 18:6a4db94011d3 | 383 | |
sahilmgandhi | 18:6a4db94011d3 | 384 | static const unsigned char weak_key_table[WEAK_KEY_COUNT][MBEDTLS_DES_KEY_SIZE] = |
sahilmgandhi | 18:6a4db94011d3 | 385 | { |
sahilmgandhi | 18:6a4db94011d3 | 386 | { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 }, |
sahilmgandhi | 18:6a4db94011d3 | 387 | { 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE }, |
sahilmgandhi | 18:6a4db94011d3 | 388 | { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E }, |
sahilmgandhi | 18:6a4db94011d3 | 389 | { 0xE0, 0xE0, 0xE0, 0xE0, 0xF1, 0xF1, 0xF1, 0xF1 }, |
sahilmgandhi | 18:6a4db94011d3 | 390 | |
sahilmgandhi | 18:6a4db94011d3 | 391 | { 0x01, 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E }, |
sahilmgandhi | 18:6a4db94011d3 | 392 | { 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E, 0x01 }, |
sahilmgandhi | 18:6a4db94011d3 | 393 | { 0x01, 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1 }, |
sahilmgandhi | 18:6a4db94011d3 | 394 | { 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1, 0x01 }, |
sahilmgandhi | 18:6a4db94011d3 | 395 | { 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE }, |
sahilmgandhi | 18:6a4db94011d3 | 396 | { 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01 }, |
sahilmgandhi | 18:6a4db94011d3 | 397 | { 0x1F, 0xE0, 0x1F, 0xE0, 0x0E, 0xF1, 0x0E, 0xF1 }, |
sahilmgandhi | 18:6a4db94011d3 | 398 | { 0xE0, 0x1F, 0xE0, 0x1F, 0xF1, 0x0E, 0xF1, 0x0E }, |
sahilmgandhi | 18:6a4db94011d3 | 399 | { 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE }, |
sahilmgandhi | 18:6a4db94011d3 | 400 | { 0xFE, 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E }, |
sahilmgandhi | 18:6a4db94011d3 | 401 | { 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE }, |
sahilmgandhi | 18:6a4db94011d3 | 402 | { 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1 } |
sahilmgandhi | 18:6a4db94011d3 | 403 | }; |
sahilmgandhi | 18:6a4db94011d3 | 404 | |
sahilmgandhi | 18:6a4db94011d3 | 405 | int mbedtls_des_sw_key_check_weak( const unsigned char key[MBEDTLS_DES_KEY_SIZE] ) |
sahilmgandhi | 18:6a4db94011d3 | 406 | { |
sahilmgandhi | 18:6a4db94011d3 | 407 | int i; |
sahilmgandhi | 18:6a4db94011d3 | 408 | |
sahilmgandhi | 18:6a4db94011d3 | 409 | for( i = 0; i < WEAK_KEY_COUNT; i++ ) |
sahilmgandhi | 18:6a4db94011d3 | 410 | if( memcmp( weak_key_table[i], key, MBEDTLS_DES_KEY_SIZE) == 0 ) |
sahilmgandhi | 18:6a4db94011d3 | 411 | return( 1 ); |
sahilmgandhi | 18:6a4db94011d3 | 412 | |
sahilmgandhi | 18:6a4db94011d3 | 413 | return( 0 ); |
sahilmgandhi | 18:6a4db94011d3 | 414 | } |
sahilmgandhi | 18:6a4db94011d3 | 415 | |
sahilmgandhi | 18:6a4db94011d3 | 416 | void mbedtls_des_setkey( uint32_t SK[32], const unsigned char key[MBEDTLS_DES_KEY_SIZE] ) |
sahilmgandhi | 18:6a4db94011d3 | 417 | { |
sahilmgandhi | 18:6a4db94011d3 | 418 | int i; |
sahilmgandhi | 18:6a4db94011d3 | 419 | uint32_t X, Y, T; |
sahilmgandhi | 18:6a4db94011d3 | 420 | |
sahilmgandhi | 18:6a4db94011d3 | 421 | GET_UINT32_BE( X, key, 0 ); |
sahilmgandhi | 18:6a4db94011d3 | 422 | GET_UINT32_BE( Y, key, 4 ); |
sahilmgandhi | 18:6a4db94011d3 | 423 | |
sahilmgandhi | 18:6a4db94011d3 | 424 | /* |
sahilmgandhi | 18:6a4db94011d3 | 425 | * Permuted Choice 1 |
sahilmgandhi | 18:6a4db94011d3 | 426 | */ |
sahilmgandhi | 18:6a4db94011d3 | 427 | T = ((Y >> 4) ^ X) & 0x0F0F0F0F; X ^= T; Y ^= (T << 4); |
sahilmgandhi | 18:6a4db94011d3 | 428 | T = ((Y ) ^ X) & 0x10101010; X ^= T; Y ^= (T ); |
sahilmgandhi | 18:6a4db94011d3 | 429 | |
sahilmgandhi | 18:6a4db94011d3 | 430 | X = (LHs[ (X ) & 0xF] << 3) | (LHs[ (X >> 8) & 0xF ] << 2) |
sahilmgandhi | 18:6a4db94011d3 | 431 | | (LHs[ (X >> 16) & 0xF] << 1) | (LHs[ (X >> 24) & 0xF ] ) |
sahilmgandhi | 18:6a4db94011d3 | 432 | | (LHs[ (X >> 5) & 0xF] << 7) | (LHs[ (X >> 13) & 0xF ] << 6) |
sahilmgandhi | 18:6a4db94011d3 | 433 | | (LHs[ (X >> 21) & 0xF] << 5) | (LHs[ (X >> 29) & 0xF ] << 4); |
sahilmgandhi | 18:6a4db94011d3 | 434 | |
sahilmgandhi | 18:6a4db94011d3 | 435 | Y = (RHs[ (Y >> 1) & 0xF] << 3) | (RHs[ (Y >> 9) & 0xF ] << 2) |
sahilmgandhi | 18:6a4db94011d3 | 436 | | (RHs[ (Y >> 17) & 0xF] << 1) | (RHs[ (Y >> 25) & 0xF ] ) |
sahilmgandhi | 18:6a4db94011d3 | 437 | | (RHs[ (Y >> 4) & 0xF] << 7) | (RHs[ (Y >> 12) & 0xF ] << 6) |
sahilmgandhi | 18:6a4db94011d3 | 438 | | (RHs[ (Y >> 20) & 0xF] << 5) | (RHs[ (Y >> 28) & 0xF ] << 4); |
sahilmgandhi | 18:6a4db94011d3 | 439 | |
sahilmgandhi | 18:6a4db94011d3 | 440 | X &= 0x0FFFFFFF; |
sahilmgandhi | 18:6a4db94011d3 | 441 | Y &= 0x0FFFFFFF; |
sahilmgandhi | 18:6a4db94011d3 | 442 | |
sahilmgandhi | 18:6a4db94011d3 | 443 | /* |
sahilmgandhi | 18:6a4db94011d3 | 444 | * calculate subkeys |
sahilmgandhi | 18:6a4db94011d3 | 445 | */ |
sahilmgandhi | 18:6a4db94011d3 | 446 | for( i = 0; i < 16; i++ ) |
sahilmgandhi | 18:6a4db94011d3 | 447 | { |
sahilmgandhi | 18:6a4db94011d3 | 448 | if( i < 2 || i == 8 || i == 15 ) |
sahilmgandhi | 18:6a4db94011d3 | 449 | { |
sahilmgandhi | 18:6a4db94011d3 | 450 | X = ((X << 1) | (X >> 27)) & 0x0FFFFFFF; |
sahilmgandhi | 18:6a4db94011d3 | 451 | Y = ((Y << 1) | (Y >> 27)) & 0x0FFFFFFF; |
sahilmgandhi | 18:6a4db94011d3 | 452 | } |
sahilmgandhi | 18:6a4db94011d3 | 453 | else |
sahilmgandhi | 18:6a4db94011d3 | 454 | { |
sahilmgandhi | 18:6a4db94011d3 | 455 | X = ((X << 2) | (X >> 26)) & 0x0FFFFFFF; |
sahilmgandhi | 18:6a4db94011d3 | 456 | Y = ((Y << 2) | (Y >> 26)) & 0x0FFFFFFF; |
sahilmgandhi | 18:6a4db94011d3 | 457 | } |
sahilmgandhi | 18:6a4db94011d3 | 458 | |
sahilmgandhi | 18:6a4db94011d3 | 459 | *SK++ = ((X << 4) & 0x24000000) | ((X << 28) & 0x10000000) |
sahilmgandhi | 18:6a4db94011d3 | 460 | | ((X << 14) & 0x08000000) | ((X << 18) & 0x02080000) |
sahilmgandhi | 18:6a4db94011d3 | 461 | | ((X << 6) & 0x01000000) | ((X << 9) & 0x00200000) |
sahilmgandhi | 18:6a4db94011d3 | 462 | | ((X >> 1) & 0x00100000) | ((X << 10) & 0x00040000) |
sahilmgandhi | 18:6a4db94011d3 | 463 | | ((X << 2) & 0x00020000) | ((X >> 10) & 0x00010000) |
sahilmgandhi | 18:6a4db94011d3 | 464 | | ((Y >> 13) & 0x00002000) | ((Y >> 4) & 0x00001000) |
sahilmgandhi | 18:6a4db94011d3 | 465 | | ((Y << 6) & 0x00000800) | ((Y >> 1) & 0x00000400) |
sahilmgandhi | 18:6a4db94011d3 | 466 | | ((Y >> 14) & 0x00000200) | ((Y ) & 0x00000100) |
sahilmgandhi | 18:6a4db94011d3 | 467 | | ((Y >> 5) & 0x00000020) | ((Y >> 10) & 0x00000010) |
sahilmgandhi | 18:6a4db94011d3 | 468 | | ((Y >> 3) & 0x00000008) | ((Y >> 18) & 0x00000004) |
sahilmgandhi | 18:6a4db94011d3 | 469 | | ((Y >> 26) & 0x00000002) | ((Y >> 24) & 0x00000001); |
sahilmgandhi | 18:6a4db94011d3 | 470 | |
sahilmgandhi | 18:6a4db94011d3 | 471 | *SK++ = ((X << 15) & 0x20000000) | ((X << 17) & 0x10000000) |
sahilmgandhi | 18:6a4db94011d3 | 472 | | ((X << 10) & 0x08000000) | ((X << 22) & 0x04000000) |
sahilmgandhi | 18:6a4db94011d3 | 473 | | ((X >> 2) & 0x02000000) | ((X << 1) & 0x01000000) |
sahilmgandhi | 18:6a4db94011d3 | 474 | | ((X << 16) & 0x00200000) | ((X << 11) & 0x00100000) |
sahilmgandhi | 18:6a4db94011d3 | 475 | | ((X << 3) & 0x00080000) | ((X >> 6) & 0x00040000) |
sahilmgandhi | 18:6a4db94011d3 | 476 | | ((X << 15) & 0x00020000) | ((X >> 4) & 0x00010000) |
sahilmgandhi | 18:6a4db94011d3 | 477 | | ((Y >> 2) & 0x00002000) | ((Y << 8) & 0x00001000) |
sahilmgandhi | 18:6a4db94011d3 | 478 | | ((Y >> 14) & 0x00000808) | ((Y >> 9) & 0x00000400) |
sahilmgandhi | 18:6a4db94011d3 | 479 | | ((Y ) & 0x00000200) | ((Y << 7) & 0x00000100) |
sahilmgandhi | 18:6a4db94011d3 | 480 | | ((Y >> 7) & 0x00000020) | ((Y >> 3) & 0x00000011) |
sahilmgandhi | 18:6a4db94011d3 | 481 | | ((Y << 2) & 0x00000004) | ((Y >> 21) & 0x00000002); |
sahilmgandhi | 18:6a4db94011d3 | 482 | } |
sahilmgandhi | 18:6a4db94011d3 | 483 | } |
sahilmgandhi | 18:6a4db94011d3 | 484 | |
sahilmgandhi | 18:6a4db94011d3 | 485 | /* |
sahilmgandhi | 18:6a4db94011d3 | 486 | * DES key schedule (56-bit, encryption) |
sahilmgandhi | 18:6a4db94011d3 | 487 | */ |
sahilmgandhi | 18:6a4db94011d3 | 488 | int mbedtls_des_sw_setkey_enc( mbedtls_des_sw_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] ) |
sahilmgandhi | 18:6a4db94011d3 | 489 | { |
sahilmgandhi | 18:6a4db94011d3 | 490 | mbedtls_des_setkey( ctx->sk, key ); |
sahilmgandhi | 18:6a4db94011d3 | 491 | |
sahilmgandhi | 18:6a4db94011d3 | 492 | return( 0 ); |
sahilmgandhi | 18:6a4db94011d3 | 493 | } |
sahilmgandhi | 18:6a4db94011d3 | 494 | |
sahilmgandhi | 18:6a4db94011d3 | 495 | /* |
sahilmgandhi | 18:6a4db94011d3 | 496 | * DES key schedule (56-bit, decryption) |
sahilmgandhi | 18:6a4db94011d3 | 497 | */ |
sahilmgandhi | 18:6a4db94011d3 | 498 | int mbedtls_des_sw_setkey_dec( mbedtls_des_sw_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] ) |
sahilmgandhi | 18:6a4db94011d3 | 499 | { |
sahilmgandhi | 18:6a4db94011d3 | 500 | int i; |
sahilmgandhi | 18:6a4db94011d3 | 501 | |
sahilmgandhi | 18:6a4db94011d3 | 502 | mbedtls_des_setkey( ctx->sk, key ); |
sahilmgandhi | 18:6a4db94011d3 | 503 | |
sahilmgandhi | 18:6a4db94011d3 | 504 | for( i = 0; i < 16; i += 2 ) |
sahilmgandhi | 18:6a4db94011d3 | 505 | { |
sahilmgandhi | 18:6a4db94011d3 | 506 | SWAP( ctx->sk[i ], ctx->sk[30 - i] ); |
sahilmgandhi | 18:6a4db94011d3 | 507 | SWAP( ctx->sk[i + 1], ctx->sk[31 - i] ); |
sahilmgandhi | 18:6a4db94011d3 | 508 | } |
sahilmgandhi | 18:6a4db94011d3 | 509 | |
sahilmgandhi | 18:6a4db94011d3 | 510 | return( 0 ); |
sahilmgandhi | 18:6a4db94011d3 | 511 | } |
sahilmgandhi | 18:6a4db94011d3 | 512 | |
sahilmgandhi | 18:6a4db94011d3 | 513 | static void des3_set2key( uint32_t esk[96], |
sahilmgandhi | 18:6a4db94011d3 | 514 | uint32_t dsk[96], |
sahilmgandhi | 18:6a4db94011d3 | 515 | const unsigned char key[MBEDTLS_DES_KEY_SIZE*2] ) |
sahilmgandhi | 18:6a4db94011d3 | 516 | { |
sahilmgandhi | 18:6a4db94011d3 | 517 | int i; |
sahilmgandhi | 18:6a4db94011d3 | 518 | |
sahilmgandhi | 18:6a4db94011d3 | 519 | mbedtls_des_setkey( esk, key ); |
sahilmgandhi | 18:6a4db94011d3 | 520 | mbedtls_des_setkey( dsk + 32, key + 8 ); |
sahilmgandhi | 18:6a4db94011d3 | 521 | |
sahilmgandhi | 18:6a4db94011d3 | 522 | for( i = 0; i < 32; i += 2 ) |
sahilmgandhi | 18:6a4db94011d3 | 523 | { |
sahilmgandhi | 18:6a4db94011d3 | 524 | dsk[i ] = esk[30 - i]; |
sahilmgandhi | 18:6a4db94011d3 | 525 | dsk[i + 1] = esk[31 - i]; |
sahilmgandhi | 18:6a4db94011d3 | 526 | |
sahilmgandhi | 18:6a4db94011d3 | 527 | esk[i + 32] = dsk[62 - i]; |
sahilmgandhi | 18:6a4db94011d3 | 528 | esk[i + 33] = dsk[63 - i]; |
sahilmgandhi | 18:6a4db94011d3 | 529 | |
sahilmgandhi | 18:6a4db94011d3 | 530 | esk[i + 64] = esk[i ]; |
sahilmgandhi | 18:6a4db94011d3 | 531 | esk[i + 65] = esk[i + 1]; |
sahilmgandhi | 18:6a4db94011d3 | 532 | |
sahilmgandhi | 18:6a4db94011d3 | 533 | dsk[i + 64] = dsk[i ]; |
sahilmgandhi | 18:6a4db94011d3 | 534 | dsk[i + 65] = dsk[i + 1]; |
sahilmgandhi | 18:6a4db94011d3 | 535 | } |
sahilmgandhi | 18:6a4db94011d3 | 536 | } |
sahilmgandhi | 18:6a4db94011d3 | 537 | |
sahilmgandhi | 18:6a4db94011d3 | 538 | /* |
sahilmgandhi | 18:6a4db94011d3 | 539 | * Triple-DES key schedule (112-bit, encryption) |
sahilmgandhi | 18:6a4db94011d3 | 540 | */ |
sahilmgandhi | 18:6a4db94011d3 | 541 | int mbedtls_des3_sw_set2key_enc( mbedtls_des3_sw_context *ctx, |
sahilmgandhi | 18:6a4db94011d3 | 542 | const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] ) |
sahilmgandhi | 18:6a4db94011d3 | 543 | { |
sahilmgandhi | 18:6a4db94011d3 | 544 | uint32_t sk[96]; |
sahilmgandhi | 18:6a4db94011d3 | 545 | |
sahilmgandhi | 18:6a4db94011d3 | 546 | des3_set2key( ctx->sk, sk, key ); |
sahilmgandhi | 18:6a4db94011d3 | 547 | mbedtls_zeroize( sk, sizeof( sk ) ); |
sahilmgandhi | 18:6a4db94011d3 | 548 | |
sahilmgandhi | 18:6a4db94011d3 | 549 | return( 0 ); |
sahilmgandhi | 18:6a4db94011d3 | 550 | } |
sahilmgandhi | 18:6a4db94011d3 | 551 | |
sahilmgandhi | 18:6a4db94011d3 | 552 | /* |
sahilmgandhi | 18:6a4db94011d3 | 553 | * Triple-DES key schedule (112-bit, decryption) |
sahilmgandhi | 18:6a4db94011d3 | 554 | */ |
sahilmgandhi | 18:6a4db94011d3 | 555 | int mbedtls_des3_sw_set2key_dec( mbedtls_des3_sw_context *ctx, |
sahilmgandhi | 18:6a4db94011d3 | 556 | const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] ) |
sahilmgandhi | 18:6a4db94011d3 | 557 | { |
sahilmgandhi | 18:6a4db94011d3 | 558 | uint32_t sk[96]; |
sahilmgandhi | 18:6a4db94011d3 | 559 | |
sahilmgandhi | 18:6a4db94011d3 | 560 | des3_set2key( sk, ctx->sk, key ); |
sahilmgandhi | 18:6a4db94011d3 | 561 | mbedtls_zeroize( sk, sizeof( sk ) ); |
sahilmgandhi | 18:6a4db94011d3 | 562 | |
sahilmgandhi | 18:6a4db94011d3 | 563 | return( 0 ); |
sahilmgandhi | 18:6a4db94011d3 | 564 | } |
sahilmgandhi | 18:6a4db94011d3 | 565 | |
sahilmgandhi | 18:6a4db94011d3 | 566 | static void des3_set3key( uint32_t esk[96], |
sahilmgandhi | 18:6a4db94011d3 | 567 | uint32_t dsk[96], |
sahilmgandhi | 18:6a4db94011d3 | 568 | const unsigned char key[24] ) |
sahilmgandhi | 18:6a4db94011d3 | 569 | { |
sahilmgandhi | 18:6a4db94011d3 | 570 | int i; |
sahilmgandhi | 18:6a4db94011d3 | 571 | |
sahilmgandhi | 18:6a4db94011d3 | 572 | mbedtls_des_setkey( esk, key ); |
sahilmgandhi | 18:6a4db94011d3 | 573 | mbedtls_des_setkey( dsk + 32, key + 8 ); |
sahilmgandhi | 18:6a4db94011d3 | 574 | mbedtls_des_setkey( esk + 64, key + 16 ); |
sahilmgandhi | 18:6a4db94011d3 | 575 | |
sahilmgandhi | 18:6a4db94011d3 | 576 | for( i = 0; i < 32; i += 2 ) |
sahilmgandhi | 18:6a4db94011d3 | 577 | { |
sahilmgandhi | 18:6a4db94011d3 | 578 | dsk[i ] = esk[94 - i]; |
sahilmgandhi | 18:6a4db94011d3 | 579 | dsk[i + 1] = esk[95 - i]; |
sahilmgandhi | 18:6a4db94011d3 | 580 | |
sahilmgandhi | 18:6a4db94011d3 | 581 | esk[i + 32] = dsk[62 - i]; |
sahilmgandhi | 18:6a4db94011d3 | 582 | esk[i + 33] = dsk[63 - i]; |
sahilmgandhi | 18:6a4db94011d3 | 583 | |
sahilmgandhi | 18:6a4db94011d3 | 584 | dsk[i + 64] = esk[30 - i]; |
sahilmgandhi | 18:6a4db94011d3 | 585 | dsk[i + 65] = esk[31 - i]; |
sahilmgandhi | 18:6a4db94011d3 | 586 | } |
sahilmgandhi | 18:6a4db94011d3 | 587 | } |
sahilmgandhi | 18:6a4db94011d3 | 588 | |
sahilmgandhi | 18:6a4db94011d3 | 589 | /* |
sahilmgandhi | 18:6a4db94011d3 | 590 | * Triple-DES key schedule (168-bit, encryption) |
sahilmgandhi | 18:6a4db94011d3 | 591 | */ |
sahilmgandhi | 18:6a4db94011d3 | 592 | int mbedtls_des3_sw_set3key_enc( mbedtls_des3_sw_context *ctx, |
sahilmgandhi | 18:6a4db94011d3 | 593 | const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] ) |
sahilmgandhi | 18:6a4db94011d3 | 594 | { |
sahilmgandhi | 18:6a4db94011d3 | 595 | uint32_t sk[96]; |
sahilmgandhi | 18:6a4db94011d3 | 596 | |
sahilmgandhi | 18:6a4db94011d3 | 597 | des3_set3key( ctx->sk, sk, key ); |
sahilmgandhi | 18:6a4db94011d3 | 598 | mbedtls_zeroize( sk, sizeof( sk ) ); |
sahilmgandhi | 18:6a4db94011d3 | 599 | |
sahilmgandhi | 18:6a4db94011d3 | 600 | return( 0 ); |
sahilmgandhi | 18:6a4db94011d3 | 601 | } |
sahilmgandhi | 18:6a4db94011d3 | 602 | |
sahilmgandhi | 18:6a4db94011d3 | 603 | /* |
sahilmgandhi | 18:6a4db94011d3 | 604 | * Triple-DES key schedule (168-bit, decryption) |
sahilmgandhi | 18:6a4db94011d3 | 605 | */ |
sahilmgandhi | 18:6a4db94011d3 | 606 | int mbedtls_des3_sw_set3key_dec( mbedtls_des3_sw_context *ctx, |
sahilmgandhi | 18:6a4db94011d3 | 607 | const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] ) |
sahilmgandhi | 18:6a4db94011d3 | 608 | { |
sahilmgandhi | 18:6a4db94011d3 | 609 | uint32_t sk[96]; |
sahilmgandhi | 18:6a4db94011d3 | 610 | |
sahilmgandhi | 18:6a4db94011d3 | 611 | des3_set3key( sk, ctx->sk, key ); |
sahilmgandhi | 18:6a4db94011d3 | 612 | mbedtls_zeroize( sk, sizeof( sk ) ); |
sahilmgandhi | 18:6a4db94011d3 | 613 | |
sahilmgandhi | 18:6a4db94011d3 | 614 | return( 0 ); |
sahilmgandhi | 18:6a4db94011d3 | 615 | } |
sahilmgandhi | 18:6a4db94011d3 | 616 | |
sahilmgandhi | 18:6a4db94011d3 | 617 | /* |
sahilmgandhi | 18:6a4db94011d3 | 618 | * DES-ECB block encryption/decryption |
sahilmgandhi | 18:6a4db94011d3 | 619 | */ |
sahilmgandhi | 18:6a4db94011d3 | 620 | int mbedtls_des_sw_crypt_ecb( mbedtls_des_sw_context *ctx, |
sahilmgandhi | 18:6a4db94011d3 | 621 | const unsigned char input[8], |
sahilmgandhi | 18:6a4db94011d3 | 622 | unsigned char output[8] ) |
sahilmgandhi | 18:6a4db94011d3 | 623 | { |
sahilmgandhi | 18:6a4db94011d3 | 624 | int i; |
sahilmgandhi | 18:6a4db94011d3 | 625 | uint32_t X, Y, T, *SK; |
sahilmgandhi | 18:6a4db94011d3 | 626 | |
sahilmgandhi | 18:6a4db94011d3 | 627 | SK = ctx->sk; |
sahilmgandhi | 18:6a4db94011d3 | 628 | |
sahilmgandhi | 18:6a4db94011d3 | 629 | GET_UINT32_BE( X, input, 0 ); |
sahilmgandhi | 18:6a4db94011d3 | 630 | GET_UINT32_BE( Y, input, 4 ); |
sahilmgandhi | 18:6a4db94011d3 | 631 | |
sahilmgandhi | 18:6a4db94011d3 | 632 | DES_IP( X, Y ); |
sahilmgandhi | 18:6a4db94011d3 | 633 | |
sahilmgandhi | 18:6a4db94011d3 | 634 | for( i = 0; i < 8; i++ ) |
sahilmgandhi | 18:6a4db94011d3 | 635 | { |
sahilmgandhi | 18:6a4db94011d3 | 636 | DES_ROUND( Y, X ); |
sahilmgandhi | 18:6a4db94011d3 | 637 | DES_ROUND( X, Y ); |
sahilmgandhi | 18:6a4db94011d3 | 638 | } |
sahilmgandhi | 18:6a4db94011d3 | 639 | |
sahilmgandhi | 18:6a4db94011d3 | 640 | DES_FP( Y, X ); |
sahilmgandhi | 18:6a4db94011d3 | 641 | |
sahilmgandhi | 18:6a4db94011d3 | 642 | PUT_UINT32_BE( Y, output, 0 ); |
sahilmgandhi | 18:6a4db94011d3 | 643 | PUT_UINT32_BE( X, output, 4 ); |
sahilmgandhi | 18:6a4db94011d3 | 644 | |
sahilmgandhi | 18:6a4db94011d3 | 645 | return( 0 ); |
sahilmgandhi | 18:6a4db94011d3 | 646 | } |
sahilmgandhi | 18:6a4db94011d3 | 647 | |
sahilmgandhi | 18:6a4db94011d3 | 648 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
sahilmgandhi | 18:6a4db94011d3 | 649 | /* |
sahilmgandhi | 18:6a4db94011d3 | 650 | * DES-CBC buffer encryption/decryption |
sahilmgandhi | 18:6a4db94011d3 | 651 | */ |
sahilmgandhi | 18:6a4db94011d3 | 652 | int mbedtls_des_sw_crypt_cbc( mbedtls_des_sw_context *ctx, |
sahilmgandhi | 18:6a4db94011d3 | 653 | int mode, |
sahilmgandhi | 18:6a4db94011d3 | 654 | size_t length, |
sahilmgandhi | 18:6a4db94011d3 | 655 | unsigned char iv[8], |
sahilmgandhi | 18:6a4db94011d3 | 656 | const unsigned char *input, |
sahilmgandhi | 18:6a4db94011d3 | 657 | unsigned char *output ) |
sahilmgandhi | 18:6a4db94011d3 | 658 | { |
sahilmgandhi | 18:6a4db94011d3 | 659 | int i; |
sahilmgandhi | 18:6a4db94011d3 | 660 | unsigned char temp[8]; |
sahilmgandhi | 18:6a4db94011d3 | 661 | |
sahilmgandhi | 18:6a4db94011d3 | 662 | if( length % 8 ) |
sahilmgandhi | 18:6a4db94011d3 | 663 | return( MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH ); |
sahilmgandhi | 18:6a4db94011d3 | 664 | |
sahilmgandhi | 18:6a4db94011d3 | 665 | if( mode == MBEDTLS_DES_ENCRYPT ) |
sahilmgandhi | 18:6a4db94011d3 | 666 | { |
sahilmgandhi | 18:6a4db94011d3 | 667 | while( length > 0 ) |
sahilmgandhi | 18:6a4db94011d3 | 668 | { |
sahilmgandhi | 18:6a4db94011d3 | 669 | for( i = 0; i < 8; i++ ) |
sahilmgandhi | 18:6a4db94011d3 | 670 | output[i] = (unsigned char)( input[i] ^ iv[i] ); |
sahilmgandhi | 18:6a4db94011d3 | 671 | |
sahilmgandhi | 18:6a4db94011d3 | 672 | mbedtls_des_sw_crypt_ecb( ctx, output, output ); |
sahilmgandhi | 18:6a4db94011d3 | 673 | memcpy( iv, output, 8 ); |
sahilmgandhi | 18:6a4db94011d3 | 674 | |
sahilmgandhi | 18:6a4db94011d3 | 675 | input += 8; |
sahilmgandhi | 18:6a4db94011d3 | 676 | output += 8; |
sahilmgandhi | 18:6a4db94011d3 | 677 | length -= 8; |
sahilmgandhi | 18:6a4db94011d3 | 678 | } |
sahilmgandhi | 18:6a4db94011d3 | 679 | } |
sahilmgandhi | 18:6a4db94011d3 | 680 | else /* MBEDTLS_DES_DECRYPT */ |
sahilmgandhi | 18:6a4db94011d3 | 681 | { |
sahilmgandhi | 18:6a4db94011d3 | 682 | while( length > 0 ) |
sahilmgandhi | 18:6a4db94011d3 | 683 | { |
sahilmgandhi | 18:6a4db94011d3 | 684 | memcpy( temp, input, 8 ); |
sahilmgandhi | 18:6a4db94011d3 | 685 | mbedtls_des_sw_crypt_ecb( ctx, input, output ); |
sahilmgandhi | 18:6a4db94011d3 | 686 | |
sahilmgandhi | 18:6a4db94011d3 | 687 | for( i = 0; i < 8; i++ ) |
sahilmgandhi | 18:6a4db94011d3 | 688 | output[i] = (unsigned char)( output[i] ^ iv[i] ); |
sahilmgandhi | 18:6a4db94011d3 | 689 | |
sahilmgandhi | 18:6a4db94011d3 | 690 | memcpy( iv, temp, 8 ); |
sahilmgandhi | 18:6a4db94011d3 | 691 | |
sahilmgandhi | 18:6a4db94011d3 | 692 | input += 8; |
sahilmgandhi | 18:6a4db94011d3 | 693 | output += 8; |
sahilmgandhi | 18:6a4db94011d3 | 694 | length -= 8; |
sahilmgandhi | 18:6a4db94011d3 | 695 | } |
sahilmgandhi | 18:6a4db94011d3 | 696 | } |
sahilmgandhi | 18:6a4db94011d3 | 697 | |
sahilmgandhi | 18:6a4db94011d3 | 698 | return( 0 ); |
sahilmgandhi | 18:6a4db94011d3 | 699 | } |
sahilmgandhi | 18:6a4db94011d3 | 700 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
sahilmgandhi | 18:6a4db94011d3 | 701 | |
sahilmgandhi | 18:6a4db94011d3 | 702 | /* |
sahilmgandhi | 18:6a4db94011d3 | 703 | * 3DES-ECB block encryption/decryption |
sahilmgandhi | 18:6a4db94011d3 | 704 | */ |
sahilmgandhi | 18:6a4db94011d3 | 705 | int mbedtls_des3_sw_crypt_ecb( mbedtls_des3_sw_context *ctx, |
sahilmgandhi | 18:6a4db94011d3 | 706 | const unsigned char input[8], |
sahilmgandhi | 18:6a4db94011d3 | 707 | unsigned char output[8] ) |
sahilmgandhi | 18:6a4db94011d3 | 708 | { |
sahilmgandhi | 18:6a4db94011d3 | 709 | int i; |
sahilmgandhi | 18:6a4db94011d3 | 710 | uint32_t X, Y, T, *SK; |
sahilmgandhi | 18:6a4db94011d3 | 711 | |
sahilmgandhi | 18:6a4db94011d3 | 712 | SK = ctx->sk; |
sahilmgandhi | 18:6a4db94011d3 | 713 | |
sahilmgandhi | 18:6a4db94011d3 | 714 | GET_UINT32_BE( X, input, 0 ); |
sahilmgandhi | 18:6a4db94011d3 | 715 | GET_UINT32_BE( Y, input, 4 ); |
sahilmgandhi | 18:6a4db94011d3 | 716 | |
sahilmgandhi | 18:6a4db94011d3 | 717 | DES_IP( X, Y ); |
sahilmgandhi | 18:6a4db94011d3 | 718 | |
sahilmgandhi | 18:6a4db94011d3 | 719 | for( i = 0; i < 8; i++ ) |
sahilmgandhi | 18:6a4db94011d3 | 720 | { |
sahilmgandhi | 18:6a4db94011d3 | 721 | DES_ROUND( Y, X ); |
sahilmgandhi | 18:6a4db94011d3 | 722 | DES_ROUND( X, Y ); |
sahilmgandhi | 18:6a4db94011d3 | 723 | } |
sahilmgandhi | 18:6a4db94011d3 | 724 | |
sahilmgandhi | 18:6a4db94011d3 | 725 | for( i = 0; i < 8; i++ ) |
sahilmgandhi | 18:6a4db94011d3 | 726 | { |
sahilmgandhi | 18:6a4db94011d3 | 727 | DES_ROUND( X, Y ); |
sahilmgandhi | 18:6a4db94011d3 | 728 | DES_ROUND( Y, X ); |
sahilmgandhi | 18:6a4db94011d3 | 729 | } |
sahilmgandhi | 18:6a4db94011d3 | 730 | |
sahilmgandhi | 18:6a4db94011d3 | 731 | for( i = 0; i < 8; i++ ) |
sahilmgandhi | 18:6a4db94011d3 | 732 | { |
sahilmgandhi | 18:6a4db94011d3 | 733 | DES_ROUND( Y, X ); |
sahilmgandhi | 18:6a4db94011d3 | 734 | DES_ROUND( X, Y ); |
sahilmgandhi | 18:6a4db94011d3 | 735 | } |
sahilmgandhi | 18:6a4db94011d3 | 736 | |
sahilmgandhi | 18:6a4db94011d3 | 737 | DES_FP( Y, X ); |
sahilmgandhi | 18:6a4db94011d3 | 738 | |
sahilmgandhi | 18:6a4db94011d3 | 739 | PUT_UINT32_BE( Y, output, 0 ); |
sahilmgandhi | 18:6a4db94011d3 | 740 | PUT_UINT32_BE( X, output, 4 ); |
sahilmgandhi | 18:6a4db94011d3 | 741 | |
sahilmgandhi | 18:6a4db94011d3 | 742 | return( 0 ); |
sahilmgandhi | 18:6a4db94011d3 | 743 | } |
sahilmgandhi | 18:6a4db94011d3 | 744 | |
sahilmgandhi | 18:6a4db94011d3 | 745 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
sahilmgandhi | 18:6a4db94011d3 | 746 | /* |
sahilmgandhi | 18:6a4db94011d3 | 747 | * 3DES-CBC buffer encryption/decryption |
sahilmgandhi | 18:6a4db94011d3 | 748 | */ |
sahilmgandhi | 18:6a4db94011d3 | 749 | int mbedtls_des3_sw_crypt_cbc( mbedtls_des3_sw_context *ctx, |
sahilmgandhi | 18:6a4db94011d3 | 750 | int mode, |
sahilmgandhi | 18:6a4db94011d3 | 751 | size_t length, |
sahilmgandhi | 18:6a4db94011d3 | 752 | unsigned char iv[8], |
sahilmgandhi | 18:6a4db94011d3 | 753 | const unsigned char *input, |
sahilmgandhi | 18:6a4db94011d3 | 754 | unsigned char *output ) |
sahilmgandhi | 18:6a4db94011d3 | 755 | { |
sahilmgandhi | 18:6a4db94011d3 | 756 | int i; |
sahilmgandhi | 18:6a4db94011d3 | 757 | unsigned char temp[8]; |
sahilmgandhi | 18:6a4db94011d3 | 758 | |
sahilmgandhi | 18:6a4db94011d3 | 759 | if( length % 8 ) |
sahilmgandhi | 18:6a4db94011d3 | 760 | return( MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH ); |
sahilmgandhi | 18:6a4db94011d3 | 761 | |
sahilmgandhi | 18:6a4db94011d3 | 762 | if( mode == MBEDTLS_DES_ENCRYPT ) |
sahilmgandhi | 18:6a4db94011d3 | 763 | { |
sahilmgandhi | 18:6a4db94011d3 | 764 | while( length > 0 ) |
sahilmgandhi | 18:6a4db94011d3 | 765 | { |
sahilmgandhi | 18:6a4db94011d3 | 766 | for( i = 0; i < 8; i++ ) |
sahilmgandhi | 18:6a4db94011d3 | 767 | output[i] = (unsigned char)( input[i] ^ iv[i] ); |
sahilmgandhi | 18:6a4db94011d3 | 768 | |
sahilmgandhi | 18:6a4db94011d3 | 769 | mbedtls_des3_sw_crypt_ecb( ctx, output, output ); |
sahilmgandhi | 18:6a4db94011d3 | 770 | memcpy( iv, output, 8 ); |
sahilmgandhi | 18:6a4db94011d3 | 771 | |
sahilmgandhi | 18:6a4db94011d3 | 772 | input += 8; |
sahilmgandhi | 18:6a4db94011d3 | 773 | output += 8; |
sahilmgandhi | 18:6a4db94011d3 | 774 | length -= 8; |
sahilmgandhi | 18:6a4db94011d3 | 775 | } |
sahilmgandhi | 18:6a4db94011d3 | 776 | } |
sahilmgandhi | 18:6a4db94011d3 | 777 | else /* MBEDTLS_DES_DECRYPT */ |
sahilmgandhi | 18:6a4db94011d3 | 778 | { |
sahilmgandhi | 18:6a4db94011d3 | 779 | while( length > 0 ) |
sahilmgandhi | 18:6a4db94011d3 | 780 | { |
sahilmgandhi | 18:6a4db94011d3 | 781 | memcpy( temp, input, 8 ); |
sahilmgandhi | 18:6a4db94011d3 | 782 | mbedtls_des3_sw_crypt_ecb( ctx, input, output ); |
sahilmgandhi | 18:6a4db94011d3 | 783 | |
sahilmgandhi | 18:6a4db94011d3 | 784 | for( i = 0; i < 8; i++ ) |
sahilmgandhi | 18:6a4db94011d3 | 785 | output[i] = (unsigned char)( output[i] ^ iv[i] ); |
sahilmgandhi | 18:6a4db94011d3 | 786 | |
sahilmgandhi | 18:6a4db94011d3 | 787 | memcpy( iv, temp, 8 ); |
sahilmgandhi | 18:6a4db94011d3 | 788 | |
sahilmgandhi | 18:6a4db94011d3 | 789 | input += 8; |
sahilmgandhi | 18:6a4db94011d3 | 790 | output += 8; |
sahilmgandhi | 18:6a4db94011d3 | 791 | length -= 8; |
sahilmgandhi | 18:6a4db94011d3 | 792 | } |
sahilmgandhi | 18:6a4db94011d3 | 793 | } |
sahilmgandhi | 18:6a4db94011d3 | 794 | |
sahilmgandhi | 18:6a4db94011d3 | 795 | return( 0 ); |
sahilmgandhi | 18:6a4db94011d3 | 796 | } |
sahilmgandhi | 18:6a4db94011d3 | 797 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
sahilmgandhi | 18:6a4db94011d3 | 798 | |
sahilmgandhi | 18:6a4db94011d3 | 799 | #endif /* MBEDTLS_DES_ALT */ |
sahilmgandhi | 18:6a4db94011d3 | 800 | #endif /* MBEDTLS_DES_C */ |
sahilmgandhi | 18:6a4db94011d3 | 801 | |
sahilmgandhi | 18:6a4db94011d3 | 802 | #endif /* MBED_CONF_RTOS_PRESENT */ |