test/jdk/java/security/Signature/Offsets.java
changeset 50204 3195a713e24d
parent 47421 f9e03aef3a49
child 54333 2a29e62446bd
child 56592 b1902b22005e
equal deleted inserted replaced
50203:39d88709b138 50204:3195a713e24d
     1 /*
     1 /*
     2  * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    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
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 import java.security.InvalidKeyException;
    24 import java.security.*;
    25 import java.security.KeyPair;
    25 import java.security.spec.*;
    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;
    26 import jdk.test.lib.RandomFactory;
    34 
    27 
    35 /*
    28 /*
    36  * @test
    29  * @test
    37  * @bug 8050374 8181048
    30  * @bug 8050374 8181048 8146293
    38  * @key randomness
    31  * @key randomness
    39  * @summary This test validates signature verification
    32  * @summary This test validates signature verification
    40  *          Signature.verify(byte[], int, int). The test uses RandomFactory to
    33  *          Signature.verify(byte[], int, int). The test uses RandomFactory to
    41  *          get random set of clear text data to sign. After the signature
    34  *          get random set of clear text data to sign. After the signature
    42  *          generation, the test tries to verify signature with the above API
    35  *          generation, the test tries to verify signature with the above API
    45  * @build jdk.test.lib.RandomFactory
    38  * @build jdk.test.lib.RandomFactory
    46  * @run main Offsets SUN NONEwithDSA
    39  * @run main Offsets SUN NONEwithDSA
    47  * @run main Offsets SUN SHA1withDSA
    40  * @run main Offsets SUN SHA1withDSA
    48  * @run main Offsets SUN SHA224withDSA
    41  * @run main Offsets SUN SHA224withDSA
    49  * @run main Offsets SUN SHA256withDSA
    42  * @run main Offsets SUN SHA256withDSA
       
    43  * @run main Offsets SunRsaSign SHA224withRSA
       
    44  * @run main Offsets SunRsaSign SHA256withRSA
       
    45  * @run main Offsets SunRsaSign SHA384withRSA
       
    46  * @run main Offsets SunRsaSign SHA512withRSA
       
    47  * @run main Offsets SunRsaSign SHA512/224withRSA
       
    48  * @run main Offsets SunRsaSign SHA512/256withRSA
    50  */
    49  */
    51 public class Offsets {
    50 public class Offsets {
    52 
    51 
    53     private final int size;
    52     private final int size;
    54     private final byte[] cleartext;
    53     private final byte[] cleartext;
    57     private final byte[] signed;
    56     private final byte[] signed;
    58 
    57 
    59     private Offsets(Signature signature, PublicKey pubkey, PrivateKey privkey,
    58     private Offsets(Signature signature, PublicKey pubkey, PrivateKey privkey,
    60             int size, byte[] cleartext) throws InvalidKeyException,
    59             int size, byte[] cleartext) throws InvalidKeyException,
    61                 SignatureException {
    60                 SignatureException {
       
    61         System.out.println("Testing signature " + signature.getAlgorithm());
    62         this.pubkey = pubkey;
    62         this.pubkey = pubkey;
    63         this.signature = signature;
    63         this.signature = signature;
    64         this.size = size;
    64         this.size = size;
    65         this.cleartext = cleartext;
    65         this.cleartext = cleartext;
    66 
    66 
       
    67         String sigAlg = signature.getAlgorithm();
    67         signature.initSign(privkey);
    68         signature.initSign(privkey);
    68         signature.update(cleartext, 0, size);
    69         signature.update(cleartext, 0, size);
    69         signed = signature.sign();
    70         signed = signature.sign();
    70     }
    71     }
    71 
    72 
    84         return testSignData;
    85         return testSignData;
    85     }
    86     }
    86 
    87 
    87     boolean verifySignature(byte[] sigData, int sigOffset, int sigLength,
    88     boolean verifySignature(byte[] sigData, int sigOffset, int sigLength,
    88             int updateOffset, int updateLength)
    89             int updateOffset, int updateLength)
    89                 throws InvalidKeyException, SignatureException {
    90             throws InvalidKeyException, SignatureException {
    90         signature.initVerify(pubkey);
    91         signature.initVerify(pubkey);
    91         signature.update(cleartext, updateOffset, updateLength);
    92         signature.update(cleartext, updateOffset, updateLength);
    92         return signature.verify(sigData, sigOffset, sigLength);
    93         return signature.verify(sigData, sigOffset, sigLength);
    93     }
    94     }
    94 
    95