Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "tools.h"
- int Table_C[9][4] = {
- {0xAC25, 0x8A93, 0x243D, 0x262E},
- {0xF887, 0xC4F2, 0x8E36, 0x9FA1},
- {0x7DC0, 0x6A29, 0x6D84, 0x34BD},
- {0xA267, 0xCC15, 0x04FE, 0xB94A},
- {0xDF24, 0x40EF, 0x96DA, 0x905F},
- {0xD631, 0xAA62, 0x4D15, 0x70CB},
- {0x7533, 0x45FC, 0x5337, 0xD25E},
- {0xA926, 0x1C7B, 0x5F12, 0x4ECC},
- {0x3C86, 0x28DB, 0xFC01, 0x7CB1}
- };
- int Table_S[9][4] = {
- { 4, 7, 11, 8},
- { 7, 14, 5, 4},
- { 8, 2, 9, 4},
- {13, 1, 14, 6},
- { 7, 12, 5, 1},
- { 2, 4, 12, 3},
- { 9, 2, 11, 13},
- {12, 3, 6, 11},
- { 7, 15, 4, 14}
- };
- char Table_logic[9][4] = {
- {'&','|','&','|',},
- {'|','|','|','|',},
- {'&','|','|','&',},
- {'|','&','|','|',},
- {'|','|','&','&',},
- {'&','&','&','|',},
- {'&','|','|','|',},
- {'&','|','&','&',},
- {'|','|','&','|',}
- };
- std::bitset<16> Tools::r_shift (std::bitset<16> BITS, unsigned int STEP) {
- return (BITS >> STEP) | ((BITS << 16 - STEP));
- }
- bool Tools::fullAdder(bool b1, bool b2, bool& carry)
- {
- bool sum = (b1 ^ b2) ^ carry;
- carry = (b1 && b2) || (b1 && carry) || (b2 && carry);
- return sum;
- }
- std::bitset<16> Tools::bitsetAdd(std::bitset<16>& x, std::bitset<16>& y)
- {
- bool carry = false;
- std::bitset<16> ans;
- for (int i = 0; i < 16; i++) {
- ans[i] = fullAdder(x[i], y[i], carry);
- }
- return ans;
- }
- std::vector<std::bitset<16> > Tools::keygen(std::bitset<128> key128) {
- std::bitset<16> k0, k1, k2, k3, k4, k5, k6, k7;
- std::vector<std::bitset<16> > result;
- for (int i = 127; i >= 0; i--) {
- if (i > 111) {
- k0 <<= 1;
- k0[0] = key128[i];
- }
- if (i > 95) {
- k1 <<= 1;
- k1[0] = key128[i];
- }
- if (i > 79) {
- k2 <<= 1;
- k2[0] = key128[i];
- }
- if (i > 63) {
- k3 <<= 1;
- k3[0] = key128[i];
- }
- if (i > 47) {
- k4 <<= 1;
- k4[0] = key128[i];
- }
- if (i > 31) {
- k5 <<= 1;
- k5[0] = key128[i];
- }
- if (i > 15) {
- k6 <<= 1;
- k6[0] = key128[i];
- }
- if (i >= 0) {
- k7 <<= 1;
- k7[0] = key128[i];
- }
- }
- result.push_back(k0);
- result.push_back(k1);
- result.push_back(k2);
- result.push_back(k3);
- result.push_back(k4);
- result.push_back(k5);
- result.push_back(k6);
- result.push_back(k7);
- return result;
- }
- std::vector<std::bitset<16> > Tools::subbgen(std::bitset<64> N) {
- std::bitset<16> a, b, c, d;
- std::vector<std::bitset<16> > result;
- for (int i = 63; i >= 0; i--) {
- if (i > 47) {
- a <<= 1;
- a[0] = N[i];
- }
- if (i > 31) {
- b <<= 1;
- b[0] = N[i];
- }
- if (i > 15) {
- c <<= 1;
- c[0] = N[i];
- }
- if (i >= 0) {
- d <<= 1;
- d[0] = N[i];
- }
- }
- result.push_back(a);
- result.push_back(b);
- result.push_back(c);
- result.push_back(d);
- return result;
- }
- std::vector<std::bitset<8> > Tools::resultcompiler(std::bitset<16> A, std::bitset<16>B, std::bitset<16>C, std::bitset<16>D) {
- std::vector<std::bitset<8> > bit_result;
- std::bitset<8> temp1, temp2;
- for (int j = 0; j < 16; j++) {
- if (j < 8) {
- temp1 << 1;
- temp1[j] = A[j];
- } else {
- temp2 << 1;
- temp2[j-8] = A[j];
- }
- } bit_result.push_back(temp1); bit_result.push_back(temp2);
- for (int j = 0; j < 16; j++) {
- if (j < 8) {
- temp1 << 1;
- temp1[j] = B[j];
- } else {
- temp2 << 1;
- temp2[j-8] = B[j];
- }
- } bit_result.push_back(temp1); bit_result.push_back(temp2);
- for (int j = 0; j < 16; j++) {
- if (j < 8) {
- temp1 << 1;
- temp1[j] = C[j];
- } else {
- temp2 << 1;
- temp2[j-8] = C[j];
- }
- } bit_result.push_back(temp1); bit_result.push_back(temp2);
- for (int j = 0; j < 16; j++) {
- if (j < 8) {
- temp1 << 1;
- temp1[j] = D[j];
- } else {
- temp2 << 1;
- temp2[j-8] = D[j];
- }
- } bit_result.push_back(temp1); bit_result.push_back(temp2);
- return bit_result;
- }
- std::vector<std::bitset<16> > Tools::iteration (std::bitset<16> X1, std::bitset<16> X2, std::bitset<16> X3, std::bitset<16> X4, std::bitset<16>key, int round, int iter) {
- std::vector<std::bitset<16> > Y_array;
- std::bitset<16> Y1, Y2, Y3, Y4;
- std::bitset<16> temp1 = X3 ^ key;
- int s = Table_S[round][iter];
- Y3 = r_shift(bitsetAdd(X2, temp1), s);
- std::bitset<16> temp2;
- if (Table_logic[round][iter] == '&') temp2 = Y3 & X4;
- else temp2 = Y3 | X4;
- Y1 = bitsetAdd(X1, temp2);
- Y2 = X2; Y4 = X4;
- Y_array.push_back(Y1);
- Y_array.push_back(Y2);
- Y_array.push_back(Y3);
- Y_array.push_back(Y4);
- return Y_array;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement