1 /*	$OpenBSD: curve25519_internal.h,v 1.3 2019/05/11 15:55:52 tb Exp $ */
2 /*
3  * Copyright (c) 2015, Google Inc.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
12  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
14  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
15  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #ifndef HEADER_CURVE25519_INTERNAL_H
19 #define HEADER_CURVE25519_INTERNAL_H
20 
21 #include <stdint.h>
22 
23 __BEGIN_HIDDEN_DECLS
24 
25 /* fe means field element. Here the field is \Z/(2^255-19). An element t,
26  * entries t[0]...t[9], represents the integer t[0]+2^26 t[1]+2^51 t[2]+2^77
27  * t[3]+2^102 t[4]+...+2^230 t[9]. Bounds on each t[i] vary depending on
28  * context.  */
29 typedef int32_t fe[10];
30 
31 /* ge means group element.
32 
33  * Here the group is the set of pairs (x,y) of field elements (see fe.h)
34  * satisfying -x^2 + y^2 = 1 + d x^2y^2
35  * where d = -121665/121666.
36  *
37  * Representations:
38  *   ge_p2 (projective): (X:Y:Z) satisfying x=X/Z, y=Y/Z
39  *   ge_p3 (extended): (X:Y:Z:T) satisfying x=X/Z, y=Y/Z, XY=ZT
40  *   ge_p1p1 (completed): ((X:Z),(Y:T)) satisfying x=X/Z, y=Y/T
41  *   ge_precomp (Duif): (y+x,y-x,2dxy) */
42 
43 typedef struct {
44   fe X;
45   fe Y;
46   fe Z;
47 } ge_p2;
48 
49 typedef struct {
50   fe X;
51   fe Y;
52   fe Z;
53   fe T;
54 } ge_p3;
55 
56 typedef struct {
57   fe X;
58   fe Y;
59   fe Z;
60   fe T;
61 } ge_p1p1;
62 
63 typedef struct {
64   fe yplusx;
65   fe yminusx;
66   fe xy2d;
67 } ge_precomp;
68 
69 typedef struct {
70   fe YplusX;
71   fe YminusX;
72   fe Z;
73   fe T2d;
74 } ge_cached;
75 
76 void x25519_ge_tobytes(uint8_t *s, const ge_p2 *h);
77 int x25519_ge_frombytes_vartime(ge_p3 *h, const uint8_t *s);
78 void x25519_ge_p3_to_cached(ge_cached *r, const ge_p3 *p);
79 void x25519_ge_p1p1_to_p2(ge_p2 *r, const ge_p1p1 *p);
80 void x25519_ge_p1p1_to_p3(ge_p3 *r, const ge_p1p1 *p);
81 void x25519_ge_add(ge_p1p1 *r, const ge_p3 *p, const ge_cached *q);
82 void x25519_ge_sub(ge_p1p1 *r, const ge_p3 *p, const ge_cached *q);
83 void x25519_ge_scalarmult_small_precomp(ge_p3 *h, const uint8_t a[32],
84     const uint8_t precomp_table[15 * 2 * 32]);
85 void x25519_ge_scalarmult_base(ge_p3 *h, const uint8_t a[32]);
86 void x25519_ge_scalarmult(ge_p2 *r, const uint8_t *scalar, const ge_p3 *A);
87 void x25519_sc_reduce(uint8_t *s);
88 
89 void x25519_public_from_private(uint8_t out_public_value[32],
90     const uint8_t private_key[32]);
91 
92 void x25519_scalar_mult(uint8_t out[32], const uint8_t scalar[32],
93     const uint8_t point[32]);
94 void x25519_scalar_mult_generic(uint8_t out[32], const uint8_t scalar[32],
95     const uint8_t point[32]);
96 
97 __END_HIDDEN_DECLS
98 
99 #endif  /* HEADER_CURVE25519_INTERNAL_H */
100