SSL/TLS Library
CyaSSL is SSL/TLS library for embedded systems.
ctaocrypt/src/des3.c@0:9d17e4342598, 2014-04-20 (annotated)
- Committer:
- wolfSSL
- Date:
- Sun Apr 20 12:40:57 2014 +0000
- Revision:
- 0:9d17e4342598
CyaSSL SSL/TLS Library 2.9.4;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wolfSSL | 0:9d17e4342598 | 1 | /* des3.c |
wolfSSL | 0:9d17e4342598 | 2 | * |
wolfSSL | 0:9d17e4342598 | 3 | * Copyright (C) 2006-2013 wolfSSL Inc. |
wolfSSL | 0:9d17e4342598 | 4 | * |
wolfSSL | 0:9d17e4342598 | 5 | * This file is part of CyaSSL. |
wolfSSL | 0:9d17e4342598 | 6 | * |
wolfSSL | 0:9d17e4342598 | 7 | * CyaSSL is free software; you can redistribute it and/or modify |
wolfSSL | 0:9d17e4342598 | 8 | * it under the terms of the GNU General Public License as published by |
wolfSSL | 0:9d17e4342598 | 9 | * the Free Software Foundation; either version 2 of the License, or |
wolfSSL | 0:9d17e4342598 | 10 | * (at your option) any later version. |
wolfSSL | 0:9d17e4342598 | 11 | * |
wolfSSL | 0:9d17e4342598 | 12 | * CyaSSL is distributed in the hope that it will be useful, |
wolfSSL | 0:9d17e4342598 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
wolfSSL | 0:9d17e4342598 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
wolfSSL | 0:9d17e4342598 | 15 | * GNU General Public License for more details. |
wolfSSL | 0:9d17e4342598 | 16 | * |
wolfSSL | 0:9d17e4342598 | 17 | * You should have received a copy of the GNU General Public License |
wolfSSL | 0:9d17e4342598 | 18 | * along with this program; if not, write to the Free Software |
wolfSSL | 0:9d17e4342598 | 19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
wolfSSL | 0:9d17e4342598 | 20 | */ |
wolfSSL | 0:9d17e4342598 | 21 | |
wolfSSL | 0:9d17e4342598 | 22 | #ifdef HAVE_CONFIG_H |
wolfSSL | 0:9d17e4342598 | 23 | #include <config.h> |
wolfSSL | 0:9d17e4342598 | 24 | #endif |
wolfSSL | 0:9d17e4342598 | 25 | |
wolfSSL | 0:9d17e4342598 | 26 | #include <cyassl/ctaocrypt/settings.h> |
wolfSSL | 0:9d17e4342598 | 27 | |
wolfSSL | 0:9d17e4342598 | 28 | #ifndef NO_DES3 |
wolfSSL | 0:9d17e4342598 | 29 | |
wolfSSL | 0:9d17e4342598 | 30 | #ifdef HAVE_FIPS |
wolfSSL | 0:9d17e4342598 | 31 | /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */ |
wolfSSL | 0:9d17e4342598 | 32 | #define FIPS_NO_WRAPPERS |
wolfSSL | 0:9d17e4342598 | 33 | #endif |
wolfSSL | 0:9d17e4342598 | 34 | |
wolfSSL | 0:9d17e4342598 | 35 | #include <cyassl/ctaocrypt/des3.h> |
wolfSSL | 0:9d17e4342598 | 36 | #include <cyassl/ctaocrypt/error-crypt.h> |
wolfSSL | 0:9d17e4342598 | 37 | |
wolfSSL | 0:9d17e4342598 | 38 | #ifdef NO_INLINE |
wolfSSL | 0:9d17e4342598 | 39 | #include <cyassl/ctaocrypt/misc.h> |
wolfSSL | 0:9d17e4342598 | 40 | #else |
wolfSSL | 0:9d17e4342598 | 41 | #include <ctaocrypt/src/misc.c> |
wolfSSL | 0:9d17e4342598 | 42 | #endif |
wolfSSL | 0:9d17e4342598 | 43 | |
wolfSSL | 0:9d17e4342598 | 44 | |
wolfSSL | 0:9d17e4342598 | 45 | #ifdef HAVE_CAVIUM |
wolfSSL | 0:9d17e4342598 | 46 | static int Des3_CaviumSetKey(Des3* des3, const byte* key, const byte* iv); |
wolfSSL | 0:9d17e4342598 | 47 | static int Des3_CaviumCbcEncrypt(Des3* des3, byte* out, const byte* in, |
wolfSSL | 0:9d17e4342598 | 48 | word32 length); |
wolfSSL | 0:9d17e4342598 | 49 | static int Des3_CaviumCbcDecrypt(Des3* des3, byte* out, const byte* in, |
wolfSSL | 0:9d17e4342598 | 50 | word32 length); |
wolfSSL | 0:9d17e4342598 | 51 | #endif |
wolfSSL | 0:9d17e4342598 | 52 | |
wolfSSL | 0:9d17e4342598 | 53 | |
wolfSSL | 0:9d17e4342598 | 54 | |
wolfSSL | 0:9d17e4342598 | 55 | |
wolfSSL | 0:9d17e4342598 | 56 | #ifdef STM32F2_CRYPTO |
wolfSSL | 0:9d17e4342598 | 57 | /* |
wolfSSL | 0:9d17e4342598 | 58 | * STM32F2 hardware DES/3DES support through the STM32F2 standard |
wolfSSL | 0:9d17e4342598 | 59 | * peripheral library. Documentation located in STM32F2xx Standard |
wolfSSL | 0:9d17e4342598 | 60 | * Peripheral Library document (See note in README). |
wolfSSL | 0:9d17e4342598 | 61 | */ |
wolfSSL | 0:9d17e4342598 | 62 | #include "stm32f2xx.h" |
wolfSSL | 0:9d17e4342598 | 63 | #include "stm32f2xx_cryp.h" |
wolfSSL | 0:9d17e4342598 | 64 | |
wolfSSL | 0:9d17e4342598 | 65 | int Des_SetKey(Des* des, const byte* key, const byte* iv, int dir) |
wolfSSL | 0:9d17e4342598 | 66 | { |
wolfSSL | 0:9d17e4342598 | 67 | word32 *dkey = des->key; |
wolfSSL | 0:9d17e4342598 | 68 | |
wolfSSL | 0:9d17e4342598 | 69 | XMEMCPY(dkey, key, 8); |
wolfSSL | 0:9d17e4342598 | 70 | ByteReverseWords(dkey, dkey, 8); |
wolfSSL | 0:9d17e4342598 | 71 | |
wolfSSL | 0:9d17e4342598 | 72 | Des_SetIV(des, iv); |
wolfSSL | 0:9d17e4342598 | 73 | |
wolfSSL | 0:9d17e4342598 | 74 | return 0; |
wolfSSL | 0:9d17e4342598 | 75 | } |
wolfSSL | 0:9d17e4342598 | 76 | |
wolfSSL | 0:9d17e4342598 | 77 | int Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir) |
wolfSSL | 0:9d17e4342598 | 78 | { |
wolfSSL | 0:9d17e4342598 | 79 | word32 *dkey1 = des->key[0]; |
wolfSSL | 0:9d17e4342598 | 80 | word32 *dkey2 = des->key[1]; |
wolfSSL | 0:9d17e4342598 | 81 | word32 *dkey3 = des->key[2]; |
wolfSSL | 0:9d17e4342598 | 82 | |
wolfSSL | 0:9d17e4342598 | 83 | XMEMCPY(dkey1, key, 8); /* set key 1 */ |
wolfSSL | 0:9d17e4342598 | 84 | XMEMCPY(dkey2, key + 8, 8); /* set key 2 */ |
wolfSSL | 0:9d17e4342598 | 85 | XMEMCPY(dkey3, key + 16, 8); /* set key 3 */ |
wolfSSL | 0:9d17e4342598 | 86 | |
wolfSSL | 0:9d17e4342598 | 87 | ByteReverseWords(dkey1, dkey1, 8); |
wolfSSL | 0:9d17e4342598 | 88 | ByteReverseWords(dkey2, dkey2, 8); |
wolfSSL | 0:9d17e4342598 | 89 | ByteReverseWords(dkey3, dkey3, 8); |
wolfSSL | 0:9d17e4342598 | 90 | |
wolfSSL | 0:9d17e4342598 | 91 | return Des3_SetIV(des, iv); |
wolfSSL | 0:9d17e4342598 | 92 | } |
wolfSSL | 0:9d17e4342598 | 93 | |
wolfSSL | 0:9d17e4342598 | 94 | void DesCrypt(Des* des, byte* out, const byte* in, word32 sz, |
wolfSSL | 0:9d17e4342598 | 95 | int dir, int mode) |
wolfSSL | 0:9d17e4342598 | 96 | { |
wolfSSL | 0:9d17e4342598 | 97 | word32 *dkey, *iv; |
wolfSSL | 0:9d17e4342598 | 98 | CRYP_InitTypeDef DES_CRYP_InitStructure; |
wolfSSL | 0:9d17e4342598 | 99 | CRYP_KeyInitTypeDef DES_CRYP_KeyInitStructure; |
wolfSSL | 0:9d17e4342598 | 100 | CRYP_IVInitTypeDef DES_CRYP_IVInitStructure; |
wolfSSL | 0:9d17e4342598 | 101 | |
wolfSSL | 0:9d17e4342598 | 102 | dkey = des->key; |
wolfSSL | 0:9d17e4342598 | 103 | iv = des->reg; |
wolfSSL | 0:9d17e4342598 | 104 | |
wolfSSL | 0:9d17e4342598 | 105 | /* crypto structure initialization */ |
wolfSSL | 0:9d17e4342598 | 106 | CRYP_KeyStructInit(&DES_CRYP_KeyInitStructure); |
wolfSSL | 0:9d17e4342598 | 107 | CRYP_StructInit(&DES_CRYP_InitStructure); |
wolfSSL | 0:9d17e4342598 | 108 | CRYP_IVStructInit(&DES_CRYP_IVInitStructure); |
wolfSSL | 0:9d17e4342598 | 109 | |
wolfSSL | 0:9d17e4342598 | 110 | /* reset registers to their default values */ |
wolfSSL | 0:9d17e4342598 | 111 | CRYP_DeInit(); |
wolfSSL | 0:9d17e4342598 | 112 | |
wolfSSL | 0:9d17e4342598 | 113 | /* set direction, mode, and datatype */ |
wolfSSL | 0:9d17e4342598 | 114 | if (dir == DES_ENCRYPTION) { |
wolfSSL | 0:9d17e4342598 | 115 | DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt; |
wolfSSL | 0:9d17e4342598 | 116 | } else { /* DES_DECRYPTION */ |
wolfSSL | 0:9d17e4342598 | 117 | DES_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt; |
wolfSSL | 0:9d17e4342598 | 118 | } |
wolfSSL | 0:9d17e4342598 | 119 | |
wolfSSL | 0:9d17e4342598 | 120 | if (mode == DES_CBC) { |
wolfSSL | 0:9d17e4342598 | 121 | DES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_DES_CBC; |
wolfSSL | 0:9d17e4342598 | 122 | } else { /* DES_ECB */ |
wolfSSL | 0:9d17e4342598 | 123 | DES_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_DES_ECB; |
wolfSSL | 0:9d17e4342598 | 124 | } |
wolfSSL | 0:9d17e4342598 | 125 | |
wolfSSL | 0:9d17e4342598 | 126 | DES_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b; |
wolfSSL | 0:9d17e4342598 | 127 | CRYP_Init(&DES_CRYP_InitStructure); |
wolfSSL | 0:9d17e4342598 | 128 | |
wolfSSL | 0:9d17e4342598 | 129 | /* load key into correct registers */ |
wolfSSL | 0:9d17e4342598 | 130 | DES_CRYP_KeyInitStructure.CRYP_Key1Left = dkey[0]; |
wolfSSL | 0:9d17e4342598 | 131 | DES_CRYP_KeyInitStructure.CRYP_Key1Right = dkey[1]; |
wolfSSL | 0:9d17e4342598 | 132 | CRYP_KeyInit(&DES_CRYP_KeyInitStructure); |
wolfSSL | 0:9d17e4342598 | 133 | |
wolfSSL | 0:9d17e4342598 | 134 | /* set iv */ |
wolfSSL | 0:9d17e4342598 | 135 | ByteReverseWords(iv, iv, DES_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 136 | DES_CRYP_IVInitStructure.CRYP_IV0Left = iv[0]; |
wolfSSL | 0:9d17e4342598 | 137 | DES_CRYP_IVInitStructure.CRYP_IV0Right = iv[1]; |
wolfSSL | 0:9d17e4342598 | 138 | CRYP_IVInit(&DES_CRYP_IVInitStructure); |
wolfSSL | 0:9d17e4342598 | 139 | |
wolfSSL | 0:9d17e4342598 | 140 | /* enable crypto processor */ |
wolfSSL | 0:9d17e4342598 | 141 | CRYP_Cmd(ENABLE); |
wolfSSL | 0:9d17e4342598 | 142 | |
wolfSSL | 0:9d17e4342598 | 143 | while (sz > 0) |
wolfSSL | 0:9d17e4342598 | 144 | { |
wolfSSL | 0:9d17e4342598 | 145 | /* flush IN/OUT FIFOs */ |
wolfSSL | 0:9d17e4342598 | 146 | CRYP_FIFOFlush(); |
wolfSSL | 0:9d17e4342598 | 147 | |
wolfSSL | 0:9d17e4342598 | 148 | /* if input and output same will overwrite input iv */ |
wolfSSL | 0:9d17e4342598 | 149 | XMEMCPY(des->tmp, in + sz - DES_BLOCK_SIZE, DES_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 150 | |
wolfSSL | 0:9d17e4342598 | 151 | CRYP_DataIn(*(uint32_t*)&in[0]); |
wolfSSL | 0:9d17e4342598 | 152 | CRYP_DataIn(*(uint32_t*)&in[4]); |
wolfSSL | 0:9d17e4342598 | 153 | |
wolfSSL | 0:9d17e4342598 | 154 | /* wait until the complete message has been processed */ |
wolfSSL | 0:9d17e4342598 | 155 | while(CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {} |
wolfSSL | 0:9d17e4342598 | 156 | |
wolfSSL | 0:9d17e4342598 | 157 | *(uint32_t*)&out[0] = CRYP_DataOut(); |
wolfSSL | 0:9d17e4342598 | 158 | *(uint32_t*)&out[4] = CRYP_DataOut(); |
wolfSSL | 0:9d17e4342598 | 159 | |
wolfSSL | 0:9d17e4342598 | 160 | /* store iv for next call */ |
wolfSSL | 0:9d17e4342598 | 161 | XMEMCPY(des->reg, des->tmp, DES_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 162 | |
wolfSSL | 0:9d17e4342598 | 163 | sz -= DES_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 164 | in += DES_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 165 | out += DES_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 166 | } |
wolfSSL | 0:9d17e4342598 | 167 | |
wolfSSL | 0:9d17e4342598 | 168 | /* disable crypto processor */ |
wolfSSL | 0:9d17e4342598 | 169 | CRYP_Cmd(DISABLE); |
wolfSSL | 0:9d17e4342598 | 170 | } |
wolfSSL | 0:9d17e4342598 | 171 | |
wolfSSL | 0:9d17e4342598 | 172 | void Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz) |
wolfSSL | 0:9d17e4342598 | 173 | { |
wolfSSL | 0:9d17e4342598 | 174 | DesCrypt(des, out, in, sz, DES_ENCRYPTION, DES_CBC); |
wolfSSL | 0:9d17e4342598 | 175 | } |
wolfSSL | 0:9d17e4342598 | 176 | |
wolfSSL | 0:9d17e4342598 | 177 | void Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz) |
wolfSSL | 0:9d17e4342598 | 178 | { |
wolfSSL | 0:9d17e4342598 | 179 | DesCrypt(des, out, in, sz, DES_DECRYPTION, DES_CBC); |
wolfSSL | 0:9d17e4342598 | 180 | } |
wolfSSL | 0:9d17e4342598 | 181 | |
wolfSSL | 0:9d17e4342598 | 182 | void Des_EcbEncrypt(Des* des, byte* out, const byte* in, word32 sz) |
wolfSSL | 0:9d17e4342598 | 183 | { |
wolfSSL | 0:9d17e4342598 | 184 | DesCrypt(des, out, in, sz, DES_ENCRYPTION, DES_ECB); |
wolfSSL | 0:9d17e4342598 | 185 | } |
wolfSSL | 0:9d17e4342598 | 186 | |
wolfSSL | 0:9d17e4342598 | 187 | void Des3Crypt(Des3* des, byte* out, const byte* in, word32 sz, |
wolfSSL | 0:9d17e4342598 | 188 | int dir) |
wolfSSL | 0:9d17e4342598 | 189 | { |
wolfSSL | 0:9d17e4342598 | 190 | word32 *dkey1, *dkey2, *dkey3, *iv; |
wolfSSL | 0:9d17e4342598 | 191 | CRYP_InitTypeDef DES3_CRYP_InitStructure; |
wolfSSL | 0:9d17e4342598 | 192 | CRYP_KeyInitTypeDef DES3_CRYP_KeyInitStructure; |
wolfSSL | 0:9d17e4342598 | 193 | CRYP_IVInitTypeDef DES3_CRYP_IVInitStructure; |
wolfSSL | 0:9d17e4342598 | 194 | |
wolfSSL | 0:9d17e4342598 | 195 | dkey1 = des->key[0]; |
wolfSSL | 0:9d17e4342598 | 196 | dkey2 = des->key[1]; |
wolfSSL | 0:9d17e4342598 | 197 | dkey3 = des->key[2]; |
wolfSSL | 0:9d17e4342598 | 198 | iv = des->reg; |
wolfSSL | 0:9d17e4342598 | 199 | |
wolfSSL | 0:9d17e4342598 | 200 | /* crypto structure initialization */ |
wolfSSL | 0:9d17e4342598 | 201 | CRYP_KeyStructInit(&DES3_CRYP_KeyInitStructure); |
wolfSSL | 0:9d17e4342598 | 202 | CRYP_StructInit(&DES3_CRYP_InitStructure); |
wolfSSL | 0:9d17e4342598 | 203 | CRYP_IVStructInit(&DES3_CRYP_IVInitStructure); |
wolfSSL | 0:9d17e4342598 | 204 | |
wolfSSL | 0:9d17e4342598 | 205 | /* reset registers to their default values */ |
wolfSSL | 0:9d17e4342598 | 206 | CRYP_DeInit(); |
wolfSSL | 0:9d17e4342598 | 207 | |
wolfSSL | 0:9d17e4342598 | 208 | /* set direction, mode, and datatype */ |
wolfSSL | 0:9d17e4342598 | 209 | if (dir == DES_ENCRYPTION) { |
wolfSSL | 0:9d17e4342598 | 210 | DES3_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt; |
wolfSSL | 0:9d17e4342598 | 211 | } else { |
wolfSSL | 0:9d17e4342598 | 212 | DES3_CRYP_InitStructure.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt; |
wolfSSL | 0:9d17e4342598 | 213 | } |
wolfSSL | 0:9d17e4342598 | 214 | |
wolfSSL | 0:9d17e4342598 | 215 | DES3_CRYP_InitStructure.CRYP_AlgoMode = CRYP_AlgoMode_TDES_CBC; |
wolfSSL | 0:9d17e4342598 | 216 | DES3_CRYP_InitStructure.CRYP_DataType = CRYP_DataType_8b; |
wolfSSL | 0:9d17e4342598 | 217 | CRYP_Init(&DES3_CRYP_InitStructure); |
wolfSSL | 0:9d17e4342598 | 218 | |
wolfSSL | 0:9d17e4342598 | 219 | /* load key into correct registers */ |
wolfSSL | 0:9d17e4342598 | 220 | DES3_CRYP_KeyInitStructure.CRYP_Key1Left = dkey1[0]; |
wolfSSL | 0:9d17e4342598 | 221 | DES3_CRYP_KeyInitStructure.CRYP_Key1Right = dkey1[1]; |
wolfSSL | 0:9d17e4342598 | 222 | DES3_CRYP_KeyInitStructure.CRYP_Key2Left = dkey2[0]; |
wolfSSL | 0:9d17e4342598 | 223 | DES3_CRYP_KeyInitStructure.CRYP_Key2Right = dkey2[1]; |
wolfSSL | 0:9d17e4342598 | 224 | DES3_CRYP_KeyInitStructure.CRYP_Key3Left = dkey3[0]; |
wolfSSL | 0:9d17e4342598 | 225 | DES3_CRYP_KeyInitStructure.CRYP_Key3Right = dkey3[1]; |
wolfSSL | 0:9d17e4342598 | 226 | CRYP_KeyInit(&DES3_CRYP_KeyInitStructure); |
wolfSSL | 0:9d17e4342598 | 227 | |
wolfSSL | 0:9d17e4342598 | 228 | /* set iv */ |
wolfSSL | 0:9d17e4342598 | 229 | ByteReverseWords(iv, iv, DES_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 230 | DES3_CRYP_IVInitStructure.CRYP_IV0Left = iv[0]; |
wolfSSL | 0:9d17e4342598 | 231 | DES3_CRYP_IVInitStructure.CRYP_IV0Right = iv[1]; |
wolfSSL | 0:9d17e4342598 | 232 | CRYP_IVInit(&DES3_CRYP_IVInitStructure); |
wolfSSL | 0:9d17e4342598 | 233 | |
wolfSSL | 0:9d17e4342598 | 234 | /* enable crypto processor */ |
wolfSSL | 0:9d17e4342598 | 235 | CRYP_Cmd(ENABLE); |
wolfSSL | 0:9d17e4342598 | 236 | |
wolfSSL | 0:9d17e4342598 | 237 | while (sz > 0) |
wolfSSL | 0:9d17e4342598 | 238 | { |
wolfSSL | 0:9d17e4342598 | 239 | /* flush IN/OUT FIFOs */ |
wolfSSL | 0:9d17e4342598 | 240 | CRYP_FIFOFlush(); |
wolfSSL | 0:9d17e4342598 | 241 | |
wolfSSL | 0:9d17e4342598 | 242 | CRYP_DataIn(*(uint32_t*)&in[0]); |
wolfSSL | 0:9d17e4342598 | 243 | CRYP_DataIn(*(uint32_t*)&in[4]); |
wolfSSL | 0:9d17e4342598 | 244 | |
wolfSSL | 0:9d17e4342598 | 245 | /* wait until the complete message has been processed */ |
wolfSSL | 0:9d17e4342598 | 246 | while(CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {} |
wolfSSL | 0:9d17e4342598 | 247 | |
wolfSSL | 0:9d17e4342598 | 248 | *(uint32_t*)&out[0] = CRYP_DataOut(); |
wolfSSL | 0:9d17e4342598 | 249 | *(uint32_t*)&out[4] = CRYP_DataOut(); |
wolfSSL | 0:9d17e4342598 | 250 | |
wolfSSL | 0:9d17e4342598 | 251 | /* store iv for next call */ |
wolfSSL | 0:9d17e4342598 | 252 | XMEMCPY(des->reg, out + sz - DES_BLOCK_SIZE, DES_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 253 | |
wolfSSL | 0:9d17e4342598 | 254 | sz -= DES_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 255 | in += DES_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 256 | out += DES_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 257 | } |
wolfSSL | 0:9d17e4342598 | 258 | |
wolfSSL | 0:9d17e4342598 | 259 | /* disable crypto processor */ |
wolfSSL | 0:9d17e4342598 | 260 | CRYP_Cmd(DISABLE); |
wolfSSL | 0:9d17e4342598 | 261 | |
wolfSSL | 0:9d17e4342598 | 262 | } |
wolfSSL | 0:9d17e4342598 | 263 | |
wolfSSL | 0:9d17e4342598 | 264 | int Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz) |
wolfSSL | 0:9d17e4342598 | 265 | { |
wolfSSL | 0:9d17e4342598 | 266 | Des3Crypt(des, out, in, sz, DES_ENCRYPTION); |
wolfSSL | 0:9d17e4342598 | 267 | return 0; |
wolfSSL | 0:9d17e4342598 | 268 | } |
wolfSSL | 0:9d17e4342598 | 269 | |
wolfSSL | 0:9d17e4342598 | 270 | int Des3_CbcDecrypt(Des3* des, byte* out, const byte* in, word32 sz) |
wolfSSL | 0:9d17e4342598 | 271 | { |
wolfSSL | 0:9d17e4342598 | 272 | Des3Crypt(des, out, in, sz, DES_DECRYPTION); |
wolfSSL | 0:9d17e4342598 | 273 | return 0; |
wolfSSL | 0:9d17e4342598 | 274 | } |
wolfSSL | 0:9d17e4342598 | 275 | |
wolfSSL | 0:9d17e4342598 | 276 | |
wolfSSL | 0:9d17e4342598 | 277 | #elif defined(HAVE_COLDFIRE_SEC) |
wolfSSL | 0:9d17e4342598 | 278 | |
wolfSSL | 0:9d17e4342598 | 279 | #include "sec.h" |
wolfSSL | 0:9d17e4342598 | 280 | #include "mcf548x_sec.h" |
wolfSSL | 0:9d17e4342598 | 281 | |
wolfSSL | 0:9d17e4342598 | 282 | #include "memory_pools.h" |
wolfSSL | 0:9d17e4342598 | 283 | extern TX_BYTE_POOL mp_ncached; /* Non Cached memory pool */ |
wolfSSL | 0:9d17e4342598 | 284 | #define DES_BUFFER_SIZE (DES_BLOCK_SIZE * 16) |
wolfSSL | 0:9d17e4342598 | 285 | static unsigned char *DesBuffer = NULL ; |
wolfSSL | 0:9d17e4342598 | 286 | |
wolfSSL | 0:9d17e4342598 | 287 | #define SEC_DESC_DES_CBC_ENCRYPT 0x20500010 |
wolfSSL | 0:9d17e4342598 | 288 | #define SEC_DESC_DES_CBC_DECRYPT 0x20400010 |
wolfSSL | 0:9d17e4342598 | 289 | #define SEC_DESC_DES3_CBC_ENCRYPT 0x20700010 |
wolfSSL | 0:9d17e4342598 | 290 | #define SEC_DESC_DES3_CBC_DECRYPT 0x20600010 |
wolfSSL | 0:9d17e4342598 | 291 | |
wolfSSL | 0:9d17e4342598 | 292 | extern volatile unsigned char __MBAR[]; |
wolfSSL | 0:9d17e4342598 | 293 | |
wolfSSL | 0:9d17e4342598 | 294 | static void Des_Cbc(Des* des, byte* out, const byte* in, word32 sz, word32 desc) |
wolfSSL | 0:9d17e4342598 | 295 | { |
wolfSSL | 0:9d17e4342598 | 296 | static volatile SECdescriptorType descriptor = { NULL } ; |
wolfSSL | 0:9d17e4342598 | 297 | int ret ; int stat1,stat2 ; |
wolfSSL | 0:9d17e4342598 | 298 | int i ; int size ; |
wolfSSL | 0:9d17e4342598 | 299 | volatile int v ; |
wolfSSL | 0:9d17e4342598 | 300 | |
wolfSSL | 0:9d17e4342598 | 301 | while(sz) { |
wolfSSL | 0:9d17e4342598 | 302 | if((sz%DES_BUFFER_SIZE) == sz) { |
wolfSSL | 0:9d17e4342598 | 303 | size = sz ; |
wolfSSL | 0:9d17e4342598 | 304 | sz = 0 ; |
wolfSSL | 0:9d17e4342598 | 305 | } else { |
wolfSSL | 0:9d17e4342598 | 306 | size = DES_BUFFER_SIZE ; |
wolfSSL | 0:9d17e4342598 | 307 | sz -= DES_BUFFER_SIZE ; |
wolfSSL | 0:9d17e4342598 | 308 | } |
wolfSSL | 0:9d17e4342598 | 309 | |
wolfSSL | 0:9d17e4342598 | 310 | descriptor.header = desc ; |
wolfSSL | 0:9d17e4342598 | 311 | /* |
wolfSSL | 0:9d17e4342598 | 312 | escriptor.length1 = 0x0; |
wolfSSL | 0:9d17e4342598 | 313 | descriptor.pointer1 = NULL; |
wolfSSL | 0:9d17e4342598 | 314 | */ |
wolfSSL | 0:9d17e4342598 | 315 | descriptor.length2 = des->ivlen ; |
wolfSSL | 0:9d17e4342598 | 316 | descriptor.pointer2 = (byte *)des->iv ; |
wolfSSL | 0:9d17e4342598 | 317 | descriptor.length3 = des->keylen ; |
wolfSSL | 0:9d17e4342598 | 318 | descriptor.pointer3 = (byte *)des->key; |
wolfSSL | 0:9d17e4342598 | 319 | descriptor.length4 = size; |
wolfSSL | 0:9d17e4342598 | 320 | descriptor.pointer4 = (byte *)in ; |
wolfSSL | 0:9d17e4342598 | 321 | descriptor.length5 = size; |
wolfSSL | 0:9d17e4342598 | 322 | descriptor.pointer5 = DesBuffer ; |
wolfSSL | 0:9d17e4342598 | 323 | /* |
wolfSSL | 0:9d17e4342598 | 324 | descriptor.length6 = 0; |
wolfSSL | 0:9d17e4342598 | 325 | descriptor.pointer6 = NULL; |
wolfSSL | 0:9d17e4342598 | 326 | descriptor.length7 = 0x0; |
wolfSSL | 0:9d17e4342598 | 327 | descriptor.pointer7 = NULL; |
wolfSSL | 0:9d17e4342598 | 328 | descriptor.nextDescriptorPtr = NULL ; |
wolfSSL | 0:9d17e4342598 | 329 | */ |
wolfSSL | 0:9d17e4342598 | 330 | |
wolfSSL | 0:9d17e4342598 | 331 | /* Initialize SEC and wait for encryption to complete */ |
wolfSSL | 0:9d17e4342598 | 332 | MCF_SEC_CCCR0 = 0x0000001A; //enable channel done notification |
wolfSSL | 0:9d17e4342598 | 333 | |
wolfSSL | 0:9d17e4342598 | 334 | /* Point SEC to the location of the descriptor */ |
wolfSSL | 0:9d17e4342598 | 335 | MCF_SEC_FR0 = (uint32)&descriptor; |
wolfSSL | 0:9d17e4342598 | 336 | |
wolfSSL | 0:9d17e4342598 | 337 | /* poll SISR to determine when channel is complete */ |
wolfSSL | 0:9d17e4342598 | 338 | while (!(MCF_SEC_SISRL) && !(MCF_SEC_SISRH)) |
wolfSSL | 0:9d17e4342598 | 339 | ; |
wolfSSL | 0:9d17e4342598 | 340 | |
wolfSSL | 0:9d17e4342598 | 341 | for(v=0; v<500; v++) ; |
wolfSSL | 0:9d17e4342598 | 342 | |
wolfSSL | 0:9d17e4342598 | 343 | ret = MCF_SEC_SISRH; |
wolfSSL | 0:9d17e4342598 | 344 | stat1 = MCF_SEC_DSR ; |
wolfSSL | 0:9d17e4342598 | 345 | stat2 = MCF_SEC_DISR ; |
wolfSSL | 0:9d17e4342598 | 346 | if(ret & 0xe0000000) |
wolfSSL | 0:9d17e4342598 | 347 | db_printf("Des_Cbc(%x):ISRH=%08x, DSR=%08x, DISR=%08x\n", desc, ret, stat1, stat2) ; |
wolfSSL | 0:9d17e4342598 | 348 | |
wolfSSL | 0:9d17e4342598 | 349 | XMEMCPY(out, DesBuffer, size) ; |
wolfSSL | 0:9d17e4342598 | 350 | |
wolfSSL | 0:9d17e4342598 | 351 | if((desc==SEC_DESC_DES3_CBC_ENCRYPT)||(desc==SEC_DESC_DES_CBC_ENCRYPT)) { |
wolfSSL | 0:9d17e4342598 | 352 | XMEMCPY((void*)des->iv, (void*)&(out[size-DES_IVLEN]), DES_IVLEN) ; |
wolfSSL | 0:9d17e4342598 | 353 | } else { |
wolfSSL | 0:9d17e4342598 | 354 | XMEMCPY((void*)des->iv, (void*)&(in[size-DES_IVLEN]), DES_IVLEN) ; |
wolfSSL | 0:9d17e4342598 | 355 | } |
wolfSSL | 0:9d17e4342598 | 356 | |
wolfSSL | 0:9d17e4342598 | 357 | in += size ; |
wolfSSL | 0:9d17e4342598 | 358 | out += size ; |
wolfSSL | 0:9d17e4342598 | 359 | |
wolfSSL | 0:9d17e4342598 | 360 | } |
wolfSSL | 0:9d17e4342598 | 361 | } |
wolfSSL | 0:9d17e4342598 | 362 | |
wolfSSL | 0:9d17e4342598 | 363 | |
wolfSSL | 0:9d17e4342598 | 364 | void Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz) |
wolfSSL | 0:9d17e4342598 | 365 | { |
wolfSSL | 0:9d17e4342598 | 366 | Des_Cbc(des, out, in, sz, SEC_DESC_DES_CBC_ENCRYPT) ; |
wolfSSL | 0:9d17e4342598 | 367 | } |
wolfSSL | 0:9d17e4342598 | 368 | |
wolfSSL | 0:9d17e4342598 | 369 | void Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz) |
wolfSSL | 0:9d17e4342598 | 370 | { |
wolfSSL | 0:9d17e4342598 | 371 | Des_Cbc(des, out, in, sz, SEC_DESC_DES_CBC_DECRYPT) ; |
wolfSSL | 0:9d17e4342598 | 372 | } |
wolfSSL | 0:9d17e4342598 | 373 | |
wolfSSL | 0:9d17e4342598 | 374 | int Des3_CbcEncrypt(Des3* des3, byte* out, const byte* in, word32 sz) |
wolfSSL | 0:9d17e4342598 | 375 | { |
wolfSSL | 0:9d17e4342598 | 376 | Des_Cbc((Des *)des3, out, in, sz, SEC_DESC_DES3_CBC_ENCRYPT) ; |
wolfSSL | 0:9d17e4342598 | 377 | return 0; |
wolfSSL | 0:9d17e4342598 | 378 | } |
wolfSSL | 0:9d17e4342598 | 379 | |
wolfSSL | 0:9d17e4342598 | 380 | int Des3_CbcDecrypt(Des3* des3, byte* out, const byte* in, word32 sz) |
wolfSSL | 0:9d17e4342598 | 381 | { |
wolfSSL | 0:9d17e4342598 | 382 | Des_Cbc((Des *)des3, out, in, sz, SEC_DESC_DES3_CBC_DECRYPT) ; |
wolfSSL | 0:9d17e4342598 | 383 | return 0; |
wolfSSL | 0:9d17e4342598 | 384 | } |
wolfSSL | 0:9d17e4342598 | 385 | |
wolfSSL | 0:9d17e4342598 | 386 | |
wolfSSL | 0:9d17e4342598 | 387 | int Des_SetKey(Des* des, const byte* key, const byte* iv, int dir) |
wolfSSL | 0:9d17e4342598 | 388 | { |
wolfSSL | 0:9d17e4342598 | 389 | int i ; int status ; |
wolfSSL | 0:9d17e4342598 | 390 | |
wolfSSL | 0:9d17e4342598 | 391 | if(DesBuffer == NULL) { |
wolfSSL | 0:9d17e4342598 | 392 | status = tx_byte_allocate(&mp_ncached,(void *)&DesBuffer,DES_BUFFER_SIZE,TX_NO_WAIT); |
wolfSSL | 0:9d17e4342598 | 393 | } |
wolfSSL | 0:9d17e4342598 | 394 | |
wolfSSL | 0:9d17e4342598 | 395 | XMEMCPY(des->key, key, DES_KEYLEN); |
wolfSSL | 0:9d17e4342598 | 396 | des->keylen = DES_KEYLEN ; |
wolfSSL | 0:9d17e4342598 | 397 | des->ivlen = 0 ; |
wolfSSL | 0:9d17e4342598 | 398 | if (iv) { |
wolfSSL | 0:9d17e4342598 | 399 | XMEMCPY(des->iv, iv, DES_IVLEN); |
wolfSSL | 0:9d17e4342598 | 400 | des->ivlen = DES_IVLEN ; |
wolfSSL | 0:9d17e4342598 | 401 | } else { |
wolfSSL | 0:9d17e4342598 | 402 | for(i=0; i<DES_IVLEN; i++) |
wolfSSL | 0:9d17e4342598 | 403 | des->iv[i] = 0x0 ; |
wolfSSL | 0:9d17e4342598 | 404 | } |
wolfSSL | 0:9d17e4342598 | 405 | |
wolfSSL | 0:9d17e4342598 | 406 | return 0; |
wolfSSL | 0:9d17e4342598 | 407 | } |
wolfSSL | 0:9d17e4342598 | 408 | |
wolfSSL | 0:9d17e4342598 | 409 | int Des3_SetKey(Des3* des3, const byte* key, const byte* iv, int dir) |
wolfSSL | 0:9d17e4342598 | 410 | { |
wolfSSL | 0:9d17e4342598 | 411 | int i ; int status ; |
wolfSSL | 0:9d17e4342598 | 412 | |
wolfSSL | 0:9d17e4342598 | 413 | if(DesBuffer == NULL) { |
wolfSSL | 0:9d17e4342598 | 414 | status = tx_byte_allocate(&mp_ncached,(void *)&DesBuffer,DES_BUFFER_SIZE,TX_NO_WAIT); |
wolfSSL | 0:9d17e4342598 | 415 | } |
wolfSSL | 0:9d17e4342598 | 416 | |
wolfSSL | 0:9d17e4342598 | 417 | XMEMCPY(des3->key, key, DES3_KEYLEN); |
wolfSSL | 0:9d17e4342598 | 418 | des3->keylen = DES3_KEYLEN ; |
wolfSSL | 0:9d17e4342598 | 419 | des3->ivlen = 0 ; |
wolfSSL | 0:9d17e4342598 | 420 | if (iv) { |
wolfSSL | 0:9d17e4342598 | 421 | XMEMCPY(des3->iv, iv, DES3_IVLEN); |
wolfSSL | 0:9d17e4342598 | 422 | des3->ivlen = DES3_IVLEN ; |
wolfSSL | 0:9d17e4342598 | 423 | } else { |
wolfSSL | 0:9d17e4342598 | 424 | for(i=0; i<DES_IVLEN; i++) |
wolfSSL | 0:9d17e4342598 | 425 | des3->iv[i] = 0x0 ; |
wolfSSL | 0:9d17e4342598 | 426 | } |
wolfSSL | 0:9d17e4342598 | 427 | |
wolfSSL | 0:9d17e4342598 | 428 | return 0; |
wolfSSL | 0:9d17e4342598 | 429 | } |
wolfSSL | 0:9d17e4342598 | 430 | |
wolfSSL | 0:9d17e4342598 | 431 | #elif defined FREESCALE_MMCAU |
wolfSSL | 0:9d17e4342598 | 432 | /* |
wolfSSL | 0:9d17e4342598 | 433 | * Freescale mmCAU hardware DES/3DES support through the CAU/mmCAU library. |
wolfSSL | 0:9d17e4342598 | 434 | * Documentation located in ColdFire/ColdFire+ CAU and Kinetis mmCAU |
wolfSSL | 0:9d17e4342598 | 435 | * Software Library User Guide (See note in README). |
wolfSSL | 0:9d17e4342598 | 436 | */ |
wolfSSL | 0:9d17e4342598 | 437 | #include "cau_api.h" |
wolfSSL | 0:9d17e4342598 | 438 | |
wolfSSL | 0:9d17e4342598 | 439 | const unsigned char parityLookup[128] = |
wolfSSL | 0:9d17e4342598 | 440 | { |
wolfSSL | 0:9d17e4342598 | 441 | 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0, |
wolfSSL | 0:9d17e4342598 | 442 | 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1, |
wolfSSL | 0:9d17e4342598 | 443 | 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1, |
wolfSSL | 0:9d17e4342598 | 444 | 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0 |
wolfSSL | 0:9d17e4342598 | 445 | }; |
wolfSSL | 0:9d17e4342598 | 446 | |
wolfSSL | 0:9d17e4342598 | 447 | int Des_SetKey(Des* des, const byte* key, const byte* iv, int dir) |
wolfSSL | 0:9d17e4342598 | 448 | { |
wolfSSL | 0:9d17e4342598 | 449 | int i = 0; |
wolfSSL | 0:9d17e4342598 | 450 | byte* dkey = (byte*)des->key; |
wolfSSL | 0:9d17e4342598 | 451 | |
wolfSSL | 0:9d17e4342598 | 452 | XMEMCPY(dkey, key, 8); |
wolfSSL | 0:9d17e4342598 | 453 | |
wolfSSL | 0:9d17e4342598 | 454 | Des_SetIV(des, iv); |
wolfSSL | 0:9d17e4342598 | 455 | |
wolfSSL | 0:9d17e4342598 | 456 | /* fix key parity, if needed */ |
wolfSSL | 0:9d17e4342598 | 457 | for (i = 0; i < 8; i++) { |
wolfSSL | 0:9d17e4342598 | 458 | dkey[i] = ((dkey[i] & 0xFE) | parityLookup[dkey[i] >> 1]); |
wolfSSL | 0:9d17e4342598 | 459 | } |
wolfSSL | 0:9d17e4342598 | 460 | |
wolfSSL | 0:9d17e4342598 | 461 | return 0; |
wolfSSL | 0:9d17e4342598 | 462 | } |
wolfSSL | 0:9d17e4342598 | 463 | |
wolfSSL | 0:9d17e4342598 | 464 | int Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir) |
wolfSSL | 0:9d17e4342598 | 465 | { |
wolfSSL | 0:9d17e4342598 | 466 | int i = 0, ret = 0; |
wolfSSL | 0:9d17e4342598 | 467 | byte* dkey1 = (byte*)des->key[0]; |
wolfSSL | 0:9d17e4342598 | 468 | byte* dkey2 = (byte*)des->key[1]; |
wolfSSL | 0:9d17e4342598 | 469 | byte* dkey3 = (byte*)des->key[2]; |
wolfSSL | 0:9d17e4342598 | 470 | |
wolfSSL | 0:9d17e4342598 | 471 | XMEMCPY(dkey1, key, 8); /* set key 1 */ |
wolfSSL | 0:9d17e4342598 | 472 | XMEMCPY(dkey2, key + 8, 8); /* set key 2 */ |
wolfSSL | 0:9d17e4342598 | 473 | XMEMCPY(dkey3, key + 16, 8); /* set key 3 */ |
wolfSSL | 0:9d17e4342598 | 474 | |
wolfSSL | 0:9d17e4342598 | 475 | ret = Des3_SetIV(des, iv); |
wolfSSL | 0:9d17e4342598 | 476 | if (ret != 0) |
wolfSSL | 0:9d17e4342598 | 477 | return ret; |
wolfSSL | 0:9d17e4342598 | 478 | |
wolfSSL | 0:9d17e4342598 | 479 | /* fix key parity if needed */ |
wolfSSL | 0:9d17e4342598 | 480 | for (i = 0; i < 8; i++) |
wolfSSL | 0:9d17e4342598 | 481 | dkey1[i] = ((dkey1[i] & 0xFE) | parityLookup[dkey1[i] >> 1]); |
wolfSSL | 0:9d17e4342598 | 482 | |
wolfSSL | 0:9d17e4342598 | 483 | for (i = 0; i < 8; i++) |
wolfSSL | 0:9d17e4342598 | 484 | dkey2[i] = ((dkey2[i] & 0xFE) | parityLookup[dkey2[i] >> 1]); |
wolfSSL | 0:9d17e4342598 | 485 | |
wolfSSL | 0:9d17e4342598 | 486 | for (i = 0; i < 8; i++) |
wolfSSL | 0:9d17e4342598 | 487 | dkey3[i] = ((dkey3[i] & 0xFE) | parityLookup[dkey3[i] >> 1]); |
wolfSSL | 0:9d17e4342598 | 488 | |
wolfSSL | 0:9d17e4342598 | 489 | return ret; |
wolfSSL | 0:9d17e4342598 | 490 | } |
wolfSSL | 0:9d17e4342598 | 491 | |
wolfSSL | 0:9d17e4342598 | 492 | void Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz) |
wolfSSL | 0:9d17e4342598 | 493 | { |
wolfSSL | 0:9d17e4342598 | 494 | int i; |
wolfSSL | 0:9d17e4342598 | 495 | int offset = 0; |
wolfSSL | 0:9d17e4342598 | 496 | int len = sz; |
wolfSSL | 0:9d17e4342598 | 497 | byte *iv; |
wolfSSL | 0:9d17e4342598 | 498 | byte temp_block[DES_BLOCK_SIZE]; |
wolfSSL | 0:9d17e4342598 | 499 | |
wolfSSL | 0:9d17e4342598 | 500 | iv = (byte*)des->reg; |
wolfSSL | 0:9d17e4342598 | 501 | |
wolfSSL | 0:9d17e4342598 | 502 | while (len > 0) |
wolfSSL | 0:9d17e4342598 | 503 | { |
wolfSSL | 0:9d17e4342598 | 504 | XMEMCPY(temp_block, in + offset, DES_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 505 | |
wolfSSL | 0:9d17e4342598 | 506 | /* XOR block with IV for CBC */ |
wolfSSL | 0:9d17e4342598 | 507 | for (i = 0; i < DES_BLOCK_SIZE; i++) |
wolfSSL | 0:9d17e4342598 | 508 | temp_block[i] ^= iv[i]; |
wolfSSL | 0:9d17e4342598 | 509 | |
wolfSSL | 0:9d17e4342598 | 510 | cau_des_encrypt(temp_block, (byte*)des->key, out + offset); |
wolfSSL | 0:9d17e4342598 | 511 | |
wolfSSL | 0:9d17e4342598 | 512 | len -= DES_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 513 | offset += DES_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 514 | |
wolfSSL | 0:9d17e4342598 | 515 | /* store IV for next block */ |
wolfSSL | 0:9d17e4342598 | 516 | XMEMCPY(iv, out + offset - DES_BLOCK_SIZE, DES_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 517 | } |
wolfSSL | 0:9d17e4342598 | 518 | |
wolfSSL | 0:9d17e4342598 | 519 | return; |
wolfSSL | 0:9d17e4342598 | 520 | } |
wolfSSL | 0:9d17e4342598 | 521 | |
wolfSSL | 0:9d17e4342598 | 522 | void Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz) |
wolfSSL | 0:9d17e4342598 | 523 | { |
wolfSSL | 0:9d17e4342598 | 524 | int i; |
wolfSSL | 0:9d17e4342598 | 525 | int offset = 0; |
wolfSSL | 0:9d17e4342598 | 526 | int len = sz; |
wolfSSL | 0:9d17e4342598 | 527 | byte* iv; |
wolfSSL | 0:9d17e4342598 | 528 | byte temp_block[DES_BLOCK_SIZE]; |
wolfSSL | 0:9d17e4342598 | 529 | |
wolfSSL | 0:9d17e4342598 | 530 | iv = (byte*)des->reg; |
wolfSSL | 0:9d17e4342598 | 531 | |
wolfSSL | 0:9d17e4342598 | 532 | while (len > 0) |
wolfSSL | 0:9d17e4342598 | 533 | { |
wolfSSL | 0:9d17e4342598 | 534 | XMEMCPY(temp_block, in + offset, DES_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 535 | |
wolfSSL | 0:9d17e4342598 | 536 | cau_des_decrypt(in + offset, (byte*)des->key, out + offset); |
wolfSSL | 0:9d17e4342598 | 537 | |
wolfSSL | 0:9d17e4342598 | 538 | /* XOR block with IV for CBC */ |
wolfSSL | 0:9d17e4342598 | 539 | for (i = 0; i < DES_BLOCK_SIZE; i++) |
wolfSSL | 0:9d17e4342598 | 540 | (out + offset)[i] ^= iv[i]; |
wolfSSL | 0:9d17e4342598 | 541 | |
wolfSSL | 0:9d17e4342598 | 542 | /* store IV for next block */ |
wolfSSL | 0:9d17e4342598 | 543 | XMEMCPY(iv, temp_block, DES_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 544 | |
wolfSSL | 0:9d17e4342598 | 545 | len -= DES_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 546 | offset += DES_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 547 | } |
wolfSSL | 0:9d17e4342598 | 548 | |
wolfSSL | 0:9d17e4342598 | 549 | return; |
wolfSSL | 0:9d17e4342598 | 550 | } |
wolfSSL | 0:9d17e4342598 | 551 | |
wolfSSL | 0:9d17e4342598 | 552 | int Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz) |
wolfSSL | 0:9d17e4342598 | 553 | { |
wolfSSL | 0:9d17e4342598 | 554 | int i; |
wolfSSL | 0:9d17e4342598 | 555 | int offset = 0; |
wolfSSL | 0:9d17e4342598 | 556 | int len = sz; |
wolfSSL | 0:9d17e4342598 | 557 | |
wolfSSL | 0:9d17e4342598 | 558 | byte *iv; |
wolfSSL | 0:9d17e4342598 | 559 | byte temp_block[DES_BLOCK_SIZE]; |
wolfSSL | 0:9d17e4342598 | 560 | |
wolfSSL | 0:9d17e4342598 | 561 | iv = (byte*)des->reg; |
wolfSSL | 0:9d17e4342598 | 562 | |
wolfSSL | 0:9d17e4342598 | 563 | while (len > 0) |
wolfSSL | 0:9d17e4342598 | 564 | { |
wolfSSL | 0:9d17e4342598 | 565 | XMEMCPY(temp_block, in + offset, DES_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 566 | |
wolfSSL | 0:9d17e4342598 | 567 | /* XOR block with IV for CBC */ |
wolfSSL | 0:9d17e4342598 | 568 | for (i = 0; i < DES_BLOCK_SIZE; i++) |
wolfSSL | 0:9d17e4342598 | 569 | temp_block[i] ^= iv[i]; |
wolfSSL | 0:9d17e4342598 | 570 | |
wolfSSL | 0:9d17e4342598 | 571 | cau_des_encrypt(temp_block , (byte*)des->key[0], out + offset); |
wolfSSL | 0:9d17e4342598 | 572 | cau_des_decrypt(out + offset, (byte*)des->key[1], out + offset); |
wolfSSL | 0:9d17e4342598 | 573 | cau_des_encrypt(out + offset, (byte*)des->key[2], out + offset); |
wolfSSL | 0:9d17e4342598 | 574 | |
wolfSSL | 0:9d17e4342598 | 575 | len -= DES_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 576 | offset += DES_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 577 | |
wolfSSL | 0:9d17e4342598 | 578 | /* store IV for next block */ |
wolfSSL | 0:9d17e4342598 | 579 | XMEMCPY(iv, out + offset - DES_BLOCK_SIZE, DES_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 580 | } |
wolfSSL | 0:9d17e4342598 | 581 | |
wolfSSL | 0:9d17e4342598 | 582 | return 0; |
wolfSSL | 0:9d17e4342598 | 583 | } |
wolfSSL | 0:9d17e4342598 | 584 | |
wolfSSL | 0:9d17e4342598 | 585 | int Des3_CbcDecrypt(Des3* des, byte* out, const byte* in, word32 sz) |
wolfSSL | 0:9d17e4342598 | 586 | { |
wolfSSL | 0:9d17e4342598 | 587 | int i; |
wolfSSL | 0:9d17e4342598 | 588 | int offset = 0; |
wolfSSL | 0:9d17e4342598 | 589 | int len = sz; |
wolfSSL | 0:9d17e4342598 | 590 | |
wolfSSL | 0:9d17e4342598 | 591 | byte* iv; |
wolfSSL | 0:9d17e4342598 | 592 | byte temp_block[DES_BLOCK_SIZE]; |
wolfSSL | 0:9d17e4342598 | 593 | |
wolfSSL | 0:9d17e4342598 | 594 | iv = (byte*)des->reg; |
wolfSSL | 0:9d17e4342598 | 595 | |
wolfSSL | 0:9d17e4342598 | 596 | while (len > 0) |
wolfSSL | 0:9d17e4342598 | 597 | { |
wolfSSL | 0:9d17e4342598 | 598 | XMEMCPY(temp_block, in + offset, DES_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 599 | |
wolfSSL | 0:9d17e4342598 | 600 | cau_des_decrypt(in + offset , (byte*)des->key[2], out + offset); |
wolfSSL | 0:9d17e4342598 | 601 | cau_des_encrypt(out + offset, (byte*)des->key[1], out + offset); |
wolfSSL | 0:9d17e4342598 | 602 | cau_des_decrypt(out + offset, (byte*)des->key[0], out + offset); |
wolfSSL | 0:9d17e4342598 | 603 | |
wolfSSL | 0:9d17e4342598 | 604 | /* XOR block with IV for CBC */ |
wolfSSL | 0:9d17e4342598 | 605 | for (i = 0; i < DES_BLOCK_SIZE; i++) |
wolfSSL | 0:9d17e4342598 | 606 | (out + offset)[i] ^= iv[i]; |
wolfSSL | 0:9d17e4342598 | 607 | |
wolfSSL | 0:9d17e4342598 | 608 | /* store IV for next block */ |
wolfSSL | 0:9d17e4342598 | 609 | XMEMCPY(iv, temp_block, DES_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 610 | |
wolfSSL | 0:9d17e4342598 | 611 | len -= DES_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 612 | offset += DES_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 613 | } |
wolfSSL | 0:9d17e4342598 | 614 | |
wolfSSL | 0:9d17e4342598 | 615 | return 0; |
wolfSSL | 0:9d17e4342598 | 616 | } |
wolfSSL | 0:9d17e4342598 | 617 | |
wolfSSL | 0:9d17e4342598 | 618 | |
wolfSSL | 0:9d17e4342598 | 619 | #elif defined(CYASSL_PIC32MZ_CRYPT) |
wolfSSL | 0:9d17e4342598 | 620 | |
wolfSSL | 0:9d17e4342598 | 621 | #include "../../cyassl/ctaocrypt/port/pic32/pic32mz-crypt.h" |
wolfSSL | 0:9d17e4342598 | 622 | |
wolfSSL | 0:9d17e4342598 | 623 | void Des_SetIV(Des* des, const byte* iv); |
wolfSSL | 0:9d17e4342598 | 624 | int Des3_SetIV(Des3* des, const byte* iv); |
wolfSSL | 0:9d17e4342598 | 625 | |
wolfSSL | 0:9d17e4342598 | 626 | int Des_SetKey(Des* des, const byte* key, const byte* iv, int dir) |
wolfSSL | 0:9d17e4342598 | 627 | { |
wolfSSL | 0:9d17e4342598 | 628 | word32 *dkey = des->key ; |
wolfSSL | 0:9d17e4342598 | 629 | word32 *dreg = des->reg ; |
wolfSSL | 0:9d17e4342598 | 630 | |
wolfSSL | 0:9d17e4342598 | 631 | XMEMCPY((byte *)dkey, (byte *)key, 8); |
wolfSSL | 0:9d17e4342598 | 632 | ByteReverseWords(dkey, dkey, 8); |
wolfSSL | 0:9d17e4342598 | 633 | XMEMCPY((byte *)dreg, (byte *)iv, 8); |
wolfSSL | 0:9d17e4342598 | 634 | ByteReverseWords(dreg, dreg, 8); |
wolfSSL | 0:9d17e4342598 | 635 | |
wolfSSL | 0:9d17e4342598 | 636 | return 0; |
wolfSSL | 0:9d17e4342598 | 637 | } |
wolfSSL | 0:9d17e4342598 | 638 | |
wolfSSL | 0:9d17e4342598 | 639 | int Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir) |
wolfSSL | 0:9d17e4342598 | 640 | { |
wolfSSL | 0:9d17e4342598 | 641 | word32 *dkey1 = des->key[0]; |
wolfSSL | 0:9d17e4342598 | 642 | word32 *dreg = des->reg ; |
wolfSSL | 0:9d17e4342598 | 643 | |
wolfSSL | 0:9d17e4342598 | 644 | XMEMCPY(dkey1, key, 24); |
wolfSSL | 0:9d17e4342598 | 645 | ByteReverseWords(dkey1, dkey1, 24); |
wolfSSL | 0:9d17e4342598 | 646 | XMEMCPY(dreg, iv, 8); |
wolfSSL | 0:9d17e4342598 | 647 | ByteReverseWords(dreg, dreg, 8) ; |
wolfSSL | 0:9d17e4342598 | 648 | |
wolfSSL | 0:9d17e4342598 | 649 | return 0; |
wolfSSL | 0:9d17e4342598 | 650 | } |
wolfSSL | 0:9d17e4342598 | 651 | |
wolfSSL | 0:9d17e4342598 | 652 | void DesCrypt(word32 *key, word32 *iv, byte* out, const byte* in, word32 sz, |
wolfSSL | 0:9d17e4342598 | 653 | int dir, int algo, int cryptoalgo) |
wolfSSL | 0:9d17e4342598 | 654 | { |
wolfSSL | 0:9d17e4342598 | 655 | securityAssociation *sa_p ; |
wolfSSL | 0:9d17e4342598 | 656 | bufferDescriptor *bd_p ; |
wolfSSL | 0:9d17e4342598 | 657 | const byte *in_p, *in_l ; |
wolfSSL | 0:9d17e4342598 | 658 | byte *out_p, *out_l ; |
wolfSSL | 0:9d17e4342598 | 659 | volatile securityAssociation sa __attribute__((aligned (8))); |
wolfSSL | 0:9d17e4342598 | 660 | volatile bufferDescriptor bd __attribute__((aligned (8))); |
wolfSSL | 0:9d17e4342598 | 661 | volatile int k ; |
wolfSSL | 0:9d17e4342598 | 662 | |
wolfSSL | 0:9d17e4342598 | 663 | /* get uncached address */ |
wolfSSL | 0:9d17e4342598 | 664 | |
wolfSSL | 0:9d17e4342598 | 665 | in_l = in; |
wolfSSL | 0:9d17e4342598 | 666 | out_l = out ; |
wolfSSL | 0:9d17e4342598 | 667 | sa_p = KVA0_TO_KVA1(&sa) ; |
wolfSSL | 0:9d17e4342598 | 668 | bd_p = KVA0_TO_KVA1(&bd) ; |
wolfSSL | 0:9d17e4342598 | 669 | in_p = KVA0_TO_KVA1(in_l) ; |
wolfSSL | 0:9d17e4342598 | 670 | out_p= KVA0_TO_KVA1(out_l); |
wolfSSL | 0:9d17e4342598 | 671 | |
wolfSSL | 0:9d17e4342598 | 672 | if(PIC32MZ_IF_RAM(in_p)) |
wolfSSL | 0:9d17e4342598 | 673 | XMEMCPY((void *)in_p, (void *)in, sz); |
wolfSSL | 0:9d17e4342598 | 674 | XMEMSET((void *)out_p, 0, sz); |
wolfSSL | 0:9d17e4342598 | 675 | |
wolfSSL | 0:9d17e4342598 | 676 | /* Set up the Security Association */ |
wolfSSL | 0:9d17e4342598 | 677 | XMEMSET((byte *)KVA0_TO_KVA1(&sa), 0, sizeof(sa)); |
wolfSSL | 0:9d17e4342598 | 678 | sa_p->SA_CTRL.ALGO = algo ; |
wolfSSL | 0:9d17e4342598 | 679 | sa_p->SA_CTRL.LNC = 1; |
wolfSSL | 0:9d17e4342598 | 680 | sa_p->SA_CTRL.LOADIV = 1; |
wolfSSL | 0:9d17e4342598 | 681 | sa_p->SA_CTRL.FB = 1; |
wolfSSL | 0:9d17e4342598 | 682 | sa_p->SA_CTRL.ENCTYPE = dir ; /* Encryption/Decryption */ |
wolfSSL | 0:9d17e4342598 | 683 | sa_p->SA_CTRL.CRYPTOALGO = cryptoalgo; |
wolfSSL | 0:9d17e4342598 | 684 | sa_p->SA_CTRL.KEYSIZE = 1 ; /* KEY is 192 bits */ |
wolfSSL | 0:9d17e4342598 | 685 | XMEMCPY((byte *)KVA0_TO_KVA1(&sa.SA_ENCKEY[algo==PIC32_ALGO_TDES ? 2 : 6]), |
wolfSSL | 0:9d17e4342598 | 686 | (byte *)key, algo==PIC32_ALGO_TDES ? 24 : 8); |
wolfSSL | 0:9d17e4342598 | 687 | XMEMCPY((byte *)KVA0_TO_KVA1(&sa.SA_ENCIV[2]), (byte *)iv, 8); |
wolfSSL | 0:9d17e4342598 | 688 | |
wolfSSL | 0:9d17e4342598 | 689 | XMEMSET((byte *)KVA0_TO_KVA1(&bd), 0, sizeof(bd)); |
wolfSSL | 0:9d17e4342598 | 690 | /* Set up the Buffer Descriptor */ |
wolfSSL | 0:9d17e4342598 | 691 | bd_p->BD_CTRL.BUFLEN = sz; |
wolfSSL | 0:9d17e4342598 | 692 | bd_p->BD_CTRL.LIFM = 1; |
wolfSSL | 0:9d17e4342598 | 693 | bd_p->BD_CTRL.SA_FETCH_EN = 1; |
wolfSSL | 0:9d17e4342598 | 694 | bd_p->BD_CTRL.LAST_BD = 1; |
wolfSSL | 0:9d17e4342598 | 695 | bd_p->BD_CTRL.DESC_EN = 1; |
wolfSSL | 0:9d17e4342598 | 696 | |
wolfSSL | 0:9d17e4342598 | 697 | bd_p->SA_ADDR = (unsigned int)KVA_TO_PA(&sa) ; // (unsigned int)sa_p ; |
wolfSSL | 0:9d17e4342598 | 698 | bd_p->SRCADDR = (unsigned int)KVA_TO_PA(in) ; // (unsigned int)in_p ; |
wolfSSL | 0:9d17e4342598 | 699 | bd_p->DSTADDR = (unsigned int)KVA_TO_PA(out); // (unsigned int)out_p ; |
wolfSSL | 0:9d17e4342598 | 700 | bd_p->NXTPTR = (unsigned int)KVA_TO_PA(&bd); |
wolfSSL | 0:9d17e4342598 | 701 | bd_p->MSGLEN = sz ; |
wolfSSL | 0:9d17e4342598 | 702 | |
wolfSSL | 0:9d17e4342598 | 703 | /* Fire in the hole! */ |
wolfSSL | 0:9d17e4342598 | 704 | CECON = 1 << 6; |
wolfSSL | 0:9d17e4342598 | 705 | while (CECON); |
wolfSSL | 0:9d17e4342598 | 706 | |
wolfSSL | 0:9d17e4342598 | 707 | /* Run the engine */ |
wolfSSL | 0:9d17e4342598 | 708 | CEBDPADDR = (unsigned int)KVA_TO_PA(&bd) ; // (unsigned int)bd_p ; |
wolfSSL | 0:9d17e4342598 | 709 | CEINTEN = 0x07; |
wolfSSL | 0:9d17e4342598 | 710 | CECON = 0x27; |
wolfSSL | 0:9d17e4342598 | 711 | |
wolfSSL | 0:9d17e4342598 | 712 | WAIT_ENGINE ; |
wolfSSL | 0:9d17e4342598 | 713 | |
wolfSSL | 0:9d17e4342598 | 714 | if((cryptoalgo == PIC32_CRYPTOALGO_CBC) || |
wolfSSL | 0:9d17e4342598 | 715 | (cryptoalgo == PIC32_CRYPTOALGO_TCBC)|| |
wolfSSL | 0:9d17e4342598 | 716 | (cryptoalgo == PIC32_CRYPTOALGO_RCBC)) { |
wolfSSL | 0:9d17e4342598 | 717 | /* set iv for the next call */ |
wolfSSL | 0:9d17e4342598 | 718 | if(dir == PIC32_ENCRYPTION) { |
wolfSSL | 0:9d17e4342598 | 719 | XMEMCPY((void *)iv, (void*)&(out_p[sz-DES_IVLEN]), DES_IVLEN) ; |
wolfSSL | 0:9d17e4342598 | 720 | } else { |
wolfSSL | 0:9d17e4342598 | 721 | ByteReverseWords((word32*)iv, (word32 *)&(in_p[sz-DES_IVLEN]), DES_IVLEN); |
wolfSSL | 0:9d17e4342598 | 722 | } |
wolfSSL | 0:9d17e4342598 | 723 | |
wolfSSL | 0:9d17e4342598 | 724 | } |
wolfSSL | 0:9d17e4342598 | 725 | |
wolfSSL | 0:9d17e4342598 | 726 | ByteReverseWords((word32*)out, (word32 *)KVA0_TO_KVA1(out), sz); |
wolfSSL | 0:9d17e4342598 | 727 | } |
wolfSSL | 0:9d17e4342598 | 728 | |
wolfSSL | 0:9d17e4342598 | 729 | void Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz) |
wolfSSL | 0:9d17e4342598 | 730 | { |
wolfSSL | 0:9d17e4342598 | 731 | DesCrypt(des->key, des->reg, out, in, sz, |
wolfSSL | 0:9d17e4342598 | 732 | PIC32_ENCRYPTION, PIC32_ALGO_DES, PIC32_CRYPTOALGO_CBC ); |
wolfSSL | 0:9d17e4342598 | 733 | } |
wolfSSL | 0:9d17e4342598 | 734 | |
wolfSSL | 0:9d17e4342598 | 735 | void Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz) |
wolfSSL | 0:9d17e4342598 | 736 | { |
wolfSSL | 0:9d17e4342598 | 737 | DesCrypt(des->key, des->reg, out, in, sz, |
wolfSSL | 0:9d17e4342598 | 738 | PIC32_DECRYPTION, PIC32_ALGO_DES, PIC32_CRYPTOALGO_CBC); |
wolfSSL | 0:9d17e4342598 | 739 | } |
wolfSSL | 0:9d17e4342598 | 740 | |
wolfSSL | 0:9d17e4342598 | 741 | int Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz) |
wolfSSL | 0:9d17e4342598 | 742 | { |
wolfSSL | 0:9d17e4342598 | 743 | DesCrypt(des->key[0], des->reg, out, in, sz, |
wolfSSL | 0:9d17e4342598 | 744 | PIC32_ENCRYPTION, PIC32_ALGO_TDES, PIC32_CRYPTOALGO_TCBC); |
wolfSSL | 0:9d17e4342598 | 745 | return 0; |
wolfSSL | 0:9d17e4342598 | 746 | } |
wolfSSL | 0:9d17e4342598 | 747 | |
wolfSSL | 0:9d17e4342598 | 748 | int Des3_CbcDecrypt(Des3* des, byte* out, const byte* in, word32 sz) |
wolfSSL | 0:9d17e4342598 | 749 | { |
wolfSSL | 0:9d17e4342598 | 750 | DesCrypt(des->key[0], des->reg, out, in, sz, |
wolfSSL | 0:9d17e4342598 | 751 | PIC32_DECRYPTION, PIC32_ALGO_TDES, PIC32_CRYPTOALGO_TCBC); |
wolfSSL | 0:9d17e4342598 | 752 | return 0; |
wolfSSL | 0:9d17e4342598 | 753 | } |
wolfSSL | 0:9d17e4342598 | 754 | |
wolfSSL | 0:9d17e4342598 | 755 | #else /* CTaoCrypt software implementation */ |
wolfSSL | 0:9d17e4342598 | 756 | |
wolfSSL | 0:9d17e4342598 | 757 | /* permuted choice table (key) */ |
wolfSSL | 0:9d17e4342598 | 758 | static const byte pc1[] = { |
wolfSSL | 0:9d17e4342598 | 759 | 57, 49, 41, 33, 25, 17, 9, |
wolfSSL | 0:9d17e4342598 | 760 | 1, 58, 50, 42, 34, 26, 18, |
wolfSSL | 0:9d17e4342598 | 761 | 10, 2, 59, 51, 43, 35, 27, |
wolfSSL | 0:9d17e4342598 | 762 | 19, 11, 3, 60, 52, 44, 36, |
wolfSSL | 0:9d17e4342598 | 763 | |
wolfSSL | 0:9d17e4342598 | 764 | 63, 55, 47, 39, 31, 23, 15, |
wolfSSL | 0:9d17e4342598 | 765 | 7, 62, 54, 46, 38, 30, 22, |
wolfSSL | 0:9d17e4342598 | 766 | 14, 6, 61, 53, 45, 37, 29, |
wolfSSL | 0:9d17e4342598 | 767 | 21, 13, 5, 28, 20, 12, 4 |
wolfSSL | 0:9d17e4342598 | 768 | }; |
wolfSSL | 0:9d17e4342598 | 769 | |
wolfSSL | 0:9d17e4342598 | 770 | /* number left rotations of pc1 */ |
wolfSSL | 0:9d17e4342598 | 771 | static const byte totrot[] = { |
wolfSSL | 0:9d17e4342598 | 772 | 1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28 |
wolfSSL | 0:9d17e4342598 | 773 | }; |
wolfSSL | 0:9d17e4342598 | 774 | |
wolfSSL | 0:9d17e4342598 | 775 | /* permuted choice key (table) */ |
wolfSSL | 0:9d17e4342598 | 776 | static const byte pc2[] = { |
wolfSSL | 0:9d17e4342598 | 777 | 14, 17, 11, 24, 1, 5, |
wolfSSL | 0:9d17e4342598 | 778 | 3, 28, 15, 6, 21, 10, |
wolfSSL | 0:9d17e4342598 | 779 | 23, 19, 12, 4, 26, 8, |
wolfSSL | 0:9d17e4342598 | 780 | 16, 7, 27, 20, 13, 2, |
wolfSSL | 0:9d17e4342598 | 781 | 41, 52, 31, 37, 47, 55, |
wolfSSL | 0:9d17e4342598 | 782 | 30, 40, 51, 45, 33, 48, |
wolfSSL | 0:9d17e4342598 | 783 | 44, 49, 39, 56, 34, 53, |
wolfSSL | 0:9d17e4342598 | 784 | 46, 42, 50, 36, 29, 32 |
wolfSSL | 0:9d17e4342598 | 785 | }; |
wolfSSL | 0:9d17e4342598 | 786 | |
wolfSSL | 0:9d17e4342598 | 787 | /* End of DES-defined tables */ |
wolfSSL | 0:9d17e4342598 | 788 | |
wolfSSL | 0:9d17e4342598 | 789 | /* bit 0 is left-most in byte */ |
wolfSSL | 0:9d17e4342598 | 790 | static const int bytebit[] = { |
wolfSSL | 0:9d17e4342598 | 791 | 0200,0100,040,020,010,04,02,01 |
wolfSSL | 0:9d17e4342598 | 792 | }; |
wolfSSL | 0:9d17e4342598 | 793 | |
wolfSSL | 0:9d17e4342598 | 794 | static const word32 Spbox[8][64] = { |
wolfSSL | 0:9d17e4342598 | 795 | { |
wolfSSL | 0:9d17e4342598 | 796 | 0x01010400,0x00000000,0x00010000,0x01010404, |
wolfSSL | 0:9d17e4342598 | 797 | 0x01010004,0x00010404,0x00000004,0x00010000, |
wolfSSL | 0:9d17e4342598 | 798 | 0x00000400,0x01010400,0x01010404,0x00000400, |
wolfSSL | 0:9d17e4342598 | 799 | 0x01000404,0x01010004,0x01000000,0x00000004, |
wolfSSL | 0:9d17e4342598 | 800 | 0x00000404,0x01000400,0x01000400,0x00010400, |
wolfSSL | 0:9d17e4342598 | 801 | 0x00010400,0x01010000,0x01010000,0x01000404, |
wolfSSL | 0:9d17e4342598 | 802 | 0x00010004,0x01000004,0x01000004,0x00010004, |
wolfSSL | 0:9d17e4342598 | 803 | 0x00000000,0x00000404,0x00010404,0x01000000, |
wolfSSL | 0:9d17e4342598 | 804 | 0x00010000,0x01010404,0x00000004,0x01010000, |
wolfSSL | 0:9d17e4342598 | 805 | 0x01010400,0x01000000,0x01000000,0x00000400, |
wolfSSL | 0:9d17e4342598 | 806 | 0x01010004,0x00010000,0x00010400,0x01000004, |
wolfSSL | 0:9d17e4342598 | 807 | 0x00000400,0x00000004,0x01000404,0x00010404, |
wolfSSL | 0:9d17e4342598 | 808 | 0x01010404,0x00010004,0x01010000,0x01000404, |
wolfSSL | 0:9d17e4342598 | 809 | 0x01000004,0x00000404,0x00010404,0x01010400, |
wolfSSL | 0:9d17e4342598 | 810 | 0x00000404,0x01000400,0x01000400,0x00000000, |
wolfSSL | 0:9d17e4342598 | 811 | 0x00010004,0x00010400,0x00000000,0x01010004}, |
wolfSSL | 0:9d17e4342598 | 812 | { |
wolfSSL | 0:9d17e4342598 | 813 | 0x80108020,0x80008000,0x00008000,0x00108020, |
wolfSSL | 0:9d17e4342598 | 814 | 0x00100000,0x00000020,0x80100020,0x80008020, |
wolfSSL | 0:9d17e4342598 | 815 | 0x80000020,0x80108020,0x80108000,0x80000000, |
wolfSSL | 0:9d17e4342598 | 816 | 0x80008000,0x00100000,0x00000020,0x80100020, |
wolfSSL | 0:9d17e4342598 | 817 | 0x00108000,0x00100020,0x80008020,0x00000000, |
wolfSSL | 0:9d17e4342598 | 818 | 0x80000000,0x00008000,0x00108020,0x80100000, |
wolfSSL | 0:9d17e4342598 | 819 | 0x00100020,0x80000020,0x00000000,0x00108000, |
wolfSSL | 0:9d17e4342598 | 820 | 0x00008020,0x80108000,0x80100000,0x00008020, |
wolfSSL | 0:9d17e4342598 | 821 | 0x00000000,0x00108020,0x80100020,0x00100000, |
wolfSSL | 0:9d17e4342598 | 822 | 0x80008020,0x80100000,0x80108000,0x00008000, |
wolfSSL | 0:9d17e4342598 | 823 | 0x80100000,0x80008000,0x00000020,0x80108020, |
wolfSSL | 0:9d17e4342598 | 824 | 0x00108020,0x00000020,0x00008000,0x80000000, |
wolfSSL | 0:9d17e4342598 | 825 | 0x00008020,0x80108000,0x00100000,0x80000020, |
wolfSSL | 0:9d17e4342598 | 826 | 0x00100020,0x80008020,0x80000020,0x00100020, |
wolfSSL | 0:9d17e4342598 | 827 | 0x00108000,0x00000000,0x80008000,0x00008020, |
wolfSSL | 0:9d17e4342598 | 828 | 0x80000000,0x80100020,0x80108020,0x00108000}, |
wolfSSL | 0:9d17e4342598 | 829 | { |
wolfSSL | 0:9d17e4342598 | 830 | 0x00000208,0x08020200,0x00000000,0x08020008, |
wolfSSL | 0:9d17e4342598 | 831 | 0x08000200,0x00000000,0x00020208,0x08000200, |
wolfSSL | 0:9d17e4342598 | 832 | 0x00020008,0x08000008,0x08000008,0x00020000, |
wolfSSL | 0:9d17e4342598 | 833 | 0x08020208,0x00020008,0x08020000,0x00000208, |
wolfSSL | 0:9d17e4342598 | 834 | 0x08000000,0x00000008,0x08020200,0x00000200, |
wolfSSL | 0:9d17e4342598 | 835 | 0x00020200,0x08020000,0x08020008,0x00020208, |
wolfSSL | 0:9d17e4342598 | 836 | 0x08000208,0x00020200,0x00020000,0x08000208, |
wolfSSL | 0:9d17e4342598 | 837 | 0x00000008,0x08020208,0x00000200,0x08000000, |
wolfSSL | 0:9d17e4342598 | 838 | 0x08020200,0x08000000,0x00020008,0x00000208, |
wolfSSL | 0:9d17e4342598 | 839 | 0x00020000,0x08020200,0x08000200,0x00000000, |
wolfSSL | 0:9d17e4342598 | 840 | 0x00000200,0x00020008,0x08020208,0x08000200, |
wolfSSL | 0:9d17e4342598 | 841 | 0x08000008,0x00000200,0x00000000,0x08020008, |
wolfSSL | 0:9d17e4342598 | 842 | 0x08000208,0x00020000,0x08000000,0x08020208, |
wolfSSL | 0:9d17e4342598 | 843 | 0x00000008,0x00020208,0x00020200,0x08000008, |
wolfSSL | 0:9d17e4342598 | 844 | 0x08020000,0x08000208,0x00000208,0x08020000, |
wolfSSL | 0:9d17e4342598 | 845 | 0x00020208,0x00000008,0x08020008,0x00020200}, |
wolfSSL | 0:9d17e4342598 | 846 | { |
wolfSSL | 0:9d17e4342598 | 847 | 0x00802001,0x00002081,0x00002081,0x00000080, |
wolfSSL | 0:9d17e4342598 | 848 | 0x00802080,0x00800081,0x00800001,0x00002001, |
wolfSSL | 0:9d17e4342598 | 849 | 0x00000000,0x00802000,0x00802000,0x00802081, |
wolfSSL | 0:9d17e4342598 | 850 | 0x00000081,0x00000000,0x00800080,0x00800001, |
wolfSSL | 0:9d17e4342598 | 851 | 0x00000001,0x00002000,0x00800000,0x00802001, |
wolfSSL | 0:9d17e4342598 | 852 | 0x00000080,0x00800000,0x00002001,0x00002080, |
wolfSSL | 0:9d17e4342598 | 853 | 0x00800081,0x00000001,0x00002080,0x00800080, |
wolfSSL | 0:9d17e4342598 | 854 | 0x00002000,0x00802080,0x00802081,0x00000081, |
wolfSSL | 0:9d17e4342598 | 855 | 0x00800080,0x00800001,0x00802000,0x00802081, |
wolfSSL | 0:9d17e4342598 | 856 | 0x00000081,0x00000000,0x00000000,0x00802000, |
wolfSSL | 0:9d17e4342598 | 857 | 0x00002080,0x00800080,0x00800081,0x00000001, |
wolfSSL | 0:9d17e4342598 | 858 | 0x00802001,0x00002081,0x00002081,0x00000080, |
wolfSSL | 0:9d17e4342598 | 859 | 0x00802081,0x00000081,0x00000001,0x00002000, |
wolfSSL | 0:9d17e4342598 | 860 | 0x00800001,0x00002001,0x00802080,0x00800081, |
wolfSSL | 0:9d17e4342598 | 861 | 0x00002001,0x00002080,0x00800000,0x00802001, |
wolfSSL | 0:9d17e4342598 | 862 | 0x00000080,0x00800000,0x00002000,0x00802080}, |
wolfSSL | 0:9d17e4342598 | 863 | { |
wolfSSL | 0:9d17e4342598 | 864 | 0x00000100,0x02080100,0x02080000,0x42000100, |
wolfSSL | 0:9d17e4342598 | 865 | 0x00080000,0x00000100,0x40000000,0x02080000, |
wolfSSL | 0:9d17e4342598 | 866 | 0x40080100,0x00080000,0x02000100,0x40080100, |
wolfSSL | 0:9d17e4342598 | 867 | 0x42000100,0x42080000,0x00080100,0x40000000, |
wolfSSL | 0:9d17e4342598 | 868 | 0x02000000,0x40080000,0x40080000,0x00000000, |
wolfSSL | 0:9d17e4342598 | 869 | 0x40000100,0x42080100,0x42080100,0x02000100, |
wolfSSL | 0:9d17e4342598 | 870 | 0x42080000,0x40000100,0x00000000,0x42000000, |
wolfSSL | 0:9d17e4342598 | 871 | 0x02080100,0x02000000,0x42000000,0x00080100, |
wolfSSL | 0:9d17e4342598 | 872 | 0x00080000,0x42000100,0x00000100,0x02000000, |
wolfSSL | 0:9d17e4342598 | 873 | 0x40000000,0x02080000,0x42000100,0x40080100, |
wolfSSL | 0:9d17e4342598 | 874 | 0x02000100,0x40000000,0x42080000,0x02080100, |
wolfSSL | 0:9d17e4342598 | 875 | 0x40080100,0x00000100,0x02000000,0x42080000, |
wolfSSL | 0:9d17e4342598 | 876 | 0x42080100,0x00080100,0x42000000,0x42080100, |
wolfSSL | 0:9d17e4342598 | 877 | 0x02080000,0x00000000,0x40080000,0x42000000, |
wolfSSL | 0:9d17e4342598 | 878 | 0x00080100,0x02000100,0x40000100,0x00080000, |
wolfSSL | 0:9d17e4342598 | 879 | 0x00000000,0x40080000,0x02080100,0x40000100}, |
wolfSSL | 0:9d17e4342598 | 880 | { |
wolfSSL | 0:9d17e4342598 | 881 | 0x20000010,0x20400000,0x00004000,0x20404010, |
wolfSSL | 0:9d17e4342598 | 882 | 0x20400000,0x00000010,0x20404010,0x00400000, |
wolfSSL | 0:9d17e4342598 | 883 | 0x20004000,0x00404010,0x00400000,0x20000010, |
wolfSSL | 0:9d17e4342598 | 884 | 0x00400010,0x20004000,0x20000000,0x00004010, |
wolfSSL | 0:9d17e4342598 | 885 | 0x00000000,0x00400010,0x20004010,0x00004000, |
wolfSSL | 0:9d17e4342598 | 886 | 0x00404000,0x20004010,0x00000010,0x20400010, |
wolfSSL | 0:9d17e4342598 | 887 | 0x20400010,0x00000000,0x00404010,0x20404000, |
wolfSSL | 0:9d17e4342598 | 888 | 0x00004010,0x00404000,0x20404000,0x20000000, |
wolfSSL | 0:9d17e4342598 | 889 | 0x20004000,0x00000010,0x20400010,0x00404000, |
wolfSSL | 0:9d17e4342598 | 890 | 0x20404010,0x00400000,0x00004010,0x20000010, |
wolfSSL | 0:9d17e4342598 | 891 | 0x00400000,0x20004000,0x20000000,0x00004010, |
wolfSSL | 0:9d17e4342598 | 892 | 0x20000010,0x20404010,0x00404000,0x20400000, |
wolfSSL | 0:9d17e4342598 | 893 | 0x00404010,0x20404000,0x00000000,0x20400010, |
wolfSSL | 0:9d17e4342598 | 894 | 0x00000010,0x00004000,0x20400000,0x00404010, |
wolfSSL | 0:9d17e4342598 | 895 | 0x00004000,0x00400010,0x20004010,0x00000000, |
wolfSSL | 0:9d17e4342598 | 896 | 0x20404000,0x20000000,0x00400010,0x20004010}, |
wolfSSL | 0:9d17e4342598 | 897 | { |
wolfSSL | 0:9d17e4342598 | 898 | 0x00200000,0x04200002,0x04000802,0x00000000, |
wolfSSL | 0:9d17e4342598 | 899 | 0x00000800,0x04000802,0x00200802,0x04200800, |
wolfSSL | 0:9d17e4342598 | 900 | 0x04200802,0x00200000,0x00000000,0x04000002, |
wolfSSL | 0:9d17e4342598 | 901 | 0x00000002,0x04000000,0x04200002,0x00000802, |
wolfSSL | 0:9d17e4342598 | 902 | 0x04000800,0x00200802,0x00200002,0x04000800, |
wolfSSL | 0:9d17e4342598 | 903 | 0x04000002,0x04200000,0x04200800,0x00200002, |
wolfSSL | 0:9d17e4342598 | 904 | 0x04200000,0x00000800,0x00000802,0x04200802, |
wolfSSL | 0:9d17e4342598 | 905 | 0x00200800,0x00000002,0x04000000,0x00200800, |
wolfSSL | 0:9d17e4342598 | 906 | 0x04000000,0x00200800,0x00200000,0x04000802, |
wolfSSL | 0:9d17e4342598 | 907 | 0x04000802,0x04200002,0x04200002,0x00000002, |
wolfSSL | 0:9d17e4342598 | 908 | 0x00200002,0x04000000,0x04000800,0x00200000, |
wolfSSL | 0:9d17e4342598 | 909 | 0x04200800,0x00000802,0x00200802,0x04200800, |
wolfSSL | 0:9d17e4342598 | 910 | 0x00000802,0x04000002,0x04200802,0x04200000, |
wolfSSL | 0:9d17e4342598 | 911 | 0x00200800,0x00000000,0x00000002,0x04200802, |
wolfSSL | 0:9d17e4342598 | 912 | 0x00000000,0x00200802,0x04200000,0x00000800, |
wolfSSL | 0:9d17e4342598 | 913 | 0x04000002,0x04000800,0x00000800,0x00200002}, |
wolfSSL | 0:9d17e4342598 | 914 | { |
wolfSSL | 0:9d17e4342598 | 915 | 0x10001040,0x00001000,0x00040000,0x10041040, |
wolfSSL | 0:9d17e4342598 | 916 | 0x10000000,0x10001040,0x00000040,0x10000000, |
wolfSSL | 0:9d17e4342598 | 917 | 0x00040040,0x10040000,0x10041040,0x00041000, |
wolfSSL | 0:9d17e4342598 | 918 | 0x10041000,0x00041040,0x00001000,0x00000040, |
wolfSSL | 0:9d17e4342598 | 919 | 0x10040000,0x10000040,0x10001000,0x00001040, |
wolfSSL | 0:9d17e4342598 | 920 | 0x00041000,0x00040040,0x10040040,0x10041000, |
wolfSSL | 0:9d17e4342598 | 921 | 0x00001040,0x00000000,0x00000000,0x10040040, |
wolfSSL | 0:9d17e4342598 | 922 | 0x10000040,0x10001000,0x00041040,0x00040000, |
wolfSSL | 0:9d17e4342598 | 923 | 0x00041040,0x00040000,0x10041000,0x00001000, |
wolfSSL | 0:9d17e4342598 | 924 | 0x00000040,0x10040040,0x00001000,0x00041040, |
wolfSSL | 0:9d17e4342598 | 925 | 0x10001000,0x00000040,0x10000040,0x10040000, |
wolfSSL | 0:9d17e4342598 | 926 | 0x10040040,0x10000000,0x00040000,0x10001040, |
wolfSSL | 0:9d17e4342598 | 927 | 0x00000000,0x10041040,0x00040040,0x10000040, |
wolfSSL | 0:9d17e4342598 | 928 | 0x10040000,0x10001000,0x10001040,0x00000000, |
wolfSSL | 0:9d17e4342598 | 929 | 0x10041040,0x00041000,0x00041000,0x00001040, |
wolfSSL | 0:9d17e4342598 | 930 | 0x00001040,0x00040040,0x10000000,0x10041000} |
wolfSSL | 0:9d17e4342598 | 931 | }; |
wolfSSL | 0:9d17e4342598 | 932 | |
wolfSSL | 0:9d17e4342598 | 933 | |
wolfSSL | 0:9d17e4342598 | 934 | static INLINE void IPERM(word32* left, word32* right) |
wolfSSL | 0:9d17e4342598 | 935 | { |
wolfSSL | 0:9d17e4342598 | 936 | word32 work; |
wolfSSL | 0:9d17e4342598 | 937 | |
wolfSSL | 0:9d17e4342598 | 938 | *right = rotlFixed(*right, 4U); |
wolfSSL | 0:9d17e4342598 | 939 | work = (*left ^ *right) & 0xf0f0f0f0; |
wolfSSL | 0:9d17e4342598 | 940 | *left ^= work; |
wolfSSL | 0:9d17e4342598 | 941 | |
wolfSSL | 0:9d17e4342598 | 942 | *right = rotrFixed(*right^work, 20U); |
wolfSSL | 0:9d17e4342598 | 943 | work = (*left ^ *right) & 0xffff0000; |
wolfSSL | 0:9d17e4342598 | 944 | *left ^= work; |
wolfSSL | 0:9d17e4342598 | 945 | |
wolfSSL | 0:9d17e4342598 | 946 | *right = rotrFixed(*right^work, 18U); |
wolfSSL | 0:9d17e4342598 | 947 | work = (*left ^ *right) & 0x33333333; |
wolfSSL | 0:9d17e4342598 | 948 | *left ^= work; |
wolfSSL | 0:9d17e4342598 | 949 | |
wolfSSL | 0:9d17e4342598 | 950 | *right = rotrFixed(*right^work, 6U); |
wolfSSL | 0:9d17e4342598 | 951 | work = (*left ^ *right) & 0x00ff00ff; |
wolfSSL | 0:9d17e4342598 | 952 | *left ^= work; |
wolfSSL | 0:9d17e4342598 | 953 | |
wolfSSL | 0:9d17e4342598 | 954 | *right = rotlFixed(*right^work, 9U); |
wolfSSL | 0:9d17e4342598 | 955 | work = (*left ^ *right) & 0xaaaaaaaa; |
wolfSSL | 0:9d17e4342598 | 956 | *left = rotlFixed(*left^work, 1U); |
wolfSSL | 0:9d17e4342598 | 957 | *right ^= work; |
wolfSSL | 0:9d17e4342598 | 958 | } |
wolfSSL | 0:9d17e4342598 | 959 | |
wolfSSL | 0:9d17e4342598 | 960 | |
wolfSSL | 0:9d17e4342598 | 961 | static INLINE void FPERM(word32* left, word32* right) |
wolfSSL | 0:9d17e4342598 | 962 | { |
wolfSSL | 0:9d17e4342598 | 963 | word32 work; |
wolfSSL | 0:9d17e4342598 | 964 | |
wolfSSL | 0:9d17e4342598 | 965 | *right = rotrFixed(*right, 1U); |
wolfSSL | 0:9d17e4342598 | 966 | work = (*left ^ *right) & 0xaaaaaaaa; |
wolfSSL | 0:9d17e4342598 | 967 | *right ^= work; |
wolfSSL | 0:9d17e4342598 | 968 | |
wolfSSL | 0:9d17e4342598 | 969 | *left = rotrFixed(*left^work, 9U); |
wolfSSL | 0:9d17e4342598 | 970 | work = (*left ^ *right) & 0x00ff00ff; |
wolfSSL | 0:9d17e4342598 | 971 | *right ^= work; |
wolfSSL | 0:9d17e4342598 | 972 | |
wolfSSL | 0:9d17e4342598 | 973 | *left = rotlFixed(*left^work, 6U); |
wolfSSL | 0:9d17e4342598 | 974 | work = (*left ^ *right) & 0x33333333; |
wolfSSL | 0:9d17e4342598 | 975 | *right ^= work; |
wolfSSL | 0:9d17e4342598 | 976 | |
wolfSSL | 0:9d17e4342598 | 977 | *left = rotlFixed(*left^work, 18U); |
wolfSSL | 0:9d17e4342598 | 978 | work = (*left ^ *right) & 0xffff0000; |
wolfSSL | 0:9d17e4342598 | 979 | *right ^= work; |
wolfSSL | 0:9d17e4342598 | 980 | |
wolfSSL | 0:9d17e4342598 | 981 | *left = rotlFixed(*left^work, 20U); |
wolfSSL | 0:9d17e4342598 | 982 | work = (*left ^ *right) & 0xf0f0f0f0; |
wolfSSL | 0:9d17e4342598 | 983 | *right ^= work; |
wolfSSL | 0:9d17e4342598 | 984 | |
wolfSSL | 0:9d17e4342598 | 985 | *left = rotrFixed(*left^work, 4U); |
wolfSSL | 0:9d17e4342598 | 986 | } |
wolfSSL | 0:9d17e4342598 | 987 | |
wolfSSL | 0:9d17e4342598 | 988 | |
wolfSSL | 0:9d17e4342598 | 989 | static int DesSetKey(const byte* key, int dir, word32* out) |
wolfSSL | 0:9d17e4342598 | 990 | { |
wolfSSL | 0:9d17e4342598 | 991 | byte* buffer = (byte*)XMALLOC(56+56+8, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 0:9d17e4342598 | 992 | |
wolfSSL | 0:9d17e4342598 | 993 | if (!buffer) { |
wolfSSL | 0:9d17e4342598 | 994 | return MEMORY_E; |
wolfSSL | 0:9d17e4342598 | 995 | } |
wolfSSL | 0:9d17e4342598 | 996 | else { |
wolfSSL | 0:9d17e4342598 | 997 | byte* const pc1m = buffer; /* place to modify pc1 into */ |
wolfSSL | 0:9d17e4342598 | 998 | byte* const pcr = pc1m + 56; /* place to rotate pc1 into */ |
wolfSSL | 0:9d17e4342598 | 999 | byte* const ks = pcr + 56; |
wolfSSL | 0:9d17e4342598 | 1000 | register int i, j, l; |
wolfSSL | 0:9d17e4342598 | 1001 | int m; |
wolfSSL | 0:9d17e4342598 | 1002 | |
wolfSSL | 0:9d17e4342598 | 1003 | for (j = 0; j < 56; j++) { /* convert pc1 to bits of key */ |
wolfSSL | 0:9d17e4342598 | 1004 | l = pc1[j] - 1; /* integer bit location */ |
wolfSSL | 0:9d17e4342598 | 1005 | m = l & 07; /* find bit */ |
wolfSSL | 0:9d17e4342598 | 1006 | pc1m[j] = (key[l >> 3] & /* find which key byte l is in */ |
wolfSSL | 0:9d17e4342598 | 1007 | bytebit[m]) /* and which bit of that byte */ |
wolfSSL | 0:9d17e4342598 | 1008 | ? 1 : 0; /* and store 1-bit result */ |
wolfSSL | 0:9d17e4342598 | 1009 | } |
wolfSSL | 0:9d17e4342598 | 1010 | |
wolfSSL | 0:9d17e4342598 | 1011 | for (i = 0; i < 16; i++) { /* key chunk for each iteration */ |
wolfSSL | 0:9d17e4342598 | 1012 | XMEMSET(ks, 0, 8); /* Clear key schedule */ |
wolfSSL | 0:9d17e4342598 | 1013 | |
wolfSSL | 0:9d17e4342598 | 1014 | for (j = 0; j < 56; j++) /* rotate pc1 the right amount */ |
wolfSSL | 0:9d17e4342598 | 1015 | pcr[j] = |
wolfSSL | 0:9d17e4342598 | 1016 | pc1m[(l = j + totrot[i]) < (j < 28 ? 28 : 56) ? l : l-28]; |
wolfSSL | 0:9d17e4342598 | 1017 | |
wolfSSL | 0:9d17e4342598 | 1018 | /* rotate left and right halves independently */ |
wolfSSL | 0:9d17e4342598 | 1019 | for (j = 0; j < 48; j++) { /* select bits individually */ |
wolfSSL | 0:9d17e4342598 | 1020 | if (pcr[pc2[j] - 1]) { /* check bit that goes to ks[j] */ |
wolfSSL | 0:9d17e4342598 | 1021 | l= j % 6; /* mask it in if it's there */ |
wolfSSL | 0:9d17e4342598 | 1022 | ks[j/6] |= bytebit[l] >> 2; |
wolfSSL | 0:9d17e4342598 | 1023 | } |
wolfSSL | 0:9d17e4342598 | 1024 | } |
wolfSSL | 0:9d17e4342598 | 1025 | |
wolfSSL | 0:9d17e4342598 | 1026 | /* Now convert to odd/even interleaved form for use in F */ |
wolfSSL | 0:9d17e4342598 | 1027 | out[2*i] = ((word32) ks[0] << 24) |
wolfSSL | 0:9d17e4342598 | 1028 | | ((word32) ks[2] << 16) |
wolfSSL | 0:9d17e4342598 | 1029 | | ((word32) ks[4] << 8) |
wolfSSL | 0:9d17e4342598 | 1030 | | ((word32) ks[6]); |
wolfSSL | 0:9d17e4342598 | 1031 | |
wolfSSL | 0:9d17e4342598 | 1032 | out[2*i + 1] = ((word32) ks[1] << 24) |
wolfSSL | 0:9d17e4342598 | 1033 | | ((word32) ks[3] << 16) |
wolfSSL | 0:9d17e4342598 | 1034 | | ((word32) ks[5] << 8) |
wolfSSL | 0:9d17e4342598 | 1035 | | ((word32) ks[7]); |
wolfSSL | 0:9d17e4342598 | 1036 | } |
wolfSSL | 0:9d17e4342598 | 1037 | |
wolfSSL | 0:9d17e4342598 | 1038 | /* reverse key schedule order */ |
wolfSSL | 0:9d17e4342598 | 1039 | if (dir == DES_DECRYPTION) { |
wolfSSL | 0:9d17e4342598 | 1040 | for (i = 0; i < 16; i += 2) { |
wolfSSL | 0:9d17e4342598 | 1041 | word32 swap = out[i]; |
wolfSSL | 0:9d17e4342598 | 1042 | out[i] = out[DES_KS_SIZE - 2 - i]; |
wolfSSL | 0:9d17e4342598 | 1043 | out[DES_KS_SIZE - 2 - i] = swap; |
wolfSSL | 0:9d17e4342598 | 1044 | |
wolfSSL | 0:9d17e4342598 | 1045 | swap = out[i + 1]; |
wolfSSL | 0:9d17e4342598 | 1046 | out[i + 1] = out[DES_KS_SIZE - 1 - i]; |
wolfSSL | 0:9d17e4342598 | 1047 | out[DES_KS_SIZE - 1 - i] = swap; |
wolfSSL | 0:9d17e4342598 | 1048 | } |
wolfSSL | 0:9d17e4342598 | 1049 | } |
wolfSSL | 0:9d17e4342598 | 1050 | |
wolfSSL | 0:9d17e4342598 | 1051 | XFREE(buffer, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 0:9d17e4342598 | 1052 | } |
wolfSSL | 0:9d17e4342598 | 1053 | |
wolfSSL | 0:9d17e4342598 | 1054 | return 0; |
wolfSSL | 0:9d17e4342598 | 1055 | } |
wolfSSL | 0:9d17e4342598 | 1056 | |
wolfSSL | 0:9d17e4342598 | 1057 | |
wolfSSL | 0:9d17e4342598 | 1058 | static INLINE int Reverse(int dir) |
wolfSSL | 0:9d17e4342598 | 1059 | { |
wolfSSL | 0:9d17e4342598 | 1060 | return !dir; |
wolfSSL | 0:9d17e4342598 | 1061 | } |
wolfSSL | 0:9d17e4342598 | 1062 | |
wolfSSL | 0:9d17e4342598 | 1063 | |
wolfSSL | 0:9d17e4342598 | 1064 | int Des_SetKey(Des* des, const byte* key, const byte* iv, int dir) |
wolfSSL | 0:9d17e4342598 | 1065 | { |
wolfSSL | 0:9d17e4342598 | 1066 | Des_SetIV(des, iv); |
wolfSSL | 0:9d17e4342598 | 1067 | |
wolfSSL | 0:9d17e4342598 | 1068 | return DesSetKey(key, dir, des->key); |
wolfSSL | 0:9d17e4342598 | 1069 | } |
wolfSSL | 0:9d17e4342598 | 1070 | |
wolfSSL | 0:9d17e4342598 | 1071 | |
wolfSSL | 0:9d17e4342598 | 1072 | int Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir) |
wolfSSL | 0:9d17e4342598 | 1073 | { |
wolfSSL | 0:9d17e4342598 | 1074 | int ret; |
wolfSSL | 0:9d17e4342598 | 1075 | |
wolfSSL | 0:9d17e4342598 | 1076 | #ifdef HAVE_CAVIUM |
wolfSSL | 0:9d17e4342598 | 1077 | if (des->magic == CYASSL_3DES_CAVIUM_MAGIC) |
wolfSSL | 0:9d17e4342598 | 1078 | return Des3_CaviumSetKey(des, key, iv); |
wolfSSL | 0:9d17e4342598 | 1079 | #endif |
wolfSSL | 0:9d17e4342598 | 1080 | |
wolfSSL | 0:9d17e4342598 | 1081 | ret = DesSetKey(key + (dir == DES_ENCRYPTION ? 0:16), dir, des->key[0]); |
wolfSSL | 0:9d17e4342598 | 1082 | if (ret != 0) |
wolfSSL | 0:9d17e4342598 | 1083 | return ret; |
wolfSSL | 0:9d17e4342598 | 1084 | |
wolfSSL | 0:9d17e4342598 | 1085 | ret = DesSetKey(key + 8, Reverse(dir), des->key[1]); |
wolfSSL | 0:9d17e4342598 | 1086 | if (ret != 0) |
wolfSSL | 0:9d17e4342598 | 1087 | return ret; |
wolfSSL | 0:9d17e4342598 | 1088 | |
wolfSSL | 0:9d17e4342598 | 1089 | ret = DesSetKey(key + (dir == DES_DECRYPTION ? 0:16), dir, des->key[2]); |
wolfSSL | 0:9d17e4342598 | 1090 | if (ret != 0) |
wolfSSL | 0:9d17e4342598 | 1091 | return ret; |
wolfSSL | 0:9d17e4342598 | 1092 | |
wolfSSL | 0:9d17e4342598 | 1093 | return Des3_SetIV(des, iv); |
wolfSSL | 0:9d17e4342598 | 1094 | } |
wolfSSL | 0:9d17e4342598 | 1095 | |
wolfSSL | 0:9d17e4342598 | 1096 | |
wolfSSL | 0:9d17e4342598 | 1097 | static void DesRawProcessBlock(word32* lIn, word32* rIn, const word32* kptr) |
wolfSSL | 0:9d17e4342598 | 1098 | { |
wolfSSL | 0:9d17e4342598 | 1099 | word32 l = *lIn, r = *rIn, i; |
wolfSSL | 0:9d17e4342598 | 1100 | |
wolfSSL | 0:9d17e4342598 | 1101 | for (i=0; i<8; i++) |
wolfSSL | 0:9d17e4342598 | 1102 | { |
wolfSSL | 0:9d17e4342598 | 1103 | word32 work = rotrFixed(r, 4U) ^ kptr[4*i+0]; |
wolfSSL | 0:9d17e4342598 | 1104 | l ^= Spbox[6][(work) & 0x3f] |
wolfSSL | 0:9d17e4342598 | 1105 | ^ Spbox[4][(work >> 8) & 0x3f] |
wolfSSL | 0:9d17e4342598 | 1106 | ^ Spbox[2][(work >> 16) & 0x3f] |
wolfSSL | 0:9d17e4342598 | 1107 | ^ Spbox[0][(work >> 24) & 0x3f]; |
wolfSSL | 0:9d17e4342598 | 1108 | work = r ^ kptr[4*i+1]; |
wolfSSL | 0:9d17e4342598 | 1109 | l ^= Spbox[7][(work) & 0x3f] |
wolfSSL | 0:9d17e4342598 | 1110 | ^ Spbox[5][(work >> 8) & 0x3f] |
wolfSSL | 0:9d17e4342598 | 1111 | ^ Spbox[3][(work >> 16) & 0x3f] |
wolfSSL | 0:9d17e4342598 | 1112 | ^ Spbox[1][(work >> 24) & 0x3f]; |
wolfSSL | 0:9d17e4342598 | 1113 | |
wolfSSL | 0:9d17e4342598 | 1114 | work = rotrFixed(l, 4U) ^ kptr[4*i+2]; |
wolfSSL | 0:9d17e4342598 | 1115 | r ^= Spbox[6][(work) & 0x3f] |
wolfSSL | 0:9d17e4342598 | 1116 | ^ Spbox[4][(work >> 8) & 0x3f] |
wolfSSL | 0:9d17e4342598 | 1117 | ^ Spbox[2][(work >> 16) & 0x3f] |
wolfSSL | 0:9d17e4342598 | 1118 | ^ Spbox[0][(work >> 24) & 0x3f]; |
wolfSSL | 0:9d17e4342598 | 1119 | work = l ^ kptr[4*i+3]; |
wolfSSL | 0:9d17e4342598 | 1120 | r ^= Spbox[7][(work) & 0x3f] |
wolfSSL | 0:9d17e4342598 | 1121 | ^ Spbox[5][(work >> 8) & 0x3f] |
wolfSSL | 0:9d17e4342598 | 1122 | ^ Spbox[3][(work >> 16) & 0x3f] |
wolfSSL | 0:9d17e4342598 | 1123 | ^ Spbox[1][(work >> 24) & 0x3f]; |
wolfSSL | 0:9d17e4342598 | 1124 | } |
wolfSSL | 0:9d17e4342598 | 1125 | |
wolfSSL | 0:9d17e4342598 | 1126 | *lIn = l; *rIn = r; |
wolfSSL | 0:9d17e4342598 | 1127 | } |
wolfSSL | 0:9d17e4342598 | 1128 | |
wolfSSL | 0:9d17e4342598 | 1129 | |
wolfSSL | 0:9d17e4342598 | 1130 | static void DesProcessBlock(Des* des, const byte* in, byte* out) |
wolfSSL | 0:9d17e4342598 | 1131 | { |
wolfSSL | 0:9d17e4342598 | 1132 | word32 l, r; |
wolfSSL | 0:9d17e4342598 | 1133 | |
wolfSSL | 0:9d17e4342598 | 1134 | XMEMCPY(&l, in, sizeof(l)); |
wolfSSL | 0:9d17e4342598 | 1135 | XMEMCPY(&r, in + sizeof(l), sizeof(r)); |
wolfSSL | 0:9d17e4342598 | 1136 | #ifdef LITTLE_ENDIAN_ORDER |
wolfSSL | 0:9d17e4342598 | 1137 | l = ByteReverseWord32(l); |
wolfSSL | 0:9d17e4342598 | 1138 | r = ByteReverseWord32(r); |
wolfSSL | 0:9d17e4342598 | 1139 | #endif |
wolfSSL | 0:9d17e4342598 | 1140 | IPERM(&l,&r); |
wolfSSL | 0:9d17e4342598 | 1141 | |
wolfSSL | 0:9d17e4342598 | 1142 | DesRawProcessBlock(&l, &r, des->key); |
wolfSSL | 0:9d17e4342598 | 1143 | |
wolfSSL | 0:9d17e4342598 | 1144 | FPERM(&l,&r); |
wolfSSL | 0:9d17e4342598 | 1145 | #ifdef LITTLE_ENDIAN_ORDER |
wolfSSL | 0:9d17e4342598 | 1146 | l = ByteReverseWord32(l); |
wolfSSL | 0:9d17e4342598 | 1147 | r = ByteReverseWord32(r); |
wolfSSL | 0:9d17e4342598 | 1148 | #endif |
wolfSSL | 0:9d17e4342598 | 1149 | XMEMCPY(out, &r, sizeof(r)); |
wolfSSL | 0:9d17e4342598 | 1150 | XMEMCPY(out + sizeof(r), &l, sizeof(l)); |
wolfSSL | 0:9d17e4342598 | 1151 | } |
wolfSSL | 0:9d17e4342598 | 1152 | |
wolfSSL | 0:9d17e4342598 | 1153 | |
wolfSSL | 0:9d17e4342598 | 1154 | static void Des3ProcessBlock(Des3* des, const byte* in, byte* out) |
wolfSSL | 0:9d17e4342598 | 1155 | { |
wolfSSL | 0:9d17e4342598 | 1156 | word32 l, r; |
wolfSSL | 0:9d17e4342598 | 1157 | |
wolfSSL | 0:9d17e4342598 | 1158 | XMEMCPY(&l, in, sizeof(l)); |
wolfSSL | 0:9d17e4342598 | 1159 | XMEMCPY(&r, in + sizeof(l), sizeof(r)); |
wolfSSL | 0:9d17e4342598 | 1160 | #ifdef LITTLE_ENDIAN_ORDER |
wolfSSL | 0:9d17e4342598 | 1161 | l = ByteReverseWord32(l); |
wolfSSL | 0:9d17e4342598 | 1162 | r = ByteReverseWord32(r); |
wolfSSL | 0:9d17e4342598 | 1163 | #endif |
wolfSSL | 0:9d17e4342598 | 1164 | IPERM(&l,&r); |
wolfSSL | 0:9d17e4342598 | 1165 | |
wolfSSL | 0:9d17e4342598 | 1166 | DesRawProcessBlock(&l, &r, des->key[0]); |
wolfSSL | 0:9d17e4342598 | 1167 | DesRawProcessBlock(&r, &l, des->key[1]); |
wolfSSL | 0:9d17e4342598 | 1168 | DesRawProcessBlock(&l, &r, des->key[2]); |
wolfSSL | 0:9d17e4342598 | 1169 | |
wolfSSL | 0:9d17e4342598 | 1170 | FPERM(&l,&r); |
wolfSSL | 0:9d17e4342598 | 1171 | #ifdef LITTLE_ENDIAN_ORDER |
wolfSSL | 0:9d17e4342598 | 1172 | l = ByteReverseWord32(l); |
wolfSSL | 0:9d17e4342598 | 1173 | r = ByteReverseWord32(r); |
wolfSSL | 0:9d17e4342598 | 1174 | #endif |
wolfSSL | 0:9d17e4342598 | 1175 | XMEMCPY(out, &r, sizeof(r)); |
wolfSSL | 0:9d17e4342598 | 1176 | XMEMCPY(out + sizeof(r), &l, sizeof(l)); |
wolfSSL | 0:9d17e4342598 | 1177 | } |
wolfSSL | 0:9d17e4342598 | 1178 | |
wolfSSL | 0:9d17e4342598 | 1179 | |
wolfSSL | 0:9d17e4342598 | 1180 | void Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz) |
wolfSSL | 0:9d17e4342598 | 1181 | { |
wolfSSL | 0:9d17e4342598 | 1182 | word32 blocks = sz / DES_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 1183 | |
wolfSSL | 0:9d17e4342598 | 1184 | while (blocks--) { |
wolfSSL | 0:9d17e4342598 | 1185 | xorbuf((byte*)des->reg, in, DES_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 1186 | DesProcessBlock(des, (byte*)des->reg, (byte*)des->reg); |
wolfSSL | 0:9d17e4342598 | 1187 | XMEMCPY(out, des->reg, DES_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 1188 | |
wolfSSL | 0:9d17e4342598 | 1189 | out += DES_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 1190 | in += DES_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 1191 | } |
wolfSSL | 0:9d17e4342598 | 1192 | } |
wolfSSL | 0:9d17e4342598 | 1193 | |
wolfSSL | 0:9d17e4342598 | 1194 | |
wolfSSL | 0:9d17e4342598 | 1195 | void Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz) |
wolfSSL | 0:9d17e4342598 | 1196 | { |
wolfSSL | 0:9d17e4342598 | 1197 | word32 blocks = sz / DES_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 1198 | byte hold[DES_BLOCK_SIZE]; |
wolfSSL | 0:9d17e4342598 | 1199 | |
wolfSSL | 0:9d17e4342598 | 1200 | while (blocks--) { |
wolfSSL | 0:9d17e4342598 | 1201 | XMEMCPY(des->tmp, in, DES_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 1202 | DesProcessBlock(des, (byte*)des->tmp, out); |
wolfSSL | 0:9d17e4342598 | 1203 | xorbuf(out, (byte*)des->reg, DES_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 1204 | |
wolfSSL | 0:9d17e4342598 | 1205 | XMEMCPY(hold, des->reg, DES_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 1206 | XMEMCPY(des->reg, des->tmp, DES_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 1207 | XMEMCPY(des->tmp, hold, DES_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 1208 | |
wolfSSL | 0:9d17e4342598 | 1209 | out += DES_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 1210 | in += DES_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 1211 | } |
wolfSSL | 0:9d17e4342598 | 1212 | } |
wolfSSL | 0:9d17e4342598 | 1213 | |
wolfSSL | 0:9d17e4342598 | 1214 | |
wolfSSL | 0:9d17e4342598 | 1215 | int Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz) |
wolfSSL | 0:9d17e4342598 | 1216 | { |
wolfSSL | 0:9d17e4342598 | 1217 | word32 blocks; |
wolfSSL | 0:9d17e4342598 | 1218 | |
wolfSSL | 0:9d17e4342598 | 1219 | #ifdef HAVE_CAVIUM |
wolfSSL | 0:9d17e4342598 | 1220 | if (des->magic == CYASSL_3DES_CAVIUM_MAGIC) |
wolfSSL | 0:9d17e4342598 | 1221 | return Des3_CaviumCbcEncrypt(des, out, in, sz); |
wolfSSL | 0:9d17e4342598 | 1222 | #endif |
wolfSSL | 0:9d17e4342598 | 1223 | |
wolfSSL | 0:9d17e4342598 | 1224 | blocks = sz / DES_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 1225 | while (blocks--) { |
wolfSSL | 0:9d17e4342598 | 1226 | xorbuf((byte*)des->reg, in, DES_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 1227 | Des3ProcessBlock(des, (byte*)des->reg, (byte*)des->reg); |
wolfSSL | 0:9d17e4342598 | 1228 | XMEMCPY(out, des->reg, DES_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 1229 | |
wolfSSL | 0:9d17e4342598 | 1230 | out += DES_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 1231 | in += DES_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 1232 | } |
wolfSSL | 0:9d17e4342598 | 1233 | return 0; |
wolfSSL | 0:9d17e4342598 | 1234 | } |
wolfSSL | 0:9d17e4342598 | 1235 | |
wolfSSL | 0:9d17e4342598 | 1236 | |
wolfSSL | 0:9d17e4342598 | 1237 | int Des3_CbcDecrypt(Des3* des, byte* out, const byte* in, word32 sz) |
wolfSSL | 0:9d17e4342598 | 1238 | { |
wolfSSL | 0:9d17e4342598 | 1239 | word32 blocks; |
wolfSSL | 0:9d17e4342598 | 1240 | |
wolfSSL | 0:9d17e4342598 | 1241 | #ifdef HAVE_CAVIUM |
wolfSSL | 0:9d17e4342598 | 1242 | if (des->magic == CYASSL_3DES_CAVIUM_MAGIC) |
wolfSSL | 0:9d17e4342598 | 1243 | return Des3_CaviumCbcDecrypt(des, out, in, sz); |
wolfSSL | 0:9d17e4342598 | 1244 | #endif |
wolfSSL | 0:9d17e4342598 | 1245 | |
wolfSSL | 0:9d17e4342598 | 1246 | blocks = sz / DES_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 1247 | while (blocks--) { |
wolfSSL | 0:9d17e4342598 | 1248 | XMEMCPY(des->tmp, in, DES_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 1249 | Des3ProcessBlock(des, (byte*)des->tmp, out); |
wolfSSL | 0:9d17e4342598 | 1250 | xorbuf(out, (byte*)des->reg, DES_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 1251 | XMEMCPY(des->reg, des->tmp, DES_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 1252 | |
wolfSSL | 0:9d17e4342598 | 1253 | out += DES_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 1254 | in += DES_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 1255 | } |
wolfSSL | 0:9d17e4342598 | 1256 | return 0; |
wolfSSL | 0:9d17e4342598 | 1257 | } |
wolfSSL | 0:9d17e4342598 | 1258 | |
wolfSSL | 0:9d17e4342598 | 1259 | #ifdef CYASSL_DES_ECB |
wolfSSL | 0:9d17e4342598 | 1260 | |
wolfSSL | 0:9d17e4342598 | 1261 | /* One block, compatibility only */ |
wolfSSL | 0:9d17e4342598 | 1262 | void Des_EcbEncrypt(Des* des, byte* out, const byte* in, word32 sz) |
wolfSSL | 0:9d17e4342598 | 1263 | { |
wolfSSL | 0:9d17e4342598 | 1264 | word32 blocks = sz / DES_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 1265 | |
wolfSSL | 0:9d17e4342598 | 1266 | while (blocks--) { |
wolfSSL | 0:9d17e4342598 | 1267 | DesProcessBlock(des, in, out); |
wolfSSL | 0:9d17e4342598 | 1268 | |
wolfSSL | 0:9d17e4342598 | 1269 | out += DES_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 1270 | in += DES_BLOCK_SIZE; |
wolfSSL | 0:9d17e4342598 | 1271 | } |
wolfSSL | 0:9d17e4342598 | 1272 | } |
wolfSSL | 0:9d17e4342598 | 1273 | |
wolfSSL | 0:9d17e4342598 | 1274 | #endif /* CYASSL_DES_ECB */ |
wolfSSL | 0:9d17e4342598 | 1275 | |
wolfSSL | 0:9d17e4342598 | 1276 | #endif /* STM32F2_CRYPTO */ |
wolfSSL | 0:9d17e4342598 | 1277 | |
wolfSSL | 0:9d17e4342598 | 1278 | void Des_SetIV(Des* des, const byte* iv) |
wolfSSL | 0:9d17e4342598 | 1279 | { |
wolfSSL | 0:9d17e4342598 | 1280 | if (des && iv) |
wolfSSL | 0:9d17e4342598 | 1281 | XMEMCPY(des->reg, iv, DES_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 1282 | else if (des) |
wolfSSL | 0:9d17e4342598 | 1283 | XMEMSET(des->reg, 0, DES_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 1284 | } |
wolfSSL | 0:9d17e4342598 | 1285 | |
wolfSSL | 0:9d17e4342598 | 1286 | |
wolfSSL | 0:9d17e4342598 | 1287 | int Des3_SetIV(Des3* des, const byte* iv) |
wolfSSL | 0:9d17e4342598 | 1288 | { |
wolfSSL | 0:9d17e4342598 | 1289 | if (des && iv) |
wolfSSL | 0:9d17e4342598 | 1290 | XMEMCPY(des->reg, iv, DES_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 1291 | else if (des) |
wolfSSL | 0:9d17e4342598 | 1292 | XMEMSET(des->reg, 0, DES_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 1293 | |
wolfSSL | 0:9d17e4342598 | 1294 | return 0; |
wolfSSL | 0:9d17e4342598 | 1295 | } |
wolfSSL | 0:9d17e4342598 | 1296 | |
wolfSSL | 0:9d17e4342598 | 1297 | |
wolfSSL | 0:9d17e4342598 | 1298 | #ifdef HAVE_CAVIUM |
wolfSSL | 0:9d17e4342598 | 1299 | |
wolfSSL | 0:9d17e4342598 | 1300 | #include <cyassl/ctaocrypt/logging.h> |
wolfSSL | 0:9d17e4342598 | 1301 | #include "cavium_common.h" |
wolfSSL | 0:9d17e4342598 | 1302 | |
wolfSSL | 0:9d17e4342598 | 1303 | /* Initiliaze Des3 for use with Nitrox device */ |
wolfSSL | 0:9d17e4342598 | 1304 | int Des3_InitCavium(Des3* des3, int devId) |
wolfSSL | 0:9d17e4342598 | 1305 | { |
wolfSSL | 0:9d17e4342598 | 1306 | if (des3 == NULL) |
wolfSSL | 0:9d17e4342598 | 1307 | return -1; |
wolfSSL | 0:9d17e4342598 | 1308 | |
wolfSSL | 0:9d17e4342598 | 1309 | if (CspAllocContext(CONTEXT_SSL, &des3->contextHandle, devId) != 0) |
wolfSSL | 0:9d17e4342598 | 1310 | return -1; |
wolfSSL | 0:9d17e4342598 | 1311 | |
wolfSSL | 0:9d17e4342598 | 1312 | des3->devId = devId; |
wolfSSL | 0:9d17e4342598 | 1313 | des3->magic = CYASSL_3DES_CAVIUM_MAGIC; |
wolfSSL | 0:9d17e4342598 | 1314 | |
wolfSSL | 0:9d17e4342598 | 1315 | return 0; |
wolfSSL | 0:9d17e4342598 | 1316 | } |
wolfSSL | 0:9d17e4342598 | 1317 | |
wolfSSL | 0:9d17e4342598 | 1318 | |
wolfSSL | 0:9d17e4342598 | 1319 | /* Free Des3 from use with Nitrox device */ |
wolfSSL | 0:9d17e4342598 | 1320 | void Des3_FreeCavium(Des3* des3) |
wolfSSL | 0:9d17e4342598 | 1321 | { |
wolfSSL | 0:9d17e4342598 | 1322 | if (des3 == NULL) |
wolfSSL | 0:9d17e4342598 | 1323 | return; |
wolfSSL | 0:9d17e4342598 | 1324 | |
wolfSSL | 0:9d17e4342598 | 1325 | if (des3->magic != CYASSL_3DES_CAVIUM_MAGIC) |
wolfSSL | 0:9d17e4342598 | 1326 | return; |
wolfSSL | 0:9d17e4342598 | 1327 | |
wolfSSL | 0:9d17e4342598 | 1328 | CspFreeContext(CONTEXT_SSL, des3->contextHandle, des3->devId); |
wolfSSL | 0:9d17e4342598 | 1329 | des3->magic = 0; |
wolfSSL | 0:9d17e4342598 | 1330 | } |
wolfSSL | 0:9d17e4342598 | 1331 | |
wolfSSL | 0:9d17e4342598 | 1332 | |
wolfSSL | 0:9d17e4342598 | 1333 | static int Des3_CaviumSetKey(Des3* des3, const byte* key, const byte* iv) |
wolfSSL | 0:9d17e4342598 | 1334 | { |
wolfSSL | 0:9d17e4342598 | 1335 | if (des3 == NULL) |
wolfSSL | 0:9d17e4342598 | 1336 | return -1; |
wolfSSL | 0:9d17e4342598 | 1337 | |
wolfSSL | 0:9d17e4342598 | 1338 | /* key[0] holds key, iv in reg */ |
wolfSSL | 0:9d17e4342598 | 1339 | XMEMCPY(des3->key[0], key, DES_BLOCK_SIZE*3); |
wolfSSL | 0:9d17e4342598 | 1340 | |
wolfSSL | 0:9d17e4342598 | 1341 | return Des3_SetIV(des3, iv); |
wolfSSL | 0:9d17e4342598 | 1342 | } |
wolfSSL | 0:9d17e4342598 | 1343 | |
wolfSSL | 0:9d17e4342598 | 1344 | |
wolfSSL | 0:9d17e4342598 | 1345 | static int Des3_CaviumCbcEncrypt(Des3* des3, byte* out, const byte* in, |
wolfSSL | 0:9d17e4342598 | 1346 | word32 length) |
wolfSSL | 0:9d17e4342598 | 1347 | { |
wolfSSL | 0:9d17e4342598 | 1348 | word offset = 0; |
wolfSSL | 0:9d17e4342598 | 1349 | word32 requestId; |
wolfSSL | 0:9d17e4342598 | 1350 | |
wolfSSL | 0:9d17e4342598 | 1351 | while (length > CYASSL_MAX_16BIT) { |
wolfSSL | 0:9d17e4342598 | 1352 | word16 slen = (word16)CYASSL_MAX_16BIT; |
wolfSSL | 0:9d17e4342598 | 1353 | if (CspEncrypt3Des(CAVIUM_BLOCKING, des3->contextHandle, |
wolfSSL | 0:9d17e4342598 | 1354 | CAVIUM_NO_UPDATE, slen, (byte*)in + offset, |
wolfSSL | 0:9d17e4342598 | 1355 | out + offset, (byte*)des3->reg, (byte*)des3->key[0], |
wolfSSL | 0:9d17e4342598 | 1356 | &requestId, des3->devId) != 0) { |
wolfSSL | 0:9d17e4342598 | 1357 | CYASSL_MSG("Bad Cavium 3DES Cbc Encrypt"); |
wolfSSL | 0:9d17e4342598 | 1358 | return -1; |
wolfSSL | 0:9d17e4342598 | 1359 | } |
wolfSSL | 0:9d17e4342598 | 1360 | length -= CYASSL_MAX_16BIT; |
wolfSSL | 0:9d17e4342598 | 1361 | offset += CYASSL_MAX_16BIT; |
wolfSSL | 0:9d17e4342598 | 1362 | XMEMCPY(des3->reg, out + offset - DES_BLOCK_SIZE, DES_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 1363 | } |
wolfSSL | 0:9d17e4342598 | 1364 | if (length) { |
wolfSSL | 0:9d17e4342598 | 1365 | word16 slen = (word16)length; |
wolfSSL | 0:9d17e4342598 | 1366 | |
wolfSSL | 0:9d17e4342598 | 1367 | if (CspEncrypt3Des(CAVIUM_BLOCKING, des3->contextHandle, |
wolfSSL | 0:9d17e4342598 | 1368 | CAVIUM_NO_UPDATE, slen, (byte*)in + offset, |
wolfSSL | 0:9d17e4342598 | 1369 | out + offset, (byte*)des3->reg, (byte*)des3->key[0], |
wolfSSL | 0:9d17e4342598 | 1370 | &requestId, des3->devId) != 0) { |
wolfSSL | 0:9d17e4342598 | 1371 | CYASSL_MSG("Bad Cavium 3DES Cbc Encrypt"); |
wolfSSL | 0:9d17e4342598 | 1372 | return -1; |
wolfSSL | 0:9d17e4342598 | 1373 | } |
wolfSSL | 0:9d17e4342598 | 1374 | XMEMCPY(des3->reg, out+offset+length - DES_BLOCK_SIZE, DES_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 1375 | } |
wolfSSL | 0:9d17e4342598 | 1376 | return 0; |
wolfSSL | 0:9d17e4342598 | 1377 | } |
wolfSSL | 0:9d17e4342598 | 1378 | |
wolfSSL | 0:9d17e4342598 | 1379 | static int Des3_CaviumCbcDecrypt(Des3* des3, byte* out, const byte* in, |
wolfSSL | 0:9d17e4342598 | 1380 | word32 length) |
wolfSSL | 0:9d17e4342598 | 1381 | { |
wolfSSL | 0:9d17e4342598 | 1382 | word32 requestId; |
wolfSSL | 0:9d17e4342598 | 1383 | word offset = 0; |
wolfSSL | 0:9d17e4342598 | 1384 | |
wolfSSL | 0:9d17e4342598 | 1385 | while (length > CYASSL_MAX_16BIT) { |
wolfSSL | 0:9d17e4342598 | 1386 | word16 slen = (word16)CYASSL_MAX_16BIT; |
wolfSSL | 0:9d17e4342598 | 1387 | XMEMCPY(des3->tmp, in + offset + slen - DES_BLOCK_SIZE, DES_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 1388 | if (CspDecrypt3Des(CAVIUM_BLOCKING, des3->contextHandle, |
wolfSSL | 0:9d17e4342598 | 1389 | CAVIUM_NO_UPDATE, slen, (byte*)in+offset, out+offset, |
wolfSSL | 0:9d17e4342598 | 1390 | (byte*)des3->reg, (byte*)des3->key[0], &requestId, |
wolfSSL | 0:9d17e4342598 | 1391 | des3->devId) != 0) { |
wolfSSL | 0:9d17e4342598 | 1392 | CYASSL_MSG("Bad Cavium 3Des Decrypt"); |
wolfSSL | 0:9d17e4342598 | 1393 | return -1; |
wolfSSL | 0:9d17e4342598 | 1394 | } |
wolfSSL | 0:9d17e4342598 | 1395 | length -= CYASSL_MAX_16BIT; |
wolfSSL | 0:9d17e4342598 | 1396 | offset += CYASSL_MAX_16BIT; |
wolfSSL | 0:9d17e4342598 | 1397 | XMEMCPY(des3->reg, des3->tmp, DES_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 1398 | } |
wolfSSL | 0:9d17e4342598 | 1399 | if (length) { |
wolfSSL | 0:9d17e4342598 | 1400 | word16 slen = (word16)length; |
wolfSSL | 0:9d17e4342598 | 1401 | XMEMCPY(des3->tmp, in + offset + slen - DES_BLOCK_SIZE,DES_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 1402 | if (CspDecrypt3Des(CAVIUM_BLOCKING, des3->contextHandle, |
wolfSSL | 0:9d17e4342598 | 1403 | CAVIUM_NO_UPDATE, slen, (byte*)in+offset, out+offset, |
wolfSSL | 0:9d17e4342598 | 1404 | (byte*)des3->reg, (byte*)des3->key[0], &requestId, |
wolfSSL | 0:9d17e4342598 | 1405 | des3->devId) != 0) { |
wolfSSL | 0:9d17e4342598 | 1406 | CYASSL_MSG("Bad Cavium 3Des Decrypt"); |
wolfSSL | 0:9d17e4342598 | 1407 | return -1; |
wolfSSL | 0:9d17e4342598 | 1408 | } |
wolfSSL | 0:9d17e4342598 | 1409 | XMEMCPY(des3->reg, des3->tmp, DES_BLOCK_SIZE); |
wolfSSL | 0:9d17e4342598 | 1410 | } |
wolfSSL | 0:9d17e4342598 | 1411 | return 0; |
wolfSSL | 0:9d17e4342598 | 1412 | } |
wolfSSL | 0:9d17e4342598 | 1413 | |
wolfSSL | 0:9d17e4342598 | 1414 | #endif /* HAVE_CAVIUM */ |
wolfSSL | 0:9d17e4342598 | 1415 | |
wolfSSL | 0:9d17e4342598 | 1416 | #endif /* NO_DES3 */ |