< prev index next >

test/java/security/Signature/Offsets.java

Print this page
rev 12543 : 8181048: Refactor existing providers to refer to the same constants for default values for key length
Reviewed-by: mullan, ahgross
   1 /*
   2  * Copyright (c) 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 import java.security.InvalidKeyException;
  25 import java.security.KeyPair;
  26 import java.security.KeyPairGenerator;
  27 import java.security.NoSuchAlgorithmException;
  28 import java.security.NoSuchProviderException;
  29 import java.security.PrivateKey;
  30 import java.security.PublicKey;
  31 import java.security.Signature;
  32 import java.security.SignatureException;
  33 import jdk.testlibrary.RandomFactory;
  34 
  35 /*
  36  * @test
  37  * @bug 8050374
  38  * @key randomness
  39  * @summary This test validates signature verification
  40  *          Signature.verify(byte[], int, int). The test uses RandomFactory to
  41  *          get random set of clear text data to sign. After the signature
  42  *          generation, the test tries to verify signature with the above API
  43  *          and passing in different signature offset (0, 33, 66, 99).
  44  * @library /lib/testlibrary
  45  * @run main Offsets SUN NONEwithDSA
  46  * @run main Offsets SUN SHA1withDSA
  47  * @run main Offsets SUN SHA224withDSA
  48  * @run main Offsets SUN SHA256withDSA
  49  */
  50 public class Offsets {
  51 
  52     private final int size;
  53     private final byte[] cleartext;
  54     private final PublicKey pubkey;
  55     private final Signature signature;
  56     private final byte[] signed;
  57 


  88                 throws InvalidKeyException, SignatureException {
  89         signature.initVerify(pubkey);
  90         signature.update(cleartext, updateOffset, updateLength);
  91         return signature.verify(sigData, sigOffset, sigLength);
  92     }
  93 
  94     static Offsets init(String provider, String algorithm)
  95             throws NoSuchAlgorithmException, NoSuchProviderException,
  96             InvalidKeyException, SignatureException {
  97         // fill the cleartext data with random bytes
  98         byte[] cleartext = new byte[100];
  99         RandomFactory.getRandom().nextBytes(cleartext);
 100 
 101         // NONEwith requires input to be of 20 bytes
 102         int size = algorithm.contains("NONEwith") ? 20 : 100;
 103 
 104         // create signature instance
 105         Signature signature = Signature.getInstance(algorithm, provider);
 106 
 107         String keyAlgo;

 108         if (algorithm.contains("RSA")) {
 109             keyAlgo = "RSA";
 110         } else if (algorithm.contains("ECDSA")) {
 111             keyAlgo = "EC";

 112         } else if (algorithm.contains("DSA")) {
 113             keyAlgo = "DSA";




 114         } else {
 115             throw new RuntimeException("Test doesn't support this signature "
 116                     + "algorithm: " + algorithm);
 117         }
 118 
 119         KeyPairGenerator kpg = KeyPairGenerator.getInstance(keyAlgo, provider);

 120         KeyPair kp = kpg.generateKeyPair();
 121         PublicKey pubkey = kp.getPublic();
 122         PrivateKey privkey = kp.getPrivate();
 123 
 124         return new Offsets(signature, pubkey, privkey, size, cleartext);
 125     }
 126 
 127     public static void main(String[] args) throws NoSuchAlgorithmException,
 128             InvalidKeyException, SignatureException {
 129         if (args.length < 2) {
 130             throw new RuntimeException("Wrong parameters");
 131         }
 132 
 133         boolean result = true;
 134         try {
 135             Offsets test = init(args[0], args[1]);
 136 
 137             // We are trying 3 different offsets, data size has nothing to do
 138             // with signature length
 139             for (int chunk = 3; chunk > 0; chunk--) {


   1 /*
   2  * Copyright (c) 2015, 2017, 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 import java.security.InvalidKeyException;
  25 import java.security.KeyPair;
  26 import java.security.KeyPairGenerator;
  27 import java.security.NoSuchAlgorithmException;
  28 import java.security.NoSuchProviderException;
  29 import java.security.PrivateKey;
  30 import java.security.PublicKey;
  31 import java.security.Signature;
  32 import java.security.SignatureException;
  33 import jdk.testlibrary.RandomFactory;
  34 
  35 /*
  36  * @test
  37  * @bug 8050374 8181048
  38  * @key randomness
  39  * @summary This test validates signature verification
  40  *          Signature.verify(byte[], int, int). The test uses RandomFactory to
  41  *          get random set of clear text data to sign. After the signature
  42  *          generation, the test tries to verify signature with the above API
  43  *          and passing in different signature offset (0, 33, 66, 99).
  44  * @library /lib/testlibrary
  45  * @run main Offsets SUN NONEwithDSA
  46  * @run main Offsets SUN SHA1withDSA
  47  * @run main Offsets SUN SHA224withDSA
  48  * @run main Offsets SUN SHA256withDSA
  49  */
  50 public class Offsets {
  51 
  52     private final int size;
  53     private final byte[] cleartext;
  54     private final PublicKey pubkey;
  55     private final Signature signature;
  56     private final byte[] signed;
  57 


  88                 throws InvalidKeyException, SignatureException {
  89         signature.initVerify(pubkey);
  90         signature.update(cleartext, updateOffset, updateLength);
  91         return signature.verify(sigData, sigOffset, sigLength);
  92     }
  93 
  94     static Offsets init(String provider, String algorithm)
  95             throws NoSuchAlgorithmException, NoSuchProviderException,
  96             InvalidKeyException, SignatureException {
  97         // fill the cleartext data with random bytes
  98         byte[] cleartext = new byte[100];
  99         RandomFactory.getRandom().nextBytes(cleartext);
 100 
 101         // NONEwith requires input to be of 20 bytes
 102         int size = algorithm.contains("NONEwith") ? 20 : 100;
 103 
 104         // create signature instance
 105         Signature signature = Signature.getInstance(algorithm, provider);
 106 
 107         String keyAlgo;
 108         int keySize = 2048;
 109         if (algorithm.contains("RSA")) {
 110             keyAlgo = "RSA";
 111         } else if (algorithm.contains("ECDSA")) {
 112             keyAlgo = "EC";
 113             keySize = 256;
 114         } else if (algorithm.contains("DSA")) {
 115             keyAlgo = "DSA";
 116             if (algorithm.startsWith("SHAwith") ||
 117                     algorithm.startsWith("SHA1with")) {
 118                 keySize = 1024;
 119             }
 120         } else {
 121             throw new RuntimeException("Test doesn't support this signature "
 122                     + "algorithm: " + algorithm);
 123         }
 124 
 125         KeyPairGenerator kpg = KeyPairGenerator.getInstance(keyAlgo, provider);
 126         kpg.initialize(keySize);
 127         KeyPair kp = kpg.generateKeyPair();
 128         PublicKey pubkey = kp.getPublic();
 129         PrivateKey privkey = kp.getPrivate();
 130 
 131         return new Offsets(signature, pubkey, privkey, size, cleartext);
 132     }
 133 
 134     public static void main(String[] args) throws NoSuchAlgorithmException,
 135             InvalidKeyException, SignatureException {
 136         if (args.length < 2) {
 137             throw new RuntimeException("Wrong parameters");
 138         }
 139 
 140         boolean result = true;
 141         try {
 142             Offsets test = init(args[0], args[1]);
 143 
 144             // We are trying 3 different offsets, data size has nothing to do
 145             // with signature length
 146             for (int chunk = 3; chunk > 0; chunk--) {


< prev index next >