|
14 | 14 | // You should have received a copy of the GNU General Public License
|
15 | 15 | // along with this program. If not, see <https://www.gnu.org/licenses/>.
|
16 | 16 |
|
17 |
| -use crate::{Ed25519KeyPair, Generator, KeyPairTrait, X25519KeyPair, X25519Private, X25519Public}; |
| 17 | +use crate::{Ed25519KeyPair, Generator, KeyPairTrait, SealKeyPair, X25519KeyPair, X25519Private, X25519Public}; |
18 | 18 | use never_type::Never;
|
19 | 19 | use rand::rngs::OsRng;
|
20 | 20 | #[cfg(test)]
|
21 | 21 | use rand::SeedableRng;
|
22 | 22 | use rand_xorshift::XorShiftRng;
|
| 23 | +use sodiumoxide::crypto::box_::gen_keypair as gen_curve25519xsalsa20poly1305; |
23 | 24 | use sodiumoxide::crypto::kx::gen_keypair as gen_x25519;
|
24 | 25 | use sodiumoxide::crypto::sign::gen_keypair as gen_ed25519;
|
25 | 26 | #[cfg(test)]
|
@@ -126,3 +127,45 @@ impl Generator<X25519KeyPair> for XorShiftRng {
|
126 | 127 | Ok(X25519KeyPair::from_keypair(sec, publ))
|
127 | 128 | }
|
128 | 129 | }
|
| 130 | + |
| 131 | +impl Generator<SealKeyPair> for Random { |
| 132 | + type Error = ::std::io::Error; |
| 133 | + |
| 134 | + //FIXME: there is no distinction between the two generate functions |
| 135 | + #[cfg(not(test))] |
| 136 | + fn generate(&mut self) -> Result<SealKeyPair, Self::Error> { |
| 137 | + let mut rng = OsRng::new()?; |
| 138 | + match rng.generate() { |
| 139 | + Ok(pair) => Ok(pair), |
| 140 | + Err(never) => match never {}, // LLVM unreachable |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + #[cfg(test)] |
| 145 | + fn generate(&mut self) -> Result<SealKeyPair, Self::Error> { |
| 146 | + RNG.with(|rng| { |
| 147 | + match rng.borrow_mut().generate() { |
| 148 | + Ok(pair) => Ok(pair), |
| 149 | + Err(never) => match never {}, // LLVM unreachable |
| 150 | + } |
| 151 | + }) |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +impl Generator<SealKeyPair> for OsRng { |
| 156 | + type Error = Never; |
| 157 | + |
| 158 | + fn generate(&mut self) -> Result<SealKeyPair, Self::Error> { |
| 159 | + let (publ, sec) = gen_curve25519xsalsa20poly1305(); |
| 160 | + Ok(SealKeyPair::from_keypair(sec.into(), publ.into())) |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +impl Generator<SealKeyPair> for XorShiftRng { |
| 165 | + type Error = Never; |
| 166 | + |
| 167 | + fn generate(&mut self) -> Result<SealKeyPair, Self::Error> { |
| 168 | + let (publ, sec) = gen_curve25519xsalsa20poly1305(); |
| 169 | + Ok(SealKeyPair::from_keypair(sec.into(), publ.into())) |
| 170 | + } |
| 171 | +} |
0 commit comments