ecx.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 "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. CRYPTO_RWLOCK *lock;
  61. };
  62. size_t ossl_ecx_key_length(ECX_KEY_TYPE type);
  63. ECX_KEY *ossl_ecx_key_new(OSSL_LIB_CTX *libctx, ECX_KEY_TYPE type,
  64. int haspubkey, const char *propq);
  65. void ossl_ecx_key_set0_libctx(ECX_KEY *key, OSSL_LIB_CTX *libctx);
  66. unsigned char *ossl_ecx_key_allocate_privkey(ECX_KEY *key);
  67. void ossl_ecx_key_free(ECX_KEY *key);
  68. int ossl_ecx_key_up_ref(ECX_KEY *key);
  69. ECX_KEY *ossl_ecx_key_dup(const ECX_KEY *key, int selection);
  70. int ossl_ecx_compute_key(ECX_KEY *peer, ECX_KEY *priv, size_t keylen,
  71. unsigned char *secret, size_t *secretlen,
  72. size_t outlen);
  73. int ossl_x25519(uint8_t out_shared_key[32], const uint8_t private_key[32],
  74. const uint8_t peer_public_value[32]);
  75. void ossl_x25519_public_from_private(uint8_t out_public_value[32],
  76. const uint8_t private_key[32]);
  77. int
  78. ossl_ed25519_public_from_private(OSSL_LIB_CTX *ctx, uint8_t out_public_key[32],
  79. const uint8_t private_key[32],
  80. const char *propq);
  81. int
  82. ossl_ed25519_sign(uint8_t *out_sig, const uint8_t *message, size_t message_len,
  83. const uint8_t public_key[32], const uint8_t private_key[32],
  84. OSSL_LIB_CTX *libctx, const char *propq);
  85. int
  86. ossl_ed25519_verify(const uint8_t *message, size_t message_len,
  87. const uint8_t signature[64], const uint8_t public_key[32],
  88. OSSL_LIB_CTX *libctx, const char *propq);
  89. int
  90. ossl_ed448_public_from_private(OSSL_LIB_CTX *ctx, uint8_t out_public_key[57],
  91. const uint8_t private_key[57], const char *propq);
  92. int
  93. ossl_ed448_sign(OSSL_LIB_CTX *ctx, uint8_t *out_sig, const uint8_t *message,
  94. size_t message_len, const uint8_t public_key[57],
  95. const uint8_t private_key[57], const uint8_t *context,
  96. size_t context_len, const char *propq);
  97. int
  98. ossl_ed448_verify(OSSL_LIB_CTX *ctx, const uint8_t *message, size_t message_len,
  99. const uint8_t signature[114], const uint8_t public_key[57],
  100. const uint8_t *context, size_t context_len, const char *propq);
  101. int
  102. ossl_x448(uint8_t out_shared_key[56], const uint8_t private_key[56],
  103. const uint8_t peer_public_value[56]);
  104. void
  105. ossl_x448_public_from_private(uint8_t out_public_value[56],
  106. const uint8_t private_key[56]);
  107. /* Backend support */
  108. typedef enum {
  109. KEY_OP_PUBLIC,
  110. KEY_OP_PRIVATE,
  111. KEY_OP_KEYGEN
  112. } ecx_key_op_t;
  113. ECX_KEY *ossl_ecx_key_op(const X509_ALGOR *palg,
  114. const unsigned char *p, int plen,
  115. int pkey_id, ecx_key_op_t op,
  116. OSSL_LIB_CTX *libctx, const char *propq);
  117. int ossl_ecx_public_from_private(ECX_KEY *key);
  118. int ossl_ecx_key_fromdata(ECX_KEY *ecx, const OSSL_PARAM params[],
  119. int include_private);
  120. ECX_KEY *ossl_ecx_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
  121. OSSL_LIB_CTX *libctx, const char *propq);
  122. ECX_KEY *ossl_evp_pkey_get1_X25519(EVP_PKEY *pkey);
  123. ECX_KEY *ossl_evp_pkey_get1_X448(EVP_PKEY *pkey);
  124. ECX_KEY *ossl_evp_pkey_get1_ED25519(EVP_PKEY *pkey);
  125. ECX_KEY *ossl_evp_pkey_get1_ED448(EVP_PKEY *pkey);
  126. # endif /* OPENSSL_NO_EC */
  127. #endif