您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

73 行
2.2 KiB

  1. /* Copyright (c) 2017, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <openssl/evp.h>
  15. #include <openssl/curve25519.h>
  16. #include <openssl/err.h>
  17. #include "internal.h"
  18. // Ed25519 has no parameters to copy.
  19. static int pkey_ed25519_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) { return 1; }
  20. static int pkey_ed25519_sign_message(EVP_PKEY_CTX *ctx, uint8_t *sig,
  21. size_t *siglen, const uint8_t *tbs,
  22. size_t tbslen) {
  23. ED25519_KEY *key = ctx->pkey->pkey.ptr;
  24. if (!key->has_private) {
  25. OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY);
  26. return 0;
  27. }
  28. *siglen = 64;
  29. if (sig == NULL) {
  30. return 1;
  31. }
  32. return ED25519_sign(sig, tbs, tbslen, key->key.priv);
  33. }
  34. static int pkey_ed25519_verify_message(EVP_PKEY_CTX *ctx, const uint8_t *sig,
  35. size_t siglen, const uint8_t *tbs,
  36. size_t tbslen) {
  37. ED25519_KEY *key = ctx->pkey->pkey.ptr;
  38. if (siglen != 64 ||
  39. !ED25519_verify(tbs, tbslen, sig, key->key.pub.value)) {
  40. OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_SIGNATURE);
  41. return 0;
  42. }
  43. return 1;
  44. }
  45. const EVP_PKEY_METHOD ed25519_pkey_meth = {
  46. EVP_PKEY_ED25519,
  47. NULL /* init */,
  48. pkey_ed25519_copy,
  49. NULL /* cleanup */,
  50. NULL /* keygen */,
  51. NULL /* sign */,
  52. pkey_ed25519_sign_message,
  53. NULL /* verify */,
  54. pkey_ed25519_verify_message,
  55. NULL /* verify_recover */,
  56. NULL /* encrypt */,
  57. NULL /* decrypt */,
  58. NULL /* derive */,
  59. NULL /* paramgen */,
  60. NULL /* ctrl */,
  61. };