forked from cloudflare/boringtun
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mod.rs
129 lines (107 loc) · 3.06 KB
/
mod.rs
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
120
121
122
123
124
125
126
127
128
129
// Copyright (c) 2019 Quentin Kniep. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause
use std::fmt;
mod tests;
mod kyber1024;
pub use kyber1024::*;
#[repr(C)]
pub struct PQPublicKey {
pub bytes: [u8; PQ_PUBLIC_KEY_SIZE],
}
impl PQPublicKey {
pub fn encaps(&self) -> (PQCiphertext, PQSharedSecret) {
let mut ciphertext = PQCiphertext { bytes: [0; PQ_CIPHERTEXT_SIZE] };
let mut shared_secret = PQSharedSecret { bytes: [0; PQ_SHARED_KEY_SIZE] };
unsafe {
pq_encaps(&mut ciphertext.bytes[0],
&mut shared_secret.bytes[0],
&self.bytes[0]);
}
(ciphertext, shared_secret)
}
pub fn as_bytes(&self) -> &[u8] {
&self.bytes[..]
}
}
impl fmt::Debug for PQPublicKey {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
self.bytes[..].fmt(formatter)
}
}
/// Will panic if the slice.len() != PQ_PUBLIC_KEY_SIZE
impl<'a> From<&'a [u8]> for PQPublicKey {
fn from(slice: &[u8]) -> Self {
let mut bytes = [0u8; PQ_PUBLIC_KEY_SIZE];
bytes[..].copy_from_slice(slice);
PQPublicKey { bytes }
}
}
#[repr(C)]
pub struct PQSecretKey {
pub bytes: [u8; PQ_SECRET_KEY_SIZE],
}
impl PQSecretKey {
pub fn as_bytes(&self) -> &[u8] {
&self.bytes[..]
}
}
impl fmt::Debug for PQSecretKey {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
self.bytes[..].fmt(formatter)
}
}
#[repr(C)]
pub struct PQCiphertext {
bytes: [u8; PQ_CIPHERTEXT_SIZE],
}
impl PQCiphertext {
pub fn decaps(&self, secret_key: &PQSecretKey) -> PQSharedSecret {
let mut shared_secret = PQSharedSecret { bytes: [0; PQ_SHARED_KEY_SIZE] };
unsafe {
pq_decaps(&mut shared_secret.bytes[0],
&self.bytes[0],
&secret_key.bytes[0]);
}
shared_secret
}
pub fn as_bytes(&self) -> &[u8] {
&self.bytes[..]
}
}
/// Will panic if the slice.len() != PQ_CIPHERTEXT_SIZE
impl<'a> From<&'a [u8]> for PQCiphertext {
fn from(slice: &[u8]) -> Self {
let mut bytes = [0u8; PQ_CIPHERTEXT_SIZE];
bytes[..].copy_from_slice(slice);
PQCiphertext { bytes }
}
}
#[repr(C)]
pub struct PQSharedSecret {
bytes: [u8; PQ_SHARED_KEY_SIZE],
}
impl PQSharedSecret {
pub fn as_bytes(&self) -> &[u8] {
&self.bytes[..]
}
}
#[repr(C)]
pub struct PQKeyPair {
pub public_key: PQPublicKey,
pub secret_key: PQSecretKey,
}
impl PQKeyPair {
pub fn new() -> PQKeyPair {
let mut public_key = PQPublicKey { bytes: [0; PQ_PUBLIC_KEY_SIZE] };
let mut secret_key = PQSecretKey { bytes: [0; PQ_SECRET_KEY_SIZE] };
unsafe {
while pq_keypair(&mut public_key.bytes[0],
&mut secret_key.bytes[0]) != 0 {};
}
//println!("PUBLIC PQ KEY:\n{:?}\n\nSECRET PQ KEY:\n{:?}", public_key, secret_key);
PQKeyPair { public_key, secret_key }
}
pub fn public_key_bytes(&self) -> &[u8] {
&self.public_key.bytes[..]
}
}