base64-ng 1.3.3

no_std-first Base64 encoding and decoding with strict APIs and a security-heavy release process
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
use super::*;

struct DispatchFallbackAlphabet;

impl Alphabet for DispatchFallbackAlphabet {
    const ENCODE: [u8; 64] = *b"./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    fn decode(byte: u8) -> Option<u8> {
        decode_alphabet_byte(byte, &Self::ENCODE)
    }
}

fn fill_pattern(output: &mut [u8], seed: usize) {
    for (index, byte) in output.iter_mut().enumerate() {
        let value = (index * 73 + seed * 19) % 256;
        *byte = u8::try_from(value).unwrap();
    }
}

fn assert_encode_backend_matches_scalar<A, const PAD: bool>(input: &[u8])
where
    A: Alphabet,
{
    let engine = Engine::<A, PAD>::new();
    let mut dispatched = [0x55; 256];
    let mut scalar = [0xaa; 256];

    let dispatched_result = engine.encode_slice(input, &mut dispatched);
    let scalar_result = scalar::scalar_reference_encode_slice::<A, PAD>(input, &mut scalar);

    assert_eq!(dispatched_result, scalar_result);
    if let Ok(written) = dispatched_result {
        assert_eq!(&dispatched[..written], &scalar[..written]);
    }

    let required = checked_encoded_len(input.len(), PAD).unwrap();
    if required > 0 {
        let mut dispatched_short = [0x55; 256];
        let mut scalar_short = [0xaa; 256];
        let available = required - 1;

        assert_eq!(
            engine.encode_slice(input, &mut dispatched_short[..available]),
            scalar::scalar_reference_encode_slice::<A, PAD>(input, &mut scalar_short[..available],)
        );
    }
}

fn assert_decode_backend_matches_scalar<A, const PAD: bool>(input: &[u8])
where
    A: Alphabet,
{
    let engine = Engine::<A, PAD>::new();
    let mut dispatched = [0x55; 128];
    let mut scalar = [0xaa; 128];

    let dispatched_result = engine.decode_slice(input, &mut dispatched);
    let scalar_result = scalar::scalar_reference_decode_slice::<A, PAD>(input, &mut scalar);

    assert_eq!(dispatched_result, scalar_result);
    if let Ok(written) = dispatched_result {
        assert_eq!(&dispatched[..written], &scalar[..written]);

        if written > 0 {
            let mut dispatched_short = [0x55; 128];
            let mut scalar_short = [0xaa; 128];
            let available = written - 1;

            assert_eq!(
                engine.decode_slice(input, &mut dispatched_short[..available]),
                scalar::scalar_reference_decode_slice::<A, PAD>(
                    input,
                    &mut scalar_short[..available],
                )
            );
        }
    }
}

fn assert_strict_decode_public_surfaces_match_scalar<A, const PAD: bool>(input: &[u8])
where
    A: Alphabet,
{
    let engine = Engine::<A, PAD>::new();
    let mut slice_output = [0x55; 128];
    let mut clear_tail_output = [0xaa; 128];
    let mut scalar_output = [0xcc; 128];

    let scalar_result = scalar::scalar_reference_decode_slice::<A, PAD>(input, &mut scalar_output);
    let slice_result = engine.decode_slice(input, &mut slice_output);
    let clear_tail_result = engine.decode_slice_clear_tail(input, &mut clear_tail_output);
    let buffer_result: Result<DecodedBuffer<128>, DecodeError> = engine.decode_buffer(input);

    assert_eq!(slice_result, scalar_result);
    assert_eq!(clear_tail_result, scalar_result);
    match slice_result {
        Ok(written) => {
            assert_eq!(&slice_output[..written], &scalar_output[..written]);
            assert_eq!(&clear_tail_output[..written], &scalar_output[..written]);
            assert!(clear_tail_output[written..].iter().all(|byte| *byte == 0));
            let buffer = buffer_result.unwrap();
            assert_eq!(buffer.as_bytes(), &scalar_output[..written]);
        }
        Err(err) => {
            assert!(clear_tail_output.iter().all(|byte| *byte == 0));
            assert_eq!(buffer_result.unwrap_err(), err);
        }
    }
}

fn assert_backend_round_trip_matches_scalar<A, const PAD: bool>(input: &[u8])
where
    A: Alphabet,
{
    assert_encode_backend_matches_scalar::<A, PAD>(input);

    let mut encoded = [0; 256];
    let encoded_len = scalar::scalar_reference_encode_slice::<A, PAD>(input, &mut encoded).unwrap();
    assert_decode_backend_matches_scalar::<A, PAD>(&encoded[..encoded_len]);
}

fn assert_standard_decode_chunk_matches_input(input: &[u8]) {
    let mut encoded = [0u8; 4];
    let encoded_len = STANDARD.encode_slice(input, &mut encoded).unwrap();
    assert_eq!(encoded_len, 4);

    let chunk = [encoded[0], encoded[1], encoded[2], encoded[3]];
    let mut decoded = [0u8; 3];
    let decoded_len = decode_chunk::<Standard, true>(chunk, &mut decoded).unwrap();

    assert_eq!(decoded_len, input.len());
    assert_eq!(&decoded[..decoded_len], input);
}

#[test]
fn backend_dispatch_matches_scalar_reference_for_canonical_inputs() {
    let mut input = [0; 128];

    for input_len in 0..=input.len() {
        fill_pattern(&mut input[..input_len], input_len);
        let input = &input[..input_len];

        assert_backend_round_trip_matches_scalar::<Standard, true>(input);
        assert_backend_round_trip_matches_scalar::<Standard, false>(input);
        assert_backend_round_trip_matches_scalar::<UrlSafe, true>(input);
        assert_backend_round_trip_matches_scalar::<UrlSafe, false>(input);
        assert_backend_round_trip_matches_scalar::<DispatchFallbackAlphabet, true>(input);
        assert_backend_round_trip_matches_scalar::<DispatchFallbackAlphabet, false>(input);
    }
}

#[test]
fn backend_dispatch_matches_scalar_reference_for_malformed_inputs() {
    for input in [
        &b"Z"[..],
        b"====",
        b"AA=A",
        b"Zh==",
        b"Zm9=",
        b"Zm9v$g==",
        b"Zm9vZh==",
    ] {
        assert_decode_backend_matches_scalar::<Standard, true>(input);
    }

    for input in [&b"Z"[..], b"AA=A", b"Zh", b"Zm9", b"Zm9vYg$"] {
        assert_decode_backend_matches_scalar::<Standard, false>(input);
    }

    assert_decode_backend_matches_scalar::<UrlSafe, true>(b"AA+A");
    assert_decode_backend_matches_scalar::<UrlSafe, false>(b"AA/A");
    assert_decode_backend_matches_scalar::<Standard, true>(b"AA-A");
    assert_decode_backend_matches_scalar::<Standard, false>(b"AA_A");
}

#[test]
fn strict_decode_reports_invalid_byte_positions_exhaustively() {
    for index in 0..8 {
        let mut input = *b"Zm9vYmFy";
        input[index] = b'!';
        let mut output = [0; 6];
        assert_eq!(
            STANDARD.decode_slice(&input, &mut output),
            Err(DecodeError::InvalidByte { index, byte: b'!' })
        );
        assert_decode_backend_matches_scalar::<Standard, true>(&input);
    }

    for index in 0..8 {
        let mut input = *b"Zm9vYmFy";
        input[index] = b'/';
        let mut output = [0; 6];
        assert_eq!(
            URL_SAFE.decode_slice(&input, &mut output),
            Err(DecodeError::InvalidByte { index, byte: b'/' })
        );
        assert_decode_backend_matches_scalar::<UrlSafe, true>(&input);
    }
}

#[test]
fn strict_decode_rejects_padding_and_canonicality_matrix() {
    for input in [
        &b"=m9v"[..],
        b"Z=9v",
        b"Zm=v",
        b"Zm9v=AAA",
        b"Zh==",
        b"Zi==",
        b"Zm9=",
        b"Zm+=",
    ] {
        assert_decode_backend_matches_scalar::<Standard, true>(input);
        assert!(STANDARD.decode_buffer::<8>(input).is_err());
    }

    for input in [&b"Z"[..], b"Zh", b"Zi", b"Zm9", b"Zm+"] {
        assert_decode_backend_matches_scalar::<Standard, false>(input);
        assert!(STANDARD_NO_PAD.decode_buffer::<8>(input).is_err());
    }
}

#[test]
fn strict_decode_public_surfaces_match_scalar_reference() {
    for input in [
        &b""[..],
        b"Zg==",
        b"Zm8=",
        b"Zm9v",
        b"Zm9vYg==",
        b"Zm9vYmE=",
        b"Zm9vYmFy",
        b"////",
        b"AAEC",
    ] {
        assert_strict_decode_public_surfaces_match_scalar::<Standard, true>(input);
    }

    for input in [
        &b""[..],
        b"Zg",
        b"Zm8",
        b"Zm9v",
        b"Zm9vYg",
        b"Zm9vYmE",
        b"Zm9vYmFy",
        b"____",
        b"AAEC",
    ] {
        assert_strict_decode_public_surfaces_match_scalar::<UrlSafe, false>(input);
    }

    for input in [
        &b"Z"[..],
        b"====",
        b"AA=A",
        b"Zh==",
        b"Zm9=",
        b"Zm9v$g==",
        b"Zm9vZh==",
        b"AA-A",
    ] {
        assert_strict_decode_public_surfaces_match_scalar::<Standard, true>(input);
    }

    for input in [&b"Z"[..], b"AA=A", b"Zh", b"Zm9", b"Zm9vYg$", b"AA/A"] {
        assert_strict_decode_public_surfaces_match_scalar::<UrlSafe, false>(input);
    }
}

#[test]
fn decode_chunk_bit_packing_matches_exhaustive_small_inputs() {
    for byte in u8::MIN..=u8::MAX {
        assert_standard_decode_chunk_matches_input(&[byte]);
    }

    for first in u8::MIN..=u8::MAX {
        for second in u8::MIN..=u8::MAX {
            assert_standard_decode_chunk_matches_input(&[first, second]);
        }
    }
}

#[test]
fn decode_chunk_bit_packing_matches_representative_full_quanta() {
    const SAMPLES: [u8; 16] = [
        0, 1, 2, 15, 16, 31, 32, 63, 64, 95, 127, 128, 191, 192, 254, 255,
    ];

    for first in SAMPLES {
        for second in SAMPLES {
            for third in SAMPLES {
                assert_standard_decode_chunk_matches_input(&[first, second, third]);
            }
        }
    }
}

#[test]
fn ct_padded_final_quantum_fails_closed_for_invalid_padding_count() {
    let (_, invalid_byte, invalid_padding, written) =
        ct_padded_final_quantum::<Standard>(*b"ABCD", 3);

    assert_ne!(invalid_byte, 0);
    assert_ne!(invalid_padding, 0);
    assert_eq!(written, 0);
    assert_eq!(
        report_ct_error(invalid_byte, invalid_padding),
        Err(DecodeError::InvalidInput)
    );
}

#[cfg(feature = "simd")]
#[test]
fn simd_dispatch_uses_only_admitted_backends() {
    match simd::active_backend() {
        simd::ActiveBackend::Scalar => {}
        #[cfg(all(feature = "std", any(target_arch = "x86", target_arch = "x86_64")))]
        simd::ActiveBackend::Avx512Vbmi => {
            assert!(matches!(
                simd::detected_candidate(),
                simd::Candidate::Avx512Vbmi
            ));
        }
        #[cfg(all(feature = "std", any(target_arch = "x86", target_arch = "x86_64")))]
        simd::ActiveBackend::Avx2 => {
            assert!(matches!(
                simd::detected_candidate(),
                simd::Candidate::Avx2 | simd::Candidate::Avx512Vbmi
            ));
        }
        #[cfg(all(feature = "std", any(target_arch = "x86", target_arch = "x86_64")))]
        simd::ActiveBackend::Ssse3Sse41 => {
            assert!(matches!(
                simd::detected_candidate(),
                simd::Candidate::Ssse3Sse41 | simd::Candidate::Avx2 | simd::Candidate::Avx512Vbmi
            ));
        }
        #[cfg(all(feature = "std", target_arch = "aarch64", target_endian = "little"))]
        simd::ActiveBackend::Neon => {
            assert!(matches!(simd::detected_candidate(), simd::Candidate::Neon));
        }
        #[cfg(all(feature = "simd", target_arch = "wasm32"))]
        simd::ActiveBackend::WasmSimd128 => {
            assert!(matches!(
                simd::detected_candidate(),
                simd::Candidate::WasmSimd128
            ));
        }
    }
}

#[test]
fn encode_backend_boundary_uses_only_admitted_backends() {
    match encode_backend::active_encode_backend() {
        encode_backend::EncodeBackend::Scalar => {}
        #[cfg(all(
            feature = "simd",
            feature = "std",
            any(target_arch = "x86", target_arch = "x86_64")
        ))]
        encode_backend::EncodeBackend::Avx512Vbmi => {
            assert!(simd::avx512_supports_alphabet::<Standard>());
            assert!(simd::avx512_supports_alphabet::<UrlSafe>());
        }
        #[cfg(all(
            feature = "simd",
            feature = "std",
            any(target_arch = "x86", target_arch = "x86_64")
        ))]
        encode_backend::EncodeBackend::Avx2 => {
            assert!(simd::avx2_supports_alphabet::<Standard>());
            assert!(simd::avx2_supports_alphabet::<UrlSafe>());
        }
        #[cfg(all(
            feature = "simd",
            feature = "std",
            any(target_arch = "x86", target_arch = "x86_64")
        ))]
        encode_backend::EncodeBackend::Ssse3Sse41 => {
            assert!(simd::ssse3_sse41_supports_alphabet::<Standard>());
            assert!(simd::ssse3_sse41_supports_alphabet::<UrlSafe>());
        }
        #[cfg(all(
            feature = "simd",
            feature = "std",
            target_arch = "aarch64",
            target_endian = "little"
        ))]
        encode_backend::EncodeBackend::Neon => {
            assert!(simd::neon_supports_alphabet::<Standard>());
            assert!(simd::neon_supports_alphabet::<UrlSafe>());
        }
        #[cfg(all(feature = "simd", target_arch = "wasm32"))]
        encode_backend::EncodeBackend::WasmSimd128 => {
            assert!(simd::wasm_simd128_supports_alphabet::<Standard>());
            assert!(simd::wasm_simd128_supports_alphabet::<UrlSafe>());
        }
    }
}

#[test]
fn encodes_standard_vectors() {
    let vectors = [
        (&b""[..], &b""[..]),
        (&b"f"[..], &b"Zg=="[..]),
        (&b"fo"[..], &b"Zm8="[..]),
        (&b"foo"[..], &b"Zm9v"[..]),
        (&b"foob"[..], &b"Zm9vYg=="[..]),
        (&b"fooba"[..], &b"Zm9vYmE="[..]),
        (&b"foobar"[..], &b"Zm9vYmFy"[..]),
    ];
    for (input, expected) in vectors {
        let mut output = [0u8; 16];
        let written = STANDARD.encode_slice(input, &mut output).unwrap();
        assert_eq!(&output[..written], expected);
    }
}

#[test]
fn decodes_standard_vectors() {
    let vectors = [
        (&b""[..], &b""[..]),
        (&b"Zg=="[..], &b"f"[..]),
        (&b"Zm8="[..], &b"fo"[..]),
        (&b"Zm9v"[..], &b"foo"[..]),
        (&b"Zm9vYg=="[..], &b"foob"[..]),
        (&b"Zm9vYmE="[..], &b"fooba"[..]),
        (&b"Zm9vYmFy"[..], &b"foobar"[..]),
    ];
    for (input, expected) in vectors {
        let mut output = [0u8; 16];
        let written = STANDARD.decode_slice(input, &mut output).unwrap();
        assert_eq!(&output[..written], expected);
    }
}

#[test]
fn supports_unpadded_url_safe() {
    let mut encoded = [0u8; 16];
    let written = URL_SAFE_NO_PAD
        .encode_slice(b"\xfb\xff", &mut encoded)
        .unwrap();
    assert_eq!(&encoded[..written], b"-_8");

    let mut decoded = [0u8; 2];
    let written = URL_SAFE_NO_PAD
        .decode_slice(&encoded[..written], &mut decoded)
        .unwrap();
    assert_eq!(&decoded[..written], b"\xfb\xff");
}

#[test]
fn decodes_in_place() {
    let mut buffer = *b"Zm9vYmFy";
    let decoded = STANDARD_NO_PAD.decode_in_place(&mut buffer).unwrap();
    assert_eq!(decoded, b"foobar");
}

#[test]
fn rejects_non_canonical_padding_bits() {
    let mut output = [0u8; 4];
    assert_eq!(
        STANDARD.decode_slice(b"Zh==", &mut output),
        Err(DecodeError::InvalidPadding { index: 1 })
    );
    assert_eq!(
        STANDARD.decode_slice(b"Zm9=", &mut output),
        Err(DecodeError::InvalidPadding { index: 2 })
    );
}