1. /*
  2. * @(#)SignatureSpi.java 1.14 00/02/02
  3. *
  4. * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.security;
  11. import java.security.spec.AlgorithmParameterSpec;
  12. import java.util.*;
  13. import java.io.*;
  14. /**
  15. * This class defines the <i>Service Provider Interface</i> (<b>SPI</b>)
  16. * for the <code>Signature</code> class, which is used to provide the
  17. * functionality of a digital signature algorithm. Digital signatures are used
  18. * for authentication and integrity assurance of digital data.
  19. *.
  20. * <p> All the abstract methods in this class must be implemented by each
  21. * cryptographic service provider who wishes to supply the implementation
  22. * of a particular signature algorithm.
  23. *
  24. * @author Benjamin Renaud
  25. *
  26. * @version 1.14, 02/02/00
  27. *
  28. * @see Signature
  29. */
  30. public abstract class SignatureSpi {
  31. /**
  32. * Application-specified source of randomness.
  33. */
  34. protected SecureRandom appRandom = null;
  35. /**
  36. * Initializes this signature object with the specified
  37. * public key for verification operations.
  38. *
  39. * @param publicKey the public key of the identity whose signature is
  40. * going to be verified.
  41. *
  42. * @exception InvalidKeyException if the key is improperly
  43. * encoded, parameters are missing, and so on.
  44. */
  45. protected abstract void engineInitVerify(PublicKey publicKey)
  46. throws InvalidKeyException;
  47. /**
  48. * Initializes this signature object with the specified
  49. * private key for signing operations.
  50. *
  51. * @param privateKey the private key of the identity whose signature
  52. * will be generated.
  53. *
  54. * @exception InvalidKeyException if the key is improperly
  55. * encoded, parameters are missing, and so on.
  56. */
  57. protected abstract void engineInitSign(PrivateKey privateKey)
  58. throws InvalidKeyException;
  59. /**
  60. * Initializes this signature object with the specified
  61. * private key and source of randomness for signing operations.
  62. *
  63. * <p>This concrete method has been added to this previously-defined
  64. * abstract class. (For backwards compatibility, it cannot be abstract.)
  65. *
  66. * @param privateKey the private key of the identity whose signature
  67. * will be generated.
  68. * @param random the source of randomness
  69. *
  70. * @exception InvalidKeyException if the key is improperly
  71. * encoded, parameters are missing, and so on.
  72. */
  73. protected void engineInitSign(PrivateKey privateKey,
  74. SecureRandom random)
  75. throws InvalidKeyException {
  76. this.appRandom = random;
  77. engineInitSign(privateKey);
  78. }
  79. /**
  80. * Updates the data to be signed or verified
  81. * using the specified byte.
  82. *
  83. * @param b the byte to use for the update.
  84. *
  85. * @exception SignatureException if the engine is not initialized
  86. * properly.
  87. */
  88. protected abstract void engineUpdate(byte b) throws SignatureException;
  89. /**
  90. * Updates the data to be signed or verified, using the
  91. * specified array of bytes, starting at the specified offset.
  92. *
  93. * @param data the array of bytes.
  94. * @param off the offset to start from in the array of bytes.
  95. * @param len the number of bytes to use, starting at offset.
  96. *
  97. * @exception SignatureException if the engine is not initialized
  98. * properly.
  99. */
  100. protected abstract void engineUpdate(byte[] b, int off, int len)
  101. throws SignatureException;
  102. /**
  103. * Returns the signature bytes of all the data
  104. * updated so far.
  105. * The format of the signature depends on the underlying
  106. * signature scheme.
  107. *
  108. * @return the signature bytes of the signing operation's result.
  109. *
  110. * @exception SignatureException if the engine is not
  111. * initialized properly.
  112. */
  113. protected abstract byte[] engineSign() throws SignatureException;
  114. /**
  115. * Finishes this signature operation and stores the resulting signature
  116. * bytes in the provided buffer <code>outbuf</code>, starting at
  117. * <code>offset</code>.
  118. * The format of the signature depends on the underlying
  119. * signature scheme.
  120. *
  121. * <p>The signature implementation is reset to its initial state
  122. * (the state it was in after a call to one of the
  123. * <code>engineInitSign</code> methods)
  124. * and can be reused to generate further signatures with the same private
  125. * key.
  126. *
  127. * This method should be abstract, but we leave it concrete for
  128. * binary compatibility. Knowledgeable providers should override this
  129. * method.
  130. *
  131. * @param outbuf buffer for the signature result.
  132. *
  133. * @param offset offset into <code>outbuf</code> where the signature is
  134. * stored.
  135. *
  136. * @param len number of bytes within <code>outbuf</code> allotted for the
  137. * signature.
  138. * Both this default implementation and the SUN provider do not
  139. * return partial digests. If the value of this parameter is less
  140. * than the actual signature length, this method will throw a
  141. * SignatureException.
  142. * This parameter is ignored if its value is greater than or equal to
  143. * the actual signature length.
  144. *
  145. * @return the number of bytes placed into <code>outbuf</code>
  146. *
  147. * @exception SignatureException if an error occurs or <code>len</code>
  148. * is less than the actual signature length.
  149. *
  150. * @since 1.2
  151. */
  152. protected int engineSign(byte[] outbuf, int offset, int len)
  153. throws SignatureException {
  154. byte[] sig = engineSign();
  155. if (len < sig.length) {
  156. throw new SignatureException
  157. ("partial signatures not returned");
  158. }
  159. if (outbuf.length - offset < sig.length) {
  160. throw new SignatureException
  161. ("insufficient space in the output buffer to store the "
  162. + "signature");
  163. }
  164. System.arraycopy(sig, 0, outbuf, offset, sig.length);
  165. return sig.length;
  166. }
  167. /**
  168. * Verifies the passed-in signature.
  169. *
  170. * @param sigBytes the signature bytes to be verified.
  171. *
  172. * @return true if the signature was verified, false if not.
  173. *
  174. * @exception SignatureException if the engine is not initialized
  175. * properly, or the passed-in signature is improperly encoded or
  176. * of the wrong type, etc.
  177. */
  178. protected abstract boolean engineVerify(byte[] sigBytes)
  179. throws SignatureException;
  180. /**
  181. * Sets the specified algorithm parameter to the specified
  182. * value. This method supplies a general-purpose mechanism through
  183. * which it is possible to set the various parameters of this object.
  184. * A parameter may be any settable parameter for the algorithm, such as
  185. * a parameter size, or a source of random bits for signature generation
  186. * (if appropriate), or an indication of whether or not to perform
  187. * a specific but optional computation. A uniform algorithm-specific
  188. * naming scheme for each parameter is desirable but left unspecified
  189. * at this time.
  190. *
  191. * @param param the string identifier of the parameter.
  192. *
  193. * @param value the parameter value.
  194. *
  195. * @exception InvalidParameterException if <code>param</code> is an
  196. * invalid parameter for this signature algorithm engine,
  197. * the parameter is already set
  198. * and cannot be set again, a security exception occurs, and so on.
  199. *
  200. * @deprecated Replaced by {@link
  201. * #engineSetParameter(java.security.spec.AlgorithmParameterSpec)
  202. * engineSetParameter}.
  203. */
  204. protected abstract void engineSetParameter(String param, Object value)
  205. throws InvalidParameterException;
  206. /**
  207. * Initializes this signature engine with the specified parameter set.
  208. *
  209. * This concrete method has been added to this previously-defined
  210. * abstract class. (For backwards compatibility, it cannot be abstract.)
  211. * It may be overridden by a provider to set the algorithm parameters
  212. * using the specified <code>params</code>. Such an override
  213. * is expected to throw an InvalidAlgorithmParameterException if
  214. * a parameter is invalid.
  215. * If this method is not overridden, it always throws an
  216. * UnsupportedOperationException.
  217. *
  218. * @param params the parameters
  219. *
  220. * @exception InvalidAlgorithmParameterException if the given parameters
  221. * are inappropriate for this signature engine
  222. */
  223. protected void engineSetParameter(AlgorithmParameterSpec params)
  224. throws InvalidAlgorithmParameterException {
  225. throw new UnsupportedOperationException();
  226. }
  227. /**
  228. * Gets the value of the specified algorithm parameter.
  229. * This method supplies a general-purpose mechanism through which it
  230. * is possible to get the various parameters of this object. A parameter
  231. * may be any settable parameter for the algorithm, such as a parameter
  232. * size, or a source of random bits for signature generation (if
  233. * appropriate), or an indication of whether or not to perform a
  234. * specific but optional computation. A uniform algorithm-specific
  235. * naming scheme for each parameter is desirable but left unspecified
  236. * at this time.
  237. *
  238. * @param param the string name of the parameter.
  239. *
  240. * @return the object that represents the parameter value, or null if
  241. * there is none.
  242. *
  243. * @exception InvalidParameterException if <code>param</code> is an
  244. * invalid parameter for this engine, or another exception occurs while
  245. * trying to get this parameter.
  246. *
  247. * @deprecated
  248. */
  249. protected abstract Object engineGetParameter(String param)
  250. throws InvalidParameterException;
  251. /**
  252. * Returns a clone if the implementation is cloneable.
  253. *
  254. * @return a clone if the implementation is cloneable.
  255. *
  256. * @exception CloneNotSupportedException if this is called
  257. * on an implementation that does not support <code>Cloneable</code>.
  258. */
  259. public Object clone() throws CloneNotSupportedException {
  260. if (this instanceof Cloneable) {
  261. return super.clone();
  262. } else {
  263. throw new CloneNotSupportedException();
  264. }
  265. }
  266. }