1. /*
  2. * @(#)RSAMultiPrimePrivateCrtKeySpec.java 1.8 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.security.spec;
  8. import java.math.BigInteger;
  9. /**
  10. * This class specifies an RSA multi-prime private key, as defined in the
  11. * PKCS#1 v2.1, using the Chinese Remainder Theorem (CRT) information
  12. * values for efficiency.
  13. *
  14. * @author Valerie Peng
  15. *
  16. * @version 1.8 03/12/19
  17. *
  18. * @see java.security.Key
  19. * @see java.security.KeyFactory
  20. * @see KeySpec
  21. * @see PKCS8EncodedKeySpec
  22. * @see RSAPrivateKeySpec
  23. * @see RSAPublicKeySpec
  24. * @see RSAOtherPrimeInfo
  25. *
  26. * @since 1.4
  27. */
  28. public class RSAMultiPrimePrivateCrtKeySpec extends RSAPrivateKeySpec {
  29. private final BigInteger publicExponent;
  30. private final BigInteger primeP;
  31. private final BigInteger primeQ;
  32. private final BigInteger primeExponentP;
  33. private final BigInteger primeExponentQ;
  34. private final BigInteger crtCoefficient;
  35. private final RSAOtherPrimeInfo otherPrimeInfo[];
  36. /**
  37. * Creates a new <code>RSAMultiPrimePrivateCrtKeySpec</code>
  38. * given the modulus, publicExponent, privateExponent,
  39. * primeP, primeQ, primeExponentP, primeExponentQ,
  40. * crtCoefficient, and otherPrimeInfo as defined in PKCS#1 v2.1.
  41. *
  42. * <p>Note that the contents of <code>otherPrimeInfo</code>
  43. * are copied to protect against subsequent modification when
  44. * constructing this object.
  45. *
  46. * @param modulus the modulus n.
  47. * @param publicExponent the public exponent e.
  48. * @param privateExponent the private exponent d.
  49. * @param primeP the prime factor p of n.
  50. * @param primeQ the prime factor q of n.
  51. * @param primeExponentP this is d mod (p-1).
  52. * @param primeExponentQ this is d mod (q-1).
  53. * @param crtCoefficient the Chinese Remainder Theorem
  54. * coefficient q-1 mod p.
  55. * @param otherPrimeInfo triplets of the rest of primes, null can be
  56. * specified if there are only two prime factors (p and q).
  57. * @exception NullPointerException if any of the parameters, i.e.
  58. * <code>modulus</code>,
  59. * <code>publicExponent</code>, <code>privateExponent</code>,
  60. * <code>primeP</code>, <code>primeQ</code>,
  61. * <code>primeExponentP</code>, <code>primeExponentQ</code>,
  62. * <code>crtCoefficient</code>, is null.
  63. * @exception IllegalArgumentException if an empty, i.e. 0-length,
  64. * <code>otherPrimeInfo</code> is specified.
  65. */
  66. public RSAMultiPrimePrivateCrtKeySpec(BigInteger modulus,
  67. BigInteger publicExponent,
  68. BigInteger privateExponent,
  69. BigInteger primeP,
  70. BigInteger primeQ,
  71. BigInteger primeExponentP,
  72. BigInteger primeExponentQ,
  73. BigInteger crtCoefficient,
  74. RSAOtherPrimeInfo[] otherPrimeInfo) {
  75. super(modulus, privateExponent);
  76. if (modulus == null) {
  77. throw new NullPointerException("the modulus parameter must be " +
  78. "non-null");
  79. }
  80. if (publicExponent == null) {
  81. throw new NullPointerException("the publicExponent parameter " +
  82. "must be non-null");
  83. }
  84. if (privateExponent == null) {
  85. throw new NullPointerException("the privateExponent parameter " +
  86. "must be non-null");
  87. }
  88. if (primeP == null) {
  89. throw new NullPointerException("the primeP parameter " +
  90. "must be non-null");
  91. }
  92. if (primeQ == null) {
  93. throw new NullPointerException("the primeQ parameter " +
  94. "must be non-null");
  95. }
  96. if (primeExponentP == null) {
  97. throw new NullPointerException("the primeExponentP parameter " +
  98. "must be non-null");
  99. }
  100. if (primeExponentQ == null) {
  101. throw new NullPointerException("the primeExponentQ parameter " +
  102. "must be non-null");
  103. }
  104. if (crtCoefficient == null) {
  105. throw new NullPointerException("the crtCoefficient parameter " +
  106. "must be non-null");
  107. }
  108. this.publicExponent = publicExponent;
  109. this.primeP = primeP;
  110. this.primeQ = primeQ;
  111. this.primeExponentP = primeExponentP;
  112. this.primeExponentQ = primeExponentQ;
  113. this.crtCoefficient = crtCoefficient;
  114. if (otherPrimeInfo == null) {
  115. this.otherPrimeInfo = null;
  116. } else if (otherPrimeInfo.length == 0) {
  117. throw new IllegalArgumentException("the otherPrimeInfo " +
  118. "parameter must not be empty");
  119. } else {
  120. this.otherPrimeInfo = (RSAOtherPrimeInfo[])otherPrimeInfo.clone();
  121. }
  122. }
  123. /**
  124. * Returns the public exponent.
  125. *
  126. * @return the public exponent.
  127. */
  128. public BigInteger getPublicExponent() {
  129. return this.publicExponent;
  130. }
  131. /**
  132. * Returns the primeP.
  133. *
  134. * @return the primeP.
  135. */
  136. public BigInteger getPrimeP() {
  137. return this.primeP;
  138. }
  139. /**
  140. * Returns the primeQ.
  141. *
  142. * @return the primeQ.
  143. */
  144. public BigInteger getPrimeQ() {
  145. return this.primeQ;
  146. }
  147. /**
  148. * Returns the primeExponentP.
  149. *
  150. * @return the primeExponentP.
  151. */
  152. public BigInteger getPrimeExponentP() {
  153. return this.primeExponentP;
  154. }
  155. /**
  156. * Returns the primeExponentQ.
  157. *
  158. * @return the primeExponentQ.
  159. */
  160. public BigInteger getPrimeExponentQ() {
  161. return this.primeExponentQ;
  162. }
  163. /**
  164. * Returns the crtCoefficient.
  165. *
  166. * @return the crtCoefficient.
  167. */
  168. public BigInteger getCrtCoefficient() {
  169. return this.crtCoefficient;
  170. }
  171. /**
  172. * Returns a copy of the otherPrimeInfo or null if there are
  173. * only two prime factors (p and q).
  174. *
  175. * @return the otherPrimeInfo. Returns a new array each
  176. * time this method is called.
  177. */
  178. public RSAOtherPrimeInfo[] getOtherPrimeInfo() {
  179. if (otherPrimeInfo == null) return null;
  180. return (RSAOtherPrimeInfo[]) otherPrimeInfo.clone();
  181. }
  182. }