hm_ameth.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright 2007-2021 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/evp.h>
  12. #include "crypto/asn1.h"
  13. #include "crypto/evp.h"
  14. /*
  15. * HMAC "ASN1" method. This is just here to indicate the maximum HMAC output
  16. * length and to free up an HMAC key.
  17. */
  18. static int hmac_size(const EVP_PKEY *pkey)
  19. {
  20. return EVP_MAX_MD_SIZE;
  21. }
  22. static void hmac_key_free(EVP_PKEY *pkey)
  23. {
  24. ASN1_OCTET_STRING *os = EVP_PKEY_get0(pkey);
  25. if (os) {
  26. if (os->data)
  27. OPENSSL_cleanse(os->data, os->length);
  28. ASN1_OCTET_STRING_free(os);
  29. }
  30. }
  31. static int hmac_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
  32. {
  33. switch (op) {
  34. case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
  35. *(int *)arg2 = NID_sha256;
  36. return 1;
  37. default:
  38. return -2;
  39. }
  40. }
  41. static int hmac_pkey_public_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
  42. {
  43. /* the ameth pub_cmp must return 1 on match, 0 on mismatch */
  44. return ASN1_OCTET_STRING_cmp(EVP_PKEY_get0(a), EVP_PKEY_get0(b)) == 0;
  45. }
  46. static int hmac_set_priv_key(EVP_PKEY *pkey, const unsigned char *priv,
  47. size_t len)
  48. {
  49. ASN1_OCTET_STRING *os;
  50. if (pkey->pkey.ptr != NULL)
  51. return 0;
  52. os = ASN1_OCTET_STRING_new();
  53. if (os == NULL)
  54. return 0;
  55. if (!ASN1_OCTET_STRING_set(os, priv, len)) {
  56. ASN1_OCTET_STRING_free(os);
  57. return 0;
  58. }
  59. pkey->pkey.ptr = os;
  60. return 1;
  61. }
  62. static int hmac_get_priv_key(const EVP_PKEY *pkey, unsigned char *priv,
  63. size_t *len)
  64. {
  65. ASN1_OCTET_STRING *os = (ASN1_OCTET_STRING *)pkey->pkey.ptr;
  66. if (priv == NULL) {
  67. *len = ASN1_STRING_length(os);
  68. return 1;
  69. }
  70. if (os == NULL || *len < (size_t)ASN1_STRING_length(os))
  71. return 0;
  72. *len = ASN1_STRING_length(os);
  73. memcpy(priv, ASN1_STRING_get0_data(os), *len);
  74. return 1;
  75. }
  76. const EVP_PKEY_ASN1_METHOD hmac_asn1_meth = {
  77. EVP_PKEY_HMAC,
  78. EVP_PKEY_HMAC,
  79. 0,
  80. "HMAC",
  81. "OpenSSL HMAC method",
  82. 0, 0, hmac_pkey_public_cmp, 0,
  83. 0, 0, 0,
  84. hmac_size,
  85. 0, 0,
  86. 0, 0, 0, 0, 0, 0, 0,
  87. hmac_key_free,
  88. hmac_pkey_ctrl,
  89. NULL,
  90. NULL,
  91. NULL,
  92. NULL,
  93. NULL,
  94. NULL,
  95. NULL,
  96. NULL,
  97. hmac_set_priv_key,
  98. NULL,
  99. hmac_get_priv_key,
  100. NULL,
  101. };