1 | // Copyright 2016 Brian Smith. |
2 | // |
3 | // Permission to use, copy, modify, and/or distribute this software for any |
4 | // purpose with or without fee is hereby granted, provided that the above |
5 | // copyright notice and this permission notice appear in all copies. |
6 | // |
7 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES |
8 | // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
9 | // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY |
10 | // SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
11 | // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
12 | // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
13 | // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
14 | |
15 | //! Functionality shared by operations on public keys (ECDSA verification and |
16 | //! ECDH agreement). |
17 | |
18 | use super::{ops::*, verify_affine_point_is_on_the_curve}; |
19 | use crate::{arithmetic::montgomery::*, error}; |
20 | |
21 | /// Parses a public key encoded in uncompressed form. The key is validated |
22 | /// using the ECC Partial Public-Key Validation Routine from |
23 | /// [NIST SP 800-56A, revision 2] Section 5.6.2.3.3, the NSA's |
24 | /// "Suite B Implementer's Guide to NIST SP 800-56A," Appendix B.3, and the |
25 | /// NSA's "Suite B Implementer's Guide to FIPS 186-3 (ECDSA)," Appendix A.3. |
26 | /// |
27 | /// [NIST SP 800-56A, revision 2]: |
28 | /// http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-56Ar2.pdf |
29 | pub(super) fn parse_uncompressed_point( |
30 | ops: &PublicKeyOps, |
31 | q: &Modulus<Q>, |
32 | input: untrusted::Input, |
33 | ) -> Result<(Elem<R>, Elem<R>), error::Unspecified> { |
34 | // NIST SP 800-56A Step 1: "Verify that Q is not the point at infinity. |
35 | // This can be done by inspection if the point is entered in the standard |
36 | // affine representation." (We do it by inspection since we only accept |
37 | // the affine representation.) |
38 | let (x, y) = input.read_all(error::Unspecified, |input| { |
39 | // The encoding must be 4, which is the encoding for "uncompressed". |
40 | let encoding = input.read_byte()?; |
41 | if encoding != 4 { |
42 | return Err(error::Unspecified); |
43 | } |
44 | |
45 | // NIST SP 800-56A Step 2: "Verify that xQ and yQ are integers in the |
46 | // interval [0, p-1] in the case that q is an odd prime p[.]" |
47 | let x = ops.elem_parse(q, input)?; |
48 | let y = ops.elem_parse(q, input)?; |
49 | Ok((x, y)) |
50 | })?; |
51 | |
52 | // NIST SP 800-56A Step 3: "If q is an odd prime p, verify that |
53 | // yQ**2 = xQ**3 + axQ + b in GF(p), where the arithmetic is performed |
54 | // modulo p." |
55 | verify_affine_point_is_on_the_curve(q, (&x, &y))?; |
56 | |
57 | // NIST SP 800-56A Note: "Since its order is not verified, there is no |
58 | // check that the public key is in the correct EC subgroup." |
59 | // |
60 | // NSA Suite B Implementer's Guide Note: "ECC Full Public-Key Validation |
61 | // includes an additional check to ensure that the point has the correct |
62 | // order. This check is not necessary for curves having prime order (and |
63 | // cofactor h = 1), such as P-256 and P-384." |
64 | |
65 | Ok((x, y)) |
66 | } |
67 | |
68 | #[cfg (test)] |
69 | mod tests { |
70 | use super::*; |
71 | use crate::{cpu, test}; |
72 | |
73 | #[test ] |
74 | fn parse_uncompressed_point_test() { |
75 | let cpu = cpu::features(); |
76 | test::run( |
77 | test_file!("suite_b_public_key_tests.txt" ), |
78 | |section, test_case| { |
79 | assert_eq!(section, "" ); |
80 | |
81 | let curve_name = test_case .consume_string("Curve" ); |
82 | |
83 | let public_key = test_case .consume_bytes("Q" ); |
84 | let public_key = untrusted::Input::from(&public_key); |
85 | let is_valid = test_case .consume_string("Result" ) == "P" ; |
86 | |
87 | let curve_ops = public_key_ops_from_curve_name(&curve_name); |
88 | let q = &curve_ops.common.elem_modulus(cpu); |
89 | |
90 | let result = parse_uncompressed_point(curve_ops, q, public_key); |
91 | assert_eq!(is_valid, result.is_ok()); |
92 | |
93 | // TODO: Verify that we when we re-serialize the parsed (x, y), the |
94 | // output is equal to the input. |
95 | |
96 | Ok(()) |
97 | }, |
98 | ); |
99 | } |
100 | |
101 | fn public_key_ops_from_curve_name(curve_name: &str) -> &'static PublicKeyOps { |
102 | if curve_name == "P-256" { |
103 | &p256::PUBLIC_KEY_OPS |
104 | } else if curve_name == "P-384" { |
105 | &p384::PUBLIC_KEY_OPS |
106 | } else { |
107 | panic!("Unsupported curve: {}" , curve_name); |
108 | } |
109 | } |
110 | } |
111 | |