src/jdk.crypto.ec/share/classes/sun/security/ec/ECDSASignature.java
author valeriep
Thu, 11 Jul 2019 20:11:47 +0000
changeset 55667 6521aec1c26e
parent 54197 ddfb658c8ce3
permissions -rw-r--r--
8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support Summary: Fixed SignatureUtil and ECDSA signature impl to handle EC parameters Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
     1
/*
53397
bc1de01b3e78 8217344: Make comparison overflow-aware in ECDHKeyAgreement.engineGenerateSecret()
igerasim
parents: 53007
diff changeset
     2
 * Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved.
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
     4
 *
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3863
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3863
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    10
 *
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    15
 * accompanied this code).
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    16
 *
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3863
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3863
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3863
diff changeset
    23
 * questions.
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    24
 */
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    25
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    26
package sun.security.ec;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    27
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    28
import java.nio.ByteBuffer;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    29
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    30
import java.security.*;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    31
import java.security.interfaces.*;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    32
import java.security.spec.*;
52946
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
    33
import java.util.Optional;
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    34
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    35
import sun.security.jca.JCAUtil;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    36
import sun.security.util.*;
52946
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
    37
import static sun.security.ec.ECOperations.IntermediateValueException;
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    38
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    39
/**
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    40
 * ECDSA signature implementation. This class currently supports the
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    41
 * following algorithm names:
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    42
 *
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    43
 *   . "NONEwithECDSA"
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    44
 *   . "SHA1withECDSA"
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 10336
diff changeset
    45
 *   . "SHA224withECDSA"
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    46
 *   . "SHA256withECDSA"
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    47
 *   . "SHA384withECDSA"
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    48
 *   . "SHA512withECDSA"
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
    49
 *   . "NONEwithECDSAinP1363Format"
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
    50
 *   . "SHA1withECDSAinP1363Format"
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
    51
 *   . "SHA224withECDSAinP1363Format"
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
    52
 *   . "SHA256withECDSAinP1363Format"
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
    53
 *   . "SHA384withECDSAinP1363Format"
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
    54
 *   . "SHA512withECDSAinP1363Format"
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    55
 *
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    56
 * @since   1.7
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    57
 */
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    58
abstract class ECDSASignature extends SignatureSpi {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    59
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    60
    // message digest implementation we use
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    61
    private final MessageDigest messageDigest;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    62
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    63
    // supplied entropy
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    64
    private SecureRandom random;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    65
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    66
    // flag indicating whether the digest has been reset
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    67
    private boolean needsReset;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    68
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    69
    // private key, if initialized for signing
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    70
    private ECPrivateKey privateKey;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    71
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    72
    // public key, if initialized for verifying
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    73
    private ECPublicKey publicKey;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    74
55667
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
    75
    // signature parameters
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
    76
    private ECParameterSpec sigParams = null;
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
    77
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
    78
    // The format. true for the IEEE P1363 format. false (default) for ASN.1
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
    79
    private final boolean p1363Format;
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
    80
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    81
    /**
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
    82
     * Constructs a new ECDSASignature.
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    83
     *
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    84
     * @exception ProviderException if the native ECC library is unavailable.
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    85
     */
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    86
    ECDSASignature() {
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
    87
        this(false);
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
    88
    }
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
    89
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
    90
    /**
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
    91
     * Constructs a new ECDSASignature that will use the specified
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
    92
     * signature format. {@code p1363Format} should be {@code true} to
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
    93
     * use the IEEE P1363 format. If {@code p1363Format} is {@code false},
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
    94
     * the DER-encoded ASN.1 format will be used. This constructor is
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
    95
     * used by the RawECDSA subclasses.
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
    96
     */
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
    97
    ECDSASignature(boolean p1363Format) {
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
    98
        this.messageDigest = null;
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
    99
        this.p1363Format = p1363Format;
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   100
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   101
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   102
    /**
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   103
     * Constructs a new ECDSASignature. Used by subclasses.
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   104
     */
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   105
    ECDSASignature(String digestName) {
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   106
        this(digestName, false);
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   107
    }
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   108
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   109
    /**
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   110
     * Constructs a new ECDSASignature that will use the specified
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   111
     * digest and signature format. {@code p1363Format} should be
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   112
     * {@code true} to use the IEEE P1363 format. If {@code p1363Format}
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   113
     * is {@code false}, the DER-encoded ASN.1 format will be used. This
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   114
     * constructor is used by subclasses.
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   115
     */
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   116
    ECDSASignature(String digestName, boolean p1363Format) {
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   117
        try {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   118
            messageDigest = MessageDigest.getInstance(digestName);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   119
        } catch (NoSuchAlgorithmException e) {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   120
            throw new ProviderException(e);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   121
        }
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   122
        this.needsReset = false;
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   123
        this.p1363Format = p1363Format;
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   124
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   125
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   126
    // Class for Raw ECDSA signatures.
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   127
    static class RawECDSA extends ECDSASignature {
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   128
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   129
        // the longest supported digest is 512 bits (SHA-512)
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   130
        private static final int RAW_ECDSA_MAX = 64;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   131
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   132
        private final byte[] precomputedDigest;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   133
        private int offset = 0;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   134
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   135
        RawECDSA(boolean p1363Format) {
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   136
            super(p1363Format);
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   137
            precomputedDigest = new byte[RAW_ECDSA_MAX];
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   138
        }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   139
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   140
        // Stores the precomputed message digest value.
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   141
        @Override
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   142
        protected void engineUpdate(byte b) throws SignatureException {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   143
            if (offset >= precomputedDigest.length) {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   144
                offset = RAW_ECDSA_MAX + 1;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   145
                return;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   146
            }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   147
            precomputedDigest[offset++] = b;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   148
        }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   149
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   150
        // Stores the precomputed message digest value.
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   151
        @Override
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   152
        protected void engineUpdate(byte[] b, int off, int len)
52946
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   153
        throws SignatureException {
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   154
            if (offset >= precomputedDigest.length) {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   155
                offset = RAW_ECDSA_MAX + 1;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   156
                return;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   157
            }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   158
            System.arraycopy(b, off, precomputedDigest, offset, len);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   159
            offset += len;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   160
        }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   161
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   162
        // Stores the precomputed message digest value.
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   163
        @Override
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   164
        protected void engineUpdate(ByteBuffer byteBuffer) {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   165
            int len = byteBuffer.remaining();
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   166
            if (len <= 0) {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   167
                return;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   168
            }
53397
bc1de01b3e78 8217344: Make comparison overflow-aware in ECDHKeyAgreement.engineGenerateSecret()
igerasim
parents: 53007
diff changeset
   169
            if (len >= precomputedDigest.length - offset) {
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   170
                offset = RAW_ECDSA_MAX + 1;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   171
                return;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   172
            }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   173
            byteBuffer.get(precomputedDigest, offset, len);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   174
            offset += len;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   175
        }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   176
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   177
        @Override
52946
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   178
        protected void resetDigest() {
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   179
            offset = 0;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   180
        }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   181
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   182
        // Returns the precomputed message digest value.
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   183
        @Override
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   184
        protected byte[] getDigestValue() throws SignatureException {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   185
            if (offset > RAW_ECDSA_MAX) {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   186
                throw new SignatureException("Message digest is too long");
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   187
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   188
            }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   189
            byte[] result = new byte[offset];
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   190
            System.arraycopy(precomputedDigest, 0, result, 0, offset);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   191
            offset = 0;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   192
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   193
            return result;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   194
        }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   195
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   196
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   197
    // Nested class for NONEwithECDSA signatures
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   198
    public static final class Raw extends RawECDSA {
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   199
        public Raw() {
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   200
            super(false);
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   201
        }
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   202
    }
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   203
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   204
    // Nested class for NONEwithECDSAinP1363Format signatures
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   205
    public static final class RawinP1363Format extends RawECDSA {
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   206
        public RawinP1363Format() {
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   207
            super(true);
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   208
        }
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   209
    }
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   210
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   211
    // Nested class for SHA1withECDSA signatures
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   212
    public static final class SHA1 extends ECDSASignature {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   213
        public SHA1() {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   214
            super("SHA1");
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   215
        }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   216
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   217
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   218
    // Nested class for SHA1withECDSAinP1363Format signatures
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   219
    public static final class SHA1inP1363Format extends ECDSASignature {
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   220
        public SHA1inP1363Format() {
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   221
            super("SHA1", true);
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   222
        }
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   223
    }
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   224
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 10336
diff changeset
   225
    // Nested class for SHA224withECDSA signatures
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 10336
diff changeset
   226
    public static final class SHA224 extends ECDSASignature {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 10336
diff changeset
   227
        public SHA224() {
52946
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   228
            super("SHA-224");
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 10336
diff changeset
   229
        }
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 10336
diff changeset
   230
    }
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 10336
diff changeset
   231
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   232
    // Nested class for SHA224withECDSAinP1363Format signatures
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   233
    public static final class SHA224inP1363Format extends ECDSASignature {
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   234
        public SHA224inP1363Format() {
52946
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   235
            super("SHA-224", true);
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   236
        }
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   237
    }
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   238
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   239
    // Nested class for SHA256withECDSA signatures
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   240
    public static final class SHA256 extends ECDSASignature {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   241
        public SHA256() {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   242
            super("SHA-256");
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   243
        }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   244
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   245
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   246
    // Nested class for SHA256withECDSAinP1363Format signatures
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   247
    public static final class SHA256inP1363Format extends ECDSASignature {
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   248
        public SHA256inP1363Format() {
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   249
            super("SHA-256", true);
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   250
        }
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   251
    }
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   252
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   253
    // Nested class for SHA384withECDSA signatures
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   254
    public static final class SHA384 extends ECDSASignature {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   255
        public SHA384() {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   256
            super("SHA-384");
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   257
        }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   258
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   259
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   260
    // Nested class for SHA384withECDSAinP1363Format signatures
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   261
    public static final class SHA384inP1363Format extends ECDSASignature {
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   262
        public SHA384inP1363Format() {
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   263
            super("SHA-384", true);
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   264
        }
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   265
    }
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   266
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   267
    // Nested class for SHA512withECDSA signatures
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   268
    public static final class SHA512 extends ECDSASignature {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   269
        public SHA512() {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   270
            super("SHA-512");
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   271
        }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   272
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   273
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   274
    // Nested class for SHA512withECDSAinP1363Format signatures
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   275
    public static final class SHA512inP1363Format extends ECDSASignature {
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   276
        public SHA512inP1363Format() {
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   277
            super("SHA-512", true);
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   278
        }
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   279
    }
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   280
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   281
    // initialize for verification. See JCA doc
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   282
    @Override
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   283
    protected void engineInitVerify(PublicKey publicKey)
52946
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   284
    throws InvalidKeyException {
55667
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   285
        ECPublicKey key = (ECPublicKey) ECKeyFactory.toECKey(publicKey);
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   286
        if (!isCompatible(this.sigParams, key.getParams())) {
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   287
            throw new InvalidKeyException("Key params does not match signature params");
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   288
        }
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   289
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   290
        // Should check that the supplied key is appropriate for signature
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   291
        // algorithm (e.g. P-256 for SHA256withECDSA)
55667
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   292
        this.publicKey = key;
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   293
        this.privateKey = null;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   294
        resetDigest();
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   295
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   296
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   297
    // initialize for signing. See JCA doc
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   298
    @Override
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   299
    protected void engineInitSign(PrivateKey privateKey)
52946
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   300
    throws InvalidKeyException {
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   301
        engineInitSign(privateKey, null);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   302
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   303
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   304
    // initialize for signing. See JCA doc
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   305
    @Override
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   306
    protected void engineInitSign(PrivateKey privateKey, SecureRandom random)
52946
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   307
    throws InvalidKeyException {
55667
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   308
        ECPrivateKey key = (ECPrivateKey) ECKeyFactory.toECKey(privateKey);
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   309
        if (!isCompatible(this.sigParams, key.getParams())) {
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   310
            throw new InvalidKeyException("Key params does not match signature params");
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   311
        }
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   312
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   313
        // Should check that the supplied key is appropriate for signature
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   314
        // algorithm (e.g. P-256 for SHA256withECDSA)
55667
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   315
        this.privateKey = key;
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   316
        this.publicKey = null;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   317
        this.random = random;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   318
        resetDigest();
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   319
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   320
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   321
    /**
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   322
     * Resets the message digest if needed.
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   323
     */
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   324
    protected void resetDigest() {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   325
        if (needsReset) {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   326
            if (messageDigest != null) {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   327
                messageDigest.reset();
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   328
            }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   329
            needsReset = false;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   330
        }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   331
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   332
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   333
    /**
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   334
     * Returns the message digest value.
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   335
     */
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   336
    protected byte[] getDigestValue() throws SignatureException {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   337
        needsReset = false;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   338
        return messageDigest.digest();
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   339
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   340
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   341
    // update the signature with the plaintext data. See JCA doc
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   342
    @Override
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   343
    protected void engineUpdate(byte b) throws SignatureException {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   344
        messageDigest.update(b);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   345
        needsReset = true;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   346
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   347
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   348
    // update the signature with the plaintext data. See JCA doc
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   349
    @Override
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   350
    protected void engineUpdate(byte[] b, int off, int len)
52946
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   351
    throws SignatureException {
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   352
        messageDigest.update(b, off, len);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   353
        needsReset = true;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   354
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   355
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   356
    // update the signature with the plaintext data. See JCA doc
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   357
    @Override
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   358
    protected void engineUpdate(ByteBuffer byteBuffer) {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   359
        int len = byteBuffer.remaining();
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   360
        if (len <= 0) {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   361
            return;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   362
        }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   363
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   364
        messageDigest.update(byteBuffer);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   365
        needsReset = true;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   366
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   367
55667
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   368
    private static boolean isCompatible(ECParameterSpec sigParams,
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   369
            ECParameterSpec keyParams) {
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   370
        if (sigParams == null) {
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   371
            // no restriction on key param
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   372
            return true;
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   373
        }
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   374
        return ECUtil.equals(sigParams, keyParams);
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   375
    }
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   376
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   377
52946
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   378
    private byte[] signDigestImpl(ECDSAOperations ops, int seedBits,
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   379
        byte[] digest, ECPrivateKeyImpl privImpl, SecureRandom random)
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   380
        throws SignatureException {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   381
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   382
        byte[] seedBytes = new byte[(seedBits + 7) / 8];
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   383
        byte[] s = privImpl.getArrayS();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   384
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   385
        // Attempt to create the signature in a loop that uses new random input
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   386
        // each time. The chance of failure is very small assuming the
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   387
        // implementation derives the nonce using extra bits
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   388
        int numAttempts = 128;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   389
        for (int i = 0; i < numAttempts; i++) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   390
            random.nextBytes(seedBytes);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   391
            ECDSAOperations.Seed seed = new ECDSAOperations.Seed(seedBytes);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   392
            try {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   393
                return ops.signDigest(s, digest, seed);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   394
            } catch (IntermediateValueException ex) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   395
                // try again in the next iteration
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   396
            }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   397
        }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   398
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   399
        throw new SignatureException("Unable to produce signature after "
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   400
            + numAttempts + " attempts");
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   401
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   402
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   403
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   404
    private Optional<byte[]> signDigestImpl(ECPrivateKey privateKey,
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   405
        byte[] digest, SecureRandom random) throws SignatureException {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   406
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   407
        if (! (privateKey instanceof ECPrivateKeyImpl)) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   408
            return Optional.empty();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   409
        }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   410
        ECPrivateKeyImpl privImpl = (ECPrivateKeyImpl) privateKey;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   411
        ECParameterSpec params = privateKey.getParams();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   412
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   413
        // seed is the key size + 64 bits
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   414
        int seedBits = params.getOrder().bitLength() + 64;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   415
        Optional<ECDSAOperations> opsOpt =
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   416
            ECDSAOperations.forParameters(params);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   417
        if (opsOpt.isEmpty()) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   418
            return Optional.empty();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   419
        } else {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   420
            byte[] sig = signDigestImpl(opsOpt.get(), seedBits, digest,
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   421
                privImpl, random);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   422
            return Optional.of(sig);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   423
        }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   424
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   425
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   426
    private byte[] signDigestNative(ECPrivateKey privateKey, byte[] digest,
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   427
        SecureRandom random) throws SignatureException {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   428
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   429
        byte[] s = privateKey.getS().toByteArray();
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   430
        ECParameterSpec params = privateKey.getParams();
52946
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   431
17491
7a33824ec8c5 7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents: 12685
diff changeset
   432
        // DER OID
7a33824ec8c5 7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents: 12685
diff changeset
   433
        byte[] encodedParams = ECUtil.encodeECParameterSpec(null, params);
54197
ddfb658c8ce3 8147502: Digest is incorrectly truncated for ECDSA signatures when the bit length of n is less than the field size
apetcher
parents: 53397
diff changeset
   434
        int orderLength = params.getOrder().bitLength();
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   435
54197
ddfb658c8ce3 8147502: Digest is incorrectly truncated for ECDSA signatures when the bit length of n is less than the field size
apetcher
parents: 53397
diff changeset
   436
        // seed is twice the order length (in bytes) plus 1
ddfb658c8ce3 8147502: Digest is incorrectly truncated for ECDSA signatures when the bit length of n is less than the field size
apetcher
parents: 53397
diff changeset
   437
        byte[] seed = new byte[(((orderLength + 7) >> 3) + 1) * 2];
52946
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   438
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   439
        random.nextBytes(seed);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   440
45993
076a6b39a5a9 8175110: Higher quality ECDSA operations
apetcher
parents: 43214
diff changeset
   441
        // random bits needed for timing countermeasures
076a6b39a5a9 8175110: Higher quality ECDSA operations
apetcher
parents: 43214
diff changeset
   442
        int timingArgument = random.nextInt();
076a6b39a5a9 8175110: Higher quality ECDSA operations
apetcher
parents: 43214
diff changeset
   443
        // values must be non-zero to enable countermeasures
076a6b39a5a9 8175110: Higher quality ECDSA operations
apetcher
parents: 43214
diff changeset
   444
        timingArgument |= 1;
076a6b39a5a9 8175110: Higher quality ECDSA operations
apetcher
parents: 43214
diff changeset
   445
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   446
        try {
52946
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   447
            return signDigest(digest, s, encodedParams, seed,
45993
076a6b39a5a9 8175110: Higher quality ECDSA operations
apetcher
parents: 43214
diff changeset
   448
                timingArgument);
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   449
        } catch (GeneralSecurityException e) {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   450
            throw new SignatureException("Could not sign data", e);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   451
        }
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   452
52946
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   453
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   454
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   455
    // sign the data and return the signature. See JCA doc
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   456
    @Override
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   457
    protected byte[] engineSign() throws SignatureException {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   458
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   459
        if (random == null) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   460
            random = JCAUtil.getSecureRandom();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   461
        }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   462
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   463
        byte[] digest = getDigestValue();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   464
        Optional<byte[]> sigOpt = signDigestImpl(privateKey, digest, random);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   465
        byte[] sig;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   466
        if (sigOpt.isPresent()) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   467
            sig = sigOpt.get();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   468
        } else {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   469
            sig = signDigestNative(privateKey, digest, random);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   470
        }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   471
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   472
        if (p1363Format) {
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   473
            return sig;
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   474
        } else {
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 52946
diff changeset
   475
            return ECUtil.encodeSignature(sig);
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   476
        }
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   477
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   478
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   479
    // verify the data and return the result. See JCA doc
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   480
    @Override
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   481
    protected boolean engineVerify(byte[] signature) throws SignatureException {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   482
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   483
        byte[] w;
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   484
        ECParameterSpec params = publicKey.getParams();
17491
7a33824ec8c5 7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents: 12685
diff changeset
   485
        // DER OID
7a33824ec8c5 7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents: 12685
diff changeset
   486
        byte[] encodedParams = ECUtil.encodeECParameterSpec(null, params);
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   487
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   488
        if (publicKey instanceof ECPublicKeyImpl) {
52946
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   489
            w = ((ECPublicKeyImpl) publicKey).getEncodedPublicValue();
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   490
        } else { // instanceof ECPublicKey
17491
7a33824ec8c5 7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents: 12685
diff changeset
   491
            w = ECUtil.encodePoint(publicKey.getW(), params.getCurve());
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   492
        }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   493
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   494
        byte[] sig;
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   495
        if (p1363Format) {
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   496
            sig = signature;
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   497
        } else {
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 52946
diff changeset
   498
            sig = ECUtil.decodeSignature(signature);
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   499
        }
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   500
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   501
        try {
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 25859
diff changeset
   502
            return verifySignedDigest(sig, getDigestValue(), w, encodedParams);
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   503
        } catch (GeneralSecurityException e) {
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   504
            throw new SignatureException("Could not verify signature", e);
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   505
        }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   506
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   507
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   508
    // set parameter, not supported. See JCA doc
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   509
    @Override
19209
6d3db5fe54e3 8022439: Fix lint warnings in sun.security.ec
juh
parents: 17491
diff changeset
   510
    @Deprecated
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   511
    protected void engineSetParameter(String param, Object value)
52946
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   512
    throws InvalidParameterException {
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   513
        throw new UnsupportedOperationException("setParameter() not supported");
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   514
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   515
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   516
    @Override
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   517
    protected void engineSetParameter(AlgorithmParameterSpec params)
52946
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   518
    throws InvalidAlgorithmParameterException {
55667
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   519
        if (params != null && !(params instanceof ECParameterSpec)) {
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   520
            throw new InvalidAlgorithmParameterException("No parameter accepted");
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   521
        }
55667
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   522
        ECKey key = (this.privateKey == null? this.publicKey : this.privateKey);
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   523
        if ((key != null) && !isCompatible((ECParameterSpec)params, key.getParams())) {
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   524
            throw new InvalidAlgorithmParameterException
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   525
                ("Signature params does not match key params");
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   526
        }
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   527
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   528
        sigParams = (ECParameterSpec) params;
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   529
    }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   530
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   531
    // get parameter, not supported. See JCA doc
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   532
    @Override
19209
6d3db5fe54e3 8022439: Fix lint warnings in sun.security.ec
juh
parents: 17491
diff changeset
   533
    @Deprecated
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   534
    protected Object engineGetParameter(String param)
52946
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   535
    throws InvalidParameterException {
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   536
        throw new UnsupportedOperationException("getParameter() not supported");
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   537
    }
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   538
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   539
    @Override
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   540
    protected AlgorithmParameters engineGetParameters() {
55667
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   541
        if (sigParams == null) {
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   542
            return null;
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   543
        }
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   544
        try {
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   545
            AlgorithmParameters ap = AlgorithmParameters.getInstance("EC");
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   546
            ap.init(sigParams);
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   547
            return ap;
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   548
        } catch (Exception e) {
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   549
            // should never happen
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   550
            throw new ProviderException("Error retrieving EC parameters", e);
6521aec1c26e 8225745: NoSuchAlgorithmException exception for SHA256withECDSA with RSASSA-PSS support
valeriep
parents: 54197
diff changeset
   551
        }
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   552
    }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   553
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   554
    /**
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   555
     * Signs the digest using the private key.
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   556
     *
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   557
     * @param digest the digest to be signed.
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   558
     * @param s the private key's S value.
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   559
     * @param encodedParams the curve's DER encoded object identifier.
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   560
     * @param seed the random seed.
45993
076a6b39a5a9 8175110: Higher quality ECDSA operations
apetcher
parents: 43214
diff changeset
   561
     * @param timing When non-zero, the implmentation will use timing
076a6b39a5a9 8175110: Higher quality ECDSA operations
apetcher
parents: 43214
diff changeset
   562
     *     countermeasures to hide secrets from timing channels. The EC
076a6b39a5a9 8175110: Higher quality ECDSA operations
apetcher
parents: 43214
diff changeset
   563
     *     implementation will disable the countermeasures when this value is
076a6b39a5a9 8175110: Higher quality ECDSA operations
apetcher
parents: 43214
diff changeset
   564
     *     zero, because the underlying EC functions are shared by several
076a6b39a5a9 8175110: Higher quality ECDSA operations
apetcher
parents: 43214
diff changeset
   565
     *     crypto operations, some of which do not use the countermeasures.
076a6b39a5a9 8175110: Higher quality ECDSA operations
apetcher
parents: 43214
diff changeset
   566
     *     The high-order 31 bits must be uniformly random. The entropy from
076a6b39a5a9 8175110: Higher quality ECDSA operations
apetcher
parents: 43214
diff changeset
   567
     *     these bits is used by the countermeasures.
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   568
     *
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   569
     * @return byte[] the signature.
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   570
     */
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   571
    private static native byte[] signDigest(byte[] digest, byte[] s,
52946
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   572
                                            byte[] encodedParams, byte[] seed, int timing)
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   573
        throws GeneralSecurityException;
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   574
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   575
    /**
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   576
     * Verifies the signed digest using the public key.
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   577
     *
52946
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   578
     * @param signature the signature to be verified. It is encoded
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   579
     *        as a concatenation of the key's R and S values.
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   580
     * @param digest the digest to be used.
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   581
     * @param w the public key's W point (in uncompressed form).
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   582
     * @param encodedParams the curve's DER encoded object identifier.
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   583
     *
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   584
     * @return boolean true if the signature is successfully verified.
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   585
     */
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   586
    private static native boolean verifySignedDigest(byte[] signature,
52946
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   587
                                                     byte[] digest, byte[] w, byte[] encodedParams)
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents: 50204
diff changeset
   588
        throws GeneralSecurityException;
3492
e549cea58864 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   589
}