001: package org.bouncycastle.jce.provider.test;
002:
003: import java.math.BigInteger;
004: import java.security.AlgorithmParameters;
005: import java.security.KeyFactory;
006: import java.security.PrivateKey;
007: import java.security.PublicKey;
008: import java.security.SecureRandom;
009: import java.security.Security;
010: import java.security.Signature;
011: import java.security.spec.MGF1ParameterSpec;
012: import java.security.spec.PSSParameterSpec;
013: import java.security.spec.RSAPrivateCrtKeySpec;
014: import java.security.spec.RSAPublicKeySpec;
015:
016: import org.bouncycastle.jce.provider.BouncyCastleProvider;
017: import org.bouncycastle.util.encoders.Hex;
018: import org.bouncycastle.util.test.SimpleTestResult;
019: import org.bouncycastle.util.test.Test;
020: import org.bouncycastle.util.test.TestResult;
021:
022: public class PSSTest implements Test {
023: private class FixedRandom extends SecureRandom {
024: byte[] vals;
025:
026: FixedRandom(byte[] vals) {
027: this .vals = vals;
028: }
029:
030: public void nextBytes(byte[] bytes) {
031: System.arraycopy(vals, 0, bytes, 0, vals.length);
032: }
033: }
034:
035: private boolean arrayEquals(byte[] a, byte[] b) {
036: if (a.length != b.length) {
037: return false;
038: }
039:
040: for (int i = 0; i != a.length; i++) {
041: if (a[i] != b[i]) {
042: return false;
043: }
044: }
045:
046: return true;
047: }
048:
049: private RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(
050: new BigInteger(
051: "a56e4a0e701017589a5187dc7ea841d156f2ec0e36ad52a44dfeb1e61f7ad991d8c51056ffedb162b4c0f283a12a88a394dff526ab7291cbb307ceabfce0b1dfd5cd9508096d5b2b8b6df5d671ef6377c0921cb23c270a70e2598e6ff89d19f105acc2d3f0cb35f29280e1386b6f64c4ef22e1e1f20d0ce8cffb2249bd9a2137",
052: 16), new BigInteger("010001", 16));
053:
054: private RSAPrivateCrtKeySpec privKeySpec = new RSAPrivateCrtKeySpec(
055: new BigInteger(
056: "a56e4a0e701017589a5187dc7ea841d156f2ec0e36ad52a44dfeb1e61f7ad991d8c51056ffedb162b4c0f283a12a88a394dff526ab7291cbb307ceabfce0b1dfd5cd9508096d5b2b8b6df5d671ef6377c0921cb23c270a70e2598e6ff89d19f105acc2d3f0cb35f29280e1386b6f64c4ef22e1e1f20d0ce8cffb2249bd9a2137",
057: 16),
058: new BigInteger("010001", 16),
059: new BigInteger(
060: "33a5042a90b27d4f5451ca9bbbd0b44771a101af884340aef9885f2a4bbe92e894a724ac3c568c8f97853ad07c0266c8c6a3ca0929f1e8f11231884429fc4d9ae55fee896a10ce707c3ed7e734e44727a39574501a532683109c2abacaba283c31b4bd2f53c3ee37e352cee34f9e503bd80c0622ad79c6dcee883547c6a3b325",
061: 16),
062: new BigInteger(
063: "e7e8942720a877517273a356053ea2a1bc0c94aa72d55c6e86296b2dfc967948c0a72cbccca7eacb35706e09a1df55a1535bd9b3cc34160b3b6dcd3eda8e6443",
064: 16),
065: new BigInteger(
066: "b69dca1cf7d4d7ec81e75b90fcca874abcde123fd2700180aa90479b6e48de8d67ed24f9f19d85ba275874f542cd20dc723e6963364a1f9425452b269a6799fd",
067: 16),
068: new BigInteger(
069: "28fa13938655be1f8a159cbaca5a72ea190c30089e19cd274a556f36c4f6e19f554b34c077790427bbdd8dd3ede2448328f385d81b30e8e43b2fffa027861979",
070: 16),
071: new BigInteger(
072: "1a8b38f398fa712049898d7fb79ee0a77668791299cdfa09efc0e507acb21ed74301ef5bfd48be455eaeb6e1678255827580a8e4e8e14151d1510a82a3f2e729",
073: 16),
074: new BigInteger(
075: "27156aba4126d24a81f3a528cbfb27f56886f840a9f6e86e17a44b94fe9319584b8e22fdde1e5a2e3bd8aa5ba8d8584194eb2190acf832b847f13a3d24a79f4d",
076: 16));
077:
078: // PSSExample1.1
079:
080: private byte[] msg1a = Hex
081: .decode("cdc87da223d786df3b45e0bbbc721326d1ee2af806cc315475cc6f0d9c66e1b62371d45ce2392e1ac92844c310102f156a0d8d52c1f4c40ba3aa65095786cb769757a6563ba958fed0bcc984e8b517a3d5f515b23b8a41e74aa867693f90dfb061a6e86dfaaee64472c00e5f20945729cbebe77f06ce78e08f4098fba41f9d6193c0317e8b60d4b6084acb42d29e3808a3bc372d85e331170fcbf7cc72d0b71c296648b3a4d10f416295d0807aa625cab2744fd9ea8fd223c42537029828bd16be02546f130fd2e33b936d2676e08aed1b73318b750a0167d0");
082:
083: private byte[] slt1a = Hex
084: .decode("dee959c7e06411361420ff80185ed57f3e6776af");
085:
086: private byte[] sig1a = Hex
087: .decode("9074308fb598e9701b2294388e52f971faac2b60a5145af185df5287b5ed2887e57ce7fd44dc8634e407c8e0e4360bc226f3ec227f9d9e54638e8d31f5051215df6ebb9c2f9579aa77598a38f914b5b9c1bd83c4e2f9f382a0d0aa3542ffee65984a601bc69eb28deb27dca12c82c2d4c3f66cd500f1ff2b994d8a4e30cbb33c");
088:
089: private byte[] sig1b = Hex
090: .decode("96ea348db4db2947aee807bd687411a880913706f21b383a1002b97e43656e5450a9d1812efbedd1ed159f8307986adf48bada66a8efd14bd9e2f6f6f458e73b50c8ce6e3079011c5b4bd1600a2601a66198a1582574a43f13e0966c6c2337e6ca0886cd9e1b1037aeadef1382117d22b35e7e4403f90531c8cfccdf223f98e4");
091:
092: private byte[] sig1c = Hex
093: .decode("9e64cc1062c537b142480bc5af407b55904ead970e20e0f8f6664279c96c6da6b03522160f224a85cc413dfe6bd00621485b665abac6d90ff38c9af06f4ddd6c7c81540439e5795601a1343d9feb465712ff8a5f5150391522fb5a9b8e2225a555f4efaa5e5c0ed7a19b27074c2d9f6dbbd0c893ba02c4a35b115d337bccd7a2");
094:
095: public TestResult perform() {
096: try {
097: KeyFactory fact = KeyFactory.getInstance("RSA", "BC");
098:
099: PrivateKey privKey = fact.generatePrivate(privKeySpec);
100: PublicKey pubKey = fact.generatePublic(pubKeySpec);
101:
102: Signature s = Signature
103: .getInstance("SHA1withRSA/PSS", "BC");
104:
105: s.initSign(privKey, new FixedRandom(slt1a));
106: s.update(msg1a);
107: byte[] sig = s.sign();
108:
109: if (!arrayEquals(sig1a, sig)) {
110: return new SimpleTestResult(false,
111: "PSS Sign test expected "
112: + new String(Hex.encode(sig1a))
113: + " got " + new String(Hex.encode(sig)));
114: }
115:
116: s = Signature.getInstance("SHA1withRSAandMGF1", "BC");
117:
118: s.initVerify(pubKey);
119: s.update(msg1a);
120: if (!s.verify(sig1a)) {
121: return new SimpleTestResult(false,
122: "SHA1 signature verification failed");
123: }
124:
125: s = Signature.getInstance("SHA1withRSAandMGF1", "BC");
126:
127: s.setParameter(PSSParameterSpec.DEFAULT);
128:
129: s.initVerify(pubKey);
130: s.update(msg1a);
131: if (!s.verify(sig1a)) {
132: return new SimpleTestResult(false,
133: "SHA1 signature verification with default parameters failed");
134: }
135:
136: AlgorithmParameters pss = s.getParameters();
137: if (!arrayEquals(pss.getEncoded(),
138: new byte[] { 0x30, 0x00 })) {
139: return new SimpleTestResult(false,
140: "failed default encoding test.");
141: }
142:
143: s = Signature.getInstance("SHA256withRSA/PSS", "BC");
144:
145: s.initSign(privKey, new FixedRandom(slt1a));
146: s.update(msg1a);
147: sig = s.sign();
148:
149: pss = s.getParameters();
150:
151: if (!arrayEquals(sig1b, sig)) {
152: return new SimpleTestResult(false,
153: "PSS Sign test expected "
154: + new String(Hex.encode(sig1b))
155: + " got " + new String(Hex.encode(sig)));
156: }
157:
158: s = Signature.getInstance("SHA256withRSAandMGF1", "BC");
159:
160: s
161: .setParameter(pss
162: .getParameterSpec(PSSParameterSpec.class));
163:
164: s.initVerify(pubKey);
165: s.update(msg1a);
166: if (!s.verify(sig1b)) {
167: return new SimpleTestResult(false,
168: "SHA256 signature verification failed");
169: }
170:
171: //
172: // 512 test -with zero salt length
173: //
174: s = Signature.getInstance("SHA512withRSAandMGF1", "BC");
175:
176: s.setParameter(new PSSParameterSpec("SHA-512", "MGF1",
177: new MGF1ParameterSpec("SHA-512"), 0, 1));
178: s.initSign(privKey);
179:
180: s.update(msg1a);
181: sig = s.sign();
182:
183: pss = s.getParameters();
184:
185: if (!arrayEquals(sig1c, sig)) {
186: return new SimpleTestResult(false,
187: "PSS Sign test expected "
188: + new String(Hex.encode(sig1c))
189: + " got " + new String(Hex.encode(sig)));
190: }
191:
192: s = Signature.getInstance("SHA512withRSAandMGF1", "BC");
193:
194: s
195: .setParameter(pss
196: .getParameterSpec(PSSParameterSpec.class));
197:
198: s.initVerify(pubKey);
199: s.update(msg1a);
200: if (!s.verify(sig1c)) {
201: return new SimpleTestResult(false,
202: "SHA512 signature verification failed");
203: }
204:
205: return new SimpleTestResult(true, getName() + ": Okay");
206: } catch (Exception e) {
207: return new SimpleTestResult(false, getName()
208: + ": exception - " + e.toString(), e);
209: }
210: }
211:
212: public String getName() {
213: return "PSSTest";
214: }
215:
216: public static void main(String[] args) {
217: Security.addProvider(new BouncyCastleProvider());
218:
219: Test test = new PSSTest();
220: TestResult result = test.perform();
221:
222: System.out.println(result.toString());
223: }
224: }
|