Path: blob/master/src/jdk.crypto.ec/share/classes/sun/security/ec/XDHPublicKeyImpl.java
41161 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.ec;2627import java.math.BigInteger;28import java.security.InvalidKeyException;29import java.security.KeyRep;30import java.security.PublicKey;31import java.security.interfaces.XECPublicKey;32import java.security.spec.AlgorithmParameterSpec;33import java.security.spec.NamedParameterSpec;34import java.util.Arrays;3536import sun.security.util.BitArray;37import sun.security.x509.AlgorithmId;38import sun.security.x509.X509Key;3940public final class XDHPublicKeyImpl extends X509Key implements XECPublicKey {4142private static final long serialVersionUID = 1L;4344private final BigInteger u;45private final NamedParameterSpec paramSpec;4647XDHPublicKeyImpl(XECParameters params, BigInteger u)48throws InvalidKeyException {4950this.paramSpec = new NamedParameterSpec(params.getName());51this.algid = new AlgorithmId(params.getOid());52this.u = u.mod(params.getP());5354byte[] u_arr = this.u.toByteArray();55reverse(u_arr);56// u_arr may be too large or too small, depending on the value of u57u_arr = Arrays.copyOf(u_arr, params.getBytes());5859setKey(new BitArray(u_arr.length * 8, u_arr));6061checkLength(params);62}6364XDHPublicKeyImpl(byte[] encoded) throws InvalidKeyException {65decode(encoded);6667XECParameters params =68XECParameters.get(InvalidKeyException::new, algid);69this.paramSpec = new NamedParameterSpec(params.getName());70// construct the BigInteger representation71byte[] u_arr = getKey().toByteArray();72reverse(u_arr);7374// clear the extra bits75int bitsMod8 = params.getBits() % 8;76if (bitsMod8 != 0) {77int mask = (1 << bitsMod8) - 1;78u_arr[0] &= mask;79}8081this.u = new BigInteger(1, u_arr);8283checkLength(params);84}8586void checkLength(XECParameters params) throws InvalidKeyException {8788if (params.getBytes() * 8 != getKey().length()) {89throw new InvalidKeyException(90"key length must be " + params.getBytes());91}92}9394@Override95public BigInteger getU() {96return u;97}9899@Override100public AlgorithmParameterSpec getParams() {101return paramSpec;102}103104@Override105public String getAlgorithm() {106return "XDH";107}108109protected Object writeReplace() throws java.io.ObjectStreamException {110return new KeyRep(KeyRep.Type.PUBLIC,111getAlgorithm(),112getFormat(),113getEncoded());114}115116private static void swap(byte[] arr, int i, int j) {117byte tmp = arr[i];118arr[i] = arr[j];119arr[j] = tmp;120}121122private static void reverse(byte [] arr) {123int i = 0;124int j = arr.length - 1;125126while (i < j) {127swap(arr, i, j);128i++;129j--;130}131}132}133134135136