J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003-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 4894151 |
| 27 | * @summary known answer test for OAEP encryption |
| 28 | * @author Andreas Sterbenz |
| 29 | */ |
| 30 | |
| 31 | import java.io.*; |
| 32 | import java.math.BigInteger; |
| 33 | import java.util.*; |
| 34 | import java.util.regex.*; |
| 35 | |
| 36 | import java.security.*; |
| 37 | import java.security.spec.*; |
| 38 | |
| 39 | import javax.crypto.*; |
| 40 | |
| 41 | /** |
| 42 | * Known answer test for OAEP encryption. The "oaep-vect.txt" file was taken |
| 43 | * from the RSA Security web site. It contains a number of test cases using |
| 44 | * keys of various lengths. |
| 45 | * |
| 46 | * Note that we only test decryption. We cannot do a KAT encryption test |
| 47 | * because our APIs do now allow us to explicitly specify the seed. |
| 48 | * Encryption is tested in a different test case. |
| 49 | */ |
| 50 | public class TestOAEP_KAT { |
| 51 | |
| 52 | private final static String BASE = System.getProperty("test.src", "."); |
| 53 | |
| 54 | private static BigInteger n, e, d, p, q, pe, qe, coeff; |
| 55 | |
| 56 | private static byte[] plainText, seed, cipherText, cipherText2; |
| 57 | |
| 58 | public static void main(String[] args) throws Exception { |
| 59 | long start = System.currentTimeMillis(); |
| 60 | Provider provider = Security.getProvider("SunJCE"); |
| 61 | Provider kfProvider = Security.getProvider("SunRsaSign"); |
| 62 | System.out.println("Testing provider " + provider.getName() + "..."); |
| 63 | Cipher c = Cipher.getInstance("RSA/ECB/OAEPwithSHA1andMGF1Padding", provider); |
| 64 | KeyFactory kf = KeyFactory.getInstance("RSA", kfProvider); |
| 65 | InputStream in = new FileInputStream(new File(BASE, "oaep-vect.txt")); |
| 66 | BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF8")); |
| 67 | while (true) { |
| 68 | String line = reader.readLine(); |
| 69 | if (line == null) { |
| 70 | break; |
| 71 | } |
| 72 | line = line.trim(); |
| 73 | if (line.length() == 0) { |
| 74 | continue; |
| 75 | } |
| 76 | if (line.equals("# RSA modulus n:")) { |
| 77 | n = parseNumber(reader); |
| 78 | } else if (line.equals("# RSA public exponent e:")) { |
| 79 | e = parseNumber(reader); |
| 80 | } else if (line.equals("# RSA private exponent d:")) { |
| 81 | d = parseNumber(reader); |
| 82 | } else if (line.equals("# Prime p:")) { |
| 83 | p = parseNumber(reader); |
| 84 | } else if (line.equals("# Prime q:")) { |
| 85 | q = parseNumber(reader); |
| 86 | } else if (line.equals("# p's CRT exponent dP:")) { |
| 87 | pe = parseNumber(reader); |
| 88 | } else if (line.equals("# q's CRT exponent dQ:")) { |
| 89 | qe = parseNumber(reader); |
| 90 | } else if (line.equals("# CRT coefficient qInv:")) { |
| 91 | coeff = parseNumber(reader); |
| 92 | } else if (line.equals("# Message to be encrypted:")) { |
| 93 | plainText = parseBytes(reader); |
| 94 | } else if (line.equals("# Seed:")) { |
| 95 | seed = parseBytes(reader); |
| 96 | } else if (line.equals("# Encryption:")) { |
| 97 | cipherText = parseBytes(reader); |
| 98 | // do encryption test first |
| 99 | KeySpec pubSpec = new RSAPublicKeySpec(n, e); |
| 100 | PublicKey pubKey = kf.generatePublic(pubSpec); |
| 101 | c.init(Cipher.ENCRYPT_MODE, pubKey, new MyRandom(seed)); |
| 102 | cipherText2 = c.doFinal(plainText); |
| 103 | if (Arrays.equals(cipherText2, cipherText) == false) { |
| 104 | throw new Exception("Encryption mismatch"); |
| 105 | } |
| 106 | // followed by decryption test |
| 107 | KeySpec privSpec = new RSAPrivateCrtKeySpec(n, e, d, p, q, pe, qe, coeff); |
| 108 | PrivateKey privKey = kf.generatePrivate(privSpec); |
| 109 | c.init(Cipher.DECRYPT_MODE, privKey); |
| 110 | byte[] dec = c.doFinal(cipherText); |
| 111 | if (Arrays.equals(plainText, dec) == false) { |
| 112 | throw new Exception("Decryption mismatch"); |
| 113 | } |
| 114 | } else if (line.startsWith("# ------------------------------")) { |
| 115 | // ignore, do not print |
| 116 | } else { |
| 117 | // unknown line (comment), print |
| 118 | System.out.println(": " + line); |
| 119 | } |
| 120 | } |
| 121 | long stop = System.currentTimeMillis(); |
| 122 | System.out.println("Done (" + (stop - start) + " ms)."); |
| 123 | } |
| 124 | |
| 125 | private static BigInteger parseNumber(BufferedReader reader) throws IOException { |
| 126 | return new BigInteger(1, parseBytes(reader)); |
| 127 | } |
| 128 | |
| 129 | private static byte[] parseBytes(BufferedReader reader) throws IOException { |
| 130 | ByteArrayOutputStream buffer = new ByteArrayOutputStream(); |
| 131 | while (true) { |
| 132 | String line = reader.readLine(); |
| 133 | if (line == null) { |
| 134 | throw new EOFException("Unexpected EOF"); |
| 135 | } |
| 136 | line = line.trim(); |
| 137 | if (line.length() == 0) { |
| 138 | break; |
| 139 | } |
| 140 | buffer.write(parse(line)); |
| 141 | } |
| 142 | return buffer.toByteArray(); |
| 143 | } |
| 144 | |
| 145 | public static byte[] parse(String s) { |
| 146 | try { |
| 147 | int n = s.length(); |
| 148 | ByteArrayOutputStream out = new ByteArrayOutputStream(n / 3); |
| 149 | StringReader r = new StringReader(s); |
| 150 | while (true) { |
| 151 | int b1 = nextNibble(r); |
| 152 | if (b1 < 0) { |
| 153 | break; |
| 154 | } |
| 155 | int b2 = nextNibble(r); |
| 156 | if (b2 < 0) { |
| 157 | throw new RuntimeException("Invalid string " + s); |
| 158 | } |
| 159 | int b = (b1 << 4) | b2; |
| 160 | out.write(b); |
| 161 | } |
| 162 | return out.toByteArray(); |
| 163 | } catch (IOException e) { |
| 164 | throw new RuntimeException(e); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | public static byte[] b(String s) { |
| 169 | return parse(s); |
| 170 | } |
| 171 | |
| 172 | private static int nextNibble(StringReader r) throws IOException { |
| 173 | while (true) { |
| 174 | int ch = r.read(); |
| 175 | if (ch == -1) { |
| 176 | return -1; |
| 177 | } else if ((ch >= '0') && (ch <= '9')) { |
| 178 | return ch - '0'; |
| 179 | } else if ((ch >= 'a') && (ch <= 'f')) { |
| 180 | return ch - 'a' + 10; |
| 181 | } else if ((ch >= 'A') && (ch <= 'F')) { |
| 182 | return ch - 'A' + 10; |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | } |
| 188 | |
| 189 | class MyRandom extends SecureRandom { |
| 190 | |
| 191 | private byte[] source; |
| 192 | private int count; |
| 193 | |
| 194 | MyRandom(byte[] source) { |
| 195 | this.source = (byte[]) source.clone(); |
| 196 | count = 0; |
| 197 | } |
| 198 | |
| 199 | public void nextBytes(byte[] bytes) { |
| 200 | if (bytes.length > source.length - count) { |
| 201 | throw new RuntimeException("Insufficient random data"); |
| 202 | } |
| 203 | System.arraycopy(source, count, bytes, 0, bytes.length); |
| 204 | count += bytes.length; |
| 205 | } |
| 206 | } |