poly1305_ameth.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/poly1305.h"
  14. #include "poly1305_local.h"
  15. #include "crypto/evp.h"
  16. /*
  17. * POLY1305 "ASN1" method. This is just here to indicate the maximum
  18. * POLY1305 output length and to free up a POLY1305 key.
  19. */
  20. static int poly1305_size(const EVP_PKEY *pkey)
  21. {
  22. return POLY1305_DIGEST_SIZE;
  23. }
  24. static void poly1305_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 poly1305_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 poly1305_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)) == 0;
  41. }
  42. static int poly1305_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 != POLY1305_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 poly1305_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 = POLY1305_KEY_SIZE;
  64. return 1;
  65. }
  66. if (os == NULL || *len < POLY1305_KEY_SIZE)
  67. return 0;
  68. memcpy(priv, ASN1_STRING_get0_data(os), ASN1_STRING_length(os));
  69. *len = POLY1305_KEY_SIZE;
  70. return 1;
  71. }
  72. const EVP_PKEY_ASN1_METHOD poly1305_asn1_meth = {
  73. EVP_PKEY_POLY1305,
  74. EVP_PKEY_POLY1305,
  75. 0,
  76. "POLY1305",
  77. "OpenSSL POLY1305 method",
  78. 0, 0, poly1305_pkey_public_cmp, 0,
  79. 0, 0, 0,
  80. poly1305_size,
  81. 0, 0,
  82. 0, 0, 0, 0, 0, 0, 0,
  83. poly1305_key_free,
  84. poly1305_pkey_ctrl,
  85. NULL,
  86. NULL,
  87. NULL,
  88. NULL,
  89. NULL,
  90. NULL,
  91. NULL,
  92. NULL,
  93. poly1305_set_priv_key,
  94. NULL,
  95. poly1305_get_priv_key,
  96. NULL,
  97. };