This is a port of cyaSSL 2.7.0.
Dependents: CyaSSL_DTLS_Cellular CyaSSL_DTLS_Ethernet
ctaocrypt/src/des3.c@0:714293de3836, 2013-09-05 (annotated)
- Committer:
- ashleymills
- Date:
- Thu Sep 05 10:33:04 2013 +0000
- Revision:
- 0:714293de3836
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ashleymills | 0:714293de3836 | 1 | /* des3.c |
ashleymills | 0:714293de3836 | 2 | * |
ashleymills | 0:714293de3836 | 3 | * Copyright (C) 2006-2013 wolfSSL Inc. |
ashleymills | 0:714293de3836 | 4 | * |
ashleymills | 0:714293de3836 | 5 | * This file is part of CyaSSL. |
ashleymills | 0:714293de3836 | 6 | * |
ashleymills | 0:714293de3836 | 7 | * CyaSSL is free software; you can redistribute it and/or modify |
ashleymills | 0:714293de3836 | 8 | * it under the terms of the GNU General Public License as published by |
ashleymills | 0:714293de3836 | 9 | * the Free Software Foundation; either version 2 of the License, or |
ashleymills | 0:714293de3836 | 10 | * (at your option) any later version. |
ashleymills | 0:714293de3836 | 11 | * |
ashleymills | 0:714293de3836 | 12 | * CyaSSL is distributed in the hope that it will be useful, |
ashleymills | 0:714293de3836 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
ashleymills | 0:714293de3836 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ashleymills | 0:714293de3836 | 15 | * GNU General Public License for more details. |
ashleymills | 0:714293de3836 | 16 | * |
ashleymills | 0:714293de3836 | 17 | * You should have received a copy of the GNU General Public License |
ashleymills | 0:714293de3836 | 18 | * along with this program; if not, write to the Free Software |
ashleymills | 0:714293de3836 | 19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
ashleymills | 0:714293de3836 | 20 | */ |
ashleymills | 0:714293de3836 | 21 | |
ashleymills | 0:714293de3836 | 22 | #ifdef HAVE_CONFIG_H |
ashleymills | 0:714293de3836 | 23 | #include <config.h> |
ashleymills | 0:714293de3836 | 24 | #endif |
ashleymills | 0:714293de3836 | 25 | |
ashleymills | 0:714293de3836 | 26 | #include <cyassl/ctaocrypt/settings.h> |
ashleymills | 0:714293de3836 | 27 | |
ashleymills | 0:714293de3836 | 28 | #ifndef NO_DES3 |
ashleymills | 0:714293de3836 | 29 | |
ashleymills | 0:714293de3836 | 30 | #include <cyassl/ctaocrypt/des3.h> |
ashleymills | 0:714293de3836 | 31 | |
ashleymills | 0:714293de3836 | 32 | #ifdef NO_INLINE |
ashleymills | 0:714293de3836 | 33 | #include <cyassl/ctaocrypt/misc.h> |
ashleymills | 0:714293de3836 | 34 | #else |
ashleymills | 0:714293de3836 | 35 | #include <ctaocrypt/src/misc.c> |
ashleymills | 0:714293de3836 | 36 | #endif |
ashleymills | 0:714293de3836 | 37 | |
ashleymills | 0:714293de3836 | 38 | |
ashleymills | 0:714293de3836 | 39 | #ifdef HAVE_CAVIUM |
ashleymills | 0:714293de3836 | 40 | static void Des3_CaviumSetKey(Des3* des3, const byte* key, const byte* iv); |
ashleymills | 0:714293de3836 | 41 | static void Des3_CaviumCbcEncrypt(Des3* des3, byte* out, const byte* in, |
ashleymills | 0:714293de3836 | 42 | word32 length); |
ashleymills | 0:714293de3836 | 43 | static void Des3_CaviumCbcDecrypt(Des3* des3, byte* out, const byte* in, |
ashleymills | 0:714293de3836 | 44 | word32 length); |
ashleymills | 0:714293de3836 | 45 | #endif |
ashleymills | 0:714293de3836 | 46 | |
ashleymills | 0:714293de3836 | 47 | #ifdef STM32F2_CRYPTO |
ashleymills | 0:714293de3836 | 48 | /* |
ashleymills | 0:714293de3836 | 49 | * STM32F2 hardware DES/3DES support through the STM32F2 standard |
ashleymills | 0:714293de3836 | 50 | * peripheral library. Documentation located in STM32F2xx Standard |
ashleymills | 0:714293de3836 | 51 | * Peripheral Library document (See note in README). |
ashleymills | 0:714293de3836 | 52 | */ |
ashleymills | 0:714293de3836 | 53 | #include "stm32f2xx.h" |
ashleymills | 0:714293de3836 | 54 | #include "stm32f2xx_cryp.h" |
ashleymills | 0:714293de3836 | 55 | |
ashleymills | 0:714293de3836 | 56 | void Des_SetKey(Des* des, const byte* key, const byte* iv, int dir) |
ashleymills | 0:714293de3836 | 57 | { |
ashleymills | 0:714293de3836 | 58 | word32 *dkey = des->key; |
ashleymills | 0:714293de3836 | 59 | |
ashleymills | 0:714293de3836 | 60 | XMEMCPY(dkey, key, 8); |
ashleymills | 0:714293de3836 | 61 | ByteReverseWords(dkey, dkey, 8); |
ashleymills | 0:714293de3836 | 62 | |
ashleymills | 0:714293de3836 | 63 | Des_SetIV(des, iv); |
ashleymills | 0:714293de3836 | 64 | } |
ashleymills | 0:714293de3836 | 65 | |
ashleymills | 0:714293de3836 | 66 | void Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir) |
ashleymills | 0:714293de3836 | 67 | { |
ashleymills | 0:714293de3836 | 68 | word32 *dkey1 = des->key[0]; |
ashleymills | 0:714293de3836 | 69 | word32 *dkey2 = des->key[1]; |
ashleymills | 0:714293de3836 | 70 | word32 *dkey3 = des->key[2]; |
ashleymills | 0:714293de3836 | 71 | |
ashleymills | 0:714293de3836 | 72 | XMEMCPY(dkey1, key, 8); /* set key 1 */ |
ashleymills | 0:714293de3836 | 73 | XMEMCPY(dkey2, key + 8, 8); /* set key 2 */ |
ashleymills | 0:714293de3836 | 74 | XMEMCPY(dkey3, key + 16, 8); /* set key 3 */ |
ashleymills | 0:714293de3836 | 75 | |
ashleymills | 0:714293de3836 | 76 | ByteReverseWords(dkey1, dkey1, 8); |
ashleymills | 0:714293de3836 | 77 | ByteReverseWords(dkey2, dkey2, 8); |
ashleymills | 0:714293de3836 | 78 | ByteReverseWords(dkey3, dkey3, 8); |
ashleymills | 0:714293de3836 | 79 | |
ashleymills | 0:714293de3836 | 80 | Des3_SetIV(des, iv); |
ashleymills | 0:714293de3836 | 81 | } |
ashleymills | 0:714293de3836 | 82 | |
ashleymills | 0:714293de3836 | 83 | void DesCrypt(Des* des, byte* out, const byte* in, word32 sz, |
ashleymills | 0:714293de3836 | 84 | int dir, int mode) |
ashleymills | 0:714293de3836 | 85 | { |
ashleymills | 0:714293de3836 | 86 | word32 *dkey, *iv; |
ashleymills | 0:714293de3836 | 87 | CRYP_InitTypeDef DES_CRYP_InitStructure; |
ashleymills | 0:714293de3836 | 88 | CRYP_KeyInitTypeDef DES_CRYP_KeyInitStructure; |
ashleymills | 0:714293de3836 | 89 | CRYP_IVInitTypeDef DES_CRYP_IVInitStructure; |
ashleymills | 0:714293de3836 | 90 | |
ashleymills | 0:714293de3836 | 91 | dkey = des->key; |
ashleymills | 0:714293de3836 | 92 | iv = des->reg; |
ashleymills | 0:714293de3836 | 93 | |
ashleymills | 0:714293de3836 | 94 | /* crypto structure initialization */ |
ashleymills | 0:714293de3836 | 95 | CRYP_KeyStructInit(&DES_CRYP_KeyInitStructure); |
ashleymills | 0:714293de3836 | 96 | CRYP_StructInit(&DES_CRYP_InitStructure); |
ashleymills | 0:714293de3836 | 97 | CRYP_IVStructInit(&DES_CRYP_IVInitStructure); |
ashleymills | 0:714293de3836 | 98 | |
ashleymills | 0:714293de3836 | 99 | /* reset registers to their default values */ |
ashleymills | 0:714293de3836 | 100 | CRYP_DeInit(); |
ashleymills | 0:714293de3836 | 101 | |
ashleymills | 0:714293de3836 | 102 | /* set direction, mode, and datatype */ |
ashleymills | 0:714293de3836 | 103 | if (dir == DES_ENCRYPTION) { |
ashleymills | 0:714293de3836 | 104 | DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt; |
ashleymills | 0:714293de3836 | 105 | } else { /* DES_DECRYPTION */ |
ashleymills | 0:714293de3836 | 106 | DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt; |
ashleymills | 0:714293de3836 | 107 | } |
ashleymills | 0:714293de3836 | 108 | |
ashleymills | 0:714293de3836 | 109 | if (mode == DES_CBC) { |
ashleymills | 0:714293de3836 | 110 | DES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_DES_CBC; |
ashleymills | 0:714293de3836 | 111 | } else { /* DES_ECB */ |
ashleymills | 0:714293de3836 | 112 | DES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_DES_ECB; |
ashleymills | 0:714293de3836 | 113 | } |
ashleymills | 0:714293de3836 | 114 | |
ashleymills | 0:714293de3836 | 115 | DES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b; |
ashleymills | 0:714293de3836 | 116 | CRYP_Init(&DES_CRYP_InitStructure); |
ashleymills | 0:714293de3836 | 117 | |
ashleymills | 0:714293de3836 | 118 | /* load key into correct registers */ |
ashleymills | 0:714293de3836 | 119 | DES_CRYP_KeyInitStructure.CRYP_Key1Left = dkey[0]; |
ashleymills | 0:714293de3836 | 120 | DES_CRYP_KeyInitStructure.CRYP_Key1Right = dkey[1]; |
ashleymills | 0:714293de3836 | 121 | CRYP_KeyInit(&DES_CRYP_KeyInitStructure); |
ashleymills | 0:714293de3836 | 122 | |
ashleymills | 0:714293de3836 | 123 | /* set iv */ |
ashleymills | 0:714293de3836 | 124 | ByteReverseWords(iv, iv, DES_BLOCK_SIZE); |
ashleymills | 0:714293de3836 | 125 | DES_CRYP_IVInitStructure.CRYP_IV0Left = iv[0]; |
ashleymills | 0:714293de3836 | 126 | DES_CRYP_IVInitStructure.CRYP_IV0Right = iv[1]; |
ashleymills | 0:714293de3836 | 127 | CRYP_IVInit(&DES_CRYP_IVInitStructure); |
ashleymills | 0:714293de3836 | 128 | |
ashleymills | 0:714293de3836 | 129 | /* enable crypto processor */ |
ashleymills | 0:714293de3836 | 130 | CRYP_Cmd(ENABLE); |
ashleymills | 0:714293de3836 | 131 | |
ashleymills | 0:714293de3836 | 132 | while (sz > 0) |
ashleymills | 0:714293de3836 | 133 | { |
ashleymills | 0:714293de3836 | 134 | /* flush IN/OUT FIFOs */ |
ashleymills | 0:714293de3836 | 135 | CRYP_FIFOFlush(); |
ashleymills | 0:714293de3836 | 136 | |
ashleymills | 0:714293de3836 | 137 | /* if input and output same will overwrite input iv */ |
ashleymills | 0:714293de3836 | 138 | XMEMCPY(des->tmp, in + sz - DES_BLOCK_SIZE, DES_BLOCK_SIZE); |
ashleymills | 0:714293de3836 | 139 | |
ashleymills | 0:714293de3836 | 140 | CRYP_DataIn(*(uint32_t*)&in[0]); |
ashleymills | 0:714293de3836 | 141 | CRYP_DataIn(*(uint32_t*)&in[4]); |
ashleymills | 0:714293de3836 | 142 | |
ashleymills | 0:714293de3836 | 143 | /* wait until the complete message has been processed */ |
ashleymills | 0:714293de3836 | 144 | while(CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {} |
ashleymills | 0:714293de3836 | 145 | |
ashleymills | 0:714293de3836 | 146 | *(uint32_t*)&out[0] = CRYP_DataOut(); |
ashleymills | 0:714293de3836 | 147 | *(uint32_t*)&out[4] = CRYP_DataOut(); |
ashleymills | 0:714293de3836 | 148 | |
ashleymills | 0:714293de3836 | 149 | /* store iv for next call */ |
ashleymills | 0:714293de3836 | 150 | XMEMCPY(des->reg, des->tmp, DES_BLOCK_SIZE); |
ashleymills | 0:714293de3836 | 151 | |
ashleymills | 0:714293de3836 | 152 | sz -= DES_BLOCK_SIZE; |
ashleymills | 0:714293de3836 | 153 | in += DES_BLOCK_SIZE; |
ashleymills | 0:714293de3836 | 154 | out += DES_BLOCK_SIZE; |
ashleymills | 0:714293de3836 | 155 | } |
ashleymills | 0:714293de3836 | 156 | |
ashleymills | 0:714293de3836 | 157 | /* disable crypto processor */ |
ashleymills | 0:714293de3836 | 158 | CRYP_Cmd(DISABLE); |
ashleymills | 0:714293de3836 | 159 | } |
ashleymills | 0:714293de3836 | 160 | |
ashleymills | 0:714293de3836 | 161 | void Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz) |
ashleymills | 0:714293de3836 | 162 | { |
ashleymills | 0:714293de3836 | 163 | DesCrypt(des, out, in, sz, DES_ENCRYPTION, DES_CBC); |
ashleymills | 0:714293de3836 | 164 | } |
ashleymills | 0:714293de3836 | 165 | |
ashleymills | 0:714293de3836 | 166 | void Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz) |
ashleymills | 0:714293de3836 | 167 | { |
ashleymills | 0:714293de3836 | 168 | DesCrypt(des, out, in, sz, DES_DECRYPTION, DES_CBC); |
ashleymills | 0:714293de3836 | 169 | } |
ashleymills | 0:714293de3836 | 170 | |
ashleymills | 0:714293de3836 | 171 | void Des_EcbEncrypt(Des* des, byte* out, const byte* in, word32 sz) |
ashleymills | 0:714293de3836 | 172 | { |
ashleymills | 0:714293de3836 | 173 | DesCrypt(des, out, in, sz, DES_ENCRYPTION, DES_ECB); |
ashleymills | 0:714293de3836 | 174 | } |
ashleymills | 0:714293de3836 | 175 | |
ashleymills | 0:714293de3836 | 176 | void Des3Crypt(Des3* des, byte* out, const byte* in, word32 sz, |
ashleymills | 0:714293de3836 | 177 | int dir) |
ashleymills | 0:714293de3836 | 178 | { |
ashleymills | 0:714293de3836 | 179 | word32 *dkey1, *dkey2, *dkey3, *iv; |
ashleymills | 0:714293de3836 | 180 | CRYP_InitTypeDef DES3_CRYP_InitStructure; |
ashleymills | 0:714293de3836 | 181 | CRYP_KeyInitTypeDef DES3_CRYP_KeyInitStructure; |
ashleymills | 0:714293de3836 | 182 | CRYP_IVInitTypeDef DES3_CRYP_IVInitStructure; |
ashleymills | 0:714293de3836 | 183 | |
ashleymills | 0:714293de3836 | 184 | dkey1 = des->key[0]; |
ashleymills | 0:714293de3836 | 185 | dkey2 = des->key[1]; |
ashleymills | 0:714293de3836 | 186 | dkey3 = des->key[2]; |
ashleymills | 0:714293de3836 | 187 | iv = des->reg; |
ashleymills | 0:714293de3836 | 188 | |
ashleymills | 0:714293de3836 | 189 | /* crypto structure initialization */ |
ashleymills | 0:714293de3836 | 190 | CRYP_KeyStructInit(&DES3_CRYP_KeyInitStructure); |
ashleymills | 0:714293de3836 | 191 | CRYP_StructInit(&DES3_CRYP_InitStructure); |
ashleymills | 0:714293de3836 | 192 | CRYP_IVStructInit(&DES3_CRYP_IVInitStructure); |
ashleymills | 0:714293de3836 | 193 | |
ashleymills | 0:714293de3836 | 194 | /* reset registers to their default values */ |
ashleymills | 0:714293de3836 | 195 | CRYP_DeInit(); |
ashleymills | 0:714293de3836 | 196 | |
ashleymills | 0:714293de3836 | 197 | /* set direction, mode, and datatype */ |
ashleymills | 0:714293de3836 | 198 | if (dir == DES_ENCRYPTION) { |
ashleymills | 0:714293de3836 | 199 | DES3_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt; |
ashleymills | 0:714293de3836 | 200 | } else { |
ashleymills | 0:714293de3836 | 201 | DES3_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt; |
ashleymills | 0:714293de3836 | 202 | } |
ashleymills | 0:714293de3836 | 203 | |
ashleymills | 0:714293de3836 | 204 | DES3_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_TDES_CBC; |
ashleymills | 0:714293de3836 | 205 | DES3_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b; |
ashleymills | 0:714293de3836 | 206 | CRYP_Init(&DES3_CRYP_InitStructure); |
ashleymills | 0:714293de3836 | 207 | |
ashleymills | 0:714293de3836 | 208 | /* load key into correct registers */ |
ashleymills | 0:714293de3836 | 209 | DES3_CRYP_KeyInitStructure.CRYP_Key1Left = dkey1[0]; |
ashleymills | 0:714293de3836 | 210 | DES3_CRYP_KeyInitStructure.CRYP_Key1Right = dkey1[1]; |
ashleymills | 0:714293de3836 | 211 | DES3_CRYP_KeyInitStructure.CRYP_Key2Left = dkey2[0]; |
ashleymills | 0:714293de3836 | 212 | DES3_CRYP_KeyInitStructure.CRYP_Key2Right = dkey2[1]; |
ashleymills | 0:714293de3836 | 213 | DES3_CRYP_KeyInitStructure.CRYP_Key3Left = dkey3[0]; |
ashleymills | 0:714293de3836 | 214 | DES3_CRYP_KeyInitStructure.CRYP_Key3Right = dkey3[1]; |
ashleymills | 0:714293de3836 | 215 | CRYP_KeyInit(&DES3_CRYP_KeyInitStructure); |
ashleymills | 0:714293de3836 | 216 | |
ashleymills | 0:714293de3836 | 217 | /* set iv */ |
ashleymills | 0:714293de3836 | 218 | ByteReverseWords(iv, iv, DES_BLOCK_SIZE); |
ashleymills | 0:714293de3836 | 219 | DES3_CRYP_IVInitStructure.CRYP_IV0Left = iv[0]; |
ashleymills | 0:714293de3836 | 220 | DES3_CRYP_IVInitStructure.CRYP_IV0Right = iv[1]; |
ashleymills | 0:714293de3836 | 221 | CRYP_IVInit(&DES3_CRYP_IVInitStructure); |
ashleymills | 0:714293de3836 | 222 | |
ashleymills | 0:714293de3836 | 223 | /* enable crypto processor */ |
ashleymills | 0:714293de3836 | 224 | CRYP_Cmd(ENABLE); |
ashleymills | 0:714293de3836 | 225 | |
ashleymills | 0:714293de3836 | 226 | while (sz > 0) |
ashleymills | 0:714293de3836 | 227 | { |
ashleymills | 0:714293de3836 | 228 | /* flush IN/OUT FIFOs */ |
ashleymills | 0:714293de3836 | 229 | CRYP_FIFOFlush(); |
ashleymills | 0:714293de3836 | 230 | |
ashleymills | 0:714293de3836 | 231 | CRYP_DataIn(*(uint32_t*)&in[0]); |
ashleymills | 0:714293de3836 | 232 | CRYP_DataIn(*(uint32_t*)&in[4]); |
ashleymills | 0:714293de3836 | 233 | |
ashleymills | 0:714293de3836 | 234 | /* wait until the complete message has been processed */ |
ashleymills | 0:714293de3836 | 235 | while(CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {} |
ashleymills | 0:714293de3836 | 236 | |
ashleymills | 0:714293de3836 | 237 | *(uint32_t*)&out[0] = CRYP_DataOut(); |
ashleymills | 0:714293de3836 | 238 | *(uint32_t*)&out[4] = CRYP_DataOut(); |
ashleymills | 0:714293de3836 | 239 | |
ashleymills | 0:714293de3836 | 240 | /* store iv for next call */ |
ashleymills | 0:714293de3836 | 241 | XMEMCPY(des->reg, out + sz - DES_BLOCK_SIZE, DES_BLOCK_SIZE); |
ashleymills | 0:714293de3836 | 242 | |
ashleymills | 0:714293de3836 | 243 | sz -= DES_BLOCK_SIZE; |
ashleymills | 0:714293de3836 | 244 | in += DES_BLOCK_SIZE; |
ashleymills | 0:714293de3836 | 245 | out += DES_BLOCK_SIZE; |
ashleymills | 0:714293de3836 | 246 | } |
ashleymills | 0:714293de3836 | 247 | |
ashleymills | 0:714293de3836 | 248 | /* disable crypto processor */ |
ashleymills | 0:714293de3836 | 249 | CRYP_Cmd(DISABLE); |
ashleymills | 0:714293de3836 | 250 | |
ashleymills | 0:714293de3836 | 251 | } |
ashleymills | 0:714293de3836 | 252 | |
ashleymills | 0:714293de3836 | 253 | void Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz) |
ashleymills | 0:714293de3836 | 254 | { |
ashleymills | 0:714293de3836 | 255 | Des3Crypt(des, out, in, sz, DES_ENCRYPTION); |
ashleymills | 0:714293de3836 | 256 | } |
ashleymills | 0:714293de3836 | 257 | |
ashleymills | 0:714293de3836 | 258 | void Des3_CbcDecrypt(Des3* des, byte* out, const byte* in, word32 sz) |
ashleymills | 0:714293de3836 | 259 | { |
ashleymills | 0:714293de3836 | 260 | Des3Crypt(des, out, in, sz, DES_DECRYPTION); |
ashleymills | 0:714293de3836 | 261 | } |
ashleymills | 0:714293de3836 | 262 | |
ashleymills | 0:714293de3836 | 263 | #else /* CTaoCrypt software implementation */ |
ashleymills | 0:714293de3836 | 264 | |
ashleymills | 0:714293de3836 | 265 | /* permuted choice table (key) */ |
ashleymills | 0:714293de3836 | 266 | static const byte pc1[] = { |
ashleymills | 0:714293de3836 | 267 | 57, 49, 41, 33, 25, 17, 9, |
ashleymills | 0:714293de3836 | 268 | 1, 58, 50, 42, 34, 26, 18, |
ashleymills | 0:714293de3836 | 269 | 10, 2, 59, 51, 43, 35, 27, |
ashleymills | 0:714293de3836 | 270 | 19, 11, 3, 60, 52, 44, 36, |
ashleymills | 0:714293de3836 | 271 | |
ashleymills | 0:714293de3836 | 272 | 63, 55, 47, 39, 31, 23, 15, |
ashleymills | 0:714293de3836 | 273 | 7, 62, 54, 46, 38, 30, 22, |
ashleymills | 0:714293de3836 | 274 | 14, 6, 61, 53, 45, 37, 29, |
ashleymills | 0:714293de3836 | 275 | 21, 13, 5, 28, 20, 12, 4 |
ashleymills | 0:714293de3836 | 276 | }; |
ashleymills | 0:714293de3836 | 277 | |
ashleymills | 0:714293de3836 | 278 | /* number left rotations of pc1 */ |
ashleymills | 0:714293de3836 | 279 | static const byte totrot[] = { |
ashleymills | 0:714293de3836 | 280 | 1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28 |
ashleymills | 0:714293de3836 | 281 | }; |
ashleymills | 0:714293de3836 | 282 | |
ashleymills | 0:714293de3836 | 283 | /* permuted choice key (table) */ |
ashleymills | 0:714293de3836 | 284 | static const byte pc2[] = { |
ashleymills | 0:714293de3836 | 285 | 14, 17, 11, 24, 1, 5, |
ashleymills | 0:714293de3836 | 286 | 3, 28, 15, 6, 21, 10, |
ashleymills | 0:714293de3836 | 287 | 23, 19, 12, 4, 26, 8, |
ashleymills | 0:714293de3836 | 288 | 16, 7, 27, 20, 13, 2, |
ashleymills | 0:714293de3836 | 289 | 41, 52, 31, 37, 47, 55, |
ashleymills | 0:714293de3836 | 290 | 30, 40, 51, 45, 33, 48, |
ashleymills | 0:714293de3836 | 291 | 44, 49, 39, 56, 34, 53, |
ashleymills | 0:714293de3836 | 292 | 46, 42, 50, 36, 29, 32 |
ashleymills | 0:714293de3836 | 293 | }; |
ashleymills | 0:714293de3836 | 294 | |
ashleymills | 0:714293de3836 | 295 | /* End of DES-defined tables */ |
ashleymills | 0:714293de3836 | 296 | |
ashleymills | 0:714293de3836 | 297 | /* bit 0 is left-most in byte */ |
ashleymills | 0:714293de3836 | 298 | static const int bytebit[] = { |
ashleymills | 0:714293de3836 | 299 | 0200,0100,040,020,010,04,02,01 |
ashleymills | 0:714293de3836 | 300 | }; |
ashleymills | 0:714293de3836 | 301 | |
ashleymills | 0:714293de3836 | 302 | const word32 Spbox[8][64] = { |
ashleymills | 0:714293de3836 | 303 | { |
ashleymills | 0:714293de3836 | 304 | 0x01010400,0x00000000,0x00010000,0x01010404, |
ashleymills | 0:714293de3836 | 305 | 0x01010004,0x00010404,0x00000004,0x00010000, |
ashleymills | 0:714293de3836 | 306 | 0x00000400,0x01010400,0x01010404,0x00000400, |
ashleymills | 0:714293de3836 | 307 | 0x01000404,0x01010004,0x01000000,0x00000004, |
ashleymills | 0:714293de3836 | 308 | 0x00000404,0x01000400,0x01000400,0x00010400, |
ashleymills | 0:714293de3836 | 309 | 0x00010400,0x01010000,0x01010000,0x01000404, |
ashleymills | 0:714293de3836 | 310 | 0x00010004,0x01000004,0x01000004,0x00010004, |
ashleymills | 0:714293de3836 | 311 | 0x00000000,0x00000404,0x00010404,0x01000000, |
ashleymills | 0:714293de3836 | 312 | 0x00010000,0x01010404,0x00000004,0x01010000, |
ashleymills | 0:714293de3836 | 313 | 0x01010400,0x01000000,0x01000000,0x00000400, |
ashleymills | 0:714293de3836 | 314 | 0x01010004,0x00010000,0x00010400,0x01000004, |
ashleymills | 0:714293de3836 | 315 | 0x00000400,0x00000004,0x01000404,0x00010404, |
ashleymills | 0:714293de3836 | 316 | 0x01010404,0x00010004,0x01010000,0x01000404, |
ashleymills | 0:714293de3836 | 317 | 0x01000004,0x00000404,0x00010404,0x01010400, |
ashleymills | 0:714293de3836 | 318 | 0x00000404,0x01000400,0x01000400,0x00000000, |
ashleymills | 0:714293de3836 | 319 | 0x00010004,0x00010400,0x00000000,0x01010004}, |
ashleymills | 0:714293de3836 | 320 | { |
ashleymills | 0:714293de3836 | 321 | 0x80108020,0x80008000,0x00008000,0x00108020, |
ashleymills | 0:714293de3836 | 322 | 0x00100000,0x00000020,0x80100020,0x80008020, |
ashleymills | 0:714293de3836 | 323 | 0x80000020,0x80108020,0x80108000,0x80000000, |
ashleymills | 0:714293de3836 | 324 | 0x80008000,0x00100000,0x00000020,0x80100020, |
ashleymills | 0:714293de3836 | 325 | 0x00108000,0x00100020,0x80008020,0x00000000, |
ashleymills | 0:714293de3836 | 326 | 0x80000000,0x00008000,0x00108020,0x80100000, |
ashleymills | 0:714293de3836 | 327 | 0x00100020,0x80000020,0x00000000,0x00108000, |
ashleymills | 0:714293de3836 | 328 | 0x00008020,0x80108000,0x80100000,0x00008020, |
ashleymills | 0:714293de3836 | 329 | 0x00000000,0x00108020,0x80100020,0x00100000, |
ashleymills | 0:714293de3836 | 330 | 0x80008020,0x80100000,0x80108000,0x00008000, |
ashleymills | 0:714293de3836 | 331 | 0x80100000,0x80008000,0x00000020,0x80108020, |
ashleymills | 0:714293de3836 | 332 | 0x00108020,0x00000020,0x00008000,0x80000000, |
ashleymills | 0:714293de3836 | 333 | 0x00008020,0x80108000,0x00100000,0x80000020, |
ashleymills | 0:714293de3836 | 334 | 0x00100020,0x80008020,0x80000020,0x00100020, |
ashleymills | 0:714293de3836 | 335 | 0x00108000,0x00000000,0x80008000,0x00008020, |
ashleymills | 0:714293de3836 | 336 | 0x80000000,0x80100020,0x80108020,0x00108000}, |
ashleymills | 0:714293de3836 | 337 | { |
ashleymills | 0:714293de3836 | 338 | 0x00000208,0x08020200,0x00000000,0x08020008, |
ashleymills | 0:714293de3836 | 339 | 0x08000200,0x00000000,0x00020208,0x08000200, |
ashleymills | 0:714293de3836 | 340 | 0x00020008,0x08000008,0x08000008,0x00020000, |
ashleymills | 0:714293de3836 | 341 | 0x08020208,0x00020008,0x08020000,0x00000208, |
ashleymills | 0:714293de3836 | 342 | 0x08000000,0x00000008,0x08020200,0x00000200, |
ashleymills | 0:714293de3836 | 343 | 0x00020200,0x08020000,0x08020008,0x00020208, |
ashleymills | 0:714293de3836 | 344 | 0x08000208,0x00020200,0x00020000,0x08000208, |
ashleymills | 0:714293de3836 | 345 | 0x00000008,0x08020208,0x00000200,0x08000000, |
ashleymills | 0:714293de3836 | 346 | 0x08020200,0x08000000,0x00020008,0x00000208, |
ashleymills | 0:714293de3836 | 347 | 0x00020000,0x08020200,0x08000200,0x00000000, |
ashleymills | 0:714293de3836 | 348 | 0x00000200,0x00020008,0x08020208,0x08000200, |
ashleymills | 0:714293de3836 | 349 | 0x08000008,0x00000200,0x00000000,0x08020008, |
ashleymills | 0:714293de3836 | 350 | 0x08000208,0x00020000,0x08000000,0x08020208, |
ashleymills | 0:714293de3836 | 351 | 0x00000008,0x00020208,0x00020200,0x08000008, |
ashleymills | 0:714293de3836 | 352 | 0x08020000,0x08000208,0x00000208,0x08020000, |
ashleymills | 0:714293de3836 | 353 | 0x00020208,0x00000008,0x08020008,0x00020200}, |
ashleymills | 0:714293de3836 | 354 | { |
ashleymills | 0:714293de3836 | 355 | 0x00802001,0x00002081,0x00002081,0x00000080, |
ashleymills | 0:714293de3836 | 356 | 0x00802080,0x00800081,0x00800001,0x00002001, |
ashleymills | 0:714293de3836 | 357 | 0x00000000,0x00802000,0x00802000,0x00802081, |
ashleymills | 0:714293de3836 | 358 | 0x00000081,0x00000000,0x00800080,0x00800001, |
ashleymills | 0:714293de3836 | 359 | 0x00000001,0x00002000,0x00800000,0x00802001, |
ashleymills | 0:714293de3836 | 360 | 0x00000080,0x00800000,0x00002001,0x00002080, |
ashleymills | 0:714293de3836 | 361 | 0x00800081,0x00000001,0x00002080,0x00800080, |
ashleymills | 0:714293de3836 | 362 | 0x00002000,0x00802080,0x00802081,0x00000081, |
ashleymills | 0:714293de3836 | 363 | 0x00800080,0x00800001,0x00802000,0x00802081, |
ashleymills | 0:714293de3836 | 364 | 0x00000081,0x00000000,0x00000000,0x00802000, |
ashleymills | 0:714293de3836 | 365 | 0x00002080,0x00800080,0x00800081,0x00000001, |
ashleymills | 0:714293de3836 | 366 | 0x00802001,0x00002081,0x00002081,0x00000080, |
ashleymills | 0:714293de3836 | 367 | 0x00802081,0x00000081,0x00000001,0x00002000, |
ashleymills | 0:714293de3836 | 368 | 0x00800001,0x00002001,0x00802080,0x00800081, |
ashleymills | 0:714293de3836 | 369 | 0x00002001,0x00002080,0x00800000,0x00802001, |
ashleymills | 0:714293de3836 | 370 | 0x00000080,0x00800000,0x00002000,0x00802080}, |
ashleymills | 0:714293de3836 | 371 | { |
ashleymills | 0:714293de3836 | 372 | 0x00000100,0x02080100,0x02080000,0x42000100, |
ashleymills | 0:714293de3836 | 373 | 0x00080000,0x00000100,0x40000000,0x02080000, |
ashleymills | 0:714293de3836 | 374 | 0x40080100,0x00080000,0x02000100,0x40080100, |
ashleymills | 0:714293de3836 | 375 | 0x42000100,0x42080000,0x00080100,0x40000000, |
ashleymills | 0:714293de3836 | 376 | 0x02000000,0x40080000,0x40080000,0x00000000, |
ashleymills | 0:714293de3836 | 377 | 0x40000100,0x42080100,0x42080100,0x02000100, |
ashleymills | 0:714293de3836 | 378 | 0x42080000,0x40000100,0x00000000,0x42000000, |
ashleymills | 0:714293de3836 | 379 | 0x02080100,0x02000000,0x42000000,0x00080100, |
ashleymills | 0:714293de3836 | 380 | 0x00080000,0x42000100,0x00000100,0x02000000, |
ashleymills | 0:714293de3836 | 381 | 0x40000000,0x02080000,0x42000100,0x40080100, |
ashleymills | 0:714293de3836 | 382 | 0x02000100,0x40000000,0x42080000,0x02080100, |
ashleymills | 0:714293de3836 | 383 | 0x40080100,0x00000100,0x02000000,0x42080000, |
ashleymills | 0:714293de3836 | 384 | 0x42080100,0x00080100,0x42000000,0x42080100, |
ashleymills | 0:714293de3836 | 385 | 0x02080000,0x00000000,0x40080000,0x42000000, |
ashleymills | 0:714293de3836 | 386 | 0x00080100,0x02000100,0x40000100,0x00080000, |
ashleymills | 0:714293de3836 | 387 | 0x00000000,0x40080000,0x02080100,0x40000100}, |
ashleymills | 0:714293de3836 | 388 | { |
ashleymills | 0:714293de3836 | 389 | 0x20000010,0x20400000,0x00004000,0x20404010, |
ashleymills | 0:714293de3836 | 390 | 0x20400000,0x00000010,0x20404010,0x00400000, |
ashleymills | 0:714293de3836 | 391 | 0x20004000,0x00404010,0x00400000,0x20000010, |
ashleymills | 0:714293de3836 | 392 | 0x00400010,0x20004000,0x20000000,0x00004010, |
ashleymills | 0:714293de3836 | 393 | 0x00000000,0x00400010,0x20004010,0x00004000, |
ashleymills | 0:714293de3836 | 394 | 0x00404000,0x20004010,0x00000010,0x20400010, |
ashleymills | 0:714293de3836 | 395 | 0x20400010,0x00000000,0x00404010,0x20404000, |
ashleymills | 0:714293de3836 | 396 | 0x00004010,0x00404000,0x20404000,0x20000000, |
ashleymills | 0:714293de3836 | 397 | 0x20004000,0x00000010,0x20400010,0x00404000, |
ashleymills | 0:714293de3836 | 398 | 0x20404010,0x00400000,0x00004010,0x20000010, |
ashleymills | 0:714293de3836 | 399 | 0x00400000,0x20004000,0x20000000,0x00004010, |
ashleymills | 0:714293de3836 | 400 | 0x20000010,0x20404010,0x00404000,0x20400000, |
ashleymills | 0:714293de3836 | 401 | 0x00404010,0x20404000,0x00000000,0x20400010, |
ashleymills | 0:714293de3836 | 402 | 0x00000010,0x00004000,0x20400000,0x00404010, |
ashleymills | 0:714293de3836 | 403 | 0x00004000,0x00400010,0x20004010,0x00000000, |
ashleymills | 0:714293de3836 | 404 | 0x20404000,0x20000000,0x00400010,0x20004010}, |
ashleymills | 0:714293de3836 | 405 | { |
ashleymills | 0:714293de3836 | 406 | 0x00200000,0x04200002,0x04000802,0x00000000, |
ashleymills | 0:714293de3836 | 407 | 0x00000800,0x04000802,0x00200802,0x04200800, |
ashleymills | 0:714293de3836 | 408 | 0x04200802,0x00200000,0x00000000,0x04000002, |
ashleymills | 0:714293de3836 | 409 | 0x00000002,0x04000000,0x04200002,0x00000802, |
ashleymills | 0:714293de3836 | 410 | 0x04000800,0x00200802,0x00200002,0x04000800, |
ashleymills | 0:714293de3836 | 411 | 0x04000002,0x04200000,0x04200800,0x00200002, |
ashleymills | 0:714293de3836 | 412 | 0x04200000,0x00000800,0x00000802,0x04200802, |
ashleymills | 0:714293de3836 | 413 | 0x00200800,0x00000002,0x04000000,0x00200800, |
ashleymills | 0:714293de3836 | 414 | 0x04000000,0x00200800,0x00200000,0x04000802, |
ashleymills | 0:714293de3836 | 415 | 0x04000802,0x04200002,0x04200002,0x00000002, |
ashleymills | 0:714293de3836 | 416 | 0x00200002,0x04000000,0x04000800,0x00200000, |
ashleymills | 0:714293de3836 | 417 | 0x04200800,0x00000802,0x00200802,0x04200800, |
ashleymills | 0:714293de3836 | 418 | 0x00000802,0x04000002,0x04200802,0x04200000, |
ashleymills | 0:714293de3836 | 419 | 0x00200800,0x00000000,0x00000002,0x04200802, |
ashleymills | 0:714293de3836 | 420 | 0x00000000,0x00200802,0x04200000,0x00000800, |
ashleymills | 0:714293de3836 | 421 | 0x04000002,0x04000800,0x00000800,0x00200002}, |
ashleymills | 0:714293de3836 | 422 | { |
ashleymills | 0:714293de3836 | 423 | 0x10001040,0x00001000,0x00040000,0x10041040, |
ashleymills | 0:714293de3836 | 424 | 0x10000000,0x10001040,0x00000040,0x10000000, |
ashleymills | 0:714293de3836 | 425 | 0x00040040,0x10040000,0x10041040,0x00041000, |
ashleymills | 0:714293de3836 | 426 | 0x10041000,0x00041040,0x00001000,0x00000040, |
ashleymills | 0:714293de3836 | 427 | 0x10040000,0x10000040,0x10001000,0x00001040, |
ashleymills | 0:714293de3836 | 428 | 0x00041000,0x00040040,0x10040040,0x10041000, |
ashleymills | 0:714293de3836 | 429 | 0x00001040,0x00000000,0x00000000,0x10040040, |
ashleymills | 0:714293de3836 | 430 | 0x10000040,0x10001000,0x00041040,0x00040000, |
ashleymills | 0:714293de3836 | 431 | 0x00041040,0x00040000,0x10041000,0x00001000, |
ashleymills | 0:714293de3836 | 432 | 0x00000040,0x10040040,0x00001000,0x00041040, |
ashleymills | 0:714293de3836 | 433 | 0x10001000,0x00000040,0x10000040,0x10040000, |
ashleymills | 0:714293de3836 | 434 | 0x10040040,0x10000000,0x00040000,0x10001040, |
ashleymills | 0:714293de3836 | 435 | 0x00000000,0x10041040,0x00040040,0x10000040, |
ashleymills | 0:714293de3836 | 436 | 0x10040000,0x10001000,0x10001040,0x00000000, |
ashleymills | 0:714293de3836 | 437 | 0x10041040,0x00041000,0x00041000,0x00001040, |
ashleymills | 0:714293de3836 | 438 | 0x00001040,0x00040040,0x10000000,0x10041000} |
ashleymills | 0:714293de3836 | 439 | }; |
ashleymills | 0:714293de3836 | 440 | |
ashleymills | 0:714293de3836 | 441 | |
ashleymills | 0:714293de3836 | 442 | static INLINE void IPERM(word32* left, word32* right) |
ashleymills | 0:714293de3836 | 443 | { |
ashleymills | 0:714293de3836 | 444 | word32 work; |
ashleymills | 0:714293de3836 | 445 | |
ashleymills | 0:714293de3836 | 446 | *right = rotlFixed(*right, 4U); |
ashleymills | 0:714293de3836 | 447 | work = (*left ^ *right) & 0xf0f0f0f0; |
ashleymills | 0:714293de3836 | 448 | *left ^= work; |
ashleymills | 0:714293de3836 | 449 | |
ashleymills | 0:714293de3836 | 450 | *right = rotrFixed(*right^work, 20U); |
ashleymills | 0:714293de3836 | 451 | work = (*left ^ *right) & 0xffff0000; |
ashleymills | 0:714293de3836 | 452 | *left ^= work; |
ashleymills | 0:714293de3836 | 453 | |
ashleymills | 0:714293de3836 | 454 | *right = rotrFixed(*right^work, 18U); |
ashleymills | 0:714293de3836 | 455 | work = (*left ^ *right) & 0x33333333; |
ashleymills | 0:714293de3836 | 456 | *left ^= work; |
ashleymills | 0:714293de3836 | 457 | |
ashleymills | 0:714293de3836 | 458 | *right = rotrFixed(*right^work, 6U); |
ashleymills | 0:714293de3836 | 459 | work = (*left ^ *right) & 0x00ff00ff; |
ashleymills | 0:714293de3836 | 460 | *left ^= work; |
ashleymills | 0:714293de3836 | 461 | |
ashleymills | 0:714293de3836 | 462 | *right = rotlFixed(*right^work, 9U); |
ashleymills | 0:714293de3836 | 463 | work = (*left ^ *right) & 0xaaaaaaaa; |
ashleymills | 0:714293de3836 | 464 | *left = rotlFixed(*left^work, 1U); |
ashleymills | 0:714293de3836 | 465 | *right ^= work; |
ashleymills | 0:714293de3836 | 466 | } |
ashleymills | 0:714293de3836 | 467 | |
ashleymills | 0:714293de3836 | 468 | |
ashleymills | 0:714293de3836 | 469 | static INLINE void FPERM(word32* left, word32* right) |
ashleymills | 0:714293de3836 | 470 | { |
ashleymills | 0:714293de3836 | 471 | word32 work; |
ashleymills | 0:714293de3836 | 472 | |
ashleymills | 0:714293de3836 | 473 | *right = rotrFixed(*right, 1U); |
ashleymills | 0:714293de3836 | 474 | work = (*left ^ *right) & 0xaaaaaaaa; |
ashleymills | 0:714293de3836 | 475 | *right ^= work; |
ashleymills | 0:714293de3836 | 476 | |
ashleymills | 0:714293de3836 | 477 | *left = rotrFixed(*left^work, 9U); |
ashleymills | 0:714293de3836 | 478 | work = (*left ^ *right) & 0x00ff00ff; |
ashleymills | 0:714293de3836 | 479 | *right ^= work; |
ashleymills | 0:714293de3836 | 480 | |
ashleymills | 0:714293de3836 | 481 | *left = rotlFixed(*left^work, 6U); |
ashleymills | 0:714293de3836 | 482 | work = (*left ^ *right) & 0x33333333; |
ashleymills | 0:714293de3836 | 483 | *right ^= work; |
ashleymills | 0:714293de3836 | 484 | |
ashleymills | 0:714293de3836 | 485 | *left = rotlFixed(*left^work, 18U); |
ashleymills | 0:714293de3836 | 486 | work = (*left ^ *right) & 0xffff0000; |
ashleymills | 0:714293de3836 | 487 | *right ^= work; |
ashleymills | 0:714293de3836 | 488 | |
ashleymills | 0:714293de3836 | 489 | *left = rotlFixed(*left^work, 20U); |
ashleymills | 0:714293de3836 | 490 | work = (*left ^ *right) & 0xf0f0f0f0; |
ashleymills | 0:714293de3836 | 491 | *right ^= work; |
ashleymills | 0:714293de3836 | 492 | |
ashleymills | 0:714293de3836 | 493 | *left = rotrFixed(*left^work, 4U); |
ashleymills | 0:714293de3836 | 494 | } |
ashleymills | 0:714293de3836 | 495 | |
ashleymills | 0:714293de3836 | 496 | |
ashleymills | 0:714293de3836 | 497 | static void DesSetKey(const byte* key, int dir, word32* out) |
ashleymills | 0:714293de3836 | 498 | { |
ashleymills | 0:714293de3836 | 499 | byte buffer[56+56+8]; |
ashleymills | 0:714293de3836 | 500 | byte *const pc1m = buffer; /* place to modify pc1 into */ |
ashleymills | 0:714293de3836 | 501 | byte *const pcr = pc1m + 56; /* place to rotate pc1 into */ |
ashleymills | 0:714293de3836 | 502 | byte *const ks = pcr + 56; |
ashleymills | 0:714293de3836 | 503 | register int i,j,l; |
ashleymills | 0:714293de3836 | 504 | int m; |
ashleymills | 0:714293de3836 | 505 | |
ashleymills | 0:714293de3836 | 506 | for (j = 0; j < 56; j++) { /* convert pc1 to bits of key */ |
ashleymills | 0:714293de3836 | 507 | l = pc1[j] - 1; /* integer bit location */ |
ashleymills | 0:714293de3836 | 508 | m = l & 07; /* find bit */ |
ashleymills | 0:714293de3836 | 509 | pc1m[j] = (key[l >> 3] & /* find which key byte l is in */ |
ashleymills | 0:714293de3836 | 510 | bytebit[m]) /* and which bit of that byte */ |
ashleymills | 0:714293de3836 | 511 | ? 1 : 0; /* and store 1-bit result */ |
ashleymills | 0:714293de3836 | 512 | } |
ashleymills | 0:714293de3836 | 513 | for (i = 0; i < 16; i++) { /* key chunk for each iteration */ |
ashleymills | 0:714293de3836 | 514 | XMEMSET(ks, 0, 8); /* Clear key schedule */ |
ashleymills | 0:714293de3836 | 515 | for (j = 0; j < 56; j++) /* rotate pc1 the right amount */ |
ashleymills | 0:714293de3836 | 516 | pcr[j] = pc1m[(l = j + totrot[i]) < (j < 28 ? 28 : 56) ? l: l-28]; |
ashleymills | 0:714293de3836 | 517 | /* rotate left and right halves independently */ |
ashleymills | 0:714293de3836 | 518 | for (j = 0; j < 48; j++){ /* select bits individually */ |
ashleymills | 0:714293de3836 | 519 | /* check bit that goes to ks[j] */ |
ashleymills | 0:714293de3836 | 520 | if (pcr[pc2[j] - 1]){ |
ashleymills | 0:714293de3836 | 521 | /* mask it in if it's there */ |
ashleymills | 0:714293de3836 | 522 | l= j % 6; |
ashleymills | 0:714293de3836 | 523 | ks[j/6] |= bytebit[l] >> 2; |
ashleymills | 0:714293de3836 | 524 | } |
ashleymills | 0:714293de3836 | 525 | } |
ashleymills | 0:714293de3836 | 526 | /* Now convert to odd/even interleaved form for use in F */ |
ashleymills | 0:714293de3836 | 527 | out[2*i] = ((word32)ks[0] << 24) |
ashleymills | 0:714293de3836 | 528 | | ((word32)ks[2] << 16) |
ashleymills | 0:714293de3836 | 529 | | ((word32)ks[4] << 8) |
ashleymills | 0:714293de3836 | 530 | | ((word32)ks[6]); |
ashleymills | 0:714293de3836 | 531 | out[2*i + 1] = ((word32)ks[1] << 24) |
ashleymills | 0:714293de3836 | 532 | | ((word32)ks[3] << 16) |
ashleymills | 0:714293de3836 | 533 | | ((word32)ks[5] << 8) |
ashleymills | 0:714293de3836 | 534 | | ((word32)ks[7]); |
ashleymills | 0:714293de3836 | 535 | } |
ashleymills | 0:714293de3836 | 536 | |
ashleymills | 0:714293de3836 | 537 | /* reverse key schedule order */ |
ashleymills | 0:714293de3836 | 538 | if (dir == DES_DECRYPTION) |
ashleymills | 0:714293de3836 | 539 | for (i = 0; i < 16; i += 2) { |
ashleymills | 0:714293de3836 | 540 | word32 swap = out[i]; |
ashleymills | 0:714293de3836 | 541 | out[i] = out[DES_KS_SIZE - 2 - i]; |
ashleymills | 0:714293de3836 | 542 | out[DES_KS_SIZE - 2 - i] = swap; |
ashleymills | 0:714293de3836 | 543 | |
ashleymills | 0:714293de3836 | 544 | swap = out[i + 1]; |
ashleymills | 0:714293de3836 | 545 | out[i + 1] = out[DES_KS_SIZE - 1 - i]; |
ashleymills | 0:714293de3836 | 546 | out[DES_KS_SIZE - 1 - i] = swap; |
ashleymills | 0:714293de3836 | 547 | } |
ashleymills | 0:714293de3836 | 548 | |
ashleymills | 0:714293de3836 | 549 | } |
ashleymills | 0:714293de3836 | 550 | |
ashleymills | 0:714293de3836 | 551 | |
ashleymills | 0:714293de3836 | 552 | static INLINE int Reverse(int dir) |
ashleymills | 0:714293de3836 | 553 | { |
ashleymills | 0:714293de3836 | 554 | return !dir; |
ashleymills | 0:714293de3836 | 555 | } |
ashleymills | 0:714293de3836 | 556 | |
ashleymills | 0:714293de3836 | 557 | |
ashleymills | 0:714293de3836 | 558 | void Des_SetKey(Des* des, const byte* key, const byte* iv, int dir) |
ashleymills | 0:714293de3836 | 559 | { |
ashleymills | 0:714293de3836 | 560 | DesSetKey(key, dir, des->key); |
ashleymills | 0:714293de3836 | 561 | |
ashleymills | 0:714293de3836 | 562 | Des_SetIV(des, iv); |
ashleymills | 0:714293de3836 | 563 | } |
ashleymills | 0:714293de3836 | 564 | |
ashleymills | 0:714293de3836 | 565 | |
ashleymills | 0:714293de3836 | 566 | void Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir) |
ashleymills | 0:714293de3836 | 567 | { |
ashleymills | 0:714293de3836 | 568 | #ifdef HAVE_CAVIUM |
ashleymills | 0:714293de3836 | 569 | if (des->magic == CYASSL_3DES_CAVIUM_MAGIC) |
ashleymills | 0:714293de3836 | 570 | return Des3_CaviumSetKey(des, key, iv); |
ashleymills | 0:714293de3836 | 571 | #endif |
ashleymills | 0:714293de3836 | 572 | |
ashleymills | 0:714293de3836 | 573 | DesSetKey(key + (dir == DES_ENCRYPTION ? 0 : 16), dir, des->key[0]); |
ashleymills | 0:714293de3836 | 574 | DesSetKey(key + 8, Reverse(dir), des->key[1]); |
ashleymills | 0:714293de3836 | 575 | DesSetKey(key + (dir == DES_DECRYPTION ? 0 : 16), dir, des->key[2]); |
ashleymills | 0:714293de3836 | 576 | |
ashleymills | 0:714293de3836 | 577 | Des3_SetIV(des, iv); |
ashleymills | 0:714293de3836 | 578 | } |
ashleymills | 0:714293de3836 | 579 | |
ashleymills | 0:714293de3836 | 580 | |
ashleymills | 0:714293de3836 | 581 | static void DesRawProcessBlock(word32* lIn, word32* rIn, const word32* kptr) |
ashleymills | 0:714293de3836 | 582 | { |
ashleymills | 0:714293de3836 | 583 | word32 l = *lIn, r = *rIn, i; |
ashleymills | 0:714293de3836 | 584 | |
ashleymills | 0:714293de3836 | 585 | for (i=0; i<8; i++) |
ashleymills | 0:714293de3836 | 586 | { |
ashleymills | 0:714293de3836 | 587 | word32 work = rotrFixed(r, 4U) ^ kptr[4*i+0]; |
ashleymills | 0:714293de3836 | 588 | l ^= Spbox[6][(work) & 0x3f] |
ashleymills | 0:714293de3836 | 589 | ^ Spbox[4][(work >> 8) & 0x3f] |
ashleymills | 0:714293de3836 | 590 | ^ Spbox[2][(work >> 16) & 0x3f] |
ashleymills | 0:714293de3836 | 591 | ^ Spbox[0][(work >> 24) & 0x3f]; |
ashleymills | 0:714293de3836 | 592 | work = r ^ kptr[4*i+1]; |
ashleymills | 0:714293de3836 | 593 | l ^= Spbox[7][(work) & 0x3f] |
ashleymills | 0:714293de3836 | 594 | ^ Spbox[5][(work >> 8) & 0x3f] |
ashleymills | 0:714293de3836 | 595 | ^ Spbox[3][(work >> 16) & 0x3f] |
ashleymills | 0:714293de3836 | 596 | ^ Spbox[1][(work >> 24) & 0x3f]; |
ashleymills | 0:714293de3836 | 597 | |
ashleymills | 0:714293de3836 | 598 | work = rotrFixed(l, 4U) ^ kptr[4*i+2]; |
ashleymills | 0:714293de3836 | 599 | r ^= Spbox[6][(work) & 0x3f] |
ashleymills | 0:714293de3836 | 600 | ^ Spbox[4][(work >> 8) & 0x3f] |
ashleymills | 0:714293de3836 | 601 | ^ Spbox[2][(work >> 16) & 0x3f] |
ashleymills | 0:714293de3836 | 602 | ^ Spbox[0][(work >> 24) & 0x3f]; |
ashleymills | 0:714293de3836 | 603 | work = l ^ kptr[4*i+3]; |
ashleymills | 0:714293de3836 | 604 | r ^= Spbox[7][(work) & 0x3f] |
ashleymills | 0:714293de3836 | 605 | ^ Spbox[5][(work >> 8) & 0x3f] |
ashleymills | 0:714293de3836 | 606 | ^ Spbox[3][(work >> 16) & 0x3f] |
ashleymills | 0:714293de3836 | 607 | ^ Spbox[1][(work >> 24) & 0x3f]; |
ashleymills | 0:714293de3836 | 608 | } |
ashleymills | 0:714293de3836 | 609 | |
ashleymills | 0:714293de3836 | 610 | *lIn = l; *rIn = r; |
ashleymills | 0:714293de3836 | 611 | } |
ashleymills | 0:714293de3836 | 612 | |
ashleymills | 0:714293de3836 | 613 | |
ashleymills | 0:714293de3836 | 614 | static void DesProcessBlock(Des* des, const byte* in, byte* out) |
ashleymills | 0:714293de3836 | 615 | { |
ashleymills | 0:714293de3836 | 616 | word32 l, r; |
ashleymills | 0:714293de3836 | 617 | |
ashleymills | 0:714293de3836 | 618 | XMEMCPY(&l, in, sizeof(l)); |
ashleymills | 0:714293de3836 | 619 | XMEMCPY(&r, in + sizeof(l), sizeof(r)); |
ashleymills | 0:714293de3836 | 620 | #ifdef LITTLE_ENDIAN_ORDER |
ashleymills | 0:714293de3836 | 621 | l = ByteReverseWord32(l); |
ashleymills | 0:714293de3836 | 622 | r = ByteReverseWord32(r); |
ashleymills | 0:714293de3836 | 623 | #endif |
ashleymills | 0:714293de3836 | 624 | IPERM(&l,&r); |
ashleymills | 0:714293de3836 | 625 | |
ashleymills | 0:714293de3836 | 626 | DesRawProcessBlock(&l, &r, des->key); |
ashleymills | 0:714293de3836 | 627 | |
ashleymills | 0:714293de3836 | 628 | FPERM(&l,&r); |
ashleymills | 0:714293de3836 | 629 | #ifdef LITTLE_ENDIAN_ORDER |
ashleymills | 0:714293de3836 | 630 | l = ByteReverseWord32(l); |
ashleymills | 0:714293de3836 | 631 | r = ByteReverseWord32(r); |
ashleymills | 0:714293de3836 | 632 | #endif |
ashleymills | 0:714293de3836 | 633 | XMEMCPY(out, &r, sizeof(r)); |
ashleymills | 0:714293de3836 | 634 | XMEMCPY(out + sizeof(r), &l, sizeof(l)); |
ashleymills | 0:714293de3836 | 635 | } |
ashleymills | 0:714293de3836 | 636 | |
ashleymills | 0:714293de3836 | 637 | |
ashleymills | 0:714293de3836 | 638 | static void Des3ProcessBlock(Des3* des, const byte* in, byte* out) |
ashleymills | 0:714293de3836 | 639 | { |
ashleymills | 0:714293de3836 | 640 | word32 l, r; |
ashleymills | 0:714293de3836 | 641 | |
ashleymills | 0:714293de3836 | 642 | XMEMCPY(&l, in, sizeof(l)); |
ashleymills | 0:714293de3836 | 643 | XMEMCPY(&r, in + sizeof(l), sizeof(r)); |
ashleymills | 0:714293de3836 | 644 | #ifdef LITTLE_ENDIAN_ORDER |
ashleymills | 0:714293de3836 | 645 | l = ByteReverseWord32(l); |
ashleymills | 0:714293de3836 | 646 | r = ByteReverseWord32(r); |
ashleymills | 0:714293de3836 | 647 | #endif |
ashleymills | 0:714293de3836 | 648 | IPERM(&l,&r); |
ashleymills | 0:714293de3836 | 649 | |
ashleymills | 0:714293de3836 | 650 | DesRawProcessBlock(&l, &r, des->key[0]); |
ashleymills | 0:714293de3836 | 651 | DesRawProcessBlock(&r, &l, des->key[1]); |
ashleymills | 0:714293de3836 | 652 | DesRawProcessBlock(&l, &r, des->key[2]); |
ashleymills | 0:714293de3836 | 653 | |
ashleymills | 0:714293de3836 | 654 | FPERM(&l,&r); |
ashleymills | 0:714293de3836 | 655 | #ifdef LITTLE_ENDIAN_ORDER |
ashleymills | 0:714293de3836 | 656 | l = ByteReverseWord32(l); |
ashleymills | 0:714293de3836 | 657 | r = ByteReverseWord32(r); |
ashleymills | 0:714293de3836 | 658 | #endif |
ashleymills | 0:714293de3836 | 659 | XMEMCPY(out, &r, sizeof(r)); |
ashleymills | 0:714293de3836 | 660 | XMEMCPY(out + sizeof(r), &l, sizeof(l)); |
ashleymills | 0:714293de3836 | 661 | } |
ashleymills | 0:714293de3836 | 662 | |
ashleymills | 0:714293de3836 | 663 | |
ashleymills | 0:714293de3836 | 664 | void Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz) |
ashleymills | 0:714293de3836 | 665 | { |
ashleymills | 0:714293de3836 | 666 | word32 blocks = sz / DES_BLOCK_SIZE; |
ashleymills | 0:714293de3836 | 667 | |
ashleymills | 0:714293de3836 | 668 | while (blocks--) { |
ashleymills | 0:714293de3836 | 669 | xorbuf((byte*)des->reg, in, DES_BLOCK_SIZE); |
ashleymills | 0:714293de3836 | 670 | DesProcessBlock(des, (byte*)des->reg, (byte*)des->reg); |
ashleymills | 0:714293de3836 | 671 | XMEMCPY(out, des->reg, DES_BLOCK_SIZE); |
ashleymills | 0:714293de3836 | 672 | |
ashleymills | 0:714293de3836 | 673 | out += DES_BLOCK_SIZE; |
ashleymills | 0:714293de3836 | 674 | in += DES_BLOCK_SIZE; |
ashleymills | 0:714293de3836 | 675 | } |
ashleymills | 0:714293de3836 | 676 | } |
ashleymills | 0:714293de3836 | 677 | |
ashleymills | 0:714293de3836 | 678 | |
ashleymills | 0:714293de3836 | 679 | void Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz) |
ashleymills | 0:714293de3836 | 680 | { |
ashleymills | 0:714293de3836 | 681 | word32 blocks = sz / DES_BLOCK_SIZE; |
ashleymills | 0:714293de3836 | 682 | byte hold[DES_BLOCK_SIZE]; |
ashleymills | 0:714293de3836 | 683 | |
ashleymills | 0:714293de3836 | 684 | while (blocks--) { |
ashleymills | 0:714293de3836 | 685 | XMEMCPY(des->tmp, in, DES_BLOCK_SIZE); |
ashleymills | 0:714293de3836 | 686 | DesProcessBlock(des, (byte*)des->tmp, out); |
ashleymills | 0:714293de3836 | 687 | xorbuf(out, (byte*)des->reg, DES_BLOCK_SIZE); |
ashleymills | 0:714293de3836 | 688 | |
ashleymills | 0:714293de3836 | 689 | XMEMCPY(hold, des->reg, DES_BLOCK_SIZE); |
ashleymills | 0:714293de3836 | 690 | XMEMCPY(des->reg, des->tmp, DES_BLOCK_SIZE); |
ashleymills | 0:714293de3836 | 691 | XMEMCPY(des->tmp, hold, DES_BLOCK_SIZE); |
ashleymills | 0:714293de3836 | 692 | |
ashleymills | 0:714293de3836 | 693 | out += DES_BLOCK_SIZE; |
ashleymills | 0:714293de3836 | 694 | in += DES_BLOCK_SIZE; |
ashleymills | 0:714293de3836 | 695 | } |
ashleymills | 0:714293de3836 | 696 | } |
ashleymills | 0:714293de3836 | 697 | |
ashleymills | 0:714293de3836 | 698 | |
ashleymills | 0:714293de3836 | 699 | void Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz) |
ashleymills | 0:714293de3836 | 700 | { |
ashleymills | 0:714293de3836 | 701 | word32 blocks; |
ashleymills | 0:714293de3836 | 702 | |
ashleymills | 0:714293de3836 | 703 | #ifdef HAVE_CAVIUM |
ashleymills | 0:714293de3836 | 704 | if (des->magic == CYASSL_3DES_CAVIUM_MAGIC) |
ashleymills | 0:714293de3836 | 705 | return Des3_CaviumCbcEncrypt(des, out, in, sz); |
ashleymills | 0:714293de3836 | 706 | #endif |
ashleymills | 0:714293de3836 | 707 | |
ashleymills | 0:714293de3836 | 708 | blocks = sz / DES_BLOCK_SIZE; |
ashleymills | 0:714293de3836 | 709 | while (blocks--) { |
ashleymills | 0:714293de3836 | 710 | xorbuf((byte*)des->reg, in, DES_BLOCK_SIZE); |
ashleymills | 0:714293de3836 | 711 | Des3ProcessBlock(des, (byte*)des->reg, (byte*)des->reg); |
ashleymills | 0:714293de3836 | 712 | XMEMCPY(out, des->reg, DES_BLOCK_SIZE); |
ashleymills | 0:714293de3836 | 713 | |
ashleymills | 0:714293de3836 | 714 | out += DES_BLOCK_SIZE; |
ashleymills | 0:714293de3836 | 715 | in += DES_BLOCK_SIZE; |
ashleymills | 0:714293de3836 | 716 | } |
ashleymills | 0:714293de3836 | 717 | } |
ashleymills | 0:714293de3836 | 718 | |
ashleymills | 0:714293de3836 | 719 | |
ashleymills | 0:714293de3836 | 720 | void Des3_CbcDecrypt(Des3* des, byte* out, const byte* in, word32 sz) |
ashleymills | 0:714293de3836 | 721 | { |
ashleymills | 0:714293de3836 | 722 | word32 blocks; |
ashleymills | 0:714293de3836 | 723 | |
ashleymills | 0:714293de3836 | 724 | #ifdef HAVE_CAVIUM |
ashleymills | 0:714293de3836 | 725 | if (des->magic == CYASSL_3DES_CAVIUM_MAGIC) |
ashleymills | 0:714293de3836 | 726 | return Des3_CaviumCbcDecrypt(des, out, in, sz); |
ashleymills | 0:714293de3836 | 727 | #endif |
ashleymills | 0:714293de3836 | 728 | |
ashleymills | 0:714293de3836 | 729 | blocks = sz / DES_BLOCK_SIZE; |
ashleymills | 0:714293de3836 | 730 | while (blocks--) { |
ashleymills | 0:714293de3836 | 731 | XMEMCPY(des->tmp, in, DES_BLOCK_SIZE); |
ashleymills | 0:714293de3836 | 732 | Des3ProcessBlock(des, (byte*)des->tmp, out); |
ashleymills | 0:714293de3836 | 733 | xorbuf(out, (byte*)des->reg, DES_BLOCK_SIZE); |
ashleymills | 0:714293de3836 | 734 | XMEMCPY(des->reg, des->tmp, DES_BLOCK_SIZE); |
ashleymills | 0:714293de3836 | 735 | |
ashleymills | 0:714293de3836 | 736 | out += DES_BLOCK_SIZE; |
ashleymills | 0:714293de3836 | 737 | in += DES_BLOCK_SIZE; |
ashleymills | 0:714293de3836 | 738 | } |
ashleymills | 0:714293de3836 | 739 | } |
ashleymills | 0:714293de3836 | 740 | |
ashleymills | 0:714293de3836 | 741 | #ifdef CYASSL_DES_ECB |
ashleymills | 0:714293de3836 | 742 | |
ashleymills | 0:714293de3836 | 743 | /* One block, compatibility only */ |
ashleymills | 0:714293de3836 | 744 | void Des_EcbEncrypt(Des* des, byte* out, const byte* in, word32 sz) |
ashleymills | 0:714293de3836 | 745 | { |
ashleymills | 0:714293de3836 | 746 | word32 blocks = sz / DES_BLOCK_SIZE; |
ashleymills | 0:714293de3836 | 747 | |
ashleymills | 0:714293de3836 | 748 | while (blocks--) { |
ashleymills | 0:714293de3836 | 749 | DesProcessBlock(des, in, out); |
ashleymills | 0:714293de3836 | 750 | |
ashleymills | 0:714293de3836 | 751 | out += DES_BLOCK_SIZE; |
ashleymills | 0:714293de3836 | 752 | in += DES_BLOCK_SIZE; |
ashleymills | 0:714293de3836 | 753 | } |
ashleymills | 0:714293de3836 | 754 | } |
ashleymills | 0:714293de3836 | 755 | |
ashleymills | 0:714293de3836 | 756 | #endif /* CYASSL_DES_ECB */ |
ashleymills | 0:714293de3836 | 757 | |
ashleymills | 0:714293de3836 | 758 | #endif /* STM32F2_CRYPTO */ |
ashleymills | 0:714293de3836 | 759 | |
ashleymills | 0:714293de3836 | 760 | void Des_SetIV(Des* des, const byte* iv) |
ashleymills | 0:714293de3836 | 761 | { |
ashleymills | 0:714293de3836 | 762 | if (des && iv) |
ashleymills | 0:714293de3836 | 763 | XMEMCPY(des->reg, iv, DES_BLOCK_SIZE); |
ashleymills | 0:714293de3836 | 764 | } |
ashleymills | 0:714293de3836 | 765 | |
ashleymills | 0:714293de3836 | 766 | |
ashleymills | 0:714293de3836 | 767 | void Des3_SetIV(Des3* des, const byte* iv) |
ashleymills | 0:714293de3836 | 768 | { |
ashleymills | 0:714293de3836 | 769 | if (des && iv) |
ashleymills | 0:714293de3836 | 770 | XMEMCPY(des->reg, iv, DES_BLOCK_SIZE); |
ashleymills | 0:714293de3836 | 771 | } |
ashleymills | 0:714293de3836 | 772 | |
ashleymills | 0:714293de3836 | 773 | |
ashleymills | 0:714293de3836 | 774 | #ifdef HAVE_CAVIUM |
ashleymills | 0:714293de3836 | 775 | |
ashleymills | 0:714293de3836 | 776 | #include <cyassl/ctaocrypt/logging.h> |
ashleymills | 0:714293de3836 | 777 | #include "cavium_common.h" |
ashleymills | 0:714293de3836 | 778 | |
ashleymills | 0:714293de3836 | 779 | /* Initiliaze Des3 for use with Nitrox device */ |
ashleymills | 0:714293de3836 | 780 | int Des3_InitCavium(Des3* des3, int devId) |
ashleymills | 0:714293de3836 | 781 | { |
ashleymills | 0:714293de3836 | 782 | if (des3 == NULL) |
ashleymills | 0:714293de3836 | 783 | return -1; |
ashleymills | 0:714293de3836 | 784 | |
ashleymills | 0:714293de3836 | 785 | if (CspAllocContext(CONTEXT_SSL, &des3->contextHandle, devId) != 0) |
ashleymills | 0:714293de3836 | 786 | return -1; |
ashleymills | 0:714293de3836 | 787 | |
ashleymills | 0:714293de3836 | 788 | des3->devId = devId; |
ashleymills | 0:714293de3836 | 789 | des3->magic = CYASSL_3DES_CAVIUM_MAGIC; |
ashleymills | 0:714293de3836 | 790 | |
ashleymills | 0:714293de3836 | 791 | return 0; |
ashleymills | 0:714293de3836 | 792 | } |
ashleymills | 0:714293de3836 | 793 | |
ashleymills | 0:714293de3836 | 794 | |
ashleymills | 0:714293de3836 | 795 | /* Free Des3 from use with Nitrox device */ |
ashleymills | 0:714293de3836 | 796 | void Des3_FreeCavium(Des3* des3) |
ashleymills | 0:714293de3836 | 797 | { |
ashleymills | 0:714293de3836 | 798 | if (des3 == NULL) |
ashleymills | 0:714293de3836 | 799 | return; |
ashleymills | 0:714293de3836 | 800 | |
ashleymills | 0:714293de3836 | 801 | if (des3->magic != CYASSL_3DES_CAVIUM_MAGIC) |
ashleymills | 0:714293de3836 | 802 | return; |
ashleymills | 0:714293de3836 | 803 | |
ashleymills | 0:714293de3836 | 804 | CspFreeContext(CONTEXT_SSL, des3->contextHandle, des3->devId); |
ashleymills | 0:714293de3836 | 805 | des3->magic = 0; |
ashleymills | 0:714293de3836 | 806 | } |
ashleymills | 0:714293de3836 | 807 | |
ashleymills | 0:714293de3836 | 808 | |
ashleymills | 0:714293de3836 | 809 | static void Des3_CaviumSetKey(Des3* des3, const byte* key, const byte* iv) |
ashleymills | 0:714293de3836 | 810 | { |
ashleymills | 0:714293de3836 | 811 | if (des3 == NULL) |
ashleymills | 0:714293de3836 | 812 | return; |
ashleymills | 0:714293de3836 | 813 | |
ashleymills | 0:714293de3836 | 814 | /* key[0] holds key, iv in reg */ |
ashleymills | 0:714293de3836 | 815 | XMEMCPY(des3->key[0], key, DES_BLOCK_SIZE*3); |
ashleymills | 0:714293de3836 | 816 | |
ashleymills | 0:714293de3836 | 817 | Des3_SetIV(des3, iv); |
ashleymills | 0:714293de3836 | 818 | } |
ashleymills | 0:714293de3836 | 819 | |
ashleymills | 0:714293de3836 | 820 | |
ashleymills | 0:714293de3836 | 821 | static void Des3_CaviumCbcEncrypt(Des3* des3, byte* out, const byte* in, |
ashleymills | 0:714293de3836 | 822 | word32 length) |
ashleymills | 0:714293de3836 | 823 | { |
ashleymills | 0:714293de3836 | 824 | word offset = 0; |
ashleymills | 0:714293de3836 | 825 | word32 requestId; |
ashleymills | 0:714293de3836 | 826 | |
ashleymills | 0:714293de3836 | 827 | while (length > CYASSL_MAX_16BIT) { |
ashleymills | 0:714293de3836 | 828 | word16 slen = (word16)CYASSL_MAX_16BIT; |
ashleymills | 0:714293de3836 | 829 | if (CspEncrypt3Des(CAVIUM_BLOCKING, des3->contextHandle, |
ashleymills | 0:714293de3836 | 830 | CAVIUM_NO_UPDATE, slen, (byte*)in + offset, |
ashleymills | 0:714293de3836 | 831 | out + offset, (byte*)des3->reg, (byte*)des3->key[0], |
ashleymills | 0:714293de3836 | 832 | &requestId, des3->devId) != 0) { |
ashleymills | 0:714293de3836 | 833 | CYASSL_MSG("Bad Cavium 3DES Cbc Encrypt"); |
ashleymills | 0:714293de3836 | 834 | } |
ashleymills | 0:714293de3836 | 835 | length -= CYASSL_MAX_16BIT; |
ashleymills | 0:714293de3836 | 836 | offset += CYASSL_MAX_16BIT; |
ashleymills | 0:714293de3836 | 837 | XMEMCPY(des3->reg, out + offset - DES_BLOCK_SIZE, DES_BLOCK_SIZE); |
ashleymills | 0:714293de3836 | 838 | } |
ashleymills | 0:714293de3836 | 839 | if (length) { |
ashleymills | 0:714293de3836 | 840 | word16 slen = (word16)length; |
ashleymills | 0:714293de3836 | 841 | |
ashleymills | 0:714293de3836 | 842 | if (CspEncrypt3Des(CAVIUM_BLOCKING, des3->contextHandle, |
ashleymills | 0:714293de3836 | 843 | CAVIUM_NO_UPDATE, slen, (byte*)in + offset, |
ashleymills | 0:714293de3836 | 844 | out + offset, (byte*)des3->reg, (byte*)des3->key[0], |
ashleymills | 0:714293de3836 | 845 | &requestId, des3->devId) != 0) { |
ashleymills | 0:714293de3836 | 846 | CYASSL_MSG("Bad Cavium 3DES Cbc Encrypt"); |
ashleymills | 0:714293de3836 | 847 | } |
ashleymills | 0:714293de3836 | 848 | XMEMCPY(des3->reg, out+offset+length - DES_BLOCK_SIZE, DES_BLOCK_SIZE); |
ashleymills | 0:714293de3836 | 849 | } |
ashleymills | 0:714293de3836 | 850 | } |
ashleymills | 0:714293de3836 | 851 | |
ashleymills | 0:714293de3836 | 852 | static void Des3_CaviumCbcDecrypt(Des3* des3, byte* out, const byte* in, |
ashleymills | 0:714293de3836 | 853 | word32 length) |
ashleymills | 0:714293de3836 | 854 | { |
ashleymills | 0:714293de3836 | 855 | word32 requestId; |
ashleymills | 0:714293de3836 | 856 | word offset = 0; |
ashleymills | 0:714293de3836 | 857 | |
ashleymills | 0:714293de3836 | 858 | while (length > CYASSL_MAX_16BIT) { |
ashleymills | 0:714293de3836 | 859 | word16 slen = (word16)CYASSL_MAX_16BIT; |
ashleymills | 0:714293de3836 | 860 | XMEMCPY(des3->tmp, in + offset + slen - DES_BLOCK_SIZE, DES_BLOCK_SIZE); |
ashleymills | 0:714293de3836 | 861 | if (CspDecrypt3Des(CAVIUM_BLOCKING, des3->contextHandle, |
ashleymills | 0:714293de3836 | 862 | CAVIUM_NO_UPDATE, slen, (byte*)in+offset, out+offset, |
ashleymills | 0:714293de3836 | 863 | (byte*)des3->reg, (byte*)des3->key[0], &requestId, |
ashleymills | 0:714293de3836 | 864 | des3->devId) != 0) { |
ashleymills | 0:714293de3836 | 865 | CYASSL_MSG("Bad Cavium 3Des Decrypt"); |
ashleymills | 0:714293de3836 | 866 | } |
ashleymills | 0:714293de3836 | 867 | length -= CYASSL_MAX_16BIT; |
ashleymills | 0:714293de3836 | 868 | offset += CYASSL_MAX_16BIT; |
ashleymills | 0:714293de3836 | 869 | XMEMCPY(des3->reg, des3->tmp, DES_BLOCK_SIZE); |
ashleymills | 0:714293de3836 | 870 | } |
ashleymills | 0:714293de3836 | 871 | if (length) { |
ashleymills | 0:714293de3836 | 872 | word16 slen = (word16)length; |
ashleymills | 0:714293de3836 | 873 | XMEMCPY(des3->tmp, in + offset + slen - DES_BLOCK_SIZE,DES_BLOCK_SIZE); |
ashleymills | 0:714293de3836 | 874 | if (CspDecrypt3Des(CAVIUM_BLOCKING, des3->contextHandle, |
ashleymills | 0:714293de3836 | 875 | CAVIUM_NO_UPDATE, slen, (byte*)in+offset, out+offset, |
ashleymills | 0:714293de3836 | 876 | (byte*)des3->reg, (byte*)des3->key[0], &requestId, |
ashleymills | 0:714293de3836 | 877 | des3->devId) != 0) { |
ashleymills | 0:714293de3836 | 878 | CYASSL_MSG("Bad Cavium 3Des Decrypt"); |
ashleymills | 0:714293de3836 | 879 | } |
ashleymills | 0:714293de3836 | 880 | XMEMCPY(des3->reg, des3->tmp, DES_BLOCK_SIZE); |
ashleymills | 0:714293de3836 | 881 | } |
ashleymills | 0:714293de3836 | 882 | } |
ashleymills | 0:714293de3836 | 883 | |
ashleymills | 0:714293de3836 | 884 | #endif /* HAVE_CAVIUM */ |
ashleymills | 0:714293de3836 | 885 | |
ashleymills | 0:714293de3836 | 886 | #endif /* NO_DES3 */ |