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 26 package java.security.spec; 27 28 import java.security.InvalidParameterException; 29 import java.util.Objects; 30 import java.util.Optional; 31 32 /** 33 * A class used to specify EdDSA signature and verification parameters. All 34 * algorithm modes in <a href="https://tools.ietf.org/html/rfc8032">RFC 8032: 35 * Edwards-Curve Digital Signature Algorithm (EdDSA)</a> can be specified using 36 * combinations of the settings in this class. 37 * 38 * <ul> 39 * <li>If prehash is true, then the mode is Ed25519ph or Ed448ph</li> 40 * <li>Otherwise, if a context is present, the mode is Ed25519ctx or Ed448</li> 41 * <li>Otherwise, the mode is Ed25519 or Ed448</li> 42 * </ul> 43 * 44 * @since 15 45 */ 46 47 public class EdDSAParameterSpec implements AlgorithmParameterSpec { 48 49 private final boolean prehash; 50 private final byte[] context; 51 52 /** 53 * Construct an {@code EdDSAParameterSpec} by specifying whether the prehash mode 54 * is used. No context is provided so this constructor specifies a mode 55 * in which the context is null. Note that this mode may be different 56 * than the mode in which an empty array is used as the context. 57 * 58 * @param prehash whether the prehash mode is specified. 59 */ EdDSAParameterSpec(boolean prehash)60 public EdDSAParameterSpec(boolean prehash) { 61 this.prehash = prehash; 62 this.context = null; 63 } 64 65 /** 66 * Construct an {@code EdDSAParameterSpec} by specifying a context and whether the 67 * prehash mode is used. The context may not be null, but it may be an 68 * empty array. The mode used when the context is an empty array may not be 69 * the same as the mode used when the context is absent. 70 * 71 * @param prehash whether the prehash mode is specified. 72 * @param context the context is copied and bound to the signature. 73 * @throws NullPointerException if context is null. 74 * @throws InvalidParameterException if context length is greater than 255. 75 */ EdDSAParameterSpec(boolean prehash, byte[] context)76 public EdDSAParameterSpec(boolean prehash, byte[] context) { 77 78 Objects.requireNonNull(context, "context may not be null"); 79 if (context.length > 255) { 80 throw new InvalidParameterException("context length cannot be " + 81 "greater than 255"); 82 } 83 84 this.prehash = prehash; 85 this.context = context.clone(); 86 } 87 88 /** 89 * Get whether the prehash mode is specified. 90 * 91 * @return whether the prehash mode is specified. 92 */ isPrehash()93 public boolean isPrehash() { 94 return prehash; 95 } 96 97 /** 98 * Get the context that the signature will use. 99 * 100 * @return {@code Optional} contains a copy of the context or empty 101 * if context is null. 102 */ getContext()103 public Optional<byte[]> getContext() { 104 if (context == null) { 105 return Optional.empty(); 106 } else { 107 return Optional.of(context.clone()); 108 } 109 } 110 } 111