ecx.h 5.0 KB

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