📄 speed.java
字号:
{ int temp = ((t5) & (t4) & (t0)) ^ ((t6) & (t4)) ^ ((t5) & (t2)) ^ ((t3) & (t0)) ^ ((t1) & (t0)) ^ (t3); int vv = (((temp >>> h_wd_len) + temp) & h_wd_mask) >>> v_shift; // // rotate_data_right(t7, h_wd_len - 1) // + rotate_data_right(temp, vv) // + round_key[k++]; // t7 &= f_wd_mask; // not re-used int rot1 = (t7 >>> (h_wd_len - 1)) | (t7 << (f_wd_len - (h_wd_len - 1))); temp &= f_wd_mask; // not re-used int rot2 = (temp >>> vv) | (temp << (f_wd_len - vv)); temp = rot1 + rot2 + round_key[k++]; t7 = t6; t6 = t5; t5 = t4; t4 = t3; t3 = t2; t2 = t1; t1 = t0; t0 = temp & f_wd_mask; }if (DEBUG_SLOW && debuglevel >= 5) { debug("PASS 3: " + Hex.intToString(t7) + " " + Hex.intToString(t6) + " " + Hex.intToString(t5) + " " + Hex.intToString(t4) + " " + Hex.intToString(t3) + " " + Hex.intToString(t2) + " " + Hex.intToString(t1) + " " + Hex.intToString(t0) + " ");} /* Pass 4 uses FF4 */ for (int i = 0; i < quarter_rounds; i++) { int temp = ((t6) & (t4) & (t2) & (t0)) ^ ((t6) & (t5)) ^ ((t4) & (t3)) ^ ((t3) & (t2)) ^ ((t1) & (t0)) ^ (t2); int vv = (((temp >>> h_wd_len) + temp) & h_wd_mask) >>> v_shift; // // rotate_data_right(t7, h_wd_len - 1) // + rotate_data_right(temp, vv) // + round_key[k++]; // t7 &= f_wd_mask; // not re-used int rot1 = (t7 >>> (h_wd_len - 1)) | (t7 << (f_wd_len - (h_wd_len - 1))); temp &= f_wd_mask; // not re-used int rot2 = (temp >>> vv) | (temp << (f_wd_len - vv)); temp = rot1 + rot2 + round_key[k++]; t7 = t6; t6 = t5; t5 = t4; t4 = t3; t3 = t2; t2 = t1; t1 = t0; t0 = temp & f_wd_mask; }if (DEBUG_SLOW && debuglevel >= 5) { debug("PASS 4: " + Hex.intToString(t7) + " " + Hex.intToString(t6) + " " + Hex.intToString(t5) + " " + Hex.intToString(t4) + " " + Hex.intToString(t3) + " " + Hex.intToString(t2) + " " + Hex.intToString(t1) + " " + Hex.intToString(t0) + " ");} out[0] = t0; out[1] = t1; out[2] = t2; out[3] = t3; out[4] = t4; out[5] = t5; out[6] = t6; out[7] = t7; } /** * Decrypts a block. */ private void decrypt(int in[], int out[]) { int t0 = in[0] & 0xFFFFFFFF, t1 = in[1] & 0xFFFFFFFF, t2 = in[2] & 0xFFFFFFFF, t3 = in[3] & 0xFFFFFFFF, t4 = in[4] & 0xFFFFFFFF, t5 = in[5] & 0xFFFFFFFF, t6 = in[6] & 0xFFFFFFFF, t7 = in[7] & 0xFFFFFFFF; int k = rounds - 1; /* points to the first round key */ int quarter_rounds = rounds / 4;// err.println("START " + k + " " + quarter_rounds);// err.println(// " " + Integer.toString((0xFFFFFFFF & t7), 16) +// " " + Integer.toString((0xFFFFFFFF & t6), 16) +// " " + Integer.toString((0xFFFFFFFF & t5), 16) +// " " + Integer.toString((0xFFFFFFFF & t4), 16) +// " " + Integer.toString((0xFFFFFFFF & t3), 16) +// " " + Integer.toString((0xFFFFFFFF & t2), 16) +// " " + Integer.toString((0xFFFFFFFF & t1), 16) +// " " + Integer.toString((0xFFFFFFFF & t0), 16) +// " "); /* Inverse of Pass 4 */ for (int i = 0; i < quarter_rounds; i++) { int new7 = t0; t0 = t1; t1 = t2; t2 = t3; t3 = t4; t4 = t5; t5 = t6; t6 = t7;// err.print("PASS 4." + i); int temp = ((t6) & (t4) & (t2) & (t0)) ^ ((t6) & (t5)) ^ ((t4) & (t3)) ^ ((t3) & (t2)) ^ ((t1) & (t0)) ^ (t2);// err.print(" "+Integer.toString((0xFFFFFFFF & temp), 16)); int vv = (((temp >>> h_wd_len) + temp) & h_wd_mask) >>> v_shift;// err.print(" "+Integer.toString((0xFFFFFFFF & vv), 16)); temp &= f_wd_mask; // not re-used int rot2 = (temp >>> vv) // rotate right | (temp << (f_wd_len - vv)); new7 -= rot2 + round_key[k--]; // check -- new7 &= f_wd_mask;// err.print(" "+Integer.toString((0xFFFFFFFF & new7), 16)); t7 = (new7 << (h_wd_len - 1)) // rotate left | (new7 >>> (f_wd_len - (h_wd_len - 1)));// err.println();// err.println(// " " + Integer.toString((0xFFFFFFFF & t7), 16) +// " " + Integer.toString((0xFFFFFFFF & t6), 16) +// " " + Integer.toString((0xFFFFFFFF & t5), 16) +// " " + Integer.toString((0xFFFFFFFF & t4), 16) +// " " + Integer.toString((0xFFFFFFFF & t3), 16) +// " " + Integer.toString((0xFFFFFFFF & t2), 16) +// " " + Integer.toString((0xFFFFFFFF & t1), 16) +// " " + Integer.toString((0xFFFFFFFF & t0), 16) +// " "); }// err.println("PASS 4");// err.println(// " " + Integer.toString((0xFFFFFFFF & t7), 16) +// " " + Integer.toString((0xFFFFFFFF & t6), 16) +// " " + Integer.toString((0xFFFFFFFF & t5), 16) +// " " + Integer.toString((0xFFFFFFFF & t4), 16) +// " " + Integer.toString((0xFFFFFFFF & t3), 16) +// " " + Integer.toString((0xFFFFFFFF & t2), 16) +// " " + Integer.toString((0xFFFFFFFF & t1), 16) +// " " + Integer.toString((0xFFFFFFFF & t0), 16) +// " "); /* Inverse of Pass 3 */ for (int i = 0; i < quarter_rounds; i++) { int new7 = t0; t0 = t1; t1 = t2; t2 = t3; t3 = t4; t4 = t5; t5 = t6; t6 = t7; int temp = ((t5) & (t4) & (t0)) ^ ((t6) & (t4)) ^ ((t5) & (t2)) ^ ((t3) & (t0)) ^ ((t1) & (t0)) ^ (t3); int vv = (((temp >>> h_wd_len) + temp) & h_wd_mask) >>> v_shift; temp &= f_wd_mask; // not re-used int rot2 = (temp >>> vv) // rotate right | (temp << (f_wd_len - vv)); new7 -= rot2 + round_key[k--]; // check -- new7 &= f_wd_mask; t7 = (new7 << (h_wd_len - 1)) // rotate left | (new7 >>> (f_wd_len - (h_wd_len - 1))); } /* Inverse of Pass 2 */ for (int i = 0; i < quarter_rounds; i++) { int new7 = t0; t0 = t1; t1 = t2; t2 = t3; t3 = t4; t4 = t5; t5 = t6; t6 = t7; int temp = ((t6) & (t4) & (t0)) ^ ((t4) & (t3) & (t0)) ^ ((t5) & (t2)) ^ ((t4) & (t3)) ^ ((t4) & (t1)) ^ ((t3) & (t0)) ^ (t1); int vv = (((temp >>> h_wd_len) + temp) & h_wd_mask) >>> v_shift; temp &= f_wd_mask; // not re-used int rot2 = (temp >>> vv) // rotate right | (temp << (f_wd_len - vv)); new7 -= rot2 + round_key[k--]; // check -- new7 &= f_wd_mask; t7 = (new7 << (h_wd_len - 1)) // rotate left | (new7 >>> (f_wd_len - (h_wd_len - 1))); } /* Inverse of Pass 1 */ for (int i = 0; i < quarter_rounds; i++) { int new7 = t0; t0 = t1; t1 = t2; t2 = t3; t3 = t4; t4 = t5; t5 = t6; t6 = t7; int temp = ((t6) & (t3)) ^ ((t5) & (t1)) ^ ((t4) & (t2)) ^ ((t1) & (t0)) ^ (t0); int vv = (((temp >>> h_wd_len) + temp) & h_wd_mask) >>> v_shift; temp &= f_wd_mask; // not re-used int rot2 = (temp >>> vv) // rotate right | (temp << (f_wd_len - vv)); new7 -= rot2 + round_key[k--]; // check -- new7 &= f_wd_mask; t7 = (new7 << (h_wd_len - 1)) // rotate left | (new7 >>> (f_wd_len - (h_wd_len - 1))); } out[0] = t0; out[1] = t1; out[2] = t2; out[3] = t3; out[4] = t4; out[5] = t5; out[6] = t6; out[7] = t7; }///////////////////////////////// T E S T ///////////////////////// /** * Entry point for self_test. */ public static final void main(String argv[]) { try { self_test(new PrintWriter(System.err), argv); } catch (Exception e) { e.printStackTrace(); } } /** * Runs algorithm through test data, including certification data * provided in paper. */ public static void self_test(PrintWriter out, String argv[]) throws Exception { out.println("Note: hex strings are printed in conventional order, not the order"); out.println(" in the SPEED paper."); out.println(); // // Note that the paper uses certification data that is indexed // from RIGHT to LEFT, i.e., 7, 6, 5, 4, 3, 2, 1, 0. // test(out, 64, // certification 1 "0000000000000000", "0000000000000000", "2E008019BC26856D"); test(out, 128, "00000000000000000000000000000000", "00000000000000000000000000000000", "A44FBF29EDF6CBF8D7A2DFD57163B909"); test(out, 128, // certification 2 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "6C13E4B9C3171571AB54D816915BC4E8"); test(out, 48, "504F4E4D4C4B4A494847464544434241", "1F1E1D1C1B1A191817161514131211100F0E0D0C0B0A09080706050403020100", "90C5981EF6A3D21BC178CACDAD6BF39B2E51CDB70A6EE875A73BF5ED883E3692"); test(out, 256, "0000000000000000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000000000000000", "6CD44D2B49BC6AA7E95FD1C4AF713A2C0AFA1701308D56298CDF27A02EB09BF5"); test(out, 256, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "C8F3E864263FAF24222E38227BEBC022CF4A9A0ECE89FB81CA1B9BA3BA93D0C5"); test(out, 256, // certification 3 "605F5E5D5C5B5A595857565554535251504F4E4D4C4B4A494847464544434241", "1F1E1D1C1B1A191817161514131211100F0E0D0C0B0A09080706050403020100", "3DE16CFA9A626847434E1574693FEC1B3FAA558A296B61D708B131CCBA311068"); } private static void test(PrintWriter out, int rounds, String keyStr, String plainStr, String cipherStr) throws Exception { // Note that the paper uses certification data that is indexed // from RIGHT to LEFT, i.e., 7, 6, 5, 4, 3, 2, 1, 0. byte keyBytes[] = Hex.fromReversedString(keyStr); byte plain[] = Hex.fromReversedString(plainStr); byte cipher[] = Hex.fromReversedString(cipherStr); SPEED speed = new SPEED(); speed.setBlockSize(plain.length); speed.setRounds(rounds); Key key = new RawSecretKey("SPEED", keyBytes); speed.initEncrypt(key); byte encP[] = speed.crypt(plain); String a, b; out.println(" key:" + Hex.toString(keyBytes)); out.println(" plain:" + Hex.toString(plain)); out.println(" enc:" + (a = Hex.toString(encP))); b = Hex.toString(cipher); if (a.equals(b)) out.print("encryption good; "); else { out.println(" calc:" + b); out.println(" ********* SPEED ENCRYPTION FAILED ********* "); speed.dump(); } speed.initDecrypt(key); byte[] decC = speed.crypt(encP); a = Hex.toString(decC); b = Hex.toString(plain); if (a.equals(b)) out.println("decryption good"); else { out.println(); out.println(" enc:" + Hex.toString(encP)); out.println(" dec:" + a); out.println(" calc:" + b); out.println(" ********* SPEED DECRYPTION FAILED ********* "); speed.dump(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -