pkey_mac.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/err.h>
  10. #include <openssl/evp.h>
  11. #include "internal/evp_int.h"
  12. /* MAC PKEY context structure */
  13. typedef struct {
  14. EVP_MAC_CTX *ctx;
  15. /*
  16. * We know of two MAC types:
  17. *
  18. * 1. those who take a secret in raw form, i.e. raw data as a
  19. * ASN1_OCTET_STRING embedded in a EVP_PKEY. So far, that's
  20. * all of them but CMAC.
  21. * 2. those who take a secret with associated cipher in very generic
  22. * form, i.e. a complete EVP_MAC_CTX embedded in a PKEY. So far,
  23. * only CMAC does this.
  24. *
  25. * (one might wonder why the second form isn't used for all)
  26. */
  27. #define MAC_TYPE_RAW 1 /* HMAC like MAC type (all but CMAC so far) */
  28. #define MAC_TYPE_MAC 2 /* CMAC like MAC type (only CMAC known so far) */
  29. int type;
  30. /* The following is only used for MAC_TYPE_RAW implementations */
  31. struct {
  32. const EVP_MD *md; /* temp storage of MD */
  33. ASN1_OCTET_STRING ktmp; /* temp storage for key */
  34. } raw_data;
  35. } MAC_PKEY_CTX;
  36. static int pkey_mac_init(EVP_PKEY_CTX *ctx)
  37. {
  38. MAC_PKEY_CTX *hctx;
  39. int nid = ctx->pmeth->pkey_id;
  40. if ((hctx = OPENSSL_zalloc(sizeof(*hctx))) == NULL) {
  41. EVPerr(EVP_F_PKEY_MAC_INIT, ERR_R_MALLOC_FAILURE);
  42. return 0;
  43. }
  44. /* We're being smart and using the same base NIDs for PKEY and for MAC */
  45. hctx->ctx = EVP_MAC_CTX_new_id(nid);
  46. if (hctx->ctx == NULL) {
  47. OPENSSL_free(hctx);
  48. return 0;
  49. }
  50. if (nid == EVP_PKEY_CMAC) {
  51. hctx->type = MAC_TYPE_MAC;
  52. } else {
  53. hctx->type = MAC_TYPE_RAW;
  54. hctx->raw_data.ktmp.type = V_ASN1_OCTET_STRING;
  55. }
  56. EVP_PKEY_CTX_set_data(ctx, hctx);
  57. ctx->keygen_info_count = 0;
  58. return 1;
  59. }
  60. static void pkey_mac_cleanup(EVP_PKEY_CTX *ctx);
  61. static int pkey_mac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
  62. {
  63. MAC_PKEY_CTX *sctx, *dctx;
  64. if (!pkey_mac_init(dst))
  65. return 0;
  66. sctx = EVP_PKEY_CTX_get_data(src);
  67. dctx = EVP_PKEY_CTX_get_data(dst);
  68. if (!EVP_MAC_CTX_copy(dctx->ctx, sctx->ctx))
  69. goto err;
  70. switch (dctx->type) {
  71. case MAC_TYPE_RAW:
  72. dctx->raw_data.md = sctx->raw_data.md;
  73. if (ASN1_STRING_get0_data(&sctx->raw_data.ktmp) != NULL &&
  74. !ASN1_STRING_copy(&dctx->raw_data.ktmp, &sctx->raw_data.ktmp))
  75. goto err;
  76. break;
  77. case MAC_TYPE_MAC:
  78. /* Nothing more to do */
  79. break;
  80. default:
  81. /* This should be dead code */
  82. return 0;
  83. }
  84. return 1;
  85. err:
  86. pkey_mac_cleanup (dst);
  87. return 0;
  88. }
  89. static void pkey_mac_cleanup(EVP_PKEY_CTX *ctx)
  90. {
  91. MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
  92. if (hctx != NULL) {
  93. switch (hctx->type) {
  94. case MAC_TYPE_RAW:
  95. OPENSSL_clear_free(hctx->raw_data.ktmp.data,
  96. hctx->raw_data.ktmp.length);
  97. break;
  98. }
  99. EVP_MAC_CTX_free(hctx->ctx);
  100. OPENSSL_free(hctx);
  101. EVP_PKEY_CTX_set_data(ctx, NULL);
  102. }
  103. }
  104. static int pkey_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  105. {
  106. MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
  107. int nid = ctx->pmeth->pkey_id;
  108. switch (hctx->type) {
  109. case MAC_TYPE_RAW:
  110. {
  111. ASN1_OCTET_STRING *hkey = NULL;
  112. if (!hctx->raw_data.ktmp.data)
  113. return 0;
  114. hkey = ASN1_OCTET_STRING_dup(&hctx->raw_data.ktmp);
  115. if (!hkey)
  116. return 0;
  117. EVP_PKEY_assign(pkey, nid, hkey);
  118. }
  119. break;
  120. case MAC_TYPE_MAC:
  121. {
  122. EVP_MAC_CTX *cmkey = EVP_MAC_CTX_new_id(nid);
  123. if (cmkey == NULL)
  124. return 0;
  125. if (!EVP_MAC_CTX_copy(cmkey, hctx->ctx)) {
  126. EVP_MAC_CTX_free(cmkey);
  127. return 0;
  128. }
  129. EVP_PKEY_assign(pkey, nid, cmkey);
  130. }
  131. break;
  132. default:
  133. /* This should be dead code */
  134. return 0;
  135. }
  136. return 1;
  137. }
  138. static int int_update(EVP_MD_CTX *ctx, const void *data, size_t count)
  139. {
  140. MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(EVP_MD_CTX_pkey_ctx(ctx));
  141. if (!EVP_MAC_update(hctx->ctx, data, count))
  142. return 0;
  143. return 1;
  144. }
  145. static int pkey_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
  146. {
  147. MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
  148. ASN1_OCTET_STRING *key = NULL;
  149. int rv = 1;
  150. /*
  151. * For MACs with the EVP_PKEY_FLAG_SIGCTX_CUSTOM flag set and that
  152. * gets the key passed as an ASN.1 OCTET STRING, we set the key here,
  153. * as this may be only time it's set during a DigestSign.
  154. *
  155. * MACs that pass around the key in form of EVP_MAC_CTX are setting
  156. * the key through other mechanisms. (this is only CMAC for now)
  157. */
  158. int set_key =
  159. hctx->type == MAC_TYPE_RAW
  160. && (ctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM) != 0;
  161. if (set_key) {
  162. if (EVP_PKEY_id(EVP_PKEY_CTX_get0_pkey(ctx))
  163. != EVP_MAC_nid(EVP_MAC_CTX_mac(hctx->ctx)))
  164. return 0;
  165. key = EVP_PKEY_get0(EVP_PKEY_CTX_get0_pkey(ctx));
  166. if (key == NULL)
  167. return 0;
  168. }
  169. /* Some MACs don't support this control... that's fine */
  170. EVP_MAC_ctrl(hctx->ctx, EVP_MAC_CTRL_SET_FLAGS,
  171. EVP_MD_CTX_test_flags(mctx, ~EVP_MD_CTX_FLAG_NO_INIT));
  172. EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_NO_INIT);
  173. EVP_MD_CTX_set_update_fn(mctx, int_update);
  174. if (set_key)
  175. rv = EVP_MAC_ctrl(hctx->ctx, EVP_MAC_CTRL_SET_KEY, key->data,
  176. key->length);
  177. return rv > 0;
  178. }
  179. static int pkey_mac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig,
  180. size_t *siglen, EVP_MD_CTX *mctx)
  181. {
  182. MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
  183. return EVP_MAC_final(hctx->ctx, sig, siglen);
  184. }
  185. static int pkey_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  186. {
  187. MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
  188. switch (type) {
  189. case EVP_PKEY_CTRL_CIPHER:
  190. switch (hctx->type) {
  191. case MAC_TYPE_RAW:
  192. return -2; /* The raw types don't support ciphers */
  193. case MAC_TYPE_MAC:
  194. {
  195. int rv;
  196. if ((rv = EVP_MAC_ctrl(hctx->ctx, EVP_MAC_CTRL_SET_ENGINE,
  197. ctx->engine)) < 0
  198. || (rv = EVP_MAC_ctrl(hctx->ctx, EVP_MAC_CTRL_SET_CIPHER,
  199. p2)) < 0
  200. || !(rv = EVP_MAC_init(hctx->ctx)))
  201. return rv;
  202. }
  203. break;
  204. default:
  205. /* This should be dead code */
  206. return 0;
  207. }
  208. break;
  209. case EVP_PKEY_CTRL_MD:
  210. switch (hctx->type) {
  211. case MAC_TYPE_RAW:
  212. hctx->raw_data.md = p2;
  213. break;
  214. case MAC_TYPE_MAC:
  215. if (ctx->pkey != NULL
  216. && !EVP_MAC_CTX_copy(hctx->ctx,
  217. (EVP_MAC_CTX *)ctx->pkey->pkey.ptr))
  218. return 0;
  219. if (!EVP_MAC_init(hctx->ctx))
  220. return 0;
  221. break;
  222. default:
  223. /* This should be dead code */
  224. return 0;
  225. }
  226. break;
  227. case EVP_PKEY_CTRL_SET_DIGEST_SIZE:
  228. return EVP_MAC_ctrl(hctx->ctx, EVP_MAC_CTRL_SET_SIZE, (size_t)p1);
  229. case EVP_PKEY_CTRL_SET_MAC_KEY:
  230. switch (hctx->type) {
  231. case MAC_TYPE_RAW:
  232. if ((!p2 && p1 > 0) || (p1 < -1))
  233. return 0;
  234. if (!ASN1_OCTET_STRING_set(&hctx->raw_data.ktmp, p2, p1))
  235. return 0;
  236. break;
  237. case MAC_TYPE_MAC:
  238. if (!EVP_MAC_ctrl(hctx->ctx, EVP_MAC_CTRL_SET_KEY, p2, p1))
  239. return 0;
  240. break;
  241. default:
  242. /* This should be dead code */
  243. return 0;
  244. }
  245. break;
  246. case EVP_PKEY_CTRL_DIGESTINIT:
  247. switch (hctx->type) {
  248. case MAC_TYPE_RAW:
  249. /* Ensure that we have attached the implementation */
  250. if (!EVP_MAC_init(hctx->ctx))
  251. return 0;
  252. {
  253. int rv;
  254. ASN1_OCTET_STRING *key =
  255. (ASN1_OCTET_STRING *)ctx->pkey->pkey.ptr;
  256. if ((rv = EVP_MAC_ctrl(hctx->ctx, EVP_MAC_CTRL_SET_ENGINE,
  257. ctx->engine)) < 0
  258. || (rv = EVP_MAC_ctrl(hctx->ctx, EVP_MAC_CTRL_SET_MD,
  259. hctx->raw_data.md)) < 0
  260. || (rv = EVP_MAC_ctrl(hctx->ctx, EVP_MAC_CTRL_SET_KEY,
  261. key->data, key->length)) < 0)
  262. return rv;
  263. }
  264. break;
  265. case MAC_TYPE_MAC:
  266. return -2; /* The mac types don't support ciphers */
  267. default:
  268. /* This should be dead code */
  269. return 0;
  270. }
  271. break;
  272. default:
  273. return -2;
  274. }
  275. return 1;
  276. }
  277. static int pkey_mac_ctrl_str(EVP_PKEY_CTX *ctx,
  278. const char *type, const char *value)
  279. {
  280. MAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
  281. return EVP_MAC_ctrl_str(hctx->ctx, type, value);
  282. }
  283. const EVP_PKEY_METHOD cmac_pkey_meth = {
  284. EVP_PKEY_CMAC,
  285. EVP_PKEY_FLAG_SIGCTX_CUSTOM,
  286. pkey_mac_init,
  287. pkey_mac_copy,
  288. pkey_mac_cleanup,
  289. 0, 0,
  290. 0,
  291. pkey_mac_keygen,
  292. 0, 0,
  293. 0, 0,
  294. 0, 0,
  295. pkey_mac_signctx_init,
  296. pkey_mac_signctx,
  297. 0, 0,
  298. 0, 0,
  299. 0, 0,
  300. 0, 0,
  301. pkey_mac_ctrl,
  302. pkey_mac_ctrl_str
  303. };
  304. const EVP_PKEY_METHOD hmac_pkey_meth = {
  305. EVP_PKEY_HMAC,
  306. 0,
  307. pkey_mac_init,
  308. pkey_mac_copy,
  309. pkey_mac_cleanup,
  310. 0, 0,
  311. 0,
  312. pkey_mac_keygen,
  313. 0, 0,
  314. 0, 0,
  315. 0, 0,
  316. pkey_mac_signctx_init,
  317. pkey_mac_signctx,
  318. 0, 0,
  319. 0, 0,
  320. 0, 0,
  321. 0, 0,
  322. pkey_mac_ctrl,
  323. pkey_mac_ctrl_str
  324. };
  325. const EVP_PKEY_METHOD siphash_pkey_meth = {
  326. EVP_PKEY_SIPHASH,
  327. EVP_PKEY_FLAG_SIGCTX_CUSTOM,
  328. pkey_mac_init,
  329. pkey_mac_copy,
  330. pkey_mac_cleanup,
  331. 0, 0,
  332. 0,
  333. pkey_mac_keygen,
  334. 0, 0,
  335. 0, 0,
  336. 0, 0,
  337. pkey_mac_signctx_init,
  338. pkey_mac_signctx,
  339. 0, 0,
  340. 0, 0,
  341. 0, 0,
  342. 0, 0,
  343. pkey_mac_ctrl,
  344. pkey_mac_ctrl_str
  345. };
  346. const EVP_PKEY_METHOD poly1305_pkey_meth = {
  347. EVP_PKEY_POLY1305,
  348. EVP_PKEY_FLAG_SIGCTX_CUSTOM,
  349. pkey_mac_init,
  350. pkey_mac_copy,
  351. pkey_mac_cleanup,
  352. 0, 0,
  353. 0,
  354. pkey_mac_keygen,
  355. 0, 0,
  356. 0, 0,
  357. 0, 0,
  358. pkey_mac_signctx_init,
  359. pkey_mac_signctx,
  360. 0, 0,
  361. 0, 0,
  362. 0, 0,
  363. 0, 0,
  364. pkey_mac_ctrl,
  365. pkey_mac_ctrl_str
  366. };