cm_meth.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 <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. /* local CMAC pkey structure */
  17. /* typedef EVP_MAC_IMPL */
  18. struct evp_mac_impl_st {
  19. /* tmpcipher and tmpengine are set to NULL after a CMAC_Init call */
  20. const EVP_CIPHER *tmpcipher; /* cached CMAC cipher */
  21. const ENGINE *tmpengine; /* cached CMAC cipher engine */
  22. CMAC_CTX *ctx;
  23. };
  24. static EVP_MAC_IMPL *cmac_new(void)
  25. {
  26. EVP_MAC_IMPL *cctx;
  27. if ((cctx = OPENSSL_zalloc(sizeof(*cctx))) == NULL
  28. || (cctx->ctx = CMAC_CTX_new()) == NULL) {
  29. OPENSSL_free(cctx);
  30. cctx = NULL;
  31. }
  32. return cctx;
  33. }
  34. static void cmac_free(EVP_MAC_IMPL *cctx)
  35. {
  36. if (cctx != NULL) {
  37. CMAC_CTX_free(cctx->ctx);
  38. OPENSSL_free(cctx);
  39. }
  40. }
  41. static int cmac_copy(EVP_MAC_IMPL *cdst, EVP_MAC_IMPL *csrc)
  42. {
  43. if (!CMAC_CTX_copy(cdst->ctx, csrc->ctx))
  44. return 0;
  45. cdst->tmpengine = csrc->tmpengine;
  46. cdst->tmpcipher = csrc->tmpcipher;
  47. return 1;
  48. }
  49. static size_t cmac_size(EVP_MAC_IMPL *cctx)
  50. {
  51. return EVP_CIPHER_CTX_block_size(CMAC_CTX_get0_cipher_ctx(cctx->ctx));
  52. }
  53. static int cmac_init(EVP_MAC_IMPL *cctx)
  54. {
  55. int rv = CMAC_Init(cctx->ctx, NULL, 0, cctx->tmpcipher,
  56. (ENGINE *)cctx->tmpengine);
  57. cctx->tmpcipher = NULL;
  58. cctx->tmpengine = NULL;
  59. return rv;
  60. }
  61. static int cmac_update(EVP_MAC_IMPL *cctx, const unsigned char *data,
  62. size_t datalen)
  63. {
  64. return CMAC_Update(cctx->ctx, data, datalen);
  65. }
  66. static int cmac_final(EVP_MAC_IMPL *cctx, unsigned char *out)
  67. {
  68. size_t hlen;
  69. return CMAC_Final(cctx->ctx, out, &hlen);
  70. }
  71. static int cmac_ctrl(EVP_MAC_IMPL *cctx, int cmd, va_list args)
  72. {
  73. switch (cmd) {
  74. case EVP_MAC_CTRL_SET_KEY:
  75. {
  76. const unsigned char *key = va_arg(args, const unsigned char *);
  77. size_t keylen = va_arg(args, size_t);
  78. int rv = CMAC_Init(cctx->ctx, key, keylen, cctx->tmpcipher,
  79. (ENGINE *)cctx->tmpengine);
  80. cctx->tmpcipher = NULL;
  81. cctx->tmpengine = NULL;
  82. return rv;
  83. }
  84. break;
  85. case EVP_MAC_CTRL_SET_CIPHER:
  86. cctx->tmpcipher = va_arg(args, const EVP_CIPHER *);
  87. break;
  88. case EVP_MAC_CTRL_SET_ENGINE:
  89. cctx->tmpengine = va_arg(args, const ENGINE *);
  90. break;
  91. default:
  92. return -2;
  93. }
  94. return 1;
  95. }
  96. static int cmac_ctrl_int(EVP_MAC_IMPL *hctx, int cmd, ...)
  97. {
  98. int rv;
  99. va_list args;
  100. va_start(args, cmd);
  101. rv = cmac_ctrl(hctx, cmd, args);
  102. va_end(args);
  103. return rv;
  104. }
  105. static int cmac_ctrl_str_cb(void *hctx, int cmd, void *buf, size_t buflen)
  106. {
  107. return cmac_ctrl_int(hctx, cmd, buf, buflen);
  108. }
  109. static int cmac_ctrl_str(EVP_MAC_IMPL *cctx, const char *type,
  110. const char *value)
  111. {
  112. if (!value)
  113. return 0;
  114. if (strcmp(type, "cipher") == 0) {
  115. const EVP_CIPHER *c = EVP_get_cipherbyname(value);
  116. if (c == NULL)
  117. return 0;
  118. return cmac_ctrl_int(cctx, EVP_MAC_CTRL_SET_CIPHER, c);
  119. }
  120. if (strcmp(type, "key") == 0)
  121. return EVP_str2ctrl(cmac_ctrl_str_cb, cctx, EVP_MAC_CTRL_SET_KEY,
  122. value);
  123. if (strcmp(type, "hexkey") == 0)
  124. return EVP_hex2ctrl(cmac_ctrl_str_cb, cctx, EVP_MAC_CTRL_SET_KEY,
  125. value);
  126. return -2;
  127. }
  128. const EVP_MAC cmac_meth = {
  129. EVP_MAC_CMAC,
  130. cmac_new,
  131. cmac_copy,
  132. cmac_free,
  133. cmac_size,
  134. cmac_init,
  135. cmac_update,
  136. cmac_final,
  137. cmac_ctrl,
  138. cmac_ctrl_str
  139. };