blob: e9976e6d9fd37acac01482b372a27fa8cf874165 [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
Alex Vakulenko674f0eb2016-01-20 08:10:48 -08008#include <stddef.h>
9#include <stdint.h>
10
Vitaly Buka6ca6a232015-08-06 17:32:43 -070011#include <string>
12
Alex Vakulenko674f0eb2016-01-20 08:10:48 -080013#include "base/strings/string_piece.h"
Vitaly Buka6ca6a232015-08-06 17:32:43 -070014
15namespace crypto {
16
17// P224 implements an elliptic curve group, commonly known as P224 and defined
18// in FIPS 186-3, section D.2.2.
19namespace p224 {
20
21// An element of the field (ℤ/pℤ) is represented with 8, 28-bit limbs in
22// little endian order.
Alex Vakulenko674f0eb2016-01-20 08:10:48 -080023typedef uint32_t FieldElement[8];
Vitaly Buka6ca6a232015-08-06 17:32:43 -070024
Vitaly Buka9ba72a82015-08-06 17:36:17 -070025struct Point {
Vitaly Buka6ca6a232015-08-06 17:32:43 -070026 // 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 Vakulenko674f0eb2016-01-20 08:10:48 -080030 bool SetFromString(const base::StringPiece& in);
Vitaly Buka6ca6a232015-08-06 17:32:43 -070031
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.
41static const size_t kScalarBytes = 28;
42
43// ScalarMult computes *out = in*scalar where scalar is a 28-byte, big-endian
44// number.
Alex Vakulenko674f0eb2016-01-20 08:10:48 -080045void ScalarMult(const Point& in, const uint8_t* scalar, Point* out);
Vitaly Buka6ca6a232015-08-06 17:32:43 -070046
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 Vakulenko674f0eb2016-01-20 08:10:48 -080049void ScalarBaseMult(const uint8_t* scalar, Point* out);
Vitaly Buka6ca6a232015-08-06 17:32:43 -070050
51// Add computes *out = a+b.
Vitaly Buka9ba72a82015-08-06 17:36:17 -070052void Add(const Point& a, const Point& b, Point* out);
Vitaly Buka6ca6a232015-08-06 17:32:43 -070053
54// Negate calculates out = -a;
Vitaly Buka9ba72a82015-08-06 17:36:17 -070055void Negate(const Point& a, Point* out);
Vitaly Buka6ca6a232015-08-06 17:32:43 -070056
57} // namespace p224
Alex Vakulenko674f0eb2016-01-20 08:10:48 -080058
Vitaly Buka6ca6a232015-08-06 17:32:43 -070059} // namespace crypto
60
Vitaly Buka9e5b6832015-10-14 15:57:14 -070061#endif // LIBWEAVE_THIRD_PARTY_CHROMIUM_P224_H_