Path: blob/master/src/jdk.crypto.ec/share/classes/sun/security/ec/ed/EdDSAPublicKeyImpl.java
41162 views
/*1* Copyright (c) 2020, 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.ed;2627import java.math.BigInteger;28import java.security.InvalidKeyException;29import java.security.KeyRep;30import java.security.interfaces.EdECPublicKey;31import java.security.spec.EdECPoint;32import java.security.spec.NamedParameterSpec;33import java.util.Arrays;3435import sun.security.util.BitArray;36import sun.security.x509.AlgorithmId;37import sun.security.x509.X509Key;3839public final class EdDSAPublicKeyImpl extends X509Key implements EdECPublicKey {4041private static final long serialVersionUID = 1L;4243private final EdECPoint point;44private final NamedParameterSpec paramSpec;4546public EdDSAPublicKeyImpl(EdDSAParameters params, EdECPoint point)47throws InvalidKeyException {48this.paramSpec = new NamedParameterSpec(params.getName());49this.algid = new AlgorithmId(params.getOid());50this.point = point;5152byte[] encodedPoint = point.getY().toByteArray();53reverse(encodedPoint);54// array may be too large or too small, depending on the value55encodedPoint = Arrays.copyOf(encodedPoint, params.getKeyLength());56// set the high-order bit of the encoded point57byte msb = (byte) (point.isXOdd() ? 0x80 : 0);58encodedPoint[encodedPoint.length - 1] |= msb;59setKey(new BitArray(encodedPoint.length * 8, encodedPoint));6061checkLength(params);62}6364public EdDSAPublicKeyImpl(byte[] encoded) throws InvalidKeyException {65decode(encoded);6667EdDSAParameters params =68EdDSAParameters.get(InvalidKeyException::new, algid);69this.paramSpec = new NamedParameterSpec(params.getName());70// construct the EdECPoint representation71byte[] encodedPoint = getKey().toByteArray();72byte msb = encodedPoint[encodedPoint.length - 1];73encodedPoint[encodedPoint.length - 1] &= (byte) 0x7F;74boolean xOdd = (msb & 0x80) != 0;75reverse(encodedPoint);76BigInteger y = new BigInteger(1, encodedPoint);77this.point = new EdECPoint(xOdd, y);7879checkLength(params);80}8182void checkLength(EdDSAParameters params) throws InvalidKeyException {83if (params.getKeyLength() * 8 != getKey().length()) {84throw new InvalidKeyException(85"key length must be " + params.getKeyLength());86}87}8889public byte[] getEncodedPoint() {90return getKey().toByteArray();91}9293@Override94public EdECPoint getPoint() {95return point;96}9798@Override99public NamedParameterSpec getParams() {100return paramSpec;101}102103@Override104public String getAlgorithm() {105return "EdDSA";106}107108protected Object writeReplace() throws java.io.ObjectStreamException {109return new KeyRep(KeyRep.Type.PUBLIC, getAlgorithm(), getFormat(),110getEncoded());111}112113private static void swap(byte[] arr, int i, int j) {114byte tmp = arr[i];115arr[i] = arr[j];116arr[j] = tmp;117}118119private static void reverse(byte [] arr) {120int i = 0;121int j = arr.length - 1;122123while (i < j) {124swap(arr, i, j);125i++;126j--;127}128}129}130131132