xref: /openbsd-src/lib/libcrypto/curve25519/curve25519_internal.h (revision ab3ec570303cab596f4d663d61e0b5321a4292c3)
1*ab3ec570Sjsing /*	$OpenBSD: curve25519_internal.h,v 1.6 2022/11/09 17:45:55 jsing Exp $ */
25f5d09a5Sjsing /*
35f5d09a5Sjsing  * Copyright (c) 2015, Google Inc.
45f5d09a5Sjsing  *
55f5d09a5Sjsing  * Permission to use, copy, modify, and/or distribute this software for any
65f5d09a5Sjsing  * purpose with or without fee is hereby granted, provided that the above
75f5d09a5Sjsing  * copyright notice and this permission notice appear in all copies.
85f5d09a5Sjsing  *
95f5d09a5Sjsing  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
105f5d09a5Sjsing  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
115f5d09a5Sjsing  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
125f5d09a5Sjsing  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
135f5d09a5Sjsing  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
145f5d09a5Sjsing  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
155f5d09a5Sjsing  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
165f5d09a5Sjsing  */
175f5d09a5Sjsing 
185f5d09a5Sjsing #ifndef HEADER_CURVE25519_INTERNAL_H
195f5d09a5Sjsing #define HEADER_CURVE25519_INTERNAL_H
205f5d09a5Sjsing 
215f5d09a5Sjsing #include <stdint.h>
225f5d09a5Sjsing 
2352173d4bSguenther __BEGIN_HIDDEN_DECLS
245f5d09a5Sjsing 
255f5d09a5Sjsing /* fe means field element. Here the field is \Z/(2^255-19). An element t,
265f5d09a5Sjsing  * entries t[0]...t[9], represents the integer t[0]+2^26 t[1]+2^51 t[2]+2^77
275f5d09a5Sjsing  * t[3]+2^102 t[4]+...+2^230 t[9]. Bounds on each t[i] vary depending on
285f5d09a5Sjsing  * context.  */
295f5d09a5Sjsing typedef int32_t fe[10];
305f5d09a5Sjsing 
315f5d09a5Sjsing /* ge means group element.
325f5d09a5Sjsing 
335f5d09a5Sjsing  * Here the group is the set of pairs (x,y) of field elements (see fe.h)
345f5d09a5Sjsing  * satisfying -x^2 + y^2 = 1 + d x^2y^2
355f5d09a5Sjsing  * where d = -121665/121666.
365f5d09a5Sjsing  *
375f5d09a5Sjsing  * Representations:
385f5d09a5Sjsing  *   ge_p2 (projective): (X:Y:Z) satisfying x=X/Z, y=Y/Z
395f5d09a5Sjsing  *   ge_p3 (extended): (X:Y:Z:T) satisfying x=X/Z, y=Y/Z, XY=ZT
405f5d09a5Sjsing  *   ge_p1p1 (completed): ((X:Z),(Y:T)) satisfying x=X/Z, y=Y/T
415f5d09a5Sjsing  *   ge_precomp (Duif): (y+x,y-x,2dxy) */
425f5d09a5Sjsing 
435f5d09a5Sjsing typedef struct {
445f5d09a5Sjsing   fe X;
455f5d09a5Sjsing   fe Y;
465f5d09a5Sjsing   fe Z;
475f5d09a5Sjsing } ge_p2;
485f5d09a5Sjsing 
495f5d09a5Sjsing typedef struct {
505f5d09a5Sjsing   fe X;
515f5d09a5Sjsing   fe Y;
525f5d09a5Sjsing   fe Z;
535f5d09a5Sjsing   fe T;
545f5d09a5Sjsing } ge_p3;
555f5d09a5Sjsing 
565f5d09a5Sjsing typedef struct {
575f5d09a5Sjsing   fe X;
585f5d09a5Sjsing   fe Y;
595f5d09a5Sjsing   fe Z;
605f5d09a5Sjsing   fe T;
615f5d09a5Sjsing } ge_p1p1;
625f5d09a5Sjsing 
635f5d09a5Sjsing typedef struct {
645f5d09a5Sjsing   fe yplusx;
655f5d09a5Sjsing   fe yminusx;
665f5d09a5Sjsing   fe xy2d;
675f5d09a5Sjsing } ge_precomp;
685f5d09a5Sjsing 
695f5d09a5Sjsing typedef struct {
705f5d09a5Sjsing   fe YplusX;
715f5d09a5Sjsing   fe YminusX;
725f5d09a5Sjsing   fe Z;
735f5d09a5Sjsing   fe T2d;
745f5d09a5Sjsing } ge_cached;
755f5d09a5Sjsing 
765f5d09a5Sjsing void x25519_ge_tobytes(uint8_t *s, const ge_p2 *h);
775f5d09a5Sjsing int x25519_ge_frombytes_vartime(ge_p3 *h, const uint8_t *s);
785f5d09a5Sjsing void x25519_ge_p3_to_cached(ge_cached *r, const ge_p3 *p);
795f5d09a5Sjsing void x25519_ge_p1p1_to_p2(ge_p2 *r, const ge_p1p1 *p);
805f5d09a5Sjsing void x25519_ge_p1p1_to_p3(ge_p3 *r, const ge_p1p1 *p);
815f5d09a5Sjsing void x25519_ge_add(ge_p1p1 *r, const ge_p3 *p, const ge_cached *q);
825f5d09a5Sjsing void x25519_ge_sub(ge_p1p1 *r, const ge_p3 *p, const ge_cached *q);
835f5d09a5Sjsing void x25519_ge_scalarmult_small_precomp(ge_p3 *h, const uint8_t a[32],
845f5d09a5Sjsing     const uint8_t precomp_table[15 * 2 * 32]);
855f5d09a5Sjsing void x25519_ge_scalarmult_base(ge_p3 *h, const uint8_t a[32]);
865f5d09a5Sjsing void x25519_ge_scalarmult(ge_p2 *r, const uint8_t *scalar, const ge_p3 *A);
875f5d09a5Sjsing void x25519_sc_reduce(uint8_t *s);
885f5d09a5Sjsing 
895f5d09a5Sjsing void x25519_public_from_private(uint8_t out_public_value[32],
905f5d09a5Sjsing     const uint8_t private_key[32]);
915f5d09a5Sjsing 
925f5d09a5Sjsing void x25519_scalar_mult(uint8_t out[32], const uint8_t scalar[32],
935f5d09a5Sjsing     const uint8_t point[32]);
945f5d09a5Sjsing void x25519_scalar_mult_generic(uint8_t out[32], const uint8_t scalar[32],
955f5d09a5Sjsing     const uint8_t point[32]);
965f5d09a5Sjsing 
975b0711d2Sjsing void ED25519_public_from_private(uint8_t out_public_key[32],
985b0711d2Sjsing     const uint8_t private_key[32]);
99dac51b63Sjsing 
100*ab3ec570Sjsing void X25519_public_from_private(uint8_t out_public_key[32],
101*ab3ec570Sjsing     const uint8_t private_key[32]);
102*ab3ec570Sjsing 
10352173d4bSguenther __END_HIDDEN_DECLS
1045f5d09a5Sjsing 
1055f5d09a5Sjsing #endif  /* HEADER_CURVE25519_INTERNAL_H */
106