1 /*
2  * Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @bug 0000000
27  * @library ../UTIL
28  * @build TestUtil
29  * @run main BlowfishTestVector
30  * @summary Known Answer Test for Blowfish cipher with ECB mode
31  * @author Jan Luehe
32  */
33 
34 import java.security.*;
35 import java.util.*;
36 import javax.crypto.*;
37 import javax.crypto.spec.*;
38 
39 public class BlowfishTestVector {
40 
41     // test vector #1 (checking for the "signed" bug)
42     // (ECB mode)
43     private static final byte[] TEST_KEY_1 = new byte[] {
44         (byte)0x1c, (byte)0x58, (byte)0x7f, (byte)0x1c,
45         (byte)0x13, (byte)0x92, (byte)0x4f, (byte)0xef
46     };
47     private static final byte[] TV_P1 = new byte[] {
48         (byte)0x30, (byte)0x55, (byte)0x32, (byte)0x28,
49         (byte)0x6d, (byte)0x6f, (byte)0x29, (byte)0x5a
50     };
51     private static final byte[] TV_C1 = new byte[] {
52         (byte)0x55, (byte)0xcb, (byte)0x37, (byte)0x74,
53         (byte)0xd1, (byte)0x3e, (byte)0xf2, (byte)0x01
54     };
55 
56     // test vector #2 (offical vector by Bruce Schneier)
57     // (ECB mode)
58     private static final String S_TEST_KEY_2 = "Who is John Galt?";
59 
60     private static final byte[] TV_P2 = new byte[] {
61         (byte)0xfe, (byte)0xdc, (byte)0xba, (byte)0x98,
62         (byte)0x76, (byte)0x54, (byte)0x32, (byte)0x10
63     };
64     private static final byte[] TV_C2 = new byte[] {
65         (byte)0xcc, (byte)0x91, (byte)0x73, (byte)0x2b,
66         (byte)0x80, (byte)0x22, (byte)0xf6, (byte)0x84
67     };
68 
main(String[] argv)69     public static void main(String[] argv) throws Exception {
70 
71         String transformation = "Blowfish/ECB/NoPadding";
72         Cipher cipher = Cipher.getInstance(transformation, "SunJCE");
73         int MAX_KEY_SIZE = Cipher.getMaxAllowedKeyLength(transformation);
74         //
75         // test 1
76         //
77         if (TEST_KEY_1.length*8 <= MAX_KEY_SIZE) {
78             SecretKey sKey = new SecretKeySpec(TEST_KEY_1, "Blowfish");
79             try {
80                 cipher.init(Cipher.ENCRYPT_MODE, sKey);
81                 byte[] c1 = cipher.doFinal(TV_P1);
82                 if (!Arrays.equals(c1, TV_C1))
83                     throw new Exception("Encryption (Test vector 1) failed");
84 
85                 cipher.init(Cipher.DECRYPT_MODE, sKey);
86                 byte[] p1 = cipher.doFinal(c1);
87                 if (!Arrays.equals(p1, TV_P1))
88                     throw new Exception("Decryption (Test vector 1) failed");
89             } catch (SecurityException se) {
90                 TestUtil.handleSE(se);
91             }
92         }
93         //
94         // test 2
95         //
96         byte[] testKey2 = S_TEST_KEY_2.getBytes();
97         if (testKey2.length*8 <= MAX_KEY_SIZE) {
98             SecretKey sKey = new SecretKeySpec(testKey2, "Blowfish");
99             try {
100                 cipher.init(Cipher.ENCRYPT_MODE, sKey);
101                 byte[] c2 = cipher.doFinal(TV_P2);
102                 if (!Arrays.equals(c2, TV_C2))
103                     throw new Exception("Encryption (Test vector 2) failed");
104 
105                 cipher.init(Cipher.DECRYPT_MODE, sKey);
106                 byte[] p2 = cipher.doFinal(c2);
107                 if (!Arrays.equals(p2, TV_P2))
108                     throw new Exception("Decryption (Test vector 2) failed");
109             } catch (SecurityException se) {
110                 TestUtil.handleSE(se);
111             }
112         }
113         System.out.println("Test passed");
114     }
115 
116     /*
117      * Converts a byte to hex digit and writes to the supplied buffer
118      */
byte2hex(byte b, StringBuffer buf)119     static private void byte2hex(byte b, StringBuffer buf) {
120         char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
121                             '9', 'A', 'B', 'C', 'D', 'E', 'F' };
122         int high = ((b & 0xf0) >> 4);
123         int low = (b & 0x0f);
124         buf.append(hexChars[high]);
125         buf.append(hexChars[low]);
126     }
127 
128     /*
129      * Converts a byte array to hex string
130      */
toHexString(byte[] block)131     static private String toHexString(byte[] block) {
132         StringBuffer buf = new StringBuffer();
133 
134         int len = block.length;
135 
136         for (int i = 0; i < len; i++) {
137              byte2hex(block[i], buf);
138              if (i < len-1) {
139                  buf.append(":");
140              }
141         }
142         return buf.toString();
143     }
144 }
145