Vitaly Buka | 9ba72a8 | 2015-08-06 17:36:17 -0700 | [diff] [blame] | 1 | // Copyright 2012 The Chromium OS Authors. All rights reserved. |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Vitaly Buka | 9e5b683 | 2015-10-14 15:57:14 -0700 | [diff] [blame] | 5 | #ifndef LIBWEAVE_THIRD_PARTY_CHROMIUM_P224_H_ |
| 6 | #define LIBWEAVE_THIRD_PARTY_CHROMIUM_P224_H_ |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 7 | |
Alex Vakulenko | 674f0eb | 2016-01-20 08:10:48 -0800 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 11 | #include <string> |
| 12 | |
Alex Vakulenko | 674f0eb | 2016-01-20 08:10:48 -0800 | [diff] [blame] | 13 | #include "base/strings/string_piece.h" |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 14 | |
| 15 | namespace crypto { |
| 16 | |
| 17 | // P224 implements an elliptic curve group, commonly known as P224 and defined |
| 18 | // in FIPS 186-3, section D.2.2. |
| 19 | namespace p224 { |
| 20 | |
| 21 | // An element of the field (ℤ/pℤ) is represented with 8, 28-bit limbs in |
| 22 | // little endian order. |
Alex Vakulenko | 674f0eb | 2016-01-20 08:10:48 -0800 | [diff] [blame] | 23 | typedef uint32_t FieldElement[8]; |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 24 | |
Vitaly Buka | 9ba72a8 | 2015-08-06 17:36:17 -0700 | [diff] [blame] | 25 | struct Point { |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 26 | // SetFromString the value of the point from the 56 byte, external |
| 27 | // representation. The external point representation is an (x, y) pair of a |
| 28 | // point on the curve. Each field element is represented as a big-endian |
| 29 | // number < p. |
Alex Vakulenko | 674f0eb | 2016-01-20 08:10:48 -0800 | [diff] [blame] | 30 | bool SetFromString(const base::StringPiece& in); |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 31 | |
| 32 | // ToString returns an external representation of the Point. |
| 33 | std::string ToString() const; |
| 34 | |
| 35 | // An Point is represented in Jacobian form (x/z², y/z³). |
| 36 | FieldElement x, y, z; |
| 37 | }; |
| 38 | |
| 39 | // kScalarBytes is the number of bytes needed to represent an element of the |
| 40 | // P224 field. |
| 41 | static const size_t kScalarBytes = 28; |
| 42 | |
| 43 | // ScalarMult computes *out = in*scalar where scalar is a 28-byte, big-endian |
| 44 | // number. |
Alex Vakulenko | 674f0eb | 2016-01-20 08:10:48 -0800 | [diff] [blame] | 45 | void ScalarMult(const Point& in, const uint8_t* scalar, Point* out); |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 46 | |
| 47 | // ScalarBaseMult computes *out = g*scalar where g is the base point of the |
| 48 | // curve and scalar is a 28-byte, big-endian number. |
Alex Vakulenko | 674f0eb | 2016-01-20 08:10:48 -0800 | [diff] [blame] | 49 | void ScalarBaseMult(const uint8_t* scalar, Point* out); |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 50 | |
| 51 | // Add computes *out = a+b. |
Vitaly Buka | 9ba72a8 | 2015-08-06 17:36:17 -0700 | [diff] [blame] | 52 | void Add(const Point& a, const Point& b, Point* out); |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 53 | |
| 54 | // Negate calculates out = -a; |
Vitaly Buka | 9ba72a8 | 2015-08-06 17:36:17 -0700 | [diff] [blame] | 55 | void Negate(const Point& a, Point* out); |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 56 | |
| 57 | } // namespace p224 |
Alex Vakulenko | 674f0eb | 2016-01-20 08:10:48 -0800 | [diff] [blame] | 58 | |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 59 | } // namespace crypto |
| 60 | |
Vitaly Buka | 9e5b683 | 2015-10-14 15:57:14 -0700 | [diff] [blame] | 61 | #endif // LIBWEAVE_THIRD_PARTY_CHROMIUM_P224_H_ |