| 1 |
/* pubkey-dilithium.c - the Dilithium for libgcrypt |
| 2 |
* Copyright (C) 2025 g10 Code GmbH |
| 3 |
* |
| 4 |
* This file was modified for use by Libgcrypt. |
| 5 |
* |
| 6 |
* This file is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU Lesser General Public License as |
| 8 |
* published by the Free Software Foundation; either version 2.1 of |
| 9 |
* the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This file is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this program; if not, see <https://www.gnu.org/licenses/>. |
| 18 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <config.h> |
| 23 |
|
| 24 |
#include "g10lib.h" |
| 25 |
#include "mpi.h" |
| 26 |
#include "cipher.h" |
| 27 |
#include "pubkey-internal.h" |
| 28 |
#include "dilithium.h" |
| 29 |
|
| 30 |
static const char *mldsa_names[] = |
| 31 |
{ |
| 32 |
"dilithium2", |
| 33 |
"dilithium3", |
| 34 |
"dilithium5", |
| 35 |
NULL, |
| 36 |
}; |
| 37 |
|
| 38 |
#define MAX_PUBKEY_LEN 2592 |
| 39 |
#define MAX_SECKEY_LEN 4896 |
| 40 |
#define MAX_SIG_LEN 4627 |
| 41 |
struct mldsa_info |
| 42 |
{ |
| 43 |
const char *name; /* Name of the algo. */ |
| 44 |
unsigned int namelen; /* Only here to avoid strlen calls. */ |
| 45 |
int algo; /* ML-DSA algo number. */ |
| 46 |
unsigned int nbits; /* Number of bits (pubkey size in bits). */ |
| 47 |
unsigned int fips:1; /* True if this is a FIPS140-4??? approved. */ |
| 48 |
int pubkey_len; /* Length of the public key. */ |
| 49 |
int seckey_len; /* Length of the secret key. */ |
| 50 |
int sig_len; /* Length of the signature. */ |
| 51 |
}; |
| 52 |
/* Information about the the ML-DSA algoithms for use by the |
| 53 |
* s-expression interface. */ |
| 54 |
static const struct mldsa_info mldsa_infos[] = |
| 55 |
{ |
| 56 |
{ "dilithium2", 10, GCRY_MLDSA44, 1312*8, 1, 1312, 2560, 2420 }, |
| 57 |
{ "dilithium3", 10, GCRY_MLDSA65, 1952*8, 1, 1952, 4032, 3309 }, |
| 58 |
{ "dilithium5", 10, GCRY_MLDSA87, 2592*8, 1, 2592, 4896, 4627 }, |
| 59 |
{ NULL } |
| 60 |
}; |
| 61 |
|
| 62 |
static const struct mldsa_info * |
| 63 |
mldsa_get_info (gcry_sexp_t keyparam) |
| 64 |
{ |
| 65 |
const char *algo; |
| 66 |
size_t algolen; |
| 67 |
const char *name; |
| 68 |
int i; |
| 69 |
|
| 70 |
algo = sexp_nth_data (keyparam, 0, &algolen); |
| 71 |
if (!algo || !algolen) |
| 72 |
return NULL; |
| 73 |
for (i=0; (name=mldsa_infos[i].name); i++) |
| 74 |
if (mldsa_infos[i].namelen == algolen && !memcmp (name, algo, algolen)) |
| 75 |
break; |
| 76 |
if (!name) |
| 77 |
return NULL; |
| 78 |
|
| 79 |
return &mldsa_infos[i]; |
| 80 |
} |
| 81 |
|
| 82 |
static unsigned int |
| 83 |
mldsa_get_nbits (gcry_sexp_t keyparam) |
| 84 |
{ |
| 85 |
const struct mldsa_info *info = mldsa_get_info (keyparam); |
| 86 |
if (!info) |
| 87 |
return 0; /* GPG_ERR_PUBKEY_ALGO */ |
| 88 |
|
| 89 |
return info->nbits; |
| 90 |
} |
| 91 |
|
| 92 |
static gpg_err_code_t |
| 93 |
mldsa_compute_keygrip (gcry_md_hd_t md, gcry_sexp_t keyparam) |
| 94 |
{ |
| 95 |
gcry_sexp_t l1; |
| 96 |
const char *data; |
| 97 |
size_t datalen; |
| 98 |
|
| 99 |
const struct mldsa_info *info = mldsa_get_info (keyparam); |
| 100 |
if (!info) |
| 101 |
return GPG_ERR_WRONG_PUBKEY_ALGO; |
| 102 |
|
| 103 |
_gcry_md_write (md, info->name, info->namelen+1); /* (also hash the nul) */ |
| 104 |
|
| 105 |
l1 = sexp_find_token (keyparam, "p", 1); |
| 106 |
if (!l1) |
| 107 |
return GPG_ERR_NO_OBJ; |
| 108 |
|
| 109 |
data = sexp_nth_data (l1, 1, &datalen); |
| 110 |
if (!data) |
| 111 |
{ |
| 112 |
sexp_release (l1); |
| 113 |
return GPG_ERR_NO_OBJ; |
| 114 |
} |
| 115 |
|
| 116 |
_gcry_md_write (md, data, datalen); |
| 117 |
sexp_release (l1); |
| 118 |
|
| 119 |
return 0; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
static void |
| 124 |
randombytes (unsigned char *out, size_t outlen) |
| 125 |
{ |
| 126 |
_gcry_randomize (out, outlen, GCRY_VERY_STRONG_RANDOM); |
| 127 |
} |
| 128 |
|
| 129 |
static gcry_err_code_t |
| 130 |
mldsa_generate (const gcry_sexp_t genparms, gcry_sexp_t *r_skey) |
| 131 |
{ |
| 132 |
gpg_err_code_t rc = 0; |
| 133 |
gcry_mpi_t seed_mpi = NULL; |
| 134 |
unsigned char seed[SEEDBYTES]; |
| 135 |
unsigned char pk[MAX_PUBKEY_LEN]; |
| 136 |
unsigned char sk[MAX_SECKEY_LEN]; |
| 137 |
const struct mldsa_info *info = mldsa_get_info (genparms); |
| 138 |
|
| 139 |
if (!info) |
| 140 |
return GPG_ERR_PUBKEY_ALGO; |
| 141 |
|
| 142 |
if (info->pubkey_len > MAX_PUBKEY_LEN) |
| 143 |
return GPG_ERR_INTERNAL; |
| 144 |
|
| 145 |
if (info->seckey_len > MAX_SECKEY_LEN) |
| 146 |
return GPG_ERR_INTERNAL; |
| 147 |
|
| 148 |
/* |
| 149 |
* Extract the seed (if any). |
| 150 |
*/ |
| 151 |
rc = sexp_extract_param (genparms, NULL, "/S", &seed_mpi, NULL); |
| 152 |
if (rc == GPG_ERR_NOT_FOUND) |
| 153 |
{ |
| 154 |
randombytes (seed, SEEDBYTES); |
| 155 |
rc = 0; |
| 156 |
} |
| 157 |
else if (rc) |
| 158 |
goto leave; |
| 159 |
else |
| 160 |
{ |
| 161 |
const unsigned char *seed_supplied; |
| 162 |
unsigned int n; |
| 163 |
|
| 164 |
seed_supplied = mpi_get_opaque (seed_mpi, &n); |
| 165 |
if (SEEDBYTES != (n + 7) / 8) |
| 166 |
{ |
| 167 |
rc = GPG_ERR_INV_DATA; |
| 168 |
goto leave; |
| 169 |
} |
| 170 |
memcpy (seed, seed_supplied, SEEDBYTES); |
| 171 |
} |
| 172 |
|
| 173 |
rc = dilithium_keypair (info->algo, pk, sk, seed); |
| 174 |
_gcry_burn_stack (DILITHIUM_KEYPAIR_STACK_BURN); |
| 175 |
|
| 176 |
if (!rc) |
| 177 |
rc = sexp_build (r_skey, |
| 178 |
NULL, |
| 179 |
"(key-data" |
| 180 |
" (public-key(%s(p%b)))" |
| 181 |
" (private-key(%s(p%b)(s%b)(S%b))))", |
| 182 |
info->name, info->pubkey_len, pk, |
| 183 |
info->name, info->pubkey_len, pk, info->seckey_len, sk, |
| 184 |
SEEDBYTES, seed, |
| 185 |
NULL); |
| 186 |
|
| 187 |
leave: |
| 188 |
_gcry_mpi_release (seed_mpi); |
| 189 |
wipememory (seed, SEEDBYTES); |
| 190 |
wipememory (sk, info->seckey_len); |
| 191 |
return rc; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
static gcry_err_code_t |
| 196 |
mldsa_sign (gcry_sexp_t *r_sig, gcry_sexp_t s_data, gcry_sexp_t keyparms) |
| 197 |
{ |
| 198 |
gpg_err_code_t rc = 0; |
| 199 |
unsigned int n; |
| 200 |
struct pk_encoding_ctx ctx; |
| 201 |
gcry_mpi_t sk_mpi = NULL; |
| 202 |
gcry_mpi_t data_mpi = NULL; |
| 203 |
unsigned char sig[MAX_SIG_LEN]; |
| 204 |
unsigned char rnd[RNDBYTES]; |
| 205 |
const unsigned char *data; |
| 206 |
size_t data_len; |
| 207 |
const unsigned char *sk; |
| 208 |
const struct mldsa_info *info = mldsa_get_info (keyparms); |
| 209 |
|
| 210 |
if (!info) |
| 211 |
return GPG_ERR_PUBKEY_ALGO; |
| 212 |
|
| 213 |
if (info->sig_len > MAX_SIG_LEN) |
| 214 |
return GPG_ERR_INTERNAL; |
| 215 |
|
| 216 |
_gcry_pk_util_init_encoding_ctx (&ctx, PUBKEY_OP_SIGN, 0); |
| 217 |
|
| 218 |
/* Dilithium requires the byte string for its DATA. */ |
| 219 |
ctx.flags |= PUBKEY_FLAG_BYTE_STRING; |
| 220 |
|
| 221 |
/* |
| 222 |
* Extract the secret key. |
| 223 |
*/ |
| 224 |
rc = sexp_extract_param (keyparms, NULL, "/s", &sk_mpi, NULL); |
| 225 |
if (rc) |
| 226 |
goto leave; |
| 227 |
sk = mpi_get_opaque (sk_mpi, &n); |
| 228 |
if (!sk || info->seckey_len != (n + 7) / 8) |
| 229 |
{ |
| 230 |
rc = GPG_ERR_BAD_SECKEY; |
| 231 |
goto leave; |
| 232 |
} |
| 233 |
|
| 234 |
/* Extract the data. */ |
| 235 |
rc = _gcry_pk_util_data_to_mpi (s_data, &data_mpi, &ctx); |
| 236 |
if (rc) |
| 237 |
goto leave; |
| 238 |
if (DBG_CIPHER) |
| 239 |
log_mpidump ("mldsa_sign data", data_mpi); |
| 240 |
if (!mpi_is_opaque (data_mpi)) |
| 241 |
{ |
| 242 |
rc = GPG_ERR_INV_DATA; |
| 243 |
goto leave; |
| 244 |
} |
| 245 |
data = mpi_get_opaque (data_mpi, &n); |
| 246 |
data_len = (n + 7) / 8; |
| 247 |
|
| 248 |
if (ctx.rnd) |
| 249 |
{ |
| 250 |
if (ctx.rndlen != RNDBYTES) |
| 251 |
{ |
| 252 |
rc = GPG_ERR_INV_DATA; |
| 253 |
goto leave; |
| 254 |
} |
| 255 |
memcpy (rnd, ctx.rnd, RNDBYTES); |
| 256 |
} |
| 257 |
else |
| 258 |
randombytes (rnd, RNDBYTES); |
| 259 |
if (ctx.flags & PUBKEY_FLAG_NO_PREFIX) |
| 260 |
rc = dilithium_sign (info->algo, sig, info->sig_len, data, data_len, |
| 261 |
NULL, -1, sk, rnd); |
| 262 |
else |
| 263 |
rc = dilithium_sign (info->algo, sig, info->sig_len, data, data_len, |
| 264 |
ctx.label, ctx.labellen, sk, rnd); |
| 265 |
_gcry_burn_stack (DILITHIUM_SIGN_STACK_BURN); |
| 266 |
if (rc) |
| 267 |
goto leave; |
| 268 |
|
| 269 |
rc = sexp_build (r_sig, NULL, "(sig-val(%s(s%b)))", info->name, |
| 270 |
info->sig_len, sig); |
| 271 |
if (rc) |
| 272 |
goto leave; |
| 273 |
|
| 274 |
leave: |
| 275 |
_gcry_pk_util_free_encoding_ctx (&ctx); |
| 276 |
_gcry_mpi_release (sk_mpi); |
| 277 |
_gcry_mpi_release (data_mpi); |
| 278 |
wipememory (rnd, RNDBYTES); |
| 279 |
if (DBG_CIPHER) |
| 280 |
log_debug ("mldsa_sign => %s\n", gpg_strerror (rc)); |
| 281 |
return rc; |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
static gcry_err_code_t |
| 286 |
mldsa_verify (gcry_sexp_t s_sig, gcry_sexp_t s_data, gcry_sexp_t keyparms) |
| 287 |
{ |
| 288 |
gpg_err_code_t rc = 0; |
| 289 |
unsigned int n; |
| 290 |
struct pk_encoding_ctx ctx; |
| 291 |
gcry_mpi_t sig_mpi = NULL; |
| 292 |
gcry_mpi_t data_mpi = NULL; |
| 293 |
gcry_mpi_t pk_mpi = NULL; |
| 294 |
const unsigned char *sig; |
| 295 |
const unsigned char *data; |
| 296 |
size_t data_len; |
| 297 |
const unsigned char *pk; |
| 298 |
const struct mldsa_info *info = mldsa_get_info (keyparms); |
| 299 |
|
| 300 |
if (!info) |
| 301 |
return GPG_ERR_PUBKEY_ALGO; |
| 302 |
|
| 303 |
_gcry_pk_util_init_encoding_ctx (&ctx, PUBKEY_OP_VERIFY, 0); |
| 304 |
|
| 305 |
/* Dilithium requires the byte string for its DATA. */ |
| 306 |
ctx.flags |= PUBKEY_FLAG_BYTE_STRING; |
| 307 |
|
| 308 |
/* |
| 309 |
* Extract the public key. |
| 310 |
*/ |
| 311 |
rc = sexp_extract_param (keyparms, NULL, "/p", &pk_mpi, NULL); |
| 312 |
if (rc) |
| 313 |
goto leave; |
| 314 |
pk = mpi_get_opaque (pk_mpi, &n); |
| 315 |
if (!pk || info->pubkey_len != (n + 7) / 8) |
| 316 |
{ |
| 317 |
rc = GPG_ERR_BAD_PUBKEY; |
| 318 |
goto leave; |
| 319 |
} |
| 320 |
|
| 321 |
rc = _gcry_pk_util_data_to_mpi (s_data, &data_mpi, &ctx); |
| 322 |
if (rc) |
| 323 |
goto leave; |
| 324 |
if (DBG_CIPHER) |
| 325 |
log_mpidump ("mldsa_verify data", data_mpi); |
| 326 |
if (!mpi_is_opaque (data_mpi)) |
| 327 |
{ |
| 328 |
rc = GPG_ERR_INV_DATA; |
| 329 |
goto leave; |
| 330 |
} |
| 331 |
data = mpi_get_opaque (data_mpi, &n); |
| 332 |
data_len = (n + 7) / 8; |
| 333 |
|
| 334 |
/* Extract the signature. */ |
| 335 |
rc = sexp_extract_param (s_sig, NULL, "/s", &sig_mpi, NULL); |
| 336 |
if (rc) |
| 337 |
goto leave; |
| 338 |
if (DBG_CIPHER) |
| 339 |
log_printmpi ("mldsa_verify sig", sig_mpi); |
| 340 |
sig = mpi_get_opaque (sig_mpi, &n); |
| 341 |
if (!sig || info->sig_len != (n + 7) / 8) |
| 342 |
{ |
| 343 |
rc = GPG_ERR_BAD_SIGNATURE; |
| 344 |
goto leave; |
| 345 |
} |
| 346 |
|
| 347 |
if (ctx.flags & PUBKEY_FLAG_NO_PREFIX) |
| 348 |
rc = dilithium_verify (info->algo, sig, info->sig_len, data, data_len, |
| 349 |
NULL, -1, pk); |
| 350 |
else |
| 351 |
rc = dilithium_verify (info->algo, sig, info->sig_len, data, data_len, |
| 352 |
ctx.label, ctx.labellen, pk); |
| 353 |
_gcry_burn_stack (DILITHIUM_VERIFY_STACK_BURN); |
| 354 |
if (rc) |
| 355 |
goto leave; |
| 356 |
|
| 357 |
leave: |
| 358 |
_gcry_pk_util_free_encoding_ctx (&ctx); |
| 359 |
_gcry_mpi_release (pk_mpi); |
| 360 |
_gcry_mpi_release (data_mpi); |
| 361 |
_gcry_mpi_release (sig_mpi); |
| 362 |
if (DBG_CIPHER) |
| 363 |
log_debug ("mldsa_verify => %s\n", gpg_strerror (rc)); |
| 364 |
return rc; |
| 365 |
} |
| 366 |
|
| 367 |
gcry_pk_spec_t _gcry_pubkey_spec_mldsa = |
| 368 |
{ |
| 369 |
GCRY_PK_MLDSA, {0, 1}, |
| 370 |
GCRY_PK_USAGE_SIGN, |
| 371 |
"ML-DSA", mldsa_names, |
| 372 |
"p", /* p: public */ |
| 373 |
"sS", /* s: secret, S: seed */ |
| 374 |
"", |
| 375 |
"s", /* s: signature */ |
| 376 |
"p", /* p: public */ |
| 377 |
mldsa_generate, |
| 378 |
NULL /* mldsa_check_secret_key */, |
| 379 |
NULL, |
| 380 |
NULL, |
| 381 |
mldsa_sign, |
| 382 |
mldsa_verify, |
| 383 |
mldsa_get_nbits, |
| 384 |
NULL, /*run_selftests*/ |
| 385 |
mldsa_compute_keygrip |
| 386 |
}; |