Webserver+3d print

Dependents:   Nucleo

Committer:
Sergunb
Date:
Sat Feb 04 18:15:49 2017 +0000
Revision:
0:8918a71cdbe9
nothing else

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sergunb 0:8918a71cdbe9 1 /**
Sergunb 0:8918a71cdbe9 2 * @file des.c
Sergunb 0:8918a71cdbe9 3 * @brief DES (Data Encryption Standard)
Sergunb 0:8918a71cdbe9 4 *
Sergunb 0:8918a71cdbe9 5 * @section License
Sergunb 0:8918a71cdbe9 6 *
Sergunb 0:8918a71cdbe9 7 * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved.
Sergunb 0:8918a71cdbe9 8 *
Sergunb 0:8918a71cdbe9 9 * This file is part of CycloneCrypto Open.
Sergunb 0:8918a71cdbe9 10 *
Sergunb 0:8918a71cdbe9 11 * This program is free software; you can redistribute it and/or
Sergunb 0:8918a71cdbe9 12 * modify it under the terms of the GNU General Public License
Sergunb 0:8918a71cdbe9 13 * as published by the Free Software Foundation; either version 2
Sergunb 0:8918a71cdbe9 14 * of the License, or (at your option) any later version.
Sergunb 0:8918a71cdbe9 15 *
Sergunb 0:8918a71cdbe9 16 * This program is distributed in the hope that it will be useful,
Sergunb 0:8918a71cdbe9 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Sergunb 0:8918a71cdbe9 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Sergunb 0:8918a71cdbe9 19 * GNU General Public License for more details.
Sergunb 0:8918a71cdbe9 20 *
Sergunb 0:8918a71cdbe9 21 * You should have received a copy of the GNU General Public License
Sergunb 0:8918a71cdbe9 22 * along with this program; if not, write to the Free Software Foundation,
Sergunb 0:8918a71cdbe9 23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Sergunb 0:8918a71cdbe9 24 *
Sergunb 0:8918a71cdbe9 25 * @section Description
Sergunb 0:8918a71cdbe9 26 *
Sergunb 0:8918a71cdbe9 27 * DES is an encryption algorithm designed to encipher and decipher blocks of
Sergunb 0:8918a71cdbe9 28 * 64 bits under control of a 64-bit key. Refer to FIPS 46-3 for more details
Sergunb 0:8918a71cdbe9 29 *
Sergunb 0:8918a71cdbe9 30 * @author Oryx Embedded SARL (www.oryx-embedded.com)
Sergunb 0:8918a71cdbe9 31 * @version 1.7.6
Sergunb 0:8918a71cdbe9 32 **/
Sergunb 0:8918a71cdbe9 33
Sergunb 0:8918a71cdbe9 34 //Switch to the appropriate trace level
Sergunb 0:8918a71cdbe9 35 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
Sergunb 0:8918a71cdbe9 36
Sergunb 0:8918a71cdbe9 37 //Dependencies
Sergunb 0:8918a71cdbe9 38 #include <string.h>
Sergunb 0:8918a71cdbe9 39 #include "crypto.h"
Sergunb 0:8918a71cdbe9 40 #include "des.h"
Sergunb 0:8918a71cdbe9 41
Sergunb 0:8918a71cdbe9 42 //Check crypto library configuration
Sergunb 0:8918a71cdbe9 43 #if (DES_SUPPORT == ENABLED || DES3_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 44
Sergunb 0:8918a71cdbe9 45 //Rotate left operation
Sergunb 0:8918a71cdbe9 46 #define ROL28(a, n) ((((a) << (n)) | ((a) >> (28 - (n)))) & 0x0FFFFFFF)
Sergunb 0:8918a71cdbe9 47
Sergunb 0:8918a71cdbe9 48 //Initial permutation
Sergunb 0:8918a71cdbe9 49 #define DES_IP(left, right) \
Sergunb 0:8918a71cdbe9 50 { \
Sergunb 0:8918a71cdbe9 51 temp = ((left >> 4) ^ right) & 0x0F0F0F0F; \
Sergunb 0:8918a71cdbe9 52 right ^= temp; \
Sergunb 0:8918a71cdbe9 53 left ^= temp << 4; \
Sergunb 0:8918a71cdbe9 54 temp = ((left >> 16) ^ right) & 0x0000FFFF; \
Sergunb 0:8918a71cdbe9 55 right ^= temp; \
Sergunb 0:8918a71cdbe9 56 left ^= temp << 16; \
Sergunb 0:8918a71cdbe9 57 temp = ((right >> 2) ^ left) & 0x33333333; \
Sergunb 0:8918a71cdbe9 58 left ^= temp; \
Sergunb 0:8918a71cdbe9 59 right ^= temp << 2; \
Sergunb 0:8918a71cdbe9 60 temp = ((right >> 8) ^ left) & 0x00FF00FF; \
Sergunb 0:8918a71cdbe9 61 left ^= temp; \
Sergunb 0:8918a71cdbe9 62 right ^= temp << 8; \
Sergunb 0:8918a71cdbe9 63 temp = ((left >> 1) ^ right) & 0x55555555; \
Sergunb 0:8918a71cdbe9 64 right ^= temp; \
Sergunb 0:8918a71cdbe9 65 left ^= temp << 1; \
Sergunb 0:8918a71cdbe9 66 left = ROL32(left, 1); \
Sergunb 0:8918a71cdbe9 67 right = ROL32(right, 1); \
Sergunb 0:8918a71cdbe9 68 }
Sergunb 0:8918a71cdbe9 69
Sergunb 0:8918a71cdbe9 70 //Final permutation
Sergunb 0:8918a71cdbe9 71 #define DES_FP(left, right) \
Sergunb 0:8918a71cdbe9 72 { \
Sergunb 0:8918a71cdbe9 73 left = ROR32(left, 1); \
Sergunb 0:8918a71cdbe9 74 right = ROR32(right, 1); \
Sergunb 0:8918a71cdbe9 75 temp = ((left >> 1) ^ right) & 0x55555555; \
Sergunb 0:8918a71cdbe9 76 right ^= temp; \
Sergunb 0:8918a71cdbe9 77 left ^= temp << 1; \
Sergunb 0:8918a71cdbe9 78 temp = ((right >> 8) ^ left) & 0x00FF00FF; \
Sergunb 0:8918a71cdbe9 79 left ^= temp; \
Sergunb 0:8918a71cdbe9 80 right ^= temp << 8; \
Sergunb 0:8918a71cdbe9 81 temp = ((right >> 2) ^ left) & 0x33333333; \
Sergunb 0:8918a71cdbe9 82 left ^= temp; \
Sergunb 0:8918a71cdbe9 83 right ^= temp << 2; \
Sergunb 0:8918a71cdbe9 84 temp = ((left >> 16) ^ right) & 0x0000FFFF; \
Sergunb 0:8918a71cdbe9 85 right ^= temp; \
Sergunb 0:8918a71cdbe9 86 left ^= temp << 16; \
Sergunb 0:8918a71cdbe9 87 temp = ((left >> 4) ^ right) & 0x0F0F0F0F; \
Sergunb 0:8918a71cdbe9 88 right ^= temp; \
Sergunb 0:8918a71cdbe9 89 left ^= temp << 4; \
Sergunb 0:8918a71cdbe9 90 }
Sergunb 0:8918a71cdbe9 91
Sergunb 0:8918a71cdbe9 92 //DES round
Sergunb 0:8918a71cdbe9 93 #define DES_ROUND(left, right, ks) \
Sergunb 0:8918a71cdbe9 94 { \
Sergunb 0:8918a71cdbe9 95 temp = right ^ *(ks); \
Sergunb 0:8918a71cdbe9 96 left ^= sp2[(temp >> 24) & 0x3F]; \
Sergunb 0:8918a71cdbe9 97 left ^= sp4[(temp >> 16) & 0x3F]; \
Sergunb 0:8918a71cdbe9 98 left ^= sp6[(temp >> 8) & 0x3F]; \
Sergunb 0:8918a71cdbe9 99 left ^= sp8[temp & 0x3F]; \
Sergunb 0:8918a71cdbe9 100 temp = ROR32(right, 4) ^ *(ks + 1); \
Sergunb 0:8918a71cdbe9 101 left ^= sp1[(temp >> 24) & 0x3F]; \
Sergunb 0:8918a71cdbe9 102 left ^= sp3[(temp >> 16) & 0x3F]; \
Sergunb 0:8918a71cdbe9 103 left ^= sp5[(temp >> 8) & 0x3F]; \
Sergunb 0:8918a71cdbe9 104 left ^= sp7[temp & 0x3F]; \
Sergunb 0:8918a71cdbe9 105 temp = right; \
Sergunb 0:8918a71cdbe9 106 right = left; \
Sergunb 0:8918a71cdbe9 107 left = temp; \
Sergunb 0:8918a71cdbe9 108 }
Sergunb 0:8918a71cdbe9 109
Sergunb 0:8918a71cdbe9 110 //Permuted choice 1
Sergunb 0:8918a71cdbe9 111 #define DES_PC1(left, right) \
Sergunb 0:8918a71cdbe9 112 { \
Sergunb 0:8918a71cdbe9 113 uint32_t temp; \
Sergunb 0:8918a71cdbe9 114 temp = ((left >> 4) ^ right) & 0x0F0F0F0F; \
Sergunb 0:8918a71cdbe9 115 right ^= temp; \
Sergunb 0:8918a71cdbe9 116 left ^= (temp << 4); \
Sergunb 0:8918a71cdbe9 117 temp = ((right >> 16) ^ left) & 0x0000FFFF; \
Sergunb 0:8918a71cdbe9 118 left ^= temp; \
Sergunb 0:8918a71cdbe9 119 right ^= (temp << 16); \
Sergunb 0:8918a71cdbe9 120 temp = ((left >> 2) ^ right) & 0x33333333; \
Sergunb 0:8918a71cdbe9 121 right ^= temp; \
Sergunb 0:8918a71cdbe9 122 left ^= (temp << 2); \
Sergunb 0:8918a71cdbe9 123 temp = ((right >> 16) ^ left) & 0x0000FFFF; \
Sergunb 0:8918a71cdbe9 124 left ^= temp; \
Sergunb 0:8918a71cdbe9 125 right ^= (temp << 16); \
Sergunb 0:8918a71cdbe9 126 temp = ((left >> 1) ^ right) & 0x55555555; \
Sergunb 0:8918a71cdbe9 127 right ^= temp; \
Sergunb 0:8918a71cdbe9 128 left ^= (temp << 1); \
Sergunb 0:8918a71cdbe9 129 temp = ((right >> 8) ^ left) & 0x00FF00FF; \
Sergunb 0:8918a71cdbe9 130 left ^= temp; \
Sergunb 0:8918a71cdbe9 131 right ^= (temp << 8); \
Sergunb 0:8918a71cdbe9 132 temp = ((left >> 1) ^ right) & 0x55555555; \
Sergunb 0:8918a71cdbe9 133 right ^= temp; \
Sergunb 0:8918a71cdbe9 134 left ^= (temp << 1); \
Sergunb 0:8918a71cdbe9 135 temp = (left << 8) | ((right >> 20) & 0x000000F0); \
Sergunb 0:8918a71cdbe9 136 left = ((right << 20) & 0x0FF00000); \
Sergunb 0:8918a71cdbe9 137 left |= ((right << 4) & 0x000FF000); \
Sergunb 0:8918a71cdbe9 138 left |= ((right >> 12) & 0x00000FF0); \
Sergunb 0:8918a71cdbe9 139 left |= ((right >> 28) & 0x0000000F); \
Sergunb 0:8918a71cdbe9 140 right = temp >> 4; \
Sergunb 0:8918a71cdbe9 141 }
Sergunb 0:8918a71cdbe9 142
Sergunb 0:8918a71cdbe9 143 //Selection function 1
Sergunb 0:8918a71cdbe9 144 static const uint32_t sp1[64] =
Sergunb 0:8918a71cdbe9 145 {
Sergunb 0:8918a71cdbe9 146 0x01010400, 0x00000000, 0x00010000, 0x01010404, 0x01010004, 0x00010404, 0x00000004, 0x00010000,
Sergunb 0:8918a71cdbe9 147 0x00000400, 0x01010400, 0x01010404, 0x00000400, 0x01000404, 0x01010004, 0x01000000, 0x00000004,
Sergunb 0:8918a71cdbe9 148 0x00000404, 0x01000400, 0x01000400, 0x00010400, 0x00010400, 0x01010000, 0x01010000, 0x01000404,
Sergunb 0:8918a71cdbe9 149 0x00010004, 0x01000004, 0x01000004, 0x00010004, 0x00000000, 0x00000404, 0x00010404, 0x01000000,
Sergunb 0:8918a71cdbe9 150 0x00010000, 0x01010404, 0x00000004, 0x01010000, 0x01010400, 0x01000000, 0x01000000, 0x00000400,
Sergunb 0:8918a71cdbe9 151 0x01010004, 0x00010000, 0x00010400, 0x01000004, 0x00000400, 0x00000004, 0x01000404, 0x00010404,
Sergunb 0:8918a71cdbe9 152 0x01010404, 0x00010004, 0x01010000, 0x01000404, 0x01000004, 0x00000404, 0x00010404, 0x01010400,
Sergunb 0:8918a71cdbe9 153 0x00000404, 0x01000400, 0x01000400, 0x00000000, 0x00010004, 0x00010400, 0x00000000, 0x01010004
Sergunb 0:8918a71cdbe9 154 };
Sergunb 0:8918a71cdbe9 155
Sergunb 0:8918a71cdbe9 156 //Selection function 2
Sergunb 0:8918a71cdbe9 157 static const uint32_t sp2[64] =
Sergunb 0:8918a71cdbe9 158 {
Sergunb 0:8918a71cdbe9 159 0x80108020, 0x80008000, 0x00008000, 0x00108020, 0x00100000, 0x00000020, 0x80100020, 0x80008020,
Sergunb 0:8918a71cdbe9 160 0x80000020, 0x80108020, 0x80108000, 0x80000000, 0x80008000, 0x00100000, 0x00000020, 0x80100020,
Sergunb 0:8918a71cdbe9 161 0x00108000, 0x00100020, 0x80008020, 0x00000000, 0x80000000, 0x00008000, 0x00108020, 0x80100000,
Sergunb 0:8918a71cdbe9 162 0x00100020, 0x80000020, 0x00000000, 0x00108000, 0x00008020, 0x80108000, 0x80100000, 0x00008020,
Sergunb 0:8918a71cdbe9 163 0x00000000, 0x00108020, 0x80100020, 0x00100000, 0x80008020, 0x80100000, 0x80108000, 0x00008000,
Sergunb 0:8918a71cdbe9 164 0x80100000, 0x80008000, 0x00000020, 0x80108020, 0x00108020, 0x00000020, 0x00008000, 0x80000000,
Sergunb 0:8918a71cdbe9 165 0x00008020, 0x80108000, 0x00100000, 0x80000020, 0x00100020, 0x80008020, 0x80000020, 0x00100020,
Sergunb 0:8918a71cdbe9 166 0x00108000, 0x00000000, 0x80008000, 0x00008020, 0x80000000, 0x80100020, 0x80108020, 0x00108000
Sergunb 0:8918a71cdbe9 167 };
Sergunb 0:8918a71cdbe9 168
Sergunb 0:8918a71cdbe9 169 //Selection function 3
Sergunb 0:8918a71cdbe9 170 static const uint32_t sp3[64] =
Sergunb 0:8918a71cdbe9 171 {
Sergunb 0:8918a71cdbe9 172 0x00000208, 0x08020200, 0x00000000, 0x08020008, 0x08000200, 0x00000000, 0x00020208, 0x08000200,
Sergunb 0:8918a71cdbe9 173 0x00020008, 0x08000008, 0x08000008, 0x00020000, 0x08020208, 0x00020008, 0x08020000, 0x00000208,
Sergunb 0:8918a71cdbe9 174 0x08000000, 0x00000008, 0x08020200, 0x00000200, 0x00020200, 0x08020000, 0x08020008, 0x00020208,
Sergunb 0:8918a71cdbe9 175 0x08000208, 0x00020200, 0x00020000, 0x08000208, 0x00000008, 0x08020208, 0x00000200, 0x08000000,
Sergunb 0:8918a71cdbe9 176 0x08020200, 0x08000000, 0x00020008, 0x00000208, 0x00020000, 0x08020200, 0x08000200, 0x00000000,
Sergunb 0:8918a71cdbe9 177 0x00000200, 0x00020008, 0x08020208, 0x08000200, 0x08000008, 0x00000200, 0x00000000, 0x08020008,
Sergunb 0:8918a71cdbe9 178 0x08000208, 0x00020000, 0x08000000, 0x08020208, 0x00000008, 0x00020208, 0x00020200, 0x08000008,
Sergunb 0:8918a71cdbe9 179 0x08020000, 0x08000208, 0x00000208, 0x08020000, 0x00020208, 0x00000008, 0x08020008, 0x00020200
Sergunb 0:8918a71cdbe9 180 };
Sergunb 0:8918a71cdbe9 181
Sergunb 0:8918a71cdbe9 182 //Selection function 4
Sergunb 0:8918a71cdbe9 183 static const uint32_t sp4[64] =
Sergunb 0:8918a71cdbe9 184 {
Sergunb 0:8918a71cdbe9 185 0x00802001, 0x00002081, 0x00002081, 0x00000080, 0x00802080, 0x00800081, 0x00800001, 0x00002001,
Sergunb 0:8918a71cdbe9 186 0x00000000, 0x00802000, 0x00802000, 0x00802081, 0x00000081, 0x00000000, 0x00800080, 0x00800001,
Sergunb 0:8918a71cdbe9 187 0x00000001, 0x00002000, 0x00800000, 0x00802001, 0x00000080, 0x00800000, 0x00002001, 0x00002080,
Sergunb 0:8918a71cdbe9 188 0x00800081, 0x00000001, 0x00002080, 0x00800080, 0x00002000, 0x00802080, 0x00802081, 0x00000081,
Sergunb 0:8918a71cdbe9 189 0x00800080, 0x00800001, 0x00802000, 0x00802081, 0x00000081, 0x00000000, 0x00000000, 0x00802000,
Sergunb 0:8918a71cdbe9 190 0x00002080, 0x00800080, 0x00800081, 0x00000001, 0x00802001, 0x00002081, 0x00002081, 0x00000080,
Sergunb 0:8918a71cdbe9 191 0x00802081, 0x00000081, 0x00000001, 0x00002000, 0x00800001, 0x00002001, 0x00802080, 0x00800081,
Sergunb 0:8918a71cdbe9 192 0x00002001, 0x00002080, 0x00800000, 0x00802001, 0x00000080, 0x00800000, 0x00002000, 0x00802080
Sergunb 0:8918a71cdbe9 193 };
Sergunb 0:8918a71cdbe9 194
Sergunb 0:8918a71cdbe9 195 //Selection function 5
Sergunb 0:8918a71cdbe9 196 static const uint32_t sp5[64] =
Sergunb 0:8918a71cdbe9 197 {
Sergunb 0:8918a71cdbe9 198 0x00000100, 0x02080100, 0x02080000, 0x42000100, 0x00080000, 0x00000100, 0x40000000, 0x02080000,
Sergunb 0:8918a71cdbe9 199 0x40080100, 0x00080000, 0x02000100, 0x40080100, 0x42000100, 0x42080000, 0x00080100, 0x40000000,
Sergunb 0:8918a71cdbe9 200 0x02000000, 0x40080000, 0x40080000, 0x00000000, 0x40000100, 0x42080100, 0x42080100, 0x02000100,
Sergunb 0:8918a71cdbe9 201 0x42080000, 0x40000100, 0x00000000, 0x42000000, 0x02080100, 0x02000000, 0x42000000, 0x00080100,
Sergunb 0:8918a71cdbe9 202 0x00080000, 0x42000100, 0x00000100, 0x02000000, 0x40000000, 0x02080000, 0x42000100, 0x40080100,
Sergunb 0:8918a71cdbe9 203 0x02000100, 0x40000000, 0x42080000, 0x02080100, 0x40080100, 0x00000100, 0x02000000, 0x42080000,
Sergunb 0:8918a71cdbe9 204 0x42080100, 0x00080100, 0x42000000, 0x42080100, 0x02080000, 0x00000000, 0x40080000, 0x42000000,
Sergunb 0:8918a71cdbe9 205 0x00080100, 0x02000100, 0x40000100, 0x00080000, 0x00000000, 0x40080000, 0x02080100, 0x40000100
Sergunb 0:8918a71cdbe9 206 };
Sergunb 0:8918a71cdbe9 207
Sergunb 0:8918a71cdbe9 208 //Selection function 6
Sergunb 0:8918a71cdbe9 209 static const uint32_t sp6[64] =
Sergunb 0:8918a71cdbe9 210 {
Sergunb 0:8918a71cdbe9 211 0x20000010, 0x20400000, 0x00004000, 0x20404010, 0x20400000, 0x00000010, 0x20404010, 0x00400000,
Sergunb 0:8918a71cdbe9 212 0x20004000, 0x00404010, 0x00400000, 0x20000010, 0x00400010, 0x20004000, 0x20000000, 0x00004010,
Sergunb 0:8918a71cdbe9 213 0x00000000, 0x00400010, 0x20004010, 0x00004000, 0x00404000, 0x20004010, 0x00000010, 0x20400010,
Sergunb 0:8918a71cdbe9 214 0x20400010, 0x00000000, 0x00404010, 0x20404000, 0x00004010, 0x00404000, 0x20404000, 0x20000000,
Sergunb 0:8918a71cdbe9 215 0x20004000, 0x00000010, 0x20400010, 0x00404000, 0x20404010, 0x00400000, 0x00004010, 0x20000010,
Sergunb 0:8918a71cdbe9 216 0x00400000, 0x20004000, 0x20000000, 0x00004010, 0x20000010, 0x20404010, 0x00404000, 0x20400000,
Sergunb 0:8918a71cdbe9 217 0x00404010, 0x20404000, 0x00000000, 0x20400010, 0x00000010, 0x00004000, 0x20400000, 0x00404010,
Sergunb 0:8918a71cdbe9 218 0x00004000, 0x00400010, 0x20004010, 0x00000000, 0x20404000, 0x20000000, 0x00400010, 0x20004010
Sergunb 0:8918a71cdbe9 219 };
Sergunb 0:8918a71cdbe9 220
Sergunb 0:8918a71cdbe9 221 //Selection function 7
Sergunb 0:8918a71cdbe9 222 static const uint32_t sp7[64] =
Sergunb 0:8918a71cdbe9 223 {
Sergunb 0:8918a71cdbe9 224 0x00200000, 0x04200002, 0x04000802, 0x00000000, 0x00000800, 0x04000802, 0x00200802, 0x04200800,
Sergunb 0:8918a71cdbe9 225 0x04200802, 0x00200000, 0x00000000, 0x04000002, 0x00000002, 0x04000000, 0x04200002, 0x00000802,
Sergunb 0:8918a71cdbe9 226 0x04000800, 0x00200802, 0x00200002, 0x04000800, 0x04000002, 0x04200000, 0x04200800, 0x00200002,
Sergunb 0:8918a71cdbe9 227 0x04200000, 0x00000800, 0x00000802, 0x04200802, 0x00200800, 0x00000002, 0x04000000, 0x00200800,
Sergunb 0:8918a71cdbe9 228 0x04000000, 0x00200800, 0x00200000, 0x04000802, 0x04000802, 0x04200002, 0x04200002, 0x00000002,
Sergunb 0:8918a71cdbe9 229 0x00200002, 0x04000000, 0x04000800, 0x00200000, 0x04200800, 0x00000802, 0x00200802, 0x04200800,
Sergunb 0:8918a71cdbe9 230 0x00000802, 0x04000002, 0x04200802, 0x04200000, 0x00200800, 0x00000000, 0x00000002, 0x04200802,
Sergunb 0:8918a71cdbe9 231 0x00000000, 0x00200802, 0x04200000, 0x00000800, 0x04000002, 0x04000800, 0x00000800, 0x00200002
Sergunb 0:8918a71cdbe9 232 };
Sergunb 0:8918a71cdbe9 233
Sergunb 0:8918a71cdbe9 234 //Selection function 8
Sergunb 0:8918a71cdbe9 235 static const uint32_t sp8[64] =
Sergunb 0:8918a71cdbe9 236 {
Sergunb 0:8918a71cdbe9 237 0x10001040, 0x00001000, 0x00040000, 0x10041040, 0x10000000, 0x10001040, 0x00000040, 0x10000000,
Sergunb 0:8918a71cdbe9 238 0x00040040, 0x10040000, 0x10041040, 0x00041000, 0x10041000, 0x00041040, 0x00001000, 0x00000040,
Sergunb 0:8918a71cdbe9 239 0x10040000, 0x10000040, 0x10001000, 0x00001040, 0x00041000, 0x00040040, 0x10040040, 0x10041000,
Sergunb 0:8918a71cdbe9 240 0x00001040, 0x00000000, 0x00000000, 0x10040040, 0x10000040, 0x10001000, 0x00041040, 0x00040000,
Sergunb 0:8918a71cdbe9 241 0x00041040, 0x00040000, 0x10041000, 0x00001000, 0x00000040, 0x10040040, 0x00001000, 0x00041040,
Sergunb 0:8918a71cdbe9 242 0x10001000, 0x00000040, 0x10000040, 0x10040000, 0x10040040, 0x10000000, 0x00040000, 0x10001040,
Sergunb 0:8918a71cdbe9 243 0x00000000, 0x10041040, 0x00040040, 0x10000040, 0x10040000, 0x10001000, 0x10001040, 0x00000000,
Sergunb 0:8918a71cdbe9 244 0x10041040, 0x00041000, 0x00041000, 0x00001040, 0x00001040, 0x00040040, 0x10000000, 0x10041000
Sergunb 0:8918a71cdbe9 245 };
Sergunb 0:8918a71cdbe9 246
Sergunb 0:8918a71cdbe9 247 //Common interface for encryption algorithms
Sergunb 0:8918a71cdbe9 248 const CipherAlgo desCipherAlgo =
Sergunb 0:8918a71cdbe9 249 {
Sergunb 0:8918a71cdbe9 250 "DES",
Sergunb 0:8918a71cdbe9 251 sizeof(DesContext),
Sergunb 0:8918a71cdbe9 252 CIPHER_ALGO_TYPE_BLOCK,
Sergunb 0:8918a71cdbe9 253 DES_BLOCK_SIZE,
Sergunb 0:8918a71cdbe9 254 (CipherAlgoInit) desInit,
Sergunb 0:8918a71cdbe9 255 NULL,
Sergunb 0:8918a71cdbe9 256 NULL,
Sergunb 0:8918a71cdbe9 257 (CipherAlgoEncryptBlock) desEncryptBlock,
Sergunb 0:8918a71cdbe9 258 (CipherAlgoDecryptBlock) desDecryptBlock
Sergunb 0:8918a71cdbe9 259 };
Sergunb 0:8918a71cdbe9 260
Sergunb 0:8918a71cdbe9 261
Sergunb 0:8918a71cdbe9 262 /**
Sergunb 0:8918a71cdbe9 263 * @brief Initialize a DES context using the supplied key
Sergunb 0:8918a71cdbe9 264 * @param[in] context Pointer to the DES context to initialize
Sergunb 0:8918a71cdbe9 265 * @param[in] key Pointer to the key
Sergunb 0:8918a71cdbe9 266 * @param[in] keyLength Length of the key (must be set to 8)
Sergunb 0:8918a71cdbe9 267 * @return Error code
Sergunb 0:8918a71cdbe9 268 **/
Sergunb 0:8918a71cdbe9 269
Sergunb 0:8918a71cdbe9 270 error_t desInit(DesContext *context, const uint8_t *key, size_t keyLength)
Sergunb 0:8918a71cdbe9 271 {
Sergunb 0:8918a71cdbe9 272 uint_t i;
Sergunb 0:8918a71cdbe9 273 uint32_t c;
Sergunb 0:8918a71cdbe9 274 uint32_t d;
Sergunb 0:8918a71cdbe9 275
Sergunb 0:8918a71cdbe9 276 //Check key length
Sergunb 0:8918a71cdbe9 277 if(keyLength != 8)
Sergunb 0:8918a71cdbe9 278 return ERROR_INVALID_KEY_LENGTH;
Sergunb 0:8918a71cdbe9 279
Sergunb 0:8918a71cdbe9 280 //Copy the key
Sergunb 0:8918a71cdbe9 281 c = LOAD32BE(key + 0);
Sergunb 0:8918a71cdbe9 282 d = LOAD32BE(key + 4);
Sergunb 0:8918a71cdbe9 283
Sergunb 0:8918a71cdbe9 284 //Permuted choice 1
Sergunb 0:8918a71cdbe9 285 DES_PC1(c, d);
Sergunb 0:8918a71cdbe9 286
Sergunb 0:8918a71cdbe9 287 //Generate the key schedule
Sergunb 0:8918a71cdbe9 288 for(i = 0; i < 16; i++)
Sergunb 0:8918a71cdbe9 289 {
Sergunb 0:8918a71cdbe9 290 //Individual blocks are shifted left
Sergunb 0:8918a71cdbe9 291 if(i == 0 || i == 1 || i == 8 || i == 15)
Sergunb 0:8918a71cdbe9 292 {
Sergunb 0:8918a71cdbe9 293 c = ROL28(c, 1);
Sergunb 0:8918a71cdbe9 294 d = ROL28(d, 1);
Sergunb 0:8918a71cdbe9 295 }
Sergunb 0:8918a71cdbe9 296 else
Sergunb 0:8918a71cdbe9 297 {
Sergunb 0:8918a71cdbe9 298 c = ROL28(c, 2);
Sergunb 0:8918a71cdbe9 299 d = ROL28(d, 2);
Sergunb 0:8918a71cdbe9 300 }
Sergunb 0:8918a71cdbe9 301
Sergunb 0:8918a71cdbe9 302 //Permuted choice 2
Sergunb 0:8918a71cdbe9 303 context->ks[2 * i] =
Sergunb 0:8918a71cdbe9 304 ((c << 4) & 0x24000000) | ((c << 28) & 0x10000000) |
Sergunb 0:8918a71cdbe9 305 ((c << 14) & 0x08000000) | ((c << 18) & 0x02080000) |
Sergunb 0:8918a71cdbe9 306 ((c << 6) & 0x01000000) | ((c << 9) & 0x00200000) |
Sergunb 0:8918a71cdbe9 307 ((c >> 1) & 0x00100000) | ((c << 10) & 0x00040000) |
Sergunb 0:8918a71cdbe9 308 ((c << 2) & 0x00020000) | ((c >> 10) & 0x00010000) |
Sergunb 0:8918a71cdbe9 309 ((d >> 13) & 0x00002000) | ((d >> 4) & 0x00001000) |
Sergunb 0:8918a71cdbe9 310 ((d << 6) & 0x00000800) | ((d >> 1) & 0x00000400) |
Sergunb 0:8918a71cdbe9 311 ((d >> 14) & 0x00000200) | ((d) & 0x00000100) |
Sergunb 0:8918a71cdbe9 312 ((d >> 5) & 0x00000020) | ((d >> 10) & 0x00000010) |
Sergunb 0:8918a71cdbe9 313 ((d >> 3) & 0x00000008) | ((d >> 18) & 0x00000004) |
Sergunb 0:8918a71cdbe9 314 ((d >> 26) & 0x00000002) | ((d >> 24) & 0x00000001);
Sergunb 0:8918a71cdbe9 315
Sergunb 0:8918a71cdbe9 316 context->ks[2 * i + 1] =
Sergunb 0:8918a71cdbe9 317 ((c << 15) & 0x20000000) | ((c << 17) & 0x10000000) |
Sergunb 0:8918a71cdbe9 318 ((c << 10) & 0x08000000) | ((c << 22) & 0x04000000) |
Sergunb 0:8918a71cdbe9 319 ((c >> 2) & 0x02000000) | ((c << 1) & 0x01000000) |
Sergunb 0:8918a71cdbe9 320 ((c << 16) & 0x00200000) | ((c << 11) & 0x00100000) |
Sergunb 0:8918a71cdbe9 321 ((c << 3) & 0x00080000) | ((c >> 6) & 0x00040000) |
Sergunb 0:8918a71cdbe9 322 ((c << 15) & 0x00020000) | ((c >> 4) & 0x00010000) |
Sergunb 0:8918a71cdbe9 323 ((d >> 2) & 0x00002000) | ((d << 8) & 0x00001000) |
Sergunb 0:8918a71cdbe9 324 ((d >> 14) & 0x00000808) | ((d >> 9) & 0x00000400) |
Sergunb 0:8918a71cdbe9 325 ((d) & 0x00000200) | ((d << 7) & 0x00000100) |
Sergunb 0:8918a71cdbe9 326 ((d >> 7) & 0x00000020) | ((d >> 3) & 0x00000011) |
Sergunb 0:8918a71cdbe9 327 ((d << 2) & 0x00000004) | ((d >> 21) & 0x00000002);
Sergunb 0:8918a71cdbe9 328 }
Sergunb 0:8918a71cdbe9 329
Sergunb 0:8918a71cdbe9 330 //No error to report
Sergunb 0:8918a71cdbe9 331 return NO_ERROR;
Sergunb 0:8918a71cdbe9 332 }
Sergunb 0:8918a71cdbe9 333
Sergunb 0:8918a71cdbe9 334
Sergunb 0:8918a71cdbe9 335 /**
Sergunb 0:8918a71cdbe9 336 * @brief Encrypt a 8-byte block using DES algorithm
Sergunb 0:8918a71cdbe9 337 * @param[in] context Pointer to the DES context
Sergunb 0:8918a71cdbe9 338 * @param[in] input Plaintext block to encrypt
Sergunb 0:8918a71cdbe9 339 * @param[out] output Ciphertext block resulting from encryption
Sergunb 0:8918a71cdbe9 340 **/
Sergunb 0:8918a71cdbe9 341
Sergunb 0:8918a71cdbe9 342 void desEncryptBlock(DesContext *context, const uint8_t *input, uint8_t *output)
Sergunb 0:8918a71cdbe9 343 {
Sergunb 0:8918a71cdbe9 344 uint_t i;
Sergunb 0:8918a71cdbe9 345 uint32_t left;
Sergunb 0:8918a71cdbe9 346 uint32_t right;
Sergunb 0:8918a71cdbe9 347 uint32_t temp;
Sergunb 0:8918a71cdbe9 348
Sergunb 0:8918a71cdbe9 349 //Key schedule
Sergunb 0:8918a71cdbe9 350 uint32_t *ks = context->ks;
Sergunb 0:8918a71cdbe9 351
Sergunb 0:8918a71cdbe9 352 //Copy the plaintext from the input buffer
Sergunb 0:8918a71cdbe9 353 left = LOAD32BE(input + 0);
Sergunb 0:8918a71cdbe9 354 right = LOAD32BE(input + 4);
Sergunb 0:8918a71cdbe9 355
Sergunb 0:8918a71cdbe9 356 //Initial permutation
Sergunb 0:8918a71cdbe9 357 DES_IP(left, right);
Sergunb 0:8918a71cdbe9 358
Sergunb 0:8918a71cdbe9 359 //16 rounds of computation are needed
Sergunb 0:8918a71cdbe9 360 for(i = 0; i < 16; i++, ks += 2)
Sergunb 0:8918a71cdbe9 361 {
Sergunb 0:8918a71cdbe9 362 DES_ROUND(left, right, ks);
Sergunb 0:8918a71cdbe9 363 }
Sergunb 0:8918a71cdbe9 364
Sergunb 0:8918a71cdbe9 365 //Inverse IP permutation
Sergunb 0:8918a71cdbe9 366 DES_FP(right, left);
Sergunb 0:8918a71cdbe9 367
Sergunb 0:8918a71cdbe9 368 //Copy the resulting ciphertext
Sergunb 0:8918a71cdbe9 369 STORE32BE(right, output + 0);
Sergunb 0:8918a71cdbe9 370 STORE32BE(left, output + 4);
Sergunb 0:8918a71cdbe9 371 }
Sergunb 0:8918a71cdbe9 372
Sergunb 0:8918a71cdbe9 373
Sergunb 0:8918a71cdbe9 374 /**
Sergunb 0:8918a71cdbe9 375 * @brief Decrypt a 8-byte block using DES algorithm
Sergunb 0:8918a71cdbe9 376 * @param[in] context Pointer to the DES context
Sergunb 0:8918a71cdbe9 377 * @param[in] input Ciphertext block to decrypt
Sergunb 0:8918a71cdbe9 378 * @param[out] output Plaintext block resulting from decryption
Sergunb 0:8918a71cdbe9 379 **/
Sergunb 0:8918a71cdbe9 380
Sergunb 0:8918a71cdbe9 381 void desDecryptBlock(DesContext *context, const uint8_t *input, uint8_t *output)
Sergunb 0:8918a71cdbe9 382 {
Sergunb 0:8918a71cdbe9 383 uint_t i;
Sergunb 0:8918a71cdbe9 384 uint32_t left;
Sergunb 0:8918a71cdbe9 385 uint32_t right;
Sergunb 0:8918a71cdbe9 386 uint32_t temp;
Sergunb 0:8918a71cdbe9 387
Sergunb 0:8918a71cdbe9 388 //Keys in the key schedule must be applied in reverse order
Sergunb 0:8918a71cdbe9 389 uint32_t *ks = context->ks + 30;
Sergunb 0:8918a71cdbe9 390
Sergunb 0:8918a71cdbe9 391 //Copy the ciphertext from the input buffer
Sergunb 0:8918a71cdbe9 392 left = LOAD32BE(input + 0);
Sergunb 0:8918a71cdbe9 393 right = LOAD32BE(input + 4);
Sergunb 0:8918a71cdbe9 394
Sergunb 0:8918a71cdbe9 395 //Initial permutation
Sergunb 0:8918a71cdbe9 396 DES_IP(left, right);
Sergunb 0:8918a71cdbe9 397
Sergunb 0:8918a71cdbe9 398 //16 rounds of computation are needed
Sergunb 0:8918a71cdbe9 399 for(i = 0; i < 16; i++, ks -= 2)
Sergunb 0:8918a71cdbe9 400 {
Sergunb 0:8918a71cdbe9 401 DES_ROUND(left, right, ks);
Sergunb 0:8918a71cdbe9 402 }
Sergunb 0:8918a71cdbe9 403
Sergunb 0:8918a71cdbe9 404 //Inverse IP permutation
Sergunb 0:8918a71cdbe9 405 DES_FP(right, left);
Sergunb 0:8918a71cdbe9 406
Sergunb 0:8918a71cdbe9 407 //Copy the resulting plaintext
Sergunb 0:8918a71cdbe9 408 STORE32BE(right, output + 0);
Sergunb 0:8918a71cdbe9 409 STORE32BE(left, output + 4);
Sergunb 0:8918a71cdbe9 410 }
Sergunb 0:8918a71cdbe9 411
Sergunb 0:8918a71cdbe9 412 #endif
Sergunb 0:8918a71cdbe9 413