< prev index next >

test/jdk/java/security/Signature/Offsets.java

Print this page


   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.test.lib.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 /test/lib
  45  * @build jdk.test.lib.RandomFactory
  46  * @run main Offsets SUN NONEwithDSA
  47  * @run main Offsets SUN SHA1withDSA
  48  * @run main Offsets SUN SHA224withDSA
  49  * @run main Offsets SUN SHA256withDSA



  50  */
  51 public class Offsets {
  52 
  53     private final int size;
  54     private final byte[] cleartext;
  55     private final PublicKey pubkey;
  56     private final Signature signature;
  57     private final byte[] signed;
  58 
  59     private Offsets(Signature signature, PublicKey pubkey, PrivateKey privkey,
  60             int size, byte[] cleartext) throws InvalidKeyException,
  61                 SignatureException {

  62         this.pubkey = pubkey;
  63         this.signature = signature;
  64         this.size = size;
  65         this.cleartext = cleartext;
  66 

  67         signature.initSign(privkey);
  68         signature.update(cleartext, 0, size);
  69         signed = signature.sign();
  70     }
  71 
  72     int getDataSize() {
  73         return size;
  74     }
  75 
  76     int getSignatureLength() {
  77         return signed.length;
  78     }
  79 
  80     byte[] shiftSignData(int offset) {
  81         byte[] testSignData = new byte[offset + signed.length];
  82         System.arraycopy(signed, 0, testSignData, offset,
  83                 signed.length);
  84         return testSignData;
  85     }
  86 


   1 /*
   2  * Copyright (c) 2015, 2018, 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.*;
  25 import java.security.spec.*;







  26 import jdk.test.lib.RandomFactory;
  27 
  28 /*
  29  * @test
  30  * @bug 8050374 8181048 8146293
  31  * @key randomness
  32  * @summary This test validates signature verification
  33  *          Signature.verify(byte[], int, int). The test uses RandomFactory to
  34  *          get random set of clear text data to sign. After the signature
  35  *          generation, the test tries to verify signature with the above API
  36  *          and passing in different signature offset (0, 33, 66, 99).
  37  * @library /test/lib
  38  * @build jdk.test.lib.RandomFactory
  39  * @run main Offsets SUN NONEwithDSA
  40  * @run main Offsets SUN SHA1withDSA
  41  * @run main Offsets SUN SHA224withDSA
  42  * @run main Offsets SUN SHA256withDSA
  43  * @run main Offsets SunRsaSign SHA512withRSA
  44  * @run main Offsets SunRsaSign SHA512/224withRSA
  45  * @run main Offsets SunRsaSign SHA512/256withRSA
  46  */
  47 public class Offsets {
  48 
  49     private final int size;
  50     private final byte[] cleartext;
  51     private final PublicKey pubkey;
  52     private final Signature signature;
  53     private final byte[] signed;
  54 
  55     private Offsets(Signature signature, PublicKey pubkey, PrivateKey privkey,
  56             int size, byte[] cleartext) throws InvalidKeyException,
  57                 SignatureException {
  58         System.out.println("Testing signature " + signature.getAlgorithm()); 
  59         this.pubkey = pubkey;
  60         this.signature = signature;
  61         this.size = size;
  62         this.cleartext = cleartext;
  63 
  64         String sigAlg = signature.getAlgorithm();
  65         signature.initSign(privkey);
  66         signature.update(cleartext, 0, size);
  67         signed = signature.sign();
  68     }
  69 
  70     int getDataSize() {
  71         return size;
  72     }
  73 
  74     int getSignatureLength() {
  75         return signed.length;
  76     }
  77 
  78     byte[] shiftSignData(int offset) {
  79         byte[] testSignData = new byte[offset + signed.length];
  80         System.arraycopy(signed, 0, testSignData, offset,
  81                 signed.length);
  82         return testSignData;
  83     }
  84 


< prev index next >