Changeset c0a5a50 for tools/traceanon/rijndael.c
 Timestamp:
 01/08/13 10:22:52 (9 years ago)
 Branches:
 4.0.1hotfixes, cachetimestamps, develop, dpdkndag, etsilive, getfragoff, help, libtrace4, master, ndag_format, pfring, rc4.0.1, rc4.0.2, rc4.0.3, rc4.0.4, ringdecrementfix, ringperformance, ringtimestampfixes
 Children:
 dc6072d
 Parents:
 ebafa5d
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

tools/traceanon/rijndael.c
ra8f2692 rc0a5a50 33 33 // This implementation works on 128 , 192 , 256 bit keys 34 34 // and on 128 bit blocks 35 */ 36 37 /* 38 // Code modified by Shane Alcock to fix truckloads of warnings about 39 // dereferencing typepunned pointers when building with newer versions of 40 // gcc. These changes are also placed in the public domain and may be freely 41 // used as a result. 35 42 */ 36 43 … … 1051 1058 { 1052 1059 int i, k, numBlocks; 1053 UINT8 block[16], iv[4][4]; 1060 UINT32 block[4]; 1061 UINT32 iv[4]; 1062 UINT32 *input32 = (UINT32 *)input; 1063 UINT32 *miv = (UINT32 *)m_initVector; 1054 1064 1055 1065 if(m_state != Valid)return RIJNDAEL_NOT_INITIALIZED; … … 1061 1071 1062 1072 switch(m_mode){ 1063 case ECB: 1073 case ECB: 1064 1074 for(i = numBlocks;i > 0;i) 1065 1075 { 1066 1076 r_encrypt(input,outBuffer); 1067 input += 16; 1077 input +=16; 1078 input32 += 4; 1068 1079 outBuffer += 16; 1069 1080 } 1070 1081 break; 1071 1082 case CBC: 1072 ((UINT32*)block)[0] = ((UINT32*)m_initVector)[0] ^ ((UINT32*)input)[0];1073 ((UINT32*)block)[1] = ((UINT32*)m_initVector)[1] ^ ((UINT32*)input)[1];1074 ((UINT32*)block)[2] = ((UINT32*)m_initVector)[2] ^ ((UINT32*)input)[2];1075 ((UINT32*)block)[3] = ((UINT32*)m_initVector)[3] ^ ((UINT32*)input)[3];1076 r_encrypt( block,outBuffer);1083 block[0] = miv[0] ^ input32[0]; 1084 block[1] = miv[1] ^ input32[1]; 1085 block[2] = miv[2] ^ input32[2]; 1086 block[3] = miv[3] ^ input32[3]; 1087 r_encrypt((UINT8 *)block,outBuffer); 1077 1088 input += 16; 1078 1089 for(i = numBlocks  1;i > 0;i) 1079 1090 { 1080 ((UINT32*)block)[0] = ((UINT32*)outBuffer)[0] ^ ((UINT32*)input)[0];1081 ((UINT32*)block)[1] = ((UINT32*)outBuffer)[1] ^ ((UINT32*)input)[1];1082 ((UINT32*)block)[2] = ((UINT32*)outBuffer)[2] ^ ((UINT32*)input)[2];1083 ((UINT32*)block)[3] = ((UINT32*)outBuffer)[3] ^ ((UINT32*)input)[3];1091 block[0] = ((UINT32*)outBuffer)[0] ^ input32[0]; 1092 block[1] = ((UINT32*)outBuffer)[1] ^ input32[1]; 1093 block[2] = ((UINT32*)outBuffer)[2] ^ input32[2]; 1094 block[3] = ((UINT32*)outBuffer)[3] ^ input32[3]; 1084 1095 outBuffer += 16; 1085 r_encrypt( block,outBuffer);1096 r_encrypt((UINT8 *)block,outBuffer); 1086 1097 input += 16; 1098 input32 += 4; 1087 1099 } 1088 1100 break; 1089 1101 case CFB1: 1090 1102 #if STRICT_ALIGN 1091 memcpy(iv,m_initVector,16); 1103 memcpy(iv,m_initVector,16); 1092 1104 #else /* !STRICT_ALIGN */ 1093 *((UINT32*)iv[0]) = *((UINT32*)(m_initVector ));1094 *((UINT32*)iv[1]) = *((UINT32*)(m_initVector + 4));1095 *((UINT32*)iv[2]) = *((UINT32*)(m_initVector + 8));1096 *((UINT32*)iv[3]) = *((UINT32*)(m_initVector +12));1105 iv[0] = *(miv); 1106 iv[1] = *(miv + 1); 1107 iv[2] = *(miv + 2); 1108 iv[3] = *(miv + 3); 1097 1109 #endif /* ?STRICT_ALIGN */ 1098 1110 for(i = numBlocks; i > 0; i) … … 1100 1112 for(k = 0; k < 128; k++) 1101 1113 { 1102 *((UINT32*) block ) = *((UINT32*)iv[0]); 1103 *((UINT32*)(block+ 4)) = *((UINT32*)iv[1]); 1104 *((UINT32*)(block+ 8)) = *((UINT32*)iv[2]); 1105 *((UINT32*)(block+12)) = *((UINT32*)iv[3]); 1106 r_encrypt(block,block); 1107 outBuffer[k/8] ^= (block[0] & 0x80) >> (k & 7); 1108 iv[0][0] = (iv[0][0] << 1)  (iv[0][1] >> 7); 1109 iv[0][1] = (iv[0][1] << 1)  (iv[0][2] >> 7); 1110 iv[0][2] = (iv[0][2] << 1)  (iv[0][3] >> 7); 1111 iv[0][3] = (iv[0][3] << 1)  (iv[1][0] >> 7); 1112 iv[1][0] = (iv[1][0] << 1)  (iv[1][1] >> 7); 1113 iv[1][1] = (iv[1][1] << 1)  (iv[1][2] >> 7); 1114 iv[1][2] = (iv[1][2] << 1)  (iv[1][3] >> 7); 1115 iv[1][3] = (iv[1][3] << 1)  (iv[2][0] >> 7); 1116 iv[2][0] = (iv[2][0] << 1)  (iv[2][1] >> 7); 1117 iv[2][1] = (iv[2][1] << 1)  (iv[2][2] >> 7); 1118 iv[2][2] = (iv[2][2] << 1)  (iv[2][3] >> 7); 1119 iv[2][3] = (iv[2][3] << 1)  (iv[3][0] >> 7); 1120 iv[3][0] = (iv[3][0] << 1)  (iv[3][1] >> 7); 1121 iv[3][1] = (iv[3][1] << 1)  (iv[3][2] >> 7); 1122 iv[3][2] = (iv[3][2] << 1)  (iv[3][3] >> 7); 1123 iv[3][3] = (iv[3][3] << 1)  ((outBuffer[k >> 3] >> (7(k&7))) & 1); 1114 UINT8 *ivb = (UINT8 *)iv; 1115 1116 block[0] = iv[0]; 1117 block[1] = iv[1]; 1118 block[2] = iv[2]; 1119 block[3] = iv[3]; 1120 r_encrypt((UINT8 *)block,(UINT8 *)block); 1121 outBuffer[k/8] ^= (((UINT8 *)block)[0] & 0x80) >> (k & 7); 1122 ivb[0] = (ivb[0] << 1)  (ivb[1] >> 7); 1123 ivb[1] = (ivb[1] << 1)  (ivb[2] >> 7); 1124 ivb[2] = (ivb[2] << 1)  (ivb[3] >> 7); 1125 ivb[3] = (ivb[3] << 1)  (ivb[4] >> 7); 1126 ivb[4] = (ivb[4] << 1)  (ivb[5] >> 7); 1127 ivb[5] = (ivb[5] << 1)  (ivb[6] >> 7); 1128 ivb[6] = (ivb[6] << 1)  (ivb[7] >> 7); 1129 ivb[7] = (ivb[7] << 1)  (ivb[8] >> 7); 1130 ivb[8] = (ivb[8] << 1)  (ivb[9] >> 7); 1131 ivb[9] = (ivb[9] << 1)  (ivb[10] >> 7); 1132 ivb[10] = (ivb[10] << 1)  (ivb[11] >> 7); 1133 ivb[11] = (ivb[11] << 1)  (ivb[12] >> 7); 1134 ivb[12] = (ivb[12] << 1)  (ivb[13] >> 7); 1135 ivb[13] = (ivb[13] << 1)  (ivb[14] >> 7); 1136 ivb[14] = (ivb[14] << 1)  (ivb[15] >> 7); 1137 ivb[15] = (ivb[15] << 1)  ((outBuffer[k >> 3] >> (7(k&7))) & 1); 1124 1138 } 1125 1139 } … … 1136 1150 { 1137 1151 int i, numBlocks, padLen; 1138 UINT8 block[16], *iv; 1152 UINT32 block[4], *iv; 1153 UINT32 *input32 = (UINT32 *)input; 1154 UINT8 *ivb, *blockb; 1139 1155 1140 1156 if(m_state != Valid)return RIJNDAEL_NOT_INITIALIZED; … … 1147 1163 switch(m_mode) 1148 1164 { 1149 case ECB: 1165 case ECB: 1150 1166 for(i = numBlocks; i > 0; i) 1151 1167 { 1152 1168 r_encrypt(input, outBuffer); 1153 1169 input += 16; 1170 input32 += 4; 1154 1171 outBuffer += 16; 1155 1172 } 1156 1173 padLen = 16  (inputOctets  16*numBlocks); 1157 1174 memcpy(block, input, 16  padLen); 1158 memset(block + 16 padLen, padLen, padLen);1159 r_encrypt( block,outBuffer);1175 memset(block + 4  padLen, padLen, padLen); 1176 r_encrypt((UINT8 *)block,outBuffer); 1160 1177 break; 1161 1178 case CBC: 1162 iv = m_initVector;1179 iv = (UINT32 *)m_initVector; 1163 1180 for(i = numBlocks; i > 0; i) 1164 1181 { 1165 ( (UINT32*)block)[0] = ((UINT32*)input)[0] ^ ((UINT32*)iv)[0];1166 ( (UINT32*)block)[1] = ((UINT32*)input)[1] ^ ((UINT32*)iv)[1];1167 ( (UINT32*)block)[2] = ((UINT32*)input)[2] ^ ((UINT32*)iv)[2];1168 ( (UINT32*)block)[3] = ((UINT32*)input)[3] ^ ((UINT32*)iv)[3];1169 r_encrypt( block, outBuffer);1170 iv = outBuffer;1182 (block)[0] = (input32)[0] ^ (iv)[0]; 1183 (block)[1] = (input32)[1] ^ (iv)[1]; 1184 (block)[2] = (input32)[2] ^ (iv)[2]; 1185 (block)[3] = (input32)[3] ^ (iv)[3]; 1186 r_encrypt((UINT8 *)block, outBuffer); 1187 iv = (UINT32 *)outBuffer; 1171 1188 input += 16; 1172 1189 outBuffer += 16; 1173 1190 } 1174 1191 padLen = 16  (inputOctets  16*numBlocks); 1192 blockb = (UINT8 *)block; 1193 ivb = (UINT8 *)iv; 1175 1194 for (i = 0; i < 16  padLen; i++) { 1176 block [i] = input[i] ^ iv[i];1195 blockb[i] = input[i] ^ ivb[i]; 1177 1196 } 1178 1197 for (i = 16  padLen; i < 16; i++) { 1179 block [i] = (UINT8)padLen ^ iv[i];1180 } 1181 r_encrypt( block,outBuffer);1198 blockb[i] = (UINT8)padLen ^ ivb[i]; 1199 } 1200 r_encrypt((UINT8 *)block,outBuffer); 1182 1201 break; 1183 1202 default: … … 1192 1211 { 1193 1212 int i, k, numBlocks; 1194 UINT8 block[16], iv[4][4]; 1213 UINT32 block[4], iv[4], *miv, *input32; 1214 UINT8 *ivb; 1215 1216 miv = (UINT32 *)m_initVector; 1217 input32 = (UINT32 *)input; 1218 1195 1219 1196 1220 if(m_state != Valid)return RIJNDAEL_NOT_INITIALIZED; … … 1203 1227 switch(m_mode) 1204 1228 { 1205 case ECB: 1229 case ECB: 1206 1230 for (i = numBlocks; i > 0; i) 1207 1231 { 1208 1232 r_decrypt(input,outBuffer); 1209 1233 input += 16; 1234 input32 += 4; 1210 1235 outBuffer += 16; 1211 1236 } … … 1213 1238 case CBC: 1214 1239 #if STRICT_ALIGN 1215 memcpy(iv,m_initVector,16); 1240 memcpy(iv,m_initVector,16); 1216 1241 #else 1217 *((UINT32*)iv[0]) = *((UINT32*)(m_initVector ));1218 *((UINT32*)iv[1]) = *((UINT32*)(m_initVector+ 4));1219 *((UINT32*)iv[2]) = *((UINT32*)(m_initVector+ 8));1220 *((UINT32*)iv[3]) = *((UINT32*)(m_initVector+12));1242 iv[0] = miv[0]; 1243 iv[1] = miv[1]; 1244 iv[2] = miv[2]; 1245 iv[3] = miv[3]; 1221 1246 #endif 1222 1247 for (i = numBlocks; i > 0; i) 1223 1248 { 1224 r_decrypt(input, block);1225 ((UINT32*)block)[0] ^= *((UINT32*)iv[0]);1226 ((UINT32*)block)[1] ^= *((UINT32*)iv[1]);1227 ((UINT32*)block)[2] ^= *((UINT32*)iv[2]);1228 ((UINT32*)block)[3] ^= *((UINT32*)iv[3]);1249 r_decrypt(input, (UINT8 *)block); 1250 block[0] ^= iv[0]; 1251 block[1] ^= iv[1]; 1252 block[2] ^= iv[2]; 1253 block[3] ^= iv[3]; 1229 1254 #if STRICT_ALIGN 1230 1255 memcpy(iv, input, 16); 1231 1256 memcpy(outBuf, block, 16); 1232 1257 #else 1233 *((UINT32*)iv[0]) = ((UINT32*)input)[0]; ((UINT32*)outBuffer)[0] = ((UINT32*)block)[0]; 1234 *((UINT32*)iv[1]) = ((UINT32*)input)[1]; ((UINT32*)outBuffer)[1] = ((UINT32*)block)[1]; 1235 *((UINT32*)iv[2]) = ((UINT32*)input)[2]; ((UINT32*)outBuffer)[2] = ((UINT32*)block)[2]; 1236 *((UINT32*)iv[3]) = ((UINT32*)input)[3]; ((UINT32*)outBuffer)[3] = ((UINT32*)block)[3]; 1258 iv[0] = input32[0]; 1259 ((UINT32*)outBuffer)[0] = block[0]; 1260 iv[1] = input32[1]; 1261 ((UINT32*)outBuffer)[1] = block[1]; 1262 iv[2] = input32[2]; 1263 ((UINT32*)outBuffer)[2] = block[2]; 1264 iv[3] = input32[3]; 1265 ((UINT32*)outBuffer)[3] = block[3]; 1266 1237 1267 #endif 1238 1268 input += 16; 1269 input32 += 16; 1239 1270 outBuffer += 16; 1240 1271 } … … 1242 1273 case CFB1: 1243 1274 #if STRICT_ALIGN 1244 memcpy(iv, m_initVector, 16); 1275 memcpy(iv, m_initVector, 16); 1245 1276 #else 1246 *((UINT32*)iv[0]) = *((UINT32*)(m_initVector));1247 *((UINT32*)iv[1]) = *((UINT32*)(m_initVector+ 4));1248 *((UINT32*)iv[2]) = *((UINT32*)(m_initVector+ 8));1249 *((UINT32*)iv[3]) = *((UINT32*)(m_initVector+12));1277 iv[0] = miv[0]; 1278 iv[1] = miv[1]; 1279 iv[2] = miv[2]; 1280 iv[3] = miv[3]; 1250 1281 #endif 1251 1282 for(i = numBlocks; i > 0; i) … … 1253 1284 for(k = 0; k < 128; k++) 1254 1285 { 1255 *((UINT32*) block ) = *((UINT32*)iv[0]); 1256 *((UINT32*)(block+ 4)) = *((UINT32*)iv[1]); 1257 *((UINT32*)(block+ 8)) = *((UINT32*)iv[2]); 1258 *((UINT32*)(block+12)) = *((UINT32*)iv[3]); 1259 r_encrypt(block, block); 1260 iv[0][0] = (iv[0][0] << 1)  (iv[0][1] >> 7); 1261 iv[0][1] = (iv[0][1] << 1)  (iv[0][2] >> 7); 1262 iv[0][2] = (iv[0][2] << 1)  (iv[0][3] >> 7); 1263 iv[0][3] = (iv[0][3] << 1)  (iv[1][0] >> 7); 1264 iv[1][0] = (iv[1][0] << 1)  (iv[1][1] >> 7); 1265 iv[1][1] = (iv[1][1] << 1)  (iv[1][2] >> 7); 1266 iv[1][2] = (iv[1][2] << 1)  (iv[1][3] >> 7); 1267 iv[1][3] = (iv[1][3] << 1)  (iv[2][0] >> 7); 1268 iv[2][0] = (iv[2][0] << 1)  (iv[2][1] >> 7); 1269 iv[2][1] = (iv[2][1] << 1)  (iv[2][2] >> 7); 1270 iv[2][2] = (iv[2][2] << 1)  (iv[2][3] >> 7); 1271 iv[2][3] = (iv[2][3] << 1)  (iv[3][0] >> 7); 1272 iv[3][0] = (iv[3][0] << 1)  (iv[3][1] >> 7); 1273 iv[3][1] = (iv[3][1] << 1)  (iv[3][2] >> 7); 1274 iv[3][2] = (iv[3][2] << 1)  (iv[3][3] >> 7); 1275 iv[3][3] = (iv[3][3] << 1)  ((input[k >> 3] >> (7(k&7))) & 1); 1276 outBuffer[k/8] ^= (block[0] & 0x80) >> (k & 7); 1286 block[0] = iv[0]; 1287 block[1] = iv[1]; 1288 block[2] = iv[2]; 1289 block[3] = iv[3]; 1290 r_encrypt((UINT8 *)block, (UINT8 *)block); 1291 ivb = (UINT8 *)iv; 1292 ivb[0] = (ivb[0] << 1)  (ivb[1] >> 7); 1293 ivb[1] = (ivb[1] << 1)  (ivb[2] >> 7); 1294 ivb[2] = (ivb[2] << 1)  (ivb[3] >> 7); 1295 ivb[3] = (ivb[3] << 1)  (ivb[4] >> 7); 1296 ivb[4] = (ivb[4] << 1)  (ivb[5] >> 7); 1297 ivb[5] = (ivb[5] << 1)  (ivb[6] >> 7); 1298 ivb[6] = (ivb[6] << 1)  (ivb[7] >> 7); 1299 ivb[7] = (ivb[7] << 1)  (ivb[8] >> 7); 1300 ivb[8] = (ivb[8] << 1)  (ivb[9] >> 7); 1301 ivb[9] = (ivb[9] << 1)  (ivb[10] >> 7); 1302 ivb[10] = (ivb[10] << 1)  (ivb[11] >> 7); 1303 ivb[11] = (ivb[11] << 1)  (ivb[12] >> 7); 1304 ivb[12] = (ivb[12] << 1)  (ivb[13] >> 7); 1305 ivb[13] = (ivb[13] << 1)  (ivb[14] >> 7); 1306 ivb[14] = (ivb[14] << 1)  (ivb[15] >> 7); 1307 ivb[15] = (ivb[15] << 1)  ((input[k >> 3] >> (7(k&7))) & 1); 1308 outBuffer[k/8] ^= (((UINT8 *)block)[0] & 0x80) >> (k & 7); 1277 1309 } 1278 1310 } … … 1289 1321 { 1290 1322 int i, numBlocks, padLen; 1291 UINT 8 block[16];1323 UINT32 block[4]; 1292 1324 UINT32 iv[4]; 1325 UINT8 *blockb = (UINT8 *)block; 1293 1326 1294 1327 if(m_state != Valid)return RIJNDAEL_NOT_INITIALIZED; … … 1310 1343 } 1311 1344 1312 r_decrypt(input, block );1313 padLen = block [15];1345 r_decrypt(input, blockb); 1346 padLen = blockb[15]; 1314 1347 if (padLen >= 16)return RIJNDAEL_CORRUPTED_DATA; 1315 1348 for(i = 16  padLen; i < 16; i++) 1316 1349 { 1317 if(block [i] != padLen)return RIJNDAEL_CORRUPTED_DATA;1318 } 1319 memcpy(outBuffer, block , 16  padLen);1320 break; 1350 if(blockb[i] != padLen)return RIJNDAEL_CORRUPTED_DATA; 1351 } 1352 memcpy(outBuffer, blockb, 16  padLen); 1353 break; 1321 1354 case CBC: 1322 1355 memcpy(iv, m_initVector, 16); … … 1324 1357 for (i = numBlocks  1; i > 0; i) 1325 1358 { 1326 r_decrypt(input, block );1327 ((UINT32*)block)[0] ^= iv[0];1328 ( (UINT32*)block)[1] ^= iv[1];1329 ( (UINT32*)block)[2] ^= iv[2];1330 ( (UINT32*)block)[3] ^= iv[3];1359 r_decrypt(input, blockb); 1360 block[0] ^= iv[0]; 1361 (block)[1] ^= iv[1]; 1362 (block)[2] ^= iv[2]; 1363 (block)[3] ^= iv[3]; 1331 1364 memcpy(iv, input, 16); 1332 1365 memcpy(outBuffer, block, 16); … … 1335 1368 } 1336 1369 /* last block */ 1337 r_decrypt(input, block );1338 ( (UINT32*)block)[0] ^= iv[0];1339 ( (UINT32*)block)[1] ^= iv[1];1340 ( (UINT32*)block)[2] ^= iv[2];1341 ( (UINT32*)block)[3] ^= iv[3];1342 padLen = block [15];1370 r_decrypt(input, blockb); 1371 (block)[0] ^= iv[0]; 1372 (block)[1] ^= iv[1]; 1373 (block)[2] ^= iv[2]; 1374 (block)[3] ^= iv[3]; 1375 padLen = blockb[15]; 1343 1376 if(padLen <= 0  padLen > 16)return RIJNDAEL_CORRUPTED_DATA; 1344 1377 for(i = 16  padLen; i < 16; i++) 1345 1378 { 1346 if(block [i] != padLen)return RIJNDAEL_CORRUPTED_DATA;1347 } 1348 memcpy(outBuffer, block , 16  padLen);1379 if(blockb[i] != padLen)return RIJNDAEL_CORRUPTED_DATA; 1380 } 1381 memcpy(outBuffer, blockb, 16  padLen); 1349 1382 break; 1350 1383 … … 1459 1492 } 1460 1493 1494 static inline UINT32 encrypt_b_from_T(UINT8 ind1, UINT8 ind2, UINT8 ind3, 1495 UINT8 ind4) { 1496 1497 UINT32 *tmp1 = (UINT32 *)(T1[ind1]); 1498 UINT32 *tmp2 = (UINT32 *)(T2[ind2]); 1499 UINT32 *tmp3 = (UINT32 *)(T3[ind3]); 1500 UINT32 *tmp4 = (UINT32 *)(T4[ind4]); 1501 1502 return *tmp1 ^ *tmp2 ^ *tmp3 ^ *tmp4; 1503 } 1504 1505 static inline UINT32 decrypt_b_from_T(UINT8 ind1, UINT8 ind2, UINT8 ind3, 1506 UINT8 ind4) { 1507 1508 UINT32 *tmp1 = (UINT32 *)(T5[ind1]); 1509 UINT32 *tmp2 = (UINT32 *)(T6[ind2]); 1510 UINT32 *tmp3 = (UINT32 *)(T7[ind3]); 1511 UINT32 *tmp4 = (UINT32 *)(T8[ind4]); 1512 1513 return *tmp1 ^ *tmp2 ^ *tmp3 ^ *tmp4; 1514 } 1515 1461 1516 void r_encrypt(const UINT8 a[16], UINT8 b[16]) 1462 1517 { 1463 1518 int r; 1464 UINT8 temp[4][4]; 1465 1466 *((UINT32*)temp[0]) = *((UINT32*)(a )) ^ *((UINT32*)m_expandedKey[0][0]); 1467 *((UINT32*)temp[1]) = *((UINT32*)(a+ 4)) ^ *((UINT32*)m_expandedKey[0][1]); 1468 *((UINT32*)temp[2]) = *((UINT32*)(a+ 8)) ^ *((UINT32*)m_expandedKey[0][2]); 1469 *((UINT32*)temp[3]) = *((UINT32*)(a+12)) ^ *((UINT32*)m_expandedKey[0][3]); 1470 *((UINT32*)(b )) = *((UINT32*)T1[temp[0][0]]) 1471 ^ *((UINT32*)T2[temp[1][1]]) 1472 ^ *((UINT32*)T3[temp[2][2]]) 1473 ^ *((UINT32*)T4[temp[3][3]]); 1474 *((UINT32*)(b + 4)) = *((UINT32*)T1[temp[1][0]]) 1475 ^ *((UINT32*)T2[temp[2][1]]) 1476 ^ *((UINT32*)T3[temp[3][2]]) 1477 ^ *((UINT32*)T4[temp[0][3]]); 1478 *((UINT32*)(b + 8)) = *((UINT32*)T1[temp[2][0]]) 1479 ^ *((UINT32*)T2[temp[3][1]]) 1480 ^ *((UINT32*)T3[temp[0][2]]) 1481 ^ *((UINT32*)T4[temp[1][3]]); 1482 *((UINT32*)(b +12)) = *((UINT32*)T1[temp[3][0]]) 1483 ^ *((UINT32*)T2[temp[0][1]]) 1484 ^ *((UINT32*)T3[temp[1][2]]) 1485 ^ *((UINT32*)T4[temp[2][3]]); 1519 UINT32 temp[4]; 1520 UINT32 *a32 = (UINT32 *)a; 1521 UINT32 *b32 = (UINT32 *)b; 1522 UINT32 *mkey; 1523 UINT8 *tempb; 1524 1525 mkey = (UINT32 *)m_expandedKey[0][0]; 1526 temp[0] = a32[0] ^ *mkey; 1527 mkey = (UINT32 *)m_expandedKey[0][1]; 1528 temp[1] = a32[1] ^ *mkey; 1529 mkey = (UINT32 *)m_expandedKey[0][2]; 1530 temp[2] = a32[2] ^ *mkey; 1531 mkey = (UINT32 *)m_expandedKey[0][3]; 1532 temp[3] = a32[3] ^ *mkey; 1533 1534 tempb = (UINT8 *)temp; 1535 b32[0] = encrypt_b_from_T(tempb[0], tempb[5], tempb[10], tempb[15]); 1536 b32[1] = encrypt_b_from_T(tempb[4], tempb[9], tempb[14], tempb[3]); 1537 b32[2] = encrypt_b_from_T(tempb[8], tempb[13], tempb[2], tempb[7]); 1538 b32[3] = encrypt_b_from_T(tempb[12], tempb[1], tempb[6], tempb[11]); 1539 1486 1540 for(r = 1; r < (int)m_uRounds1; r++) 1487 1541 { 1488 *((UINT32*)temp[0]) = *((UINT32*)(b )) ^ *((UINT32*)m_expandedKey[r][0]); 1489 *((UINT32*)temp[1]) = *((UINT32*)(b+ 4)) ^ *((UINT32*)m_expandedKey[r][1]); 1490 *((UINT32*)temp[2]) = *((UINT32*)(b+ 8)) ^ *((UINT32*)m_expandedKey[r][2]); 1491 *((UINT32*)temp[3]) = *((UINT32*)(b+12)) ^ *((UINT32*)m_expandedKey[r][3]); 1492 1493 *((UINT32*)(b )) = *((UINT32*)T1[temp[0][0]]) 1494 ^ *((UINT32*)T2[temp[1][1]]) 1495 ^ *((UINT32*)T3[temp[2][2]]) 1496 ^ *((UINT32*)T4[temp[3][3]]); 1497 *((UINT32*)(b + 4)) = *((UINT32*)T1[temp[1][0]]) 1498 ^ *((UINT32*)T2[temp[2][1]]) 1499 ^ *((UINT32*)T3[temp[3][2]]) 1500 ^ *((UINT32*)T4[temp[0][3]]); 1501 *((UINT32*)(b + 8)) = *((UINT32*)T1[temp[2][0]]) 1502 ^ *((UINT32*)T2[temp[3][1]]) 1503 ^ *((UINT32*)T3[temp[0][2]]) 1504 ^ *((UINT32*)T4[temp[1][3]]); 1505 *((UINT32*)(b +12)) = *((UINT32*)T1[temp[3][0]]) 1506 ^ *((UINT32*)T2[temp[0][1]]) 1507 ^ *((UINT32*)T3[temp[1][2]]) 1508 ^ *((UINT32*)T4[temp[2][3]]); 1542 mkey = (UINT32 *)m_expandedKey[r][0]; 1543 temp[0] = b32[0] ^ *mkey; 1544 mkey = (UINT32 *)m_expandedKey[r][1]; 1545 temp[1] = b32[1] ^ *mkey; 1546 mkey = (UINT32 *)m_expandedKey[r][2]; 1547 temp[2] = b32[2] ^ *mkey; 1548 mkey = (UINT32 *)m_expandedKey[r][3]; 1549 temp[3] = b32[3] ^ *mkey; 1550 1551 tempb = (UINT8 *)temp; 1552 b32[0] = encrypt_b_from_T(tempb[0], tempb[5], tempb[10], tempb[15]); 1553 b32[1] = encrypt_b_from_T(tempb[4], tempb[9], tempb[14], tempb[3]); 1554 b32[2] = encrypt_b_from_T(tempb[8], tempb[13], tempb[2], tempb[7]); 1555 b32[3] = encrypt_b_from_T(tempb[12], tempb[1], tempb[6], tempb[11]); 1509 1556 } 1510 *((UINT32*)temp[0]) = *((UINT32*)(b )) ^ *((UINT32*)m_expandedKey[m_uRounds1][0]); 1511 *((UINT32*)temp[1]) = *((UINT32*)(b+ 4)) ^ *((UINT32*)m_expandedKey[m_uRounds1][1]); 1512 *((UINT32*)temp[2]) = *((UINT32*)(b+ 8)) ^ *((UINT32*)m_expandedKey[m_uRounds1][2]); 1513 *((UINT32*)temp[3]) = *((UINT32*)(b+12)) ^ *((UINT32*)m_expandedKey[m_uRounds1][3]); 1514 b[ 0] = T1[temp[0][0]][1]; 1515 b[ 1] = T1[temp[1][1]][1]; 1516 b[ 2] = T1[temp[2][2]][1]; 1517 b[ 3] = T1[temp[3][3]][1]; 1518 b[ 4] = T1[temp[1][0]][1]; 1519 b[ 5] = T1[temp[2][1]][1]; 1520 b[ 6] = T1[temp[3][2]][1]; 1521 b[ 7] = T1[temp[0][3]][1]; 1522 b[ 8] = T1[temp[2][0]][1]; 1523 b[ 9] = T1[temp[3][1]][1]; 1524 b[10] = T1[temp[0][2]][1]; 1525 b[11] = T1[temp[1][3]][1]; 1526 b[12] = T1[temp[3][0]][1]; 1527 b[13] = T1[temp[0][1]][1]; 1528 b[14] = T1[temp[1][2]][1]; 1529 b[15] = T1[temp[2][3]][1]; 1530 *((UINT32*)(b )) ^= *((UINT32*)m_expandedKey[m_uRounds][0]); 1531 *((UINT32*)(b+ 4)) ^= *((UINT32*)m_expandedKey[m_uRounds][1]); 1532 *((UINT32*)(b+ 8)) ^= *((UINT32*)m_expandedKey[m_uRounds][2]); 1533 *((UINT32*)(b+12)) ^= *((UINT32*)m_expandedKey[m_uRounds][3]); 1557 mkey = (UINT32 *)m_expandedKey[m_uRounds  1][0]; 1558 temp[0] = b32[0] ^ *mkey; 1559 mkey = (UINT32 *)m_expandedKey[m_uRounds  1][1]; 1560 temp[1] = b32[1] ^ *mkey; 1561 mkey = (UINT32 *)m_expandedKey[m_uRounds  1][2]; 1562 temp[2] = b32[2] ^ *mkey; 1563 mkey = (UINT32 *)m_expandedKey[m_uRounds  1][3]; 1564 temp[3] = b32[3] ^ *mkey; 1565 1566 tempb = (UINT8 *)temp; 1567 b[ 0] = T1[tempb[0]][1]; 1568 b[ 1] = T1[tempb[5]][1]; 1569 b[ 2] = T1[tempb[10]][1]; 1570 b[ 3] = T1[tempb[15]][1]; 1571 b[ 4] = T1[tempb[4]][1]; 1572 b[ 5] = T1[tempb[9]][1]; 1573 b[ 6] = T1[tempb[14]][1]; 1574 b[ 7] = T1[tempb[3]][1]; 1575 b[ 8] = T1[tempb[8]][1]; 1576 b[ 9] = T1[tempb[13]][1]; 1577 b[10] = T1[tempb[2]][1]; 1578 b[11] = T1[tempb[7]][1]; 1579 b[12] = T1[tempb[12]][1]; 1580 b[13] = T1[tempb[1]][1]; 1581 b[14] = T1[tempb[6]][1]; 1582 b[15] = T1[tempb[11]][1]; 1583 1584 mkey = (UINT32 *)m_expandedKey[m_uRounds][0]; 1585 b32[0] ^= *mkey; 1586 mkey = (UINT32 *)m_expandedKey[m_uRounds][1]; 1587 b32[1] ^= *mkey; 1588 mkey = (UINT32 *)m_expandedKey[m_uRounds][2]; 1589 b32[2] ^= *mkey; 1590 mkey = (UINT32 *)m_expandedKey[m_uRounds][3]; 1591 b32[3] ^= *mkey; 1592 1593 1534 1594 } 1535 1595 … … 1537 1597 { 1538 1598 int r; 1539 UINT8 temp[4][4]; 1540 1541 *((UINT32*)temp[0]) = *((const UINT32*)(a )) ^ *((const UINT32*)m_expandedKey[m_uRounds][0]); 1542 *((UINT32*)temp[1]) = *((const UINT32*)(a+ 4)) ^ *((const UINT32*)m_expandedKey[m_uRounds][1]); 1543 *((UINT32*)temp[2]) = *((const UINT32*)(a+ 8)) ^ *((const UINT32*)m_expandedKey[m_uRounds][2]); 1544 *((UINT32*)temp[3]) = *((const UINT32*)(a+12)) ^ *((const UINT32*)m_expandedKey[m_uRounds][3]); 1545 1546 *((UINT32*)(b )) = *((const UINT32*)T5[temp[0][0]]) 1547 ^ *((const UINT32*)T6[temp[3][1]]) 1548 ^ *((const UINT32*)T7[temp[2][2]]) 1549 ^ *((const UINT32*)T8[temp[1][3]]); 1550 *((UINT32*)(b+ 4)) = *((const UINT32*)T5[temp[1][0]]) 1551 ^ *((const UINT32*)T6[temp[0][1]]) 1552 ^ *((const UINT32*)T7[temp[3][2]]) 1553 ^ *((const UINT32*)T8[temp[2][3]]); 1554 *((UINT32*)(b+ 8)) = *((UINT32*)T5[temp[2][0]]) 1555 ^ *((const UINT32*)T6[temp[1][1]]) 1556 ^ *((const UINT32*)T7[temp[0][2]]) 1557 ^ *((const UINT32*)T8[temp[3][3]]); 1558 *((UINT32*)(b+12)) = *((UINT32*)T5[temp[3][0]]) 1559 ^ *((const UINT32*)T6[temp[2][1]]) 1560 ^ *((const UINT32*)T7[temp[1][2]]) 1561 ^ *((const UINT32*)T8[temp[0][3]]); 1599 UINT32 temp[4]; 1600 UINT32 *a32 = (UINT32 *)a; 1601 UINT32 *b32 = (UINT32 *)b; 1602 UINT32 *mkey; 1603 UINT8 *tempb; 1604 1605 mkey = (UINT32 *)m_expandedKey[m_uRounds][0]; 1606 temp[0] = a32[0] ^ *mkey; 1607 mkey = (UINT32 *)m_expandedKey[m_uRounds][1]; 1608 temp[1] = a32[1] ^ *mkey; 1609 mkey = (UINT32 *)m_expandedKey[m_uRounds][2]; 1610 temp[2] = a32[2] ^ *mkey; 1611 mkey = (UINT32 *)m_expandedKey[m_uRounds][3]; 1612 temp[3] = a32[3] ^ *mkey; 1613 1614 tempb = (UINT8 *)temp; 1615 b32[0] = decrypt_b_from_T(tempb[0], tempb[13], tempb[10], tempb[7]); 1616 b32[1] = decrypt_b_from_T(tempb[4], tempb[1], tempb[14], tempb[11]); 1617 b32[2] = decrypt_b_from_T(tempb[8], tempb[5], tempb[2], tempb[15]); 1618 b32[3] = decrypt_b_from_T(tempb[12], tempb[9], tempb[6], tempb[3]); 1619 1562 1620 for(r = m_uRounds1; r > 1; r) 1563 1621 { 1564 *((UINT32*)temp[0]) = *((UINT32*)(b )) ^ *((UINT32*)m_expandedKey[r][0]); 1565 *((UINT32*)temp[1]) = *((UINT32*)(b+ 4)) ^ *((UINT32*)m_expandedKey[r][1]); 1566 *((UINT32*)temp[2]) = *((UINT32*)(b+ 8)) ^ *((UINT32*)m_expandedKey[r][2]); 1567 *((UINT32*)temp[3]) = *((UINT32*)(b+12)) ^ *((UINT32*)m_expandedKey[r][3]); 1568 *((UINT32*)(b )) = *((UINT32*)T5[temp[0][0]]) 1569 ^ *((UINT32*)T6[temp[3][1]]) 1570 ^ *((UINT32*)T7[temp[2][2]]) 1571 ^ *((UINT32*)T8[temp[1][3]]); 1572 *((UINT32*)(b+ 4)) = *((UINT32*)T5[temp[1][0]]) 1573 ^ *((UINT32*)T6[temp[0][1]]) 1574 ^ *((UINT32*)T7[temp[3][2]]) 1575 ^ *((UINT32*)T8[temp[2][3]]); 1576 *((UINT32*)(b+ 8)) = *((UINT32*)T5[temp[2][0]]) 1577 ^ *((UINT32*)T6[temp[1][1]]) 1578 ^ *((UINT32*)T7[temp[0][2]]) 1579 ^ *((UINT32*)T8[temp[3][3]]); 1580 *((UINT32*)(b+12)) = *((UINT32*)T5[temp[3][0]]) 1581 ^ *((UINT32*)T6[temp[2][1]]) 1582 ^ *((UINT32*)T7[temp[1][2]]) 1583 ^ *((UINT32*)T8[temp[0][3]]); 1622 mkey = (UINT32 *)m_expandedKey[r][0]; 1623 temp[0] = b32[0] ^ *mkey; 1624 mkey = (UINT32 *)m_expandedKey[r][1]; 1625 temp[1] = b32[1] ^ *mkey; 1626 mkey = (UINT32 *)m_expandedKey[r][2]; 1627 temp[2] = b32[2] ^ *mkey; 1628 mkey = (UINT32 *)m_expandedKey[r][3]; 1629 temp[3] = b32[3] ^ *mkey; 1630 1631 tempb = (UINT8 *)temp; 1632 b32[0] = decrypt_b_from_T(tempb[0], tempb[13], tempb[10], tempb[7]); 1633 b32[1] = decrypt_b_from_T(tempb[4], tempb[1], tempb[14], tempb[11]); 1634 b32[2] = decrypt_b_from_T(tempb[8], tempb[5], tempb[2], tempb[15]); 1635 b32[3] = decrypt_b_from_T(tempb[12], tempb[9], tempb[6], tempb[3]); 1636 1584 1637 } 1585 1586 *((UINT32*)temp[0]) = *((UINT32*)(b )) ^ *((UINT32*)m_expandedKey[1][0]); 1587 *((UINT32*)temp[1]) = *((UINT32*)(b+ 4)) ^ *((UINT32*)m_expandedKey[1][1]); 1588 *((UINT32*)temp[2]) = *((UINT32*)(b+ 8)) ^ *((UINT32*)m_expandedKey[1][2]); 1589 *((UINT32*)temp[3]) = *((UINT32*)(b+12)) ^ *((UINT32*)m_expandedKey[1][3]); 1590 b[ 0] = S5[temp[0][0]]; 1591 b[ 1] = S5[temp[3][1]]; 1592 b[ 2] = S5[temp[2][2]]; 1593 b[ 3] = S5[temp[1][3]]; 1594 b[ 4] = S5[temp[1][0]]; 1595 b[ 5] = S5[temp[0][1]]; 1596 b[ 6] = S5[temp[3][2]]; 1597 b[ 7] = S5[temp[2][3]]; 1598 b[ 8] = S5[temp[2][0]]; 1599 b[ 9] = S5[temp[1][1]]; 1600 b[10] = S5[temp[0][2]]; 1601 b[11] = S5[temp[3][3]]; 1602 b[12] = S5[temp[3][0]]; 1603 b[13] = S5[temp[2][1]]; 1604 b[14] = S5[temp[1][2]]; 1605 b[15] = S5[temp[0][3]]; 1606 *((UINT32*)(b )) ^= *((UINT32*)m_expandedKey[0][0]); 1607 *((UINT32*)(b+ 4)) ^= *((UINT32*)m_expandedKey[0][1]); 1608 *((UINT32*)(b+ 8)) ^= *((UINT32*)m_expandedKey[0][2]); 1609 *((UINT32*)(b+12)) ^= *((UINT32*)m_expandedKey[0][3]); 1638 1639 mkey = (UINT32 *)m_expandedKey[1][0]; 1640 temp[0] = b32[0] ^ *mkey; 1641 mkey = (UINT32 *)m_expandedKey[1][1]; 1642 temp[1] = b32[1] ^ *mkey; 1643 mkey = (UINT32 *)m_expandedKey[1][2]; 1644 temp[2] = b32[2] ^ *mkey; 1645 mkey = (UINT32 *)m_expandedKey[1][3]; 1646 temp[3] = b32[3] ^ *mkey; 1647 1648 tempb = (UINT8 *)temp; 1649 b[ 0] = S5[tempb[0]]; 1650 b[ 1] = S5[tempb[13]]; 1651 b[ 2] = S5[tempb[10]]; 1652 b[ 3] = S5[tempb[7]]; 1653 b[ 4] = S5[tempb[4]]; 1654 b[ 5] = S5[tempb[1]]; 1655 b[ 6] = S5[tempb[14]]; 1656 b[ 7] = S5[tempb[11]]; 1657 b[ 8] = S5[tempb[8]]; 1658 b[ 9] = S5[tempb[5]]; 1659 b[10] = S5[tempb[2]]; 1660 b[11] = S5[tempb[15]]; 1661 b[12] = S5[tempb[12]]; 1662 b[13] = S5[tempb[9]]; 1663 b[14] = S5[tempb[6]]; 1664 b[15] = S5[tempb[3]]; 1665 1666 mkey = (UINT32 *)m_expandedKey[0][0]; 1667 b32[0] ^= *mkey; 1668 mkey = (UINT32 *)m_expandedKey[0][1]; 1669 b32[1] ^= *mkey; 1670 mkey = (UINT32 *)m_expandedKey[0][2]; 1671 b32[2] ^= *mkey; 1672 mkey = (UINT32 *)m_expandedKey[0][3]; 1673 b32[3] ^= *mkey; 1674 1610 1675 }
Note: See TracChangeset
for help on using the changeset viewer.