gnunet-android

GNUnet for Android
Log | Files | Refs | README

mldsa.h (14740B)


      1 // Copyright 2024 The BoringSSL Authors
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //     https://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 #ifndef OPENSSL_HEADER_MLDSA_H_
     16 #define OPENSSL_HEADER_MLDSA_H_
     17 
     18 #include <openssl/base.h>  // IWYU pragma: export
     19 
     20 #if defined(__cplusplus)
     21 extern "C" {
     22 #endif
     23 
     24 
     25 // ML-DSA.
     26 //
     27 // This implements the Module-Lattice-Based Digital Signature Standard from
     28 // https://csrc.nist.gov/pubs/fips/204/final
     29 
     30 
     31 // MLDSA_SEED_BYTES is the number of bytes in an ML-DSA seed value.
     32 #define MLDSA_SEED_BYTES 32
     33 
     34 // MLDSA_MU_BYTES is the number of bytes in an ML-DSA mu value.
     35 #define MLDSA_MU_BYTES 64
     36 
     37 
     38 // ML-DSA-65.
     39 
     40 // MLDSA65_private_key contains an ML-DSA-65 private key. The contents of this
     41 // object should never leave the address space since the format is unstable.
     42 struct MLDSA65_private_key {
     43   union {
     44     uint8_t bytes[32 + 32 + 64 + 256 * 4 * (5 + 6 + 6)];
     45     uint32_t alignment;
     46   } opaque;
     47 };
     48 
     49 // MLDSA65_public_key contains an ML-DSA-65 public key. The contents of this
     50 // object should never leave the address space since the format is unstable.
     51 struct MLDSA65_public_key {
     52   union {
     53     uint8_t bytes[32 + 64 + 256 * 4 * 6];
     54     uint32_t alignment;
     55   } opaque;
     56 };
     57 
     58 // MLDSA65_prehash contains a pre-hash context for ML-DSA-65. The contents of
     59 // this object should never leave the address space since the format is
     60 // unstable.
     61 struct MLDSA65_prehash {
     62   union {
     63     uint8_t bytes[200 + 4 + 4 + 4 * sizeof(size_t)];
     64     uint64_t alignment;
     65   } opaque;
     66 };
     67 
     68 // MLDSA65_PRIVATE_KEY_BYTES is the number of bytes in an encoded ML-DSA-65
     69 // private key.
     70 #define MLDSA65_PRIVATE_KEY_BYTES 4032
     71 
     72 // MLDSA65_PUBLIC_KEY_BYTES is the number of bytes in an encoded ML-DSA-65
     73 // public key.
     74 #define MLDSA65_PUBLIC_KEY_BYTES 1952
     75 
     76 // MLDSA65_SIGNATURE_BYTES is the number of bytes in an encoded ML-DSA-65
     77 // signature.
     78 #define MLDSA65_SIGNATURE_BYTES 3309
     79 
     80 // MLDSA65_generate_key generates a random public/private key pair, writes the
     81 // encoded public key to |out_encoded_public_key|, writes the seed to
     82 // |out_seed|, and sets |out_private_key| to the private key. Returns 1 on
     83 // success and 0 on allocation failure.
     84 OPENSSL_EXPORT int MLDSA65_generate_key(
     85     uint8_t out_encoded_public_key[MLDSA65_PUBLIC_KEY_BYTES],
     86     uint8_t out_seed[MLDSA_SEED_BYTES],
     87     struct MLDSA65_private_key *out_private_key);
     88 
     89 // MLDSA65_private_key_from_seed regenerates a private key from a seed value
     90 // that was generated by |MLDSA65_generate_key|. Returns 1 on success and 0 on
     91 // allocation failure or if |seed_len| is incorrect.
     92 OPENSSL_EXPORT int MLDSA65_private_key_from_seed(
     93     struct MLDSA65_private_key *out_private_key, const uint8_t *seed,
     94     size_t seed_len);
     95 
     96 // MLDSA65_public_from_private sets |*out_public_key| to the public key that
     97 // corresponds to |private_key|. Returns 1 on success and 0 on failure.
     98 OPENSSL_EXPORT int MLDSA65_public_from_private(
     99     struct MLDSA65_public_key *out_public_key,
    100     const struct MLDSA65_private_key *private_key);
    101 
    102 // MLDSA65_sign generates a signature for the message |msg| of length
    103 // |msg_len| using |private_key| (following the randomized algorithm), and
    104 // writes the encoded signature to |out_encoded_signature|. The |context|
    105 // argument is also signed over and can be used to include implicit contextual
    106 // information that isn't included in |msg|. The same value of |context| must be
    107 // presented to |MLDSA65_verify| in order for the generated signature to be
    108 // considered valid. |context| and |context_len| may be |NULL| and 0 to use an
    109 // empty context (this is common). Returns 1 on success and 0 on failure.
    110 OPENSSL_EXPORT int MLDSA65_sign(
    111     uint8_t out_encoded_signature[MLDSA65_SIGNATURE_BYTES],
    112     const struct MLDSA65_private_key *private_key, const uint8_t *msg,
    113     size_t msg_len, const uint8_t *context, size_t context_len);
    114 
    115 // MLDSA65_verify verifies that |signature| constitutes a valid
    116 // signature for the message |msg| of length |msg_len| using |public_key|. The
    117 // value of |context| must equal the value that was passed to |MLDSA65_sign|
    118 // when the signature was generated. Returns 1 on success or 0 on error.
    119 OPENSSL_EXPORT int MLDSA65_verify(const struct MLDSA65_public_key *public_key,
    120                                   const uint8_t *signature,
    121                                   size_t signature_len, const uint8_t *msg,
    122                                   size_t msg_len, const uint8_t *context,
    123                                   size_t context_len);
    124 
    125 // MLDSA65_prehash_init initializes a pre-hashing state using |public_key|. The
    126 // |context| argument can be used to include implicit contextual information
    127 // that isn't included in the message. The same value of |context| must be
    128 // presented to |MLDSA65_verify| in order for the generated signature to be
    129 // considered valid. |context| and |context_len| may be |NULL| and 0 to use an
    130 // empty context (this is common). Returns 1 on success and 0 on failure (if the
    131 // context is too long).
    132 OPENSSL_EXPORT int MLDSA65_prehash_init(
    133     struct MLDSA65_prehash *out_state,
    134     const struct MLDSA65_public_key *public_key, const uint8_t *context,
    135     size_t context_len);
    136 
    137 // MLDSA65_prehash_update incorporates the given |msg| of length |msg_len| into
    138 // the pre-hashing state. This can be called multiple times on successive chunks
    139 // of the message. This should be called after |MLDSA65_prehash_init| and before
    140 // |MLDSA65_prehash_finalize|.
    141 OPENSSL_EXPORT void MLDSA65_prehash_update(struct MLDSA65_prehash *inout_state,
    142                                            const uint8_t *msg, size_t msg_len);
    143 
    144 // MLDSA65_prehash_finalize extracts a pre-hashed message representative from
    145 // the given pre-hashing state. This should be called after
    146 // |MLDSA65_prehash_init| and |MLDSA65_prehash_update|. The resulting
    147 // |out_msg_rep| should then be passed to |MLDSA65_sign_message_representative|
    148 // to obtain a signature.
    149 OPENSSL_EXPORT void MLDSA65_prehash_finalize(
    150     uint8_t out_msg_rep[MLDSA_MU_BYTES], struct MLDSA65_prehash *inout_state);
    151 
    152 // MLDSA65_sign_message_representative generates a signature for the pre-hashed
    153 // message |msg_rep| using |private_key| (following the randomized algorithm),
    154 // and writes the encoded signature to |out_encoded_signature|. The |msg_rep|
    155 // should be obtained via calls to |MLDSA65_prehash_init|,
    156 // |MLDSA65_prehash_update| and |MLDSA65_prehash_finalize| using the public key
    157 // from the same key pair, otherwise the signature will not verify. Returns 1 on
    158 // success and 0 on failure.
    159 OPENSSL_EXPORT int MLDSA65_sign_message_representative(
    160     uint8_t out_encoded_signature[MLDSA65_SIGNATURE_BYTES],
    161     const struct MLDSA65_private_key *private_key,
    162     const uint8_t msg_rep[MLDSA_MU_BYTES]);
    163 
    164 // MLDSA65_marshal_public_key serializes |public_key| to |out| in the standard
    165 // format for ML-DSA-65 public keys. It returns 1 on success or 0 on
    166 // allocation error.
    167 OPENSSL_EXPORT int MLDSA65_marshal_public_key(
    168     CBB *out, const struct MLDSA65_public_key *public_key);
    169 
    170 // MLDSA65_parse_public_key parses a public key, in the format generated by
    171 // |MLDSA65_marshal_public_key|, from |in| and writes the result to
    172 // |out_public_key|. It returns 1 on success or 0 on parse error or if
    173 // there are trailing bytes in |in|.
    174 OPENSSL_EXPORT int MLDSA65_parse_public_key(
    175     struct MLDSA65_public_key *public_key, CBS *in);
    176 
    177 
    178 // ML-DSA-87.
    179 //
    180 // ML-DSA-87 is excessively and unnecessarily large. Use -65 unless you are
    181 // specifically attempting to achieve CNSA 2.0 compliance.
    182 
    183 // MLDSA87_private_key contains an ML-DSA-87 private key. The contents of this
    184 // object should never leave the address space since the format is unstable.
    185 struct MLDSA87_private_key {
    186   union {
    187     uint8_t bytes[32 + 32 + 64 + 256 * 4 * (7 + 8 + 8)];
    188     uint32_t alignment;
    189   } opaque;
    190 };
    191 
    192 // MLDSA87_public_key contains an ML-DSA-87 public key. The contents of this
    193 // object should never leave the address space since the format is unstable.
    194 struct MLDSA87_public_key {
    195   union {
    196     uint8_t bytes[32 + 64 + 256 * 4 * 8];
    197     uint32_t alignment;
    198   } opaque;
    199 };
    200 
    201 // MLDSA87_prehash contains a pre-hash context for ML-DSA-87. The contents of
    202 // this object should never leave the address space since the format is
    203 // unstable.
    204 struct MLDSA87_prehash {
    205   union {
    206     uint8_t bytes[200 + 4 + 4 + 4 * sizeof(size_t)];
    207     uint64_t alignment;
    208   } opaque;
    209 };
    210 
    211 // MLDSA87_PRIVATE_KEY_BYTES is the number of bytes in an encoded ML-DSA-87
    212 // private key.
    213 #define MLDSA87_PRIVATE_KEY_BYTES 4896
    214 
    215 // MLDSA87_PUBLIC_KEY_BYTES is the number of bytes in an encoded ML-DSA-87
    216 // public key.
    217 #define MLDSA87_PUBLIC_KEY_BYTES 2592
    218 
    219 // MLDSA87_SIGNATURE_BYTES is the number of bytes in an encoded ML-DSA-87
    220 // signature.
    221 #define MLDSA87_SIGNATURE_BYTES 4627
    222 
    223 // MLDSA87_generate_key generates a random public/private key pair, writes the
    224 // encoded public key to |out_encoded_public_key|, writes the seed to
    225 // |out_seed|, and sets |out_private_key| to the private key. Returns 1 on
    226 // success and 0 on allocation failure.
    227 OPENSSL_EXPORT int MLDSA87_generate_key(
    228     uint8_t out_encoded_public_key[MLDSA87_PUBLIC_KEY_BYTES],
    229     uint8_t out_seed[MLDSA_SEED_BYTES],
    230     struct MLDSA87_private_key *out_private_key);
    231 
    232 // MLDSA87_private_key_from_seed regenerates a private key from a seed value
    233 // that was generated by |MLDSA87_generate_key|. Returns 1 on success and 0 on
    234 // allocation failure or if |seed_len| is incorrect.
    235 OPENSSL_EXPORT int MLDSA87_private_key_from_seed(
    236     struct MLDSA87_private_key *out_private_key, const uint8_t *seed,
    237     size_t seed_len);
    238 
    239 // MLDSA87_public_from_private sets |*out_public_key| to the public key that
    240 // corresponds to |private_key|. Returns 1 on success and 0 on failure.
    241 OPENSSL_EXPORT int MLDSA87_public_from_private(
    242     struct MLDSA87_public_key *out_public_key,
    243     const struct MLDSA87_private_key *private_key);
    244 
    245 // MLDSA87_sign generates a signature for the message |msg| of length
    246 // |msg_len| using |private_key| (following the randomized algorithm), and
    247 // writes the encoded signature to |out_encoded_signature|. The |context|
    248 // argument is also signed over and can be used to include implicit contextual
    249 // information that isn't included in |msg|. The same value of |context| must be
    250 // presented to |MLDSA87_verify| in order for the generated signature to be
    251 // considered valid. |context| and |context_len| may be |NULL| and 0 to use an
    252 // empty context (this is common). Returns 1 on success and 0 on failure.
    253 OPENSSL_EXPORT int MLDSA87_sign(
    254     uint8_t out_encoded_signature[MLDSA87_SIGNATURE_BYTES],
    255     const struct MLDSA87_private_key *private_key, const uint8_t *msg,
    256     size_t msg_len, const uint8_t *context, size_t context_len);
    257 
    258 // MLDSA87_verify verifies that |signature| constitutes a valid
    259 // signature for the message |msg| of length |msg_len| using |public_key|. The
    260 // value of |context| must equal the value that was passed to |MLDSA87_sign|
    261 // when the signature was generated. Returns 1 on success or 0 on error.
    262 OPENSSL_EXPORT int MLDSA87_verify(const struct MLDSA87_public_key *public_key,
    263                                   const uint8_t *signature,
    264                                   size_t signature_len, const uint8_t *msg,
    265                                   size_t msg_len, const uint8_t *context,
    266                                   size_t context_len);
    267 
    268 // MLDSA87_prehash_init initializes a pre-hashing state using |public_key|. The
    269 // |context| argument can be used to include implicit contextual information
    270 // that isn't included in the message. The same value of |context| must be
    271 // presented to |MLDSA87_verify| in order for the generated signature to be
    272 // considered valid. |context| and |context_len| may be |NULL| and 0 to use an
    273 // empty context (this is common). Returns 1 on success and 0 on failure (if the
    274 // context is too long).
    275 OPENSSL_EXPORT int MLDSA87_prehash_init(
    276     struct MLDSA87_prehash *out_state,
    277     const struct MLDSA87_public_key *public_key, const uint8_t *context,
    278     size_t context_len);
    279 
    280 // MLDSA87_prehash_update incorporates the given |msg| of length |msg_len| into
    281 // the pre-hashing state. This can be called multiple times on successive chunks
    282 // of the message. This should be called after |MLDSA87_prehash_init| and before
    283 // |MLDSA87_prehash_finalize|.
    284 OPENSSL_EXPORT void MLDSA87_prehash_update(struct MLDSA87_prehash *inout_state,
    285                                            const uint8_t *msg, size_t msg_len);
    286 
    287 // MLDSA87_prehash_finalize extracts a pre-hashed message representative from
    288 // the given pre-hashing state. This should be called after
    289 // |MLDSA87_prehash_init| and |MLDSA87_prehash_update|. The resulting
    290 // |out_msg_rep| should then be passed to |MLDSA87_sign_message_representative|
    291 // to obtain a signature.
    292 OPENSSL_EXPORT void MLDSA87_prehash_finalize(
    293     uint8_t out_msg_rep[MLDSA_MU_BYTES], struct MLDSA87_prehash *inout_state);
    294 
    295 // MLDSA87_sign_message_representative generates a signature for the pre-hashed
    296 // message |msg_rep| using |private_key| (following the randomized algorithm),
    297 // and writes the encoded signature to |out_encoded_signature|. The |msg_rep|
    298 // should be obtained via calls to |MLDSA87_prehash_init|,
    299 // |MLDSA87_prehash_update| and |MLDSA87_prehash_finalize| using the public key
    300 // from the same key pair, otherwise the signature will not verify. Returns 1 on
    301 // success and 0 on failure.
    302 OPENSSL_EXPORT int MLDSA87_sign_message_representative(
    303     uint8_t out_encoded_signature[MLDSA87_SIGNATURE_BYTES],
    304     const struct MLDSA87_private_key *private_key,
    305     const uint8_t msg_rep[MLDSA_MU_BYTES]);
    306 
    307 // MLDSA87_marshal_public_key serializes |public_key| to |out| in the standard
    308 // format for ML-DSA-87 public keys. It returns 1 on success or 0 on
    309 // allocation error.
    310 OPENSSL_EXPORT int MLDSA87_marshal_public_key(
    311     CBB *out, const struct MLDSA87_public_key *public_key);
    312 
    313 // MLDSA87_parse_public_key parses a public key, in the format generated by
    314 // |MLDSA87_marshal_public_key|, from |in| and writes the result to
    315 // |out_public_key|. It returns 1 on success or 0 on parse error or if
    316 // there are trailing bytes in |in|.
    317 OPENSSL_EXPORT int MLDSA87_parse_public_key(
    318     struct MLDSA87_public_key *public_key, CBS *in);
    319 
    320 
    321 #if defined(__cplusplus)
    322 }  // extern C
    323 #endif
    324 
    325 #endif  // OPENSSL_HEADER_MLDSA_H_