2
0

siphash_ameth.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright 2007-2018 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 "internal/asn1_int.h"
  13. #include "internal/siphash.h"
  14. #include "siphash_local.h"
  15. #include "internal/evp_int.h"
  16. /*
  17. * SIPHASH "ASN1" method. This is just here to indicate the maximum
  18. * SIPHASH output length and to free up a SIPHASH key.
  19. */
  20. static int siphash_size(const EVP_PKEY *pkey)
  21. {
  22. return SIPHASH_MAX_DIGEST_SIZE;
  23. }
  24. static void siphash_key_free(EVP_PKEY *pkey)
  25. {
  26. ASN1_OCTET_STRING *os = EVP_PKEY_get0(pkey);
  27. if (os != NULL) {
  28. if (os->data != NULL)
  29. OPENSSL_cleanse(os->data, os->length);
  30. ASN1_OCTET_STRING_free(os);
  31. }
  32. }
  33. static int siphash_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
  34. {
  35. /* nothing (including ASN1_PKEY_CTRL_DEFAULT_MD_NID), is supported */
  36. return -2;
  37. }
  38. static int siphash_pkey_public_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
  39. {
  40. return ASN1_OCTET_STRING_cmp(EVP_PKEY_get0(a), EVP_PKEY_get0(b));
  41. }
  42. static int siphash_set_priv_key(EVP_PKEY *pkey, const unsigned char *priv,
  43. size_t len)
  44. {
  45. ASN1_OCTET_STRING *os;
  46. if (pkey->pkey.ptr != NULL || len != SIPHASH_KEY_SIZE)
  47. return 0;
  48. os = ASN1_OCTET_STRING_new();
  49. if (os == NULL)
  50. return 0;
  51. if (!ASN1_OCTET_STRING_set(os, priv, len)) {
  52. ASN1_OCTET_STRING_free(os);
  53. return 0;
  54. }
  55. pkey->pkey.ptr = os;
  56. return 1;
  57. }
  58. const EVP_PKEY_ASN1_METHOD siphash_asn1_meth = {
  59. EVP_PKEY_SIPHASH,
  60. EVP_PKEY_SIPHASH,
  61. 0,
  62. "SIPHASH",
  63. "OpenSSL SIPHASH method",
  64. 0, 0, siphash_pkey_public_cmp, 0,
  65. 0, 0, 0,
  66. siphash_size,
  67. 0, 0,
  68. 0, 0, 0, 0, 0, 0, 0,
  69. siphash_key_free,
  70. siphash_pkey_ctrl,
  71. NULL,
  72. NULL,
  73. NULL,
  74. NULL,
  75. NULL,
  76. NULL,
  77. NULL,
  78. NULL,
  79. siphash_set_priv_key,
  80. NULL,
  81. };