pq_crypto 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/ci.yml +37 -0
  3. data/CHANGELOG.md +29 -0
  4. data/GET_STARTED.md +65 -0
  5. data/LICENSE.txt +21 -0
  6. data/README.md +135 -0
  7. data/SECURITY.md +57 -0
  8. data/ext/pqcrypto/extconf.rb +157 -0
  9. data/ext/pqcrypto/mldsa_api.h +51 -0
  10. data/ext/pqcrypto/mlkem_api.h +21 -0
  11. data/ext/pqcrypto/pqcrypto_ruby_secure.c +889 -0
  12. data/ext/pqcrypto/pqcrypto_secure.c +1178 -0
  13. data/ext/pqcrypto/pqcrypto_secure.h +135 -0
  14. data/ext/pqcrypto/vendor/.vendored +5 -0
  15. data/ext/pqcrypto/vendor/pqclean/common/aes.c +639 -0
  16. data/ext/pqcrypto/vendor/pqclean/common/aes.h +64 -0
  17. data/ext/pqcrypto/vendor/pqclean/common/compat.h +73 -0
  18. data/ext/pqcrypto/vendor/pqclean/common/crypto_declassify.h +7 -0
  19. data/ext/pqcrypto/vendor/pqclean/common/fips202.c +928 -0
  20. data/ext/pqcrypto/vendor/pqclean/common/fips202.h +166 -0
  21. data/ext/pqcrypto/vendor/pqclean/common/keccak2x/feat.S +168 -0
  22. data/ext/pqcrypto/vendor/pqclean/common/keccak2x/fips202x2.c +684 -0
  23. data/ext/pqcrypto/vendor/pqclean/common/keccak2x/fips202x2.h +60 -0
  24. data/ext/pqcrypto/vendor/pqclean/common/keccak4x/KeccakP-1600-times4-SIMD256.c +1028 -0
  25. data/ext/pqcrypto/vendor/pqclean/common/keccak4x/KeccakP-1600-times4-SnP.h +50 -0
  26. data/ext/pqcrypto/vendor/pqclean/common/keccak4x/KeccakP-1600-unrolling.macros +198 -0
  27. data/ext/pqcrypto/vendor/pqclean/common/keccak4x/Makefile +8 -0
  28. data/ext/pqcrypto/vendor/pqclean/common/keccak4x/Makefile.Microsoft_nmake +8 -0
  29. data/ext/pqcrypto/vendor/pqclean/common/keccak4x/SIMD256-config.h +3 -0
  30. data/ext/pqcrypto/vendor/pqclean/common/keccak4x/align.h +34 -0
  31. data/ext/pqcrypto/vendor/pqclean/common/keccak4x/brg_endian.h +142 -0
  32. data/ext/pqcrypto/vendor/pqclean/common/nistseedexpander.c +101 -0
  33. data/ext/pqcrypto/vendor/pqclean/common/nistseedexpander.h +39 -0
  34. data/ext/pqcrypto/vendor/pqclean/common/randombytes.c +355 -0
  35. data/ext/pqcrypto/vendor/pqclean/common/randombytes.h +27 -0
  36. data/ext/pqcrypto/vendor/pqclean/common/sha2.c +769 -0
  37. data/ext/pqcrypto/vendor/pqclean/common/sha2.h +173 -0
  38. data/ext/pqcrypto/vendor/pqclean/common/sp800-185.c +156 -0
  39. data/ext/pqcrypto/vendor/pqclean/common/sp800-185.h +27 -0
  40. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/LICENSE +5 -0
  41. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/Makefile +19 -0
  42. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/Makefile.Microsoft_nmake +23 -0
  43. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/api.h +18 -0
  44. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/cbd.c +83 -0
  45. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/cbd.h +11 -0
  46. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/indcpa.c +327 -0
  47. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/indcpa.h +22 -0
  48. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/kem.c +164 -0
  49. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/kem.h +23 -0
  50. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/ntt.c +146 -0
  51. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/ntt.h +14 -0
  52. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/params.h +36 -0
  53. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/poly.c +299 -0
  54. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/poly.h +37 -0
  55. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/polyvec.c +188 -0
  56. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/polyvec.h +26 -0
  57. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/reduce.c +41 -0
  58. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/reduce.h +13 -0
  59. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/symmetric-shake.c +71 -0
  60. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/symmetric.h +30 -0
  61. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/verify.c +67 -0
  62. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/verify.h +13 -0
  63. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/LICENSE +5 -0
  64. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/Makefile +19 -0
  65. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/Makefile.Microsoft_nmake +23 -0
  66. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/api.h +50 -0
  67. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/ntt.c +98 -0
  68. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/ntt.h +10 -0
  69. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/packing.c +261 -0
  70. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/packing.h +31 -0
  71. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/params.h +44 -0
  72. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/poly.c +799 -0
  73. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/poly.h +52 -0
  74. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/polyvec.c +415 -0
  75. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/polyvec.h +65 -0
  76. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/reduce.c +69 -0
  77. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/reduce.h +17 -0
  78. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/rounding.c +92 -0
  79. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/rounding.h +14 -0
  80. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/sign.c +407 -0
  81. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/sign.h +47 -0
  82. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/symmetric-shake.c +26 -0
  83. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/symmetric.h +34 -0
  84. data/lib/pq_crypto/errors.rb +10 -0
  85. data/lib/pq_crypto/hybrid_kem.rb +106 -0
  86. data/lib/pq_crypto/kem.rb +199 -0
  87. data/lib/pq_crypto/serialization.rb +102 -0
  88. data/lib/pq_crypto/signature.rb +198 -0
  89. data/lib/pq_crypto/version.rb +5 -0
  90. data/lib/pq_crypto.rb +177 -0
  91. data/lib/pqcrypto.rb +3 -0
  92. data/script/vendor_libs.rb +199 -0
  93. metadata +195 -0
@@ -0,0 +1,135 @@
1
+ #ifndef PQCRYPTO_SECURE_H
2
+ #define PQCRYPTO_SECURE_H
3
+
4
+ #include <stdint.h>
5
+ #include <stdlib.h>
6
+ #include <string.h>
7
+
8
+ #ifndef HAVE_PQCLEAN
9
+ #error "PQClean sources are required to build pq_crypto. Run: bundle exec rake vendor"
10
+ #endif
11
+
12
+ #include "mlkem_api.h"
13
+ #include "mldsa_api.h"
14
+ #define MLKEM_PUBLICKEYBYTES PQCLEAN_MLKEM768_CLEAN_CRYPTO_PUBLICKEYBYTES
15
+ #define MLKEM_SECRETKEYBYTES PQCLEAN_MLKEM768_CLEAN_CRYPTO_SECRETKEYBYTES
16
+ #define MLKEM_CIPHERTEXTBYTES PQCLEAN_MLKEM768_CLEAN_CRYPTO_CIPHERTEXTBYTES
17
+ #define MLKEM_SHAREDSECRETBYTES PQCLEAN_MLKEM768_CLEAN_CRYPTO_BYTES
18
+
19
+ #define MLDSA_PUBLICKEYBYTES 1952
20
+ #define MLDSA_SECRETKEYBYTES 4032
21
+ #define MLDSA_BYTES 3309
22
+
23
+ #define X25519_PUBLICKEYBYTES 32
24
+ #define X25519_SECRETKEYBYTES 32
25
+ #define X25519_SHAREDSECRETBYTES 32
26
+
27
+ #define HYBRID_PUBLICKEYBYTES (MLKEM_PUBLICKEYBYTES + X25519_PUBLICKEYBYTES)
28
+ #define HYBRID_SECRETKEYBYTES (MLKEM_SECRETKEYBYTES + X25519_SECRETKEYBYTES)
29
+ #define HYBRID_CIPHERTEXTBYTES (MLKEM_CIPHERTEXTBYTES + X25519_PUBLICKEYBYTES)
30
+ #define HYBRID_SHAREDSECRETBYTES 32
31
+
32
+ typedef enum {
33
+ PQ_SUCCESS = 0,
34
+ PQ_ERROR_KEYPAIR = -1,
35
+ PQ_ERROR_ENCAPSULATE = -2,
36
+ PQ_ERROR_DECAPSULATE = -3,
37
+ PQ_ERROR_SIGN = -4,
38
+ PQ_ERROR_VERIFY = -5,
39
+ PQ_ERROR_KDF = -6,
40
+ PQ_ERROR_RANDOM = -7,
41
+ PQ_ERROR_BUFFER = -8,
42
+ PQ_ERROR_NOMEM = -9,
43
+ PQ_ERROR_OPENSSL = -10
44
+ } pq_error_t;
45
+
46
+ typedef struct {
47
+ uint8_t mlkem_pk[MLKEM_PUBLICKEYBYTES];
48
+ uint8_t x25519_pk[X25519_PUBLICKEYBYTES];
49
+ } hybrid_public_key_t;
50
+
51
+ typedef struct {
52
+ uint8_t mlkem_sk[MLKEM_SECRETKEYBYTES];
53
+ uint8_t x25519_sk[X25519_SECRETKEYBYTES];
54
+ } hybrid_secret_key_t;
55
+
56
+ typedef struct {
57
+ uint8_t mlkem_ct[MLKEM_CIPHERTEXTBYTES];
58
+ uint8_t x25519_ephemeral[X25519_PUBLICKEYBYTES];
59
+ } hybrid_ciphertext_t;
60
+
61
+ void pq_secure_wipe(void *ptr, size_t len);
62
+
63
+ int pq_mlkem_keypair(uint8_t *public_key, uint8_t *secret_key);
64
+ int pq_mlkem_encapsulate(uint8_t *ciphertext, uint8_t *shared_secret, const uint8_t *public_key);
65
+ int pq_mlkem_decapsulate(uint8_t *shared_secret, const uint8_t *ciphertext,
66
+ const uint8_t *secret_key);
67
+
68
+ int pq_sign_keypair(uint8_t *public_key, uint8_t *secret_key);
69
+ int pq_sign(uint8_t *signature, size_t *signature_len, const uint8_t *message, size_t message_len,
70
+ const uint8_t *secret_key);
71
+ int pq_verify(const uint8_t *signature, size_t signature_len, const uint8_t *message,
72
+ size_t message_len, const uint8_t *public_key);
73
+
74
+ int pq_public_key_to_pqc_container_der(uint8_t **output, size_t *output_len,
75
+ const uint8_t *public_key,
76
+ size_t public_key_len, const char *algorithm);
77
+ int pq_public_key_to_pqc_container_pem(char **output, size_t *output_len,
78
+ const uint8_t *public_key,
79
+ size_t public_key_len, const char *algorithm);
80
+ int pq_secret_key_to_pqc_container_der(uint8_t **output, size_t *output_len,
81
+ const uint8_t *secret_key,
82
+ size_t secret_key_len, const char *algorithm);
83
+ int pq_secret_key_to_pqc_container_pem(char **output, size_t *output_len,
84
+ const uint8_t *secret_key,
85
+ size_t secret_key_len, const char *algorithm);
86
+ int pq_public_key_from_pqc_container_der(char **algorithm_out, uint8_t **key_out,
87
+ size_t *key_len_out, const uint8_t *input,
88
+ size_t input_len);
89
+ int pq_public_key_from_pqc_container_pem(char **algorithm_out, uint8_t **key_out,
90
+ size_t *key_len_out, const char *input,
91
+ size_t input_len);
92
+ int pq_secret_key_from_pqc_container_der(char **algorithm_out, uint8_t **key_out,
93
+ size_t *key_len_out, const uint8_t *input,
94
+ size_t input_len);
95
+ int pq_secret_key_from_pqc_container_pem(char **algorithm_out, uint8_t **key_out,
96
+ size_t *key_len_out, const char *input,
97
+ size_t input_len);
98
+
99
+
100
+ /* Test-only deterministic hooks for regression harness. */
101
+ int pq_testing_mlkem_keypair_from_seed(uint8_t *public_key, uint8_t *secret_key,
102
+ const uint8_t *seed, size_t seed_len);
103
+ int pq_testing_mlkem_encapsulate_from_seed(uint8_t *ciphertext, uint8_t *shared_secret,
104
+ const uint8_t *public_key, const uint8_t *seed,
105
+ size_t seed_len);
106
+ int pq_testing_mldsa_keypair_from_seed(uint8_t *public_key, uint8_t *secret_key,
107
+ const uint8_t *seed, size_t seed_len);
108
+ int pq_testing_mldsa_sign_from_seed(uint8_t *signature, size_t *signature_len,
109
+ const uint8_t *message, size_t message_len,
110
+ const uint8_t *secret_key, const uint8_t *seed,
111
+ size_t seed_len);
112
+
113
+ const char *pq_version(void);
114
+
115
+ #define PQ_MLKEM_PUBLICKEYBYTES MLKEM_PUBLICKEYBYTES
116
+ #define PQ_MLKEM_SECRETKEYBYTES MLKEM_SECRETKEYBYTES
117
+ #define PQ_MLKEM_CIPHERTEXTBYTES MLKEM_CIPHERTEXTBYTES
118
+ #define PQ_MLKEM_SHAREDSECRETBYTES MLKEM_SHAREDSECRETBYTES
119
+
120
+ #define PQ_HYBRID_PUBLICKEYBYTES HYBRID_PUBLICKEYBYTES
121
+ #define PQ_HYBRID_SECRETKEYBYTES HYBRID_SECRETKEYBYTES
122
+ #define PQ_HYBRID_CIPHERTEXTBYTES HYBRID_CIPHERTEXTBYTES
123
+ #define PQ_HYBRID_SHAREDSECRETBYTES HYBRID_SHAREDSECRETBYTES
124
+
125
+ #define PQ_MLDSA_PUBLICKEYBYTES MLDSA_PUBLICKEYBYTES
126
+ #define PQ_MLDSA_SECRETKEYBYTES MLDSA_SECRETKEYBYTES
127
+ #define PQ_MLDSA_BYTES MLDSA_BYTES
128
+
129
+ int pq_hybrid_kem_keypair(uint8_t *public_key, uint8_t *secret_key);
130
+ int pq_hybrid_kem_encapsulate(uint8_t *ciphertext, uint8_t *shared_secret,
131
+ const uint8_t *public_key);
132
+ int pq_hybrid_kem_decapsulate(uint8_t *shared_secret, const uint8_t *ciphertext,
133
+ const uint8_t *secret_key);
134
+
135
+ #endif
@@ -0,0 +1,5 @@
1
+ pqclean_version=2cc64716044832eea747234ddbffc06746ab815d
2
+ pqclean_url=https://github.com/PQClean/PQClean/archive/2cc64716044832eea747234ddbffc06746ab815d.tar.gz
3
+ pqclean_archive_sha256=0e92076a79082a8d220e27227f37b280fb2ce050af412babd2bc755ab37b871a
4
+ pqclean_strip=PQClean-2cc64716044832eea747234ddbffc06746ab815d
5
+ pqclean_tree_sha256=2af0c3ec2cbe3b06805c39d3d1389ee7a9b0b29a83183328374a0db55f56c19e