siphash_ameth.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright 2007-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/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. static int siphash_get_priv_key(const EVP_PKEY *pkey, unsigned char *priv,
  59. size_t *len)
  60. {
  61. ASN1_OCTET_STRING *os = (ASN1_OCTET_STRING *)pkey->pkey.ptr;
  62. if (priv == NULL) {
  63. *len = SIPHASH_KEY_SIZE;
  64. return 1;
  65. }
  66. if (os == NULL || *len < SIPHASH_KEY_SIZE)
  67. return 0;
  68. memcpy(priv, ASN1_STRING_get0_data(os), ASN1_STRING_length(os));
  69. *len = SIPHASH_KEY_SIZE;
  70. return 1;
  71. }
  72. const EVP_PKEY_ASN1_METHOD siphash_asn1_meth = {
  73. EVP_PKEY_SIPHASH,
  74. EVP_PKEY_SIPHASH,
  75. 0,
  76. "SIPHASH",
  77. "OpenSSL SIPHASH method",
  78. 0, 0, siphash_pkey_public_cmp, 0,
  79. 0, 0, 0,
  80. siphash_size,
  81. 0, 0,
  82. 0, 0, 0, 0, 0, 0, 0,
  83. siphash_key_free,
  84. siphash_pkey_ctrl,
  85. NULL,
  86. NULL,
  87. NULL,
  88. NULL,
  89. NULL,
  90. NULL,
  91. NULL,
  92. NULL,
  93. siphash_set_priv_key,
  94. NULL,
  95. siphash_get_priv_key,
  96. NULL,
  97. };