📄 testsafer.java
字号:
// $Id: TestSAFER.java,v 1.1.1.1 2002/08/27 12:32:14 grosbois Exp $//// $Log: TestSAFER.java,v $// Revision 1.1.1.1 2002/08/27 12:32:14 grosbois// Add cryptix 3.2//// Revision 1.7 2000/08/17 11:41:02 edwin// java.* -> xjava.*//// Revision 1.6 1998/01/28 05:40:02 hopwood// + Major update of test classes.//// Revision 1.5.1 1998/01/28 hopwood// + Fixed conflicts with my version.// + Updated expected number of passes.// + Changed to use BaseTest API.//// Revision 1.5 1998/01/22 04:40:36 iang// + Added some extra tests, only one is now missing from check.ref.// + Added a few additional comments where tests didn't match check.ref.// + Now System.exit(1) on exception, and throws exception on bad data.//// Revision 1.4 1997/11/22 07:05:41 raif// *** empty log message ***//// Revision 1.3 1997/11/22 05:59:03 iang// + core.util ==> util.core//// Revision 1.2 1997/11/10 07:31:33 raif// + Tests new support of engineGet/SetParameter();//// Revision 1.2 1997/10/28 00:09:10 raif// + Incorporated name changes done to SAFER//// Revision 1.1 1997/10/27 22:50:17 raif// + Use new cryptix.util classes.// + More compact.//// Revision 0.1.0.1 1997/07/?? R. Naffah// + Added decryption tests in CFB and OFB modes.//// Revision 0.1.0.0 1997/07/?? R. Naffah// + Original version.//// $Endlog$/* * Copyright (c) 1997 Systemics Ltd * on behalf of the Cryptix Development Team. All rights reserved. */package cryptix.test;import cryptix.provider.key.RawSecretKey;import cryptix.util.core.ArrayUtil;import cryptix.util.core.Hex;import cryptix.util.test.BaseTest;import xjava.security.Cipher;import xjava.security.FeedbackCipher;import xjava.security.SecretKey;import xjava.security.Parameterized;/** * Tests the output of the SAFER cipher algorithm implementation against * certified pre-computed output for a given set of reference input. * <p> * Test data is taken from <code>check.ref</code> file in Richard De Moliner's * <a href="ftp://ftp.isi.ee.ethz.ch/pub/simpl/safer.V1.2.tar.Z"> * SAFER toolkit V1.2</a>. As this is the distribution of the * author of the algorithm, this is understood to be conformance data, * cautious label of Examples notwithstanding. * <p> * One test is not present, that of testing a text key * (more a test for the test program). * <p> * <b>$Revision: 1.1.1.1 $</b> * @author Raif S. Naffah * @author David Hopwood */class TestSAFERextends BaseTest{// Variables and constants//........................................................................... private static byte[] input = new byte[16 * 18]; private static final byte[] anIV = { (byte)0x74, (byte)0x53, (byte)0x6E, (byte)0xBD, (byte)0xC2, (byte)0x11, (byte)0x48, (byte)0x4A }; private SecretKey aKey;// Test methods//........................................................................... public static void main(String[] args) { new TestSAFER().commandline(args); } protected void engineTest() throws Exception { setExpectedPasses(17); byte[] b = { (byte)0x42, (byte)0x43, (byte)0x1B, (byte)0xA4, (byte)0x0D, (byte)0x29, (byte)0x1F, (byte)0x81, (byte)0xD6, (byte)0x60, (byte)0x83, (byte)0xC6, (byte)0x05, (byte)0xD3, (byte)0xA4, (byte)0xD6 }; aKey = new RawSecretKey("SAFER", b); for (int i = 0; i < 2; i++) { for (int j = 1; j < 9; j++) input[16 * i + j + 7] = (byte)j; } for (int i = 0; i < 256; i++) input[32 + i] = (byte)i; out.println("input data\n" + Hex.dumpString(input)); // There is a small amount of test data in SAFER_SK.TXT // (see the above test kit). This data has internal // round data included for debugging purposes. // These data sets are included in the below tests. // Look at the second block of the common input data. test1(); test2(); test3(); test4(); test5(); test6(); test7(); test8(); test8B(); test8C(); // This is using a text key - how to interpret extra chars? // safer -e -ecb -k AaBcDeFgHiJkLmNoPqRsTuVwXyZz0123456789 // test8D(); test8E(); test9(); test10(); test11(); } private void test1() throws Exception { int[] data = { 3, 40, 8, 201, 14, 231, 171, 127, 125, 40, 3, 134, 51, 185, 46, 180, 3, 40, 8, 201, 14, 231, 171, 127, 125, 40, 3, 134, 51, 185, 46, 180, 145, 193, 218, 242, 149, 199, 15, 163, 25, 207, 49, 154, 65, 22, 246, 7, 222, 135, 141, 58, 225, 131, 75, 71, 89, 235, 249, 44, 127, 227, 181, 69, 159, 12, 78, 64, 221, 251, 20, 32, 119, 184, 78, 136, 10, 2, 106, 193, 143, 74, 196, 156, 26, 228, 156, 155, 251, 162, 156, 195, 133, 136, 151, 164, 7, 189, 49, 125, 68, 77, 106, 185, 12, 206, 130, 131, 69, 124, 179, 4, 5, 19, 191, 12, 223, 108, 112, 79, 171, 62, 108, 206, 116, 117, 67, 115, 96, 119, 81, 7, 167, 42, 84, 64, 251, 182, 224, 42, 5, 204, 31, 169, 157, 122, 237, 33, 196, 183, 241, 50, 125, 221, 74, 87, 198, 143, 128, 131, 240, 168, 59, 136, 63, 211, 116, 117, 93, 13, 180, 15, 216, 25, 39, 243, 204, 51, 23, 62, 218, 100, 194, 254, 138, 60, 153, 12, 141, 112, 67, 189, 52, 19, 188, 96, 221, 105, 239, 51, 136, 244, 210, 250, 24, 224, 181, 103, 74, 22, 174, 213, 84, 134, 59, 146, 195, 129, 144, 255, 209, 179, 88, 219, 130, 133, 36, 133, 12, 70, 104, 64, 18, 157, 66, 244, 86, 129, 171, 231, 70, 15, 199, 114, 63, 5, 106, 141, 217, 241, 87, 204, 106, 178, 64, 190, 91, 58, 131, 40, 121, 246, 9, 129, 172, 3, 144, 148, 86, 233, 70, 137, 234, 154, 150, 87, 27, 126, 194, 213, 97, 0, 42, 166, 24, 28, 36, 46 }; out.println("\nSAFER in ECB mode: " + "safer -e -ecb -kx 0000000000000000 -r 6\n"); byte[] output = new byte[data.length]; for (int i = 0; i < data.length; i++) output[i] = (byte)data[i]; Cipher alg = Cipher.getInstance("SAFER", "Cryptix"); ((Parameterized) alg).setParameter("variant", "K-64"); ((Parameterized) alg).setParameter("rounds", new Integer(6)); byte[] b = new byte[8]; RawSecretKey key = new RawSecretKey("SAFER", b); alg.initEncrypt(key); compareIt(alg.crypt(input), output); } private void test2() throws Exception { int[] data = { 90, 178, 127, 114, 20, 163, 58, 225, 53, 216, 27, 187, 244, 86, 143, 221, 90, 178, 127, 114, 20, 163, 58, 225, 53, 216, 27, 187, 244, 86, 143, 221, 176, 77, 205, 34, 113, 253, 45, 100, 117, 35, 111, 82, 245, 22, 192, 247, 113, 229, 207, 127, 8, 58, 89, 197, 53, 111, 112, 44, 199, 250, 129, 97, 22, 45, 118, 143, 36, 19, 175, 42, 17, 98, 225, 143, 245, 22, 92, 104, 30, 78, 11, 197, 233, 245, 156, 211, 252, 128, 59, 102, 39, 106, 52, 201, 36, 150, 88, 51, 99, 193, 250, 199, 101, 188, 204, 108, 195, 158, 4, 16, 64, 14, 12, 4, 69, 77, 87, 174, 179, 168, 218, 211, 98, 49, 250, 255, 108, 197, 119, 252, 139, 188, 188, 69, 111, 126, 135, 26, 214, 89, 66, 223, 89, 23, 144, 219, 71, 139, 62, 28, 168, 72, 23, 84, 206, 185, 152, 189, 224, 0, 3, 206, 49, 73, 65, 167, 192, 241, 81, 40, 12, 142, 229, 74, 4, 186, 59, 173, 112, 219, 152, 96, 6, 40, 84, 232, 152, 173, 91, 48, 65, 34, 12, 56, 92, 127, 196, 80, 104, 37, 133, 26, 77, 151, 103, 250, 83, 94, 214, 100, 211, 196, 211, 86, 176, 148, 84, 52, 16, 98, 181, 167, 171, 165, 176, 131, 34, 234, 253, 245, 243, 229, 190, 104, 135, 12, 178, 255, 88, 83, 26, 197, 13, 87, 161, 25, 152, 13, 132, 120, 232, 132, 144, 232, 238, 120, 80, 141, 244, 81, 238, 133, 178, 195, 161, 51, 196, 174, 204, 11, 79, 48, 86, 90, 145, 237, 144, 173, 117, 144, 116, 74, 226, 113, 221, 90 }; out.println("\nSAFER in ECB mode: " + "safer -e -ecb -kx 0102030405060708 -r 6\n"); byte[] output = new byte[data.length]; for (int i = 0; i < data.length; i++) output[i] = (byte)data[i]; Cipher alg = Cipher.getInstance("SAFER", "Cryptix"); ((Parameterized) alg).setParameter("variant", "K-64"); ((Parameterized) alg).setParameter("rounds", new Integer(6)); byte[] b = new byte[8]; for (int i = 1; i < 9; i++) b[i - 1] = (byte)i; RawSecretKey key = new RawSecretKey("SAFER", b); alg.initEncrypt(key); compareIt(alg.crypt(input), output); } private void test3() throws Exception { int[] data = { 3, 92, 58, 98, 214, 251, 36, 18, 200, 242, 156, 221, 135, 120, 62, 217, 3, 92, 58, 98, 214, 251, 36, 18, 200, 242, 156, 221, 135, 120, 62, 217, 2, 200, 228, 89, 250, 111, 128, 147, 203, 125, 189, 26, 90, 50, 9, 146, 149, 78, 101, 142, 114, 229, 131, 2, 62, 174, 98, 110, 169, 197, 54, 63, 89, 85, 87, 223, 143, 246, 139, 69, 175, 47, 36, 136, 12, 214, 13, 29, 27, 37, 48, 227, 234, 251, 65, 118, 189, 249, 125, 187, 61, 95, 43, 165, 192, 19, 53, 236, 129, 201, 111, 186, 128, 238, 168, 161, 255, 185, 159, 60, 39, 215, 88, 181, 24, 210, 205, 138, 73, 233, 133, 129, 204, 192, 157, 89, 11, 204, 197, 13, 254, 57, 201, 86, 1, 104, 36, 93, 89, 167, 209, 28, 93, 108, 251, 66, 28, 190, 50, 250, 173, 10, 172, 104, 44, 220, 12, 66, 227, 252, 245, 29, 253, 71, 160, 74, 246, 140, 143, 29, 178, 92, 125, 196, 43, 64, 156, 229, 193, 21, 103, 211, 217, 196, 211, 32, 127, 150, 31, 19, 116, 226, 149, 154, 103, 37, 206, 37, 94, 65, 62, 89, 112, 69, 212, 173, 227, 86, 217, 69, 24, 157, 171, 91, 84, 105, 146, 134, 221, 99, 119, 167, 3, 38, 221, 132, 166, 194, 145, 244, 136, 58, 22, 218, 103, 85, 190, 178, 60, 157, 226, 234, 174, 78, 151, 117, 238, 127, 201, 155, 244, 3, 63, 66, 47, 145, 200, 239, 161, 113, 76, 233, 78, 142, 46, 172, 80, 1, 4, 9, 183, 25, 176, 250, 175, 216, 123, 45, 90, 108, 67, 187, 147, 57, 235, 25 }; out.println("\nSAFER in ECB mode: " + "safer -e -ecb -kx 0807060504030201 -r 6\n"); byte[] output = new byte[data.length]; for (int i = 0; i < data.length; i++) output[i] = (byte)data[i]; Cipher alg = Cipher.getInstance("SAFER", "Cryptix"); ((Parameterized) alg).setParameter("variant", "K-64"); ((Parameterized) alg).setParameter("rounds", new Integer(6)); byte[] b = new byte[8]; for (int i = 0; i < 8; i++) b[i] = (byte)(8 - i); RawSecretKey key = new RawSecretKey("SAFER", b); alg.initEncrypt(key); compareIt(alg.crypt(input), output);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -