cm_pmeth.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  2. * project 2010.
  3. */
  4. /* ====================================================================
  5. * Copyright (c) 2010 The OpenSSL Project. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. All advertising materials mentioning features or use of this
  20. * software must display the following acknowledgment:
  21. * "This product includes software developed by the OpenSSL Project
  22. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  23. *
  24. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  25. * endorse or promote products derived from this software without
  26. * prior written permission. For written permission, please contact
  27. * licensing@OpenSSL.org.
  28. *
  29. * 5. Products derived from this software may not be called "OpenSSL"
  30. * nor may "OpenSSL" appear in their names without prior written
  31. * permission of the OpenSSL Project.
  32. *
  33. * 6. Redistributions of any form whatsoever must retain the following
  34. * acknowledgment:
  35. * "This product includes software developed by the OpenSSL Project
  36. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  39. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  41. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  42. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  44. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  45. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  46. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  47. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  48. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  49. * OF THE POSSIBILITY OF SUCH DAMAGE.
  50. * ====================================================================
  51. *
  52. * This product includes cryptographic software written by Eric Young
  53. * (eay@cryptsoft.com). This product includes software written by Tim
  54. * Hudson (tjh@cryptsoft.com).
  55. *
  56. */
  57. #include <stdio.h>
  58. #include "cryptlib.h"
  59. #include <openssl/x509.h>
  60. #include <openssl/x509v3.h>
  61. #include <openssl/evp.h>
  62. #include <openssl/cmac.h>
  63. #include "evp_locl.h"
  64. /* The context structure and "key" is simply a CMAC_CTX */
  65. static int pkey_cmac_init(EVP_PKEY_CTX *ctx)
  66. {
  67. ctx->data = CMAC_CTX_new();
  68. if (!ctx->data)
  69. return 0;
  70. ctx->keygen_info_count = 0;
  71. return 1;
  72. }
  73. static int pkey_cmac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
  74. {
  75. if (!pkey_cmac_init(dst))
  76. return 0;
  77. if (!CMAC_CTX_copy(dst->data, src->data))
  78. return 0;
  79. return 1;
  80. }
  81. static void pkey_cmac_cleanup(EVP_PKEY_CTX *ctx)
  82. {
  83. CMAC_CTX_free(ctx->data);
  84. }
  85. static int pkey_cmac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  86. {
  87. CMAC_CTX *cmkey = CMAC_CTX_new();
  88. CMAC_CTX *cmctx = ctx->data;
  89. if (!cmkey)
  90. return 0;
  91. if (!CMAC_CTX_copy(cmkey, cmctx))
  92. {
  93. CMAC_CTX_free(cmkey);
  94. return 0;
  95. }
  96. EVP_PKEY_assign(pkey, EVP_PKEY_CMAC, cmkey);
  97. return 1;
  98. }
  99. static int int_update(EVP_MD_CTX *ctx,const void *data,size_t count)
  100. {
  101. if (!CMAC_Update(ctx->pctx->data, data, count))
  102. return 0;
  103. return 1;
  104. }
  105. static int cmac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
  106. {
  107. EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_NO_INIT);
  108. mctx->update = int_update;
  109. return 1;
  110. }
  111. static int cmac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
  112. EVP_MD_CTX *mctx)
  113. {
  114. return CMAC_Final(ctx->data, sig, siglen);
  115. }
  116. static int pkey_cmac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  117. {
  118. CMAC_CTX *cmctx = ctx->data;
  119. switch (type)
  120. {
  121. case EVP_PKEY_CTRL_SET_MAC_KEY:
  122. if (!p2 || p1 < 0)
  123. return 0;
  124. if (!CMAC_Init(cmctx, p2, p1, NULL, NULL))
  125. return 0;
  126. break;
  127. case EVP_PKEY_CTRL_CIPHER:
  128. if (!CMAC_Init(cmctx, NULL, 0, p2, ctx->engine))
  129. return 0;
  130. break;
  131. case EVP_PKEY_CTRL_MD:
  132. if (ctx->pkey && !CMAC_CTX_copy(ctx->data,
  133. (CMAC_CTX *)ctx->pkey->pkey.ptr))
  134. return 0;
  135. if (!CMAC_Init(cmctx, NULL, 0, NULL, NULL))
  136. return 0;
  137. break;
  138. default:
  139. return -2;
  140. }
  141. return 1;
  142. }
  143. static int pkey_cmac_ctrl_str(EVP_PKEY_CTX *ctx,
  144. const char *type, const char *value)
  145. {
  146. if (!value)
  147. {
  148. return 0;
  149. }
  150. if (!strcmp(type, "key"))
  151. {
  152. void *p = (void *)value;
  153. return pkey_cmac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY,
  154. strlen(p), p);
  155. }
  156. if (!strcmp(type, "cipher"))
  157. {
  158. const EVP_CIPHER *c;
  159. c = EVP_get_cipherbyname(value);
  160. if (!c)
  161. return 0;
  162. return pkey_cmac_ctrl(ctx, EVP_PKEY_CTRL_CIPHER, -1, (void *)c);
  163. }
  164. if (!strcmp(type, "hexkey"))
  165. {
  166. unsigned char *key;
  167. int r;
  168. long keylen;
  169. key = string_to_hex(value, &keylen);
  170. if (!key)
  171. return 0;
  172. r = pkey_cmac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, keylen, key);
  173. OPENSSL_free(key);
  174. return r;
  175. }
  176. return -2;
  177. }
  178. const EVP_PKEY_METHOD cmac_pkey_meth =
  179. {
  180. EVP_PKEY_CMAC,
  181. EVP_PKEY_FLAG_SIGCTX_CUSTOM,
  182. pkey_cmac_init,
  183. pkey_cmac_copy,
  184. pkey_cmac_cleanup,
  185. 0, 0,
  186. 0,
  187. pkey_cmac_keygen,
  188. 0, 0,
  189. 0, 0,
  190. 0,0,
  191. cmac_signctx_init,
  192. cmac_signctx,
  193. 0,0,
  194. 0,0,
  195. 0,0,
  196. 0,0,
  197. pkey_cmac_ctrl,
  198. pkey_cmac_ctrl_str
  199. };