RSAMultiPrimePrivateCrtKeySpec.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* PSSParameterSpec.java --
  2. Copyright (C) 2003, Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.security.spec;
  32. import java.math.BigInteger;
  33. /**
  34. * This class represents an RSA multi-prime private key, as defined in the
  35. * PKCS#1 v2.1, using the <i>Chinese Remainder Theorem</i> (CRT) information
  36. * values.
  37. *
  38. * @since 1.4
  39. * @see java.security.Key
  40. * @see java.security.KeyFactory
  41. * @see KeySpec
  42. * @see PKCS8EncodedKeySpec
  43. * @see RSAPrivateKeySpec
  44. * @see RSAPublicKeySpec
  45. * @see RSAOtherPrimeInfo
  46. */
  47. public class RSAMultiPrimePrivateCrtKeySpec extends RSAPrivateKeySpec
  48. {
  49. // Constants and fields
  50. // --------------------------------------------------------------------------
  51. private BigInteger publicExponent;
  52. private BigInteger primeP;
  53. private BigInteger primeQ;
  54. private BigInteger primeExponentP;
  55. private BigInteger primeExponentQ;
  56. private BigInteger crtCoefficient;
  57. private RSAOtherPrimeInfo[] otherPrimeInfo;
  58. // Constructor(s)
  59. // --------------------------------------------------------------------------
  60. /**
  61. * Constructs a new instance of <code>RSAMultiPrimePrivateCrtKeySpec</code>
  62. * given the various PKCS#1 v2.1 parameters.
  63. *
  64. * <p>Note that <code>otherPrimeInfo</code> is cloned when constructing this
  65. * object.</p>
  66. *
  67. * @param modulus
  68. * the modulus n.
  69. * @param publicExponent
  70. * the public exponent e.
  71. * @param privateExponent
  72. * the private exponent d.
  73. * @param primeP
  74. * the prime factor p of n.
  75. * @param primeQ
  76. * the prime factor q of n.
  77. * @param primeExponentP
  78. * this is d mod (p-1).
  79. * @param primeExponentQ
  80. * this is d mod (q-1).
  81. * @param crtCoefficient
  82. * the Chinese Remainder Theorem coefficient q-1 mod p.
  83. * @param otherPrimeInfo
  84. * triplets of the rest of primes, <code>null</code> can be
  85. * specified if there are only two prime factors (p and q).
  86. * @throws NullPointerException
  87. * if any of the parameters is <code>null</code>.
  88. * @throws IllegalArgumentException
  89. * if an empty <code>otherPrimeInfo</code> is specified.
  90. */
  91. public RSAMultiPrimePrivateCrtKeySpec(BigInteger modulus,
  92. BigInteger publicExponent,
  93. BigInteger privateExponent,
  94. BigInteger primeP,
  95. BigInteger primeQ,
  96. BigInteger primeExponentP,
  97. BigInteger primeExponentQ,
  98. BigInteger crtCoefficient,
  99. RSAOtherPrimeInfo[] otherPrimeInfo)
  100. {
  101. super(modulus, privateExponent);
  102. if (modulus == null)
  103. throw new NullPointerException("modulus");
  104. if (publicExponent == null)
  105. throw new NullPointerException("publicExponent");
  106. if (privateExponent == null)
  107. throw new NullPointerException("privateExponent");
  108. if (primeP == null)
  109. throw new NullPointerException("primeP");
  110. if (primeQ == null)
  111. throw new NullPointerException("primeQ");
  112. if (primeExponentP == null)
  113. throw new NullPointerException("primeExponentP");
  114. if (primeExponentQ == null)
  115. throw new NullPointerException("primeExponentQ");
  116. if (crtCoefficient == null)
  117. throw new NullPointerException("crtCoefficient");
  118. if (otherPrimeInfo != null)
  119. if (otherPrimeInfo.length == 0)
  120. throw new IllegalArgumentException();
  121. else
  122. this.otherPrimeInfo = (RSAOtherPrimeInfo[]) otherPrimeInfo.clone();
  123. this.publicExponent = publicExponent;
  124. this.primeP = primeP;
  125. this.primeQ = primeQ;
  126. this.primeExponentP = primeExponentP;
  127. this.primeExponentQ = primeExponentQ;
  128. this.crtCoefficient = crtCoefficient;
  129. }
  130. // Class methods
  131. // --------------------------------------------------------------------------
  132. // Instance methods
  133. // --------------------------------------------------------------------------
  134. /**
  135. * Returns the public exponent.
  136. *
  137. * @return the public exponent.
  138. */
  139. public BigInteger getPublicExponent()
  140. {
  141. return this.publicExponent;
  142. }
  143. /**
  144. * Returns the prime p.
  145. *
  146. * @return the prime p.
  147. */
  148. public BigInteger getPrimeP()
  149. {
  150. return this.primeP;
  151. }
  152. /**
  153. * Returns the prime q.
  154. *
  155. * @return the prime q.
  156. */
  157. public BigInteger getPrimeQ()
  158. {
  159. return this.primeQ;
  160. }
  161. /**
  162. * Returns d mod (p-1).
  163. *
  164. * @return d mod (p-1).
  165. */
  166. public BigInteger getPrimeExponentP()
  167. {
  168. return this.primeExponentP;
  169. }
  170. /**
  171. * Returns d mod (q-1).
  172. *
  173. * @return d mod (q-1).
  174. */
  175. public BigInteger getPrimeExponentQ()
  176. {
  177. return this.primeExponentQ;
  178. }
  179. /**
  180. * Returns the CRT Coefficient q-1 mod p.
  181. *
  182. * @return the CRT Coefficient q-1 mod p.
  183. */
  184. public BigInteger getCrtCoefficient()
  185. {
  186. return this.crtCoefficient;
  187. }
  188. /**
  189. * Returns a clone of <code>otherPrimeInfo</code> or <code>null</code> if
  190. * it was <code>null</code> at construction time.
  191. *
  192. * @return a cloned copy of <code>otherPrimeInfo</code>.
  193. */
  194. public RSAOtherPrimeInfo[] getOtherPrimeInfo()
  195. {
  196. return this.otherPrimeInfo == null
  197. ? null
  198. : (RSAOtherPrimeInfo[]) this.otherPrimeInfo.clone();
  199. }
  200. }