1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//! Low level library for post-quantum signature scheme dilithium.
//!
//! Uses a slightly modified version of the C code of [`pqclean`] as the actual
//! implementation, which is compiled by a build script. The API is modified to
//! put the user in control of required randomness.
//!
//! The library has a minimal set of dependencies: in the default configuration
//! (without [`serde`] support) only [`cty`].
//!
//! # Security
//! __Warning__: This crate is intended as a lower level crate implementing a
//! primitive and exposing "not hard to misuse" APIs to provide the user with
//! maximum control. Only use if you know what you are doing! Always read
//! security sections in the documentation. Otherwise use a higher level
//! wrapper.
//!
//! __Warning__: This crate has not been audited for correctness. The C code is
//! copied from the well-regarded [`pqclean`] project, but since then
//! modifications have been made.
//!
//! USE AT YOUR OWN RISK!
//!
//! # Usage
//! The API is located in the `dilithiumX` module, for X in {2, 3, 5}. To
//! generate a keypair, use `generate_keypair`. Note: it requires a buffer
//! filled with cryptographically secure random bytes. The random buffer is not
//! modified, so zeroization is left to the user. Example:
//! ```
//! use dilithium_raw::dilithium5::generate_keypair;
//! use rand::rngs::OsRng;
//! use rand::Rng;
//! use zeroize::Zeroize;
//!
//! // fill buffer of 128 bytes with secure random data
//! let mut random = [0; 128];
//! OsRng.fill(&mut random[..]);
//!
//! // generate keypair
//! let (pubkey, seckey) = generate_keypair(&mut random);
//!
//! // zeroize the buffer with random data
//! random.zeroize();
//! ```
//!
//! To sign a message using the secret key, use `sign` and to verify it using
//! the public key, use `verify`. `verify` returns `Ok` for a valid signature
//! and `Err` for an invalid signature. Example:
//! ```
//! use dilithium_raw::dilithium5::{sign, verify};
//!
//! // snip, get a `pubkey` and `seckey` with the public and secret key respectively
//! # use rand::Rng;
//! # let mut random = [0; 128];
//! # rand::rngs::OsRng.fill(&mut random[..]);
//! # let (pubkey, seckey) = dilithium_raw::dilithium5::generate_keypair(&mut random);
//!
//! let msg = "hello world";
//! let sig = sign(msg, &seckey);
//! assert!(verify(msg, &sig, &pubkey).is_ok());
//! ```
//!
//! [`cty`]: https://crates.io/crates/cty
//! [`pqclean`]: https://github.com/PQClean/PQClean
//! [`serde`]: https://crates.io/crates/serde
/// Low level C bindings.
/// Utilities, mostly for use in this crate.
/// Message did verify correctly.
;
/// Message did not verify against the given signature.
;
/// Type alias for the return type of verification checks.
pub type VerificationResult = ;
/// Module containing a mid-level API to dilithium 2.
/// Module containing a mid-level API to dilithium 3.
/// Module containing a mid-level API to dilithium 5.