tobogganSe connecter

louise

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

279 lignes · 9736 octetsBrut · Historique
1//! Cryptographic hashes for `std/crypto` (`louise_rt_prims::crypto` does
2//! the work; PostgreSQL's SCRAM-SHA-256 needs them, issue #145), Ed25519
3//! signatures (ADR 0067), and ChaCha20-Poly1305 with HKDF-SHA-256 (ADR
4//! 0071).
5//!
6//! `tl_crypto_pbkdf2_sha256` is `blocking`: the iteration count is the
7//! caller's, so the call runs on the blocking pool and never stalls a
8//! scheduler thread.
9//!
10//! Without the `crypto` feature (`ring`'s build script needs a C
11//! cross-compiler the no-qemu aarch64 check does not have, scripts/ci.sh)
12//! the externs exist and panic with "crypto support not built in", the
13//! `tls` feature's precedent: a program that hashes, linked against such
14//! a runtime, fails with that message instead of a link error.
15
16use crate::abi::{Ctx, Value};
17
18#[cfg(feature = "crypto")]
19pub use enabled::*;
20
21#[cfg(not(feature = "crypto"))]
22pub use disabled::*;
23
24#[cfg(not(feature = "crypto"))]
25mod disabled {
26 use super::*;
27 use crate::prims::panic_with;
28
29 const NOT_BUILT: &str = "crypto support not built in (louise_rt's `crypto` feature)";
30
31 macro_rules! not_built {
32 ($($name:ident($($arg:ident),*)),*) => {$(
33 /// # Safety
34 /// Never reached: panics before touching the arguments.
35 #[unsafe(no_mangle)]
36 pub unsafe extern "C" fn $name(ctx: *mut Ctx, $($arg: Value),*) -> Value {
37 $(let _ = $arg;)*
38 // SAFETY: caller guarantees the running actor's context.
39 unsafe { panic_with(ctx, NOT_BUILT) }
40 }
41 )*};
42 }
43 not_built!(
44 tl_crypto_sha256(data),
45 tl_crypto_hmac_sha256(key, message),
46 tl_crypto_pbkdf2_sha256(password, salt, iterations, length),
47 tl_crypto_ed25519_verify(public_key, message, signature),
48 tl_crypto_ed25519_public_key(seed),
49 tl_crypto_ed25519_sign(seed, message),
50 tl_crypto_chacha20_poly1305_seal(key, nonce, plaintext, aad),
51 tl_crypto_chacha20_poly1305_open(key, nonce, sealed, aad),
52 tl_crypto_hkdf_sha256(secret, salt, info, length)
53 );
54}
55
56#[cfg(feature = "crypto")]
57mod enabled {
58 use super::*;
59 use crate::int::saturating;
60 use crate::prims::{boolean, panic_with};
61 use crate::values::{bytes_from, bytes_of, error, ok};
62
63 /// SHA-256 of `data` as `Bytes`.
64 ///
65 /// # Safety
66 /// `data` must be live `Bytes`.
67 #[unsafe(no_mangle)]
68 pub unsafe extern "C" fn tl_crypto_sha256(ctx: *mut Ctx, data: Value) -> Value {
69 // SAFETY: caller guarantees live Bytes.
70 let hash = louise_rt_prims::crypto::sha256(unsafe { bytes_of(data) });
71 bytes_from(ctx, &hash)
72 }
73
74 /// HMAC-SHA-256 of `message` under `key`, as `Bytes`.
75 ///
76 /// # Safety
77 /// Both arguments must be live `Bytes`.
78 #[unsafe(no_mangle)]
79 pub unsafe extern "C" fn tl_crypto_hmac_sha256(
80 ctx: *mut Ctx,
81 key: Value,
82 message: Value,
83 ) -> Value {
84 // SAFETY: caller guarantees live Bytes.
85 let tag = louise_rt_prims::crypto::hmac_sha256(unsafe { bytes_of(key) }, unsafe {
86 bytes_of(message)
87 });
88 bytes_from(ctx, &tag)
89 }
90
91 /// PBKDF2-HMAC-SHA-256 of `password` with `salt` and `iterations`,
92 /// `length` bytes of output.
93 ///
94 /// # Safety
95 /// `password` and `salt` must be live `Bytes`, `iterations` and `length`
96 /// live `Int`s. Runs on the blocking pool: the result is built with the
97 /// allocation functions of `louise_rt.h`, which work on any thread.
98 #[unsafe(no_mangle)]
99 pub unsafe extern "C" fn tl_crypto_pbkdf2_sha256(
100 ctx: *mut Ctx,
101 password: Value,
102 salt: Value,
103 iterations: Value,
104 length: Value,
105 ) -> Value {
106 // SAFETY: caller guarantees live Bytes and Ints.
107 let (password, salt, iterations, length) = unsafe {
108 (
109 bytes_of(password),
110 bytes_of(salt),
111 saturating(iterations),
112 saturating(length).clamp(0, 1 << 20),
113 )
114 };
115 let derived = louise_rt_prims::crypto::pbkdf2_hmac_sha256(
116 password,
117 salt,
118 iterations,
119 length as usize,
120 );
121 bytes_from(ctx, &derived)
122 }
123
124 /// Whether `signature` is the Ed25519 signature of `message` under
125 /// `public_key`, as a `Bool`; wrong lengths are `False`.
126 ///
127 /// # Safety
128 /// All three arguments must be live `Bytes`.
129 #[unsafe(no_mangle)]
130 pub unsafe extern "C" fn tl_crypto_ed25519_verify(
131 _ctx: *mut Ctx,
132 public_key: Value,
133 message: Value,
134 signature: Value,
135 ) -> Value {
136 // SAFETY: caller guarantees live Bytes.
137 let valid = unsafe {
138 louise_rt_prims::crypto::ed25519_verify(
139 bytes_of(public_key),
140 bytes_of(message),
141 bytes_of(signature),
142 )
143 };
144 boolean(valid)
145 }
146
147 /// The Ed25519 public key of a 32-byte `seed`, as `Bytes`; panics on a
148 /// seed of another length.
149 ///
150 /// # Safety
151 /// `seed` must be live `Bytes`, and `ctx` the running actor's context.
152 #[unsafe(no_mangle)]
153 pub unsafe extern "C" fn tl_crypto_ed25519_public_key(ctx: *mut Ctx, seed: Value) -> Value {
154 // SAFETY: caller guarantees live Bytes.
155 match louise_rt_prims::crypto::ed25519_public_key(unsafe { bytes_of(seed) }) {
156 Ok(key) => bytes_from(ctx, &key),
157 // SAFETY: caller guarantees the running actor's context.
158 Err(message) => unsafe { panic_with(ctx, &message) },
159 }
160 }
161
162 /// The Ed25519 signature of `message` under the key pair of a 32-byte
163 /// `seed`, as `Bytes`; panics on a seed of another length.
164 ///
165 /// # Safety
166 /// Both arguments must be live `Bytes`, and `ctx` the running actor's
167 /// context.
168 #[unsafe(no_mangle)]
169 pub unsafe extern "C" fn tl_crypto_ed25519_sign(
170 ctx: *mut Ctx,
171 seed: Value,
172 message: Value,
173 ) -> Value {
174 // SAFETY: caller guarantees live Bytes.
175 let signature = louise_rt_prims::crypto::ed25519_sign(unsafe { bytes_of(seed) }, unsafe {
176 bytes_of(message)
177 });
178 match signature {
179 Ok(signature) => bytes_from(ctx, &signature),
180 // SAFETY: caller guarantees the running actor's context.
181 Err(message) => unsafe { panic_with(ctx, &message) },
182 }
183 }
184
185 /// The ChaCha20-Poly1305 sealing of `plaintext`, as `Bytes` (the
186 /// ciphertext, then the 16-byte tag); panics on a key that is not 32
187 /// bytes or a nonce that is not 12.
188 ///
189 /// # Safety
190 /// All four arguments must be live `Bytes`, and `ctx` the running
191 /// actor's context.
192 #[unsafe(no_mangle)]
193 pub unsafe extern "C" fn tl_crypto_chacha20_poly1305_seal(
194 ctx: *mut Ctx,
195 key: Value,
196 nonce: Value,
197 plaintext: Value,
198 aad: Value,
199 ) -> Value {
200 // SAFETY: caller guarantees live Bytes.
201 let sealed = unsafe {
202 louise_rt_prims::crypto::chacha20_poly1305_seal(
203 bytes_of(key),
204 bytes_of(nonce),
205 bytes_of(plaintext),
206 bytes_of(aad),
207 )
208 };
209 match sealed {
210 Ok(sealed) => bytes_from(ctx, &sealed),
211 // SAFETY: caller guarantees the running actor's context.
212 Err(message) => unsafe { panic_with(ctx, &message) },
213 }
214 }
215
216 /// `Ok(plaintext)` if `sealed` authenticates under `key`, `nonce` and
217 /// `aad`, else `Error`; panics on a key or nonce of the wrong length.
218 ///
219 /// # Safety
220 /// All four arguments must be live `Bytes`, and `ctx` the running
221 /// actor's context.
222 #[unsafe(no_mangle)]
223 pub unsafe extern "C" fn tl_crypto_chacha20_poly1305_open(
224 ctx: *mut Ctx,
225 key: Value,
226 nonce: Value,
227 sealed: Value,
228 aad: Value,
229 ) -> Value {
230 // SAFETY: caller guarantees live Bytes.
231 let opened = unsafe {
232 louise_rt_prims::crypto::chacha20_poly1305_open(
233 bytes_of(key),
234 bytes_of(nonce),
235 bytes_of(sealed),
236 bytes_of(aad),
237 )
238 };
239 match opened {
240 Ok(Some(plaintext)) => {
241 let plaintext = bytes_from(ctx, &plaintext);
242 ok(ctx, plaintext)
243 }
244 Ok(None) => error(ctx, louise_rt_prims::crypto::NOT_AUTHENTIC),
245 // SAFETY: caller guarantees the running actor's context.
246 Err(message) => unsafe { panic_with(ctx, &message) },
247 }
248 }
249
250 /// HKDF-SHA-256 of `secret` with `salt` and `info`, `length` bytes, as
251 /// `Bytes`; panics on a length outside 0 to 8160.
252 ///
253 /// # Safety
254 /// `secret`, `salt` and `info` must be live `Bytes`, `length` a live
255 /// `Int`, and `ctx` the running actor's context.
256 #[unsafe(no_mangle)]
257 pub unsafe extern "C" fn tl_crypto_hkdf_sha256(
258 ctx: *mut Ctx,
259 secret: Value,
260 salt: Value,
261 info: Value,
262 length: Value,
263 ) -> Value {
264 // SAFETY: caller guarantees live Bytes and an Int.
265 let derived = unsafe {
266 louise_rt_prims::crypto::hkdf_sha256(
267 bytes_of(secret),
268 bytes_of(salt),
269 bytes_of(info),
270 saturating(length),
271 )
272 };
273 match derived {
274 Ok(derived) => bytes_from(ctx, &derived),
275 // SAFETY: caller guarantees the running actor's context.
276 Err(message) => unsafe { panic_with(ctx, &message) },
277 }
278 }
279}