1 /* 2 * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package java.security.spec; 26 27 import java.util.Objects; 28 29 /** 30 * A class representing elliptic curve private keys as defined in 31 * <a href="https://tools.ietf.org/html/rfc8032">RFC 8032: Edwards-Curve 32 * Digital Signature Algorithm (EdDSA)</a>, including the curve and other 33 * algorithm parameters. The private key is a bit string represented using 34 * a byte array. This class only supports bit string lengths that are a 35 * multiple of 8. 36 * 37 * @since 15 38 */ 39 public final class EdECPrivateKeySpec implements KeySpec { 40 41 private final NamedParameterSpec params; 42 private final byte[] bytes; 43 44 /** 45 * Construct a private key spec using the supplied parameters and 46 * bit string. 47 * 48 * @param params the algorithm parameters. 49 * @param bytes the key as a byte array. This array is copied 50 * to protect against subsequent modification. 51 * 52 * @throws NullPointerException if {@code params} or {@code bytes} 53 * is null. 54 */ EdECPrivateKeySpec(NamedParameterSpec params, byte[] bytes)55 public EdECPrivateKeySpec(NamedParameterSpec params, byte[] bytes) { 56 Objects.requireNonNull(params, "params must not be null"); 57 Objects.requireNonNull(bytes, "bytes must not be null"); 58 59 this.params = params; 60 this.bytes = bytes.clone(); 61 } 62 63 /** 64 * Get the algorithm parameters that define the curve and other settings. 65 * 66 * @return the algorithm parameters. 67 */ getParams()68 public NamedParameterSpec getParams() { 69 return params; 70 } 71 72 /** 73 * Get the byte array representing the private key. A new copy of the array 74 * is returned each time this method is called. 75 * 76 * @return the private key as a byte array. 77 */ getBytes()78 public byte[] getBytes() { 79 return bytes.clone(); 80 } 81 } 82