cm_pmeth.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright 2010-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/x509.h>
  12. #include <openssl/x509v3.h>
  13. #include <openssl/evp.h>
  14. #include <openssl/cmac.h>
  15. #include "internal/evp_int.h"
  16. /* The context structure and "key" is simply a CMAC_CTX */
  17. static int pkey_cmac_init(EVP_PKEY_CTX *ctx)
  18. {
  19. ctx->data = CMAC_CTX_new();
  20. if (ctx->data == NULL)
  21. return 0;
  22. ctx->keygen_info_count = 0;
  23. return 1;
  24. }
  25. static int pkey_cmac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
  26. {
  27. if (!pkey_cmac_init(dst))
  28. return 0;
  29. if (!CMAC_CTX_copy(dst->data, src->data))
  30. return 0;
  31. return 1;
  32. }
  33. static void pkey_cmac_cleanup(EVP_PKEY_CTX *ctx)
  34. {
  35. CMAC_CTX_free(ctx->data);
  36. }
  37. static int pkey_cmac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  38. {
  39. CMAC_CTX *cmkey = CMAC_CTX_new();
  40. CMAC_CTX *cmctx = ctx->data;
  41. if (cmkey == NULL)
  42. return 0;
  43. if (!CMAC_CTX_copy(cmkey, cmctx)) {
  44. CMAC_CTX_free(cmkey);
  45. return 0;
  46. }
  47. EVP_PKEY_assign(pkey, EVP_PKEY_CMAC, cmkey);
  48. return 1;
  49. }
  50. static int int_update(EVP_MD_CTX *ctx, const void *data, size_t count)
  51. {
  52. if (!CMAC_Update(EVP_MD_CTX_pkey_ctx(ctx)->data, data, count))
  53. return 0;
  54. return 1;
  55. }
  56. static int cmac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
  57. {
  58. EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_NO_INIT);
  59. EVP_MD_CTX_set_update_fn(mctx, int_update);
  60. return 1;
  61. }
  62. static int cmac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
  63. EVP_MD_CTX *mctx)
  64. {
  65. return CMAC_Final(ctx->data, sig, siglen);
  66. }
  67. static int pkey_cmac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  68. {
  69. CMAC_CTX *cmctx = ctx->data;
  70. switch (type) {
  71. case EVP_PKEY_CTRL_SET_MAC_KEY:
  72. if (!p2 || p1 < 0)
  73. return 0;
  74. if (!CMAC_Init(cmctx, p2, p1, NULL, NULL))
  75. return 0;
  76. break;
  77. case EVP_PKEY_CTRL_CIPHER:
  78. if (!CMAC_Init(cmctx, NULL, 0, p2, ctx->engine))
  79. return 0;
  80. break;
  81. case EVP_PKEY_CTRL_MD:
  82. if (ctx->pkey && !CMAC_CTX_copy(ctx->data,
  83. (CMAC_CTX *)ctx->pkey->pkey.ptr))
  84. return 0;
  85. if (!CMAC_Init(cmctx, NULL, 0, NULL, NULL))
  86. return 0;
  87. break;
  88. default:
  89. return -2;
  90. }
  91. return 1;
  92. }
  93. static int pkey_cmac_ctrl_str(EVP_PKEY_CTX *ctx,
  94. const char *type, const char *value)
  95. {
  96. if (!value) {
  97. return 0;
  98. }
  99. if (strcmp(type, "cipher") == 0) {
  100. const EVP_CIPHER *c;
  101. c = EVP_get_cipherbyname(value);
  102. if (!c)
  103. return 0;
  104. return pkey_cmac_ctrl(ctx, EVP_PKEY_CTRL_CIPHER, -1, (void *)c);
  105. }
  106. if (strcmp(type, "key") == 0)
  107. return EVP_PKEY_CTX_str2ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, value);
  108. if (strcmp(type, "hexkey") == 0)
  109. return EVP_PKEY_CTX_hex2ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, value);
  110. return -2;
  111. }
  112. const EVP_PKEY_METHOD cmac_pkey_meth = {
  113. EVP_PKEY_CMAC,
  114. EVP_PKEY_FLAG_SIGCTX_CUSTOM,
  115. pkey_cmac_init,
  116. pkey_cmac_copy,
  117. pkey_cmac_cleanup,
  118. 0, 0,
  119. 0,
  120. pkey_cmac_keygen,
  121. 0, 0,
  122. 0, 0,
  123. 0, 0,
  124. cmac_signctx_init,
  125. cmac_signctx,
  126. 0, 0,
  127. 0, 0,
  128. 0, 0,
  129. 0, 0,
  130. pkey_cmac_ctrl,
  131. pkey_cmac_ctrl_str
  132. };