Path: blob/master/src/jdk.crypto.ec/share/classes/sun/security/ec/ed/EdDSAPrivateKeyImpl.java
41162 views
/*1* Copyright (c) 2020, 2021, 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.io.IOException;28import java.security.InvalidKeyException;29import java.security.ProviderException;30import java.security.interfaces.EdECPrivateKey;31import java.util.Optional;32import java.security.spec.NamedParameterSpec;3334import sun.security.pkcs.PKCS8Key;35import sun.security.x509.AlgorithmId;36import sun.security.util.*;3738public final class EdDSAPrivateKeyImpl39extends PKCS8Key implements EdECPrivateKey {4041private static final long serialVersionUID = 1L;4243private final NamedParameterSpec paramSpec;44private byte[] h;4546EdDSAPrivateKeyImpl(EdDSAParameters params, byte[] h)47throws InvalidKeyException {4849this.paramSpec = new NamedParameterSpec(params.getName());50this.algid = new AlgorithmId(params.getOid());51this.h = h.clone();5253DerValue val = new DerValue(DerValue.tag_OctetString, h);54try {55this.key = val.toByteArray();56} catch (IOException ex) {57throw new AssertionError("Should not happen", ex);58} finally {59val.clear();60}61checkLength(params);62}6364EdDSAPrivateKeyImpl(byte[] encoded) throws InvalidKeyException {6566super(encoded);67EdDSAParameters params = EdDSAParameters.get(68InvalidKeyException::new, algid);69paramSpec = new NamedParameterSpec(params.getName());7071try {72DerInputStream derStream = new DerInputStream(key);73h = derStream.getOctetString();74} catch (IOException ex) {75throw new InvalidKeyException(ex);76}77checkLength(params);78}7980void checkLength(EdDSAParameters params) throws InvalidKeyException {8182if (params.getKeyLength() != this.h.length) {83throw new InvalidKeyException("key length is " + this.h.length +84", key length must be " + params.getKeyLength());85}86}8788public byte[] getKey() {89return h.clone();90}9192@Override93public String getAlgorithm() {94return "EdDSA";95}9697@Override98public NamedParameterSpec getParams() {99return paramSpec;100}101102@Override103public Optional<byte[]> getBytes() {104return Optional.of(getKey());105}106}107108109