eddsa.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright 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. #include <openssl/crypto.h>
  10. #include <openssl/core_dispatch.h>
  11. #include <openssl/core_names.h>
  12. #include <openssl/err.h>
  13. #include <openssl/params.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/err.h>
  16. #include "internal/nelem.h"
  17. #include "internal/sizes.h"
  18. #include "prov/providercommonerr.h"
  19. #include "prov/implementations.h"
  20. #include "prov/providercommonerr.h"
  21. #include "prov/provider_ctx.h"
  22. #include "crypto/ecx.h"
  23. static OSSL_FUNC_signature_newctx_fn eddsa_newctx;
  24. static OSSL_FUNC_signature_digest_sign_init_fn eddsa_digest_signverify_init;
  25. static OSSL_FUNC_signature_digest_sign_fn ed25519_digest_sign;
  26. static OSSL_FUNC_signature_digest_sign_fn ed448_digest_sign;
  27. static OSSL_FUNC_signature_digest_verify_fn ed25519_digest_verify;
  28. static OSSL_FUNC_signature_digest_verify_fn ed448_digest_verify;
  29. static OSSL_FUNC_signature_freectx_fn eddsa_freectx;
  30. static OSSL_FUNC_signature_dupctx_fn eddsa_dupctx;
  31. typedef struct {
  32. OPENSSL_CTX *libctx;
  33. ECX_KEY *key;
  34. } PROV_EDDSA_CTX;
  35. static void *eddsa_newctx(void *provctx, const char *propq_unused)
  36. {
  37. PROV_EDDSA_CTX *peddsactx = OPENSSL_zalloc(sizeof(PROV_EDDSA_CTX));
  38. if (peddsactx == NULL) {
  39. PROVerr(0, ERR_R_MALLOC_FAILURE);
  40. return NULL;
  41. }
  42. peddsactx->libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
  43. return peddsactx;
  44. }
  45. static int eddsa_digest_signverify_init(void *vpeddsactx, const char *mdname,
  46. void *vedkey)
  47. {
  48. PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
  49. ECX_KEY *edkey = (ECX_KEY *)vedkey;
  50. if (mdname != NULL && mdname[0] != '\0') {
  51. PROVerr(0, PROV_R_INVALID_DIGEST);
  52. return 0;
  53. }
  54. if (!ecx_key_up_ref(edkey)) {
  55. PROVerr(0, ERR_R_INTERNAL_ERROR);
  56. return 0;
  57. }
  58. peddsactx->key = edkey;
  59. return 1;
  60. }
  61. int ed25519_digest_sign(void *vpeddsactx, unsigned char *sigret,
  62. size_t *siglen, size_t sigsize,
  63. const unsigned char *tbs, size_t tbslen)
  64. {
  65. PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
  66. const ECX_KEY *edkey = peddsactx->key;
  67. if (sigret == NULL) {
  68. *siglen = ED25519_SIGSIZE;
  69. return 1;
  70. }
  71. if (sigsize < ED25519_SIGSIZE) {
  72. PROVerr(0, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  73. return 0;
  74. }
  75. if (ED25519_sign(sigret, tbs, tbslen, edkey->pubkey, edkey->privkey,
  76. peddsactx->libctx, NULL) == 0) {
  77. PROVerr(0, PROV_R_FAILED_TO_SIGN);
  78. return 0;
  79. }
  80. *siglen = ED25519_SIGSIZE;
  81. return 1;
  82. }
  83. int ed448_digest_sign(void *vpeddsactx, unsigned char *sigret,
  84. size_t *siglen, size_t sigsize,
  85. const unsigned char *tbs, size_t tbslen)
  86. {
  87. PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
  88. const ECX_KEY *edkey = peddsactx->key;
  89. if (sigret == NULL) {
  90. *siglen = ED448_SIGSIZE;
  91. return 1;
  92. }
  93. if (sigsize < ED448_SIGSIZE) {
  94. PROVerr(0, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  95. return 0;
  96. }
  97. if (ED448_sign(peddsactx->libctx, sigret, tbs, tbslen, edkey->pubkey,
  98. edkey->privkey, NULL, 0) == 0) {
  99. PROVerr(0, PROV_R_FAILED_TO_SIGN);
  100. return 0;
  101. }
  102. *siglen = ED448_SIGSIZE;
  103. return 1;
  104. }
  105. int ed25519_digest_verify(void *vpeddsactx, const unsigned char *sig,
  106. size_t siglen, const unsigned char *tbs,
  107. size_t tbslen)
  108. {
  109. PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
  110. const ECX_KEY *edkey = peddsactx->key;
  111. if (siglen != ED25519_SIGSIZE)
  112. return 0;
  113. return ED25519_verify(tbs, tbslen, sig, edkey->pubkey, peddsactx->libctx,
  114. NULL);
  115. }
  116. int ed448_digest_verify(void *vpeddsactx, const unsigned char *sig,
  117. size_t siglen, const unsigned char *tbs,
  118. size_t tbslen)
  119. {
  120. PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
  121. const ECX_KEY *edkey = peddsactx->key;
  122. if (siglen != ED448_SIGSIZE)
  123. return 0;
  124. return ED448_verify(peddsactx->libctx, tbs, tbslen, sig, edkey->pubkey,
  125. NULL, 0);
  126. }
  127. static void eddsa_freectx(void *vpeddsactx)
  128. {
  129. PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
  130. ecx_key_free(peddsactx->key);
  131. OPENSSL_free(peddsactx);
  132. }
  133. static void *eddsa_dupctx(void *vpeddsactx)
  134. {
  135. PROV_EDDSA_CTX *srcctx = (PROV_EDDSA_CTX *)vpeddsactx;
  136. PROV_EDDSA_CTX *dstctx;
  137. dstctx = OPENSSL_zalloc(sizeof(*srcctx));
  138. if (dstctx == NULL)
  139. return NULL;
  140. *dstctx = *srcctx;
  141. dstctx->key = NULL;
  142. if (srcctx->key != NULL && !ecx_key_up_ref(srcctx->key)) {
  143. PROVerr(0, ERR_R_INTERNAL_ERROR);
  144. goto err;
  145. }
  146. dstctx->key = srcctx->key;
  147. return dstctx;
  148. err:
  149. eddsa_freectx(dstctx);
  150. return NULL;
  151. }
  152. const OSSL_DISPATCH ed25519_signature_functions[] = {
  153. { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))eddsa_newctx },
  154. { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
  155. (void (*)(void))eddsa_digest_signverify_init },
  156. { OSSL_FUNC_SIGNATURE_DIGEST_SIGN,
  157. (void (*)(void))ed25519_digest_sign },
  158. { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
  159. (void (*)(void))eddsa_digest_signverify_init },
  160. { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY,
  161. (void (*)(void))ed25519_digest_verify },
  162. { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))eddsa_freectx },
  163. { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))eddsa_dupctx },
  164. { 0, NULL }
  165. };
  166. const OSSL_DISPATCH ed448_signature_functions[] = {
  167. { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))eddsa_newctx },
  168. { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
  169. (void (*)(void))eddsa_digest_signverify_init },
  170. { OSSL_FUNC_SIGNATURE_DIGEST_SIGN,
  171. (void (*)(void))ed448_digest_sign },
  172. { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
  173. (void (*)(void))eddsa_digest_signverify_init },
  174. { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY,
  175. (void (*)(void))ed448_digest_verify },
  176. { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))eddsa_freectx },
  177. { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))eddsa_dupctx },
  178. { 0, NULL }
  179. };