src/jdk.crypto.ec/share/classes/sun/security/ec/ECDSAOperations.java
author jcbeyler
Thu, 14 Mar 2019 09:35:45 -0700
changeset 54128 c0fccca69aff
parent 52946 752e57845ad2
permissions -rw-r--r--
8220628: Move the HeapMonitor library to C++ Summary: Migrate libHeapMonitorTest.c to libHeapMonitorTest.cpp Reviewed-by: cjplummer, sspitsyn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52946
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
     1
/*
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
     4
 *
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    10
 *
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    15
 * accompanied this code).
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    16
 *
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    20
 *
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    23
 * questions.
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    24
 */
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    25
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    26
package sun.security.ec;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    27
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    28
import sun.security.ec.point.*;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    29
import sun.security.util.ArrayUtil;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    30
import sun.security.util.math.*;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    31
import static sun.security.ec.ECOperations.IntermediateValueException;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    32
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    33
import java.security.ProviderException;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    34
import java.security.spec.*;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    35
import java.util.Optional;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    36
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    37
public class ECDSAOperations {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    38
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    39
    public static class Seed {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    40
        private final byte[] seedValue;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    41
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    42
        public Seed(byte[] seedValue) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    43
            this.seedValue = seedValue;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    44
        }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    45
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    46
        public byte[] getSeedValue() {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    47
            return seedValue;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    48
        }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    49
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    50
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    51
    public static class Nonce {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    52
        private final byte[] nonceValue;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    53
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    54
        public Nonce(byte[] nonceValue) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    55
            this.nonceValue = nonceValue;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    56
        }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    57
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    58
        public byte[] getNonceValue() {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    59
            return nonceValue;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    60
        }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    61
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    62
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    63
    private final ECOperations ecOps;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    64
    private final AffinePoint basePoint;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    65
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    66
    public ECDSAOperations(ECOperations ecOps, ECPoint basePoint) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    67
        this.ecOps = ecOps;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    68
        this.basePoint = toAffinePoint(basePoint, ecOps.getField());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    69
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    70
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    71
    public ECOperations getEcOperations() {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    72
        return ecOps;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    73
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    74
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    75
    public AffinePoint basePointMultiply(byte[] scalar) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    76
        return ecOps.multiply(basePoint, scalar).asAffine();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    77
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    78
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    79
    public static AffinePoint toAffinePoint(ECPoint point,
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    80
        IntegerFieldModuloP field) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    81
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    82
        ImmutableIntegerModuloP affineX = field.getElement(point.getAffineX());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    83
        ImmutableIntegerModuloP affineY = field.getElement(point.getAffineY());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    84
        return new AffinePoint(affineX, affineY);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    85
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    86
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    87
    public static
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    88
    Optional<ECDSAOperations> forParameters(ECParameterSpec ecParams) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    89
        Optional<ECOperations> curveOps =
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    90
            ECOperations.forParameters(ecParams);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    91
        return curveOps.map(
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    92
            ops -> new ECDSAOperations(ops, ecParams.getGenerator())
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    93
        );
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    94
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    95
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    96
    /**
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    97
     *
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    98
     * Sign a digest using the provided private key and seed.
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    99
     * IMPORTANT: The private key is a scalar represented using a
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   100
     * little-endian byte array. This is backwards from the conventional
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   101
     * representation in ECDSA. The routines that produce and consume this
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   102
     * value uses little-endian, so this deviation from convention removes
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   103
     * the requirement to swap the byte order. The returned signature is in
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   104
     * the conventional byte order.
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   105
     *
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   106
     * @param privateKey the private key scalar as a little-endian byte array
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   107
     * @param digest the digest to be signed
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   108
     * @param seed the seed that will be used to produce the nonce. This object
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   109
     *             should contain an array that is at least 64 bits longer than
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   110
     *             the number of bits required to represent the group order.
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   111
     * @return the ECDSA signature value
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   112
     * @throws IntermediateValueException if the signature cannot be produced
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   113
     *      due to an unacceptable intermediate or final value. If this
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   114
     *      exception is thrown, then the caller should discard the nonnce and
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   115
     *      try again with an entirely new nonce value.
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   116
     */
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   117
    public byte[] signDigest(byte[] privateKey, byte[] digest, Seed seed)
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   118
        throws IntermediateValueException {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   119
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   120
        byte[] nonceArr = ecOps.seedToScalar(seed.getSeedValue());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   121
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   122
        Nonce nonce = new Nonce(nonceArr);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   123
        return signDigest(privateKey, digest, nonce);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   124
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   125
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   126
    /**
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   127
     *
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   128
     * Sign a digest using the provided private key and nonce.
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   129
     * IMPORTANT: The private key and nonce are scalars represented by a
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   130
     * little-endian byte array. This is backwards from the conventional
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   131
     * representation in ECDSA. The routines that produce and consume these
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   132
     * values use little-endian, so this deviation from convention removes
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   133
     * the requirement to swap the byte order. The returned signature is in
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   134
     * the conventional byte order.
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   135
     *
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   136
     * @param privateKey the private key scalar as a little-endian byte array
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   137
     * @param digest the digest to be signed
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   138
     * @param nonce the nonce object containing a little-endian scalar value.
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   139
     * @return the ECDSA signature value
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   140
     * @throws IntermediateValueException if the signature cannot be produced
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   141
     *      due to an unacceptable intermediate or final value. If this
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   142
     *      exception is thrown, then the caller should discard the nonnce and
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   143
     *      try again with an entirely new nonce value.
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   144
     */
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   145
    public byte[] signDigest(byte[] privateKey, byte[] digest, Nonce nonce)
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   146
        throws IntermediateValueException {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   147
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   148
        IntegerFieldModuloP orderField = ecOps.getOrderField();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   149
        int orderBits = orderField.getSize().bitLength();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   150
        if (orderBits % 8 != 0 && orderBits < digest.length * 8) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   151
            // This implementation does not support truncating digests to
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   152
            // a length that is not a multiple of 8.
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   153
            throw new ProviderException("Invalid digest length");
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   154
        }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   155
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   156
        byte[] k = nonce.getNonceValue();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   157
        // check nonce length
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   158
        int length = (orderField.getSize().bitLength() + 7) / 8;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   159
        if (k.length != length) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   160
            throw new ProviderException("Incorrect nonce length");
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   161
        }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   162
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   163
        MutablePoint R = ecOps.multiply(basePoint, k);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   164
        IntegerModuloP r = R.asAffine().getX();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   165
        // put r into the correct field by fully reducing to an array
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   166
        byte[] temp = new byte[length];
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   167
        r.asByteArray(temp);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   168
        r = orderField.getElement(temp);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   169
        // store r in result
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   170
        r.asByteArray(temp);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   171
        byte[] result = new byte[2 * length];
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   172
        ArrayUtil.reverse(temp);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   173
        System.arraycopy(temp, 0, result, 0, length);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   174
        // compare r to 0
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   175
        if (ECOperations.allZero(temp)) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   176
            throw new IntermediateValueException();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   177
        }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   178
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   179
        IntegerModuloP dU = orderField.getElement(privateKey);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   180
        int lengthE = Math.min(length, digest.length);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   181
        byte[] E = new byte[lengthE];
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   182
        System.arraycopy(digest, 0, E, 0, lengthE);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   183
        ArrayUtil.reverse(E);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   184
        IntegerModuloP e = orderField.getElement(E);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   185
        IntegerModuloP kElem = orderField.getElement(k);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   186
        IntegerModuloP kInv = kElem.multiplicativeInverse();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   187
        MutableIntegerModuloP s = r.mutable();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   188
        s.setProduct(dU).setSum(e).setProduct(kInv);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   189
        // store s in result
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   190
        s.asByteArray(temp);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   191
        ArrayUtil.reverse(temp);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   192
        System.arraycopy(temp, 0, result, length, length);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   193
        // compare s to 0
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   194
        if (ECOperations.allZero(temp)) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   195
            throw new IntermediateValueException();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   196
        }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   197
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   198
        return result;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   199
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   200
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   201
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   202
}