test/jdk/java/security/Signature/Offsets.java
branchJDK-8145252-TLS13-branch
changeset 56542 56aaa6cb3693
parent 47421 f9e03aef3a49
child 56592 b1902b22005e
equal deleted inserted replaced
56541:92cbbfc996f3 56542:56aaa6cb3693
     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 SHA512withRSA
       
    44  * @run main Offsets SunRsaSign SHA512/224withRSA
       
    45  * @run main Offsets SunRsaSign SHA512/256withRSA
    50  */
    46  */
    51 public class Offsets {
    47 public class Offsets {
    52 
    48 
    53     private final int size;
    49     private final int size;
    54     private final byte[] cleartext;
    50     private final byte[] cleartext;
    57     private final byte[] signed;
    53     private final byte[] signed;
    58 
    54 
    59     private Offsets(Signature signature, PublicKey pubkey, PrivateKey privkey,
    55     private Offsets(Signature signature, PublicKey pubkey, PrivateKey privkey,
    60             int size, byte[] cleartext) throws InvalidKeyException,
    56             int size, byte[] cleartext) throws InvalidKeyException,
    61                 SignatureException {
    57                 SignatureException {
       
    58         System.out.println("Testing signature " + signature.getAlgorithm()); 
    62         this.pubkey = pubkey;
    59         this.pubkey = pubkey;
    63         this.signature = signature;
    60         this.signature = signature;
    64         this.size = size;
    61         this.size = size;
    65         this.cleartext = cleartext;
    62         this.cleartext = cleartext;
    66 
    63 
       
    64         String sigAlg = signature.getAlgorithm();
    67         signature.initSign(privkey);
    65         signature.initSign(privkey);
    68         signature.update(cleartext, 0, size);
    66         signature.update(cleartext, 0, size);
    69         signed = signature.sign();
    67         signed = signature.sign();
    70     }
    68     }
    71 
    69 
    84         return testSignData;
    82         return testSignData;
    85     }
    83     }
    86 
    84 
    87     boolean verifySignature(byte[] sigData, int sigOffset, int sigLength,
    85     boolean verifySignature(byte[] sigData, int sigOffset, int sigLength,
    88             int updateOffset, int updateLength)
    86             int updateOffset, int updateLength)
    89                 throws InvalidKeyException, SignatureException {
    87             throws InvalidKeyException, SignatureException {
    90         signature.initVerify(pubkey);
    88         signature.initVerify(pubkey);
    91         signature.update(cleartext, updateOffset, updateLength);
    89         signature.update(cleartext, updateOffset, updateLength);
    92         return signature.verify(sigData, sigOffset, sigLength);
    90         return signature.verify(sigData, sigOffset, sigLength);
    93     }
    91     }
    94 
    92