1. /*
  2. * @(#)PSSParameterSpec.java 1.6 04/01/27
  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. import java.security.spec.MGF1ParameterSpec;
  10. /**
  11. * This class specifies a parameter spec for RSA-PSS signature scheme,
  12. * as defined in the
  13. * <a href="http://www.rsa.com/rsalabs/pubs/PKCS/html/pkcs-1.html">
  14. * PKCS#1 v2.1</a> standard.
  15. *
  16. * <p>Its ASN.1 definition in PKCS#1 standard is described below:
  17. * <pre>
  18. * RSASSA-PSS-params ::= SEQUENCE {
  19. * hashAlgorithm [0] OAEP-PSSDigestAlgorithms DEFAULT sha1,
  20. * maskGenAlgorithm [1] PKCS1MGFAlgorithms DEFAULT mgf1SHA1,
  21. * saltLength [2] INTEGER DEFAULT 20,
  22. * trailerField [3] INTEGER DEFAULT 1
  23. * }
  24. * </pre>
  25. * where
  26. * <pre>
  27. * OAEP-PSSDigestAlgorithms ALGORITHM-IDENTIFIER ::= {
  28. * { OID id-sha1 PARAMETERS NULL }|
  29. * { OID id-sha256 PARAMETERS NULL }|
  30. * { OID id-sha384 PARAMETERS NULL }|
  31. * { OID id-sha512 PARAMETERS NULL },
  32. * ... -- Allows for future expansion --
  33. * }
  34. *
  35. * PKCS1MGFAlgorithms ALGORITHM-IDENTIFIER ::= {
  36. * { OID id-mgf1 PARAMETERS OAEP-PSSDigestAlgorithms },
  37. * ... -- Allows for future expansion --
  38. * }
  39. * </pre>
  40. * <p>Note: the PSSParameterSpec.DEFAULT uses the following:
  41. * message digest -- "SHA-1"
  42. * mask generation function (mgf) -- "MGF1"
  43. * parameters for mgf -- MGF1ParameterSpec.SHA1
  44. * SaltLength -- 20
  45. * TrailerField -- 1
  46. *
  47. * @see MGF1ParameterSpec
  48. * @see AlgorithmParameterSpec
  49. * @see java.security.Signature
  50. *
  51. * @author Valerie Peng
  52. *
  53. * @version 1.6 04/01/27
  54. *
  55. * @since 1.4
  56. */
  57. public class PSSParameterSpec implements AlgorithmParameterSpec {
  58. private String mdName = "SHA-1";
  59. private String mgfName = "MGF1";
  60. private AlgorithmParameterSpec mgfSpec = MGF1ParameterSpec.SHA1;
  61. private int saltLen = 20;
  62. private int trailerField = 1;
  63. /**
  64. * The PSS parameter set with all default values.
  65. */
  66. public static final PSSParameterSpec DEFAULT = new PSSParameterSpec();
  67. /**
  68. * Constructs a new <code>PSSParameterSpec</code> as defined in
  69. * the PKCS #1 standard using the default values.
  70. */
  71. private PSSParameterSpec() {
  72. }
  73. /**
  74. * Creates a new <code>PSSParameterSpec</code> as defined in
  75. * the PKCS #1 standard using the specified message digest,
  76. * mask generation function, parameters for mask generation
  77. * function, salt length, and trailer field values.
  78. *
  79. * @param mdName the algorithm name of the hash function.
  80. * @param mgfName the algorithm name of the mask generation
  81. * function.
  82. * @param mgfSpec the parameters for the mask generation
  83. * function. If null is specified, null will be returned by
  84. * getMGFParameters().
  85. * @param saltLen the length of salt.
  86. * @param trailerField the value of the trailer field.
  87. * @exception NullPointerException if <code>mdName</code>,
  88. * or <code>mgfName</code> is null.
  89. * @exception IllegalArgumentException if <code>saltLen</code>
  90. * or <code>trailerField</code> is less than 0.
  91. * @since 1.5
  92. */
  93. public PSSParameterSpec(String mdName, String mgfName,
  94. AlgorithmParameterSpec mgfSpec,
  95. int saltLen, int trailerField) {
  96. if (mdName == null) {
  97. throw new NullPointerException("digest algorithm is null");
  98. }
  99. if (mgfName == null) {
  100. throw new NullPointerException("mask generation function " +
  101. "algorithm is null");
  102. }
  103. if (saltLen < 0) {
  104. throw new IllegalArgumentException("negative saltLen value: " +
  105. saltLen);
  106. }
  107. if (trailerField < 0) {
  108. throw new IllegalArgumentException("negative trailerField: " +
  109. trailerField);
  110. }
  111. this.mdName = mdName;
  112. this.mgfName = mgfName;
  113. this.mgfSpec = mgfSpec;
  114. this.saltLen = saltLen;
  115. this.trailerField = trailerField;
  116. }
  117. /**
  118. * Creates a new <code>PSSParameterSpec</code>
  119. * using the specified salt length and other default values as
  120. * defined in PKCS#1.
  121. *
  122. * @param saltLen the length of salt in bits to be used in PKCS#1
  123. * PSS encoding.
  124. * @exception IllegalArgumentException if <code>saltLen</code> is
  125. * less than 0.
  126. */
  127. public PSSParameterSpec(int saltLen) {
  128. if (saltLen < 0) {
  129. throw new IllegalArgumentException("negative saltLen value: " +
  130. saltLen);
  131. }
  132. this.saltLen = saltLen;
  133. }
  134. /**
  135. * Returns the message digest algorithm name.
  136. *
  137. * @return the message digest algorithm name.
  138. * @since 1.5
  139. */
  140. public String getDigestAlgorithm() {
  141. return mdName;
  142. }
  143. /**
  144. * Returns the mask generation function algorithm name.
  145. *
  146. * @return the mask generation function algorithm name.
  147. *
  148. * @since 1.5
  149. */
  150. public String getMGFAlgorithm() {
  151. return mgfName;
  152. }
  153. /**
  154. * Returns the parameters for the mask generation function.
  155. *
  156. * @return the parameters for the mask generation function.
  157. * @since 1.5
  158. */
  159. public AlgorithmParameterSpec getMGFParameters() {
  160. return mgfSpec;
  161. }
  162. /**
  163. * Returns the salt length in bits.
  164. *
  165. * @return the salt length.
  166. */
  167. public int getSaltLength() {
  168. return saltLen;
  169. }
  170. /**
  171. * Returns the value for the trailer field, i.e. bc in PKCS#1 v2.1.
  172. *
  173. * @return the value for the trailer field, i.e. bc in PKCS#1 v2.1.
  174. * @since 1.5
  175. */
  176. public int getTrailerField() {
  177. return trailerField;
  178. }
  179. }