fips_dsa_sign.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* fips_dsa_sign.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  4. * 2007.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2007 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <string.h>
  60. #include <openssl/evp.h>
  61. #include <openssl/dsa.h>
  62. #include <openssl/err.h>
  63. #include <openssl/sha.h>
  64. #include <openssl/bn.h>
  65. #ifdef OPENSSL_FIPS
  66. /*
  67. * FIPS versions of DSA_sign() and DSA_verify(). These include a tiny ASN1
  68. * encoder/decoder to handle the specific case of a DSA signature.
  69. */
  70. # if 0
  71. int FIPS_dsa_size(DSA *r)
  72. {
  73. int ilen;
  74. ilen = BN_num_bytes(r->q);
  75. if (ilen > 20)
  76. return -1;
  77. /* If MSB set need padding byte */
  78. ilen++;
  79. /*
  80. * Also need 2 bytes INTEGER header for r and s plus 2 bytes SEQUENCE
  81. * header making 6 in total.
  82. */
  83. return ilen * 2 + 6;
  84. }
  85. # endif
  86. /*
  87. * Tiny ASN1 encoder for DSA_SIG structure. We can assume r, s smaller than
  88. * 0x80 octets as by the DSA standards they will be less than 2^160
  89. */
  90. int FIPS_dsa_sig_encode(unsigned char *out, DSA_SIG *sig)
  91. {
  92. int rlen, slen, rpad, spad, seqlen;
  93. rlen = BN_num_bytes(sig->r);
  94. if (rlen > 20)
  95. return -1;
  96. if (BN_num_bits(sig->r) & 0x7)
  97. rpad = 0;
  98. else
  99. rpad = 1;
  100. slen = BN_num_bytes(sig->s);
  101. if (slen > 20)
  102. return -1;
  103. if (BN_num_bits(sig->s) & 0x7)
  104. spad = 0;
  105. else
  106. spad = 1;
  107. /* Length of SEQUENCE, (1 tag + 1 len octet) * 2 + content octets */
  108. seqlen = rlen + rpad + slen + spad + 4;
  109. /* Actual encoded length: include SEQUENCE header */
  110. if (!out)
  111. return seqlen + 2;
  112. /* Output SEQUENCE header */
  113. *out++ = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
  114. *out++ = (unsigned char)seqlen;
  115. /* Output r */
  116. *out++ = V_ASN1_INTEGER;
  117. *out++ = (unsigned char)(rlen + rpad);
  118. if (rpad)
  119. *out++ = 0;
  120. BN_bn2bin(sig->r, out);
  121. out += rlen;
  122. /* Output s */
  123. *out++ = V_ASN1_INTEGER;
  124. *out++ = (unsigned char)(slen + spad);
  125. if (spad)
  126. *out++ = 0;
  127. BN_bn2bin(sig->s, out);
  128. return seqlen + 2;
  129. }
  130. /* Companion DSA_SIG decoder */
  131. int FIPS_dsa_sig_decode(DSA_SIG *sig, const unsigned char *in, int inlen)
  132. {
  133. int seqlen, rlen, slen;
  134. const unsigned char *rbin;
  135. /* Sanity check */
  136. /* Need SEQUENCE tag */
  137. if (*in++ != (V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED))
  138. return 0;
  139. /* Get length octet */
  140. seqlen = *in++;
  141. /* Check sensible length value */
  142. if (seqlen < 4 || seqlen > 0x7F)
  143. return 0;
  144. /* Check INTEGER tag */
  145. if (*in++ != V_ASN1_INTEGER)
  146. return 0;
  147. rlen = *in++;
  148. seqlen -= 2 + rlen;
  149. /* Check sensible seqlen value */
  150. if (seqlen < 2)
  151. return 0;
  152. rbin = in;
  153. in += rlen;
  154. /* Check INTEGER tag */
  155. if (*in++ != V_ASN1_INTEGER)
  156. return 0;
  157. slen = *in++;
  158. /*
  159. * Remaining bytes of SEQUENCE should exactly match encoding of s
  160. */
  161. if (seqlen != (slen + 2))
  162. return 0;
  163. if (!sig->r && !(sig->r = BN_new()))
  164. return 0;
  165. if (!sig->s && !(sig->s = BN_new()))
  166. return 0;
  167. if (!BN_bin2bn(rbin, rlen, sig->r))
  168. return 0;
  169. if (!BN_bin2bn(in, slen, sig->s))
  170. return 0;
  171. return 1;
  172. }
  173. static int fips_dsa_sign(int type, const unsigned char *x, int y,
  174. unsigned char *sig, unsigned int *siglen,
  175. EVP_MD_SVCTX * sv)
  176. {
  177. DSA *dsa = sv->key;
  178. unsigned char dig[EVP_MAX_MD_SIZE];
  179. unsigned int dlen;
  180. DSA_SIG *s;
  181. EVP_DigestFinal_ex(sv->mctx, dig, &dlen);
  182. s = dsa->meth->dsa_do_sign(dig, dlen, dsa);
  183. OPENSSL_cleanse(dig, dlen);
  184. if (s == NULL) {
  185. *siglen = 0;
  186. return 0;
  187. }
  188. *siglen = FIPS_dsa_sig_encode(sig, s);
  189. DSA_SIG_free(s);
  190. if (*siglen < 0)
  191. return 0;
  192. return 1;
  193. }
  194. static int fips_dsa_verify(int type, const unsigned char *x, int y,
  195. const unsigned char *sigbuf, unsigned int siglen,
  196. EVP_MD_SVCTX * sv)
  197. {
  198. DSA *dsa = sv->key;
  199. DSA_SIG *s;
  200. int ret = -1;
  201. unsigned char dig[EVP_MAX_MD_SIZE];
  202. unsigned int dlen;
  203. s = DSA_SIG_new();
  204. if (s == NULL)
  205. return ret;
  206. if (!FIPS_dsa_sig_decode(s, sigbuf, siglen))
  207. goto err;
  208. EVP_DigestFinal_ex(sv->mctx, dig, &dlen);
  209. ret = dsa->meth->dsa_do_verify(dig, dlen, s, dsa);
  210. OPENSSL_cleanse(dig, dlen);
  211. err:
  212. DSA_SIG_free(s);
  213. return ret;
  214. }
  215. static int init(EVP_MD_CTX *ctx)
  216. {
  217. return SHA1_Init(ctx->md_data);
  218. }
  219. static int update(EVP_MD_CTX *ctx, const void *data, size_t count)
  220. {
  221. return SHA1_Update(ctx->md_data, data, count);
  222. }
  223. static int final(EVP_MD_CTX *ctx, unsigned char *md)
  224. {
  225. return SHA1_Final(md, ctx->md_data);
  226. }
  227. static const EVP_MD dss1_md = {
  228. NID_dsa,
  229. NID_dsaWithSHA1,
  230. SHA_DIGEST_LENGTH,
  231. EVP_MD_FLAG_FIPS | EVP_MD_FLAG_SVCTX,
  232. init,
  233. update,
  234. final,
  235. NULL,
  236. NULL,
  237. (evp_sign_method *) fips_dsa_sign,
  238. (evp_verify_method *) fips_dsa_verify,
  239. {EVP_PKEY_DSA, EVP_PKEY_DSA2, EVP_PKEY_DSA3, EVP_PKEY_DSA4, 0},
  240. SHA_CBLOCK,
  241. sizeof(EVP_MD *) + sizeof(SHA_CTX),
  242. };
  243. const EVP_MD *EVP_dss1(void)
  244. {
  245. return (&dss1_md);
  246. }
  247. #endif