1 /* 2 * Copyright (c) 2001, 2013, 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.math.BigInteger; 29 30 /** 31 * This class specifies an RSA multi-prime private key, as defined in the 32 * PKCS#1 v2.1, using the Chinese Remainder Theorem (CRT) information 33 * values for efficiency. 34 * 35 * @author Valerie Peng 36 * 37 * 38 * @see java.security.Key 39 * @see java.security.KeyFactory 40 * @see KeySpec 41 * @see PKCS8EncodedKeySpec 42 * @see RSAPrivateKeySpec 43 * @see RSAPublicKeySpec 44 * @see RSAOtherPrimeInfo 45 * 46 * @since 1.4 47 */ 48 49 public class RSAMultiPrimePrivateCrtKeySpec extends RSAPrivateKeySpec { 50 51 private final BigInteger publicExponent; 52 private final BigInteger primeP; 53 private final BigInteger primeQ; 54 private final BigInteger primeExponentP; 55 private final BigInteger primeExponentQ; 56 private final BigInteger crtCoefficient; 57 private final RSAOtherPrimeInfo[] otherPrimeInfo; 58 59 /** 60 * Creates a new {@code RSAMultiPrimePrivateCrtKeySpec} 61 * given the modulus, publicExponent, privateExponent, 62 * primeP, primeQ, primeExponentP, primeExponentQ, 63 * crtCoefficient, and otherPrimeInfo as defined in PKCS#1 v2.1. 64 * 65 * <p>Note that the contents of {@code otherPrimeInfo} 66 * are copied to protect against subsequent modification when 67 * constructing this object. 68 * 69 * @param modulus the modulus n. 70 * @param publicExponent the public exponent e. 71 * @param privateExponent the private exponent d. 72 * @param primeP the prime factor p of n. 73 * @param primeQ the prime factor q of n. 74 * @param primeExponentP this is d mod (p-1). 75 * @param primeExponentQ this is d mod (q-1). 76 * @param crtCoefficient the Chinese Remainder Theorem 77 * coefficient q-1 mod p. 78 * @param otherPrimeInfo triplets of the rest of primes, null can be 79 * specified if there are only two prime factors (p and q). 80 * @exception NullPointerException if any of the parameters, i.e. 81 * {@code modulus}, 82 * {@code publicExponent}, {@code privateExponent}, 83 * {@code primeP}, {@code primeQ}, 84 * {@code primeExponentP}, {@code primeExponentQ}, 85 * {@code crtCoefficient}, is null. 86 * @exception IllegalArgumentException if an empty, i.e. 0-length, 87 * {@code otherPrimeInfo} is specified. 88 */ 89 public RSAMultiPrimePrivateCrtKeySpec(BigInteger modulus, 90 BigInteger publicExponent, 91 BigInteger privateExponent, 92 BigInteger primeP, 93 BigInteger primeQ, 94 BigInteger primeExponentP, 95 BigInteger primeExponentQ, 96 BigInteger crtCoefficient, 97 RSAOtherPrimeInfo[] otherPrimeInfo) { 98 super(modulus, privateExponent); 99 if (modulus == null) { 100 throw new NullPointerException("the modulus parameter must be " + 101 "non-null"); 102 } 103 if (publicExponent == null) { 104 throw new NullPointerException("the publicExponent parameter " + 105 "must be non-null"); 106 } 107 if (privateExponent == null) { 108 throw new NullPointerException("the privateExponent parameter " + 109 "must be non-null"); 110 } 111 if (primeP == null) { 112 throw new NullPointerException("the primeP parameter " + 113 "must be non-null"); 114 } 115 if (primeQ == null) { 116 throw new NullPointerException("the primeQ parameter " + 117 "must be non-null"); 118 } 119 if (primeExponentP == null) { 120 throw new NullPointerException("the primeExponentP parameter " + 121 "must be non-null"); 122 } 123 if (primeExponentQ == null) { 124 throw new NullPointerException("the primeExponentQ parameter " + 125 "must be non-null"); 126 } 127 if (crtCoefficient == null) { 128 throw new NullPointerException("the crtCoefficient parameter " + 129 "must be non-null"); 130 } 131 this.publicExponent = publicExponent; 132 this.primeP = primeP; 133 this.primeQ = primeQ; 134 this.primeExponentP = primeExponentP; 135 this.primeExponentQ = primeExponentQ; 136 this.crtCoefficient = crtCoefficient; 137 if (otherPrimeInfo == null) { 138 this.otherPrimeInfo = null; 139 } else if (otherPrimeInfo.length == 0) { 140 throw new IllegalArgumentException("the otherPrimeInfo " + 141 "parameter must not be empty"); 142 } else { 143 this.otherPrimeInfo = otherPrimeInfo.clone(); 144 } 145 } 146 147 /** 148 * Returns the public exponent. 149 * 150 * @return the public exponent. 185 * 186 * @return the primeExponentQ. 187 */ 188 public BigInteger getPrimeExponentQ() { 189 return this.primeExponentQ; 190 } 191 192 /** 193 * Returns the crtCoefficient. 194 * 195 * @return the crtCoefficient. 196 */ 197 public BigInteger getCrtCoefficient() { 198 return this.crtCoefficient; 199 } 200 201 /** 202 * Returns a copy of the otherPrimeInfo or null if there are 203 * only two prime factors (p and q). 204 * 205 * @return the otherPrimeInfo. Returns a new array each 206 * time this method is called. 207 */ 208 public RSAOtherPrimeInfo[] getOtherPrimeInfo() { 209 if (otherPrimeInfo == null) return null; 210 return otherPrimeInfo.clone(); 211 } 212 } | 1 /* 2 * Copyright (c) 2001, 2018, 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.math.BigInteger; 29 import java.util.Objects; 30 31 /** 32 * This class specifies an RSA multi-prime private key, as defined in the 33 * <a href="https://tools.ietf.org/rfc/rfc8017.txt">PKCS#1 v2.2</a> standard 34 * using the Chinese Remainder Theorem (CRT) information values 35 * for efficiency. 36 * 37 * @author Valerie Peng 38 * 39 * 40 * @see java.security.Key 41 * @see java.security.KeyFactory 42 * @see KeySpec 43 * @see PKCS8EncodedKeySpec 44 * @see RSAPrivateKeySpec 45 * @see RSAPublicKeySpec 46 * @see RSAOtherPrimeInfo 47 * 48 * @since 1.4 49 */ 50 51 public class RSAMultiPrimePrivateCrtKeySpec extends RSAPrivateKeySpec { 52 53 private final BigInteger publicExponent; 54 private final BigInteger primeP; 55 private final BigInteger primeQ; 56 private final BigInteger primeExponentP; 57 private final BigInteger primeExponentQ; 58 private final BigInteger crtCoefficient; 59 private final RSAOtherPrimeInfo[] otherPrimeInfo; 60 61 /** 62 * Creates a new {@code RSAMultiPrimePrivateCrtKeySpec}. 63 * 64 * <p>Note that the contents of {@code otherPrimeInfo} 65 * are copied to protect against subsequent modification when 66 * constructing this object. 67 * 68 * @param modulus the modulus n 69 * @param publicExponent the public exponent e 70 * @param privateExponent the private exponent d 71 * @param primeP the prime factor p of n 72 * @param primeQ the prime factor q of q 73 * @param primeExponentP this is d mod (p-1) 74 * @param primeExponentQ this is d mod (q-1) 75 * @param crtCoefficient the Chinese Remainder Theorem 76 * coefficient q-1 mod p 77 * @param otherPrimeInfo triplets of the rest of primes, null can be 78 * specified if there are only two prime factors 79 * (p and q) 80 * @exception NullPointerException if any of the specified parameters 81 * with the exception of {@code otherPrimeInfo} is null 82 * @exception IllegalArgumentException if an empty, i.e. 0-length, 83 * {@code otherPrimeInfo} is specified 84 */ 85 public RSAMultiPrimePrivateCrtKeySpec(BigInteger modulus, 86 BigInteger publicExponent, 87 BigInteger privateExponent, 88 BigInteger primeP, 89 BigInteger primeQ, 90 BigInteger primeExponentP, 91 BigInteger primeExponentQ, 92 BigInteger crtCoefficient, 93 RSAOtherPrimeInfo[] otherPrimeInfo) { 94 this(modulus, publicExponent, privateExponent, primeP, primeQ, 95 primeExponentP, primeExponentQ, crtCoefficient, otherPrimeInfo, 96 null); 97 } 98 99 /** 100 * Creates a new {@code RSAMultiPrimePrivateCrtKeySpec} with additional 101 * key parameters. 102 * 103 * <p>Note that the contents of {@code otherPrimeInfo} 104 * are copied to protect against subsequent modification when 105 * constructing this object. 106 * 107 * @param modulus the modulus n 108 * @param publicExponent the public exponent e 109 * @param privateExponent the private exponent d 110 * @param primeP the prime factor p of n 111 * @param primeQ the prime factor q of n 112 * @param primeExponentP this is d mod (p-1) 113 * @param primeExponentQ this is d mod (q-1) 114 * @param crtCoefficient the Chinese Remainder Theorem coefficient 115 * q-1 mod p 116 * @param otherPrimeInfo triplets of the rest of primes, null can be 117 * specified if there are only two prime factors 118 * (p and q) 119 * @param keyParams the parameters associated with key 120 * @exception NullPointerException if any of the specified parameters 121 * with the exception of {@code otherPrimeInfo} and {@code keyParams} 122 * is null 123 * @exception IllegalArgumentException if an empty, i.e. 0-length, 124 * {@code otherPrimeInfo} is specified 125 * @since 11 126 */ 127 public RSAMultiPrimePrivateCrtKeySpec(BigInteger modulus, 128 BigInteger publicExponent, 129 BigInteger privateExponent, 130 BigInteger primeP, 131 BigInteger primeQ, 132 BigInteger primeExponentP, 133 BigInteger primeExponentQ, 134 BigInteger crtCoefficient, 135 RSAOtherPrimeInfo[] otherPrimeInfo, 136 AlgorithmParameterSpec keyParams) { 137 super(modulus, privateExponent, keyParams); 138 Objects.requireNonNull(modulus, 139 "the modulus parameter must be non-null"); 140 Objects.requireNonNull(publicExponent, 141 "the publicExponent parameter must be non-null"); 142 Objects.requireNonNull(privateExponent, 143 "the privateExponent parameter must be non-null"); 144 Objects.requireNonNull(primeP, "the primeP parameter must be non-null"); 145 Objects.requireNonNull(primeQ, "the primeQ parameter must be non-null"); 146 Objects.requireNonNull(primeExponentP, 147 "the primeExponentP parameter must be non-null"); 148 Objects.requireNonNull(primeExponentQ, 149 "the primeExponentQ parameter must be non-null"); 150 Objects.requireNonNull(crtCoefficient, 151 "the crtCoefficient parameter must be non-null"); 152 153 this.publicExponent = publicExponent; 154 this.primeP = primeP; 155 this.primeQ = primeQ; 156 this.primeExponentP = primeExponentP; 157 this.primeExponentQ = primeExponentQ; 158 this.crtCoefficient = crtCoefficient; 159 if (otherPrimeInfo == null) { 160 this.otherPrimeInfo = null; 161 } else if (otherPrimeInfo.length == 0) { 162 throw new IllegalArgumentException("the otherPrimeInfo " + 163 "parameter must not be empty"); 164 } else { 165 this.otherPrimeInfo = otherPrimeInfo.clone(); 166 } 167 } 168 169 /** 170 * Returns the public exponent. 171 * 172 * @return the public exponent. 207 * 208 * @return the primeExponentQ. 209 */ 210 public BigInteger getPrimeExponentQ() { 211 return this.primeExponentQ; 212 } 213 214 /** 215 * Returns the crtCoefficient. 216 * 217 * @return the crtCoefficient. 218 */ 219 public BigInteger getCrtCoefficient() { 220 return this.crtCoefficient; 221 } 222 223 /** 224 * Returns a copy of the otherPrimeInfo or null if there are 225 * only two prime factors (p and q). 226 * 227 * @return the otherPrimeInfo. Returns a new array each time this method 228 * is called. 229 */ 230 public RSAOtherPrimeInfo[] getOtherPrimeInfo() { 231 if (otherPrimeInfo == null) return null; 232 return otherPrimeInfo.clone(); 233 } 234 } |