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
parse_uncompressed_point( ops: &PublicKeyOps, input: untrusted::Input, ) -> Result<(Elem<R>, Elem<R>), error::Unspecified>29 pub fn parse_uncompressed_point(
30     ops: &PublicKeyOps,
31     input: untrusted::Input,
32 ) -> Result<(Elem<R>, Elem<R>), error::Unspecified> {
33     // NIST SP 800-56A Step 1: "Verify that Q is not the point at infinity.
34     // This can be done by inspection if the point is entered in the standard
35     // affine representation." (We do it by inspection since we only accept
36     // the affine representation.)
37     let (x, y) = input.read_all(error::Unspecified, |input| {
38         // The encoding must be 4, which is the encoding for "uncompressed".
39         let encoding = input.read_byte()?;
40         if encoding != 4 {
41             return Err(error::Unspecified);
42         }
43 
44         // NIST SP 800-56A Step 2: "Verify that xQ and yQ are integers in the
45         // interval [0, p-1] in the case that q is an odd prime p[.]"
46         let x = ops.elem_parse(input)?;
47         let y = ops.elem_parse(input)?;
48         Ok((x, y))
49     })?;
50 
51     // NIST SP 800-56A Step 3: "If q is an odd prime p, verify that
52     // yQ**2 = xQ**3 + axQ + b in GF(p), where the arithmetic is performed
53     // modulo p."
54     verify_affine_point_is_on_the_curve(ops.common, (&x, &y))?;
55 
56     // NIST SP 800-56A Note: "Since its order is not verified, there is no
57     // check that the public key is in the correct EC subgroup."
58     //
59     // NSA Suite B Implementer's Guide Note: "ECC Full Public-Key Validation
60     // includes an additional check to ensure that the point has the correct
61     // order. This check is not necessary for curves having prime order (and
62     // cofactor h = 1), such as P-256 and P-384."
63 
64     Ok((x, y))
65 }
66 
67 #[cfg(test)]
68 mod tests {
69     use super::{super::ops, *};
70     use crate::test;
71 
72     #[test]
parse_uncompressed_point_test()73     fn parse_uncompressed_point_test() {
74         test::run(
75             test_file!("suite_b_public_key_tests.txt"),
76             |section, test_case| {
77                 assert_eq!(section, "");
78 
79                 let curve_name = test_case.consume_string("Curve");
80 
81                 let public_key = test_case.consume_bytes("Q");
82                 let public_key = untrusted::Input::from(&public_key);
83                 let is_valid = test_case.consume_string("Result") == "P";
84 
85                 let curve_ops = public_key_ops_from_curve_name(&curve_name);
86 
87                 let result = parse_uncompressed_point(curve_ops, public_key);
88                 assert_eq!(is_valid, result.is_ok());
89 
90                 // TODO: Verify that we when we re-serialize the parsed (x, y), the
91                 // output is equal to the input.
92 
93                 Ok(())
94             },
95         );
96     }
97 
public_key_ops_from_curve_name(curve_name: &str) -> &'static PublicKeyOps98     fn public_key_ops_from_curve_name(curve_name: &str) -> &'static PublicKeyOps {
99         if curve_name == "P-256" {
100             &ops::p256::PUBLIC_KEY_OPS
101         } else if curve_name == "P-384" {
102             &ops::p384::PUBLIC_KEY_OPS
103         } else {
104             panic!("Unsupported curve: {}", curve_name);
105         }
106     }
107 }
108