cya_u

Fork of CyaSSL-forEncrypt by Mobius IoT

Committer:
vbahl2
Date:
Wed May 10 18:20:47 2017 +0000
Revision:
2:d0516dc143b1
Parent:
0:5045d2638c29
updated

Who changed what in which revision?

UserRevisionLine numberNew contents of line
toddouska 0:5045d2638c29 1 /* des3.c
toddouska 0:5045d2638c29 2 *
toddouska 0:5045d2638c29 3 * Copyright (C) 2006-2009 Sawtooth Consulting Ltd.
toddouska 0:5045d2638c29 4 *
toddouska 0:5045d2638c29 5 * This file is part of CyaSSL.
toddouska 0:5045d2638c29 6 *
toddouska 0:5045d2638c29 7 * CyaSSL is free software; you can redistribute it and/or modify
toddouska 0:5045d2638c29 8 * it under the terms of the GNU General Public License as published by
toddouska 0:5045d2638c29 9 * the Free Software Foundation; either version 2 of the License, or
toddouska 0:5045d2638c29 10 * (at your option) any later version.
toddouska 0:5045d2638c29 11 *
toddouska 0:5045d2638c29 12 * CyaSSL is distributed in the hope that it will be useful,
toddouska 0:5045d2638c29 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
toddouska 0:5045d2638c29 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
toddouska 0:5045d2638c29 15 * GNU General Public License for more details.
toddouska 0:5045d2638c29 16 *
toddouska 0:5045d2638c29 17 * You should have received a copy of the GNU General Public License
toddouska 0:5045d2638c29 18 * along with this program; if not, write to the Free Software
toddouska 0:5045d2638c29 19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
toddouska 0:5045d2638c29 20 */
toddouska 0:5045d2638c29 21
toddouska 0:5045d2638c29 22
toddouska 0:5045d2638c29 23 #ifndef NO_DES3
toddouska 0:5045d2638c29 24
toddouska 0:5045d2638c29 25 #include "des3.h"
toddouska 0:5045d2638c29 26 #ifdef NO_INLINE
toddouska 0:5045d2638c29 27 #include "misc.h"
toddouska 0:5045d2638c29 28 #else
toddouska 0:5045d2638c29 29 #include "misc.c"
toddouska 0:5045d2638c29 30 #endif
toddouska 0:5045d2638c29 31
toddouska 0:5045d2638c29 32
toddouska 0:5045d2638c29 33 /* permuted choice table (key) */
toddouska 0:5045d2638c29 34 static const byte pc1[] = {
toddouska 0:5045d2638c29 35 57, 49, 41, 33, 25, 17, 9,
toddouska 0:5045d2638c29 36 1, 58, 50, 42, 34, 26, 18,
toddouska 0:5045d2638c29 37 10, 2, 59, 51, 43, 35, 27,
toddouska 0:5045d2638c29 38 19, 11, 3, 60, 52, 44, 36,
toddouska 0:5045d2638c29 39
toddouska 0:5045d2638c29 40 63, 55, 47, 39, 31, 23, 15,
toddouska 0:5045d2638c29 41 7, 62, 54, 46, 38, 30, 22,
toddouska 0:5045d2638c29 42 14, 6, 61, 53, 45, 37, 29,
toddouska 0:5045d2638c29 43 21, 13, 5, 28, 20, 12, 4
toddouska 0:5045d2638c29 44 };
toddouska 0:5045d2638c29 45
toddouska 0:5045d2638c29 46 /* number left rotations of pc1 */
toddouska 0:5045d2638c29 47 static const byte totrot[] = {
toddouska 0:5045d2638c29 48 1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28
toddouska 0:5045d2638c29 49 };
toddouska 0:5045d2638c29 50
toddouska 0:5045d2638c29 51 /* permuted choice key (table) */
toddouska 0:5045d2638c29 52 static const byte pc2[] = {
toddouska 0:5045d2638c29 53 14, 17, 11, 24, 1, 5,
toddouska 0:5045d2638c29 54 3, 28, 15, 6, 21, 10,
toddouska 0:5045d2638c29 55 23, 19, 12, 4, 26, 8,
toddouska 0:5045d2638c29 56 16, 7, 27, 20, 13, 2,
toddouska 0:5045d2638c29 57 41, 52, 31, 37, 47, 55,
toddouska 0:5045d2638c29 58 30, 40, 51, 45, 33, 48,
toddouska 0:5045d2638c29 59 44, 49, 39, 56, 34, 53,
toddouska 0:5045d2638c29 60 46, 42, 50, 36, 29, 32
toddouska 0:5045d2638c29 61 };
toddouska 0:5045d2638c29 62
toddouska 0:5045d2638c29 63 /* End of DES-defined tables */
toddouska 0:5045d2638c29 64
toddouska 0:5045d2638c29 65 /* bit 0 is left-most in byte */
toddouska 0:5045d2638c29 66 static const int bytebit[] = {
toddouska 0:5045d2638c29 67 0200,0100,040,020,010,04,02,01
toddouska 0:5045d2638c29 68 };
toddouska 0:5045d2638c29 69
toddouska 0:5045d2638c29 70 const word32 Spbox[8][64] = {
toddouska 0:5045d2638c29 71 {
toddouska 0:5045d2638c29 72 0x01010400,0x00000000,0x00010000,0x01010404,
toddouska 0:5045d2638c29 73 0x01010004,0x00010404,0x00000004,0x00010000,
toddouska 0:5045d2638c29 74 0x00000400,0x01010400,0x01010404,0x00000400,
toddouska 0:5045d2638c29 75 0x01000404,0x01010004,0x01000000,0x00000004,
toddouska 0:5045d2638c29 76 0x00000404,0x01000400,0x01000400,0x00010400,
toddouska 0:5045d2638c29 77 0x00010400,0x01010000,0x01010000,0x01000404,
toddouska 0:5045d2638c29 78 0x00010004,0x01000004,0x01000004,0x00010004,
toddouska 0:5045d2638c29 79 0x00000000,0x00000404,0x00010404,0x01000000,
toddouska 0:5045d2638c29 80 0x00010000,0x01010404,0x00000004,0x01010000,
toddouska 0:5045d2638c29 81 0x01010400,0x01000000,0x01000000,0x00000400,
toddouska 0:5045d2638c29 82 0x01010004,0x00010000,0x00010400,0x01000004,
toddouska 0:5045d2638c29 83 0x00000400,0x00000004,0x01000404,0x00010404,
toddouska 0:5045d2638c29 84 0x01010404,0x00010004,0x01010000,0x01000404,
toddouska 0:5045d2638c29 85 0x01000004,0x00000404,0x00010404,0x01010400,
toddouska 0:5045d2638c29 86 0x00000404,0x01000400,0x01000400,0x00000000,
toddouska 0:5045d2638c29 87 0x00010004,0x00010400,0x00000000,0x01010004},
toddouska 0:5045d2638c29 88 {
toddouska 0:5045d2638c29 89 0x80108020,0x80008000,0x00008000,0x00108020,
toddouska 0:5045d2638c29 90 0x00100000,0x00000020,0x80100020,0x80008020,
toddouska 0:5045d2638c29 91 0x80000020,0x80108020,0x80108000,0x80000000,
toddouska 0:5045d2638c29 92 0x80008000,0x00100000,0x00000020,0x80100020,
toddouska 0:5045d2638c29 93 0x00108000,0x00100020,0x80008020,0x00000000,
toddouska 0:5045d2638c29 94 0x80000000,0x00008000,0x00108020,0x80100000,
toddouska 0:5045d2638c29 95 0x00100020,0x80000020,0x00000000,0x00108000,
toddouska 0:5045d2638c29 96 0x00008020,0x80108000,0x80100000,0x00008020,
toddouska 0:5045d2638c29 97 0x00000000,0x00108020,0x80100020,0x00100000,
toddouska 0:5045d2638c29 98 0x80008020,0x80100000,0x80108000,0x00008000,
toddouska 0:5045d2638c29 99 0x80100000,0x80008000,0x00000020,0x80108020,
toddouska 0:5045d2638c29 100 0x00108020,0x00000020,0x00008000,0x80000000,
toddouska 0:5045d2638c29 101 0x00008020,0x80108000,0x00100000,0x80000020,
toddouska 0:5045d2638c29 102 0x00100020,0x80008020,0x80000020,0x00100020,
toddouska 0:5045d2638c29 103 0x00108000,0x00000000,0x80008000,0x00008020,
toddouska 0:5045d2638c29 104 0x80000000,0x80100020,0x80108020,0x00108000},
toddouska 0:5045d2638c29 105 {
toddouska 0:5045d2638c29 106 0x00000208,0x08020200,0x00000000,0x08020008,
toddouska 0:5045d2638c29 107 0x08000200,0x00000000,0x00020208,0x08000200,
toddouska 0:5045d2638c29 108 0x00020008,0x08000008,0x08000008,0x00020000,
toddouska 0:5045d2638c29 109 0x08020208,0x00020008,0x08020000,0x00000208,
toddouska 0:5045d2638c29 110 0x08000000,0x00000008,0x08020200,0x00000200,
toddouska 0:5045d2638c29 111 0x00020200,0x08020000,0x08020008,0x00020208,
toddouska 0:5045d2638c29 112 0x08000208,0x00020200,0x00020000,0x08000208,
toddouska 0:5045d2638c29 113 0x00000008,0x08020208,0x00000200,0x08000000,
toddouska 0:5045d2638c29 114 0x08020200,0x08000000,0x00020008,0x00000208,
toddouska 0:5045d2638c29 115 0x00020000,0x08020200,0x08000200,0x00000000,
toddouska 0:5045d2638c29 116 0x00000200,0x00020008,0x08020208,0x08000200,
toddouska 0:5045d2638c29 117 0x08000008,0x00000200,0x00000000,0x08020008,
toddouska 0:5045d2638c29 118 0x08000208,0x00020000,0x08000000,0x08020208,
toddouska 0:5045d2638c29 119 0x00000008,0x00020208,0x00020200,0x08000008,
toddouska 0:5045d2638c29 120 0x08020000,0x08000208,0x00000208,0x08020000,
toddouska 0:5045d2638c29 121 0x00020208,0x00000008,0x08020008,0x00020200},
toddouska 0:5045d2638c29 122 {
toddouska 0:5045d2638c29 123 0x00802001,0x00002081,0x00002081,0x00000080,
toddouska 0:5045d2638c29 124 0x00802080,0x00800081,0x00800001,0x00002001,
toddouska 0:5045d2638c29 125 0x00000000,0x00802000,0x00802000,0x00802081,
toddouska 0:5045d2638c29 126 0x00000081,0x00000000,0x00800080,0x00800001,
toddouska 0:5045d2638c29 127 0x00000001,0x00002000,0x00800000,0x00802001,
toddouska 0:5045d2638c29 128 0x00000080,0x00800000,0x00002001,0x00002080,
toddouska 0:5045d2638c29 129 0x00800081,0x00000001,0x00002080,0x00800080,
toddouska 0:5045d2638c29 130 0x00002000,0x00802080,0x00802081,0x00000081,
toddouska 0:5045d2638c29 131 0x00800080,0x00800001,0x00802000,0x00802081,
toddouska 0:5045d2638c29 132 0x00000081,0x00000000,0x00000000,0x00802000,
toddouska 0:5045d2638c29 133 0x00002080,0x00800080,0x00800081,0x00000001,
toddouska 0:5045d2638c29 134 0x00802001,0x00002081,0x00002081,0x00000080,
toddouska 0:5045d2638c29 135 0x00802081,0x00000081,0x00000001,0x00002000,
toddouska 0:5045d2638c29 136 0x00800001,0x00002001,0x00802080,0x00800081,
toddouska 0:5045d2638c29 137 0x00002001,0x00002080,0x00800000,0x00802001,
toddouska 0:5045d2638c29 138 0x00000080,0x00800000,0x00002000,0x00802080},
toddouska 0:5045d2638c29 139 {
toddouska 0:5045d2638c29 140 0x00000100,0x02080100,0x02080000,0x42000100,
toddouska 0:5045d2638c29 141 0x00080000,0x00000100,0x40000000,0x02080000,
toddouska 0:5045d2638c29 142 0x40080100,0x00080000,0x02000100,0x40080100,
toddouska 0:5045d2638c29 143 0x42000100,0x42080000,0x00080100,0x40000000,
toddouska 0:5045d2638c29 144 0x02000000,0x40080000,0x40080000,0x00000000,
toddouska 0:5045d2638c29 145 0x40000100,0x42080100,0x42080100,0x02000100,
toddouska 0:5045d2638c29 146 0x42080000,0x40000100,0x00000000,0x42000000,
toddouska 0:5045d2638c29 147 0x02080100,0x02000000,0x42000000,0x00080100,
toddouska 0:5045d2638c29 148 0x00080000,0x42000100,0x00000100,0x02000000,
toddouska 0:5045d2638c29 149 0x40000000,0x02080000,0x42000100,0x40080100,
toddouska 0:5045d2638c29 150 0x02000100,0x40000000,0x42080000,0x02080100,
toddouska 0:5045d2638c29 151 0x40080100,0x00000100,0x02000000,0x42080000,
toddouska 0:5045d2638c29 152 0x42080100,0x00080100,0x42000000,0x42080100,
toddouska 0:5045d2638c29 153 0x02080000,0x00000000,0x40080000,0x42000000,
toddouska 0:5045d2638c29 154 0x00080100,0x02000100,0x40000100,0x00080000,
toddouska 0:5045d2638c29 155 0x00000000,0x40080000,0x02080100,0x40000100},
toddouska 0:5045d2638c29 156 {
toddouska 0:5045d2638c29 157 0x20000010,0x20400000,0x00004000,0x20404010,
toddouska 0:5045d2638c29 158 0x20400000,0x00000010,0x20404010,0x00400000,
toddouska 0:5045d2638c29 159 0x20004000,0x00404010,0x00400000,0x20000010,
toddouska 0:5045d2638c29 160 0x00400010,0x20004000,0x20000000,0x00004010,
toddouska 0:5045d2638c29 161 0x00000000,0x00400010,0x20004010,0x00004000,
toddouska 0:5045d2638c29 162 0x00404000,0x20004010,0x00000010,0x20400010,
toddouska 0:5045d2638c29 163 0x20400010,0x00000000,0x00404010,0x20404000,
toddouska 0:5045d2638c29 164 0x00004010,0x00404000,0x20404000,0x20000000,
toddouska 0:5045d2638c29 165 0x20004000,0x00000010,0x20400010,0x00404000,
toddouska 0:5045d2638c29 166 0x20404010,0x00400000,0x00004010,0x20000010,
toddouska 0:5045d2638c29 167 0x00400000,0x20004000,0x20000000,0x00004010,
toddouska 0:5045d2638c29 168 0x20000010,0x20404010,0x00404000,0x20400000,
toddouska 0:5045d2638c29 169 0x00404010,0x20404000,0x00000000,0x20400010,
toddouska 0:5045d2638c29 170 0x00000010,0x00004000,0x20400000,0x00404010,
toddouska 0:5045d2638c29 171 0x00004000,0x00400010,0x20004010,0x00000000,
toddouska 0:5045d2638c29 172 0x20404000,0x20000000,0x00400010,0x20004010},
toddouska 0:5045d2638c29 173 {
toddouska 0:5045d2638c29 174 0x00200000,0x04200002,0x04000802,0x00000000,
toddouska 0:5045d2638c29 175 0x00000800,0x04000802,0x00200802,0x04200800,
toddouska 0:5045d2638c29 176 0x04200802,0x00200000,0x00000000,0x04000002,
toddouska 0:5045d2638c29 177 0x00000002,0x04000000,0x04200002,0x00000802,
toddouska 0:5045d2638c29 178 0x04000800,0x00200802,0x00200002,0x04000800,
toddouska 0:5045d2638c29 179 0x04000002,0x04200000,0x04200800,0x00200002,
toddouska 0:5045d2638c29 180 0x04200000,0x00000800,0x00000802,0x04200802,
toddouska 0:5045d2638c29 181 0x00200800,0x00000002,0x04000000,0x00200800,
toddouska 0:5045d2638c29 182 0x04000000,0x00200800,0x00200000,0x04000802,
toddouska 0:5045d2638c29 183 0x04000802,0x04200002,0x04200002,0x00000002,
toddouska 0:5045d2638c29 184 0x00200002,0x04000000,0x04000800,0x00200000,
toddouska 0:5045d2638c29 185 0x04200800,0x00000802,0x00200802,0x04200800,
toddouska 0:5045d2638c29 186 0x00000802,0x04000002,0x04200802,0x04200000,
toddouska 0:5045d2638c29 187 0x00200800,0x00000000,0x00000002,0x04200802,
toddouska 0:5045d2638c29 188 0x00000000,0x00200802,0x04200000,0x00000800,
toddouska 0:5045d2638c29 189 0x04000002,0x04000800,0x00000800,0x00200002},
toddouska 0:5045d2638c29 190 {
toddouska 0:5045d2638c29 191 0x10001040,0x00001000,0x00040000,0x10041040,
toddouska 0:5045d2638c29 192 0x10000000,0x10001040,0x00000040,0x10000000,
toddouska 0:5045d2638c29 193 0x00040040,0x10040000,0x10041040,0x00041000,
toddouska 0:5045d2638c29 194 0x10041000,0x00041040,0x00001000,0x00000040,
toddouska 0:5045d2638c29 195 0x10040000,0x10000040,0x10001000,0x00001040,
toddouska 0:5045d2638c29 196 0x00041000,0x00040040,0x10040040,0x10041000,
toddouska 0:5045d2638c29 197 0x00001040,0x00000000,0x00000000,0x10040040,
toddouska 0:5045d2638c29 198 0x10000040,0x10001000,0x00041040,0x00040000,
toddouska 0:5045d2638c29 199 0x00041040,0x00040000,0x10041000,0x00001000,
toddouska 0:5045d2638c29 200 0x00000040,0x10040040,0x00001000,0x00041040,
toddouska 0:5045d2638c29 201 0x10001000,0x00000040,0x10000040,0x10040000,
toddouska 0:5045d2638c29 202 0x10040040,0x10000000,0x00040000,0x10001040,
toddouska 0:5045d2638c29 203 0x00000000,0x10041040,0x00040040,0x10000040,
toddouska 0:5045d2638c29 204 0x10040000,0x10001000,0x10001040,0x00000000,
toddouska 0:5045d2638c29 205 0x10041040,0x00041000,0x00041000,0x00001040,
toddouska 0:5045d2638c29 206 0x00001040,0x00040040,0x10000000,0x10041000}
toddouska 0:5045d2638c29 207 };
toddouska 0:5045d2638c29 208
toddouska 0:5045d2638c29 209
toddouska 0:5045d2638c29 210 static INLINE void IPERM(word32* left, word32* right)
toddouska 0:5045d2638c29 211 {
toddouska 0:5045d2638c29 212 word32 work;
toddouska 0:5045d2638c29 213
toddouska 0:5045d2638c29 214 *right = rotlFixed(*right, 4U);
toddouska 0:5045d2638c29 215 work = (*left ^ *right) & 0xf0f0f0f0;
toddouska 0:5045d2638c29 216 *left ^= work;
toddouska 0:5045d2638c29 217
toddouska 0:5045d2638c29 218 *right = rotrFixed(*right^work, 20U);
toddouska 0:5045d2638c29 219 work = (*left ^ *right) & 0xffff0000;
toddouska 0:5045d2638c29 220 *left ^= work;
toddouska 0:5045d2638c29 221
toddouska 0:5045d2638c29 222 *right = rotrFixed(*right^work, 18U);
toddouska 0:5045d2638c29 223 work = (*left ^ *right) & 0x33333333;
toddouska 0:5045d2638c29 224 *left ^= work;
toddouska 0:5045d2638c29 225
toddouska 0:5045d2638c29 226 *right = rotrFixed(*right^work, 6U);
toddouska 0:5045d2638c29 227 work = (*left ^ *right) & 0x00ff00ff;
toddouska 0:5045d2638c29 228 *left ^= work;
toddouska 0:5045d2638c29 229
toddouska 0:5045d2638c29 230 *right = rotlFixed(*right^work, 9U);
toddouska 0:5045d2638c29 231 work = (*left ^ *right) & 0xaaaaaaaa;
toddouska 0:5045d2638c29 232 *left = rotlFixed(*left^work, 1U);
toddouska 0:5045d2638c29 233 *right ^= work;
toddouska 0:5045d2638c29 234 }
toddouska 0:5045d2638c29 235
toddouska 0:5045d2638c29 236
toddouska 0:5045d2638c29 237 static INLINE void FPERM(word32* left, word32* right)
toddouska 0:5045d2638c29 238 {
toddouska 0:5045d2638c29 239 word32 work;
toddouska 0:5045d2638c29 240
toddouska 0:5045d2638c29 241 *right = rotrFixed(*right, 1U);
toddouska 0:5045d2638c29 242 work = (*left ^ *right) & 0xaaaaaaaa;
toddouska 0:5045d2638c29 243 *right ^= work;
toddouska 0:5045d2638c29 244
toddouska 0:5045d2638c29 245 *left = rotrFixed(*left^work, 9U);
toddouska 0:5045d2638c29 246 work = (*left ^ *right) & 0x00ff00ff;
toddouska 0:5045d2638c29 247 *right ^= work;
toddouska 0:5045d2638c29 248
toddouska 0:5045d2638c29 249 *left = rotlFixed(*left^work, 6U);
toddouska 0:5045d2638c29 250 work = (*left ^ *right) & 0x33333333;
toddouska 0:5045d2638c29 251 *right ^= work;
toddouska 0:5045d2638c29 252
toddouska 0:5045d2638c29 253 *left = rotlFixed(*left^work, 18U);
toddouska 0:5045d2638c29 254 work = (*left ^ *right) & 0xffff0000;
toddouska 0:5045d2638c29 255 *right ^= work;
toddouska 0:5045d2638c29 256
toddouska 0:5045d2638c29 257 *left = rotlFixed(*left^work, 20U);
toddouska 0:5045d2638c29 258 work = (*left ^ *right) & 0xf0f0f0f0;
toddouska 0:5045d2638c29 259 *right ^= work;
toddouska 0:5045d2638c29 260
toddouska 0:5045d2638c29 261 *left = rotrFixed(*left^work, 4U);
toddouska 0:5045d2638c29 262 }
toddouska 0:5045d2638c29 263
toddouska 0:5045d2638c29 264
toddouska 0:5045d2638c29 265 static void DesSetKey(const byte* key, int dir, word32* out)
toddouska 0:5045d2638c29 266 {
toddouska 0:5045d2638c29 267 byte buffer[56+56+8];
toddouska 0:5045d2638c29 268 byte *const pc1m = buffer; /* place to modify pc1 into */
toddouska 0:5045d2638c29 269 byte *const pcr = pc1m + 56; /* place to rotate pc1 into */
toddouska 0:5045d2638c29 270 byte *const ks = pcr + 56;
toddouska 0:5045d2638c29 271 register int i,j,l;
toddouska 0:5045d2638c29 272 int m;
toddouska 0:5045d2638c29 273
toddouska 0:5045d2638c29 274 for (j = 0; j < 56; j++) { /* convert pc1 to bits of key */
toddouska 0:5045d2638c29 275 l = pc1[j] - 1; /* integer bit location */
toddouska 0:5045d2638c29 276 m = l & 07; /* find bit */
toddouska 0:5045d2638c29 277 pc1m[j] = (key[l >> 3] & /* find which key byte l is in */
toddouska 0:5045d2638c29 278 bytebit[m]) /* and which bit of that byte */
toddouska 0:5045d2638c29 279 ? 1 : 0; /* and store 1-bit result */
toddouska 0:5045d2638c29 280 }
toddouska 0:5045d2638c29 281 for (i = 0; i < 16; i++) { /* key chunk for each iteration */
toddouska 0:5045d2638c29 282 XMEMSET(ks, 0, 8); /* Clear key schedule */
toddouska 0:5045d2638c29 283 for (j = 0; j < 56; j++) /* rotate pc1 the right amount */
toddouska 0:5045d2638c29 284 pcr[j] = pc1m[(l = j + totrot[i]) < (j < 28 ? 28 : 56) ? l: l-28];
toddouska 0:5045d2638c29 285 /* rotate left and right halves independently */
toddouska 0:5045d2638c29 286 for (j = 0; j < 48; j++){ /* select bits individually */
toddouska 0:5045d2638c29 287 /* check bit that goes to ks[j] */
toddouska 0:5045d2638c29 288 if (pcr[pc2[j] - 1]){
toddouska 0:5045d2638c29 289 /* mask it in if it's there */
toddouska 0:5045d2638c29 290 l= j % 6;
toddouska 0:5045d2638c29 291 ks[j/6] |= bytebit[l] >> 2;
toddouska 0:5045d2638c29 292 }
toddouska 0:5045d2638c29 293 }
toddouska 0:5045d2638c29 294 /* Now convert to odd/even interleaved form for use in F */
toddouska 0:5045d2638c29 295 out[2*i] = ((word32)ks[0] << 24)
toddouska 0:5045d2638c29 296 | ((word32)ks[2] << 16)
toddouska 0:5045d2638c29 297 | ((word32)ks[4] << 8)
toddouska 0:5045d2638c29 298 | ((word32)ks[6]);
toddouska 0:5045d2638c29 299 out[2*i + 1] = ((word32)ks[1] << 24)
toddouska 0:5045d2638c29 300 | ((word32)ks[3] << 16)
toddouska 0:5045d2638c29 301 | ((word32)ks[5] << 8)
toddouska 0:5045d2638c29 302 | ((word32)ks[7]);
toddouska 0:5045d2638c29 303 }
toddouska 0:5045d2638c29 304
toddouska 0:5045d2638c29 305 /* reverse key schedule order */
toddouska 0:5045d2638c29 306 if (dir == DES_DECRYPTION)
toddouska 0:5045d2638c29 307 for (i = 0; i < 16; i += 2) {
toddouska 0:5045d2638c29 308 word32 swap = out[i];
toddouska 0:5045d2638c29 309 out[i] = out[DES_KS_SIZE - 2 - i];
toddouska 0:5045d2638c29 310 out[DES_KS_SIZE - 2 - i] = swap;
toddouska 0:5045d2638c29 311
toddouska 0:5045d2638c29 312 swap = out[i + 1];
toddouska 0:5045d2638c29 313 out[i + 1] = out[DES_KS_SIZE - 1 - i];
toddouska 0:5045d2638c29 314 out[DES_KS_SIZE - 1 - i] = swap;
toddouska 0:5045d2638c29 315 }
toddouska 0:5045d2638c29 316
toddouska 0:5045d2638c29 317 }
toddouska 0:5045d2638c29 318
toddouska 0:5045d2638c29 319
toddouska 0:5045d2638c29 320 static INLINE int Reverse(int dir)
toddouska 0:5045d2638c29 321 {
toddouska 0:5045d2638c29 322 return !dir;
toddouska 0:5045d2638c29 323 }
toddouska 0:5045d2638c29 324
toddouska 0:5045d2638c29 325
toddouska 0:5045d2638c29 326 void Des_SetKey(Des* des, const byte* key, const byte* iv, int dir)
toddouska 0:5045d2638c29 327 {
toddouska 0:5045d2638c29 328 DesSetKey(key, dir, des->key);
toddouska 0:5045d2638c29 329
toddouska 0:5045d2638c29 330 XMEMCPY(des->reg, iv, DES_BLOCK_SIZE);
toddouska 0:5045d2638c29 331 }
toddouska 0:5045d2638c29 332
toddouska 0:5045d2638c29 333
toddouska 0:5045d2638c29 334 void Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir)
toddouska 0:5045d2638c29 335 {
toddouska 0:5045d2638c29 336 DesSetKey(key + (dir == DES_ENCRYPTION ? 0 : 16), dir, des->key[0]);
toddouska 0:5045d2638c29 337 DesSetKey(key + 8, Reverse(dir), des->key[1]);
toddouska 0:5045d2638c29 338 DesSetKey(key + (dir == DES_DECRYPTION ? 0 : 16), dir, des->key[2]);
toddouska 0:5045d2638c29 339
toddouska 0:5045d2638c29 340 XMEMCPY(des->reg, iv, DES_BLOCK_SIZE);
toddouska 0:5045d2638c29 341 }
toddouska 0:5045d2638c29 342
toddouska 0:5045d2638c29 343
toddouska 0:5045d2638c29 344 void DesRawProcessBlock(word32* lIn, word32* rIn, const word32* kptr)
toddouska 0:5045d2638c29 345 {
toddouska 0:5045d2638c29 346 word32 l = *lIn, r = *rIn, i;
toddouska 0:5045d2638c29 347
toddouska 0:5045d2638c29 348 for (i=0; i<8; i++)
toddouska 0:5045d2638c29 349 {
toddouska 0:5045d2638c29 350 word32 work = rotrFixed(r, 4U) ^ kptr[4*i+0];
toddouska 0:5045d2638c29 351 l ^= Spbox[6][(work) & 0x3f]
toddouska 0:5045d2638c29 352 ^ Spbox[4][(work >> 8) & 0x3f]
toddouska 0:5045d2638c29 353 ^ Spbox[2][(work >> 16) & 0x3f]
toddouska 0:5045d2638c29 354 ^ Spbox[0][(work >> 24) & 0x3f];
toddouska 0:5045d2638c29 355 work = r ^ kptr[4*i+1];
toddouska 0:5045d2638c29 356 l ^= Spbox[7][(work) & 0x3f]
toddouska 0:5045d2638c29 357 ^ Spbox[5][(work >> 8) & 0x3f]
toddouska 0:5045d2638c29 358 ^ Spbox[3][(work >> 16) & 0x3f]
toddouska 0:5045d2638c29 359 ^ Spbox[1][(work >> 24) & 0x3f];
toddouska 0:5045d2638c29 360
toddouska 0:5045d2638c29 361 work = rotrFixed(l, 4U) ^ kptr[4*i+2];
toddouska 0:5045d2638c29 362 r ^= Spbox[6][(work) & 0x3f]
toddouska 0:5045d2638c29 363 ^ Spbox[4][(work >> 8) & 0x3f]
toddouska 0:5045d2638c29 364 ^ Spbox[2][(work >> 16) & 0x3f]
toddouska 0:5045d2638c29 365 ^ Spbox[0][(work >> 24) & 0x3f];
toddouska 0:5045d2638c29 366 work = l ^ kptr[4*i+3];
toddouska 0:5045d2638c29 367 r ^= Spbox[7][(work) & 0x3f]
toddouska 0:5045d2638c29 368 ^ Spbox[5][(work >> 8) & 0x3f]
toddouska 0:5045d2638c29 369 ^ Spbox[3][(work >> 16) & 0x3f]
toddouska 0:5045d2638c29 370 ^ Spbox[1][(work >> 24) & 0x3f];
toddouska 0:5045d2638c29 371 }
toddouska 0:5045d2638c29 372
toddouska 0:5045d2638c29 373 *lIn = l; *rIn = r;
toddouska 0:5045d2638c29 374 }
toddouska 0:5045d2638c29 375
toddouska 0:5045d2638c29 376
toddouska 0:5045d2638c29 377 static void DesProcessBlock(Des* des, const byte* in, byte* out)
toddouska 0:5045d2638c29 378 {
toddouska 0:5045d2638c29 379 word32 l, r;
toddouska 0:5045d2638c29 380
toddouska 0:5045d2638c29 381 XMEMCPY(&l, in, sizeof(l));
toddouska 0:5045d2638c29 382 XMEMCPY(&r, in + sizeof(l), sizeof(r));
toddouska 0:5045d2638c29 383 #ifdef LITTLE_ENDIAN_ORDER
toddouska 0:5045d2638c29 384 l = ByteReverseWord32(l);
toddouska 0:5045d2638c29 385 r = ByteReverseWord32(r);
toddouska 0:5045d2638c29 386 #endif
toddouska 0:5045d2638c29 387 IPERM(&l,&r);
toddouska 0:5045d2638c29 388
toddouska 0:5045d2638c29 389 DesRawProcessBlock(&l, &r, des->key);
toddouska 0:5045d2638c29 390
toddouska 0:5045d2638c29 391 FPERM(&l,&r);
toddouska 0:5045d2638c29 392 #ifdef LITTLE_ENDIAN_ORDER
toddouska 0:5045d2638c29 393 l = ByteReverseWord32(l);
toddouska 0:5045d2638c29 394 r = ByteReverseWord32(r);
toddouska 0:5045d2638c29 395 #endif
toddouska 0:5045d2638c29 396 XMEMCPY(out, &r, sizeof(r));
toddouska 0:5045d2638c29 397 XMEMCPY(out + sizeof(r), &l, sizeof(l));
toddouska 0:5045d2638c29 398 }
toddouska 0:5045d2638c29 399
toddouska 0:5045d2638c29 400
toddouska 0:5045d2638c29 401 static void Des3ProcessBlock(Des3* des, const byte* in, byte* out)
toddouska 0:5045d2638c29 402 {
toddouska 0:5045d2638c29 403 word32 l, r;
toddouska 0:5045d2638c29 404
toddouska 0:5045d2638c29 405 XMEMCPY(&l, in, sizeof(l));
toddouska 0:5045d2638c29 406 XMEMCPY(&r, in + sizeof(l), sizeof(r));
toddouska 0:5045d2638c29 407 #ifdef LITTLE_ENDIAN_ORDER
toddouska 0:5045d2638c29 408 l = ByteReverseWord32(l);
toddouska 0:5045d2638c29 409 r = ByteReverseWord32(r);
toddouska 0:5045d2638c29 410 #endif
toddouska 0:5045d2638c29 411 IPERM(&l,&r);
toddouska 0:5045d2638c29 412
toddouska 0:5045d2638c29 413 DesRawProcessBlock(&l, &r, des->key[0]);
toddouska 0:5045d2638c29 414 DesRawProcessBlock(&r, &l, des->key[1]);
toddouska 0:5045d2638c29 415 DesRawProcessBlock(&l, &r, des->key[2]);
toddouska 0:5045d2638c29 416
toddouska 0:5045d2638c29 417 FPERM(&l,&r);
toddouska 0:5045d2638c29 418 #ifdef LITTLE_ENDIAN_ORDER
toddouska 0:5045d2638c29 419 l = ByteReverseWord32(l);
toddouska 0:5045d2638c29 420 r = ByteReverseWord32(r);
toddouska 0:5045d2638c29 421 #endif
toddouska 0:5045d2638c29 422 XMEMCPY(out, &r, sizeof(r));
toddouska 0:5045d2638c29 423 XMEMCPY(out + sizeof(r), &l, sizeof(l));
toddouska 0:5045d2638c29 424 }
toddouska 0:5045d2638c29 425
toddouska 0:5045d2638c29 426
toddouska 0:5045d2638c29 427 void Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz)
toddouska 0:5045d2638c29 428 {
toddouska 0:5045d2638c29 429 word32 blocks = sz / DES_BLOCK_SIZE;
toddouska 0:5045d2638c29 430
toddouska 0:5045d2638c29 431 while (blocks--) {
toddouska 0:5045d2638c29 432 xorbuf((byte*)des->reg, in, DES_BLOCK_SIZE);
toddouska 0:5045d2638c29 433 DesProcessBlock(des, (byte*)des->reg, (byte*)des->reg);
toddouska 0:5045d2638c29 434 XMEMCPY(out, des->reg, DES_BLOCK_SIZE);
toddouska 0:5045d2638c29 435
toddouska 0:5045d2638c29 436 out += DES_BLOCK_SIZE;
toddouska 0:5045d2638c29 437 in += DES_BLOCK_SIZE;
toddouska 0:5045d2638c29 438 }
toddouska 0:5045d2638c29 439 }
toddouska 0:5045d2638c29 440
toddouska 0:5045d2638c29 441
toddouska 0:5045d2638c29 442 void Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz)
toddouska 0:5045d2638c29 443 {
toddouska 0:5045d2638c29 444 word32 blocks = sz / DES_BLOCK_SIZE;
toddouska 0:5045d2638c29 445 byte hold[16];
toddouska 0:5045d2638c29 446
toddouska 0:5045d2638c29 447 while (blocks--) {
toddouska 0:5045d2638c29 448 XMEMCPY(des->tmp, in, DES_BLOCK_SIZE);
toddouska 0:5045d2638c29 449 DesProcessBlock(des, (byte*)des->tmp, out);
toddouska 0:5045d2638c29 450 xorbuf(out, (byte*)des->reg, DES_BLOCK_SIZE);
toddouska 0:5045d2638c29 451
toddouska 0:5045d2638c29 452 XMEMCPY(hold, des->reg, DES_BLOCK_SIZE);
toddouska 0:5045d2638c29 453 XMEMCPY(des->reg, des->tmp, DES_BLOCK_SIZE);
toddouska 0:5045d2638c29 454 XMEMCPY(des->tmp, hold, DES_BLOCK_SIZE);
toddouska 0:5045d2638c29 455
toddouska 0:5045d2638c29 456 out += DES_BLOCK_SIZE;
toddouska 0:5045d2638c29 457 in += DES_BLOCK_SIZE;
toddouska 0:5045d2638c29 458 }
toddouska 0:5045d2638c29 459 }
toddouska 0:5045d2638c29 460
toddouska 0:5045d2638c29 461
toddouska 0:5045d2638c29 462 void Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz)
toddouska 0:5045d2638c29 463 {
toddouska 0:5045d2638c29 464 word32 blocks = sz / DES_BLOCK_SIZE;
toddouska 0:5045d2638c29 465
toddouska 0:5045d2638c29 466 while (blocks--) {
toddouska 0:5045d2638c29 467 xorbuf((byte*)des->reg, in, DES_BLOCK_SIZE);
toddouska 0:5045d2638c29 468 Des3ProcessBlock(des, (byte*)des->reg, (byte*)des->reg);
toddouska 0:5045d2638c29 469 XMEMCPY(out, des->reg, DES_BLOCK_SIZE);
toddouska 0:5045d2638c29 470
toddouska 0:5045d2638c29 471 out += DES_BLOCK_SIZE;
toddouska 0:5045d2638c29 472 in += DES_BLOCK_SIZE;
toddouska 0:5045d2638c29 473 }
toddouska 0:5045d2638c29 474 }
toddouska 0:5045d2638c29 475
toddouska 0:5045d2638c29 476
toddouska 0:5045d2638c29 477 void Des3_CbcDecrypt(Des3* des, byte* out, const byte* in, word32 sz)
toddouska 0:5045d2638c29 478 {
toddouska 0:5045d2638c29 479 word32 blocks = sz / DES_BLOCK_SIZE;
toddouska 0:5045d2638c29 480
toddouska 0:5045d2638c29 481 while (blocks--) {
toddouska 0:5045d2638c29 482 XMEMCPY(des->tmp, in, DES_BLOCK_SIZE);
toddouska 0:5045d2638c29 483 Des3ProcessBlock(des, (byte*)des->tmp, out);
toddouska 0:5045d2638c29 484 xorbuf(out, (byte*)des->reg, DES_BLOCK_SIZE);
toddouska 0:5045d2638c29 485 XMEMCPY(des->reg, des->tmp, DES_BLOCK_SIZE);
toddouska 0:5045d2638c29 486
toddouska 0:5045d2638c29 487 out += DES_BLOCK_SIZE;
toddouska 0:5045d2638c29 488 in += DES_BLOCK_SIZE;
toddouska 0:5045d2638c29 489 }
toddouska 0:5045d2638c29 490 }
toddouska 0:5045d2638c29 491
toddouska 0:5045d2638c29 492
toddouska 0:5045d2638c29 493 #endif /* NO_DES3 */