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