slh_dsa_local.h 4.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright 2024-2025 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 "crypto/slh_dsa.h"
  10. #include "slh_hash.h"
  11. #include "slh_params.h"
  12. /*
  13. * Maximum size of the security parameter |n| in FIPS 205 Section 11. Table 2.
  14. * This indicates the length in bytes of a message that can be signed.
  15. * It is the size used by WOTS+ public and private key elements as well as
  16. * signature elements.
  17. */
  18. #define SLH_MAX_N 32
  19. /*
  20. * For the given standard w=16 for all parameter sets.
  21. * A n byte message is converted into 2 * n base 16 Integers followed
  22. * by 3 Integers for the checksum of these values.
  23. */
  24. #define SLH_WOTS_LEN(n) (2 * (n) + 3)
  25. /*
  26. * FIPS 205 SLH-DSA algorithms have many different parameters which includes
  27. * the following constants that are stored into a |key|:
  28. * - A set of constants (Section 11. contains 12 parameter sets)
  29. * such as tree heights and security parameters associated with a algorithm
  30. * name such as SLH-DSA-SHA2-128s.
  31. * - ADRS functions (such as set_layer_address() in Section 4.3 & 11.2)
  32. * - Hash Functions (such as H_MSG() & PRF()) See Sections 11.1, 11.2.1 & 11.2.2.
  33. * - prefetched EVP_MD objects used for hashing.
  34. *
  35. * When performing operations multiple Hash related objects are also needed
  36. * such as EVP_MD_CTX and EVP_MAC_CTX (these are independent of the |key|)
  37. *
  38. * SLH_DSA_HASH_CTX is a container to hold all of these objects. This object is
  39. * resolved early and is then passed to most SLH_DSA related functions, since
  40. * there are many nested layers of calls that require these values.
  41. *
  42. * NOTE: Any changes to this structure will need updating in
  43. * ossl_slh_dsa_hash_ctx_dup().
  44. */
  45. struct slh_dsa_hash_ctx_st {
  46. const SLH_DSA_KEY *key; /* This key is not owned by this object */
  47. EVP_MD_CTX *md_ctx; /* Either SHAKE OR SHA-256 */
  48. EVP_MD_CTX *md_big_ctx; /* Either SHA-512 or points to |md_ctx| for SHA-256*/
  49. EVP_MAC_CTX *hmac_ctx; /* required by SHA algorithms for PRFmsg() */
  50. int hmac_digest_used; /* Used for lazy init of hmac_ctx digest */
  51. };
  52. __owur int ossl_slh_wots_pk_gen(SLH_DSA_HASH_CTX *ctx, const uint8_t *sk_seed,
  53. const uint8_t *pk_seed, uint8_t *adrs,
  54. uint8_t *pk_out, size_t pk_out_len);
  55. __owur int ossl_slh_wots_sign(SLH_DSA_HASH_CTX *ctx, const uint8_t *msg,
  56. const uint8_t *sk_seed, const uint8_t *pk_seed,
  57. uint8_t *adrs, WPACKET *sig_wpkt);
  58. __owur int ossl_slh_wots_pk_from_sig(SLH_DSA_HASH_CTX *ctx,
  59. PACKET *sig_rpkt, const uint8_t *msg,
  60. const uint8_t *pk_seed, uint8_t *adrs,
  61. uint8_t *pk_out, size_t pk_out_len);
  62. __owur int ossl_slh_xmss_node(SLH_DSA_HASH_CTX *ctx, const uint8_t *sk_seed,
  63. uint32_t node_id, uint32_t height,
  64. const uint8_t *pk_seed, uint8_t *adrs,
  65. uint8_t *pk_out, size_t pk_out_len);
  66. __owur int ossl_slh_xmss_sign(SLH_DSA_HASH_CTX *ctx, const uint8_t *msg,
  67. const uint8_t *sk_seed, uint32_t node_id,
  68. const uint8_t *pk_seed, uint8_t *adrs,
  69. WPACKET *sig_wpkt);
  70. __owur int ossl_slh_xmss_pk_from_sig(SLH_DSA_HASH_CTX *ctx, uint32_t node_id,
  71. PACKET *sig_rpkt, const uint8_t *msg,
  72. const uint8_t *pk_seed, uint8_t *adrs,
  73. uint8_t *pk_out, size_t pk_out_len);
  74. __owur int ossl_slh_ht_sign(SLH_DSA_HASH_CTX *ctx, const uint8_t *msg,
  75. const uint8_t *sk_seed, const uint8_t *pk_seed,
  76. uint64_t tree_id, uint32_t leaf_id,
  77. WPACKET *sig_wpkt);
  78. __owur int ossl_slh_ht_verify(SLH_DSA_HASH_CTX *ctx, const uint8_t *msg,
  79. PACKET *sig_rpkt, const uint8_t *pk_seed,
  80. uint64_t tree_id, uint32_t leaf_id,
  81. const uint8_t *pk_root);
  82. __owur int ossl_slh_fors_sign(SLH_DSA_HASH_CTX *ctx, const uint8_t *md,
  83. const uint8_t *sk_seed, const uint8_t *pk_seed,
  84. uint8_t *adrs, WPACKET *sig_wpkt);
  85. __owur int ossl_slh_fors_pk_from_sig(SLH_DSA_HASH_CTX *ctx, PACKET *sig_rpkt,
  86. const uint8_t *md, const uint8_t *pk_seed,
  87. uint8_t *adrs,
  88. uint8_t *pk_out, size_t pk_out_len);