ecx.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright 2020-2023 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. /* Internal EC functions for other submodules: not for application use */
  10. #ifndef OSSL_CRYPTO_ECX_H
  11. # define OSSL_CRYPTO_ECX_H
  12. # pragma once
  13. # include <openssl/opensslconf.h>
  14. # ifndef OPENSSL_NO_ECX
  15. # include <openssl/core.h>
  16. # include <openssl/e_os2.h>
  17. # include <openssl/crypto.h>
  18. # include "internal/refcount.h"
  19. # include "crypto/types.h"
  20. # define X25519_KEYLEN 32
  21. # define X448_KEYLEN 56
  22. # define ED25519_KEYLEN 32
  23. # define ED448_KEYLEN 57
  24. # define MAX_KEYLEN ED448_KEYLEN
  25. # define X25519_BITS 253
  26. # define X25519_SECURITY_BITS 128
  27. # define X448_BITS 448
  28. # define X448_SECURITY_BITS 224
  29. # define ED25519_BITS 256
  30. /* RFC8032 Section 8.5 */
  31. # define ED25519_SECURITY_BITS 128
  32. # define ED25519_SIGSIZE 64
  33. # define ED448_BITS 456
  34. /* RFC8032 Section 8.5 */
  35. # define ED448_SECURITY_BITS 224
  36. # define ED448_SIGSIZE 114
  37. typedef enum {
  38. ECX_KEY_TYPE_X25519,
  39. ECX_KEY_TYPE_X448,
  40. ECX_KEY_TYPE_ED25519,
  41. ECX_KEY_TYPE_ED448
  42. } ECX_KEY_TYPE;
  43. #define KEYTYPE2NID(type) \
  44. ((type) == ECX_KEY_TYPE_X25519 \
  45. ? EVP_PKEY_X25519 \
  46. : ((type) == ECX_KEY_TYPE_X448 \
  47. ? EVP_PKEY_X448 \
  48. : ((type) == ECX_KEY_TYPE_ED25519 \
  49. ? EVP_PKEY_ED25519 \
  50. : EVP_PKEY_ED448)))
  51. struct ecx_key_st {
  52. OSSL_LIB_CTX *libctx;
  53. char *propq;
  54. unsigned int haspubkey:1;
  55. unsigned char pubkey[MAX_KEYLEN];
  56. unsigned char *privkey;
  57. size_t keylen;
  58. ECX_KEY_TYPE type;
  59. CRYPTO_REF_COUNT references;
  60. };
  61. size_t ossl_ecx_key_length(ECX_KEY_TYPE type);
  62. ECX_KEY *ossl_ecx_key_new(OSSL_LIB_CTX *libctx, ECX_KEY_TYPE type,
  63. int haspubkey, const char *propq);
  64. void ossl_ecx_key_set0_libctx(ECX_KEY *key, OSSL_LIB_CTX *libctx);
  65. unsigned char *ossl_ecx_key_allocate_privkey(ECX_KEY *key);
  66. void ossl_ecx_key_free(ECX_KEY *key);
  67. int ossl_ecx_key_up_ref(ECX_KEY *key);
  68. ECX_KEY *ossl_ecx_key_dup(const ECX_KEY *key, int selection);
  69. int ossl_ecx_compute_key(ECX_KEY *peer, ECX_KEY *priv, size_t keylen,
  70. unsigned char *secret, size_t *secretlen,
  71. size_t outlen);
  72. int ossl_x25519(uint8_t out_shared_key[32], const uint8_t private_key[32],
  73. const uint8_t peer_public_value[32]);
  74. void ossl_x25519_public_from_private(uint8_t out_public_value[32],
  75. const uint8_t private_key[32]);
  76. int
  77. ossl_ed25519_public_from_private(OSSL_LIB_CTX *ctx, uint8_t out_public_key[32],
  78. const uint8_t private_key[32],
  79. const char *propq);
  80. int
  81. ossl_ed25519_sign(uint8_t *out_sig, const uint8_t *tbs, size_t tbs_len,
  82. const uint8_t public_key[32], const uint8_t private_key[32],
  83. const uint8_t dom2flag, const uint8_t phflag, const uint8_t csflag,
  84. const uint8_t *context, size_t context_len,
  85. OSSL_LIB_CTX *libctx, const char *propq);
  86. int
  87. ossl_ed25519_verify(const uint8_t *tbs, size_t tbs_len,
  88. const uint8_t signature[64], const uint8_t public_key[32],
  89. const uint8_t dom2flag, const uint8_t phflag, const uint8_t csflag,
  90. const uint8_t *context, size_t context_len,
  91. OSSL_LIB_CTX *libctx, const char *propq);
  92. int
  93. ossl_ed448_public_from_private(OSSL_LIB_CTX *ctx, uint8_t out_public_key[57],
  94. const uint8_t private_key[57], const char *propq);
  95. int
  96. ossl_ed448_sign(OSSL_LIB_CTX *ctx, uint8_t *out_sig,
  97. const uint8_t *message, size_t message_len,
  98. const uint8_t public_key[57], const uint8_t private_key[57],
  99. const uint8_t *context, size_t context_len,
  100. const uint8_t phflag, const char *propq);
  101. int
  102. ossl_ed448_verify(OSSL_LIB_CTX *ctx,
  103. const uint8_t *message, size_t message_len,
  104. const uint8_t signature[114], const uint8_t public_key[57],
  105. const uint8_t *context, size_t context_len,
  106. const uint8_t phflag, const char *propq);
  107. int
  108. ossl_x448(uint8_t out_shared_key[56], const uint8_t private_key[56],
  109. const uint8_t peer_public_value[56]);
  110. void
  111. ossl_x448_public_from_private(uint8_t out_public_value[56],
  112. const uint8_t private_key[56]);
  113. /* Backend support */
  114. typedef enum {
  115. KEY_OP_PUBLIC,
  116. KEY_OP_PRIVATE,
  117. KEY_OP_KEYGEN
  118. } ecx_key_op_t;
  119. ECX_KEY *ossl_ecx_key_op(const X509_ALGOR *palg,
  120. const unsigned char *p, int plen,
  121. int pkey_id, ecx_key_op_t op,
  122. OSSL_LIB_CTX *libctx, const char *propq);
  123. int ossl_ecx_public_from_private(ECX_KEY *key);
  124. int ossl_ecx_key_fromdata(ECX_KEY *ecx, const OSSL_PARAM params[],
  125. int include_private);
  126. ECX_KEY *ossl_ecx_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
  127. OSSL_LIB_CTX *libctx, const char *propq);
  128. ECX_KEY *ossl_evp_pkey_get1_X25519(EVP_PKEY *pkey);
  129. ECX_KEY *ossl_evp_pkey_get1_X448(EVP_PKEY *pkey);
  130. ECX_KEY *ossl_evp_pkey_get1_ED25519(EVP_PKEY *pkey);
  131. ECX_KEY *ossl_evp_pkey_get1_ED448(EVP_PKEY *pkey);
  132. # endif /* OPENSSL_NO_ECX */
  133. #endif