hm_ameth.c 2.6 KB

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