BlowfishTestVector.java revision 0:37a05a11f281
1169689Skan/*
2169689Skan * Copyright 1998-2007 Sun Microsystems, Inc.  All Rights Reserved.
3169689Skan * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4169689Skan *
5169689Skan * This code is free software; you can redistribute it and/or modify it
6169689Skan * under the terms of the GNU General Public License version 2 only, as
7169689Skan * published by the Free Software Foundation.
8169689Skan *
9169689Skan * This code is distributed in the hope that it will be useful, but WITHOUT
10169689Skan * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11169689Skan * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12169689Skan * version 2 for more details (a copy is included in the LICENSE file that
13169689Skan * accompanied this code).
14169689Skan *
15169689Skan * You should have received a copy of the GNU General Public License version
16169689Skan * 2 along with this work; if not, write to the Free Software Foundation,
17169689Skan * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18169689Skan *
19169689Skan * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20169689Skan * CA 95054 USA or visit www.sun.com if you need additional information or
21169689Skan * have any questions.
22169689Skan */
23169689Skan
24169689Skan/*
25169689Skan * @test
26169689Skan * @bug 0000000
27169689Skan * @library ../UTIL
28169689Skan * @build TestUtil
29169689Skan * @run main BlowfishTestVector
30169689Skan * @summary Known Answer Test for Blowfish cipher with ECB mode
31169689Skan * @author Jan Luehe
32169689Skan */
33169689Skan
34169689Skanimport java.security.*;
35169689Skanimport java.util.*;
36169689Skanimport javax.crypto.*;
37169689Skanimport javax.crypto.spec.*;
38169689Skan
39169689Skanpublic class BlowfishTestVector {
40169689Skan
41169689Skan    // test vector #1 (checking for the "signed" bug)
42169689Skan    // (ECB mode)
43169689Skan    private static final byte[] TEST_KEY_1 = new byte[] {
44169689Skan        (byte)0x1c, (byte)0x58, (byte)0x7f, (byte)0x1c,
45169689Skan        (byte)0x13, (byte)0x92, (byte)0x4f, (byte)0xef
46169689Skan    };
47169689Skan    private static final byte[] TV_P1 = new byte[] {
48169689Skan        (byte)0x30, (byte)0x55, (byte)0x32, (byte)0x28,
49169689Skan        (byte)0x6d, (byte)0x6f, (byte)0x29, (byte)0x5a
50169689Skan    };
51169689Skan    private static final byte[] TV_C1 = new byte[] {
52169689Skan        (byte)0x55, (byte)0xcb, (byte)0x37, (byte)0x74,
53169689Skan        (byte)0xd1, (byte)0x3e, (byte)0xf2, (byte)0x01
54169689Skan    };
55169689Skan
56169689Skan    // test vector #2 (offical vector by Bruce Schneier)
57169689Skan    // (ECB mode)
58169689Skan    private static final String S_TEST_KEY_2 = "Who is John Galt?";
59169689Skan
60169689Skan    private static final byte[] TV_P2 = new byte[] {
61169689Skan        (byte)0xfe, (byte)0xdc, (byte)0xba, (byte)0x98,
62169689Skan        (byte)0x76, (byte)0x54, (byte)0x32, (byte)0x10
63169689Skan    };
64169689Skan    private static final byte[] TV_C2 = new byte[] {
65169689Skan        (byte)0xcc, (byte)0x91, (byte)0x73, (byte)0x2b,
66169689Skan        (byte)0x80, (byte)0x22, (byte)0xf6, (byte)0x84
67169689Skan    };
68169689Skan
69169689Skan    public static void main(String[] argv) throws Exception {
70169689Skan
71169689Skan        Provider p = new com.sun.crypto.provider.SunJCE();
72169689Skan        Security.addProvider(p);
73169689Skan        String transformation = "Blowfish/ECB/NoPadding";
74169689Skan        Cipher cipher = Cipher.getInstance(transformation);
75169689Skan        int MAX_KEY_SIZE = Cipher.getMaxAllowedKeyLength(transformation);
76169689Skan        //
77169689Skan        // test 1
78169689Skan        //
79169689Skan        if (TEST_KEY_1.length*8 <= MAX_KEY_SIZE) {
80169689Skan            SecretKey sKey = new SecretKeySpec(TEST_KEY_1, "Blowfish");
81169689Skan            try {
82169689Skan                cipher.init(Cipher.ENCRYPT_MODE, sKey);
83169689Skan                byte[] c1 = cipher.doFinal(TV_P1);
84169689Skan                if (!Arrays.equals(c1, TV_C1))
85169689Skan                    throw new Exception("Encryption (Test vector 1) failed");
86169689Skan
87169689Skan                cipher.init(Cipher.DECRYPT_MODE, sKey);
88169689Skan                byte[] p1 = cipher.doFinal(c1);
89169689Skan                if (!Arrays.equals(p1, TV_P1))
90169689Skan                    throw new Exception("Decryption (Test vector 1) failed");
91169689Skan            } catch (SecurityException se) {
92169689Skan                TestUtil.handleSE(se);
93169689Skan            }
94169689Skan        }
95169689Skan        //
96169689Skan        // test 2
97169689Skan        //
98169689Skan        byte[] testKey2 = S_TEST_KEY_2.getBytes();
99169689Skan        if (testKey2.length*8 <= MAX_KEY_SIZE) {
100169689Skan            SecretKey sKey = new SecretKeySpec(testKey2, "Blowfish");
101169689Skan            try {
102169689Skan                cipher.init(Cipher.ENCRYPT_MODE, sKey);
103169689Skan                byte[] c2 = cipher.doFinal(TV_P2);
104169689Skan                if (!Arrays.equals(c2, TV_C2))
105169689Skan                    throw new Exception("Encryption (Test vector 2) failed");
106169689Skan
107169689Skan                cipher.init(Cipher.DECRYPT_MODE, sKey);
108169689Skan                byte[] p2 = cipher.doFinal(c2);
109169689Skan                if (!Arrays.equals(p2, TV_P2))
110169689Skan                    throw new Exception("Decryption (Test vector 2) failed");
111169689Skan            } catch (SecurityException se) {
112169689Skan                TestUtil.handleSE(se);
113169689Skan            }
114169689Skan        }
115169689Skan        System.out.println("Test passed");
116169689Skan    }
117169689Skan
118169689Skan    /*
119169689Skan     * Converts a byte to hex digit and writes to the supplied buffer
120169689Skan     */
121169689Skan    static private void byte2hex(byte b, StringBuffer buf) {
122169689Skan        char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
123169689Skan                            '9', 'A', 'B', 'C', 'D', 'E', 'F' };
124169689Skan        int high = ((b & 0xf0) >> 4);
125169689Skan        int low = (b & 0x0f);
126169689Skan        buf.append(hexChars[high]);
127169689Skan        buf.append(hexChars[low]);
128169689Skan    }
129169689Skan
130169689Skan    /*
131169689Skan     * Converts a byte array to hex string
132169689Skan     */
133169689Skan    static private String toHexString(byte[] block) {
134169689Skan        StringBuffer buf = new StringBuffer();
135169689Skan
136169689Skan        int len = block.length;
137169689Skan
138169689Skan        for (int i = 0; i < len; i++) {
139169689Skan             byte2hex(block[i], buf);
140169689Skan             if (i < len-1) {
141169689Skan                 buf.append(":");
142169689Skan             }
143169689Skan        }
144169689Skan        return buf.toString();
145169689Skan    }
146169689Skan}
147169689Skan