mldsa.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * crypto_sig wrapper around ML-DSA library.
  4. */
  5. #include <linux/init.h>
  6. #include <linux/module.h>
  7. #include <crypto/internal/sig.h>
  8. #include <crypto/mldsa.h>
  9. struct crypto_mldsa_ctx {
  10. u8 pk[MAX(MAX(MLDSA44_PUBLIC_KEY_SIZE,
  11. MLDSA65_PUBLIC_KEY_SIZE),
  12. MLDSA87_PUBLIC_KEY_SIZE)];
  13. unsigned int pk_len;
  14. enum mldsa_alg strength;
  15. bool key_set;
  16. };
  17. static int crypto_mldsa_sign(struct crypto_sig *tfm,
  18. const void *msg, unsigned int msg_len,
  19. void *sig, unsigned int sig_len)
  20. {
  21. return -EOPNOTSUPP;
  22. }
  23. static int crypto_mldsa_verify(struct crypto_sig *tfm,
  24. const void *sig, unsigned int sig_len,
  25. const void *msg, unsigned int msg_len)
  26. {
  27. const struct crypto_mldsa_ctx *ctx = crypto_sig_ctx(tfm);
  28. if (unlikely(!ctx->key_set))
  29. return -EINVAL;
  30. return mldsa_verify(ctx->strength, sig, sig_len, msg, msg_len,
  31. ctx->pk, ctx->pk_len);
  32. }
  33. static unsigned int crypto_mldsa_key_size(struct crypto_sig *tfm)
  34. {
  35. struct crypto_mldsa_ctx *ctx = crypto_sig_ctx(tfm);
  36. switch (ctx->strength) {
  37. case MLDSA44:
  38. return MLDSA44_PUBLIC_KEY_SIZE;
  39. case MLDSA65:
  40. return MLDSA65_PUBLIC_KEY_SIZE;
  41. case MLDSA87:
  42. return MLDSA87_PUBLIC_KEY_SIZE;
  43. default:
  44. WARN_ON_ONCE(1);
  45. return 0;
  46. }
  47. }
  48. static int crypto_mldsa_set_pub_key(struct crypto_sig *tfm,
  49. const void *key, unsigned int keylen)
  50. {
  51. struct crypto_mldsa_ctx *ctx = crypto_sig_ctx(tfm);
  52. unsigned int expected_len = crypto_mldsa_key_size(tfm);
  53. if (keylen != expected_len)
  54. return -EINVAL;
  55. ctx->pk_len = keylen;
  56. memcpy(ctx->pk, key, keylen);
  57. ctx->key_set = true;
  58. return 0;
  59. }
  60. static int crypto_mldsa_set_priv_key(struct crypto_sig *tfm,
  61. const void *key, unsigned int keylen)
  62. {
  63. return -EOPNOTSUPP;
  64. }
  65. static unsigned int crypto_mldsa_max_size(struct crypto_sig *tfm)
  66. {
  67. struct crypto_mldsa_ctx *ctx = crypto_sig_ctx(tfm);
  68. switch (ctx->strength) {
  69. case MLDSA44:
  70. return MLDSA44_SIGNATURE_SIZE;
  71. case MLDSA65:
  72. return MLDSA65_SIGNATURE_SIZE;
  73. case MLDSA87:
  74. return MLDSA87_SIGNATURE_SIZE;
  75. default:
  76. WARN_ON_ONCE(1);
  77. return 0;
  78. }
  79. }
  80. static int crypto_mldsa44_alg_init(struct crypto_sig *tfm)
  81. {
  82. struct crypto_mldsa_ctx *ctx = crypto_sig_ctx(tfm);
  83. ctx->strength = MLDSA44;
  84. ctx->key_set = false;
  85. return 0;
  86. }
  87. static int crypto_mldsa65_alg_init(struct crypto_sig *tfm)
  88. {
  89. struct crypto_mldsa_ctx *ctx = crypto_sig_ctx(tfm);
  90. ctx->strength = MLDSA65;
  91. ctx->key_set = false;
  92. return 0;
  93. }
  94. static int crypto_mldsa87_alg_init(struct crypto_sig *tfm)
  95. {
  96. struct crypto_mldsa_ctx *ctx = crypto_sig_ctx(tfm);
  97. ctx->strength = MLDSA87;
  98. ctx->key_set = false;
  99. return 0;
  100. }
  101. static void crypto_mldsa_alg_exit(struct crypto_sig *tfm)
  102. {
  103. }
  104. static struct sig_alg crypto_mldsa_algs[] = {
  105. {
  106. .sign = crypto_mldsa_sign,
  107. .verify = crypto_mldsa_verify,
  108. .set_pub_key = crypto_mldsa_set_pub_key,
  109. .set_priv_key = crypto_mldsa_set_priv_key,
  110. .key_size = crypto_mldsa_key_size,
  111. .max_size = crypto_mldsa_max_size,
  112. .init = crypto_mldsa44_alg_init,
  113. .exit = crypto_mldsa_alg_exit,
  114. .base.cra_name = "mldsa44",
  115. .base.cra_driver_name = "mldsa44-lib",
  116. .base.cra_ctxsize = sizeof(struct crypto_mldsa_ctx),
  117. .base.cra_module = THIS_MODULE,
  118. .base.cra_priority = 5000,
  119. }, {
  120. .sign = crypto_mldsa_sign,
  121. .verify = crypto_mldsa_verify,
  122. .set_pub_key = crypto_mldsa_set_pub_key,
  123. .set_priv_key = crypto_mldsa_set_priv_key,
  124. .key_size = crypto_mldsa_key_size,
  125. .max_size = crypto_mldsa_max_size,
  126. .init = crypto_mldsa65_alg_init,
  127. .exit = crypto_mldsa_alg_exit,
  128. .base.cra_name = "mldsa65",
  129. .base.cra_driver_name = "mldsa65-lib",
  130. .base.cra_ctxsize = sizeof(struct crypto_mldsa_ctx),
  131. .base.cra_module = THIS_MODULE,
  132. .base.cra_priority = 5000,
  133. }, {
  134. .sign = crypto_mldsa_sign,
  135. .verify = crypto_mldsa_verify,
  136. .set_pub_key = crypto_mldsa_set_pub_key,
  137. .set_priv_key = crypto_mldsa_set_priv_key,
  138. .key_size = crypto_mldsa_key_size,
  139. .max_size = crypto_mldsa_max_size,
  140. .init = crypto_mldsa87_alg_init,
  141. .exit = crypto_mldsa_alg_exit,
  142. .base.cra_name = "mldsa87",
  143. .base.cra_driver_name = "mldsa87-lib",
  144. .base.cra_ctxsize = sizeof(struct crypto_mldsa_ctx),
  145. .base.cra_module = THIS_MODULE,
  146. .base.cra_priority = 5000,
  147. },
  148. };
  149. static int __init mldsa_init(void)
  150. {
  151. int ret, i;
  152. for (i = 0; i < ARRAY_SIZE(crypto_mldsa_algs); i++) {
  153. ret = crypto_register_sig(&crypto_mldsa_algs[i]);
  154. if (ret < 0)
  155. goto error;
  156. }
  157. return 0;
  158. error:
  159. pr_err("Failed to register (%d)\n", ret);
  160. for (i--; i >= 0; i--)
  161. crypto_unregister_sig(&crypto_mldsa_algs[i]);
  162. return ret;
  163. }
  164. module_init(mldsa_init);
  165. static void mldsa_exit(void)
  166. {
  167. for (int i = 0; i < ARRAY_SIZE(crypto_mldsa_algs); i++)
  168. crypto_unregister_sig(&crypto_mldsa_algs[i]);
  169. }
  170. module_exit(mldsa_exit);
  171. MODULE_LICENSE("GPL");
  172. MODULE_DESCRIPTION("Crypto API support for ML-DSA signature verification");
  173. MODULE_ALIAS_CRYPTO("mldsa44");
  174. MODULE_ALIAS_CRYPTO("mldsa65");
  175. MODULE_ALIAS_CRYPTO("mldsa87");