1. /*
  2. * @(#)RSAMultiPrimePrivateCrtKeySpec.java 1.5 03/01/23
  3. *
  4. * Copyright 2003 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.5 03/01/23
  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 BigInteger modulus;
  30. private BigInteger publicExponent;
  31. private BigInteger privateExponent;
  32. private BigInteger primeP;
  33. private BigInteger primeQ;
  34. private BigInteger primeExponentP;
  35. private BigInteger primeExponentQ;
  36. private BigInteger crtCoefficient;
  37. private RSAOtherPrimeInfo otherPrimeInfo[];
  38. /**
  39. * Creates a new <code>RSAMultiPrimePrivateCrtKeySpec</code>
  40. * given the modulus, publicExponent, privateExponent,
  41. * primeP, primeQ, primeExponentP, primeExponentQ,
  42. * crtCoefficient, and otherPrimeInfo as defined in PKCS#1 v2.1.
  43. *
  44. * <p>Note that <code>otherPrimeInfo</code> is cloned when constructing
  45. * this object.
  46. *
  47. * @param modulus the modulus n.
  48. * @param publicExponent the public exponent e.
  49. * @param privateExponent the private exponent d.
  50. * @param primeP the prime factor p of n.
  51. * @param primeQ the prime factor q of n.
  52. * @param primeExponentP this is d mod (p-1).
  53. * @param primeExponentQ this is d mod (q-1).
  54. * @param crtCoefficient the Chinese Remainder Theorem
  55. * coefficient q-1 mod p.
  56. * @param otherPrimeInfo triplets of the rest of primes, null can be
  57. * specified if there are only two prime factors (p and q).
  58. * @exception NullPointerException if any of the parameters, i.e.
  59. * <code>modulus</code>,
  60. * <code>publicExponent</code>, <code>privateExponent</code>,
  61. * <code>primeP</code>, <code>primeQ</code>,
  62. * <code>primeExponentP</code>, <code>primeExponentQ</code>,
  63. * <code>crtCoefficient</code>, is null.
  64. * @exception IllegalArgumentException if an empty, i.e. 0-length,
  65. * <code>otherPrimeInfo</code> is specified.
  66. */
  67. public RSAMultiPrimePrivateCrtKeySpec(BigInteger modulus,
  68. BigInteger publicExponent,
  69. BigInteger privateExponent,
  70. BigInteger primeP,
  71. BigInteger primeQ,
  72. BigInteger primeExponentP,
  73. BigInteger primeExponentQ,
  74. BigInteger crtCoefficient,
  75. RSAOtherPrimeInfo[] otherPrimeInfo) {
  76. super(modulus, privateExponent);
  77. if (modulus == null) {
  78. throw new NullPointerException("the modulus parameter must be " +
  79. "non-null");
  80. }
  81. if (publicExponent == null) {
  82. throw new NullPointerException("the publicExponent parameter " +
  83. "must be non-null");
  84. }
  85. if (privateExponent == null) {
  86. throw new NullPointerException("the privateExponent parameter " +
  87. "must be non-null");
  88. }
  89. if (primeP == null) {
  90. throw new NullPointerException("the primeP parameter " +
  91. "must be non-null");
  92. }
  93. if (primeQ == null) {
  94. throw new NullPointerException("the primeQ parameter " +
  95. "must be non-null");
  96. }
  97. if (primeExponentP == null) {
  98. throw new NullPointerException("the primeExponentP parameter " +
  99. "must be non-null");
  100. }
  101. if (primeExponentQ == null) {
  102. throw new NullPointerException("the primeExponentQ parameter " +
  103. "must be non-null");
  104. }
  105. if (crtCoefficient == null) {
  106. throw new NullPointerException("the crtCoefficient parameter " +
  107. "must be non-null");
  108. }
  109. this.publicExponent = publicExponent;
  110. this.primeP = primeP;
  111. this.primeQ = primeQ;
  112. this.primeExponentP = primeExponentP;
  113. this.primeExponentQ = primeExponentQ;
  114. this.crtCoefficient = crtCoefficient;
  115. if (otherPrimeInfo == null) {
  116. this.otherPrimeInfo = null;
  117. } else if (otherPrimeInfo.length == 0) {
  118. throw new IllegalArgumentException("the otherPrimeInfo " +
  119. "parameter must not be empty");
  120. } else {
  121. this.otherPrimeInfo = (RSAOtherPrimeInfo[])otherPrimeInfo.clone();
  122. }
  123. }
  124. /**
  125. * Returns the public exponent.
  126. *
  127. * @return the public exponent.
  128. */
  129. public BigInteger getPublicExponent() {
  130. return this.publicExponent;
  131. }
  132. /**
  133. * Returns the primeP.
  134. *
  135. * @return the primeP.
  136. */
  137. public BigInteger getPrimeP() {
  138. return this.primeP;
  139. }
  140. /**
  141. * Returns the primeQ.
  142. *
  143. * @return the primeQ.
  144. */
  145. public BigInteger getPrimeQ() {
  146. return this.primeQ;
  147. }
  148. /**
  149. * Returns the primeExponentP.
  150. *
  151. * @return the primeExponentP.
  152. */
  153. public BigInteger getPrimeExponentP() {
  154. return this.primeExponentP;
  155. }
  156. /**
  157. * Returns the primeExponentQ.
  158. *
  159. * @return the primeExponentQ.
  160. */
  161. public BigInteger getPrimeExponentQ() {
  162. return this.primeExponentQ;
  163. }
  164. /**
  165. * Returns the crtCoefficient.
  166. *
  167. * @return the crtCoefficient.
  168. */
  169. public BigInteger getCrtCoefficient() {
  170. return this.crtCoefficient;
  171. }
  172. /**
  173. * Returns a copy of the otherPrimeInfo or null if there are
  174. * only two prime factors (p and q).
  175. *
  176. * @return the otherPrimeInfo.
  177. */
  178. public RSAOtherPrimeInfo[] getOtherPrimeInfo() {
  179. if (otherPrimeInfo == null) return null;
  180. return (RSAOtherPrimeInfo[]) otherPrimeInfo.clone();
  181. }
  182. }