|
| 1 | +//! Benches of non crypto logic like signature base extraction or serialization. |
| 2 | +
|
| 3 | +use criterion::{Criterion, criterion_group, criterion_main}; |
| 4 | +use std::hint::black_box; |
| 5 | + |
| 6 | +use httpsig::prelude::{ |
| 7 | + AlgorithmName, HttpSignatureBase, HttpSignatureHeaders, HttpSignatureParams, SecretKey, message_component::HttpMessageComponent, |
| 8 | +}; |
| 9 | + |
| 10 | +const COMPONENT_LINES: &[&str] = &[ |
| 11 | + r##""date": Tue, 20 Apr 2021 02:07:55 GMT"##, |
| 12 | + r##""@method": POST"##, |
| 13 | + r##""@path": /foo"##, |
| 14 | + r##""@authority": example.com"##, |
| 15 | + r##""content-type": application/json"##, |
| 16 | + r##""content-length": 18"##, |
| 17 | +]; |
| 18 | +const SIGNATURE_PARAMS: &str = |
| 19 | + r##"("date" "@method" "@path" "@authority" "content-type" "content-length");created=1618884473;keyid="test-key-ed25519""##; |
| 20 | + |
| 21 | +fn construct_signature_base(signature_params: &str, component_lines: &[&str]) -> HttpSignatureBase { |
| 22 | + let signature_params = HttpSignatureParams::try_from(signature_params).unwrap(); |
| 23 | + let component_lines = component_lines |
| 24 | + .iter() |
| 25 | + .map(|&line| HttpMessageComponent::try_from(line).unwrap()) |
| 26 | + .collect::<Vec<_>>(); |
| 27 | + HttpSignatureBase::try_new(component_lines, &signature_params).unwrap() |
| 28 | +} |
| 29 | + |
| 30 | +fn parse_signature_base(signature_input_header: &str, signature_header: &str, component_lines: &[&str]) -> HttpSignatureBase { |
| 31 | + let header_map = HttpSignatureHeaders::try_parse(signature_header, signature_input_header).unwrap(); |
| 32 | + let received_signature_headers = header_map.get("sig-b26").unwrap(); |
| 33 | + let component_lines = component_lines |
| 34 | + .iter() |
| 35 | + .map(|&line| HttpMessageComponent::try_from(line).unwrap()) |
| 36 | + .collect::<Vec<_>>(); |
| 37 | + HttpSignatureBase::try_new(component_lines, received_signature_headers.signature_params()).unwrap() |
| 38 | +} |
| 39 | + |
| 40 | +/* ----------------------------------------------------------------- */ |
| 41 | +// params from https://datatracker.ietf.org/doc/html/rfc9421#name-signing-a-request-using-ed2 |
| 42 | +const EDDSA_SECRET_KEY: &str = r##"-----BEGIN PRIVATE KEY----- |
| 43 | +MC4CAQAwBQYDK2VwBCIEIJ+DYvh6SEqVTm50DFtMDoQikTmiCqirVv9mWG9qfSnF |
| 44 | +-----END PRIVATE KEY----- |
| 45 | +"##; |
| 46 | + |
| 47 | +fn setup_signature_input() -> (String, String, &'static [&'static str]) { |
| 48 | + let component_lines = COMPONENT_LINES |
| 49 | + .iter() |
| 50 | + .map(|&line| HttpMessageComponent::try_from(line).unwrap()) |
| 51 | + .collect::<Vec<_>>(); |
| 52 | + |
| 53 | + // sender |
| 54 | + let signature_params = HttpSignatureParams::try_from(SIGNATURE_PARAMS).unwrap(); |
| 55 | + let signature_base = HttpSignatureBase::try_new(component_lines, &signature_params).unwrap(); |
| 56 | + let sk = SecretKey::from_pem(&AlgorithmName::Ed25519, EDDSA_SECRET_KEY).unwrap(); |
| 57 | + let signature_headers = signature_base.build_signature_headers(&sk, Some("sig-b26")).unwrap(); |
| 58 | + ( |
| 59 | + signature_headers.signature_input_header_value(), |
| 60 | + signature_headers.signature_header_value(), |
| 61 | + COMPONENT_LINES, |
| 62 | + ) |
| 63 | +} |
| 64 | + |
| 65 | +fn construct_parse(c: &mut Criterion) { |
| 66 | + c.bench_function("sig base try_new", |b| { |
| 67 | + b.iter(|| construct_signature_base(black_box(SIGNATURE_PARAMS), black_box(COMPONENT_LINES))) |
| 68 | + }); |
| 69 | + c.bench_function("sig base try_parse", |b| { |
| 70 | + b.iter_batched_ref( |
| 71 | + || setup_signature_input(), |
| 72 | + |(signature_input_header, signature_header, component_lines)| { |
| 73 | + parse_signature_base(signature_input_header, signature_header, component_lines) |
| 74 | + }, |
| 75 | + criterion::BatchSize::SmallInput, |
| 76 | + ) |
| 77 | + }); |
| 78 | +} |
| 79 | + |
| 80 | +criterion_group!(benches, construct_parse); |
| 81 | +criterion_main!(benches); |
0 commit comments