blob: 7574389841b9a66d711c26821848ba95e645d799 [file] [log] [blame]
Vitaly Buka9ba72a82015-08-06 17:36:17 -07001// Copyright 2012 The Chromium OS Authors. All rights reserved.
Vitaly Buka6ca6a232015-08-06 17:32:43 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Vitaly Buka9e5b6832015-10-14 15:57:14 -07005#ifndef LIBWEAVE_THIRD_PARTY_CHROMIUM_P224_H_
6#define LIBWEAVE_THIRD_PARTY_CHROMIUM_P224_H_
Vitaly Buka6ca6a232015-08-06 17:32:43 -07007
8#include <string>
9
Vitaly Buka9ba72a82015-08-06 17:36:17 -070010#include <base/basictypes.h>
Vitaly Buka6ca6a232015-08-06 17:32:43 -070011
12namespace crypto {
13
14// P224 implements an elliptic curve group, commonly known as P224 and defined
15// in FIPS 186-3, section D.2.2.
16namespace p224 {
17
18// An element of the field (ℤ/pℤ) is represented with 8, 28-bit limbs in
19// little endian order.
20typedef uint32 FieldElement[8];
21
Vitaly Buka9ba72a82015-08-06 17:36:17 -070022struct Point {
Vitaly Buka6ca6a232015-08-06 17:32:43 -070023 // SetFromString the value of the point from the 56 byte, external
24 // representation. The external point representation is an (x, y) pair of a
25 // point on the curve. Each field element is represented as a big-endian
26 // number < p.
Vitaly Buka0d501072015-08-18 18:09:46 -070027 bool SetFromString(const std::string& in);
Vitaly Buka6ca6a232015-08-06 17:32:43 -070028
29 // ToString returns an external representation of the Point.
30 std::string ToString() const;
31
32 // An Point is represented in Jacobian form (x/z², y/z³).
33 FieldElement x, y, z;
34};
35
36// kScalarBytes is the number of bytes needed to represent an element of the
37// P224 field.
38static const size_t kScalarBytes = 28;
39
40// ScalarMult computes *out = in*scalar where scalar is a 28-byte, big-endian
41// number.
Vitaly Buka9ba72a82015-08-06 17:36:17 -070042void ScalarMult(const Point& in, const uint8* scalar, Point* out);
Vitaly Buka6ca6a232015-08-06 17:32:43 -070043
44// ScalarBaseMult computes *out = g*scalar where g is the base point of the
45// curve and scalar is a 28-byte, big-endian number.
Vitaly Buka9ba72a82015-08-06 17:36:17 -070046void ScalarBaseMult(const uint8* scalar, Point* out);
Vitaly Buka6ca6a232015-08-06 17:32:43 -070047
48// Add computes *out = a+b.
Vitaly Buka9ba72a82015-08-06 17:36:17 -070049void Add(const Point& a, const Point& b, Point* out);
Vitaly Buka6ca6a232015-08-06 17:32:43 -070050
51// Negate calculates out = -a;
Vitaly Buka9ba72a82015-08-06 17:36:17 -070052void Negate(const Point& a, Point* out);
Vitaly Buka6ca6a232015-08-06 17:32:43 -070053
54} // namespace p224
Vitaly Buka6ca6a232015-08-06 17:32:43 -070055} // namespace crypto
56
Vitaly Buka9e5b6832015-10-14 15:57:14 -070057#endif // LIBWEAVE_THIRD_PARTY_CHROMIUM_P224_H_