openpgp_card/ocard/data/
algo_attrs.rs1use std::convert::TryFrom;
7
8use nom::branch::alt;
9use nom::bytes::complete::tag;
10use nom::combinator::map;
11use nom::{branch, bytes::complete as bytes, number::complete as number};
12
13use crate::ocard::algorithm::{AlgorithmAttributes, Curve, EccAttributes, RsaAttributes};
14use crate::ocard::crypto::EccType;
15use crate::ocard::data::complete;
16
17fn parse_oid_cv25519(input: &[u8]) -> nom::IResult<&[u8], Curve> {
18 map(tag(Curve::Curve25519.oid()), |_| Curve::Curve25519)(input)
19}
20
21fn parse_oid_ed25519(input: &[u8]) -> nom::IResult<&[u8], Curve> {
22 map(tag(Curve::Ed25519.oid()), |_| Curve::Ed25519)(input)
23}
24
25fn parse_oid_secp256k1(input: &[u8]) -> nom::IResult<&[u8], Curve> {
26 map(tag(Curve::Secp256k1.oid()), |_| Curve::Secp256k1)(input)
27}
28
29fn parse_oid_nist256(input: &[u8]) -> nom::IResult<&[u8], Curve> {
30 map(tag(Curve::NistP256r1.oid()), |_| Curve::NistP256r1)(input)
31}
32
33fn parse_oid_nist384(input: &[u8]) -> nom::IResult<&[u8], Curve> {
34 map(tag(Curve::NistP384r1.oid()), |_| Curve::NistP384r1)(input)
35}
36
37fn parse_oid_nist521(input: &[u8]) -> nom::IResult<&[u8], Curve> {
38 map(tag(Curve::NistP521r1.oid()), |_| Curve::NistP521r1)(input)
39}
40
41fn parse_oid_brainpool_p256r1(input: &[u8]) -> nom::IResult<&[u8], Curve> {
42 map(tag(Curve::BrainpoolP256r1.oid()), |_| {
43 Curve::BrainpoolP256r1
44 })(input)
45}
46
47fn parse_oid_brainpool_p384r1(input: &[u8]) -> nom::IResult<&[u8], Curve> {
48 map(tag(Curve::BrainpoolP384r1.oid()), |_| {
49 Curve::BrainpoolP384r1
50 })(input)
51}
52
53fn parse_oid_brainpool_p512r1(input: &[u8]) -> nom::IResult<&[u8], Curve> {
54 map(tag(Curve::BrainpoolP512r1.oid()), |_| {
55 Curve::BrainpoolP512r1
56 })(input)
57}
58
59fn parse_oid_ed448(input: &[u8]) -> nom::IResult<&[u8], Curve> {
60 map(tag(Curve::Ed448.oid()), |_| Curve::Ed448)(input)
61}
62
63fn parse_oid_x448(input: &[u8]) -> nom::IResult<&[u8], Curve> {
64 map(tag(Curve::X448.oid()), |_| Curve::X448)(input)
65}
66
67fn parse_oid(input: &[u8]) -> nom::IResult<&[u8], Curve> {
68 alt((
69 parse_oid_nist256,
70 parse_oid_nist384,
71 parse_oid_nist521,
72 parse_oid_brainpool_p256r1,
73 parse_oid_brainpool_p384r1,
74 parse_oid_brainpool_p512r1,
75 parse_oid_secp256k1,
76 parse_oid_ed25519,
77 parse_oid_cv25519,
78 parse_oid_ed448,
79 parse_oid_x448,
80 ))(input)
81}
82
83fn parse_rsa(input: &[u8]) -> nom::IResult<&[u8], AlgorithmAttributes> {
84 let (input, _) = bytes::tag([0x01])(input)?;
85
86 let (input, len_n) = number::be_u16(input)?;
87 let (input, len_e) = number::be_u16(input)?;
88 let (input, import_format) = number::u8(input)?;
89
90 Ok((
91 input,
92 AlgorithmAttributes::Rsa(RsaAttributes::new(len_n, len_e, import_format)),
93 ))
94}
95
96fn parse_import_format(input: &[u8]) -> nom::IResult<&[u8], Option<u8>> {
97 let (input, b) = bytes::take(1usize)(input)?;
98 Ok((input, Some(b[0])))
99}
100
101fn default_import_format(input: &[u8]) -> nom::IResult<&[u8], Option<u8>> {
102 Ok((input, None))
103}
104
105fn parse_ecdh(input: &[u8]) -> nom::IResult<&[u8], AlgorithmAttributes> {
106 let (input, _) = bytes::tag([0x12])(input)?;
107 let (input, curve) = parse_oid(input)?;
108
109 let (input, import_format) = alt((parse_import_format, default_import_format))(input)?;
110
111 Ok((
112 input,
113 AlgorithmAttributes::Ecc(EccAttributes::new(EccType::ECDH, curve, import_format)),
114 ))
115}
116
117fn parse_ecdsa(input: &[u8]) -> nom::IResult<&[u8], AlgorithmAttributes> {
118 let (input, _) = bytes::tag([0x13])(input)?;
119 let (input, curve) = parse_oid(input)?;
120
121 let (input, import_format) = alt((parse_import_format, default_import_format))(input)?;
122
123 Ok((
124 input,
125 AlgorithmAttributes::Ecc(EccAttributes::new(EccType::ECDSA, curve, import_format)),
126 ))
127}
128
129fn parse_eddsa(input: &[u8]) -> nom::IResult<&[u8], AlgorithmAttributes> {
130 let (input, _) = bytes::tag([0x16])(input)?;
131 let (input, curve) = parse_oid(input)?;
132
133 let (input, import_format) = alt((parse_import_format, default_import_format))(input)?;
134
135 Ok((
136 input,
137 AlgorithmAttributes::Ecc(EccAttributes::new(EccType::EdDSA, curve, import_format)),
138 ))
139}
140
141pub(crate) fn parse(input: &[u8]) -> nom::IResult<&[u8], AlgorithmAttributes> {
142 branch::alt((parse_rsa, parse_ecdsa, parse_eddsa, parse_ecdh))(input)
143}
144
145impl TryFrom<&[u8]> for AlgorithmAttributes {
146 type Error = crate::Error;
147
148 fn try_from(data: &[u8]) -> Result<Self, crate::Error> {
149 complete(parse(data))
150 }
151}
152
153