test/jdk/sun/security/rsa/pss/SignatureTest2.java
author valeriep
Mon, 21 May 2018 23:40:52 +0000
changeset 50204 3195a713e24d
permissions -rw-r--r--
8146293: Add support for RSASSA-PSS Signature algorithm Summary: add RSASSA-PSS key and signature support to SunRsaSign provider Reviewed-by: wetmore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
     1
/*
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
     4
 *
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
     7
 * published by the Free Software Foundation.
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
     8
 *
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    13
 * accompanied this code).
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    14
 *
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    18
 *
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    21
 * questions.
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    22
 */
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    23
import java.security.*;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    24
import java.security.interfaces.RSAPrivateKey;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    25
import java.security.interfaces.RSAPublicKey;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    26
import java.security.spec.*;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    27
import java.util.Arrays;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    28
import java.util.stream.IntStream;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    29
import static javax.crypto.Cipher.PRIVATE_KEY;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    30
import static javax.crypto.Cipher.PUBLIC_KEY;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    31
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    32
/**
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    33
 * @test
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    34
 * @bug 8146293
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    35
 * @summary Create a signature for RSA and get its signed data. re-initiate
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    36
 *          the signature with the public key. The signature can be verified
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    37
 *          by acquired signed data.
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    38
 * @run main SignatureTest2 768
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    39
 * @run main SignatureTest2 1024
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    40
 * @run main SignatureTest2 2048
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    41
 * @run main/timeout=240 SignatureTest2 4096
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    42
 */
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    43
public class SignatureTest2 {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    44
    /**
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    45
     * ALGORITHM name, fixed as RSA.
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    46
     */
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    47
    private static final String KEYALG = "RSASSA-PSS";
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    48
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    49
    /**
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    50
     * JDK default RSA Provider.
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    51
     */
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    52
    private static final String PROVIDER = "SunRsaSign";
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    53
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    54
    /**
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    55
     * How much times signature updated.
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    56
     */
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    57
    private static final int UPDATE_TIMES_TWO = 2;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    58
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    59
    /**
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    60
     * How much times signature initial updated.
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    61
     */
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    62
    private static final int UPDATE_TIMES_TEN = 10;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    63
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    64
    /**
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    65
     * Digest algorithms to test w/ RSASSA-PSS signature algorithms
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    66
     */
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    67
    private static final String[] DIGEST_ALG = {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    68
        "SHA-1", "SHA-224", "SHA-256", "SHA-384",
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    69
        "SHA-512", "SHA-512/224", "SHA-512/256"
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    70
    };
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    71
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    72
    private static final String SIG_ALG = "RSASSA-PSS";
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    73
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    74
    private static PSSParameterSpec genPSSParameter(String digestAlgo,
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    75
        int digestLen, int keySize) {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    76
        // pick a salt length based on the key length and digestAlgo
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    77
        int saltLength = keySize/8 - digestLen - 2;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    78
        if (saltLength < 0) {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    79
            System.out.println("keysize: " + keySize/8 + ", digestLen: " + digestLen);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    80
            return null;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    81
        }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    82
        return new PSSParameterSpec(digestAlgo, "MGF1",
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    83
            new MGF1ParameterSpec(digestAlgo), saltLength, 1);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    84
    }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    85
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    86
    public static void main(String[] args) throws Exception {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    87
        final int testSize = Integer.parseInt(args[0]);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    88
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    89
        byte[] data = new byte[100];
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    90
        IntStream.range(0, data.length).forEach(j -> {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    91
            data[j] = (byte) j;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    92
        });
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    93
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    94
        // create a key pair
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    95
        KeyPair kpair = generateKeys(KEYALG, testSize);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    96
        Key[] privs = manipulateKey(PRIVATE_KEY, kpair.getPrivate());
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    97
        Key[] pubs = manipulateKey(PUBLIC_KEY, kpair.getPublic());
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    98
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
    99
        // For messsage digest algorithm, create and verify a RSASSA-PSS signature
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   100
        Arrays.stream(privs).forEach(priv
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   101
                -> Arrays.stream(pubs).forEach(pub
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   102
                -> Arrays.stream(DIGEST_ALG).forEach(testAlg -> {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   103
            checkSignature(data, (PublicKey) pub, (PrivateKey) priv,
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   104
                    testAlg, testSize);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   105
        }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   106
        )));
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   107
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   108
    }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   109
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   110
    private static KeyPair generateKeys(String keyalg, int size)
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   111
            throws NoSuchAlgorithmException {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   112
        KeyPairGenerator kpg = KeyPairGenerator.getInstance(keyalg);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   113
        kpg.initialize(size);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   114
        return kpg.generateKeyPair();
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   115
    }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   116
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   117
    private static Key[] manipulateKey(int type, Key key)
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   118
            throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   119
        KeyFactory kf = KeyFactory.getInstance(KEYALG, PROVIDER);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   120
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   121
        switch (type) {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   122
            case PUBLIC_KEY:
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   123
                return new Key[]{
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   124
                    kf.generatePublic(kf.getKeySpec(key, RSAPublicKeySpec.class)),
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   125
                    kf.generatePublic(new X509EncodedKeySpec(key.getEncoded())),
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   126
                    kf.generatePublic(new RSAPublicKeySpec(
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   127
                    ((RSAPublicKey) key).getModulus(),
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   128
                    ((RSAPublicKey) key).getPublicExponent()))
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   129
                };
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   130
            case PRIVATE_KEY:
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   131
                return new Key[]{
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   132
                    kf.generatePrivate(kf.getKeySpec(key,
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   133
                    RSAPrivateKeySpec.class)),
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   134
                    kf.generatePrivate(new PKCS8EncodedKeySpec(
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   135
                    key.getEncoded())),
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   136
                    kf.generatePrivate(new RSAPrivateKeySpec(((RSAPrivateKey) key).getModulus(),
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   137
                    ((RSAPrivateKey) key).getPrivateExponent()))
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   138
                };
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   139
        }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   140
        throw new RuntimeException("We shouldn't reach here");
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   141
    }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   142
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   143
    private static void checkSignature(byte[] data, PublicKey pub,
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   144
            PrivateKey priv, String digestAlg, int keySize) throws RuntimeException {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   145
        try {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   146
            Signature sig = Signature.getInstance(SIG_ALG, PROVIDER);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   147
            int digestLen = MessageDigest.getInstance(digestAlg).getDigestLength();
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   148
            PSSParameterSpec params = genPSSParameter(digestAlg, digestLen, keySize);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   149
            if (params == null) {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   150
                System.out.println("Skip test due to short key size");
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   151
                return;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   152
            }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   153
            sig.setParameter(params);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   154
            sig.initSign(priv);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   155
            for (int i = 0; i < UPDATE_TIMES_TEN; i++) {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   156
                sig.update(data);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   157
            }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   158
            byte[] signedDataTen = sig.sign();
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   159
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   160
            // Make sure signature can be generated without re-init
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   161
            sig.update(data);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   162
            byte[] signedDataOne = sig.sign();
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   163
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   164
            // Make sure signature verifies with original data
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   165
            System.out.println("Verify using params " + sig.getParameters());
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   166
            sig.initVerify(pub);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   167
            sig.setParameter(params);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   168
            for (int i = 0; i < UPDATE_TIMES_TEN; i++) {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   169
                sig.update(data);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   170
            }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   171
            if (!sig.verify(signedDataTen)) {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   172
                throw new RuntimeException("Signature verification test#1 failed w/ "
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   173
                    + digestAlg);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   174
            }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   175
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   176
            // Make sure signature can verify without re-init
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   177
            sig.update(data);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   178
            if (!sig.verify(signedDataOne)) {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   179
                throw new RuntimeException("Signature verification test#2 failed w/ "
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   180
                    + digestAlg);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   181
            }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   182
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   183
            // Make sure signature does NOT verify when the original data
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   184
            // has changed
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   185
            for (int i = 0; i < UPDATE_TIMES_TWO; i++) {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   186
                sig.update(data);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   187
            }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   188
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   189
            if (sig.verify(signedDataOne)) {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   190
                throw new RuntimeException("Bad signature accepted w/ "
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   191
                    + digestAlg);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   192
            }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   193
        } catch (NoSuchAlgorithmException | InvalidKeyException |
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   194
                 SignatureException | NoSuchProviderException |
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   195
                 InvalidAlgorithmParameterException e) {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   196
            e.printStackTrace();
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   197
            throw new RuntimeException(e);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   198
        }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   199
    }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
diff changeset
   200
}