src/jdk.crypto.ec/share/classes/sun/security/ec/XECOperations.java
author apetcher
Tue, 22 May 2018 13:44:02 -0400
branchJDK-8171279-XDH-TLS-branch
changeset 56589 bafd8be2f970
parent 50053 9bc1e6487cbb
permissions -rw-r--r--
Initial working XDH support in TLS. I should try to refactor the code a bit.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50053
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     1
/*
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     4
 *
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    10
 *
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    15
 * accompanied this code).
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    16
 *
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    20
 *
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    23
 * questions.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    24
 */
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    25
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    26
package sun.security.ec;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    27
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    28
import sun.security.util.math.IntegerFieldModuloP;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    29
import sun.security.util.math.ImmutableIntegerModuloP;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    30
import sun.security.util.math.IntegerModuloP;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    31
import sun.security.util.math.MutableIntegerModuloP;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    32
import sun.security.util.math.SmallValue;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    33
import sun.security.util.math.intpoly.IntegerPolynomial25519;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    34
import sun.security.util.math.intpoly.IntegerPolynomial448;
56589
bafd8be2f970 Initial working XDH support in TLS. I should try to refactor the code a bit.
apetcher
parents: 50053
diff changeset
    35
import sun.security.util.XECParameters;
50053
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    36
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    37
import java.math.BigInteger;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    38
import java.security.ProviderException;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    39
import java.security.SecureRandom;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    40
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    41
public class XECOperations {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    42
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    43
    private final XECParameters params;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    44
    private final IntegerFieldModuloP field;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    45
    private final ImmutableIntegerModuloP zero;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    46
    private final ImmutableIntegerModuloP one;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    47
    private final SmallValue a24;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    48
    private final ImmutableIntegerModuloP basePoint;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    49
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    50
    public XECOperations(XECParameters c) {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    51
        this.params = c;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    52
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    53
        BigInteger p = params.getP();
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    54
        this.field = getIntegerFieldModulo(p);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    55
        this.zero = field.getElement(BigInteger.ZERO).fixed();
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    56
        this.one = field.get1().fixed();
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    57
        this.a24 = field.getSmallValue(params.getA24());
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    58
        this.basePoint = field.getElement(
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    59
            BigInteger.valueOf(c.getBasePoint()));
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    60
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    61
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    62
    public XECParameters getParameters() {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    63
        return params;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    64
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    65
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    66
    public byte[] generatePrivate(SecureRandom random) {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    67
        byte[] result = new byte[this.params.getBytes()];
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    68
        random.nextBytes(result);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    69
        return result;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    70
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    71
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    72
    /**
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    73
     * Compute a public key from an encoded private key. This method will
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    74
     * modify the supplied array in order to prune it.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    75
     */
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    76
    public BigInteger computePublic(byte[] k) {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    77
        pruneK(k);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    78
        return pointMultiply(k, this.basePoint).asBigInteger();
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    79
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    80
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    81
    /**
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    82
     *
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    83
     * Multiply an encoded scalar with a point as a BigInteger and return an
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    84
     * encoded point. The array k holding the scalar will be pruned by
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    85
     * modifying it in place.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    86
     *
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    87
     * @param k an encoded scalar
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    88
     * @param u the u-coordinate of a point as a BigInteger
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    89
     * @return the encoded product
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    90
     */
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    91
    public byte[] encodedPointMultiply(byte[] k, BigInteger u) {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    92
        pruneK(k);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    93
        ImmutableIntegerModuloP elemU = field.getElement(u);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    94
        return pointMultiply(k, elemU).asByteArray(params.getBytes());
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    95
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    96
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    97
    /**
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    98
     *
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    99
     * Multiply an encoded scalar with an encoded point and return an encoded
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   100
     * point. The array k holding the scalar will be pruned by
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   101
     * modifying it in place.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   102
     *
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   103
     * @param k an encoded scalar
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   104
     * @param u an encoded point
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   105
     * @return the encoded product
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   106
     */
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   107
    public byte[] encodedPointMultiply(byte[] k, byte[] u) {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   108
        pruneK(k);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   109
        ImmutableIntegerModuloP elemU = decodeU(u);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   110
        return pointMultiply(k, elemU).asByteArray(params.getBytes());
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   111
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   112
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   113
    /**
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   114
     * Return the field element corresponding to an encoded u-coordinate.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   115
     * This method prunes u by modifying it in place.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   116
     *
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   117
     * @param u
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   118
     * @param bits
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   119
     * @return
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   120
     */
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   121
    private ImmutableIntegerModuloP decodeU(byte[] u, int bits) {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   122
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   123
        maskHighOrder(u, bits);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   124
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   125
        return field.getElement(u);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   126
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   127
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   128
    /**
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   129
     * Mask off the high order bits of an encoded integer in an array. The
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   130
     * array is modified in place.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   131
     *
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   132
     * @param arr an array containing an encoded integer
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   133
     * @param bits the number of bits to keep
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   134
     * @return the number, in range [1,8], of bits kept in the highest byte
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   135
     */
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   136
    private static byte maskHighOrder(byte[] arr, int bits) {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   137
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   138
        int lastByteIndex = arr.length - 1;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   139
        byte bitsMod8 = (byte) (bits % 8);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   140
        byte highBits = bitsMod8 == 0 ? 8 : bitsMod8;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   141
        byte msbMaskOff = (byte) ((1 << highBits) - 1);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   142
        arr[lastByteIndex] &= msbMaskOff;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   143
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   144
        return highBits;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   145
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   146
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   147
    /**
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   148
     * Prune an encoded scalar value by modifying it in place. The extra
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   149
     * high-order bits are masked off, the highest valid bit it set, and the
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   150
     * number is rounded down to a multiple of the cofactor.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   151
     *
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   152
     * @param k an encoded scalar value
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   153
     * @param bits the number of bits in the scalar
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   154
     * @param logCofactor the base-2 logarithm of the cofactor
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   155
     */
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   156
    private static void pruneK(byte[] k, int bits, int logCofactor) {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   157
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   158
        int lastByteIndex = k.length - 1;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   159
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   160
        // mask off unused high-order bits
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   161
        byte highBits = maskHighOrder(k, bits);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   162
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   163
        // set the highest bit
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   164
        byte msbMaskOn = (byte) (1 << (highBits - 1));
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   165
        k[lastByteIndex] |= msbMaskOn;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   166
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   167
        // round down to a multiple of the cofactor
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   168
        byte lsbMaskOff = (byte) (0xFF << logCofactor);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   169
        k[0] &= lsbMaskOff;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   170
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   171
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   172
    private void pruneK(byte[] k) {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   173
        pruneK(k, params.getBits(), params.getLogCofactor());
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   174
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   175
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   176
    private ImmutableIntegerModuloP decodeU(byte [] u) {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   177
        return decodeU(u, params.getBits());
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   178
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   179
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   180
    // Constant-time conditional swap
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   181
    private static void cswap(int swap, MutableIntegerModuloP x1,
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   182
        MutableIntegerModuloP x2) {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   183
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   184
        x1.conditionalSwapWith(x2, swap);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   185
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   186
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   187
    private static IntegerFieldModuloP getIntegerFieldModulo(BigInteger p) {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   188
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   189
        if (p.equals(IntegerPolynomial25519.MODULUS)) {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   190
            return new IntegerPolynomial25519();
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   191
        }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   192
        else if (p.equals(IntegerPolynomial448.MODULUS)) {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   193
            return new IntegerPolynomial448();
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   194
        }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   195
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   196
        throw new ProviderException("Unsupported prime: " + p.toString());
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   197
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   198
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   199
    private int bitAt(byte[] arr, int index) {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   200
        int byteIndex = index / 8;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   201
        int bitIndex = index % 8;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   202
        return (arr[byteIndex] & (1 << bitIndex)) >> bitIndex;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   203
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   204
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   205
    /*
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   206
     * Constant-time Montgomery ladder that computes k*u and returns the
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   207
     * result as a field element.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   208
     */
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   209
    private IntegerModuloP pointMultiply(byte[] k,
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   210
                                         ImmutableIntegerModuloP u) {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   211
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   212
        ImmutableIntegerModuloP x_1 = u;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   213
        MutableIntegerModuloP x_2 = this.one.mutable();
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   214
        MutableIntegerModuloP z_2 = this.zero.mutable();
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   215
        MutableIntegerModuloP x_3 = u.mutable();
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   216
        MutableIntegerModuloP z_3 = this.one.mutable();
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   217
        int swap = 0;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   218
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   219
        // Variables below are reused to avoid unnecessary allocation
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   220
        // They will be assigned in the loop, so initial value doesn't matter
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   221
        MutableIntegerModuloP m1 = this.zero.mutable();
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   222
        MutableIntegerModuloP DA = this.zero.mutable();
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   223
        MutableIntegerModuloP E = this.zero.mutable();
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   224
        MutableIntegerModuloP a24_times_E = this.zero.mutable();
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   225
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   226
        // Comments describe the equivalent operations from RFC 7748
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   227
        // In comments, A(m1) means the variable m1 holds the value A
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   228
        for (int t = params.getBits() - 1; t >= 0; t--) {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   229
            int k_t = bitAt(k, t);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   230
            swap = swap ^ k_t;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   231
            cswap(swap, x_2, x_3);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   232
            cswap(swap, z_2, z_3);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   233
            swap = k_t;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   234
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   235
            // A(m1) = x_2 + z_2
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   236
            m1.setValue(x_2).setSum(z_2);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   237
            // D = x_3 - z_3
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   238
            // DA = D * A(m1)
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   239
            DA.setValue(x_3).setDifference(z_3).setProduct(m1);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   240
            // AA(m1) = A(m1)^2
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   241
            m1.setSquare();
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   242
            // B(x_2) = x_2 - z_2
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   243
            x_2.setDifference(z_2);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   244
            // C = x_3 + z_3
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   245
            // CB(x_3) = C * B(x_2)
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   246
            x_3.setSum(z_3).setProduct(x_2);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   247
            // BB(x_2) = B^2
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   248
            x_2.setSquare();
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   249
            // E = AA(m1) - BB(x_2)
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   250
            E.setValue(m1).setDifference(x_2);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   251
            // compute a24 * E using SmallValue
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   252
            a24_times_E.setValue(E);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   253
            a24_times_E.setProduct(this.a24);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   254
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   255
            // assign results to x_3, z_3, x_2, z_2
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   256
            // x_2 = AA(m1) * BB
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   257
            x_2.setProduct(m1);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   258
            // z_2 = E * (AA(m1) + a24 * E)
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   259
            z_2.setValue(m1).setSum(a24_times_E).setProduct(E);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   260
            // z_3 = x_1*(DA - CB(x_3))^2
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   261
            z_3.setValue(DA).setDifference(x_3).setSquare().setProduct(x_1);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   262
            // x_3 = (CB(x_3) + DA)^2
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   263
            x_3.setSum(DA).setSquare();
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   264
        }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   265
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   266
        cswap(swap, x_2, x_3);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   267
        cswap(swap, z_2, z_3);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   268
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   269
        // return (x_2 * z_2^(p - 2))
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   270
        return x_2.setProduct(z_2.multiplicativeInverse());
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   271
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   272
}