use der::{
Reader as _,
asn1::{AnyRef, BitStringRef, ObjectIdentifier, OctetStringRef, UintRef},
};
const OID_PRIME_FIELD: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.2.840.10045.1.1");
const fn hex<const N: usize>(s: &[u8]) -> [u8; N] {
assert!(s.len() == N * 2, "hex literal has the wrong length");
let mut out = [0u8; N];
let mut i = 0;
while i < N {
out[i] = (nibble(s[i * 2]) << 4) | nibble(s[i * 2 + 1]);
i += 1;
}
out
}
const fn nibble(c: u8) -> u8 {
match c {
b'0'..=b'9' => c - b'0',
b'a'..=b'f' => c - b'a' + 10,
_ => panic!("curve constants must be lowercase hex"),
}
}
const P256_P: [u8; 32] = hex(b"ffffffff00000001000000000000000000000000ffffffffffffffffffffffff");
const P256_A: [u8; 32] = hex(b"ffffffff00000001000000000000000000000000fffffffffffffffffffffffc");
const P256_B: [u8; 32] = hex(b"5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b");
const P256_G: [u8; 65] = hex(
b"046b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c2\
964fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51\
f5",
);
const P256_N: [u8; 32] = hex(b"ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551");
const P384_P: [u8; 48] = hex(
b"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe\
ffffffff0000000000000000ffffffff",
);
const P384_A: [u8; 48] = hex(
b"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe\
ffffffff0000000000000000fffffffc",
);
const P384_B: [u8; 48] = hex(
b"b3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875a\
c656398d8a2ed19d2a85c8edd3ec2aef",
);
const P384_G: [u8; 97] = hex(
b"04aa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a\
385502f25dbf55296c3a545e3872760ab73617de4a96262c6f5d9e98bf9292dc\
29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e\
5f",
);
const P384_N: [u8; 48] = hex(
b"ffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf\
581a0db248b0a77aecec196accc52973",
);
const P521_P: [u8; 66] = hex(
b"01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\
ffff",
);
const P521_A: [u8; 66] = hex(
b"01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\
fffc",
);
const P521_B: [u8; 66] = hex(
b"0051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef1\
09e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b50\
3f00",
);
const P521_G: [u8; 133] = hex(
b"0400c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b\
4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2\
e5bd66011839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd\
17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94\
769fd16650",
);
const P521_N: [u8; 66] = hex(
b"01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\
fffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e9138\
6409",
);
#[cfg(feature = "bp256")]
const BP256_P: [u8; 32] = hex(b"a9fb57dba1eea9bc3e660a909d838d726e3bf623d52620282013481d1f6e5377");
#[cfg(feature = "bp256")]
const BP256_A: [u8; 32] = hex(b"7d5a0975fc2c3057eef67530417affe7fb8055c126dc5c6ce94a4b44f330b5d9");
#[cfg(feature = "bp256")]
const BP256_B: [u8; 32] = hex(b"26dc5c6ce94a4b44f330b5d9bbd77cbf958416295cf7e1ce6bccdc18ff8c07b6");
#[cfg(feature = "bp256")]
const BP256_G: [u8; 65] = hex(
b"048bd2aeb9cb7e57cb2c4b482ffc81b7afb9de27e1e3bd23c23a4453bd9ace32\
62547ef835c3dac4fd97f8461a14611dc9c27745132ded8e545c1d54c72f0469\
97",
);
#[cfg(feature = "bp256")]
const BP256_N: [u8; 32] = hex(b"a9fb57dba1eea9bc3e660a909d838d718c397aa3b561a6f7901e0e82974856a7");
#[cfg(feature = "bp384")]
const BP384_P: [u8; 48] = hex(
b"8cb91e82a3386d280f5d6f7e50e641df152f7109ed5456b412b1da197fb71123\
acd3a729901d1a71874700133107ec53",
);
#[cfg(feature = "bp384")]
const BP384_A: [u8; 48] = hex(
b"7bc382c63d8c150c3c72080ace05afa0c2bea28e4fb22787139165efba91f90f\
8aa5814a503ad4eb04a8c7dd22ce2826",
);
#[cfg(feature = "bp384")]
const BP384_B: [u8; 48] = hex(
b"04a8c7dd22ce28268b39b55416f0447c2fb77de107dcd2a62e880ea53eeb62d5\
7cb4390295dbc9943ab78696fa504c11",
);
#[cfg(feature = "bp384")]
const BP384_G: [u8; 97] = hex(
b"041d1c64f068cf45ffa2a63a81b7c13f6b8847a3e77ef14fe3db7fcafe0cbd10\
e8e826e03436d646aaef87b2e247d4af1e8abe1d7520f9c2a45cb1eb8e95cfd5\
5262b70b29feec5864e19c054ff99129280e4646217791811142820341263c53\
15",
);
#[cfg(feature = "bp384")]
const BP384_N: [u8; 48] = hex(
b"8cb91e82a3386d280f5d6f7e50e641df152f7109ed5456b31f166e6cac0425a7\
cf3ab6af6b7fc3103b883202e9046565",
);
#[cfg(feature = "bp224")]
const BP224_P: [u8; 28] = hex(b"d7c134aa264366862a18302575d1d787b09f075797da89f57ec8c0ff");
#[cfg(feature = "bp224")]
const BP224_A: [u8; 28] = hex(b"68a5e62ca9ce6c1c299803a6c1530b514e182ad8b0042a59cad29f43");
#[cfg(feature = "bp224")]
const BP224_B: [u8; 28] = hex(b"2580f63ccfe44138870713b1a92369e33e2135d266dbb372386c400b");
#[cfg(feature = "bp224")]
const BP224_G: [u8; 57] = hex(
b"040d9029ad2c7e5cf4340823b2a87dc68c9e4ce3174c1e6efdee12c07d58aa56\
f772c0726f24c6b89e4ecdac24354b9e99caa3f6d3761402cd",
);
#[cfg(feature = "bp224")]
const BP224_N: [u8; 28] = hex(b"d7c134aa264366862a18302575d0fb98d116bc4b6ddebca3a5a7939f");
#[cfg(feature = "bp512")]
const BP512_P: [u8; 64] = hex(
b"aadd9db8dbe9c48b3fd4e6ae33c9fc07cb308db3b3c9d20ed6639cca70330871\
7d4d9b009bc66842aecda12ae6a380e62881ff2f2d82c68528aa6056583a48f3",
);
#[cfg(feature = "bp512")]
const BP512_A: [u8; 64] = hex(
b"7830a3318b603b89e2327145ac234cc594cbdd8d3df91610a83441caea9863bc\
2ded5d5aa8253aa10a2ef1c98b9ac8b57f1117a72bf2c7b9e7c1ac4d77fc94ca",
);
#[cfg(feature = "bp512")]
const BP512_B: [u8; 64] = hex(
b"3df91610a83441caea9863bc2ded5d5aa8253aa10a2ef1c98b9ac8b57f1117a7\
2bf2c7b9e7c1ac4d77fc94cadc083e67984050b75ebae5dd2809bd638016f723",
);
#[cfg(feature = "bp512")]
const BP512_G: [u8; 129] = hex(
b"0481aee4bdd82ed9645a21322e9c4c6a9385ed9f70b5d916c1b43b62eef4d009\
8eff3b1f78e2d0d48d50d1687b93b97d5f7c6d5047406a5e688b352209bcb9f8\
227dde385d566332ecc0eabfa9cf7822fdf209f70024a57b1aa000c55b881f81\
11b2dcde494a5f485e5bca4bd88a2763aed1ca2b2fa8f0540678cd1e0f3ad808\
92",
);
#[cfg(feature = "bp512")]
const BP512_N: [u8; 64] = hex(
b"aadd9db8dbe9c48b3fd4e6ae33c9fc07cb308db3b3c9d20ed6639cca70330870\
553e5c414ca92619418661197fac10471db1d381085ddaddb58796829ca90069",
);
struct KnownCurve {
oid: ObjectIdentifier,
p: &'static [u8],
a: &'static [u8],
b: &'static [u8],
base: &'static [u8],
order: &'static [u8],
}
const KNOWN: &[KnownCurve] = &[
#[cfg(feature = "bp256")]
KnownCurve {
oid: ObjectIdentifier::new_unwrap("1.3.36.3.3.2.8.1.1.7"),
p: &BP256_P,
a: &BP256_A,
b: &BP256_B,
base: &BP256_G,
order: &BP256_N,
},
#[cfg(feature = "bp224")]
KnownCurve {
oid: ObjectIdentifier::new_unwrap("1.3.36.3.3.2.8.1.1.5"),
p: &BP224_P,
a: &BP224_A,
b: &BP224_B,
base: &BP224_G,
order: &BP224_N,
},
#[cfg(feature = "bp512")]
KnownCurve {
oid: ObjectIdentifier::new_unwrap("1.3.36.3.3.2.8.1.1.13"),
p: &BP512_P,
a: &BP512_A,
b: &BP512_B,
base: &BP512_G,
order: &BP512_N,
},
#[cfg(feature = "bp384")]
KnownCurve {
oid: ObjectIdentifier::new_unwrap("1.3.36.3.3.2.8.1.1.11"),
p: &BP384_P,
a: &BP384_A,
b: &BP384_B,
base: &BP384_G,
order: &BP384_N,
},
KnownCurve {
oid: ObjectIdentifier::new_unwrap("1.2.840.10045.3.1.7"),
p: &P256_P,
a: &P256_A,
b: &P256_B,
base: &P256_G,
order: &P256_N,
},
KnownCurve {
oid: ObjectIdentifier::new_unwrap("1.3.132.0.34"),
p: &P384_P,
a: &P384_A,
b: &P384_B,
base: &P384_G,
order: &P384_N,
},
KnownCurve {
oid: ObjectIdentifier::new_unwrap("1.3.132.0.35"),
p: &P521_P,
a: &P521_A,
b: &P521_B,
base: &P521_G,
order: &P521_N,
},
];
fn same_magnitude(x: &[u8], y: &[u8]) -> bool {
let strip = |v: &[u8]| {
let n = v.iter().take_while(|&&b| b == 0).count();
v[n..].to_vec()
};
strip(x) == strip(y)
}
#[must_use]
pub fn named_curve_from_specified(params: AnyRef<'_>) -> Option<ObjectIdentifier> {
let mut r = der::SliceReader::new(params.value()).ok()?;
let version: UintRef<'_> = r.decode().ok()?;
if version.as_bytes() != [1] {
return None;
}
let prime = r
.sequence(|r| {
let field_type: ObjectIdentifier = r.decode()?;
let prime: UintRef<'_> = r.decode()?;
Ok::<_, der::Error>((field_type, prime))
})
.ok()
.and_then(|(ty, p)| (ty == OID_PRIME_FIELD).then_some(p))?;
let (a, b) = r
.sequence(|r| {
let a: &OctetStringRef = r.decode()?;
let b: &OctetStringRef = r.decode()?;
if !r.is_finished() {
let _seed: BitStringRef<'_> = r.decode()?;
}
Ok::<_, der::Error>((a, b))
})
.ok()?;
let base: &OctetStringRef = r.decode().ok()?;
let order: UintRef<'_> = r.decode().ok()?;
if !r.is_finished()
&& let Ok(cofactor) = r.decode::<UintRef<'_>>()
&& cofactor.as_bytes() != [1]
{
return None;
}
KNOWN
.iter()
.find(|c| {
same_magnitude(prime.as_bytes(), c.p)
&& same_magnitude(a.as_bytes(), c.a)
&& same_magnitude(b.as_bytes(), c.b)
&& base.as_bytes() == c.base
&& same_magnitude(order.as_bytes(), c.order)
})
.map(|c| c.oid)
}
#[allow(clippy::many_single_char_names)]
#[cfg(test)]
mod tests {
use super::*;
use crate::Vec;
fn domain(p: &[u8], a: &[u8], b: &[u8], base: &[u8], order: &[u8], cofactor: &[u8]) -> Vec<u8> {
fn tlv(tag: u8, body: &[u8]) -> Vec<u8> {
let mut out = vec![tag];
if body.len() < 0x80 {
out.push(u8::try_from(body.len()).expect("checked above"));
} else if body.len() < 0x100 {
out.push(0x81);
out.push(u8::try_from(body.len()).expect("checked above"));
} else {
out.push(0x82);
out.extend_from_slice(
&u16::try_from(body.len())
.expect("test inputs are small")
.to_be_bytes(),
);
}
out.extend_from_slice(body);
out
}
fn int(v: &[u8]) -> Vec<u8> {
let v = &v[v.iter().take_while(|&&x| x == 0).count()..];
let mut body = Vec::new();
if v.first().is_some_and(|&b| b & 0x80 != 0) {
body.push(0);
}
body.extend_from_slice(v);
tlv(0x02, &body)
}
let field_id = {
let mut s = tlv(0x06, &[0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01]); s.extend_from_slice(&int(p));
tlv(0x30, &s)
};
let curve = {
let mut s = tlv(0x04, a);
s.extend_from_slice(&tlv(0x04, b));
tlv(0x30, &s)
};
let mut body = int(&[1]); body.extend_from_slice(&field_id);
body.extend_from_slice(&curve);
body.extend_from_slice(&tlv(0x04, base));
body.extend_from_slice(&int(order));
body.extend_from_slice(&int(cofactor));
tlv(0x30, &body)
}
fn recognize(der: &[u8]) -> Option<ObjectIdentifier> {
use der::Decode as _;
named_curve_from_specified(AnyRef::from_der(der).expect("test DER should parse"))
}
#[test]
fn conformant_p256_is_recognized() {
let der = domain(&P256_P, &P256_A, &P256_B, &P256_G, &P256_N, &[1]);
assert_eq!(
recognize(&der),
Some(ObjectIdentifier::new_unwrap("1.2.840.10045.3.1.7")),
);
}
#[test]
fn conformant_p384_and_p521_are_recognized() {
let p384 = domain(&P384_P, &P384_A, &P384_B, &P384_G, &P384_N, &[1]);
assert_eq!(
recognize(&p384),
Some(ObjectIdentifier::new_unwrap("1.3.132.0.34")),
);
let p521 = domain(&P521_P, &P521_A, &P521_B, &P521_G, &P521_N, &[1]);
assert_eq!(
recognize(&p521),
Some(ObjectIdentifier::new_unwrap("1.3.132.0.35")),
);
}
#[test]
fn a_curve_sharing_only_the_prime_is_not_recognized() {
let mut b = P384_B;
b[47] ^= 1;
let der = domain(&P384_P, &P384_A, &b, &P384_G, &P384_N, &[1]);
assert_eq!(recognize(&der), None, "b differs, so the curve differs");
}
#[test]
fn altering_any_single_parameter_prevents_recognition() {
let mut a = P256_A;
a[31] ^= 1;
assert_eq!(
recognize(&domain(&P256_P, &a, &P256_B, &P256_G, &P256_N, &[1])),
None,
"a differs",
);
let mut g = P256_G;
g[64] ^= 1;
assert_eq!(
recognize(&domain(&P256_P, &P256_A, &P256_B, &g, &P256_N, &[1])),
None,
"generator differs",
);
let mut n = P256_N;
n[31] ^= 1;
assert_eq!(
recognize(&domain(&P256_P, &P256_A, &P256_B, &P256_G, &n, &[1])),
None,
"order differs",
);
let mut p = P256_P;
p[31] ^= 1;
assert_eq!(
recognize(&domain(&p, &P256_A, &P256_B, &P256_G, &P256_N, &[1])),
None,
"prime differs",
);
}
#[test]
fn a_cofactor_other_than_one_prevents_recognition() {
let der = domain(&P256_P, &P256_A, &P256_B, &P256_G, &P256_N, &[2]);
assert_eq!(recognize(&der), None);
}
#[cfg(all(feature = "bp256", feature = "bp384"))]
#[test]
fn the_implemented_brainpool_curves_are_recognized() {
let p256 = domain(&BP256_P, &BP256_A, &BP256_B, &BP256_G, &BP256_N, &[1]);
assert_eq!(
recognize(&p256),
Some(ObjectIdentifier::new_unwrap("1.3.36.3.3.2.8.1.1.7")),
"brainpoolP256r1, RFC 5639 §3.4",
);
let p384 = domain(&BP384_P, &BP384_A, &BP384_B, &BP384_G, &BP384_N, &[1]);
assert_eq!(
recognize(&p384),
Some(ObjectIdentifier::new_unwrap("1.3.36.3.3.2.8.1.1.11")),
"brainpoolP384r1, RFC 5639 §3.6",
);
}
#[cfg(feature = "bp512")]
#[test]
fn brainpool_p512r1_is_recognized() {
let der = domain(&BP512_P, &BP512_A, &BP512_B, &BP512_G, &BP512_N, &[1]);
assert_eq!(
recognize(&der),
Some(ObjectIdentifier::new_unwrap("1.3.36.3.3.2.8.1.1.13")),
);
}
#[cfg(feature = "bp224")]
#[test]
fn brainpool_p224r1_is_recognized() {
let der = domain(&BP224_P, &BP224_A, &BP224_B, &BP224_G, &BP224_N, &[1]);
assert_eq!(
recognize(&der),
Some(ObjectIdentifier::new_unwrap("1.3.36.3.3.2.8.1.1.5")),
);
}
#[test]
fn brainpool_p320r1_is_not_recognized_because_it_is_not_implemented() {
let p: [u8; 40] = hex(
b"d35e472036bc4fb7e13c785ed201e065f98fcfa6f6f40def4f92b9ec7893ec28\
fcd412b1f1b32e27",
);
let a: [u8; 40] = hex(
b"3ee30b568fbab0f883ccebd46d3f3bb8a2a73513f5eb79da66190eb085ffa9f4\
92f375a97d860eb4",
);
let b: [u8; 40] = hex(
b"520883949dfdbc42d3ad198640688a6fe13f41349554b49acc31dccd88453981\
6f5eb4ac8fb1f1a6",
);
let g: [u8; 81] = hex(
b"0443bd7e9afb53d8b85289bcc48ee5bfe6f20137d10a087eb6e7871e2a10a599\
c710af8d0d39e2061114fdd05545ec1cc8ab4093247f77275e0743ffed117182\
eaa9c77877aaac6ac7d35245d1692e8ee1",
);
let n: [u8; 40] = hex(
b"d35e472036bc4fb7e13c785ed201e065f98fcfa5b68f12a32d482ec7ee8658e9\
8691555b44c59311",
);
let der = domain(&p, &a, &b, &g, &n, &[1]);
assert_eq!(
recognize(&der),
None,
"brainpoolP320r1 is a real curve this build has no arithmetic \
for; recognizing it would promise what it cannot do",
);
}
#[test]
fn malformed_input_is_refused_without_panicking() {
use der::Decode as _;
for bad in [
vec![0x30, 0x00],
vec![0x30, 0x03, 0x02, 0x01, 0x01],
vec![0x30, 0x02, 0xff, 0xff],
] {
let any = AnyRef::from_der(&bad).expect("these are well-formed TLVs");
assert_eq!(named_curve_from_specified(any), None);
}
}
#[test]
fn only_ecdp_version_one_is_accepted() {
let mut der = domain(&P256_P, &P256_A, &P256_B, &P256_G, &P256_N, &[1]);
let v = der
.iter()
.position(|&b| b == 0x02)
.expect("the version INTEGER should be present")
+ 2;
der[v] = 2;
assert_eq!(recognize(&der), None, "ecdpVer2 is not read as ecdpVer1");
}
}