1 /*
2  * Copyright 2014-2016 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright (c) 2014, Intel Corporation. All Rights Reserved.
4  *
5  * 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
9  *
10  * 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  */
18 
19 #ifndef OPENSSL_HEADER_EC_P256_SHARED_H
20 #define OPENSSL_HEADER_EC_P256_SHARED_H
21 
22 #include "ring-core/base.h"
23 
24 #include "../bn/internal.h"
25 
26 #if !defined(OPENSSL_NO_ASM) && \
27     (defined(OPENSSL_X86_64) || defined(OPENSSL_AARCH64)) && \
28     !defined(OPENSSL_SMALL)
29 # define OPENSSL_USE_NISTZ256
30 #endif
31 
32 // P-256 field operations.
33 //
34 // An element mod P in P-256 is represented as a little-endian array of
35 // |P256_LIMBS| |BN_ULONG|s, spanning the full range of values.
36 //
37 // The following functions take fully-reduced inputs mod P and give
38 // fully-reduced outputs. They may be used in-place.
39 
40 #define P256_LIMBS (256 / BN_BITS2)
41 
42 // A P256_POINT represents a P-256 point in Jacobian coordinates.
43 // All coordinates are in the Montgomery domain.
44 typedef struct {
45   BN_ULONG X[P256_LIMBS];
46   BN_ULONG Y[P256_LIMBS];
47   BN_ULONG Z[P256_LIMBS];
48 } P256_POINT;
49 
50 typedef unsigned char P256_SCALAR_BYTES[33];
51 
p256_scalar_bytes_from_limbs(P256_SCALAR_BYTES bytes_out,const BN_ULONG limbs[P256_LIMBS])52 static inline void p256_scalar_bytes_from_limbs(
53     P256_SCALAR_BYTES bytes_out, const BN_ULONG limbs[P256_LIMBS]) {
54   OPENSSL_memcpy(bytes_out, limbs, 32);
55   bytes_out[32] = 0;
56 }
57 
58 #endif /* !defined(OPENSSL_USE_NISTZ256) */
59