cryptography-rs 0.6.2

Block ciphers, hashes, public-key, and post-quantum primitives implemented directly from their specifications and original papers.
Documentation
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
//! Internal helpers for crate-defined public-key serialization.
//!
//! RSA uses standards-based containers in `rsa_io`. The other public-key
//! schemes do not have an equally universal interchange format for the exact
//! primitive forms exposed here, so they use two deliberately simple
//! crate-defined formats:
//! - a binary DER `SEQUENCE` of positive `INTEGER`s
//! - a flat XML document whose root tag is the Rust type name and whose child
//!   elements are fixed-schema big integers rendered as uppercase hexadecimal
//!   with no `0x` prefix
//!
//! PEM text armor for the non-RSA schemes wraps the DER body, not the XML
//! form. The XML form is a convenience export that mirrors the in-memory
//! structs closely enough to audit side-by-side with the binary encoding.
//!
//! The payload is intentionally "RSA-like" in shape: just the key components
//! encoded in a fixed field order, without pretending that these schemes have
//! PKCS / X.509 object identifiers. The current field layouts are:
//!
//! - `CocksPublicKey`: `[n]`
//! - `CocksPrivateKey`: `[pi, q]`
//! - `DsaPublicKey`: `[p, q, g, y]`
//! - `DsaPrivateKey`: `[p, q, g, x]`
//! - `ElGamalPublicKey`: `[p, exponent_bound, g, b]`
//! - `ElGamalPrivateKey`: `[p, exponent_modulus, a]`
//! - `PaillierPublicKey`: `[n, zeta]`
//! - `PaillierPrivateKey`: `[n, lambda, u]`
//! - `RabinPublicKey`: `[n]`
//! - `RabinPrivateKey`: `[n, p, q]`
//! - `SchmidtSamoaPublicKey`: `[n]`
//! - `SchmidtSamoaPrivateKey`: `[d, gamma]`
//!
//! The PEM label selects the scheme and key role. The DER body is shared.
//! Bare DER blobs are intentionally schema-shaped rather than self-describing:
//! types with the same field count can therefore share identical binary
//! encodings. The PEM labels and XML root tags are the type discriminants when
//! callers need a tagged interchange format.

use crate::public_key::bigint::BigUint;
use quick_xml::events::{BytesEnd, BytesStart, BytesText, Event};
use quick_xml::{Reader, Writer};

const UPPER_HEX: &[u8; 16] = b"0123456789ABCDEF";

pub(crate) fn encode_biguints(fields: &[&BigUint]) -> Vec<u8> {
    let mut body = Vec::new();
    for field in fields {
        body.push(0x02);
        let bytes = der_integer_bytes(field);
        encode_der_len(bytes.len(), &mut body);
        body.extend_from_slice(&bytes);
    }

    let mut out = Vec::new();
    out.push(0x30);
    encode_der_len(body.len(), &mut out);
    out.extend_from_slice(&body);
    out
}

pub(crate) fn decode_biguints(input: &[u8]) -> Option<Vec<BigUint>> {
    let (seq_tag, rest) = input.split_first()?;
    if *seq_tag != 0x30 {
        return None;
    }

    let (seq_len, mut pos) = decode_der_len(rest)?;
    // Use checked_add: seq_len can be usize::MAX with crafted input (eight
    // 0xff length bytes), and adding pos to it would otherwise overflow.
    if pos.checked_add(seq_len) != Some(rest.len()) {
        return None;
    }

    let mut out = Vec::new();
    while pos < rest.len() {
        let int_tag = *rest.get(pos)?;
        pos += 1;
        if int_tag != 0x02 {
            return None;
        }

        let (len, len_len) = decode_der_len(rest.get(pos..)?)?;
        pos += len_len;
        // pos + len can overflow when len ≈ usize::MAX; use checked_add.
        let end = pos.checked_add(len)?;
        let field = rest.get(pos..end)?;
        pos = end;
        out.push(decode_der_biguint(field)?);
    }

    Some(out)
}

fn der_integer_bytes(value: &BigUint) -> Vec<u8> {
    let mut bytes = value.to_be_bytes();
    // DER INTEGER uses signed two's-complement, so a leading 1 bit would make
    // the value look negative. Prepend a zero byte to mark the encoding as a
    // positive integer.
    if bytes.first().is_some_and(|byte| byte & 0x80 != 0) {
        bytes.insert(0, 0);
    }
    bytes
}

fn decode_der_biguint(field: &[u8]) -> Option<BigUint> {
    if field.is_empty() {
        return None;
    }
    // Negative DER INTEGER encodings are not valid for these key fields.
    if field[0] & 0x80 != 0 {
        return None;
    }

    let body = if field.len() > 1 && field[0] == 0 {
        // DER requires the shortest positive encoding: a leading zero is
        // allowed only when it prevents the next byte from being interpreted
        // as a sign bit.
        if field[1] & 0x80 == 0 {
            return None;
        }
        &field[1..]
    } else {
        field
    };

    Some(BigUint::from_be_bytes(body))
}

fn encode_der_len(len: usize, out: &mut Vec<u8>) {
    if len < 0x80 {
        // Short-form DER length.
        out.push(u8::try_from(len).expect("short DER length fits in u8"));
        return;
    }

    // Long-form DER length: high bit set, remaining bits = number of length
    // octets that follow.
    let be = len.to_be_bytes();
    let first_nonzero = be
        .iter()
        .position(|&byte| byte != 0)
        .expect("non-zero length has at least one non-zero byte");
    let len_bytes = &be[first_nonzero..];
    out.push(0x80 | u8::try_from(len_bytes.len()).expect("DER length-of-length fits in u8"));
    out.extend_from_slice(len_bytes);
}

fn decode_der_len(input: &[u8]) -> Option<(usize, usize)> {
    let first = *input.first()?;
    if first & 0x80 == 0 {
        // Short-form DER length.
        return Some((usize::from(first), 1));
    }

    // Long-form DER length. The low 7 bits say how many big-endian length
    // octets follow.
    let count = usize::from(first & 0x7f);
    if count == 0 {
        return None;
    }
    let len_bytes = input.get(1..1 + count)?;
    let mut len = 0usize;
    for &byte in len_bytes {
        len = len.checked_shl(8)?.checked_add(usize::from(byte))?;
    }
    Some((len, 1 + count))
}

pub(crate) fn pem_wrap(label: &str, blob: &[u8]) -> String {
    let b64 = base64_encode(blob);
    let mut out = String::new();
    out.push_str("-----BEGIN ");
    out.push_str(label);
    out.push_str("-----\n");

    let mut idx = 0usize;
    while idx < b64.len() {
        let end = (idx + 64).min(b64.len());
        out.push_str(&b64[idx..end]);
        out.push('\n');
        idx = end;
    }

    out.push_str("-----END ");
    out.push_str(label);
    out.push_str("-----\n");
    out
}

pub(crate) fn pem_unwrap(label: &str, pem: &str) -> Option<Vec<u8>> {
    let begin = format!("-----BEGIN {label}-----");
    let end = format!("-----END {label}-----");
    let mut lines = pem.lines();
    if lines.next()? != begin {
        return None;
    }

    let mut b64 = String::new();
    for line in lines {
        if line == end {
            return base64_decode(&b64);
        }
        if !line.is_empty() {
            b64.push_str(line.trim());
        }
    }
    None
}

/// Encode the crate's flat XML representation for one key or ciphertext.
///
/// `root` is the outer element name and `fields` supplies the fixed child
/// elements in order.
pub(crate) fn xml_wrap(root: &str, fields: &[(&str, &BigUint)]) -> String {
    let mut writer = Writer::new(Vec::new());
    writer
        .write_event(Event::Start(BytesStart::new(root)))
        .expect("in-memory XML write cannot fail");

    for (name, value) in fields {
        writer
            .write_event(Event::Start(BytesStart::new(*name)))
            .expect("in-memory XML write cannot fail");
        writer
            .write_event(Event::Text(BytesText::new(&hex_encode_upper(value))))
            .expect("in-memory XML write cannot fail");
        writer
            .write_event(Event::End(BytesEnd::new(*name)))
            .expect("in-memory XML write cannot fail");
    }

    writer
        .write_event(Event::End(BytesEnd::new(root)))
        .expect("in-memory XML write cannot fail");

    String::from_utf8(writer.into_inner()).expect("XML output is valid UTF-8")
}

/// Parse the crate's flat XML representation for one key or ciphertext.
///
/// The parser is intentionally strict: the root tag must match, the field
/// names must appear in the expected order, and extra trailing content is
/// rejected.
pub(crate) fn xml_unwrap(root: &str, field_names: &[&str], xml: &str) -> Option<Vec<BigUint>> {
    let mut reader = Reader::from_str(xml);
    reader.config_mut().trim_text(true);

    let mut buf = Vec::new();

    match reader.read_event_into(&mut buf).ok()? {
        Event::Start(start) if start.name().as_ref() == root.as_bytes() => {
            if start.attributes().with_checks(false).next().is_some() {
                return None;
            }
        }
        _ => return None,
    }
    buf.clear();

    let mut out = Vec::with_capacity(field_names.len());
    for &field_name in field_names {
        match reader.read_event_into(&mut buf).ok()? {
            Event::Start(start) if start.name().as_ref() == field_name.as_bytes() => {
                if start.attributes().with_checks(false).next().is_some() {
                    return None;
                }
            }
            _ => return None,
        }
        buf.clear();

        let text = match reader.read_event_into(&mut buf).ok()? {
            Event::Text(text) => text.decode().ok()?.into_owned(),
            _ => return None,
        };
        out.push(hex_decode_biguint(text.trim())?);
        buf.clear();

        match reader.read_event_into(&mut buf).ok()? {
            Event::End(end) if end.name().as_ref() == field_name.as_bytes() => {}
            _ => return None,
        }
        buf.clear();
    }

    match reader.read_event_into(&mut buf).ok()? {
        Event::End(end) if end.name().as_ref() == root.as_bytes() => {}
        _ => return None,
    }
    buf.clear();

    match reader.read_event_into(&mut buf).ok()? {
        Event::Eof => Some(out),
        _ => None,
    }
}

fn base64_encode(input: &[u8]) -> String {
    const TABLE: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    let mut out = String::with_capacity(input.len().div_ceil(3) * 4);
    let mut idx = 0usize;
    while idx < input.len() {
        let a = input[idx];
        let b = if idx + 1 < input.len() {
            input[idx + 1]
        } else {
            0
        };
        let c = if idx + 2 < input.len() {
            input[idx + 2]
        } else {
            0
        };

        let triple = (u32::from(a) << 16) | (u32::from(b) << 8) | u32::from(c);
        let i0 = usize::try_from((triple >> 18) & 0x3f).expect("base64 sextet fits usize");
        let i1 = usize::try_from((triple >> 12) & 0x3f).expect("base64 sextet fits usize");
        let i2 = usize::try_from((triple >> 6) & 0x3f).expect("base64 sextet fits usize");
        let i3 = usize::try_from(triple & 0x3f).expect("base64 sextet fits usize");

        out.push(char::from(TABLE[i0]));
        out.push(char::from(TABLE[i1]));
        if idx + 1 < input.len() {
            out.push(char::from(TABLE[i2]));
        } else {
            out.push('=');
        }
        if idx + 2 < input.len() {
            out.push(char::from(TABLE[i3]));
        } else {
            out.push('=');
        }

        idx += 3;
    }
    out
}

fn base64_decode(input: &str) -> Option<Vec<u8>> {
    let bytes = input.as_bytes();
    if !bytes.len().is_multiple_of(4) {
        return None;
    }

    let mut out = Vec::with_capacity((bytes.len() / 4) * 3);
    let mut idx = 0usize;
    while idx < bytes.len() {
        let a = decode_base64_char(bytes[idx])?;
        let b = decode_base64_char(bytes[idx + 1])?;
        let c = if bytes[idx + 2] == b'=' {
            64
        } else {
            decode_base64_char(bytes[idx + 2])?
        };
        let d = if bytes[idx + 3] == b'=' {
            64
        } else {
            decode_base64_char(bytes[idx + 3])?
        };

        let triple = (u32::from(a) << 18)
            | (u32::from(b) << 12)
            | (u32::from(c & 0x3f) << 6)
            | u32::from(d & 0x3f);

        out.push(u8::try_from((triple >> 16) & 0xff).expect("decoded base64 byte fits"));
        if c != 64 {
            out.push(u8::try_from((triple >> 8) & 0xff).expect("decoded base64 byte fits"));
        }
        if d != 64 {
            out.push(u8::try_from(triple & 0xff).expect("decoded base64 byte fits"));
        }

        idx += 4;
    }
    Some(out)
}

fn decode_base64_char(ch: u8) -> Option<u8> {
    match ch {
        b'A'..=b'Z' => Some(ch - b'A'),
        b'a'..=b'z' => Some(ch - b'a' + 26),
        b'0'..=b'9' => Some(ch - b'0' + 52),
        b'+' => Some(62),
        b'/' => Some(63),
        _ => None,
    }
}

fn hex_encode_upper(value: &BigUint) -> String {
    let bytes = value.to_be_bytes();
    let mut out = String::with_capacity(bytes.len() * 2);
    for byte in bytes {
        out.push(char::from(UPPER_HEX[usize::from(byte >> 4)]));
        out.push(char::from(UPPER_HEX[usize::from(byte & 0x0f)]));
    }
    out
}

fn hex_decode_biguint(input: &str) -> Option<BigUint> {
    if input.is_empty() {
        return None;
    }
    // Accept the one-digit zero shorthand for hand-written XML, even though
    // `hex_encode_upper` emits the canonical `00` form.
    if input == "0" {
        return Some(BigUint::zero());
    }
    if !input.len().is_multiple_of(2) {
        return None;
    }

    let bytes = input.as_bytes();
    let mut out = Vec::with_capacity(bytes.len() / 2);
    let mut idx = 0usize;
    while idx < bytes.len() {
        let hi = decode_hex_char(bytes[idx])?;
        let lo = decode_hex_char(bytes[idx + 1])?;
        out.push((hi << 4) | lo);
        idx += 2;
    }
    Some(BigUint::from_be_bytes(&out))
}

fn decode_hex_char(ch: u8) -> Option<u8> {
    match ch {
        b'0'..=b'9' => Some(ch - b'0'),
        b'A'..=b'F' => Some(ch - b'A' + 10),
        b'a'..=b'f' => Some(ch - b'a' + 10),
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::{decode_biguints, encode_biguints, pem_unwrap, pem_wrap, xml_unwrap, xml_wrap};
    use crate::public_key::bigint::BigUint;

    #[test]
    fn binary_roundtrip() {
        let a = BigUint::from_u64(0x1234);
        let b = BigUint::from_u64(0x5678);
        let blob = encode_biguints(&[&a, &b]);
        let parsed = decode_biguints(&blob).expect("parse");
        assert_eq!(parsed, vec![a, b]);
    }

    #[test]
    fn pem_roundtrip() {
        let blob = vec![0, 1, 2, 3, 4, 5];
        let pem = pem_wrap("CRYPTOGRAPHY TEST KEY", &blob);
        let parsed = pem_unwrap("CRYPTOGRAPHY TEST KEY", &pem).expect("pem");
        assert_eq!(parsed, blob);
    }

    #[test]
    fn xml_roundtrip() {
        let a = BigUint::from_u64(0x1234);
        let b = BigUint::from_u64(0xabcd);
        let xml = xml_wrap("TestKey", &[("first", &a), ("second", &b)]);
        let parsed = xml_unwrap("TestKey", &["first", "second"], &xml).expect("xml");
        assert_eq!(parsed, vec![a, b]);
    }

    #[test]
    fn xml_rejects_wrong_root() {
        let xml = "<Wrong><n>BB</n></Wrong>";
        assert!(xml_unwrap("TestKey", &["n"], xml).is_none());
    }

    #[test]
    fn xml_rejects_wrong_field_name() {
        let xml = "<TestKey><wrong>BB</wrong></TestKey>";
        assert!(xml_unwrap("TestKey", &["n"], xml).is_none());
    }

    #[test]
    fn xml_rejects_trailing_content() {
        let xml = "<TestKey><n>BB</n></TestKey>junk";
        assert!(xml_unwrap("TestKey", &["n"], xml).is_none());
    }

    #[test]
    fn xml_rejects_truncated_input() {
        let xml = "<TestKey><n>BB</n>";
        assert!(xml_unwrap("TestKey", &["n"], xml).is_none());
    }
}