blob: 9226124b9e6cb98810df217f1c19fd302c446bb0 [file] [log] [blame]
Robert Sloanab8b8882018-03-26 11:39:51 -07001/*
2 * Copyright 2014-2016 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2014, Intel Corporation. All Rights Reserved.
Steven Valdez909b19f2016-11-21 15:35:44 -05004 *
Robert Sloanab8b8882018-03-26 11:39:51 -07005 * Licensed under the OpenSSL license (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
Steven Valdez909b19f2016-11-21 15:35:44 -05009 *
Robert Sloanab8b8882018-03-26 11:39:51 -070010 * Originally written by Shay Gueron (1, 2), and Vlad Krasnov (1)
11 * (1) Intel Corporation, Israel Development Center, Haifa, Israel
12 * (2) University of Haifa, Israel
13 *
14 * Reference:
15 * S.Gueron and V.Krasnov, "Fast Prime Field Elliptic Curve Cryptography with
16 * 256 Bit Primes"
17 */
Steven Valdez909b19f2016-11-21 15:35:44 -050018
19#ifndef OPENSSL_HEADER_EC_P256_X86_64_H
20#define OPENSSL_HEADER_EC_P256_X86_64_H
21
22#include <openssl/base.h>
23
24#include <openssl/bn.h>
25
26#if defined(__cplusplus)
27extern "C" {
28#endif
29
30
31#if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && \
32 !defined(OPENSSL_SMALL)
33
Robert Sloan8f860b12017-08-28 07:37:06 -070034// P-256 field operations.
35//
36// An element mod P in P-256 is represented as a little-endian array of
37// |P256_LIMBS| |BN_ULONG|s, spanning the full range of values.
38//
39// The following functions take fully-reduced inputs mod P and give
40// fully-reduced outputs. They may be used in-place.
Steven Valdez909b19f2016-11-21 15:35:44 -050041
42#define P256_LIMBS (256 / BN_BITS2)
43
Robert Sloan8f860b12017-08-28 07:37:06 -070044// ecp_nistz256_neg sets |res| to -|a| mod P.
Steven Valdez909b19f2016-11-21 15:35:44 -050045void ecp_nistz256_neg(BN_ULONG res[P256_LIMBS], const BN_ULONG a[P256_LIMBS]);
46
Robert Sloan8f860b12017-08-28 07:37:06 -070047// ecp_nistz256_mul_mont sets |res| to |a| * |b| * 2^-256 mod P.
Steven Valdez909b19f2016-11-21 15:35:44 -050048void ecp_nistz256_mul_mont(BN_ULONG res[P256_LIMBS],
49 const BN_ULONG a[P256_LIMBS],
50 const BN_ULONG b[P256_LIMBS]);
51
Robert Sloan8f860b12017-08-28 07:37:06 -070052// ecp_nistz256_sqr_mont sets |res| to |a| * |a| * 2^-256 mod P.
Steven Valdez909b19f2016-11-21 15:35:44 -050053void ecp_nistz256_sqr_mont(BN_ULONG res[P256_LIMBS],
54 const BN_ULONG a[P256_LIMBS]);
55
Robert Sloan8f860b12017-08-28 07:37:06 -070056// ecp_nistz256_from_mont sets |res| to |in|, converted from Montgomery domain
57// by multiplying with 1.
Steven Valdezb0b45c62017-01-17 16:23:54 -050058static inline void ecp_nistz256_from_mont(BN_ULONG res[P256_LIMBS],
59 const BN_ULONG in[P256_LIMBS]) {
60 static const BN_ULONG ONE[P256_LIMBS] = { 1 };
61 ecp_nistz256_mul_mont(res, in, ONE);
62}
Steven Valdez909b19f2016-11-21 15:35:44 -050063
64
Robert Sloan8f860b12017-08-28 07:37:06 -070065// P-256 point operations.
66//
67// The following functions may be used in-place. All coordinates are in the
68// Montgomery domain.
Steven Valdez909b19f2016-11-21 15:35:44 -050069
Robert Sloan8f860b12017-08-28 07:37:06 -070070// A P256_POINT represents a P-256 point in Jacobian coordinates.
Steven Valdez909b19f2016-11-21 15:35:44 -050071typedef struct {
72 BN_ULONG X[P256_LIMBS];
73 BN_ULONG Y[P256_LIMBS];
74 BN_ULONG Z[P256_LIMBS];
75} P256_POINT;
76
Robert Sloan8f860b12017-08-28 07:37:06 -070077// A P256_POINT_AFFINE represents a P-256 point in affine coordinates. Infinity
78// is encoded as (0, 0).
Steven Valdez909b19f2016-11-21 15:35:44 -050079typedef struct {
80 BN_ULONG X[P256_LIMBS];
81 BN_ULONG Y[P256_LIMBS];
82} P256_POINT_AFFINE;
83
Robert Sloan8f860b12017-08-28 07:37:06 -070084// ecp_nistz256_select_w5 sets |*val| to |in_t[index-1]| if 1 <= |index| <= 16
85// and all zeros (the point at infinity) if |index| is 0. This is done in
86// constant time.
Steven Valdez909b19f2016-11-21 15:35:44 -050087void ecp_nistz256_select_w5(P256_POINT *val, const P256_POINT in_t[16],
88 int index);
89
Robert Sloan8f860b12017-08-28 07:37:06 -070090// ecp_nistz256_select_w7 sets |*val| to |in_t[index-1]| if 1 <= |index| <= 64
91// and all zeros (the point at infinity) if |index| is 0. This is done in
92// constant time.
Steven Valdez909b19f2016-11-21 15:35:44 -050093void ecp_nistz256_select_w7(P256_POINT_AFFINE *val,
94 const P256_POINT_AFFINE in_t[64], int index);
95
Robert Sloan8f860b12017-08-28 07:37:06 -070096// ecp_nistz256_point_double sets |r| to |a| doubled.
Steven Valdez909b19f2016-11-21 15:35:44 -050097void ecp_nistz256_point_double(P256_POINT *r, const P256_POINT *a);
98
Robert Sloan8f860b12017-08-28 07:37:06 -070099// ecp_nistz256_point_add adds |a| to |b| and places the result in |r|.
Steven Valdez909b19f2016-11-21 15:35:44 -0500100void ecp_nistz256_point_add(P256_POINT *r, const P256_POINT *a,
101 const P256_POINT *b);
102
Robert Sloan8f860b12017-08-28 07:37:06 -0700103// ecp_nistz256_point_add_affine adds |a| to |b| and places the result in
104// |r|. |a| and |b| must not represent the same point unless they are both
105// infinity.
Steven Valdez909b19f2016-11-21 15:35:44 -0500106void ecp_nistz256_point_add_affine(P256_POINT *r, const P256_POINT *a,
107 const P256_POINT_AFFINE *b);
108
109#endif /* !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && \
110 !defined(OPENSSL_SMALL) */
111
112
113#if defined(__cplusplus)
Robert Sloan8f860b12017-08-28 07:37:06 -0700114} // extern C++
Steven Valdez909b19f2016-11-21 15:35:44 -0500115#endif
116
Robert Sloan8f860b12017-08-28 07:37:06 -0700117#endif // OPENSSL_HEADER_EC_P256_X86_64_H