J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1998-2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any 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 | |
| 69 | public static void main(String[] argv) throws Exception { |
| 70 | |
| 71 | Provider p = new com.sun.crypto.provider.SunJCE(); |
| 72 | Security.addProvider(p); |
| 73 | String transformation = "Blowfish/ECB/NoPadding"; |
| 74 | Cipher cipher = Cipher.getInstance(transformation); |
| 75 | int MAX_KEY_SIZE = Cipher.getMaxAllowedKeyLength(transformation); |
| 76 | // |
| 77 | // test 1 |
| 78 | // |
| 79 | if (TEST_KEY_1.length*8 <= MAX_KEY_SIZE) { |
| 80 | SecretKey sKey = new SecretKeySpec(TEST_KEY_1, "Blowfish"); |
| 81 | try { |
| 82 | cipher.init(Cipher.ENCRYPT_MODE, sKey); |
| 83 | byte[] c1 = cipher.doFinal(TV_P1); |
| 84 | if (!Arrays.equals(c1, TV_C1)) |
| 85 | throw new Exception("Encryption (Test vector 1) failed"); |
| 86 | |
| 87 | cipher.init(Cipher.DECRYPT_MODE, sKey); |
| 88 | byte[] p1 = cipher.doFinal(c1); |
| 89 | if (!Arrays.equals(p1, TV_P1)) |
| 90 | throw new Exception("Decryption (Test vector 1) failed"); |
| 91 | } catch (SecurityException se) { |
| 92 | TestUtil.handleSE(se); |
| 93 | } |
| 94 | } |
| 95 | // |
| 96 | // test 2 |
| 97 | // |
| 98 | byte[] testKey2 = S_TEST_KEY_2.getBytes(); |
| 99 | if (testKey2.length*8 <= MAX_KEY_SIZE) { |
| 100 | SecretKey sKey = new SecretKeySpec(testKey2, "Blowfish"); |
| 101 | try { |
| 102 | cipher.init(Cipher.ENCRYPT_MODE, sKey); |
| 103 | byte[] c2 = cipher.doFinal(TV_P2); |
| 104 | if (!Arrays.equals(c2, TV_C2)) |
| 105 | throw new Exception("Encryption (Test vector 2) failed"); |
| 106 | |
| 107 | cipher.init(Cipher.DECRYPT_MODE, sKey); |
| 108 | byte[] p2 = cipher.doFinal(c2); |
| 109 | if (!Arrays.equals(p2, TV_P2)) |
| 110 | throw new Exception("Decryption (Test vector 2) failed"); |
| 111 | } catch (SecurityException se) { |
| 112 | TestUtil.handleSE(se); |
| 113 | } |
| 114 | } |
| 115 | System.out.println("Test passed"); |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * Converts a byte to hex digit and writes to the supplied buffer |
| 120 | */ |
| 121 | static private void byte2hex(byte b, StringBuffer buf) { |
| 122 | char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', |
| 123 | '9', 'A', 'B', 'C', 'D', 'E', 'F' }; |
| 124 | int high = ((b & 0xf0) >> 4); |
| 125 | int low = (b & 0x0f); |
| 126 | buf.append(hexChars[high]); |
| 127 | buf.append(hexChars[low]); |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * Converts a byte array to hex string |
| 132 | */ |
| 133 | static private String toHexString(byte[] block) { |
| 134 | StringBuffer buf = new StringBuffer(); |
| 135 | |
| 136 | int len = block.length; |
| 137 | |
| 138 | for (int i = 0; i < len; i++) { |
| 139 | byte2hex(block[i], buf); |
| 140 | if (i < len-1) { |
| 141 | buf.append(":"); |
| 142 | } |
| 143 | } |
| 144 | return buf.toString(); |
| 145 | } |
| 146 | } |