xref: /btstack/3rd-party/micro-ecc/uECC.c (revision af03003c8ac55cf0eea9563b597879b24aee256f)
1*af03003cSMatthias Ringwald /* Copyright 2014, Kenneth MacKay. Licensed under the BSD 2-clause license. */
2*af03003cSMatthias Ringwald 
3*af03003cSMatthias Ringwald #include "uECC.h"
4*af03003cSMatthias Ringwald 
5*af03003cSMatthias Ringwald #ifndef uECC_PLATFORM
6*af03003cSMatthias Ringwald     #if __AVR__
7*af03003cSMatthias Ringwald         #define uECC_PLATFORM uECC_avr
8*af03003cSMatthias Ringwald     #elif defined(__thumb2__) || defined(_M_ARMT) /* I think MSVC only supports Thumb-2 targets */
9*af03003cSMatthias Ringwald         #define uECC_PLATFORM uECC_arm_thumb2
10*af03003cSMatthias Ringwald     #elif defined(__thumb__)
11*af03003cSMatthias Ringwald         #define uECC_PLATFORM uECC_arm_thumb
12*af03003cSMatthias Ringwald     #elif defined(__arm__) || defined(_M_ARM)
13*af03003cSMatthias Ringwald         #define uECC_PLATFORM uECC_arm
14*af03003cSMatthias Ringwald     #elif defined(__i386__) || defined(_M_IX86) || defined(_X86_) || defined(__I86__)
15*af03003cSMatthias Ringwald         #define uECC_PLATFORM uECC_x86
16*af03003cSMatthias Ringwald     #elif defined(__amd64__) || defined(_M_X64)
17*af03003cSMatthias Ringwald         #define uECC_PLATFORM uECC_x86_64
18*af03003cSMatthias Ringwald     #else
19*af03003cSMatthias Ringwald         #define uECC_PLATFORM uECC_arch_other
20*af03003cSMatthias Ringwald     #endif
21*af03003cSMatthias Ringwald #endif
22*af03003cSMatthias Ringwald 
23*af03003cSMatthias Ringwald #ifndef uECC_WORD_SIZE
24*af03003cSMatthias Ringwald     #if uECC_PLATFORM == uECC_avr
25*af03003cSMatthias Ringwald         #define uECC_WORD_SIZE 1
26*af03003cSMatthias Ringwald     #elif (uECC_PLATFORM == uECC_x86_64)
27*af03003cSMatthias Ringwald         #define uECC_WORD_SIZE 8
28*af03003cSMatthias Ringwald     #else
29*af03003cSMatthias Ringwald         #define uECC_WORD_SIZE 4
30*af03003cSMatthias Ringwald     #endif
31*af03003cSMatthias Ringwald #endif
32*af03003cSMatthias Ringwald 
33*af03003cSMatthias Ringwald #if (uECC_CURVE == uECC_secp160r1 || uECC_CURVE == uECC_secp224r1) && (uECC_WORD_SIZE == 8)
34*af03003cSMatthias Ringwald     #undef uECC_WORD_SIZE
35*af03003cSMatthias Ringwald     #define uECC_WORD_SIZE 4
36*af03003cSMatthias Ringwald     #if (uECC_PLATFORM == uECC_x86_64)
37*af03003cSMatthias Ringwald         #undef uECC_PLATFORM
38*af03003cSMatthias Ringwald         #define uECC_PLATFORM uECC_x86
39*af03003cSMatthias Ringwald     #endif
40*af03003cSMatthias Ringwald #endif
41*af03003cSMatthias Ringwald 
42*af03003cSMatthias Ringwald #if (uECC_WORD_SIZE != 1) && (uECC_WORD_SIZE != 4) && (uECC_WORD_SIZE != 8)
43*af03003cSMatthias Ringwald     #error "Unsupported value for uECC_WORD_SIZE"
44*af03003cSMatthias Ringwald #endif
45*af03003cSMatthias Ringwald 
46*af03003cSMatthias Ringwald #if (uECC_ASM && (uECC_PLATFORM == uECC_avr) && (uECC_WORD_SIZE != 1))
47*af03003cSMatthias Ringwald     #pragma message ("uECC_WORD_SIZE must be 1 when using AVR asm")
48*af03003cSMatthias Ringwald     #undef uECC_WORD_SIZE
49*af03003cSMatthias Ringwald     #define uECC_WORD_SIZE 1
50*af03003cSMatthias Ringwald #endif
51*af03003cSMatthias Ringwald 
52*af03003cSMatthias Ringwald #if (uECC_ASM && \
53*af03003cSMatthias Ringwald      (uECC_PLATFORM == uECC_arm || uECC_PLATFORM == uECC_arm_thumb) && \
54*af03003cSMatthias Ringwald      (uECC_WORD_SIZE != 4))
55*af03003cSMatthias Ringwald     #pragma message ("uECC_WORD_SIZE must be 4 when using ARM asm")
56*af03003cSMatthias Ringwald     #undef uECC_WORD_SIZE
57*af03003cSMatthias Ringwald     #define uECC_WORD_SIZE 4
58*af03003cSMatthias Ringwald #endif
59*af03003cSMatthias Ringwald 
60*af03003cSMatthias Ringwald #if __STDC_VERSION__ >= 199901L
61*af03003cSMatthias Ringwald     #define RESTRICT restrict
62*af03003cSMatthias Ringwald #else
63*af03003cSMatthias Ringwald     #define RESTRICT
64*af03003cSMatthias Ringwald #endif
65*af03003cSMatthias Ringwald 
66*af03003cSMatthias Ringwald #if defined(__SIZEOF_INT128__) || ((__clang_major__ * 100 + __clang_minor__) >= 302)
67*af03003cSMatthias Ringwald     #define SUPPORTS_INT128 1
68*af03003cSMatthias Ringwald #else
69*af03003cSMatthias Ringwald     #define SUPPORTS_INT128 0
70*af03003cSMatthias Ringwald #endif
71*af03003cSMatthias Ringwald 
72*af03003cSMatthias Ringwald #define MAX_TRIES 64
73*af03003cSMatthias Ringwald 
74*af03003cSMatthias Ringwald #if (uECC_WORD_SIZE == 1)
75*af03003cSMatthias Ringwald 
76*af03003cSMatthias Ringwald typedef uint8_t uECC_word_t;
77*af03003cSMatthias Ringwald typedef uint16_t uECC_dword_t;
78*af03003cSMatthias Ringwald typedef uint8_t wordcount_t;
79*af03003cSMatthias Ringwald typedef int8_t swordcount_t;
80*af03003cSMatthias Ringwald typedef int16_t bitcount_t;
81*af03003cSMatthias Ringwald typedef int8_t cmpresult_t;
82*af03003cSMatthias Ringwald 
83*af03003cSMatthias Ringwald #define HIGH_BIT_SET 0x80
84*af03003cSMatthias Ringwald #define uECC_WORD_BITS 8
85*af03003cSMatthias Ringwald #define uECC_WORD_BITS_SHIFT 3
86*af03003cSMatthias Ringwald #define uECC_WORD_BITS_MASK 0x07
87*af03003cSMatthias Ringwald 
88*af03003cSMatthias Ringwald #define uECC_WORDS_1 20
89*af03003cSMatthias Ringwald #define uECC_WORDS_2 24
90*af03003cSMatthias Ringwald #define uECC_WORDS_3 32
91*af03003cSMatthias Ringwald #define uECC_WORDS_4 32
92*af03003cSMatthias Ringwald #define uECC_WORDS_5 28
93*af03003cSMatthias Ringwald 
94*af03003cSMatthias Ringwald #define uECC_N_WORDS_1 21
95*af03003cSMatthias Ringwald #define uECC_N_WORDS_2 24
96*af03003cSMatthias Ringwald #define uECC_N_WORDS_3 32
97*af03003cSMatthias Ringwald #define uECC_N_WORDS_4 32
98*af03003cSMatthias Ringwald #define uECC_N_WORDS_5 28
99*af03003cSMatthias Ringwald 
100*af03003cSMatthias Ringwald #define Curve_P_1 {0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, \
101*af03003cSMatthias Ringwald                    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \
102*af03003cSMatthias Ringwald                    0xFF, 0xFF, 0xFF, 0xFF}
103*af03003cSMatthias Ringwald #define Curve_P_2 {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \
104*af03003cSMatthias Ringwald                    0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \
105*af03003cSMatthias Ringwald                    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
106*af03003cSMatthias Ringwald #define Curve_P_3 {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \
107*af03003cSMatthias Ringwald                    0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, \
108*af03003cSMatthias Ringwald                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
109*af03003cSMatthias Ringwald                    0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF}
110*af03003cSMatthias Ringwald #define Curve_P_4 {0x2F, 0xFC, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, \
111*af03003cSMatthias Ringwald                    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \
112*af03003cSMatthias Ringwald                    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \
113*af03003cSMatthias Ringwald                    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
114*af03003cSMatthias Ringwald #define Curve_P_5 {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
115*af03003cSMatthias Ringwald                    0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, \
116*af03003cSMatthias Ringwald                    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \
117*af03003cSMatthias Ringwald                    0xFF, 0xFF, 0xFF, 0xFF}
118*af03003cSMatthias Ringwald 
119*af03003cSMatthias Ringwald #define Curve_B_1 {0x45, 0xFA, 0x65, 0xC5, 0xAD, 0xD4, 0xD4, 0x81, \
120*af03003cSMatthias Ringwald                    0x9F, 0xF8, 0xAC, 0x65, 0x8B, 0x7A, 0xBD, 0x54, \
121*af03003cSMatthias Ringwald                    0xFC, 0xBE, 0x97, 0x1C}
122*af03003cSMatthias Ringwald #define Curve_B_2 {0xB1, 0xB9, 0x46, 0xC1, 0xEC, 0xDE, 0xB8, 0xFE, \
123*af03003cSMatthias Ringwald                    0x49, 0x30, 0x24, 0x72, 0xAB, 0xE9, 0xA7, 0x0F, \
124*af03003cSMatthias Ringwald                    0xE7, 0x80, 0x9C, 0xE5, 0x19, 0x05, 0x21, 0x64}
125*af03003cSMatthias Ringwald #define Curve_B_3 {0x4B, 0x60, 0xD2, 0x27, 0x3E, 0x3C, 0xCE, 0x3B, \
126*af03003cSMatthias Ringwald                    0xF6, 0xB0, 0x53, 0xCC, 0xB0, 0x06, 0x1D, 0x65, \
127*af03003cSMatthias Ringwald                    0xBC, 0x86, 0x98, 0x76, 0x55, 0xBD, 0xEB, 0xB3, \
128*af03003cSMatthias Ringwald                    0xE7, 0x93, 0x3A, 0xAA, 0xD8, 0x35, 0xC6, 0x5A}
129*af03003cSMatthias Ringwald #define Curve_B_4 {0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
130*af03003cSMatthias Ringwald                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
131*af03003cSMatthias Ringwald                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
132*af03003cSMatthias Ringwald                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
133*af03003cSMatthias Ringwald #define Curve_B_5 {0xB4, 0xFF, 0x55, 0x23, 0x43, 0x39, 0x0B, 0x27, \
134*af03003cSMatthias Ringwald                    0xBA, 0xD8, 0xBF, 0xD7, 0xB7, 0xB0, 0x44, 0x50, \
135*af03003cSMatthias Ringwald                    0x56, 0x32, 0x41, 0xF5, 0xAB, 0xB3, 0x04, 0x0C, \
136*af03003cSMatthias Ringwald                    0x85, 0x0A, 0x05, 0xB4}
137*af03003cSMatthias Ringwald 
138*af03003cSMatthias Ringwald #define Curve_G_1 { \
139*af03003cSMatthias Ringwald     {0x82, 0xFC, 0xCB, 0x13, 0xB9, 0x8B, 0xC3, 0x68, \
140*af03003cSMatthias Ringwald         0x89, 0x69, 0x64, 0x46, 0x28, 0x73, 0xF5, 0x8E, \
141*af03003cSMatthias Ringwald         0x68, 0xB5, 0x96, 0x4A}, \
142*af03003cSMatthias Ringwald     {0x32, 0xFB, 0xC5, 0x7A, 0x37, 0x51, 0x23, 0x04, \
143*af03003cSMatthias Ringwald         0x12, 0xC9, 0xDC, 0x59, 0x7D, 0x94, 0x68, 0x31, \
144*af03003cSMatthias Ringwald         0x55, 0x28, 0xA6, 0x23}}
145*af03003cSMatthias Ringwald 
146*af03003cSMatthias Ringwald #define Curve_G_2 { \
147*af03003cSMatthias Ringwald     {0x12, 0x10, 0xFF, 0x82, 0xFD, 0x0A, 0xFF, 0xF4, \
148*af03003cSMatthias Ringwald         0x00, 0x88, 0xA1, 0x43, 0xEB, 0x20, 0xBF, 0x7C, \
149*af03003cSMatthias Ringwald         0xF6, 0x90, 0x30, 0xB0, 0x0E, 0xA8, 0x8D, 0x18}, \
150*af03003cSMatthias Ringwald     {0x11, 0x48, 0x79, 0x1E, 0xA1, 0x77, 0xF9, 0x73, \
151*af03003cSMatthias Ringwald         0xD5, 0xCD, 0x24, 0x6B, 0xED, 0x11, 0x10, 0x63, \
152*af03003cSMatthias Ringwald         0x78, 0xDA, 0xC8, 0xFF, 0x95, 0x2B, 0x19, 0x07}}
153*af03003cSMatthias Ringwald 
154*af03003cSMatthias Ringwald #define Curve_G_3 { \
155*af03003cSMatthias Ringwald     {0x96, 0xC2, 0x98, 0xD8, 0x45, 0x39, 0xA1, 0xF4, \
156*af03003cSMatthias Ringwald         0xA0, 0x33, 0xEB, 0x2D, 0x81, 0x7D, 0x03, 0x77, \
157*af03003cSMatthias Ringwald         0xF2, 0x40, 0xA4, 0x63, 0xE5, 0xE6, 0xBC, 0xF8, \
158*af03003cSMatthias Ringwald         0x47, 0x42, 0x2C, 0xE1, 0xF2, 0xD1, 0x17, 0x6B}, \
159*af03003cSMatthias Ringwald     {0xF5, 0x51, 0xBF, 0x37, 0x68, 0x40, 0xB6, 0xCB, \
160*af03003cSMatthias Ringwald         0xCE, 0x5E, 0x31, 0x6B, 0x57, 0x33, 0xCE, 0x2B, \
161*af03003cSMatthias Ringwald         0x16, 0x9E, 0x0F, 0x7C, 0x4A, 0xEB, 0xE7, 0x8E, \
162*af03003cSMatthias Ringwald         0x9B, 0x7F, 0x1A, 0xFE, 0xE2, 0x42, 0xE3, 0x4F}}
163*af03003cSMatthias Ringwald 
164*af03003cSMatthias Ringwald #define Curve_G_4 { \
165*af03003cSMatthias Ringwald     {0x98, 0x17, 0xF8, 0x16, 0x5B, 0x81, 0xF2, 0x59, \
166*af03003cSMatthias Ringwald         0xD9, 0x28, 0xCE, 0x2D, 0xDB, 0xFC, 0x9B, 0x02, \
167*af03003cSMatthias Ringwald         0x07, 0x0B, 0x87, 0xCE, 0x95, 0x62, 0xA0, 0x55, \
168*af03003cSMatthias Ringwald         0xAC, 0xBB, 0xDC, 0xF9, 0x7E, 0x66, 0xBE, 0x79}, \
169*af03003cSMatthias Ringwald     {0xB8, 0xD4, 0x10, 0xFB, 0x8F, 0xD0, 0x47, 0x9C, \
170*af03003cSMatthias Ringwald         0x19, 0x54, 0x85, 0xA6, 0x48, 0xB4, 0x17, 0xFD, \
171*af03003cSMatthias Ringwald         0xA8, 0x08, 0x11, 0x0E, 0xFC, 0xFB, 0xA4, 0x5D, \
172*af03003cSMatthias Ringwald         0x65, 0xC4, 0xA3, 0x26, 0x77, 0xDA, 0x3A, 0x48}}
173*af03003cSMatthias Ringwald 
174*af03003cSMatthias Ringwald #define Curve_G_5 { \
175*af03003cSMatthias Ringwald     {0x21, 0x1D, 0x5C, 0x11, 0xD6, 0x80, 0x32, 0x34, \
176*af03003cSMatthias Ringwald         0x22, 0x11, 0xC2, 0x56, 0xD3, 0xC1, 0x03, 0x4A, \
177*af03003cSMatthias Ringwald         0xB9, 0x90, 0x13, 0x32, 0x7F, 0xBF, 0xB4, 0x6B, \
178*af03003cSMatthias Ringwald         0xBD, 0x0C, 0x0E, 0xB7}, \
179*af03003cSMatthias Ringwald     {0x34, 0x7E, 0x00, 0x85, 0x99, 0x81, 0xD5, 0x44, \
180*af03003cSMatthias Ringwald         0x64, 0x47, 0x07, 0x5A, 0xA0, 0x75, 0x43, 0xCD, \
181*af03003cSMatthias Ringwald         0xE6, 0xDF, 0x22, 0x4C, 0xFB, 0x23, 0xF7, 0xB5, \
182*af03003cSMatthias Ringwald         0x88, 0x63, 0x37, 0xBD}}
183*af03003cSMatthias Ringwald 
184*af03003cSMatthias Ringwald #define Curve_N_1 {0x57, 0x22, 0x75, 0xCA, 0xD3, 0xAE, 0x27, 0xF9, \
185*af03003cSMatthias Ringwald                    0xC8, 0xF4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, \
186*af03003cSMatthias Ringwald                    0x00, 0x00, 0x00, 0x00, 0x01}
187*af03003cSMatthias Ringwald #define Curve_N_2 {0x31, 0x28, 0xD2, 0xB4, 0xB1, 0xC9, 0x6B, 0x14, \
188*af03003cSMatthias Ringwald                    0x36, 0xF8, 0xDE, 0x99, 0xFF, 0xFF, 0xFF, 0xFF, \
189*af03003cSMatthias Ringwald                    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
190*af03003cSMatthias Ringwald #define Curve_N_3 {0x51, 0x25, 0x63, 0xFC, 0xC2, 0xCA, 0xB9, 0xF3, \
191*af03003cSMatthias Ringwald                    0x84, 0x9E, 0x17, 0xA7, 0xAD, 0xFA, 0xE6, 0xBC, \
192*af03003cSMatthias Ringwald                    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \
193*af03003cSMatthias Ringwald                    0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF}
194*af03003cSMatthias Ringwald #define Curve_N_4 {0x41, 0x41, 0x36, 0xD0, 0x8C, 0x5E, 0xD2, 0xBF, \
195*af03003cSMatthias Ringwald                    0x3B, 0xA0, 0x48, 0xAF, 0xE6, 0xDC, 0xAE, 0xBA, \
196*af03003cSMatthias Ringwald                    0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \
197*af03003cSMatthias Ringwald                    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
198*af03003cSMatthias Ringwald #define Curve_N_5 {0x3D, 0x2A, 0x5C, 0x5C, 0x45, 0x29, 0xDD, 0x13, \
199*af03003cSMatthias Ringwald                    0x3E, 0xF0, 0xB8, 0xE0, 0xA2, 0x16, 0xFF, 0xFF, \
200*af03003cSMatthias Ringwald                    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \
201*af03003cSMatthias Ringwald                    0xFF, 0xFF, 0xFF, 0xFF}
202*af03003cSMatthias Ringwald 
203*af03003cSMatthias Ringwald #elif (uECC_WORD_SIZE == 4)
204*af03003cSMatthias Ringwald 
205*af03003cSMatthias Ringwald typedef uint32_t uECC_word_t;
206*af03003cSMatthias Ringwald typedef uint64_t uECC_dword_t;
207*af03003cSMatthias Ringwald typedef unsigned wordcount_t;
208*af03003cSMatthias Ringwald typedef int swordcount_t;
209*af03003cSMatthias Ringwald typedef int bitcount_t;
210*af03003cSMatthias Ringwald typedef int cmpresult_t;
211*af03003cSMatthias Ringwald 
212*af03003cSMatthias Ringwald #define HIGH_BIT_SET 0x80000000
213*af03003cSMatthias Ringwald #define uECC_WORD_BITS 32
214*af03003cSMatthias Ringwald #define uECC_WORD_BITS_SHIFT 5
215*af03003cSMatthias Ringwald #define uECC_WORD_BITS_MASK 0x01F
216*af03003cSMatthias Ringwald 
217*af03003cSMatthias Ringwald #define uECC_WORDS_1 5
218*af03003cSMatthias Ringwald #define uECC_WORDS_2 6
219*af03003cSMatthias Ringwald #define uECC_WORDS_3 8
220*af03003cSMatthias Ringwald #define uECC_WORDS_4 8
221*af03003cSMatthias Ringwald #define uECC_WORDS_5 7
222*af03003cSMatthias Ringwald 
223*af03003cSMatthias Ringwald #define uECC_N_WORDS_1 6
224*af03003cSMatthias Ringwald #define uECC_N_WORDS_2 6
225*af03003cSMatthias Ringwald #define uECC_N_WORDS_3 8
226*af03003cSMatthias Ringwald #define uECC_N_WORDS_4 8
227*af03003cSMatthias Ringwald #define uECC_N_WORDS_5 7
228*af03003cSMatthias Ringwald 
229*af03003cSMatthias Ringwald #define Curve_P_1 {0x7FFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF}
230*af03003cSMatthias Ringwald #define Curve_P_2 {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF}
231*af03003cSMatthias Ringwald #define Curve_P_3 {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, \
232*af03003cSMatthias Ringwald                    0x00000000, 0x00000000, 0x00000001, 0xFFFFFFFF}
233*af03003cSMatthias Ringwald #define Curve_P_4 {0xFFFFFC2F, 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, \
234*af03003cSMatthias Ringwald                    0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF}
235*af03003cSMatthias Ringwald #define Curve_P_5 {0x00000001, 0x00000000, 0x00000000, 0xFFFFFFFF, \
236*af03003cSMatthias Ringwald                    0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF}
237*af03003cSMatthias Ringwald 
238*af03003cSMatthias Ringwald #define Curve_B_1 {0xC565FA45, 0x81D4D4AD, 0x65ACF89F, 0x54BD7A8B, 0x1C97BEFC}
239*af03003cSMatthias Ringwald #define Curve_B_2 {0xC146B9B1, 0xFEB8DEEC, 0x72243049, 0x0FA7E9AB, 0xE59C80E7, 0x64210519}
240*af03003cSMatthias Ringwald #define Curve_B_3 {0x27D2604B, 0x3BCE3C3E, 0xCC53B0F6, 0x651D06B0, \
241*af03003cSMatthias Ringwald                    0x769886BC, 0xB3EBBD55, 0xAA3A93E7, 0x5AC635D8}
242*af03003cSMatthias Ringwald #define Curve_B_4 {0x00000007, 0x00000000, 0x00000000, 0x00000000, \
243*af03003cSMatthias Ringwald                    0x00000000, 0x00000000, 0x00000000, 0x00000000}
244*af03003cSMatthias Ringwald #define Curve_B_5 {0x2355FFB4, 0x270B3943, 0xD7BFD8BA, 0x5044B0B7, \
245*af03003cSMatthias Ringwald                    0xF5413256, 0x0C04B3AB, 0xB4050A85}
246*af03003cSMatthias Ringwald 
247*af03003cSMatthias Ringwald #define Curve_G_1 { \
248*af03003cSMatthias Ringwald     {0x13CBFC82, 0x68C38BB9, 0x46646989, 0x8EF57328, 0x4A96B568}, \
249*af03003cSMatthias Ringwald     {0x7AC5FB32, 0x04235137, 0x59DCC912, 0x3168947D, 0x23A62855}}
250*af03003cSMatthias Ringwald 
251*af03003cSMatthias Ringwald #define Curve_G_2 { \
252*af03003cSMatthias Ringwald     {0x82FF1012, 0xF4FF0AFD, 0x43A18800, 0x7CBF20EB, 0xB03090F6, 0x188DA80E}, \
253*af03003cSMatthias Ringwald     {0x1E794811, 0x73F977A1, 0x6B24CDD5, 0x631011ED, 0xFFC8DA78, 0x07192B95}}
254*af03003cSMatthias Ringwald 
255*af03003cSMatthias Ringwald #define Curve_G_3 { \
256*af03003cSMatthias Ringwald     {0xD898C296, 0xF4A13945, 0x2DEB33A0, 0x77037D81,  \
257*af03003cSMatthias Ringwald      0x63A440F2, 0xF8BCE6E5, 0xE12C4247, 0x6B17D1F2}, \
258*af03003cSMatthias Ringwald     {0x37BF51F5, 0xCBB64068, 0x6B315ECE, 0x2BCE3357,  \
259*af03003cSMatthias Ringwald      0x7C0F9E16, 0x8EE7EB4A, 0xFE1A7F9B, 0x4FE342E2}}
260*af03003cSMatthias Ringwald 
261*af03003cSMatthias Ringwald #define Curve_G_4 { \
262*af03003cSMatthias Ringwald     {0x16F81798, 0x59F2815B, 0x2DCE28D9, 0x029BFCDB,  \
263*af03003cSMatthias Ringwald      0xCE870B07, 0x55A06295, 0xF9DCBBAC, 0x79BE667E}, \
264*af03003cSMatthias Ringwald     {0xFB10D4B8, 0x9C47D08F, 0xA6855419, 0xFD17B448,  \
265*af03003cSMatthias Ringwald      0x0E1108A8, 0x5DA4FBFC, 0x26A3C465, 0x483ADA77}}
266*af03003cSMatthias Ringwald 
267*af03003cSMatthias Ringwald #define Curve_G_5 { \
268*af03003cSMatthias Ringwald     {0x115C1D21, 0x343280D6, 0x56C21122, 0x4A03C1D3, \
269*af03003cSMatthias Ringwald      0x321390B9, 0x6BB4BF7F, 0xB70E0CBD}, \
270*af03003cSMatthias Ringwald     {0x85007E34, 0x44D58199, 0x5A074764, 0xCD4375A0, \
271*af03003cSMatthias Ringwald      0x4C22DFE6, 0xB5F723FB, 0xBD376388}}
272*af03003cSMatthias Ringwald 
273*af03003cSMatthias Ringwald #define Curve_N_1 {0xCA752257, 0xF927AED3, 0x0001F4C8, 0x00000000, 0x00000000, 0x00000001}
274*af03003cSMatthias Ringwald #define Curve_N_2 {0xB4D22831, 0x146BC9B1, 0x99DEF836, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF}
275*af03003cSMatthias Ringwald #define Curve_N_3 {0xFC632551, 0xF3B9CAC2, 0xA7179E84, 0xBCE6FAAD, \
276*af03003cSMatthias Ringwald                    0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0xFFFFFFFF}
277*af03003cSMatthias Ringwald #define Curve_N_4 {0xD0364141, 0xBFD25E8C, 0xAF48A03B, 0xBAAEDCE6, \
278*af03003cSMatthias Ringwald                    0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF}
279*af03003cSMatthias Ringwald #define Curve_N_5 {0x5C5C2A3D, 0x13DD2945, 0xE0B8F03E, 0xFFFF16A2, \
280*af03003cSMatthias Ringwald                    0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF}
281*af03003cSMatthias Ringwald 
282*af03003cSMatthias Ringwald #elif (uECC_WORD_SIZE == 8)
283*af03003cSMatthias Ringwald 
284*af03003cSMatthias Ringwald typedef uint64_t uECC_word_t;
285*af03003cSMatthias Ringwald #if SUPPORTS_INT128
286*af03003cSMatthias Ringwald typedef unsigned __int128 uECC_dword_t;
287*af03003cSMatthias Ringwald #endif
288*af03003cSMatthias Ringwald typedef unsigned wordcount_t;
289*af03003cSMatthias Ringwald typedef int swordcount_t;
290*af03003cSMatthias Ringwald typedef int bitcount_t;
291*af03003cSMatthias Ringwald typedef int cmpresult_t;
292*af03003cSMatthias Ringwald 
293*af03003cSMatthias Ringwald #define HIGH_BIT_SET 0x8000000000000000ull
294*af03003cSMatthias Ringwald #define uECC_WORD_BITS 64
295*af03003cSMatthias Ringwald #define uECC_WORD_BITS_SHIFT 6
296*af03003cSMatthias Ringwald #define uECC_WORD_BITS_MASK 0x03F
297*af03003cSMatthias Ringwald 
298*af03003cSMatthias Ringwald #define uECC_WORDS_1 3
299*af03003cSMatthias Ringwald #define uECC_WORDS_2 3
300*af03003cSMatthias Ringwald #define uECC_WORDS_3 4
301*af03003cSMatthias Ringwald #define uECC_WORDS_4 4
302*af03003cSMatthias Ringwald #define uECC_WORDS_5 4
303*af03003cSMatthias Ringwald 
304*af03003cSMatthias Ringwald #define uECC_N_WORDS_1 3
305*af03003cSMatthias Ringwald #define uECC_N_WORDS_2 3
306*af03003cSMatthias Ringwald #define uECC_N_WORDS_3 4
307*af03003cSMatthias Ringwald #define uECC_N_WORDS_4 4
308*af03003cSMatthias Ringwald #define uECC_N_WORDS_5 4
309*af03003cSMatthias Ringwald 
310*af03003cSMatthias Ringwald #define Curve_P_1 {0xFFFFFFFF7FFFFFFFull, 0xFFFFFFFFFFFFFFFFull, 0x00000000FFFFFFFFull}
311*af03003cSMatthias Ringwald #define Curve_P_2 {0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFFFFFFFFFEull, 0xFFFFFFFFFFFFFFFFull}
312*af03003cSMatthias Ringwald #define Curve_P_3 {0xFFFFFFFFFFFFFFFFull, 0x00000000FFFFFFFFull, \
313*af03003cSMatthias Ringwald                    0x0000000000000000ull, 0xFFFFFFFF00000001ull}
314*af03003cSMatthias Ringwald #define Curve_P_4 {0xFFFFFFFEFFFFFC2Full, 0xFFFFFFFFFFFFFFFFull, \
315*af03003cSMatthias Ringwald                    0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFFFFFFFFFFull}
316*af03003cSMatthias Ringwald #define Curve_P_5 {0x0000000000000001ull, 0xFFFFFFFF00000000ull, \
317*af03003cSMatthias Ringwald                    0xFFFFFFFFFFFFFFFFull, 0x00000000FFFFFFFFull}
318*af03003cSMatthias Ringwald 
319*af03003cSMatthias Ringwald #define Curve_B_1 {0x81D4D4ADC565FA45ull, 0x54BD7A8B65ACF89Full, 0x000000001C97BEFCull}
320*af03003cSMatthias Ringwald #define Curve_B_2 {0xFEB8DEECC146B9B1ull, 0x0FA7E9AB72243049ull, 0x64210519E59C80E7ull}
321*af03003cSMatthias Ringwald #define Curve_B_3 {0x3BCE3C3E27D2604Bull, 0x651D06B0CC53B0F6ull, \
322*af03003cSMatthias Ringwald                    0xB3EBBD55769886BCull, 0x5AC635D8AA3A93E7ull}
323*af03003cSMatthias Ringwald #define Curve_B_4 {0x0000000000000007ull, 0x0000000000000000ull, \
324*af03003cSMatthias Ringwald                    0x0000000000000000ull, 0x0000000000000000ull}
325*af03003cSMatthias Ringwald #define Curve_B_5 {0x270B39432355FFB4ull, 0x5044B0B7D7BFD8BAull, \
326*af03003cSMatthias Ringwald                    0x0C04B3ABF5413256ull, 0x00000000B4050A85ull}
327*af03003cSMatthias Ringwald 
328*af03003cSMatthias Ringwald #define Curve_G_1 { \
329*af03003cSMatthias Ringwald     {0x68C38BB913CBFC82ull, 0x8EF5732846646989ull, 0x000000004A96B568ull}, \
330*af03003cSMatthias Ringwald     {0x042351377AC5FB32ull, 0x3168947D59DCC912ull, 0x0000000023A62855ull}}
331*af03003cSMatthias Ringwald 
332*af03003cSMatthias Ringwald #define Curve_G_2 { \
333*af03003cSMatthias Ringwald     {0xF4FF0AFD82FF1012ull, 0x7CBF20EB43A18800ull, 0x188DA80EB03090F6ull}, \
334*af03003cSMatthias Ringwald     {0x73F977A11E794811ull, 0x631011ED6B24CDD5ull, 0x07192B95FFC8DA78ull}}
335*af03003cSMatthias Ringwald 
336*af03003cSMatthias Ringwald #define Curve_G_3 { \
337*af03003cSMatthias Ringwald     {0xF4A13945D898C296ull, 0x77037D812DEB33A0ull, 0xF8BCE6E563A440F2ull, 0x6B17D1F2E12C4247ull}, \
338*af03003cSMatthias Ringwald     {0xCBB6406837BF51F5ull, 0x2BCE33576B315ECEull, 0x8EE7EB4A7C0F9E16ull, 0x4FE342E2FE1A7F9Bull}}
339*af03003cSMatthias Ringwald 
340*af03003cSMatthias Ringwald #define Curve_G_4 { \
341*af03003cSMatthias Ringwald     {0x59F2815B16F81798ull, 0x029BFCDB2DCE28D9ull, 0x55A06295CE870B07ull, 0x79BE667EF9DCBBACull}, \
342*af03003cSMatthias Ringwald     {0x9C47D08FFB10D4B8ull, 0xFD17B448A6855419ull, 0x5DA4FBFC0E1108A8ull, 0x483ADA7726A3C465ull}}
343*af03003cSMatthias Ringwald 
344*af03003cSMatthias Ringwald #define Curve_G_5 { \
345*af03003cSMatthias Ringwald     {0x343280D6115C1D21ull, 0x4A03C1D356C21122ull, 0x6BB4BF7F321390B9ull, 0x00000000B70E0CBDull}, \
346*af03003cSMatthias Ringwald     {0x44D5819985007E34ull, 0xCD4375A05A074764ull, 0xB5F723FB4C22DFE6ull, 0x00000000BD376388ull}}
347*af03003cSMatthias Ringwald 
348*af03003cSMatthias Ringwald #define Curve_N_1 {0xF927AED3CA752257ull, 0x000000000001F4C8ull, 0x0000000100000000ull}
349*af03003cSMatthias Ringwald #define Curve_N_2 {0x146BC9B1B4D22831ull, 0xFFFFFFFF99DEF836ull, 0xFFFFFFFFFFFFFFFFull}
350*af03003cSMatthias Ringwald #define Curve_N_3 {0xF3B9CAC2FC632551ull, 0xBCE6FAADA7179E84ull, \
351*af03003cSMatthias Ringwald                    0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFF00000000ull}
352*af03003cSMatthias Ringwald #define Curve_N_4 {0xBFD25E8CD0364141ull, 0xBAAEDCE6AF48A03Bull, \
353*af03003cSMatthias Ringwald                    0xFFFFFFFFFFFFFFFEull, 0xFFFFFFFFFFFFFFFFull}
354*af03003cSMatthias Ringwald #define Curve_N_5 {0x13DD29455C5C2A3Dull, 0xFFFF16A2E0B8F03Eull, \
355*af03003cSMatthias Ringwald                    0xFFFFFFFFFFFFFFFFull, 0x00000000FFFFFFFFull}
356*af03003cSMatthias Ringwald 
357*af03003cSMatthias Ringwald #endif /* (uECC_WORD_SIZE == 8) */
358*af03003cSMatthias Ringwald 
359*af03003cSMatthias Ringwald #define uECC_WORDS uECC_CONCAT(uECC_WORDS_, uECC_CURVE)
360*af03003cSMatthias Ringwald #define uECC_N_WORDS uECC_CONCAT(uECC_N_WORDS_, uECC_CURVE)
361*af03003cSMatthias Ringwald 
362*af03003cSMatthias Ringwald typedef struct EccPoint {
363*af03003cSMatthias Ringwald     uECC_word_t x[uECC_WORDS];
364*af03003cSMatthias Ringwald     uECC_word_t y[uECC_WORDS];
365*af03003cSMatthias Ringwald } EccPoint;
366*af03003cSMatthias Ringwald 
367*af03003cSMatthias Ringwald static const uECC_word_t curve_p[uECC_WORDS] = uECC_CONCAT(Curve_P_, uECC_CURVE);
368*af03003cSMatthias Ringwald static const uECC_word_t curve_b[uECC_WORDS] = uECC_CONCAT(Curve_B_, uECC_CURVE);
369*af03003cSMatthias Ringwald static const EccPoint curve_G = uECC_CONCAT(Curve_G_, uECC_CURVE);
370*af03003cSMatthias Ringwald static const uECC_word_t curve_n[uECC_N_WORDS] = uECC_CONCAT(Curve_N_, uECC_CURVE);
371*af03003cSMatthias Ringwald 
372*af03003cSMatthias Ringwald static void vli_clear(uECC_word_t *vli);
373*af03003cSMatthias Ringwald static uECC_word_t vli_isZero(const uECC_word_t *vli);
374*af03003cSMatthias Ringwald static uECC_word_t vli_testBit(const uECC_word_t *vli, bitcount_t bit);
375*af03003cSMatthias Ringwald static bitcount_t vli_numBits(const uECC_word_t *vli, wordcount_t max_words);
376*af03003cSMatthias Ringwald static void vli_set(uECC_word_t *dest, const uECC_word_t *src);
377*af03003cSMatthias Ringwald static cmpresult_t vli_cmp(const uECC_word_t *left, const uECC_word_t *right);
378*af03003cSMatthias Ringwald static cmpresult_t vli_equal(const uECC_word_t *left, const uECC_word_t *right);
379*af03003cSMatthias Ringwald static void vli_rshift1(uECC_word_t *vli);
380*af03003cSMatthias Ringwald static uECC_word_t vli_add(uECC_word_t *result,
381*af03003cSMatthias Ringwald                            const uECC_word_t *left,
382*af03003cSMatthias Ringwald                            const uECC_word_t *right);
383*af03003cSMatthias Ringwald static uECC_word_t vli_sub(uECC_word_t *result,
384*af03003cSMatthias Ringwald                            const uECC_word_t *left,
385*af03003cSMatthias Ringwald                            const uECC_word_t *right);
386*af03003cSMatthias Ringwald static void vli_mult(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right);
387*af03003cSMatthias Ringwald static void vli_modAdd(uECC_word_t *result,
388*af03003cSMatthias Ringwald                        const uECC_word_t *left,
389*af03003cSMatthias Ringwald                        const uECC_word_t *right,
390*af03003cSMatthias Ringwald                        const uECC_word_t *mod);
391*af03003cSMatthias Ringwald static void vli_modSub(uECC_word_t *result,
392*af03003cSMatthias Ringwald                        const uECC_word_t *left,
393*af03003cSMatthias Ringwald                        const uECC_word_t *right,
394*af03003cSMatthias Ringwald                        const uECC_word_t *mod);
395*af03003cSMatthias Ringwald static void vli_mmod_fast(uECC_word_t *RESTRICT result, uECC_word_t *RESTRICT product);
396*af03003cSMatthias Ringwald static void vli_modMult_fast(uECC_word_t *result,
397*af03003cSMatthias Ringwald                              const uECC_word_t *left,
398*af03003cSMatthias Ringwald                              const uECC_word_t *right);
399*af03003cSMatthias Ringwald static void vli_modInv(uECC_word_t *result, const uECC_word_t *input, const uECC_word_t *mod);
400*af03003cSMatthias Ringwald #if uECC_SQUARE_FUNC
401*af03003cSMatthias Ringwald static void vli_square(uECC_word_t *result, const uECC_word_t *left);
402*af03003cSMatthias Ringwald static void vli_modSquare_fast(uECC_word_t *result, const uECC_word_t *left);
403*af03003cSMatthias Ringwald #endif
404*af03003cSMatthias Ringwald 
405*af03003cSMatthias Ringwald #if (defined(_WIN32) || defined(_WIN64))
406*af03003cSMatthias Ringwald /* Windows */
407*af03003cSMatthias Ringwald 
408*af03003cSMatthias Ringwald #define WIN32_LEAN_AND_MEAN
409*af03003cSMatthias Ringwald #include <windows.h>
410*af03003cSMatthias Ringwald #include <wincrypt.h>
411*af03003cSMatthias Ringwald 
412*af03003cSMatthias Ringwald static int default_RNG(uint8_t *dest, unsigned size) {
413*af03003cSMatthias Ringwald     HCRYPTPROV prov;
414*af03003cSMatthias Ringwald     if (!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
415*af03003cSMatthias Ringwald         return 0;
416*af03003cSMatthias Ringwald     }
417*af03003cSMatthias Ringwald 
418*af03003cSMatthias Ringwald     CryptGenRandom(prov, size, (BYTE *)dest);
419*af03003cSMatthias Ringwald     CryptReleaseContext(prov, 0);
420*af03003cSMatthias Ringwald     return 1;
421*af03003cSMatthias Ringwald }
422*af03003cSMatthias Ringwald 
423*af03003cSMatthias Ringwald #elif defined(unix) || defined(__linux__) || defined(__unix__) || defined(__unix) || \
424*af03003cSMatthias Ringwald     (defined(__APPLE__) && defined(__MACH__)) || defined(uECC_POSIX)
425*af03003cSMatthias Ringwald 
426*af03003cSMatthias Ringwald /* Some POSIX-like system with /dev/urandom or /dev/random. */
427*af03003cSMatthias Ringwald #include <sys/types.h>
428*af03003cSMatthias Ringwald #include <fcntl.h>
429*af03003cSMatthias Ringwald #include <unistd.h>
430*af03003cSMatthias Ringwald 
431*af03003cSMatthias Ringwald #ifndef O_CLOEXEC
432*af03003cSMatthias Ringwald     #define O_CLOEXEC 0
433*af03003cSMatthias Ringwald #endif
434*af03003cSMatthias Ringwald 
435*af03003cSMatthias Ringwald static int default_RNG(uint8_t *dest, unsigned size) {
436*af03003cSMatthias Ringwald     int fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC);
437*af03003cSMatthias Ringwald     if (fd == -1) {
438*af03003cSMatthias Ringwald         fd = open("/dev/random", O_RDONLY | O_CLOEXEC);
439*af03003cSMatthias Ringwald         if (fd == -1) {
440*af03003cSMatthias Ringwald             return 0;
441*af03003cSMatthias Ringwald         }
442*af03003cSMatthias Ringwald     }
443*af03003cSMatthias Ringwald 
444*af03003cSMatthias Ringwald     char *ptr = (char *)dest;
445*af03003cSMatthias Ringwald     size_t left = size;
446*af03003cSMatthias Ringwald     while (left > 0) {
447*af03003cSMatthias Ringwald         ssize_t bytes_read = read(fd, ptr, left);
448*af03003cSMatthias Ringwald         if (bytes_read <= 0) { // read failed
449*af03003cSMatthias Ringwald             close(fd);
450*af03003cSMatthias Ringwald             return 0;
451*af03003cSMatthias Ringwald         }
452*af03003cSMatthias Ringwald         left -= bytes_read;
453*af03003cSMatthias Ringwald         ptr += bytes_read;
454*af03003cSMatthias Ringwald     }
455*af03003cSMatthias Ringwald 
456*af03003cSMatthias Ringwald     close(fd);
457*af03003cSMatthias Ringwald     return 1;
458*af03003cSMatthias Ringwald }
459*af03003cSMatthias Ringwald 
460*af03003cSMatthias Ringwald #else /* Some other platform */
461*af03003cSMatthias Ringwald 
462*af03003cSMatthias Ringwald static int default_RNG(uint8_t *dest, unsigned size) {
463*af03003cSMatthias Ringwald     return 0;
464*af03003cSMatthias Ringwald }
465*af03003cSMatthias Ringwald 
466*af03003cSMatthias Ringwald #endif
467*af03003cSMatthias Ringwald 
468*af03003cSMatthias Ringwald static uECC_RNG_Function g_rng_function = &default_RNG;
469*af03003cSMatthias Ringwald 
470*af03003cSMatthias Ringwald void uECC_set_rng(uECC_RNG_Function rng_function) {
471*af03003cSMatthias Ringwald     g_rng_function = rng_function;
472*af03003cSMatthias Ringwald }
473*af03003cSMatthias Ringwald 
474*af03003cSMatthias Ringwald #ifdef __GNUC__ /* Only support GCC inline asm for now */
475*af03003cSMatthias Ringwald     #if (uECC_ASM && (uECC_PLATFORM == uECC_avr))
476*af03003cSMatthias Ringwald         #include "asm_avr.inc"
477*af03003cSMatthias Ringwald     #endif
478*af03003cSMatthias Ringwald 
479*af03003cSMatthias Ringwald     #if (uECC_ASM && (uECC_PLATFORM == uECC_arm || uECC_PLATFORM == uECC_arm_thumb || \
480*af03003cSMatthias Ringwald                       uECC_PLATFORM == uECC_arm_thumb2))
481*af03003cSMatthias Ringwald         #include "asm_arm.inc"
482*af03003cSMatthias Ringwald     #endif
483*af03003cSMatthias Ringwald #endif
484*af03003cSMatthias Ringwald 
485*af03003cSMatthias Ringwald #if !asm_clear
486*af03003cSMatthias Ringwald static void vli_clear(uECC_word_t *vli) {
487*af03003cSMatthias Ringwald     wordcount_t i;
488*af03003cSMatthias Ringwald     for (i = 0; i < uECC_WORDS; ++i) {
489*af03003cSMatthias Ringwald         vli[i] = 0;
490*af03003cSMatthias Ringwald     }
491*af03003cSMatthias Ringwald }
492*af03003cSMatthias Ringwald #endif
493*af03003cSMatthias Ringwald 
494*af03003cSMatthias Ringwald /* Returns 1 if vli == 0, 0 otherwise. */
495*af03003cSMatthias Ringwald #if !asm_isZero
496*af03003cSMatthias Ringwald static uECC_word_t vli_isZero(const uECC_word_t *vli) {
497*af03003cSMatthias Ringwald     wordcount_t i;
498*af03003cSMatthias Ringwald     for (i = 0; i < uECC_WORDS; ++i) {
499*af03003cSMatthias Ringwald         if (vli[i]) {
500*af03003cSMatthias Ringwald             return 0;
501*af03003cSMatthias Ringwald         }
502*af03003cSMatthias Ringwald     }
503*af03003cSMatthias Ringwald     return 1;
504*af03003cSMatthias Ringwald }
505*af03003cSMatthias Ringwald #endif
506*af03003cSMatthias Ringwald 
507*af03003cSMatthias Ringwald /* Returns nonzero if bit 'bit' of vli is set. */
508*af03003cSMatthias Ringwald #if !asm_testBit
509*af03003cSMatthias Ringwald static uECC_word_t vli_testBit(const uECC_word_t *vli, bitcount_t bit) {
510*af03003cSMatthias Ringwald     return (vli[bit >> uECC_WORD_BITS_SHIFT] & ((uECC_word_t)1 << (bit & uECC_WORD_BITS_MASK)));
511*af03003cSMatthias Ringwald }
512*af03003cSMatthias Ringwald #endif
513*af03003cSMatthias Ringwald 
514*af03003cSMatthias Ringwald /* Counts the number of words in vli. */
515*af03003cSMatthias Ringwald #if !asm_numBits
516*af03003cSMatthias Ringwald static wordcount_t vli_numDigits(const uECC_word_t *vli, wordcount_t max_words) {
517*af03003cSMatthias Ringwald     swordcount_t i;
518*af03003cSMatthias Ringwald     /* Search from the end until we find a non-zero digit.
519*af03003cSMatthias Ringwald        We do it in reverse because we expect that most digits will be nonzero. */
520*af03003cSMatthias Ringwald     for (i = max_words - 1; i >= 0 && vli[i] == 0; --i) {
521*af03003cSMatthias Ringwald     }
522*af03003cSMatthias Ringwald 
523*af03003cSMatthias Ringwald     return (i + 1);
524*af03003cSMatthias Ringwald }
525*af03003cSMatthias Ringwald 
526*af03003cSMatthias Ringwald /* Counts the number of bits required to represent vli. */
527*af03003cSMatthias Ringwald static bitcount_t vli_numBits(const uECC_word_t *vli, wordcount_t max_words) {
528*af03003cSMatthias Ringwald     uECC_word_t i;
529*af03003cSMatthias Ringwald     uECC_word_t digit;
530*af03003cSMatthias Ringwald 
531*af03003cSMatthias Ringwald     wordcount_t num_digits = vli_numDigits(vli, max_words);
532*af03003cSMatthias Ringwald     if (num_digits == 0) {
533*af03003cSMatthias Ringwald         return 0;
534*af03003cSMatthias Ringwald     }
535*af03003cSMatthias Ringwald 
536*af03003cSMatthias Ringwald     digit = vli[num_digits - 1];
537*af03003cSMatthias Ringwald     for (i = 0; digit; ++i) {
538*af03003cSMatthias Ringwald         digit >>= 1;
539*af03003cSMatthias Ringwald     }
540*af03003cSMatthias Ringwald 
541*af03003cSMatthias Ringwald     return (((bitcount_t)(num_digits - 1) << uECC_WORD_BITS_SHIFT) + i);
542*af03003cSMatthias Ringwald }
543*af03003cSMatthias Ringwald #endif /* !asm_numBits */
544*af03003cSMatthias Ringwald 
545*af03003cSMatthias Ringwald /* Sets dest = src. */
546*af03003cSMatthias Ringwald #if !asm_set
547*af03003cSMatthias Ringwald static void vli_set(uECC_word_t *dest, const uECC_word_t *src) {
548*af03003cSMatthias Ringwald     wordcount_t i;
549*af03003cSMatthias Ringwald     for (i = 0; i < uECC_WORDS; ++i) {
550*af03003cSMatthias Ringwald         dest[i] = src[i];
551*af03003cSMatthias Ringwald     }
552*af03003cSMatthias Ringwald }
553*af03003cSMatthias Ringwald #endif
554*af03003cSMatthias Ringwald 
555*af03003cSMatthias Ringwald /* Returns sign of left - right. */
556*af03003cSMatthias Ringwald #if !asm_cmp
557*af03003cSMatthias Ringwald static cmpresult_t vli_cmp(const uECC_word_t *left, const uECC_word_t *right) {
558*af03003cSMatthias Ringwald     swordcount_t i;
559*af03003cSMatthias Ringwald     for (i = uECC_WORDS - 1; i >= 0; --i) {
560*af03003cSMatthias Ringwald         if (left[i] > right[i]) {
561*af03003cSMatthias Ringwald             return 1;
562*af03003cSMatthias Ringwald         } else if (left[i] < right[i]) {
563*af03003cSMatthias Ringwald             return -1;
564*af03003cSMatthias Ringwald         }
565*af03003cSMatthias Ringwald     }
566*af03003cSMatthias Ringwald     return 0;
567*af03003cSMatthias Ringwald }
568*af03003cSMatthias Ringwald #endif
569*af03003cSMatthias Ringwald 
570*af03003cSMatthias Ringwald static cmpresult_t vli_equal(const uECC_word_t *left, const uECC_word_t *right) {
571*af03003cSMatthias Ringwald     uECC_word_t result = 0;
572*af03003cSMatthias Ringwald     swordcount_t i;
573*af03003cSMatthias Ringwald     for (i = uECC_WORDS - 1; i >= 0; --i) {
574*af03003cSMatthias Ringwald         result |= (left[i] ^ right[i]);
575*af03003cSMatthias Ringwald     }
576*af03003cSMatthias Ringwald     return (result == 0);
577*af03003cSMatthias Ringwald }
578*af03003cSMatthias Ringwald 
579*af03003cSMatthias Ringwald /* Computes vli = vli >> 1. */
580*af03003cSMatthias Ringwald #if !asm_rshift1
581*af03003cSMatthias Ringwald static void vli_rshift1(uECC_word_t *vli) {
582*af03003cSMatthias Ringwald     uECC_word_t *end = vli;
583*af03003cSMatthias Ringwald     uECC_word_t carry = 0;
584*af03003cSMatthias Ringwald 
585*af03003cSMatthias Ringwald     vli += uECC_WORDS;
586*af03003cSMatthias Ringwald     while (vli-- > end) {
587*af03003cSMatthias Ringwald         uECC_word_t temp = *vli;
588*af03003cSMatthias Ringwald         *vli = (temp >> 1) | carry;
589*af03003cSMatthias Ringwald         carry = temp << (uECC_WORD_BITS - 1);
590*af03003cSMatthias Ringwald     }
591*af03003cSMatthias Ringwald }
592*af03003cSMatthias Ringwald #endif
593*af03003cSMatthias Ringwald 
594*af03003cSMatthias Ringwald /* Computes result = left + right, returning carry. Can modify in place. */
595*af03003cSMatthias Ringwald #if !asm_add
596*af03003cSMatthias Ringwald static uECC_word_t vli_add(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) {
597*af03003cSMatthias Ringwald     uECC_word_t carry = 0;
598*af03003cSMatthias Ringwald     wordcount_t i;
599*af03003cSMatthias Ringwald     for (i = 0; i < uECC_WORDS; ++i) {
600*af03003cSMatthias Ringwald         uECC_word_t sum = left[i] + right[i] + carry;
601*af03003cSMatthias Ringwald         if (sum != left[i]) {
602*af03003cSMatthias Ringwald             carry = (sum < left[i]);
603*af03003cSMatthias Ringwald         }
604*af03003cSMatthias Ringwald         result[i] = sum;
605*af03003cSMatthias Ringwald     }
606*af03003cSMatthias Ringwald     return carry;
607*af03003cSMatthias Ringwald }
608*af03003cSMatthias Ringwald #endif
609*af03003cSMatthias Ringwald 
610*af03003cSMatthias Ringwald /* Computes result = left - right, returning borrow. Can modify in place. */
611*af03003cSMatthias Ringwald #if !asm_sub
612*af03003cSMatthias Ringwald static uECC_word_t vli_sub(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) {
613*af03003cSMatthias Ringwald     uECC_word_t borrow = 0;
614*af03003cSMatthias Ringwald     wordcount_t i;
615*af03003cSMatthias Ringwald     for (i = 0; i < uECC_WORDS; ++i) {
616*af03003cSMatthias Ringwald         uECC_word_t diff = left[i] - right[i] - borrow;
617*af03003cSMatthias Ringwald         if (diff != left[i]) {
618*af03003cSMatthias Ringwald             borrow = (diff > left[i]);
619*af03003cSMatthias Ringwald         }
620*af03003cSMatthias Ringwald         result[i] = diff;
621*af03003cSMatthias Ringwald     }
622*af03003cSMatthias Ringwald     return borrow;
623*af03003cSMatthias Ringwald }
624*af03003cSMatthias Ringwald #endif
625*af03003cSMatthias Ringwald 
626*af03003cSMatthias Ringwald #if (!asm_mult || (uECC_SQUARE_FUNC && !asm_square) || uECC_CURVE == uECC_secp256k1)
627*af03003cSMatthias Ringwald static void muladd(uECC_word_t a,
628*af03003cSMatthias Ringwald                    uECC_word_t b,
629*af03003cSMatthias Ringwald                    uECC_word_t *r0,
630*af03003cSMatthias Ringwald                    uECC_word_t *r1,
631*af03003cSMatthias Ringwald                    uECC_word_t *r2) {
632*af03003cSMatthias Ringwald #if uECC_WORD_SIZE == 8 && !SUPPORTS_INT128
633*af03003cSMatthias Ringwald     uint64_t a0 = a & 0xffffffffull;
634*af03003cSMatthias Ringwald     uint64_t a1 = a >> 32;
635*af03003cSMatthias Ringwald     uint64_t b0 = b & 0xffffffffull;
636*af03003cSMatthias Ringwald     uint64_t b1 = b >> 32;
637*af03003cSMatthias Ringwald 
638*af03003cSMatthias Ringwald     uint64_t i0 = a0 * b0;
639*af03003cSMatthias Ringwald     uint64_t i1 = a0 * b1;
640*af03003cSMatthias Ringwald     uint64_t i2 = a1 * b0;
641*af03003cSMatthias Ringwald     uint64_t i3 = a1 * b1;
642*af03003cSMatthias Ringwald 
643*af03003cSMatthias Ringwald     uint64_t p0, p1;
644*af03003cSMatthias Ringwald 
645*af03003cSMatthias Ringwald     i2 += (i0 >> 32);
646*af03003cSMatthias Ringwald     i2 += i1;
647*af03003cSMatthias Ringwald     if (i2 < i1) { // overflow
648*af03003cSMatthias Ringwald         i3 += 0x100000000ull;
649*af03003cSMatthias Ringwald     }
650*af03003cSMatthias Ringwald 
651*af03003cSMatthias Ringwald     p0 = (i0 & 0xffffffffull) | (i2 << 32);
652*af03003cSMatthias Ringwald     p1 = i3 + (i2 >> 32);
653*af03003cSMatthias Ringwald 
654*af03003cSMatthias Ringwald     *r0 += p0;
655*af03003cSMatthias Ringwald     *r1 += (p1 + (*r0 < p0));
656*af03003cSMatthias Ringwald     *r2 += ((*r1 < p1) || (*r1 == p1 && *r0 < p0));
657*af03003cSMatthias Ringwald #else
658*af03003cSMatthias Ringwald     uECC_dword_t p = (uECC_dword_t)a * b;
659*af03003cSMatthias Ringwald     uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0;
660*af03003cSMatthias Ringwald     r01 += p;
661*af03003cSMatthias Ringwald     *r2 += (r01 < p);
662*af03003cSMatthias Ringwald     *r1 = r01 >> uECC_WORD_BITS;
663*af03003cSMatthias Ringwald     *r0 = (uECC_word_t)r01;
664*af03003cSMatthias Ringwald #endif
665*af03003cSMatthias Ringwald }
666*af03003cSMatthias Ringwald #define muladd_exists 1
667*af03003cSMatthias Ringwald #endif
668*af03003cSMatthias Ringwald 
669*af03003cSMatthias Ringwald #if !asm_mult
670*af03003cSMatthias Ringwald static void vli_mult(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) {
671*af03003cSMatthias Ringwald     uECC_word_t r0 = 0;
672*af03003cSMatthias Ringwald     uECC_word_t r1 = 0;
673*af03003cSMatthias Ringwald     uECC_word_t r2 = 0;
674*af03003cSMatthias Ringwald     wordcount_t i, k;
675*af03003cSMatthias Ringwald 
676*af03003cSMatthias Ringwald     /* Compute each digit of result in sequence, maintaining the carries. */
677*af03003cSMatthias Ringwald     for (k = 0; k < uECC_WORDS; ++k) {
678*af03003cSMatthias Ringwald         for (i = 0; i <= k; ++i) {
679*af03003cSMatthias Ringwald             muladd(left[i], right[k - i], &r0, &r1, &r2);
680*af03003cSMatthias Ringwald         }
681*af03003cSMatthias Ringwald         result[k] = r0;
682*af03003cSMatthias Ringwald         r0 = r1;
683*af03003cSMatthias Ringwald         r1 = r2;
684*af03003cSMatthias Ringwald         r2 = 0;
685*af03003cSMatthias Ringwald     }
686*af03003cSMatthias Ringwald     for (k = uECC_WORDS; k < uECC_WORDS * 2 - 1; ++k) {
687*af03003cSMatthias Ringwald         for (i = (k + 1) - uECC_WORDS; i < uECC_WORDS; ++i) {
688*af03003cSMatthias Ringwald             muladd(left[i], right[k - i], &r0, &r1, &r2);
689*af03003cSMatthias Ringwald         }
690*af03003cSMatthias Ringwald         result[k] = r0;
691*af03003cSMatthias Ringwald         r0 = r1;
692*af03003cSMatthias Ringwald         r1 = r2;
693*af03003cSMatthias Ringwald         r2 = 0;
694*af03003cSMatthias Ringwald     }
695*af03003cSMatthias Ringwald     result[uECC_WORDS * 2 - 1] = r0;
696*af03003cSMatthias Ringwald }
697*af03003cSMatthias Ringwald #endif
698*af03003cSMatthias Ringwald 
699*af03003cSMatthias Ringwald #if uECC_SQUARE_FUNC
700*af03003cSMatthias Ringwald 
701*af03003cSMatthias Ringwald #if !asm_square
702*af03003cSMatthias Ringwald static void mul2add(uECC_word_t a,
703*af03003cSMatthias Ringwald                     uECC_word_t b,
704*af03003cSMatthias Ringwald                     uECC_word_t *r0,
705*af03003cSMatthias Ringwald                     uECC_word_t *r1,
706*af03003cSMatthias Ringwald                     uECC_word_t *r2) {
707*af03003cSMatthias Ringwald #if uECC_WORD_SIZE == 8 && !SUPPORTS_INT128
708*af03003cSMatthias Ringwald     uint64_t a0 = a & 0xffffffffull;
709*af03003cSMatthias Ringwald     uint64_t a1 = a >> 32;
710*af03003cSMatthias Ringwald     uint64_t b0 = b & 0xffffffffull;
711*af03003cSMatthias Ringwald     uint64_t b1 = b >> 32;
712*af03003cSMatthias Ringwald 
713*af03003cSMatthias Ringwald     uint64_t i0 = a0 * b0;
714*af03003cSMatthias Ringwald     uint64_t i1 = a0 * b1;
715*af03003cSMatthias Ringwald     uint64_t i2 = a1 * b0;
716*af03003cSMatthias Ringwald     uint64_t i3 = a1 * b1;
717*af03003cSMatthias Ringwald 
718*af03003cSMatthias Ringwald     uint64_t p0, p1;
719*af03003cSMatthias Ringwald 
720*af03003cSMatthias Ringwald     i2 += (i0 >> 32);
721*af03003cSMatthias Ringwald     i2 += i1;
722*af03003cSMatthias Ringwald     if (i2 < i1)
723*af03003cSMatthias Ringwald     { // overflow
724*af03003cSMatthias Ringwald         i3 += 0x100000000ull;
725*af03003cSMatthias Ringwald     }
726*af03003cSMatthias Ringwald 
727*af03003cSMatthias Ringwald     p0 = (i0 & 0xffffffffull) | (i2 << 32);
728*af03003cSMatthias Ringwald     p1 = i3 + (i2 >> 32);
729*af03003cSMatthias Ringwald 
730*af03003cSMatthias Ringwald     *r2 += (p1 >> 63);
731*af03003cSMatthias Ringwald     p1 = (p1 << 1) | (p0 >> 63);
732*af03003cSMatthias Ringwald     p0 <<= 1;
733*af03003cSMatthias Ringwald 
734*af03003cSMatthias Ringwald     *r0 += p0;
735*af03003cSMatthias Ringwald     *r1 += (p1 + (*r0 < p0));
736*af03003cSMatthias Ringwald     *r2 += ((*r1 < p1) || (*r1 == p1 && *r0 < p0));
737*af03003cSMatthias Ringwald #else
738*af03003cSMatthias Ringwald     uECC_dword_t p = (uECC_dword_t)a * b;
739*af03003cSMatthias Ringwald     uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0;
740*af03003cSMatthias Ringwald     *r2 += (p >> (uECC_WORD_BITS * 2 - 1));
741*af03003cSMatthias Ringwald     p *= 2;
742*af03003cSMatthias Ringwald     r01 += p;
743*af03003cSMatthias Ringwald     *r2 += (r01 < p);
744*af03003cSMatthias Ringwald     *r1 = r01 >> uECC_WORD_BITS;
745*af03003cSMatthias Ringwald     *r0 = (uECC_word_t)r01;
746*af03003cSMatthias Ringwald #endif
747*af03003cSMatthias Ringwald }
748*af03003cSMatthias Ringwald 
749*af03003cSMatthias Ringwald static void vli_square(uECC_word_t *result, const uECC_word_t *left) {
750*af03003cSMatthias Ringwald     uECC_word_t r0 = 0;
751*af03003cSMatthias Ringwald     uECC_word_t r1 = 0;
752*af03003cSMatthias Ringwald     uECC_word_t r2 = 0;
753*af03003cSMatthias Ringwald 
754*af03003cSMatthias Ringwald     wordcount_t i, k;
755*af03003cSMatthias Ringwald 
756*af03003cSMatthias Ringwald     for (k = 0; k < uECC_WORDS * 2 - 1; ++k) {
757*af03003cSMatthias Ringwald         uECC_word_t min = (k < uECC_WORDS ? 0 : (k + 1) - uECC_WORDS);
758*af03003cSMatthias Ringwald         for (i = min; i <= k && i <= k - i; ++i) {
759*af03003cSMatthias Ringwald             if (i < k-i) {
760*af03003cSMatthias Ringwald                 mul2add(left[i], left[k - i], &r0, &r1, &r2);
761*af03003cSMatthias Ringwald             } else {
762*af03003cSMatthias Ringwald                 muladd(left[i], left[k - i], &r0, &r1, &r2);
763*af03003cSMatthias Ringwald             }
764*af03003cSMatthias Ringwald         }
765*af03003cSMatthias Ringwald         result[k] = r0;
766*af03003cSMatthias Ringwald         r0 = r1;
767*af03003cSMatthias Ringwald         r1 = r2;
768*af03003cSMatthias Ringwald         r2 = 0;
769*af03003cSMatthias Ringwald     }
770*af03003cSMatthias Ringwald 
771*af03003cSMatthias Ringwald     result[uECC_WORDS * 2 - 1] = r0;
772*af03003cSMatthias Ringwald }
773*af03003cSMatthias Ringwald #endif
774*af03003cSMatthias Ringwald 
775*af03003cSMatthias Ringwald #else /* uECC_SQUARE_FUNC */
776*af03003cSMatthias Ringwald 
777*af03003cSMatthias Ringwald #define vli_square(result, left, size) vli_mult((result), (left), (left), (size))
778*af03003cSMatthias Ringwald 
779*af03003cSMatthias Ringwald #endif /* uECC_SQUARE_FUNC */
780*af03003cSMatthias Ringwald 
781*af03003cSMatthias Ringwald 
782*af03003cSMatthias Ringwald /* Computes result = (left + right) % mod.
783*af03003cSMatthias Ringwald    Assumes that left < mod and right < mod, and that result does not overlap mod. */
784*af03003cSMatthias Ringwald #if !asm_modAdd
785*af03003cSMatthias Ringwald static void vli_modAdd(uECC_word_t *result,
786*af03003cSMatthias Ringwald                        const uECC_word_t *left,
787*af03003cSMatthias Ringwald                        const uECC_word_t *right,
788*af03003cSMatthias Ringwald                        const uECC_word_t *mod) {
789*af03003cSMatthias Ringwald     uECC_word_t carry = vli_add(result, left, right);
790*af03003cSMatthias Ringwald     if (carry || vli_cmp(result, mod) >= 0) {
791*af03003cSMatthias Ringwald         /* result > mod (result = mod + remainder), so subtract mod to get remainder. */
792*af03003cSMatthias Ringwald         vli_sub(result, result, mod);
793*af03003cSMatthias Ringwald     }
794*af03003cSMatthias Ringwald }
795*af03003cSMatthias Ringwald #endif
796*af03003cSMatthias Ringwald 
797*af03003cSMatthias Ringwald /* Computes result = (left - right) % mod.
798*af03003cSMatthias Ringwald    Assumes that left < mod and right < mod, and that result does not overlap mod. */
799*af03003cSMatthias Ringwald #if !asm_modSub
800*af03003cSMatthias Ringwald static void vli_modSub(uECC_word_t *result,
801*af03003cSMatthias Ringwald                        const uECC_word_t *left,
802*af03003cSMatthias Ringwald                        const uECC_word_t *right,
803*af03003cSMatthias Ringwald                        const uECC_word_t *mod) {
804*af03003cSMatthias Ringwald     uECC_word_t l_borrow = vli_sub(result, left, right);
805*af03003cSMatthias Ringwald     if (l_borrow) {
806*af03003cSMatthias Ringwald         /* In this case, result == -diff == (max int) - diff. Since -x % d == d - x,
807*af03003cSMatthias Ringwald            we can get the correct result from result + mod (with overflow). */
808*af03003cSMatthias Ringwald         vli_add(result, result, mod);
809*af03003cSMatthias Ringwald     }
810*af03003cSMatthias Ringwald }
811*af03003cSMatthias Ringwald #endif
812*af03003cSMatthias Ringwald 
813*af03003cSMatthias Ringwald #if !asm_modSub_fast
814*af03003cSMatthias Ringwald     #define vli_modSub_fast(result, left, right) vli_modSub((result), (left), (right), curve_p)
815*af03003cSMatthias Ringwald #endif
816*af03003cSMatthias Ringwald 
817*af03003cSMatthias Ringwald #if !asm_mmod_fast
818*af03003cSMatthias Ringwald 
819*af03003cSMatthias Ringwald #if (uECC_CURVE == uECC_secp160r1 || uECC_CURVE == uECC_secp256k1)
820*af03003cSMatthias Ringwald /* omega_mult() is defined farther below for the different curves / word sizes */
821*af03003cSMatthias Ringwald static void omega_mult(uECC_word_t * RESTRICT result, const uECC_word_t * RESTRICT right);
822*af03003cSMatthias Ringwald 
823*af03003cSMatthias Ringwald /* Computes result = product % curve_p
824*af03003cSMatthias Ringwald     see http://www.isys.uni-klu.ac.at/PDF/2001-0126-MT.pdf page 354
825*af03003cSMatthias Ringwald 
826*af03003cSMatthias Ringwald     Note that this only works if log2(omega) < log2(p) / 2 */
827*af03003cSMatthias Ringwald static void vli_mmod_fast(uECC_word_t *RESTRICT result, uECC_word_t *RESTRICT product) {
828*af03003cSMatthias Ringwald     uECC_word_t tmp[2 * uECC_WORDS];
829*af03003cSMatthias Ringwald     uECC_word_t carry;
830*af03003cSMatthias Ringwald 
831*af03003cSMatthias Ringwald     vli_clear(tmp);
832*af03003cSMatthias Ringwald     vli_clear(tmp + uECC_WORDS);
833*af03003cSMatthias Ringwald 
834*af03003cSMatthias Ringwald     omega_mult(tmp, product + uECC_WORDS); /* (Rq, q) = q * c */
835*af03003cSMatthias Ringwald 
836*af03003cSMatthias Ringwald     carry = vli_add(result, product, tmp); /* (C, r) = r + q       */
837*af03003cSMatthias Ringwald     vli_clear(product);
838*af03003cSMatthias Ringwald     omega_mult(product, tmp + uECC_WORDS); /* Rq*c */
839*af03003cSMatthias Ringwald     carry += vli_add(result, result, product); /* (C1, r) = r + Rq*c */
840*af03003cSMatthias Ringwald 
841*af03003cSMatthias Ringwald     while (carry > 0) {
842*af03003cSMatthias Ringwald         --carry;
843*af03003cSMatthias Ringwald         vli_sub(result, result, curve_p);
844*af03003cSMatthias Ringwald     }
845*af03003cSMatthias Ringwald     if (vli_cmp(result, curve_p) > 0) {
846*af03003cSMatthias Ringwald         vli_sub(result, result, curve_p);
847*af03003cSMatthias Ringwald     }
848*af03003cSMatthias Ringwald }
849*af03003cSMatthias Ringwald 
850*af03003cSMatthias Ringwald #endif
851*af03003cSMatthias Ringwald 
852*af03003cSMatthias Ringwald #if uECC_CURVE == uECC_secp160r1
853*af03003cSMatthias Ringwald 
854*af03003cSMatthias Ringwald #if uECC_WORD_SIZE == 1
855*af03003cSMatthias Ringwald static void omega_mult(uint8_t * RESTRICT result, const uint8_t * RESTRICT right) {
856*af03003cSMatthias Ringwald     uint8_t carry;
857*af03003cSMatthias Ringwald     uint8_t i;
858*af03003cSMatthias Ringwald 
859*af03003cSMatthias Ringwald     /* Multiply by (2^31 + 1). */
860*af03003cSMatthias Ringwald     vli_set(result + 4, right); /* 2^32 */
861*af03003cSMatthias Ringwald     vli_rshift1(result + 4); /* 2^31 */
862*af03003cSMatthias Ringwald     result[3] = right[0] << 7; /* get last bit from shift */
863*af03003cSMatthias Ringwald 
864*af03003cSMatthias Ringwald     carry = vli_add(result, result, right); /* 2^31 + 1 */
865*af03003cSMatthias Ringwald     for (i = uECC_WORDS; carry; ++i) {
866*af03003cSMatthias Ringwald         uint16_t sum = (uint16_t)result[i] + carry;
867*af03003cSMatthias Ringwald         result[i] = (uint8_t)sum;
868*af03003cSMatthias Ringwald         carry = sum >> 8;
869*af03003cSMatthias Ringwald     }
870*af03003cSMatthias Ringwald }
871*af03003cSMatthias Ringwald #elif uECC_WORD_SIZE == 4
872*af03003cSMatthias Ringwald static void omega_mult(uint32_t * RESTRICT result, const uint32_t * RESTRICT right) {
873*af03003cSMatthias Ringwald     uint32_t carry;
874*af03003cSMatthias Ringwald     unsigned i;
875*af03003cSMatthias Ringwald 
876*af03003cSMatthias Ringwald     /* Multiply by (2^31 + 1). */
877*af03003cSMatthias Ringwald     vli_set(result + 1, right); /* 2^32 */
878*af03003cSMatthias Ringwald     vli_rshift1(result + 1); /* 2^31 */
879*af03003cSMatthias Ringwald     result[0] = right[0] << 31; /* get last bit from shift */
880*af03003cSMatthias Ringwald 
881*af03003cSMatthias Ringwald     carry = vli_add(result, result, right); /* 2^31 + 1 */
882*af03003cSMatthias Ringwald     for (i = uECC_WORDS; carry; ++i) {
883*af03003cSMatthias Ringwald         uint64_t sum = (uint64_t)result[i] + carry;
884*af03003cSMatthias Ringwald         result[i] = (uint32_t)sum;
885*af03003cSMatthias Ringwald         carry = sum >> 32;
886*af03003cSMatthias Ringwald     }
887*af03003cSMatthias Ringwald }
888*af03003cSMatthias Ringwald #endif /* uECC_WORD_SIZE */
889*af03003cSMatthias Ringwald 
890*af03003cSMatthias Ringwald #elif uECC_CURVE == uECC_secp192r1
891*af03003cSMatthias Ringwald 
892*af03003cSMatthias Ringwald /* Computes result = product % curve_p.
893*af03003cSMatthias Ringwald    See algorithm 5 and 6 from http://www.isys.uni-klu.ac.at/PDF/2001-0126-MT.pdf */
894*af03003cSMatthias Ringwald #if uECC_WORD_SIZE == 1
895*af03003cSMatthias Ringwald static void vli_mmod_fast(uint8_t *RESTRICT result, uint8_t *RESTRICT product) {
896*af03003cSMatthias Ringwald     uint8_t tmp[uECC_WORDS];
897*af03003cSMatthias Ringwald     uint8_t carry;
898*af03003cSMatthias Ringwald 
899*af03003cSMatthias Ringwald     vli_set(result, product);
900*af03003cSMatthias Ringwald 
901*af03003cSMatthias Ringwald     vli_set(tmp, &product[24]);
902*af03003cSMatthias Ringwald     carry = vli_add(result, result, tmp);
903*af03003cSMatthias Ringwald 
904*af03003cSMatthias Ringwald     tmp[0] = tmp[1] = tmp[2] = tmp[3] = tmp[4] = tmp[5] = tmp[6] = tmp[7] = 0;
905*af03003cSMatthias Ringwald     tmp[8] = product[24]; tmp[9] = product[25]; tmp[10] = product[26]; tmp[11] = product[27];
906*af03003cSMatthias Ringwald     tmp[12] = product[28]; tmp[13] = product[29]; tmp[14] = product[30]; tmp[15] = product[31];
907*af03003cSMatthias Ringwald     tmp[16] = product[32]; tmp[17] = product[33]; tmp[18] = product[34]; tmp[19] = product[35];
908*af03003cSMatthias Ringwald     tmp[20] = product[36]; tmp[21] = product[37]; tmp[22] = product[38]; tmp[23] = product[39];
909*af03003cSMatthias Ringwald     carry += vli_add(result, result, tmp);
910*af03003cSMatthias Ringwald 
911*af03003cSMatthias Ringwald     tmp[0] = tmp[8] = product[40];
912*af03003cSMatthias Ringwald     tmp[1] = tmp[9] = product[41];
913*af03003cSMatthias Ringwald     tmp[2] = tmp[10] = product[42];
914*af03003cSMatthias Ringwald     tmp[3] = tmp[11] = product[43];
915*af03003cSMatthias Ringwald     tmp[4] = tmp[12] = product[44];
916*af03003cSMatthias Ringwald     tmp[5] = tmp[13] = product[45];
917*af03003cSMatthias Ringwald     tmp[6] = tmp[14] = product[46];
918*af03003cSMatthias Ringwald     tmp[7] = tmp[15] = product[47];
919*af03003cSMatthias Ringwald     tmp[16] = tmp[17] = tmp[18] = tmp[19] = tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0;
920*af03003cSMatthias Ringwald     carry += vli_add(result, result, tmp);
921*af03003cSMatthias Ringwald 
922*af03003cSMatthias Ringwald     while (carry || vli_cmp(curve_p, result) != 1) {
923*af03003cSMatthias Ringwald         carry -= vli_sub(result, result, curve_p);
924*af03003cSMatthias Ringwald     }
925*af03003cSMatthias Ringwald }
926*af03003cSMatthias Ringwald #elif uECC_WORD_SIZE == 4
927*af03003cSMatthias Ringwald static void vli_mmod_fast(uint32_t *RESTRICT result, uint32_t *RESTRICT product) {
928*af03003cSMatthias Ringwald     uint32_t tmp[uECC_WORDS];
929*af03003cSMatthias Ringwald     int carry;
930*af03003cSMatthias Ringwald 
931*af03003cSMatthias Ringwald     vli_set(result, product);
932*af03003cSMatthias Ringwald 
933*af03003cSMatthias Ringwald     vli_set(tmp, &product[6]);
934*af03003cSMatthias Ringwald     carry = vli_add(result, result, tmp);
935*af03003cSMatthias Ringwald 
936*af03003cSMatthias Ringwald     tmp[0] = tmp[1] = 0;
937*af03003cSMatthias Ringwald     tmp[2] = product[6];
938*af03003cSMatthias Ringwald     tmp[3] = product[7];
939*af03003cSMatthias Ringwald     tmp[4] = product[8];
940*af03003cSMatthias Ringwald     tmp[5] = product[9];
941*af03003cSMatthias Ringwald     carry += vli_add(result, result, tmp);
942*af03003cSMatthias Ringwald 
943*af03003cSMatthias Ringwald     tmp[0] = tmp[2] = product[10];
944*af03003cSMatthias Ringwald     tmp[1] = tmp[3] = product[11];
945*af03003cSMatthias Ringwald     tmp[4] = tmp[5] = 0;
946*af03003cSMatthias Ringwald     carry += vli_add(result, result, tmp);
947*af03003cSMatthias Ringwald 
948*af03003cSMatthias Ringwald     while (carry || vli_cmp(curve_p, result) != 1) {
949*af03003cSMatthias Ringwald         carry -= vli_sub(result, result, curve_p);
950*af03003cSMatthias Ringwald     }
951*af03003cSMatthias Ringwald }
952*af03003cSMatthias Ringwald #else
953*af03003cSMatthias Ringwald static void vli_mmod_fast(uint64_t *RESTRICT result, uint64_t *RESTRICT product) {
954*af03003cSMatthias Ringwald     uint64_t tmp[uECC_WORDS];
955*af03003cSMatthias Ringwald     int carry;
956*af03003cSMatthias Ringwald 
957*af03003cSMatthias Ringwald     vli_set(result, product);
958*af03003cSMatthias Ringwald 
959*af03003cSMatthias Ringwald     vli_set(tmp, &product[3]);
960*af03003cSMatthias Ringwald     carry = vli_add(result, result, tmp);
961*af03003cSMatthias Ringwald 
962*af03003cSMatthias Ringwald     tmp[0] = 0;
963*af03003cSMatthias Ringwald     tmp[1] = product[3];
964*af03003cSMatthias Ringwald     tmp[2] = product[4];
965*af03003cSMatthias Ringwald     carry += vli_add(result, result, tmp);
966*af03003cSMatthias Ringwald 
967*af03003cSMatthias Ringwald     tmp[0] = tmp[1] = product[5];
968*af03003cSMatthias Ringwald     tmp[2] = 0;
969*af03003cSMatthias Ringwald     carry += vli_add(result, result, tmp);
970*af03003cSMatthias Ringwald 
971*af03003cSMatthias Ringwald     while (carry || vli_cmp(curve_p, result) != 1) {
972*af03003cSMatthias Ringwald         carry -= vli_sub(result, result, curve_p);
973*af03003cSMatthias Ringwald     }
974*af03003cSMatthias Ringwald }
975*af03003cSMatthias Ringwald #endif /* uECC_WORD_SIZE */
976*af03003cSMatthias Ringwald 
977*af03003cSMatthias Ringwald #elif uECC_CURVE == uECC_secp256r1
978*af03003cSMatthias Ringwald 
979*af03003cSMatthias Ringwald /* Computes result = product % curve_p
980*af03003cSMatthias Ringwald    from http://www.nsa.gov/ia/_files/nist-routines.pdf */
981*af03003cSMatthias Ringwald #if uECC_WORD_SIZE == 1
982*af03003cSMatthias Ringwald static void vli_mmod_fast(uint8_t *RESTRICT result, uint8_t *RESTRICT product) {
983*af03003cSMatthias Ringwald     uint8_t tmp[uECC_BYTES];
984*af03003cSMatthias Ringwald     int8_t carry;
985*af03003cSMatthias Ringwald 
986*af03003cSMatthias Ringwald     /* t */
987*af03003cSMatthias Ringwald     vli_set(result, product);
988*af03003cSMatthias Ringwald 
989*af03003cSMatthias Ringwald     /* s1 */
990*af03003cSMatthias Ringwald     tmp[0] = tmp[1] = tmp[2] = tmp[3] = 0;
991*af03003cSMatthias Ringwald     tmp[4] = tmp[5] = tmp[6] = tmp[7] = 0;
992*af03003cSMatthias Ringwald     tmp[8] = tmp[9] = tmp[10] = tmp[11] = 0;
993*af03003cSMatthias Ringwald     tmp[12] = product[44]; tmp[13] = product[45]; tmp[14] = product[46]; tmp[15] = product[47];
994*af03003cSMatthias Ringwald     tmp[16] = product[48]; tmp[17] = product[49]; tmp[18] = product[50]; tmp[19] = product[51];
995*af03003cSMatthias Ringwald     tmp[20] = product[52]; tmp[21] = product[53]; tmp[22] = product[54]; tmp[23] = product[55];
996*af03003cSMatthias Ringwald     tmp[24] = product[56]; tmp[25] = product[57]; tmp[26] = product[58]; tmp[27] = product[59];
997*af03003cSMatthias Ringwald     tmp[28] = product[60]; tmp[29] = product[61]; tmp[30] = product[62]; tmp[31] = product[63];
998*af03003cSMatthias Ringwald     carry = vli_add(tmp, tmp, tmp);
999*af03003cSMatthias Ringwald     carry += vli_add(result, result, tmp);
1000*af03003cSMatthias Ringwald 
1001*af03003cSMatthias Ringwald     /* s2 */
1002*af03003cSMatthias Ringwald     tmp[12] = product[48]; tmp[13] = product[49]; tmp[14] = product[50]; tmp[15] = product[51];
1003*af03003cSMatthias Ringwald     tmp[16] = product[52]; tmp[17] = product[53]; tmp[18] = product[54]; tmp[19] = product[55];
1004*af03003cSMatthias Ringwald     tmp[20] = product[56]; tmp[21] = product[57]; tmp[22] = product[58]; tmp[23] = product[59];
1005*af03003cSMatthias Ringwald     tmp[24] = product[60]; tmp[25] = product[61]; tmp[26] = product[62]; tmp[27] = product[63];
1006*af03003cSMatthias Ringwald     tmp[28] = tmp[29] = tmp[30] = tmp[31] = 0;
1007*af03003cSMatthias Ringwald     carry += vli_add(tmp, tmp, tmp);
1008*af03003cSMatthias Ringwald     carry += vli_add(result, result, tmp);
1009*af03003cSMatthias Ringwald 
1010*af03003cSMatthias Ringwald     /* s3 */
1011*af03003cSMatthias Ringwald     tmp[0] = product[32]; tmp[1] = product[33]; tmp[2] = product[34]; tmp[3] = product[35];
1012*af03003cSMatthias Ringwald     tmp[4] = product[36]; tmp[5] = product[37]; tmp[6] = product[38]; tmp[7] = product[39];
1013*af03003cSMatthias Ringwald     tmp[8] = product[40]; tmp[9] = product[41]; tmp[10] = product[42]; tmp[11] = product[43];
1014*af03003cSMatthias Ringwald     tmp[12] = tmp[13] = tmp[14] = tmp[15] = 0;
1015*af03003cSMatthias Ringwald     tmp[16] = tmp[17] = tmp[18] = tmp[19] = 0;
1016*af03003cSMatthias Ringwald     tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0;
1017*af03003cSMatthias Ringwald     tmp[24] = product[56]; tmp[25] = product[57]; tmp[26] = product[58]; tmp[27] = product[59];
1018*af03003cSMatthias Ringwald     tmp[28] = product[60]; tmp[29] = product[61]; tmp[30] = product[62]; tmp[31] = product[63];
1019*af03003cSMatthias Ringwald     carry += vli_add(result, result, tmp);
1020*af03003cSMatthias Ringwald 
1021*af03003cSMatthias Ringwald     /* s4 */
1022*af03003cSMatthias Ringwald     tmp[0] = product[36]; tmp[1] = product[37]; tmp[2] = product[38]; tmp[3] = product[39];
1023*af03003cSMatthias Ringwald     tmp[4] = product[40]; tmp[5] = product[41]; tmp[6] = product[42]; tmp[7] = product[43];
1024*af03003cSMatthias Ringwald     tmp[8] = product[44]; tmp[9] = product[45]; tmp[10] = product[46]; tmp[11] = product[47];
1025*af03003cSMatthias Ringwald     tmp[12] = product[52]; tmp[13] = product[53]; tmp[14] = product[54]; tmp[15] = product[55];
1026*af03003cSMatthias Ringwald     tmp[16] = product[56]; tmp[17] = product[57]; tmp[18] = product[58]; tmp[19] = product[59];
1027*af03003cSMatthias Ringwald     tmp[20] = product[60]; tmp[21] = product[61]; tmp[22] = product[62]; tmp[23] = product[63];
1028*af03003cSMatthias Ringwald     tmp[24] = product[52]; tmp[25] = product[53]; tmp[26] = product[54]; tmp[27] = product[55];
1029*af03003cSMatthias Ringwald     tmp[28] = product[32]; tmp[29] = product[33]; tmp[30] = product[34]; tmp[31] = product[35];
1030*af03003cSMatthias Ringwald     carry += vli_add(result, result, tmp);
1031*af03003cSMatthias Ringwald 
1032*af03003cSMatthias Ringwald     /* d1 */
1033*af03003cSMatthias Ringwald     tmp[0] = product[44]; tmp[1] = product[45]; tmp[2] = product[46]; tmp[3] = product[47];
1034*af03003cSMatthias Ringwald     tmp[4] = product[48]; tmp[5] = product[49]; tmp[6] = product[50]; tmp[7] = product[51];
1035*af03003cSMatthias Ringwald     tmp[8] = product[52]; tmp[9] = product[53]; tmp[10] = product[54]; tmp[11] = product[55];
1036*af03003cSMatthias Ringwald     tmp[12] = tmp[13] = tmp[14] = tmp[15] = 0;
1037*af03003cSMatthias Ringwald     tmp[16] = tmp[17] = tmp[18] = tmp[19] = 0;
1038*af03003cSMatthias Ringwald     tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0;
1039*af03003cSMatthias Ringwald     tmp[24] = product[32]; tmp[25] = product[33]; tmp[26] = product[34]; tmp[27] = product[35];
1040*af03003cSMatthias Ringwald     tmp[28] = product[40]; tmp[29] = product[41]; tmp[30] = product[42]; tmp[31] = product[43];
1041*af03003cSMatthias Ringwald     carry -= vli_sub(result, result, tmp);
1042*af03003cSMatthias Ringwald 
1043*af03003cSMatthias Ringwald     /* d2 */
1044*af03003cSMatthias Ringwald     tmp[0] = product[48]; tmp[1] = product[49]; tmp[2] = product[50]; tmp[3] = product[51];
1045*af03003cSMatthias Ringwald     tmp[4] = product[52]; tmp[5] = product[53]; tmp[6] = product[54]; tmp[7] = product[55];
1046*af03003cSMatthias Ringwald     tmp[8] = product[56]; tmp[9] = product[57]; tmp[10] = product[58]; tmp[11] = product[59];
1047*af03003cSMatthias Ringwald     tmp[12] = product[60]; tmp[13] = product[61]; tmp[14] = product[62]; tmp[15] = product[63];
1048*af03003cSMatthias Ringwald     tmp[16] = tmp[17] = tmp[18] = tmp[19] = 0;
1049*af03003cSMatthias Ringwald     tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0;
1050*af03003cSMatthias Ringwald     tmp[24] = product[36]; tmp[25] = product[37]; tmp[26] = product[38]; tmp[27] = product[39];
1051*af03003cSMatthias Ringwald     tmp[28] = product[44]; tmp[29] = product[45]; tmp[30] = product[46]; tmp[31] = product[47];
1052*af03003cSMatthias Ringwald     carry -= vli_sub(result, result, tmp);
1053*af03003cSMatthias Ringwald 
1054*af03003cSMatthias Ringwald     /* d3 */
1055*af03003cSMatthias Ringwald     tmp[0] = product[52]; tmp[1] = product[53]; tmp[2] = product[54]; tmp[3] = product[55];
1056*af03003cSMatthias Ringwald     tmp[4] = product[56]; tmp[5] = product[57]; tmp[6] = product[58]; tmp[7] = product[59];
1057*af03003cSMatthias Ringwald     tmp[8] = product[60]; tmp[9] = product[61]; tmp[10] = product[62]; tmp[11] = product[63];
1058*af03003cSMatthias Ringwald     tmp[12] = product[32]; tmp[13] = product[33]; tmp[14] = product[34]; tmp[15] = product[35];
1059*af03003cSMatthias Ringwald     tmp[16] = product[36]; tmp[17] = product[37]; tmp[18] = product[38]; tmp[19] = product[39];
1060*af03003cSMatthias Ringwald     tmp[20] = product[40]; tmp[21] = product[41]; tmp[22] = product[42]; tmp[23] = product[43];
1061*af03003cSMatthias Ringwald     tmp[24] = tmp[25] = tmp[26] = tmp[27] = 0;
1062*af03003cSMatthias Ringwald     tmp[28] = product[48]; tmp[29] = product[49]; tmp[30] = product[50]; tmp[31] = product[51];
1063*af03003cSMatthias Ringwald     carry -= vli_sub(result, result, tmp);
1064*af03003cSMatthias Ringwald 
1065*af03003cSMatthias Ringwald     /* d4 */
1066*af03003cSMatthias Ringwald     tmp[0] = product[56]; tmp[1] = product[57]; tmp[2] = product[58]; tmp[3] = product[59];
1067*af03003cSMatthias Ringwald     tmp[4] = product[60]; tmp[5] = product[61]; tmp[6] = product[62]; tmp[7] = product[63];
1068*af03003cSMatthias Ringwald     tmp[8] = tmp[9] = tmp[10] = tmp[11] = 0;
1069*af03003cSMatthias Ringwald     tmp[12] = product[36]; tmp[13] = product[37]; tmp[14] = product[38]; tmp[15] = product[39];
1070*af03003cSMatthias Ringwald     tmp[16] = product[40]; tmp[17] = product[41]; tmp[18] = product[42]; tmp[19] = product[43];
1071*af03003cSMatthias Ringwald     tmp[20] = product[44]; tmp[21] = product[45]; tmp[22] = product[46]; tmp[23] = product[47];
1072*af03003cSMatthias Ringwald     tmp[24] = tmp[25] = tmp[26] = tmp[27] = 0;
1073*af03003cSMatthias Ringwald     tmp[28] = product[52]; tmp[29] = product[53]; tmp[30] = product[54]; tmp[31] = product[55];
1074*af03003cSMatthias Ringwald     carry -= vli_sub(result, result, tmp);
1075*af03003cSMatthias Ringwald 
1076*af03003cSMatthias Ringwald     if (carry < 0) {
1077*af03003cSMatthias Ringwald         do {
1078*af03003cSMatthias Ringwald             carry += vli_add(result, result, curve_p);
1079*af03003cSMatthias Ringwald         } while (carry < 0);
1080*af03003cSMatthias Ringwald     } else {
1081*af03003cSMatthias Ringwald         while (carry || vli_cmp(curve_p, result) != 1) {
1082*af03003cSMatthias Ringwald             carry -= vli_sub(result, result, curve_p);
1083*af03003cSMatthias Ringwald         }
1084*af03003cSMatthias Ringwald     }
1085*af03003cSMatthias Ringwald }
1086*af03003cSMatthias Ringwald #elif uECC_WORD_SIZE == 4
1087*af03003cSMatthias Ringwald static void vli_mmod_fast(uint32_t *RESTRICT result, uint32_t *RESTRICT product) {
1088*af03003cSMatthias Ringwald     uint32_t tmp[uECC_WORDS];
1089*af03003cSMatthias Ringwald     int carry;
1090*af03003cSMatthias Ringwald 
1091*af03003cSMatthias Ringwald     /* t */
1092*af03003cSMatthias Ringwald     vli_set(result, product);
1093*af03003cSMatthias Ringwald 
1094*af03003cSMatthias Ringwald     /* s1 */
1095*af03003cSMatthias Ringwald     tmp[0] = tmp[1] = tmp[2] = 0;
1096*af03003cSMatthias Ringwald     tmp[3] = product[11];
1097*af03003cSMatthias Ringwald     tmp[4] = product[12];
1098*af03003cSMatthias Ringwald     tmp[5] = product[13];
1099*af03003cSMatthias Ringwald     tmp[6] = product[14];
1100*af03003cSMatthias Ringwald     tmp[7] = product[15];
1101*af03003cSMatthias Ringwald     carry = vli_add(tmp, tmp, tmp);
1102*af03003cSMatthias Ringwald     carry += vli_add(result, result, tmp);
1103*af03003cSMatthias Ringwald 
1104*af03003cSMatthias Ringwald     /* s2 */
1105*af03003cSMatthias Ringwald     tmp[3] = product[12];
1106*af03003cSMatthias Ringwald     tmp[4] = product[13];
1107*af03003cSMatthias Ringwald     tmp[5] = product[14];
1108*af03003cSMatthias Ringwald     tmp[6] = product[15];
1109*af03003cSMatthias Ringwald     tmp[7] = 0;
1110*af03003cSMatthias Ringwald     carry += vli_add(tmp, tmp, tmp);
1111*af03003cSMatthias Ringwald     carry += vli_add(result, result, tmp);
1112*af03003cSMatthias Ringwald 
1113*af03003cSMatthias Ringwald     /* s3 */
1114*af03003cSMatthias Ringwald     tmp[0] = product[8];
1115*af03003cSMatthias Ringwald     tmp[1] = product[9];
1116*af03003cSMatthias Ringwald     tmp[2] = product[10];
1117*af03003cSMatthias Ringwald     tmp[3] = tmp[4] = tmp[5] = 0;
1118*af03003cSMatthias Ringwald     tmp[6] = product[14];
1119*af03003cSMatthias Ringwald     tmp[7] = product[15];
1120*af03003cSMatthias Ringwald     carry += vli_add(result, result, tmp);
1121*af03003cSMatthias Ringwald 
1122*af03003cSMatthias Ringwald     /* s4 */
1123*af03003cSMatthias Ringwald     tmp[0] = product[9];
1124*af03003cSMatthias Ringwald     tmp[1] = product[10];
1125*af03003cSMatthias Ringwald     tmp[2] = product[11];
1126*af03003cSMatthias Ringwald     tmp[3] = product[13];
1127*af03003cSMatthias Ringwald     tmp[4] = product[14];
1128*af03003cSMatthias Ringwald     tmp[5] = product[15];
1129*af03003cSMatthias Ringwald     tmp[6] = product[13];
1130*af03003cSMatthias Ringwald     tmp[7] = product[8];
1131*af03003cSMatthias Ringwald     carry += vli_add(result, result, tmp);
1132*af03003cSMatthias Ringwald 
1133*af03003cSMatthias Ringwald     /* d1 */
1134*af03003cSMatthias Ringwald     tmp[0] = product[11];
1135*af03003cSMatthias Ringwald     tmp[1] = product[12];
1136*af03003cSMatthias Ringwald     tmp[2] = product[13];
1137*af03003cSMatthias Ringwald     tmp[3] = tmp[4] = tmp[5] = 0;
1138*af03003cSMatthias Ringwald     tmp[6] = product[8];
1139*af03003cSMatthias Ringwald     tmp[7] = product[10];
1140*af03003cSMatthias Ringwald     carry -= vli_sub(result, result, tmp);
1141*af03003cSMatthias Ringwald 
1142*af03003cSMatthias Ringwald     /* d2 */
1143*af03003cSMatthias Ringwald     tmp[0] = product[12];
1144*af03003cSMatthias Ringwald     tmp[1] = product[13];
1145*af03003cSMatthias Ringwald     tmp[2] = product[14];
1146*af03003cSMatthias Ringwald     tmp[3] = product[15];
1147*af03003cSMatthias Ringwald     tmp[4] = tmp[5] = 0;
1148*af03003cSMatthias Ringwald     tmp[6] = product[9];
1149*af03003cSMatthias Ringwald     tmp[7] = product[11];
1150*af03003cSMatthias Ringwald     carry -= vli_sub(result, result, tmp);
1151*af03003cSMatthias Ringwald 
1152*af03003cSMatthias Ringwald     /* d3 */
1153*af03003cSMatthias Ringwald     tmp[0] = product[13];
1154*af03003cSMatthias Ringwald     tmp[1] = product[14];
1155*af03003cSMatthias Ringwald     tmp[2] = product[15];
1156*af03003cSMatthias Ringwald     tmp[3] = product[8];
1157*af03003cSMatthias Ringwald     tmp[4] = product[9];
1158*af03003cSMatthias Ringwald     tmp[5] = product[10];
1159*af03003cSMatthias Ringwald     tmp[6] = 0;
1160*af03003cSMatthias Ringwald     tmp[7] = product[12];
1161*af03003cSMatthias Ringwald     carry -= vli_sub(result, result, tmp);
1162*af03003cSMatthias Ringwald 
1163*af03003cSMatthias Ringwald     /* d4 */
1164*af03003cSMatthias Ringwald     tmp[0] = product[14];
1165*af03003cSMatthias Ringwald     tmp[1] = product[15];
1166*af03003cSMatthias Ringwald     tmp[2] = 0;
1167*af03003cSMatthias Ringwald     tmp[3] = product[9];
1168*af03003cSMatthias Ringwald     tmp[4] = product[10];
1169*af03003cSMatthias Ringwald     tmp[5] = product[11];
1170*af03003cSMatthias Ringwald     tmp[6] = 0;
1171*af03003cSMatthias Ringwald     tmp[7] = product[13];
1172*af03003cSMatthias Ringwald     carry -= vli_sub(result, result, tmp);
1173*af03003cSMatthias Ringwald 
1174*af03003cSMatthias Ringwald     if (carry < 0) {
1175*af03003cSMatthias Ringwald         do {
1176*af03003cSMatthias Ringwald             carry += vli_add(result, result, curve_p);
1177*af03003cSMatthias Ringwald         } while (carry < 0);
1178*af03003cSMatthias Ringwald     } else {
1179*af03003cSMatthias Ringwald         while (carry || vli_cmp(curve_p, result) != 1) {
1180*af03003cSMatthias Ringwald             carry -= vli_sub(result, result, curve_p);
1181*af03003cSMatthias Ringwald         }
1182*af03003cSMatthias Ringwald     }
1183*af03003cSMatthias Ringwald }
1184*af03003cSMatthias Ringwald #else
1185*af03003cSMatthias Ringwald static void vli_mmod_fast(uint64_t *RESTRICT result, uint64_t *RESTRICT product) {
1186*af03003cSMatthias Ringwald     uint64_t tmp[uECC_WORDS];
1187*af03003cSMatthias Ringwald     int carry;
1188*af03003cSMatthias Ringwald 
1189*af03003cSMatthias Ringwald     /* t */
1190*af03003cSMatthias Ringwald     vli_set(result, product);
1191*af03003cSMatthias Ringwald 
1192*af03003cSMatthias Ringwald     /* s1 */
1193*af03003cSMatthias Ringwald     tmp[0] = 0;
1194*af03003cSMatthias Ringwald     tmp[1] = product[5] & 0xffffffff00000000ull;
1195*af03003cSMatthias Ringwald     tmp[2] = product[6];
1196*af03003cSMatthias Ringwald     tmp[3] = product[7];
1197*af03003cSMatthias Ringwald     carry = vli_add(tmp, tmp, tmp);
1198*af03003cSMatthias Ringwald     carry += vli_add(result, result, tmp);
1199*af03003cSMatthias Ringwald 
1200*af03003cSMatthias Ringwald     /* s2 */
1201*af03003cSMatthias Ringwald     tmp[1] = product[6] << 32;
1202*af03003cSMatthias Ringwald     tmp[2] = (product[6] >> 32) | (product[7] << 32);
1203*af03003cSMatthias Ringwald     tmp[3] = product[7] >> 32;
1204*af03003cSMatthias Ringwald     carry += vli_add(tmp, tmp, tmp);
1205*af03003cSMatthias Ringwald     carry += vli_add(result, result, tmp);
1206*af03003cSMatthias Ringwald 
1207*af03003cSMatthias Ringwald     /* s3 */
1208*af03003cSMatthias Ringwald     tmp[0] = product[4];
1209*af03003cSMatthias Ringwald     tmp[1] = product[5] & 0xffffffff;
1210*af03003cSMatthias Ringwald     tmp[2] = 0;
1211*af03003cSMatthias Ringwald     tmp[3] = product[7];
1212*af03003cSMatthias Ringwald     carry += vli_add(result, result, tmp);
1213*af03003cSMatthias Ringwald 
1214*af03003cSMatthias Ringwald     /* s4 */
1215*af03003cSMatthias Ringwald     tmp[0] = (product[4] >> 32) | (product[5] << 32);
1216*af03003cSMatthias Ringwald     tmp[1] = (product[5] >> 32) | (product[6] & 0xffffffff00000000ull);
1217*af03003cSMatthias Ringwald     tmp[2] = product[7];
1218*af03003cSMatthias Ringwald     tmp[3] = (product[6] >> 32) | (product[4] << 32);
1219*af03003cSMatthias Ringwald     carry += vli_add(result, result, tmp);
1220*af03003cSMatthias Ringwald 
1221*af03003cSMatthias Ringwald     /* d1 */
1222*af03003cSMatthias Ringwald     tmp[0] = (product[5] >> 32) | (product[6] << 32);
1223*af03003cSMatthias Ringwald     tmp[1] = (product[6] >> 32);
1224*af03003cSMatthias Ringwald     tmp[2] = 0;
1225*af03003cSMatthias Ringwald     tmp[3] = (product[4] & 0xffffffff) | (product[5] << 32);
1226*af03003cSMatthias Ringwald     carry -= vli_sub(result, result, tmp);
1227*af03003cSMatthias Ringwald 
1228*af03003cSMatthias Ringwald     /* d2 */
1229*af03003cSMatthias Ringwald     tmp[0] = product[6];
1230*af03003cSMatthias Ringwald     tmp[1] = product[7];
1231*af03003cSMatthias Ringwald     tmp[2] = 0;
1232*af03003cSMatthias Ringwald     tmp[3] = (product[4] >> 32) | (product[5] & 0xffffffff00000000ull);
1233*af03003cSMatthias Ringwald     carry -= vli_sub(result, result, tmp);
1234*af03003cSMatthias Ringwald 
1235*af03003cSMatthias Ringwald     /* d3 */
1236*af03003cSMatthias Ringwald     tmp[0] = (product[6] >> 32) | (product[7] << 32);
1237*af03003cSMatthias Ringwald     tmp[1] = (product[7] >> 32) | (product[4] << 32);
1238*af03003cSMatthias Ringwald     tmp[2] = (product[4] >> 32) | (product[5] << 32);
1239*af03003cSMatthias Ringwald     tmp[3] = (product[6] << 32);
1240*af03003cSMatthias Ringwald     carry -= vli_sub(result, result, tmp);
1241*af03003cSMatthias Ringwald 
1242*af03003cSMatthias Ringwald     /* d4 */
1243*af03003cSMatthias Ringwald     tmp[0] = product[7];
1244*af03003cSMatthias Ringwald     tmp[1] = product[4] & 0xffffffff00000000ull;
1245*af03003cSMatthias Ringwald     tmp[2] = product[5];
1246*af03003cSMatthias Ringwald     tmp[3] = product[6] & 0xffffffff00000000ull;
1247*af03003cSMatthias Ringwald     carry -= vli_sub(result, result, tmp);
1248*af03003cSMatthias Ringwald 
1249*af03003cSMatthias Ringwald     if (carry < 0) {
1250*af03003cSMatthias Ringwald         do {
1251*af03003cSMatthias Ringwald             carry += vli_add(result, result, curve_p);
1252*af03003cSMatthias Ringwald         } while (carry < 0);
1253*af03003cSMatthias Ringwald     } else {
1254*af03003cSMatthias Ringwald         while (carry || vli_cmp(curve_p, result) != 1) {
1255*af03003cSMatthias Ringwald             carry -= vli_sub(result, result, curve_p);
1256*af03003cSMatthias Ringwald         }
1257*af03003cSMatthias Ringwald     }
1258*af03003cSMatthias Ringwald }
1259*af03003cSMatthias Ringwald #endif /* uECC_WORD_SIZE */
1260*af03003cSMatthias Ringwald 
1261*af03003cSMatthias Ringwald #elif uECC_CURVE == uECC_secp256k1
1262*af03003cSMatthias Ringwald 
1263*af03003cSMatthias Ringwald #if uECC_WORD_SIZE == 1
1264*af03003cSMatthias Ringwald static void omega_mult(uint8_t * RESTRICT result, const uint8_t * RESTRICT right) {
1265*af03003cSMatthias Ringwald     /* Multiply by (2^32 + 2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */
1266*af03003cSMatthias Ringwald     uECC_word_t r0 = 0;
1267*af03003cSMatthias Ringwald     uECC_word_t r1 = 0;
1268*af03003cSMatthias Ringwald     uECC_word_t r2 = 0;
1269*af03003cSMatthias Ringwald     wordcount_t k;
1270*af03003cSMatthias Ringwald 
1271*af03003cSMatthias Ringwald     /* Multiply by (2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */
1272*af03003cSMatthias Ringwald     muladd(0xD1, right[0], &r0, &r1, &r2);
1273*af03003cSMatthias Ringwald     result[0] = r0;
1274*af03003cSMatthias Ringwald     r0 = r1;
1275*af03003cSMatthias Ringwald     r1 = r2;
1276*af03003cSMatthias Ringwald     /* r2 is still 0 */
1277*af03003cSMatthias Ringwald 
1278*af03003cSMatthias Ringwald     for (k = 1; k < uECC_WORDS; ++k) {
1279*af03003cSMatthias Ringwald         muladd(0x03, right[k - 1], &r0, &r1, &r2);
1280*af03003cSMatthias Ringwald         muladd(0xD1, right[k], &r0, &r1, &r2);
1281*af03003cSMatthias Ringwald         result[k] = r0;
1282*af03003cSMatthias Ringwald         r0 = r1;
1283*af03003cSMatthias Ringwald         r1 = r2;
1284*af03003cSMatthias Ringwald         r2 = 0;
1285*af03003cSMatthias Ringwald     }
1286*af03003cSMatthias Ringwald     muladd(0x03, right[uECC_WORDS - 1], &r0, &r1, &r2);
1287*af03003cSMatthias Ringwald     result[uECC_WORDS] = r0;
1288*af03003cSMatthias Ringwald     result[uECC_WORDS + 1] = r1;
1289*af03003cSMatthias Ringwald 
1290*af03003cSMatthias Ringwald     result[4 + uECC_WORDS] = vli_add(result + 4, result + 4, right); /* add the 2^32 multiple */
1291*af03003cSMatthias Ringwald }
1292*af03003cSMatthias Ringwald #elif uECC_WORD_SIZE == 4
1293*af03003cSMatthias Ringwald static void omega_mult(uint32_t * RESTRICT result, const uint32_t * RESTRICT right) {
1294*af03003cSMatthias Ringwald     /* Multiply by (2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */
1295*af03003cSMatthias Ringwald     uint32_t carry = 0;
1296*af03003cSMatthias Ringwald     wordcount_t k;
1297*af03003cSMatthias Ringwald 
1298*af03003cSMatthias Ringwald     for (k = 0; k < uECC_WORDS; ++k) {
1299*af03003cSMatthias Ringwald         uint64_t p = (uint64_t)0x3D1 * right[k] + carry;
1300*af03003cSMatthias Ringwald         result[k] = (p & 0xffffffff);
1301*af03003cSMatthias Ringwald         carry = p >> 32;
1302*af03003cSMatthias Ringwald     }
1303*af03003cSMatthias Ringwald     result[uECC_WORDS] = carry;
1304*af03003cSMatthias Ringwald 
1305*af03003cSMatthias Ringwald     result[1 + uECC_WORDS] = vli_add(result + 1, result + 1, right); /* add the 2^32 multiple */
1306*af03003cSMatthias Ringwald }
1307*af03003cSMatthias Ringwald #else
1308*af03003cSMatthias Ringwald static void omega_mult(uint64_t * RESTRICT result, const uint64_t * RESTRICT right) {
1309*af03003cSMatthias Ringwald     uECC_word_t r0 = 0;
1310*af03003cSMatthias Ringwald     uECC_word_t r1 = 0;
1311*af03003cSMatthias Ringwald     uECC_word_t r2 = 0;
1312*af03003cSMatthias Ringwald     wordcount_t k;
1313*af03003cSMatthias Ringwald 
1314*af03003cSMatthias Ringwald     /* Multiply by (2^32 + 2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */
1315*af03003cSMatthias Ringwald     for (k = 0; k < uECC_WORDS; ++k) {
1316*af03003cSMatthias Ringwald         muladd(0x1000003D1ull, right[k], &r0, &r1, &r2);
1317*af03003cSMatthias Ringwald         result[k] = r0;
1318*af03003cSMatthias Ringwald         r0 = r1;
1319*af03003cSMatthias Ringwald         r1 = r2;
1320*af03003cSMatthias Ringwald         r2 = 0;
1321*af03003cSMatthias Ringwald     }
1322*af03003cSMatthias Ringwald     result[uECC_WORDS] = r0;
1323*af03003cSMatthias Ringwald }
1324*af03003cSMatthias Ringwald #endif /* uECC_WORD_SIZE */
1325*af03003cSMatthias Ringwald 
1326*af03003cSMatthias Ringwald #elif uECC_CURVE == uECC_secp224r1
1327*af03003cSMatthias Ringwald 
1328*af03003cSMatthias Ringwald /* Computes result = product % curve_p
1329*af03003cSMatthias Ringwald    from http://www.nsa.gov/ia/_files/nist-routines.pdf */
1330*af03003cSMatthias Ringwald #if uECC_WORD_SIZE == 1
1331*af03003cSMatthias Ringwald // TODO it may be faster to use the omega_mult method when fully asm optimized.
1332*af03003cSMatthias Ringwald void vli_mmod_fast(uint8_t *RESTRICT result, uint8_t *RESTRICT product) {
1333*af03003cSMatthias Ringwald     uint8_t tmp[uECC_WORDS];
1334*af03003cSMatthias Ringwald     int8_t carry;
1335*af03003cSMatthias Ringwald 
1336*af03003cSMatthias Ringwald     /* t */
1337*af03003cSMatthias Ringwald     vli_set(result, product);
1338*af03003cSMatthias Ringwald 
1339*af03003cSMatthias Ringwald     /* s1 */
1340*af03003cSMatthias Ringwald     tmp[0] = tmp[1] = tmp[2] = tmp[3] = 0;
1341*af03003cSMatthias Ringwald     tmp[4] = tmp[5] = tmp[6] = tmp[7] = 0;
1342*af03003cSMatthias Ringwald     tmp[8] = tmp[9] = tmp[10] = tmp[11] = 0;
1343*af03003cSMatthias Ringwald     tmp[12] = product[28]; tmp[13] = product[29]; tmp[14] = product[30]; tmp[15] = product[31];
1344*af03003cSMatthias Ringwald     tmp[16] = product[32]; tmp[17] = product[33]; tmp[18] = product[34]; tmp[19] = product[35];
1345*af03003cSMatthias Ringwald     tmp[20] = product[36]; tmp[21] = product[37]; tmp[22] = product[38]; tmp[23] = product[39];
1346*af03003cSMatthias Ringwald     tmp[24] = product[40]; tmp[25] = product[41]; tmp[26] = product[42]; tmp[27] = product[43];
1347*af03003cSMatthias Ringwald     carry = vli_add(result, result, tmp);
1348*af03003cSMatthias Ringwald 
1349*af03003cSMatthias Ringwald     /* s2 */
1350*af03003cSMatthias Ringwald     tmp[12] = product[44]; tmp[13] = product[45]; tmp[14] = product[46]; tmp[15] = product[47];
1351*af03003cSMatthias Ringwald     tmp[16] = product[48]; tmp[17] = product[49]; tmp[18] = product[50]; tmp[19] = product[51];
1352*af03003cSMatthias Ringwald     tmp[20] = product[52]; tmp[21] = product[53]; tmp[22] = product[54]; tmp[23] = product[55];
1353*af03003cSMatthias Ringwald     tmp[24] = tmp[25] = tmp[26] = tmp[27] = 0;
1354*af03003cSMatthias Ringwald     carry += vli_add(result, result, tmp);
1355*af03003cSMatthias Ringwald 
1356*af03003cSMatthias Ringwald     /* d1 */
1357*af03003cSMatthias Ringwald     tmp[0]  = product[28]; tmp[1]  = product[29]; tmp[2]  = product[30]; tmp[3]  = product[31];
1358*af03003cSMatthias Ringwald     tmp[4]  = product[32]; tmp[5]  = product[33]; tmp[6]  = product[34]; tmp[7]  = product[35];
1359*af03003cSMatthias Ringwald     tmp[8]  = product[36]; tmp[9]  = product[37]; tmp[10] = product[38]; tmp[11] = product[39];
1360*af03003cSMatthias Ringwald     tmp[12] = product[40]; tmp[13] = product[41]; tmp[14] = product[42]; tmp[15] = product[43];
1361*af03003cSMatthias Ringwald     tmp[16] = product[44]; tmp[17] = product[45]; tmp[18] = product[46]; tmp[19] = product[47];
1362*af03003cSMatthias Ringwald     tmp[20] = product[48]; tmp[21] = product[49]; tmp[22] = product[50]; tmp[23] = product[51];
1363*af03003cSMatthias Ringwald     tmp[24] = product[52]; tmp[25] = product[53]; tmp[26] = product[54]; tmp[27] = product[55];
1364*af03003cSMatthias Ringwald     carry -= vli_sub(result, result, tmp);
1365*af03003cSMatthias Ringwald 
1366*af03003cSMatthias Ringwald     /* d2 */
1367*af03003cSMatthias Ringwald     tmp[0]  = product[44]; tmp[1]  = product[45]; tmp[2]  = product[46]; tmp[3]  = product[47];
1368*af03003cSMatthias Ringwald     tmp[4]  = product[48]; tmp[5]  = product[49]; tmp[6]  = product[50]; tmp[7]  = product[51];
1369*af03003cSMatthias Ringwald     tmp[8]  = product[52]; tmp[9]  = product[53]; tmp[10] = product[54]; tmp[11] = product[55];
1370*af03003cSMatthias Ringwald     tmp[12] = tmp[13] = tmp[14] = tmp[15] = 0;
1371*af03003cSMatthias Ringwald     tmp[16] = tmp[17] = tmp[18] = tmp[19] = 0;
1372*af03003cSMatthias Ringwald     tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0;
1373*af03003cSMatthias Ringwald     tmp[24] = tmp[25] = tmp[26] = tmp[27] = 0;
1374*af03003cSMatthias Ringwald     carry -= vli_sub(result, result, tmp);
1375*af03003cSMatthias Ringwald 
1376*af03003cSMatthias Ringwald     if (carry < 0) {
1377*af03003cSMatthias Ringwald         do {
1378*af03003cSMatthias Ringwald             carry += vli_add(result, result, curve_p);
1379*af03003cSMatthias Ringwald         } while (carry < 0);
1380*af03003cSMatthias Ringwald     } else {
1381*af03003cSMatthias Ringwald         while (carry || vli_cmp(curve_p, result) != 1) {
1382*af03003cSMatthias Ringwald             carry -= vli_sub(result, result, curve_p);
1383*af03003cSMatthias Ringwald         }
1384*af03003cSMatthias Ringwald     }
1385*af03003cSMatthias Ringwald }
1386*af03003cSMatthias Ringwald #elif uECC_WORD_SIZE == 4
1387*af03003cSMatthias Ringwald void vli_mmod_fast(uint32_t *RESTRICT result, uint32_t *RESTRICT product)
1388*af03003cSMatthias Ringwald {
1389*af03003cSMatthias Ringwald     uint32_t tmp[uECC_WORDS];
1390*af03003cSMatthias Ringwald     int carry;
1391*af03003cSMatthias Ringwald 
1392*af03003cSMatthias Ringwald     /* t */
1393*af03003cSMatthias Ringwald     vli_set(result, product);
1394*af03003cSMatthias Ringwald 
1395*af03003cSMatthias Ringwald     /* s1 */
1396*af03003cSMatthias Ringwald     tmp[0] = tmp[1] = tmp[2] = 0;
1397*af03003cSMatthias Ringwald     tmp[3] = product[7];
1398*af03003cSMatthias Ringwald     tmp[4] = product[8];
1399*af03003cSMatthias Ringwald     tmp[5] = product[9];
1400*af03003cSMatthias Ringwald     tmp[6] = product[10];
1401*af03003cSMatthias Ringwald     carry = vli_add(result, result, tmp);
1402*af03003cSMatthias Ringwald 
1403*af03003cSMatthias Ringwald     /* s2 */
1404*af03003cSMatthias Ringwald     tmp[3] = product[11];
1405*af03003cSMatthias Ringwald     tmp[4] = product[12];
1406*af03003cSMatthias Ringwald     tmp[5] = product[13];
1407*af03003cSMatthias Ringwald     tmp[6] = 0;
1408*af03003cSMatthias Ringwald     carry += vli_add(result, result, tmp);
1409*af03003cSMatthias Ringwald 
1410*af03003cSMatthias Ringwald     /* d1 */
1411*af03003cSMatthias Ringwald     tmp[0] = product[7];
1412*af03003cSMatthias Ringwald     tmp[1] = product[8];
1413*af03003cSMatthias Ringwald     tmp[2] = product[9];
1414*af03003cSMatthias Ringwald     tmp[3] = product[10];
1415*af03003cSMatthias Ringwald     tmp[4] = product[11];
1416*af03003cSMatthias Ringwald     tmp[5] = product[12];
1417*af03003cSMatthias Ringwald     tmp[6] = product[13];
1418*af03003cSMatthias Ringwald     carry -= vli_sub(result, result, tmp);
1419*af03003cSMatthias Ringwald 
1420*af03003cSMatthias Ringwald     /* d2 */
1421*af03003cSMatthias Ringwald     tmp[0] = product[11];
1422*af03003cSMatthias Ringwald     tmp[1] = product[12];
1423*af03003cSMatthias Ringwald     tmp[2] = product[13];
1424*af03003cSMatthias Ringwald     tmp[3] = tmp[4] = tmp[5] = tmp[6] = 0;
1425*af03003cSMatthias Ringwald     carry -= vli_sub(result, result, tmp);
1426*af03003cSMatthias Ringwald 
1427*af03003cSMatthias Ringwald     if (carry < 0) {
1428*af03003cSMatthias Ringwald         do {
1429*af03003cSMatthias Ringwald             carry += vli_add(result, result, curve_p);
1430*af03003cSMatthias Ringwald         } while (carry < 0);
1431*af03003cSMatthias Ringwald     } else {
1432*af03003cSMatthias Ringwald         while (carry || vli_cmp(curve_p, result) != 1) {
1433*af03003cSMatthias Ringwald             carry -= vli_sub(result, result, curve_p);
1434*af03003cSMatthias Ringwald         }
1435*af03003cSMatthias Ringwald     }
1436*af03003cSMatthias Ringwald }
1437*af03003cSMatthias Ringwald #endif /* uECC_WORD_SIZE */
1438*af03003cSMatthias Ringwald 
1439*af03003cSMatthias Ringwald #endif /* uECC_CURVE */
1440*af03003cSMatthias Ringwald #endif /* !asm_mmod_fast */
1441*af03003cSMatthias Ringwald 
1442*af03003cSMatthias Ringwald /* Computes result = (left * right) % curve_p. */
1443*af03003cSMatthias Ringwald static void vli_modMult_fast(uECC_word_t *result,
1444*af03003cSMatthias Ringwald                              const uECC_word_t *left,
1445*af03003cSMatthias Ringwald                              const uECC_word_t *right) {
1446*af03003cSMatthias Ringwald     uECC_word_t product[2 * uECC_WORDS];
1447*af03003cSMatthias Ringwald     vli_mult(product, left, right);
1448*af03003cSMatthias Ringwald     vli_mmod_fast(result, product);
1449*af03003cSMatthias Ringwald }
1450*af03003cSMatthias Ringwald 
1451*af03003cSMatthias Ringwald #if uECC_SQUARE_FUNC
1452*af03003cSMatthias Ringwald 
1453*af03003cSMatthias Ringwald /* Computes result = left^2 % curve_p. */
1454*af03003cSMatthias Ringwald static void vli_modSquare_fast(uECC_word_t *result, const uECC_word_t *left) {
1455*af03003cSMatthias Ringwald     uECC_word_t product[2 * uECC_WORDS];
1456*af03003cSMatthias Ringwald     vli_square(product, left);
1457*af03003cSMatthias Ringwald     vli_mmod_fast(result, product);
1458*af03003cSMatthias Ringwald }
1459*af03003cSMatthias Ringwald 
1460*af03003cSMatthias Ringwald #else /* uECC_SQUARE_FUNC */
1461*af03003cSMatthias Ringwald 
1462*af03003cSMatthias Ringwald #define vli_modSquare_fast(result, left) vli_modMult_fast((result), (left), (left))
1463*af03003cSMatthias Ringwald 
1464*af03003cSMatthias Ringwald #endif /* uECC_SQUARE_FUNC */
1465*af03003cSMatthias Ringwald 
1466*af03003cSMatthias Ringwald 
1467*af03003cSMatthias Ringwald #define EVEN(vli) (!(vli[0] & 1))
1468*af03003cSMatthias Ringwald /* Computes result = (1 / input) % mod. All VLIs are the same size.
1469*af03003cSMatthias Ringwald    See "From Euclid's GCD to Montgomery Multiplication to the Great Divide"
1470*af03003cSMatthias Ringwald    https://labs.oracle.com/techrep/2001/smli_tr-2001-95.pdf */
1471*af03003cSMatthias Ringwald #if !asm_modInv
1472*af03003cSMatthias Ringwald static void vli_modInv(uECC_word_t *result, const uECC_word_t *input, const uECC_word_t *mod) {
1473*af03003cSMatthias Ringwald     uECC_word_t a[uECC_WORDS], b[uECC_WORDS], u[uECC_WORDS], v[uECC_WORDS];
1474*af03003cSMatthias Ringwald     uECC_word_t carry;
1475*af03003cSMatthias Ringwald     cmpresult_t cmpResult;
1476*af03003cSMatthias Ringwald 
1477*af03003cSMatthias Ringwald     if (vli_isZero(input)) {
1478*af03003cSMatthias Ringwald         vli_clear(result);
1479*af03003cSMatthias Ringwald         return;
1480*af03003cSMatthias Ringwald     }
1481*af03003cSMatthias Ringwald 
1482*af03003cSMatthias Ringwald     vli_set(a, input);
1483*af03003cSMatthias Ringwald     vli_set(b, mod);
1484*af03003cSMatthias Ringwald     vli_clear(u);
1485*af03003cSMatthias Ringwald     u[0] = 1;
1486*af03003cSMatthias Ringwald     vli_clear(v);
1487*af03003cSMatthias Ringwald     while ((cmpResult = vli_cmp(a, b)) != 0) {
1488*af03003cSMatthias Ringwald         carry = 0;
1489*af03003cSMatthias Ringwald         if (EVEN(a)) {
1490*af03003cSMatthias Ringwald             vli_rshift1(a);
1491*af03003cSMatthias Ringwald             if (!EVEN(u)) {
1492*af03003cSMatthias Ringwald                 carry = vli_add(u, u, mod);
1493*af03003cSMatthias Ringwald             }
1494*af03003cSMatthias Ringwald             vli_rshift1(u);
1495*af03003cSMatthias Ringwald             if (carry) {
1496*af03003cSMatthias Ringwald                 u[uECC_WORDS - 1] |= HIGH_BIT_SET;
1497*af03003cSMatthias Ringwald             }
1498*af03003cSMatthias Ringwald         } else if (EVEN(b)) {
1499*af03003cSMatthias Ringwald             vli_rshift1(b);
1500*af03003cSMatthias Ringwald             if (!EVEN(v)) {
1501*af03003cSMatthias Ringwald                 carry = vli_add(v, v, mod);
1502*af03003cSMatthias Ringwald             }
1503*af03003cSMatthias Ringwald             vli_rshift1(v);
1504*af03003cSMatthias Ringwald             if (carry) {
1505*af03003cSMatthias Ringwald                 v[uECC_WORDS - 1] |= HIGH_BIT_SET;
1506*af03003cSMatthias Ringwald             }
1507*af03003cSMatthias Ringwald         } else if (cmpResult > 0) {
1508*af03003cSMatthias Ringwald             vli_sub(a, a, b);
1509*af03003cSMatthias Ringwald             vli_rshift1(a);
1510*af03003cSMatthias Ringwald             if (vli_cmp(u, v) < 0) {
1511*af03003cSMatthias Ringwald                 vli_add(u, u, mod);
1512*af03003cSMatthias Ringwald             }
1513*af03003cSMatthias Ringwald             vli_sub(u, u, v);
1514*af03003cSMatthias Ringwald             if (!EVEN(u)) {
1515*af03003cSMatthias Ringwald                 carry = vli_add(u, u, mod);
1516*af03003cSMatthias Ringwald             }
1517*af03003cSMatthias Ringwald             vli_rshift1(u);
1518*af03003cSMatthias Ringwald             if (carry) {
1519*af03003cSMatthias Ringwald                 u[uECC_WORDS - 1] |= HIGH_BIT_SET;
1520*af03003cSMatthias Ringwald             }
1521*af03003cSMatthias Ringwald         } else {
1522*af03003cSMatthias Ringwald             vli_sub(b, b, a);
1523*af03003cSMatthias Ringwald             vli_rshift1(b);
1524*af03003cSMatthias Ringwald             if (vli_cmp(v, u) < 0) {
1525*af03003cSMatthias Ringwald                 vli_add(v, v, mod);
1526*af03003cSMatthias Ringwald             }
1527*af03003cSMatthias Ringwald             vli_sub(v, v, u);
1528*af03003cSMatthias Ringwald             if (!EVEN(v)) {
1529*af03003cSMatthias Ringwald                 carry = vli_add(v, v, mod);
1530*af03003cSMatthias Ringwald             }
1531*af03003cSMatthias Ringwald             vli_rshift1(v);
1532*af03003cSMatthias Ringwald             if (carry) {
1533*af03003cSMatthias Ringwald                 v[uECC_WORDS - 1] |= HIGH_BIT_SET;
1534*af03003cSMatthias Ringwald             }
1535*af03003cSMatthias Ringwald         }
1536*af03003cSMatthias Ringwald     }
1537*af03003cSMatthias Ringwald     vli_set(result, u);
1538*af03003cSMatthias Ringwald }
1539*af03003cSMatthias Ringwald #endif /* !asm_modInv */
1540*af03003cSMatthias Ringwald 
1541*af03003cSMatthias Ringwald /* ------ Point operations ------ */
1542*af03003cSMatthias Ringwald 
1543*af03003cSMatthias Ringwald /* Returns 1 if 'point' is the point at infinity, 0 otherwise. */
1544*af03003cSMatthias Ringwald static cmpresult_t EccPoint_isZero(const EccPoint *point) {
1545*af03003cSMatthias Ringwald     return (vli_isZero(point->x) && vli_isZero(point->y));
1546*af03003cSMatthias Ringwald }
1547*af03003cSMatthias Ringwald 
1548*af03003cSMatthias Ringwald /* Point multiplication algorithm using Montgomery's ladder with co-Z coordinates.
1549*af03003cSMatthias Ringwald From http://eprint.iacr.org/2011/338.pdf
1550*af03003cSMatthias Ringwald */
1551*af03003cSMatthias Ringwald 
1552*af03003cSMatthias Ringwald /* Double in place */
1553*af03003cSMatthias Ringwald #if (uECC_CURVE == uECC_secp256k1)
1554*af03003cSMatthias Ringwald static void EccPoint_double_jacobian(uECC_word_t * RESTRICT X1,
1555*af03003cSMatthias Ringwald                                      uECC_word_t * RESTRICT Y1,
1556*af03003cSMatthias Ringwald                                      uECC_word_t * RESTRICT Z1) {
1557*af03003cSMatthias Ringwald     /* t1 = X, t2 = Y, t3 = Z */
1558*af03003cSMatthias Ringwald     uECC_word_t t4[uECC_WORDS];
1559*af03003cSMatthias Ringwald     uECC_word_t t5[uECC_WORDS];
1560*af03003cSMatthias Ringwald 
1561*af03003cSMatthias Ringwald     if (vli_isZero(Z1)) {
1562*af03003cSMatthias Ringwald         return;
1563*af03003cSMatthias Ringwald     }
1564*af03003cSMatthias Ringwald 
1565*af03003cSMatthias Ringwald     vli_modSquare_fast(t5, Y1);   /* t5 = y1^2 */
1566*af03003cSMatthias Ringwald     vli_modMult_fast(t4, X1, t5); /* t4 = x1*y1^2 = A */
1567*af03003cSMatthias Ringwald     vli_modSquare_fast(X1, X1);   /* t1 = x1^2 */
1568*af03003cSMatthias Ringwald     vli_modSquare_fast(t5, t5);   /* t5 = y1^4 */
1569*af03003cSMatthias Ringwald     vli_modMult_fast(Z1, Y1, Z1); /* t3 = y1*z1 = z3 */
1570*af03003cSMatthias Ringwald 
1571*af03003cSMatthias Ringwald     vli_modAdd(Y1, X1, X1, curve_p); /* t2 = 2*x1^2 */
1572*af03003cSMatthias Ringwald     vli_modAdd(Y1, Y1, X1, curve_p); /* t2 = 3*x1^2 */
1573*af03003cSMatthias Ringwald     if (vli_testBit(Y1, 0)) {
1574*af03003cSMatthias Ringwald         uECC_word_t carry = vli_add(Y1, Y1, curve_p);
1575*af03003cSMatthias Ringwald         vli_rshift1(Y1);
1576*af03003cSMatthias Ringwald         Y1[uECC_WORDS - 1] |= carry << (uECC_WORD_BITS - 1);
1577*af03003cSMatthias Ringwald     } else {
1578*af03003cSMatthias Ringwald         vli_rshift1(Y1);
1579*af03003cSMatthias Ringwald     }
1580*af03003cSMatthias Ringwald     /* t2 = 3/2*(x1^2) = B */
1581*af03003cSMatthias Ringwald 
1582*af03003cSMatthias Ringwald     vli_modSquare_fast(X1, Y1);      /* t1 = B^2 */
1583*af03003cSMatthias Ringwald     vli_modSub(X1, X1, t4, curve_p); /* t1 = B^2 - A */
1584*af03003cSMatthias Ringwald     vli_modSub(X1, X1, t4, curve_p); /* t1 = B^2 - 2A = x3 */
1585*af03003cSMatthias Ringwald 
1586*af03003cSMatthias Ringwald     vli_modSub(t4, t4, X1, curve_p); /* t4 = A - x3 */
1587*af03003cSMatthias Ringwald     vli_modMult_fast(Y1, Y1, t4);    /* t2 = B * (A - x3) */
1588*af03003cSMatthias Ringwald     vli_modSub(Y1, Y1, t5, curve_p); /* t2 = B * (A - x3) - y1^4 = y3 */
1589*af03003cSMatthias Ringwald }
1590*af03003cSMatthias Ringwald #else
1591*af03003cSMatthias Ringwald static void EccPoint_double_jacobian(uECC_word_t * RESTRICT X1,
1592*af03003cSMatthias Ringwald                                      uECC_word_t * RESTRICT Y1,
1593*af03003cSMatthias Ringwald                                      uECC_word_t * RESTRICT Z1) {
1594*af03003cSMatthias Ringwald     /* t1 = X, t2 = Y, t3 = Z */
1595*af03003cSMatthias Ringwald     uECC_word_t t4[uECC_WORDS];
1596*af03003cSMatthias Ringwald     uECC_word_t t5[uECC_WORDS];
1597*af03003cSMatthias Ringwald 
1598*af03003cSMatthias Ringwald     if (vli_isZero(Z1)) {
1599*af03003cSMatthias Ringwald         return;
1600*af03003cSMatthias Ringwald     }
1601*af03003cSMatthias Ringwald 
1602*af03003cSMatthias Ringwald     vli_modSquare_fast(t4, Y1);   /* t4 = y1^2 */
1603*af03003cSMatthias Ringwald     vli_modMult_fast(t5, X1, t4); /* t5 = x1*y1^2 = A */
1604*af03003cSMatthias Ringwald     vli_modSquare_fast(t4, t4);   /* t4 = y1^4 */
1605*af03003cSMatthias Ringwald     vli_modMult_fast(Y1, Y1, Z1); /* t2 = y1*z1 = z3 */
1606*af03003cSMatthias Ringwald     vli_modSquare_fast(Z1, Z1);   /* t3 = z1^2 */
1607*af03003cSMatthias Ringwald 
1608*af03003cSMatthias Ringwald     vli_modAdd(X1, X1, Z1, curve_p); /* t1 = x1 + z1^2 */
1609*af03003cSMatthias Ringwald     vli_modAdd(Z1, Z1, Z1, curve_p); /* t3 = 2*z1^2 */
1610*af03003cSMatthias Ringwald     vli_modSub_fast(Z1, X1, Z1);     /* t3 = x1 - z1^2 */
1611*af03003cSMatthias Ringwald     vli_modMult_fast(X1, X1, Z1);    /* t1 = x1^2 - z1^4 */
1612*af03003cSMatthias Ringwald 
1613*af03003cSMatthias Ringwald     vli_modAdd(Z1, X1, X1, curve_p); /* t3 = 2*(x1^2 - z1^4) */
1614*af03003cSMatthias Ringwald     vli_modAdd(X1, X1, Z1, curve_p); /* t1 = 3*(x1^2 - z1^4) */
1615*af03003cSMatthias Ringwald     if (vli_testBit(X1, 0)) {
1616*af03003cSMatthias Ringwald         uECC_word_t l_carry = vli_add(X1, X1, curve_p);
1617*af03003cSMatthias Ringwald         vli_rshift1(X1);
1618*af03003cSMatthias Ringwald         X1[uECC_WORDS - 1] |= l_carry << (uECC_WORD_BITS - 1);
1619*af03003cSMatthias Ringwald     } else {
1620*af03003cSMatthias Ringwald         vli_rshift1(X1);
1621*af03003cSMatthias Ringwald     }
1622*af03003cSMatthias Ringwald     /* t1 = 3/2*(x1^2 - z1^4) = B */
1623*af03003cSMatthias Ringwald 
1624*af03003cSMatthias Ringwald     vli_modSquare_fast(Z1, X1);   /* t3 = B^2 */
1625*af03003cSMatthias Ringwald     vli_modSub_fast(Z1, Z1, t5);  /* t3 = B^2 - A */
1626*af03003cSMatthias Ringwald     vli_modSub_fast(Z1, Z1, t5);  /* t3 = B^2 - 2A = x3 */
1627*af03003cSMatthias Ringwald     vli_modSub_fast(t5, t5, Z1);  /* t5 = A - x3 */
1628*af03003cSMatthias Ringwald     vli_modMult_fast(X1, X1, t5); /* t1 = B * (A - x3) */
1629*af03003cSMatthias Ringwald     vli_modSub_fast(t4, X1, t4);  /* t4 = B * (A - x3) - y1^4 = y3 */
1630*af03003cSMatthias Ringwald 
1631*af03003cSMatthias Ringwald     vli_set(X1, Z1);
1632*af03003cSMatthias Ringwald     vli_set(Z1, Y1);
1633*af03003cSMatthias Ringwald     vli_set(Y1, t4);
1634*af03003cSMatthias Ringwald }
1635*af03003cSMatthias Ringwald #endif
1636*af03003cSMatthias Ringwald 
1637*af03003cSMatthias Ringwald /* Modify (x1, y1) => (x1 * z^2, y1 * z^3) */
1638*af03003cSMatthias Ringwald static void apply_z(uECC_word_t * RESTRICT X1,
1639*af03003cSMatthias Ringwald                     uECC_word_t * RESTRICT Y1,
1640*af03003cSMatthias Ringwald                     const uECC_word_t * RESTRICT Z) {
1641*af03003cSMatthias Ringwald     uECC_word_t t1[uECC_WORDS];
1642*af03003cSMatthias Ringwald 
1643*af03003cSMatthias Ringwald     vli_modSquare_fast(t1, Z);    /* z^2 */
1644*af03003cSMatthias Ringwald     vli_modMult_fast(X1, X1, t1); /* x1 * z^2 */
1645*af03003cSMatthias Ringwald     vli_modMult_fast(t1, t1, Z);  /* z^3 */
1646*af03003cSMatthias Ringwald     vli_modMult_fast(Y1, Y1, t1); /* y1 * z^3 */
1647*af03003cSMatthias Ringwald }
1648*af03003cSMatthias Ringwald 
1649*af03003cSMatthias Ringwald /* P = (x1, y1) => 2P, (x2, y2) => P' */
1650*af03003cSMatthias Ringwald static void XYcZ_initial_double(uECC_word_t * RESTRICT X1,
1651*af03003cSMatthias Ringwald                                 uECC_word_t * RESTRICT Y1,
1652*af03003cSMatthias Ringwald                                 uECC_word_t * RESTRICT X2,
1653*af03003cSMatthias Ringwald                                 uECC_word_t * RESTRICT Y2,
1654*af03003cSMatthias Ringwald                                 const uECC_word_t * RESTRICT initial_Z) {
1655*af03003cSMatthias Ringwald     uECC_word_t z[uECC_WORDS];
1656*af03003cSMatthias Ringwald     if (initial_Z) {
1657*af03003cSMatthias Ringwald         vli_set(z, initial_Z);
1658*af03003cSMatthias Ringwald     } else {
1659*af03003cSMatthias Ringwald         vli_clear(z);
1660*af03003cSMatthias Ringwald         z[0] = 1;
1661*af03003cSMatthias Ringwald     }
1662*af03003cSMatthias Ringwald 
1663*af03003cSMatthias Ringwald     vli_set(X2, X1);
1664*af03003cSMatthias Ringwald     vli_set(Y2, Y1);
1665*af03003cSMatthias Ringwald 
1666*af03003cSMatthias Ringwald     apply_z(X1, Y1, z);
1667*af03003cSMatthias Ringwald     EccPoint_double_jacobian(X1, Y1, z);
1668*af03003cSMatthias Ringwald     apply_z(X2, Y2, z);
1669*af03003cSMatthias Ringwald }
1670*af03003cSMatthias Ringwald 
1671*af03003cSMatthias Ringwald /* Input P = (x1, y1, Z), Q = (x2, y2, Z)
1672*af03003cSMatthias Ringwald    Output P' = (x1', y1', Z3), P + Q = (x3, y3, Z3)
1673*af03003cSMatthias Ringwald    or P => P', Q => P + Q
1674*af03003cSMatthias Ringwald */
1675*af03003cSMatthias Ringwald static void XYcZ_add(uECC_word_t * RESTRICT X1,
1676*af03003cSMatthias Ringwald                      uECC_word_t * RESTRICT Y1,
1677*af03003cSMatthias Ringwald                      uECC_word_t * RESTRICT X2,
1678*af03003cSMatthias Ringwald                      uECC_word_t * RESTRICT Y2) {
1679*af03003cSMatthias Ringwald     /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
1680*af03003cSMatthias Ringwald     uECC_word_t t5[uECC_WORDS];
1681*af03003cSMatthias Ringwald 
1682*af03003cSMatthias Ringwald     vli_modSub_fast(t5, X2, X1);  /* t5 = x2 - x1 */
1683*af03003cSMatthias Ringwald     vli_modSquare_fast(t5, t5);   /* t5 = (x2 - x1)^2 = A */
1684*af03003cSMatthias Ringwald     vli_modMult_fast(X1, X1, t5); /* t1 = x1*A = B */
1685*af03003cSMatthias Ringwald     vli_modMult_fast(X2, X2, t5); /* t3 = x2*A = C */
1686*af03003cSMatthias Ringwald     vli_modSub_fast(Y2, Y2, Y1);  /* t4 = y2 - y1 */
1687*af03003cSMatthias Ringwald     vli_modSquare_fast(t5, Y2);   /* t5 = (y2 - y1)^2 = D */
1688*af03003cSMatthias Ringwald 
1689*af03003cSMatthias Ringwald     vli_modSub_fast(t5, t5, X1);  /* t5 = D - B */
1690*af03003cSMatthias Ringwald     vli_modSub_fast(t5, t5, X2);  /* t5 = D - B - C = x3 */
1691*af03003cSMatthias Ringwald     vli_modSub_fast(X2, X2, X1);  /* t3 = C - B */
1692*af03003cSMatthias Ringwald     vli_modMult_fast(Y1, Y1, X2); /* t2 = y1*(C - B) */
1693*af03003cSMatthias Ringwald     vli_modSub_fast(X2, X1, t5);  /* t3 = B - x3 */
1694*af03003cSMatthias Ringwald     vli_modMult_fast(Y2, Y2, X2); /* t4 = (y2 - y1)*(B - x3) */
1695*af03003cSMatthias Ringwald     vli_modSub_fast(Y2, Y2, Y1);  /* t4 = y3 */
1696*af03003cSMatthias Ringwald 
1697*af03003cSMatthias Ringwald     vli_set(X2, t5);
1698*af03003cSMatthias Ringwald }
1699*af03003cSMatthias Ringwald 
1700*af03003cSMatthias Ringwald /* Input P = (x1, y1, Z), Q = (x2, y2, Z)
1701*af03003cSMatthias Ringwald    Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3)
1702*af03003cSMatthias Ringwald    or P => P - Q, Q => P + Q
1703*af03003cSMatthias Ringwald */
1704*af03003cSMatthias Ringwald static void XYcZ_addC(uECC_word_t * RESTRICT X1,
1705*af03003cSMatthias Ringwald                       uECC_word_t * RESTRICT Y1,
1706*af03003cSMatthias Ringwald                       uECC_word_t * RESTRICT X2,
1707*af03003cSMatthias Ringwald                       uECC_word_t * RESTRICT Y2) {
1708*af03003cSMatthias Ringwald     /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
1709*af03003cSMatthias Ringwald     uECC_word_t t5[uECC_WORDS];
1710*af03003cSMatthias Ringwald     uECC_word_t t6[uECC_WORDS];
1711*af03003cSMatthias Ringwald     uECC_word_t t7[uECC_WORDS];
1712*af03003cSMatthias Ringwald 
1713*af03003cSMatthias Ringwald     vli_modSub_fast(t5, X2, X1);     /* t5 = x2 - x1 */
1714*af03003cSMatthias Ringwald     vli_modSquare_fast(t5, t5);      /* t5 = (x2 - x1)^2 = A */
1715*af03003cSMatthias Ringwald     vli_modMult_fast(X1, X1, t5);    /* t1 = x1*A = B */
1716*af03003cSMatthias Ringwald     vli_modMult_fast(X2, X2, t5);    /* t3 = x2*A = C */
1717*af03003cSMatthias Ringwald     vli_modAdd(t5, Y2, Y1, curve_p); /* t5 = y2 + y1 */
1718*af03003cSMatthias Ringwald     vli_modSub_fast(Y2, Y2, Y1);     /* t4 = y2 - y1 */
1719*af03003cSMatthias Ringwald 
1720*af03003cSMatthias Ringwald     vli_modSub_fast(t6, X2, X1);     /* t6 = C - B */
1721*af03003cSMatthias Ringwald     vli_modMult_fast(Y1, Y1, t6);    /* t2 = y1 * (C - B) = E */
1722*af03003cSMatthias Ringwald     vli_modAdd(t6, X1, X2, curve_p); /* t6 = B + C */
1723*af03003cSMatthias Ringwald     vli_modSquare_fast(X2, Y2);      /* t3 = (y2 - y1)^2 = D */
1724*af03003cSMatthias Ringwald     vli_modSub_fast(X2, X2, t6);     /* t3 = D - (B + C) = x3 */
1725*af03003cSMatthias Ringwald 
1726*af03003cSMatthias Ringwald     vli_modSub_fast(t7, X1, X2);  /* t7 = B - x3 */
1727*af03003cSMatthias Ringwald     vli_modMult_fast(Y2, Y2, t7); /* t4 = (y2 - y1)*(B - x3) */
1728*af03003cSMatthias Ringwald     vli_modSub_fast(Y2, Y2, Y1);  /* t4 = (y2 - y1)*(B - x3) - E = y3 */
1729*af03003cSMatthias Ringwald 
1730*af03003cSMatthias Ringwald     vli_modSquare_fast(t7, t5);   /* t7 = (y2 + y1)^2 = F */
1731*af03003cSMatthias Ringwald     vli_modSub_fast(t7, t7, t6);  /* t7 = F - (B + C) = x3' */
1732*af03003cSMatthias Ringwald     vli_modSub_fast(t6, t7, X1);  /* t6 = x3' - B */
1733*af03003cSMatthias Ringwald     vli_modMult_fast(t6, t6, t5); /* t6 = (y2 + y1)*(x3' - B) */
1734*af03003cSMatthias Ringwald     vli_modSub_fast(Y1, t6, Y1);  /* t2 = (y2 + y1)*(x3' - B) - E = y3' */
1735*af03003cSMatthias Ringwald 
1736*af03003cSMatthias Ringwald     vli_set(X1, t7);
1737*af03003cSMatthias Ringwald }
1738*af03003cSMatthias Ringwald 
1739*af03003cSMatthias Ringwald static void EccPoint_mult(EccPoint * RESTRICT result,
1740*af03003cSMatthias Ringwald                           const EccPoint * RESTRICT point,
1741*af03003cSMatthias Ringwald                           const uECC_word_t * RESTRICT scalar,
1742*af03003cSMatthias Ringwald                           const uECC_word_t * RESTRICT initialZ,
1743*af03003cSMatthias Ringwald                           bitcount_t numBits) {
1744*af03003cSMatthias Ringwald     /* R0 and R1 */
1745*af03003cSMatthias Ringwald     uECC_word_t Rx[2][uECC_WORDS];
1746*af03003cSMatthias Ringwald     uECC_word_t Ry[2][uECC_WORDS];
1747*af03003cSMatthias Ringwald     uECC_word_t z[uECC_WORDS];
1748*af03003cSMatthias Ringwald     bitcount_t i;
1749*af03003cSMatthias Ringwald     uECC_word_t nb;
1750*af03003cSMatthias Ringwald 
1751*af03003cSMatthias Ringwald     vli_set(Rx[1], point->x);
1752*af03003cSMatthias Ringwald     vli_set(Ry[1], point->y);
1753*af03003cSMatthias Ringwald 
1754*af03003cSMatthias Ringwald     XYcZ_initial_double(Rx[1], Ry[1], Rx[0], Ry[0], initialZ);
1755*af03003cSMatthias Ringwald 
1756*af03003cSMatthias Ringwald     for (i = numBits - 2; i > 0; --i) {
1757*af03003cSMatthias Ringwald         nb = !vli_testBit(scalar, i);
1758*af03003cSMatthias Ringwald         XYcZ_addC(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb]);
1759*af03003cSMatthias Ringwald         XYcZ_add(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb]);
1760*af03003cSMatthias Ringwald     }
1761*af03003cSMatthias Ringwald 
1762*af03003cSMatthias Ringwald     nb = !vli_testBit(scalar, 0);
1763*af03003cSMatthias Ringwald     XYcZ_addC(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb]);
1764*af03003cSMatthias Ringwald 
1765*af03003cSMatthias Ringwald     /* Find final 1/Z value. */
1766*af03003cSMatthias Ringwald     vli_modSub_fast(z, Rx[1], Rx[0]);   /* X1 - X0 */
1767*af03003cSMatthias Ringwald     vli_modMult_fast(z, z, Ry[1 - nb]); /* Yb * (X1 - X0) */
1768*af03003cSMatthias Ringwald     vli_modMult_fast(z, z, point->x); /* xP * Yb * (X1 - X0) */
1769*af03003cSMatthias Ringwald     vli_modInv(z, z, curve_p);          /* 1 / (xP * Yb * (X1 - X0)) */
1770*af03003cSMatthias Ringwald     vli_modMult_fast(z, z, point->y); /* yP / (xP * Yb * (X1 - X0)) */
1771*af03003cSMatthias Ringwald     vli_modMult_fast(z, z, Rx[1 - nb]); /* Xb * yP / (xP * Yb * (X1 - X0)) */
1772*af03003cSMatthias Ringwald     /* End 1/Z calculation */
1773*af03003cSMatthias Ringwald 
1774*af03003cSMatthias Ringwald     XYcZ_add(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb]);
1775*af03003cSMatthias Ringwald     apply_z(Rx[0], Ry[0], z);
1776*af03003cSMatthias Ringwald 
1777*af03003cSMatthias Ringwald     vli_set(result->x, Rx[0]);
1778*af03003cSMatthias Ringwald     vli_set(result->y, Ry[0]);
1779*af03003cSMatthias Ringwald }
1780*af03003cSMatthias Ringwald 
1781*af03003cSMatthias Ringwald static int EccPoint_compute_public_key(EccPoint *result, uECC_word_t *private) {
1782*af03003cSMatthias Ringwald     uECC_word_t tmp1[uECC_WORDS];
1783*af03003cSMatthias Ringwald     uECC_word_t tmp2[uECC_WORDS];
1784*af03003cSMatthias Ringwald     uECC_word_t *p2[2] = {tmp1, tmp2};
1785*af03003cSMatthias Ringwald     uECC_word_t carry;
1786*af03003cSMatthias Ringwald 
1787*af03003cSMatthias Ringwald     /* Make sure the private key is in the range [1, n-1]. */
1788*af03003cSMatthias Ringwald     if (vli_isZero(private)) {
1789*af03003cSMatthias Ringwald         return 0;
1790*af03003cSMatthias Ringwald     }
1791*af03003cSMatthias Ringwald 
1792*af03003cSMatthias Ringwald #if (uECC_CURVE == uECC_secp160r1)
1793*af03003cSMatthias Ringwald     // Don't regularize the bitcount for secp160r1, since it would have a larger performance
1794*af03003cSMatthias Ringwald     // impact (about 2% slower on average) and requires the vli_xxx_n functions, leading to
1795*af03003cSMatthias Ringwald     // a significant increase in code size.
1796*af03003cSMatthias Ringwald 
1797*af03003cSMatthias Ringwald     EccPoint_mult(result, &curve_G, private, 0, vli_numBits(private, uECC_WORDS));
1798*af03003cSMatthias Ringwald #else
1799*af03003cSMatthias Ringwald     if (vli_cmp(curve_n, private) != 1) {
1800*af03003cSMatthias Ringwald         return 0;
1801*af03003cSMatthias Ringwald     }
1802*af03003cSMatthias Ringwald 
1803*af03003cSMatthias Ringwald     // Regularize the bitcount for the private key so that attackers cannot use a side channel
1804*af03003cSMatthias Ringwald     // attack to learn the number of leading zeros.
1805*af03003cSMatthias Ringwald     carry = vli_add(tmp1, private, curve_n);
1806*af03003cSMatthias Ringwald     vli_add(tmp2, tmp1, curve_n);
1807*af03003cSMatthias Ringwald     EccPoint_mult(result, &curve_G, p2[!carry], 0, (uECC_BYTES * 8) + 1);
1808*af03003cSMatthias Ringwald #endif
1809*af03003cSMatthias Ringwald 
1810*af03003cSMatthias Ringwald     if (EccPoint_isZero(result)) {
1811*af03003cSMatthias Ringwald         return 0;
1812*af03003cSMatthias Ringwald     }
1813*af03003cSMatthias Ringwald     return 1;
1814*af03003cSMatthias Ringwald }
1815*af03003cSMatthias Ringwald 
1816*af03003cSMatthias Ringwald #if uECC_CURVE == uECC_secp224r1
1817*af03003cSMatthias Ringwald 
1818*af03003cSMatthias Ringwald /* Routine 3.2.4 RS;  from http://www.nsa.gov/ia/_files/nist-routines.pdf */
1819*af03003cSMatthias Ringwald static void mod_sqrt_secp224r1_rs(uECC_word_t *d1,
1820*af03003cSMatthias Ringwald                                   uECC_word_t *e1,
1821*af03003cSMatthias Ringwald                                   uECC_word_t *f1,
1822*af03003cSMatthias Ringwald                                   const uECC_word_t *d0,
1823*af03003cSMatthias Ringwald                                   const uECC_word_t *e0,
1824*af03003cSMatthias Ringwald                                   const uECC_word_t *f0) {
1825*af03003cSMatthias Ringwald     uECC_word_t t[uECC_WORDS];
1826*af03003cSMatthias Ringwald 
1827*af03003cSMatthias Ringwald     vli_modSquare_fast(t, d0);                 /* t <-- d0 ^ 2 */
1828*af03003cSMatthias Ringwald     vli_modMult_fast(e1, d0, e0);              /* e1 <-- d0 * e0 */
1829*af03003cSMatthias Ringwald     vli_modAdd(d1, t, f0, curve_p);            /* d1 <-- t  + f0 */
1830*af03003cSMatthias Ringwald     vli_modAdd(e1, e1, e1, curve_p);           /* e1 <-- e1 + e1 */
1831*af03003cSMatthias Ringwald     vli_modMult_fast(f1, t, f0);               /* f1 <-- t  * f0 */
1832*af03003cSMatthias Ringwald     vli_modAdd(f1, f1, f1, curve_p);           /* f1 <-- f1 + f1 */
1833*af03003cSMatthias Ringwald     vli_modAdd(f1, f1, f1, curve_p);           /* f1 <-- f1 + f1 */
1834*af03003cSMatthias Ringwald }
1835*af03003cSMatthias Ringwald 
1836*af03003cSMatthias Ringwald /* Routine 3.2.5 RSS;  from http://www.nsa.gov/ia/_files/nist-routines.pdf */
1837*af03003cSMatthias Ringwald static void mod_sqrt_secp224r1_rss(uECC_word_t *d1,
1838*af03003cSMatthias Ringwald                                    uECC_word_t *e1,
1839*af03003cSMatthias Ringwald                                    uECC_word_t *f1,
1840*af03003cSMatthias Ringwald                                    const uECC_word_t *d0,
1841*af03003cSMatthias Ringwald                                    const uECC_word_t *e0,
1842*af03003cSMatthias Ringwald                                    const uECC_word_t *f0,
1843*af03003cSMatthias Ringwald                                    const bitcount_t j) {
1844*af03003cSMatthias Ringwald     bitcount_t i;
1845*af03003cSMatthias Ringwald 
1846*af03003cSMatthias Ringwald     vli_set(d1, d0);                           /* d1 <-- d0 */
1847*af03003cSMatthias Ringwald     vli_set(e1, e0);                           /* e1 <-- e0 */
1848*af03003cSMatthias Ringwald     vli_set(f1, f0);                           /* f1 <-- f0 */
1849*af03003cSMatthias Ringwald     for (i = 1; i <= j; i++) {
1850*af03003cSMatthias Ringwald         mod_sqrt_secp224r1_rs(d1, e1, f1, d1, e1, f1); /* RS (d1,e1,f1,d1,e1,f1) */
1851*af03003cSMatthias Ringwald     }
1852*af03003cSMatthias Ringwald }
1853*af03003cSMatthias Ringwald 
1854*af03003cSMatthias Ringwald /* Routine 3.2.6 RM;  from http://www.nsa.gov/ia/_files/nist-routines.pdf */
1855*af03003cSMatthias Ringwald static void mod_sqrt_secp224r1_rm(uECC_word_t *d2,
1856*af03003cSMatthias Ringwald                                   uECC_word_t *e2,
1857*af03003cSMatthias Ringwald                                   uECC_word_t *f2,
1858*af03003cSMatthias Ringwald                                   const uECC_word_t *c,
1859*af03003cSMatthias Ringwald                                   const uECC_word_t *d0,
1860*af03003cSMatthias Ringwald                                   const uECC_word_t *e0,
1861*af03003cSMatthias Ringwald                                   const uECC_word_t *d1,
1862*af03003cSMatthias Ringwald                                   const uECC_word_t *e1) {
1863*af03003cSMatthias Ringwald     uECC_word_t t1[uECC_WORDS];
1864*af03003cSMatthias Ringwald     uECC_word_t t2[uECC_WORDS];
1865*af03003cSMatthias Ringwald 
1866*af03003cSMatthias Ringwald     vli_modMult_fast(t1, e0, e1);              /* t1 <-- e0 * e1 */
1867*af03003cSMatthias Ringwald     vli_modMult_fast(t1, t1, c);               /* t1 <-- t1 * c */
1868*af03003cSMatthias Ringwald     vli_modSub_fast(t1, curve_p, t1);          /* t1 <-- p  - t1 */
1869*af03003cSMatthias Ringwald     vli_modMult_fast(t2, d0, d1);              /* t2 <-- d0 * d1 */
1870*af03003cSMatthias Ringwald     vli_modAdd(t2, t2, t1, curve_p);           /* t2 <-- t2 + t1 */
1871*af03003cSMatthias Ringwald     vli_modMult_fast(t1, d0, e1);              /* t1 <-- d0 * e1 */
1872*af03003cSMatthias Ringwald     vli_modMult_fast(e2, d1, e0);              /* e2 <-- d1 * e0 */
1873*af03003cSMatthias Ringwald     vli_modAdd(e2, e2, t1, curve_p);           /* e2 <-- e2 + t1 */
1874*af03003cSMatthias Ringwald     vli_modSquare_fast(f2, e2);                /* f2 <-- e2^2 */
1875*af03003cSMatthias Ringwald     vli_modMult_fast(f2, f2, c);               /* f2 <-- f2 * c */
1876*af03003cSMatthias Ringwald     vli_modSub_fast(f2, curve_p, f2);          /* f2 <-- p  - f2 */
1877*af03003cSMatthias Ringwald     vli_set(d2, t2);                           /* d2 <-- t2 */
1878*af03003cSMatthias Ringwald }
1879*af03003cSMatthias Ringwald 
1880*af03003cSMatthias Ringwald /* Routine 3.2.7 RP;  from http://www.nsa.gov/ia/_files/nist-routines.pdf */
1881*af03003cSMatthias Ringwald static void mod_sqrt_secp224r1_rp(uECC_word_t *d1,
1882*af03003cSMatthias Ringwald                                   uECC_word_t *e1,
1883*af03003cSMatthias Ringwald                                   uECC_word_t *f1,
1884*af03003cSMatthias Ringwald                                   const uECC_word_t *c,
1885*af03003cSMatthias Ringwald                                   const uECC_word_t *r) {
1886*af03003cSMatthias Ringwald     wordcount_t i;
1887*af03003cSMatthias Ringwald     wordcount_t pow2i = 1;
1888*af03003cSMatthias Ringwald     uECC_word_t d0[uECC_WORDS];
1889*af03003cSMatthias Ringwald     uECC_word_t e0[uECC_WORDS] = {1};          /* e0 <-- 1 */
1890*af03003cSMatthias Ringwald     uECC_word_t f0[uECC_WORDS];
1891*af03003cSMatthias Ringwald 
1892*af03003cSMatthias Ringwald     vli_set(d0, r);                            /* d0 <-- r */
1893*af03003cSMatthias Ringwald     vli_modSub_fast(f0, curve_p, c);           /* f0 <-- p  - c */
1894*af03003cSMatthias Ringwald     for (i = 0; i <= 6; i++) {
1895*af03003cSMatthias Ringwald         mod_sqrt_secp224r1_rss(d1, e1, f1, d0, e0, f0, pow2i); /* RSS (d1,e1,f1,d0,e0,f0,2^i) */
1896*af03003cSMatthias Ringwald         mod_sqrt_secp224r1_rm(d1, e1, f1, c, d1, e1, d0, e0);  /* RM (d1,e1,f1,c,d1,e1,d0,e0) */
1897*af03003cSMatthias Ringwald         vli_set(d0, d1);                       /* d0 <-- d1 */
1898*af03003cSMatthias Ringwald         vli_set(e0, e1);                       /* e0 <-- e1 */
1899*af03003cSMatthias Ringwald         vli_set(f0, f1);                       /* f0 <-- f1 */
1900*af03003cSMatthias Ringwald         pow2i *= 2;
1901*af03003cSMatthias Ringwald     }
1902*af03003cSMatthias Ringwald }
1903*af03003cSMatthias Ringwald 
1904*af03003cSMatthias Ringwald /* Compute a = sqrt(a) (mod curve_p). */
1905*af03003cSMatthias Ringwald /* Routine 3.2.8 mp_mod_sqrt_224; from http://www.nsa.gov/ia/_files/nist-routines.pdf */
1906*af03003cSMatthias Ringwald static void mod_sqrt(uECC_word_t *a) {
1907*af03003cSMatthias Ringwald     bitcount_t i;
1908*af03003cSMatthias Ringwald     uECC_word_t e1[uECC_WORDS];
1909*af03003cSMatthias Ringwald     uECC_word_t f1[uECC_WORDS];
1910*af03003cSMatthias Ringwald     uECC_word_t d0[uECC_WORDS];
1911*af03003cSMatthias Ringwald     uECC_word_t e0[uECC_WORDS];
1912*af03003cSMatthias Ringwald     uECC_word_t f0[uECC_WORDS];
1913*af03003cSMatthias Ringwald     uECC_word_t d1[uECC_WORDS];
1914*af03003cSMatthias Ringwald 
1915*af03003cSMatthias Ringwald     // s = a; using constant instead of random value
1916*af03003cSMatthias Ringwald     mod_sqrt_secp224r1_rp(d0, e0, f0, a, a);           /* RP (d0, e0, f0, c, s) */
1917*af03003cSMatthias Ringwald     mod_sqrt_secp224r1_rs(d1, e1, f1, d0, e0, f0);     /* RS (d1, e1, f1, d0, e0, f0) */
1918*af03003cSMatthias Ringwald     for (i = 1; i <= 95; i++) {
1919*af03003cSMatthias Ringwald         vli_set(d0, d1);                               /* d0 <-- d1 */
1920*af03003cSMatthias Ringwald         vli_set(e0, e1);                               /* e0 <-- e1 */
1921*af03003cSMatthias Ringwald         vli_set(f0, f1);                               /* f0 <-- f1 */
1922*af03003cSMatthias Ringwald         mod_sqrt_secp224r1_rs(d1, e1, f1, d0, e0, f0); /* RS (d1, e1, f1, d0, e0, f0) */
1923*af03003cSMatthias Ringwald         if (vli_isZero(d1)) {                          /* if d1 == 0 */
1924*af03003cSMatthias Ringwald 	        break;
1925*af03003cSMatthias Ringwald         }
1926*af03003cSMatthias Ringwald     }
1927*af03003cSMatthias Ringwald     vli_modInv(f1, e0, curve_p);                       /* f1 <-- 1 / e0 */
1928*af03003cSMatthias Ringwald     vli_modMult_fast(a, d0, f1);                       /* a  <-- d0 / e0 */
1929*af03003cSMatthias Ringwald }
1930*af03003cSMatthias Ringwald 
1931*af03003cSMatthias Ringwald #else /* uECC_CURVE */
1932*af03003cSMatthias Ringwald 
1933*af03003cSMatthias Ringwald /* Compute a = sqrt(a) (mod curve_p). */
1934*af03003cSMatthias Ringwald static void mod_sqrt(uECC_word_t *a) {
1935*af03003cSMatthias Ringwald     bitcount_t i;
1936*af03003cSMatthias Ringwald     uECC_word_t p1[uECC_WORDS] = {1};
1937*af03003cSMatthias Ringwald     uECC_word_t l_result[uECC_WORDS] = {1};
1938*af03003cSMatthias Ringwald 
1939*af03003cSMatthias Ringwald     /* Since curve_p == 3 (mod 4) for all supported curves, we can
1940*af03003cSMatthias Ringwald        compute sqrt(a) = a^((curve_p + 1) / 4) (mod curve_p). */
1941*af03003cSMatthias Ringwald     vli_add(p1, curve_p, p1); /* p1 = curve_p + 1 */
1942*af03003cSMatthias Ringwald     for (i = vli_numBits(p1, uECC_WORDS) - 1; i > 1; --i) {
1943*af03003cSMatthias Ringwald         vli_modSquare_fast(l_result, l_result);
1944*af03003cSMatthias Ringwald         if (vli_testBit(p1, i)) {
1945*af03003cSMatthias Ringwald             vli_modMult_fast(l_result, l_result, a);
1946*af03003cSMatthias Ringwald         }
1947*af03003cSMatthias Ringwald     }
1948*af03003cSMatthias Ringwald     vli_set(a, l_result);
1949*af03003cSMatthias Ringwald }
1950*af03003cSMatthias Ringwald #endif /* uECC_CURVE */
1951*af03003cSMatthias Ringwald 
1952*af03003cSMatthias Ringwald #if uECC_WORD_SIZE == 1
1953*af03003cSMatthias Ringwald 
1954*af03003cSMatthias Ringwald static void vli_nativeToBytes(uint8_t * RESTRICT dest, const uint8_t * RESTRICT src) {
1955*af03003cSMatthias Ringwald     uint8_t i;
1956*af03003cSMatthias Ringwald     for (i = 0; i < uECC_BYTES; ++i) {
1957*af03003cSMatthias Ringwald         dest[i] = src[(uECC_BYTES - 1) - i];
1958*af03003cSMatthias Ringwald     }
1959*af03003cSMatthias Ringwald }
1960*af03003cSMatthias Ringwald 
1961*af03003cSMatthias Ringwald #define vli_bytesToNative(dest, src) vli_nativeToBytes((dest), (src))
1962*af03003cSMatthias Ringwald 
1963*af03003cSMatthias Ringwald #elif uECC_WORD_SIZE == 4
1964*af03003cSMatthias Ringwald 
1965*af03003cSMatthias Ringwald static void vli_nativeToBytes(uint8_t *bytes, const uint32_t *native) {
1966*af03003cSMatthias Ringwald     unsigned i;
1967*af03003cSMatthias Ringwald     for (i = 0; i < uECC_WORDS; ++i) {
1968*af03003cSMatthias Ringwald         uint8_t *digit = bytes + 4 * (uECC_WORDS - 1 - i);
1969*af03003cSMatthias Ringwald         digit[0] = native[i] >> 24;
1970*af03003cSMatthias Ringwald         digit[1] = native[i] >> 16;
1971*af03003cSMatthias Ringwald         digit[2] = native[i] >> 8;
1972*af03003cSMatthias Ringwald         digit[3] = native[i];
1973*af03003cSMatthias Ringwald     }
1974*af03003cSMatthias Ringwald }
1975*af03003cSMatthias Ringwald 
1976*af03003cSMatthias Ringwald static void vli_bytesToNative(uint32_t *native, const uint8_t *bytes) {
1977*af03003cSMatthias Ringwald     unsigned i;
1978*af03003cSMatthias Ringwald     for (i = 0; i < uECC_WORDS; ++i) {
1979*af03003cSMatthias Ringwald         const uint8_t *digit = bytes + 4 * (uECC_WORDS - 1 - i);
1980*af03003cSMatthias Ringwald         native[i] = ((uint32_t)digit[0] << 24) | ((uint32_t)digit[1] << 16) |
1981*af03003cSMatthias Ringwald                     ((uint32_t)digit[2] << 8) | (uint32_t)digit[3];
1982*af03003cSMatthias Ringwald     }
1983*af03003cSMatthias Ringwald }
1984*af03003cSMatthias Ringwald 
1985*af03003cSMatthias Ringwald #else
1986*af03003cSMatthias Ringwald 
1987*af03003cSMatthias Ringwald static void vli_nativeToBytes(uint8_t *bytes, const uint64_t *native) {
1988*af03003cSMatthias Ringwald     unsigned i;
1989*af03003cSMatthias Ringwald     for (i = 0; i < uECC_WORDS; ++i) {
1990*af03003cSMatthias Ringwald         uint8_t *digit = bytes + 8 * (uECC_WORDS - 1 - i);
1991*af03003cSMatthias Ringwald         digit[0] = native[i] >> 56;
1992*af03003cSMatthias Ringwald         digit[1] = native[i] >> 48;
1993*af03003cSMatthias Ringwald         digit[2] = native[i] >> 40;
1994*af03003cSMatthias Ringwald         digit[3] = native[i] >> 32;
1995*af03003cSMatthias Ringwald         digit[4] = native[i] >> 24;
1996*af03003cSMatthias Ringwald         digit[5] = native[i] >> 16;
1997*af03003cSMatthias Ringwald         digit[6] = native[i] >> 8;
1998*af03003cSMatthias Ringwald         digit[7] = native[i];
1999*af03003cSMatthias Ringwald     }
2000*af03003cSMatthias Ringwald }
2001*af03003cSMatthias Ringwald 
2002*af03003cSMatthias Ringwald static void vli_bytesToNative(uint64_t *native, const uint8_t *bytes) {
2003*af03003cSMatthias Ringwald     unsigned i;
2004*af03003cSMatthias Ringwald     for (i = 0; i < uECC_WORDS; ++i) {
2005*af03003cSMatthias Ringwald         const uint8_t *digit = bytes + 8 * (uECC_WORDS - 1 - i);
2006*af03003cSMatthias Ringwald         native[i] = ((uint64_t)digit[0] << 56) | ((uint64_t)digit[1] << 48) |
2007*af03003cSMatthias Ringwald                     ((uint64_t)digit[2] << 40) | ((uint64_t)digit[3] << 32) |
2008*af03003cSMatthias Ringwald                     ((uint64_t)digit[4] << 24) | ((uint64_t)digit[5] << 16) |
2009*af03003cSMatthias Ringwald                     ((uint64_t)digit[6] << 8) | (uint64_t)digit[7];
2010*af03003cSMatthias Ringwald     }
2011*af03003cSMatthias Ringwald }
2012*af03003cSMatthias Ringwald 
2013*af03003cSMatthias Ringwald #endif /* uECC_WORD_SIZE */
2014*af03003cSMatthias Ringwald 
2015*af03003cSMatthias Ringwald int uECC_make_key(uint8_t public_key[uECC_BYTES*2], uint8_t private_key[uECC_BYTES]) {
2016*af03003cSMatthias Ringwald     uECC_word_t private[uECC_WORDS];
2017*af03003cSMatthias Ringwald     EccPoint public;
2018*af03003cSMatthias Ringwald     uECC_word_t tries;
2019*af03003cSMatthias Ringwald     for (tries = 0; tries < MAX_TRIES; ++tries) {
2020*af03003cSMatthias Ringwald         if (g_rng_function((uint8_t *)private, sizeof(private)) &&
2021*af03003cSMatthias Ringwald                 EccPoint_compute_public_key(&public, private)) {
2022*af03003cSMatthias Ringwald             vli_nativeToBytes(private_key, private);
2023*af03003cSMatthias Ringwald             vli_nativeToBytes(public_key, public.x);
2024*af03003cSMatthias Ringwald             vli_nativeToBytes(public_key + uECC_BYTES, public.y);
2025*af03003cSMatthias Ringwald             return 1;
2026*af03003cSMatthias Ringwald         }
2027*af03003cSMatthias Ringwald     }
2028*af03003cSMatthias Ringwald     return 0;
2029*af03003cSMatthias Ringwald }
2030*af03003cSMatthias Ringwald 
2031*af03003cSMatthias Ringwald int uECC_shared_secret(const uint8_t public_key[uECC_BYTES*2],
2032*af03003cSMatthias Ringwald                        const uint8_t private_key[uECC_BYTES],
2033*af03003cSMatthias Ringwald                        uint8_t secret[uECC_BYTES]) {
2034*af03003cSMatthias Ringwald     EccPoint public;
2035*af03003cSMatthias Ringwald     EccPoint product;
2036*af03003cSMatthias Ringwald     uECC_word_t private[uECC_WORDS];
2037*af03003cSMatthias Ringwald     uECC_word_t tmp[uECC_WORDS];
2038*af03003cSMatthias Ringwald     uECC_word_t *p2[2] = {private, tmp};
2039*af03003cSMatthias Ringwald     uECC_word_t random[uECC_WORDS];
2040*af03003cSMatthias Ringwald     uECC_word_t *initial_Z = 0;
2041*af03003cSMatthias Ringwald     uECC_word_t tries;
2042*af03003cSMatthias Ringwald     uECC_word_t carry;
2043*af03003cSMatthias Ringwald 
2044*af03003cSMatthias Ringwald     // Try to get a random initial Z value to improve protection against side-channel
2045*af03003cSMatthias Ringwald     // attacks. If the RNG fails every time (eg it was not defined), we continue so that
2046*af03003cSMatthias Ringwald     // uECC_shared_secret() can still work without an RNG defined.
2047*af03003cSMatthias Ringwald     for (tries = 0; tries < MAX_TRIES; ++tries) {
2048*af03003cSMatthias Ringwald         if (g_rng_function((uint8_t *)random, sizeof(random)) && !vli_isZero(random)) {
2049*af03003cSMatthias Ringwald             initial_Z = random;
2050*af03003cSMatthias Ringwald             break;
2051*af03003cSMatthias Ringwald         }
2052*af03003cSMatthias Ringwald     }
2053*af03003cSMatthias Ringwald 
2054*af03003cSMatthias Ringwald     vli_bytesToNative(private, private_key);
2055*af03003cSMatthias Ringwald     vli_bytesToNative(public.x, public_key);
2056*af03003cSMatthias Ringwald     vli_bytesToNative(public.y, public_key + uECC_BYTES);
2057*af03003cSMatthias Ringwald 
2058*af03003cSMatthias Ringwald #if (uECC_CURVE == uECC_secp160r1)
2059*af03003cSMatthias Ringwald     // Don't regularize the bitcount for secp160r1.
2060*af03003cSMatthias Ringwald     EccPoint_mult(&product, &public, private, initial_Z, vli_numBits(private, uECC_WORDS));
2061*af03003cSMatthias Ringwald #else
2062*af03003cSMatthias Ringwald     // Regularize the bitcount for the private key so that attackers cannot use a side channel
2063*af03003cSMatthias Ringwald     // attack to learn the number of leading zeros.
2064*af03003cSMatthias Ringwald     carry = vli_add(private, private, curve_n);
2065*af03003cSMatthias Ringwald     vli_add(tmp, private, curve_n);
2066*af03003cSMatthias Ringwald     EccPoint_mult(&product, &public, p2[!carry], initial_Z, (uECC_BYTES * 8) + 1);
2067*af03003cSMatthias Ringwald #endif
2068*af03003cSMatthias Ringwald 
2069*af03003cSMatthias Ringwald     vli_nativeToBytes(secret, product.x);
2070*af03003cSMatthias Ringwald     return !EccPoint_isZero(&product);
2071*af03003cSMatthias Ringwald }
2072*af03003cSMatthias Ringwald 
2073*af03003cSMatthias Ringwald void uECC_compress(const uint8_t public_key[uECC_BYTES*2], uint8_t compressed[uECC_BYTES+1]) {
2074*af03003cSMatthias Ringwald     wordcount_t i;
2075*af03003cSMatthias Ringwald     for (i = 0; i < uECC_BYTES; ++i) {
2076*af03003cSMatthias Ringwald         compressed[i+1] = public_key[i];
2077*af03003cSMatthias Ringwald     }
2078*af03003cSMatthias Ringwald     compressed[0] = 2 + (public_key[uECC_BYTES * 2 - 1] & 0x01);
2079*af03003cSMatthias Ringwald }
2080*af03003cSMatthias Ringwald 
2081*af03003cSMatthias Ringwald /* Computes result = x^3 + ax + b. result must not overlap x. */
2082*af03003cSMatthias Ringwald static void curve_x_side(uECC_word_t * RESTRICT result, const uECC_word_t * RESTRICT x) {
2083*af03003cSMatthias Ringwald #if (uECC_CURVE == uECC_secp256k1)
2084*af03003cSMatthias Ringwald     vli_modSquare_fast(result, x); /* r = x^2 */
2085*af03003cSMatthias Ringwald     vli_modMult_fast(result, result, x); /* r = x^3 */
2086*af03003cSMatthias Ringwald     vli_modAdd(result, result, curve_b, curve_p); /* r = x^3 + b */
2087*af03003cSMatthias Ringwald #else
2088*af03003cSMatthias Ringwald     uECC_word_t _3[uECC_WORDS] = {3}; /* -a = 3 */
2089*af03003cSMatthias Ringwald 
2090*af03003cSMatthias Ringwald     vli_modSquare_fast(result, x); /* r = x^2 */
2091*af03003cSMatthias Ringwald     vli_modSub_fast(result, result, _3); /* r = x^2 - 3 */
2092*af03003cSMatthias Ringwald     vli_modMult_fast(result, result, x); /* r = x^3 - 3x */
2093*af03003cSMatthias Ringwald     vli_modAdd(result, result, curve_b, curve_p); /* r = x^3 - 3x + b */
2094*af03003cSMatthias Ringwald #endif
2095*af03003cSMatthias Ringwald }
2096*af03003cSMatthias Ringwald 
2097*af03003cSMatthias Ringwald void uECC_decompress(const uint8_t compressed[uECC_BYTES+1], uint8_t public_key[uECC_BYTES*2]) {
2098*af03003cSMatthias Ringwald     EccPoint point;
2099*af03003cSMatthias Ringwald     vli_bytesToNative(point.x, compressed + 1);
2100*af03003cSMatthias Ringwald     curve_x_side(point.y, point.x);
2101*af03003cSMatthias Ringwald     mod_sqrt(point.y);
2102*af03003cSMatthias Ringwald 
2103*af03003cSMatthias Ringwald     if ((point.y[0] & 0x01) != (compressed[0] & 0x01)) {
2104*af03003cSMatthias Ringwald         vli_sub(point.y, curve_p, point.y);
2105*af03003cSMatthias Ringwald     }
2106*af03003cSMatthias Ringwald 
2107*af03003cSMatthias Ringwald     vli_nativeToBytes(public_key, point.x);
2108*af03003cSMatthias Ringwald     vli_nativeToBytes(public_key + uECC_BYTES, point.y);
2109*af03003cSMatthias Ringwald }
2110*af03003cSMatthias Ringwald 
2111*af03003cSMatthias Ringwald int uECC_valid_public_key(const uint8_t public_key[uECC_BYTES*2]) {
2112*af03003cSMatthias Ringwald     uECC_word_t tmp1[uECC_WORDS];
2113*af03003cSMatthias Ringwald     uECC_word_t tmp2[uECC_WORDS];
2114*af03003cSMatthias Ringwald     EccPoint public;
2115*af03003cSMatthias Ringwald 
2116*af03003cSMatthias Ringwald     vli_bytesToNative(public.x, public_key);
2117*af03003cSMatthias Ringwald     vli_bytesToNative(public.y, public_key + uECC_BYTES);
2118*af03003cSMatthias Ringwald 
2119*af03003cSMatthias Ringwald     // The point at infinity is invalid.
2120*af03003cSMatthias Ringwald     if (EccPoint_isZero(&public)) {
2121*af03003cSMatthias Ringwald         return 0;
2122*af03003cSMatthias Ringwald     }
2123*af03003cSMatthias Ringwald 
2124*af03003cSMatthias Ringwald     // x and y must be smaller than p.
2125*af03003cSMatthias Ringwald     if (vli_cmp(curve_p, public.x) != 1 || vli_cmp(curve_p, public.y) != 1) {
2126*af03003cSMatthias Ringwald         return 0;
2127*af03003cSMatthias Ringwald     }
2128*af03003cSMatthias Ringwald 
2129*af03003cSMatthias Ringwald     vli_modSquare_fast(tmp1, public.y); /* tmp1 = y^2 */
2130*af03003cSMatthias Ringwald     curve_x_side(tmp2, public.x); /* tmp2 = x^3 + ax + b */
2131*af03003cSMatthias Ringwald 
2132*af03003cSMatthias Ringwald     /* Make sure that y^2 == x^3 + ax + b */
2133*af03003cSMatthias Ringwald     return (vli_cmp(tmp1, tmp2) == 0);
2134*af03003cSMatthias Ringwald }
2135*af03003cSMatthias Ringwald 
2136*af03003cSMatthias Ringwald int uECC_compute_public_key(const uint8_t private_key[uECC_BYTES],
2137*af03003cSMatthias Ringwald                             uint8_t public_key[uECC_BYTES * 2]) {
2138*af03003cSMatthias Ringwald     uECC_word_t private[uECC_WORDS];
2139*af03003cSMatthias Ringwald     EccPoint public;
2140*af03003cSMatthias Ringwald 
2141*af03003cSMatthias Ringwald     vli_bytesToNative(private, private_key);
2142*af03003cSMatthias Ringwald 
2143*af03003cSMatthias Ringwald     if (!EccPoint_compute_public_key(&public, private)) {
2144*af03003cSMatthias Ringwald         return 0;
2145*af03003cSMatthias Ringwald     }
2146*af03003cSMatthias Ringwald 
2147*af03003cSMatthias Ringwald     vli_nativeToBytes(public_key, public.x);
2148*af03003cSMatthias Ringwald     vli_nativeToBytes(public_key + uECC_BYTES, public.y);
2149*af03003cSMatthias Ringwald     return 1;
2150*af03003cSMatthias Ringwald }
2151*af03003cSMatthias Ringwald 
2152*af03003cSMatthias Ringwald int uECC_bytes(void) {
2153*af03003cSMatthias Ringwald     return uECC_BYTES;
2154*af03003cSMatthias Ringwald }
2155*af03003cSMatthias Ringwald 
2156*af03003cSMatthias Ringwald int uECC_curve(void) {
2157*af03003cSMatthias Ringwald     return uECC_CURVE;
2158*af03003cSMatthias Ringwald }
2159*af03003cSMatthias Ringwald 
2160*af03003cSMatthias Ringwald /* -------- ECDSA code -------- */
2161*af03003cSMatthias Ringwald 
2162*af03003cSMatthias Ringwald #if (uECC_CURVE == uECC_secp160r1)
2163*af03003cSMatthias Ringwald static void vli_clear_n(uECC_word_t *vli) {
2164*af03003cSMatthias Ringwald     vli_clear(vli);
2165*af03003cSMatthias Ringwald     vli[uECC_N_WORDS - 1] = 0;
2166*af03003cSMatthias Ringwald }
2167*af03003cSMatthias Ringwald 
2168*af03003cSMatthias Ringwald static uECC_word_t vli_isZero_n(const uECC_word_t *vli) {
2169*af03003cSMatthias Ringwald     if (vli[uECC_N_WORDS - 1]) {
2170*af03003cSMatthias Ringwald         return 0;
2171*af03003cSMatthias Ringwald     }
2172*af03003cSMatthias Ringwald     return vli_isZero(vli);
2173*af03003cSMatthias Ringwald }
2174*af03003cSMatthias Ringwald 
2175*af03003cSMatthias Ringwald static void vli_set_n(uECC_word_t *dest, const uECC_word_t *src) {
2176*af03003cSMatthias Ringwald     vli_set(dest, src);
2177*af03003cSMatthias Ringwald     dest[uECC_N_WORDS - 1] = src[uECC_N_WORDS - 1];
2178*af03003cSMatthias Ringwald }
2179*af03003cSMatthias Ringwald 
2180*af03003cSMatthias Ringwald static cmpresult_t vli_cmp_n(const uECC_word_t *left, const uECC_word_t *right) {
2181*af03003cSMatthias Ringwald     if (left[uECC_N_WORDS - 1] > right[uECC_N_WORDS - 1]) {
2182*af03003cSMatthias Ringwald         return 1;
2183*af03003cSMatthias Ringwald     } else if (left[uECC_N_WORDS - 1] < right[uECC_N_WORDS - 1]) {
2184*af03003cSMatthias Ringwald         return -1;
2185*af03003cSMatthias Ringwald     }
2186*af03003cSMatthias Ringwald     return vli_cmp(left, right);
2187*af03003cSMatthias Ringwald }
2188*af03003cSMatthias Ringwald 
2189*af03003cSMatthias Ringwald static void vli_rshift1_n(uECC_word_t *vli) {
2190*af03003cSMatthias Ringwald     vli_rshift1(vli);
2191*af03003cSMatthias Ringwald     vli[uECC_N_WORDS - 2] |= vli[uECC_N_WORDS - 1] << (uECC_WORD_BITS - 1);
2192*af03003cSMatthias Ringwald     vli[uECC_N_WORDS - 1] = vli[uECC_N_WORDS - 1] >> 1;
2193*af03003cSMatthias Ringwald }
2194*af03003cSMatthias Ringwald 
2195*af03003cSMatthias Ringwald static uECC_word_t vli_add_n(uECC_word_t *result,
2196*af03003cSMatthias Ringwald                              const uECC_word_t *left,
2197*af03003cSMatthias Ringwald                              const uECC_word_t *right) {
2198*af03003cSMatthias Ringwald     uECC_word_t carry = vli_add(result, left, right);
2199*af03003cSMatthias Ringwald     uECC_word_t sum = left[uECC_N_WORDS - 1] + right[uECC_N_WORDS - 1] + carry;
2200*af03003cSMatthias Ringwald     if (sum != left[uECC_N_WORDS - 1]) {
2201*af03003cSMatthias Ringwald         carry = (sum < left[uECC_N_WORDS - 1]);
2202*af03003cSMatthias Ringwald     }
2203*af03003cSMatthias Ringwald     result[uECC_N_WORDS - 1] = sum;
2204*af03003cSMatthias Ringwald     return carry;
2205*af03003cSMatthias Ringwald }
2206*af03003cSMatthias Ringwald 
2207*af03003cSMatthias Ringwald static uECC_word_t vli_sub_n(uECC_word_t *result,
2208*af03003cSMatthias Ringwald                              const uECC_word_t *left,
2209*af03003cSMatthias Ringwald                              const uECC_word_t *right) {
2210*af03003cSMatthias Ringwald     uECC_word_t borrow = vli_sub(result, left, right);
2211*af03003cSMatthias Ringwald     uECC_word_t diff = left[uECC_N_WORDS - 1] - right[uECC_N_WORDS - 1] - borrow;
2212*af03003cSMatthias Ringwald     if (diff != left[uECC_N_WORDS - 1]) {
2213*af03003cSMatthias Ringwald         borrow = (diff > left[uECC_N_WORDS - 1]);
2214*af03003cSMatthias Ringwald     }
2215*af03003cSMatthias Ringwald     result[uECC_N_WORDS - 1] = diff;
2216*af03003cSMatthias Ringwald     return borrow;
2217*af03003cSMatthias Ringwald }
2218*af03003cSMatthias Ringwald 
2219*af03003cSMatthias Ringwald #if !muladd_exists
2220*af03003cSMatthias Ringwald static void muladd(uECC_word_t a,
2221*af03003cSMatthias Ringwald                    uECC_word_t b,
2222*af03003cSMatthias Ringwald                    uECC_word_t *r0,
2223*af03003cSMatthias Ringwald                    uECC_word_t *r1,
2224*af03003cSMatthias Ringwald                    uECC_word_t *r2) {
2225*af03003cSMatthias Ringwald     uECC_dword_t p = (uECC_dword_t)a * b;
2226*af03003cSMatthias Ringwald     uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0;
2227*af03003cSMatthias Ringwald     r01 += p;
2228*af03003cSMatthias Ringwald     *r2 += (r01 < p);
2229*af03003cSMatthias Ringwald     *r1 = r01 >> uECC_WORD_BITS;
2230*af03003cSMatthias Ringwald     *r0 = (uECC_word_t)r01;
2231*af03003cSMatthias Ringwald }
2232*af03003cSMatthias Ringwald #define muladd_exists 1
2233*af03003cSMatthias Ringwald #endif
2234*af03003cSMatthias Ringwald 
2235*af03003cSMatthias Ringwald static void vli_mult_n(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) {
2236*af03003cSMatthias Ringwald     uECC_word_t r0 = 0;
2237*af03003cSMatthias Ringwald     uECC_word_t r1 = 0;
2238*af03003cSMatthias Ringwald     uECC_word_t r2 = 0;
2239*af03003cSMatthias Ringwald     wordcount_t i, k;
2240*af03003cSMatthias Ringwald 
2241*af03003cSMatthias Ringwald     for (k = 0; k < uECC_N_WORDS * 2 - 1; ++k) {
2242*af03003cSMatthias Ringwald         wordcount_t min = (k < uECC_N_WORDS ? 0 : (k + 1) - uECC_N_WORDS);
2243*af03003cSMatthias Ringwald         wordcount_t max = (k < uECC_N_WORDS ? k : uECC_N_WORDS - 1);
2244*af03003cSMatthias Ringwald         for (i = min; i <= max; ++i) {
2245*af03003cSMatthias Ringwald             muladd(left[i], right[k - i], &r0, &r1, &r2);
2246*af03003cSMatthias Ringwald         }
2247*af03003cSMatthias Ringwald         result[k] = r0;
2248*af03003cSMatthias Ringwald         r0 = r1;
2249*af03003cSMatthias Ringwald         r1 = r2;
2250*af03003cSMatthias Ringwald         r2 = 0;
2251*af03003cSMatthias Ringwald     }
2252*af03003cSMatthias Ringwald     result[uECC_N_WORDS * 2 - 1] = r0;
2253*af03003cSMatthias Ringwald }
2254*af03003cSMatthias Ringwald 
2255*af03003cSMatthias Ringwald static void vli_modAdd_n(uECC_word_t *result,
2256*af03003cSMatthias Ringwald                          const uECC_word_t *left,
2257*af03003cSMatthias Ringwald                          const uECC_word_t *right,
2258*af03003cSMatthias Ringwald                          const uECC_word_t *mod) {
2259*af03003cSMatthias Ringwald     uECC_word_t carry = vli_add_n(result, left, right);
2260*af03003cSMatthias Ringwald     if (carry || vli_cmp_n(result, mod) >= 0) {
2261*af03003cSMatthias Ringwald         vli_sub_n(result, result, mod);
2262*af03003cSMatthias Ringwald     }
2263*af03003cSMatthias Ringwald }
2264*af03003cSMatthias Ringwald 
2265*af03003cSMatthias Ringwald static void vli_modInv_n(uECC_word_t *result, const uECC_word_t *input, const uECC_word_t *mod) {
2266*af03003cSMatthias Ringwald     uECC_word_t a[uECC_N_WORDS], b[uECC_N_WORDS], u[uECC_N_WORDS], v[uECC_N_WORDS];
2267*af03003cSMatthias Ringwald     uECC_word_t carry;
2268*af03003cSMatthias Ringwald     cmpresult_t cmpResult;
2269*af03003cSMatthias Ringwald 
2270*af03003cSMatthias Ringwald     if (vli_isZero_n(input)) {
2271*af03003cSMatthias Ringwald         vli_clear_n(result);
2272*af03003cSMatthias Ringwald         return;
2273*af03003cSMatthias Ringwald     }
2274*af03003cSMatthias Ringwald 
2275*af03003cSMatthias Ringwald     vli_set_n(a, input);
2276*af03003cSMatthias Ringwald     vli_set_n(b, mod);
2277*af03003cSMatthias Ringwald     vli_clear_n(u);
2278*af03003cSMatthias Ringwald     u[0] = 1;
2279*af03003cSMatthias Ringwald     vli_clear_n(v);
2280*af03003cSMatthias Ringwald     while ((cmpResult = vli_cmp_n(a, b)) != 0) {
2281*af03003cSMatthias Ringwald         carry = 0;
2282*af03003cSMatthias Ringwald         if (EVEN(a)) {
2283*af03003cSMatthias Ringwald             vli_rshift1_n(a);
2284*af03003cSMatthias Ringwald             if (!EVEN(u)) {
2285*af03003cSMatthias Ringwald                 carry = vli_add_n(u, u, mod);
2286*af03003cSMatthias Ringwald             }
2287*af03003cSMatthias Ringwald             vli_rshift1_n(u);
2288*af03003cSMatthias Ringwald             if (carry) {
2289*af03003cSMatthias Ringwald                 u[uECC_N_WORDS - 1] |= HIGH_BIT_SET;
2290*af03003cSMatthias Ringwald             }
2291*af03003cSMatthias Ringwald         } else if (EVEN(b)) {
2292*af03003cSMatthias Ringwald             vli_rshift1_n(b);
2293*af03003cSMatthias Ringwald             if (!EVEN(v)) {
2294*af03003cSMatthias Ringwald                 carry = vli_add_n(v, v, mod);
2295*af03003cSMatthias Ringwald             }
2296*af03003cSMatthias Ringwald             vli_rshift1_n(v);
2297*af03003cSMatthias Ringwald             if (carry) {
2298*af03003cSMatthias Ringwald                 v[uECC_N_WORDS - 1] |= HIGH_BIT_SET;
2299*af03003cSMatthias Ringwald             }
2300*af03003cSMatthias Ringwald         } else if (cmpResult > 0) {
2301*af03003cSMatthias Ringwald             vli_sub_n(a, a, b);
2302*af03003cSMatthias Ringwald             vli_rshift1_n(a);
2303*af03003cSMatthias Ringwald             if (vli_cmp_n(u, v) < 0) {
2304*af03003cSMatthias Ringwald                 vli_add_n(u, u, mod);
2305*af03003cSMatthias Ringwald             }
2306*af03003cSMatthias Ringwald             vli_sub_n(u, u, v);
2307*af03003cSMatthias Ringwald             if (!EVEN(u)) {
2308*af03003cSMatthias Ringwald                 carry = vli_add_n(u, u, mod);
2309*af03003cSMatthias Ringwald             }
2310*af03003cSMatthias Ringwald             vli_rshift1_n(u);
2311*af03003cSMatthias Ringwald             if (carry) {
2312*af03003cSMatthias Ringwald                 u[uECC_N_WORDS - 1] |= HIGH_BIT_SET;
2313*af03003cSMatthias Ringwald             }
2314*af03003cSMatthias Ringwald         } else {
2315*af03003cSMatthias Ringwald             vli_sub_n(b, b, a);
2316*af03003cSMatthias Ringwald             vli_rshift1_n(b);
2317*af03003cSMatthias Ringwald             if (vli_cmp_n(v, u) < 0) {
2318*af03003cSMatthias Ringwald                 vli_add_n(v, v, mod);
2319*af03003cSMatthias Ringwald             }
2320*af03003cSMatthias Ringwald             vli_sub_n(v, v, u);
2321*af03003cSMatthias Ringwald             if (!EVEN(v)) {
2322*af03003cSMatthias Ringwald                 carry = vli_add_n(v, v, mod);
2323*af03003cSMatthias Ringwald             }
2324*af03003cSMatthias Ringwald             vli_rshift1_n(v);
2325*af03003cSMatthias Ringwald             if (carry) {
2326*af03003cSMatthias Ringwald                 v[uECC_N_WORDS - 1] |= HIGH_BIT_SET;
2327*af03003cSMatthias Ringwald             }
2328*af03003cSMatthias Ringwald         }
2329*af03003cSMatthias Ringwald     }
2330*af03003cSMatthias Ringwald     vli_set_n(result, u);
2331*af03003cSMatthias Ringwald }
2332*af03003cSMatthias Ringwald 
2333*af03003cSMatthias Ringwald static void vli2_rshift1_n(uECC_word_t *vli) {
2334*af03003cSMatthias Ringwald     vli_rshift1_n(vli);
2335*af03003cSMatthias Ringwald     vli[uECC_N_WORDS - 1] |= vli[uECC_N_WORDS] << (uECC_WORD_BITS - 1);
2336*af03003cSMatthias Ringwald     vli_rshift1_n(vli + uECC_N_WORDS);
2337*af03003cSMatthias Ringwald }
2338*af03003cSMatthias Ringwald 
2339*af03003cSMatthias Ringwald static uECC_word_t vli2_sub_n(uECC_word_t *result,
2340*af03003cSMatthias Ringwald                               const uECC_word_t *left,
2341*af03003cSMatthias Ringwald                               const uECC_word_t *right) {
2342*af03003cSMatthias Ringwald     uECC_word_t borrow = 0;
2343*af03003cSMatthias Ringwald     wordcount_t i;
2344*af03003cSMatthias Ringwald     for (i = 0; i < uECC_N_WORDS * 2; ++i) {
2345*af03003cSMatthias Ringwald         uECC_word_t diff = left[i] - right[i] - borrow;
2346*af03003cSMatthias Ringwald         if (diff != left[i]) {
2347*af03003cSMatthias Ringwald             borrow = (diff > left[i]);
2348*af03003cSMatthias Ringwald         }
2349*af03003cSMatthias Ringwald         result[i] = diff;
2350*af03003cSMatthias Ringwald     }
2351*af03003cSMatthias Ringwald     return borrow;
2352*af03003cSMatthias Ringwald }
2353*af03003cSMatthias Ringwald 
2354*af03003cSMatthias Ringwald /* Computes result = (left * right) % curve_n. */
2355*af03003cSMatthias Ringwald static void vli_modMult_n(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) {
2356*af03003cSMatthias Ringwald     bitcount_t i;
2357*af03003cSMatthias Ringwald     uECC_word_t product[2 * uECC_N_WORDS];
2358*af03003cSMatthias Ringwald     uECC_word_t modMultiple[2 * uECC_N_WORDS];
2359*af03003cSMatthias Ringwald     uECC_word_t tmp[2 * uECC_N_WORDS];
2360*af03003cSMatthias Ringwald     uECC_word_t *v[2] = {tmp, product};
2361*af03003cSMatthias Ringwald     uECC_word_t index = 1;
2362*af03003cSMatthias Ringwald 
2363*af03003cSMatthias Ringwald     vli_mult_n(product, left, right);
2364*af03003cSMatthias Ringwald     vli_clear_n(modMultiple);
2365*af03003cSMatthias Ringwald     vli_set(modMultiple + uECC_N_WORDS + 1, curve_n);
2366*af03003cSMatthias Ringwald     vli_rshift1(modMultiple + uECC_N_WORDS + 1);
2367*af03003cSMatthias Ringwald     modMultiple[2 * uECC_N_WORDS - 1] |= HIGH_BIT_SET;
2368*af03003cSMatthias Ringwald     modMultiple[uECC_N_WORDS] = HIGH_BIT_SET;
2369*af03003cSMatthias Ringwald 
2370*af03003cSMatthias Ringwald     for (i = 0;
2371*af03003cSMatthias Ringwald          i <= ((((bitcount_t)uECC_N_WORDS) << uECC_WORD_BITS_SHIFT) + (uECC_WORD_BITS - 1));
2372*af03003cSMatthias Ringwald          ++i) {
2373*af03003cSMatthias Ringwald         uECC_word_t borrow = vli2_sub_n(v[1 - index], v[index], modMultiple);
2374*af03003cSMatthias Ringwald         index = !(index ^ borrow); /* Swap the index if there was no borrow */
2375*af03003cSMatthias Ringwald         vli2_rshift1_n(modMultiple);
2376*af03003cSMatthias Ringwald     }
2377*af03003cSMatthias Ringwald     vli_set_n(result, v[index]);
2378*af03003cSMatthias Ringwald }
2379*af03003cSMatthias Ringwald 
2380*af03003cSMatthias Ringwald #else
2381*af03003cSMatthias Ringwald 
2382*af03003cSMatthias Ringwald #define vli_cmp_n vli_cmp
2383*af03003cSMatthias Ringwald #define vli_modInv_n vli_modInv
2384*af03003cSMatthias Ringwald #define vli_modAdd_n vli_modAdd
2385*af03003cSMatthias Ringwald 
2386*af03003cSMatthias Ringwald static void vli2_rshift1(uECC_word_t *vli) {
2387*af03003cSMatthias Ringwald     vli_rshift1(vli);
2388*af03003cSMatthias Ringwald     vli[uECC_WORDS - 1] |= vli[uECC_WORDS] << (uECC_WORD_BITS - 1);
2389*af03003cSMatthias Ringwald     vli_rshift1(vli + uECC_WORDS);
2390*af03003cSMatthias Ringwald }
2391*af03003cSMatthias Ringwald 
2392*af03003cSMatthias Ringwald static uECC_word_t vli2_sub(uECC_word_t *result,
2393*af03003cSMatthias Ringwald                             const uECC_word_t *left,
2394*af03003cSMatthias Ringwald                             const uECC_word_t *right) {
2395*af03003cSMatthias Ringwald     uECC_word_t borrow = 0;
2396*af03003cSMatthias Ringwald     wordcount_t i;
2397*af03003cSMatthias Ringwald     for (i = 0; i < uECC_WORDS * 2; ++i) {
2398*af03003cSMatthias Ringwald         uECC_word_t diff = left[i] - right[i] - borrow;
2399*af03003cSMatthias Ringwald         if (diff != left[i]) {
2400*af03003cSMatthias Ringwald             borrow = (diff > left[i]);
2401*af03003cSMatthias Ringwald         }
2402*af03003cSMatthias Ringwald         result[i] = diff;
2403*af03003cSMatthias Ringwald     }
2404*af03003cSMatthias Ringwald     return borrow;
2405*af03003cSMatthias Ringwald }
2406*af03003cSMatthias Ringwald 
2407*af03003cSMatthias Ringwald /* Computes result = (left * right) % curve_n. */
2408*af03003cSMatthias Ringwald static void vli_modMult_n(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) {
2409*af03003cSMatthias Ringwald     uECC_word_t product[2 * uECC_WORDS];
2410*af03003cSMatthias Ringwald     uECC_word_t modMultiple[2 * uECC_WORDS];
2411*af03003cSMatthias Ringwald     uECC_word_t tmp[2 * uECC_WORDS];
2412*af03003cSMatthias Ringwald     uECC_word_t *v[2] = {tmp, product};
2413*af03003cSMatthias Ringwald     bitcount_t i;
2414*af03003cSMatthias Ringwald     uECC_word_t index = 1;
2415*af03003cSMatthias Ringwald 
2416*af03003cSMatthias Ringwald     vli_mult(product, left, right);
2417*af03003cSMatthias Ringwald     vli_set(modMultiple + uECC_WORDS, curve_n); /* works if curve_n has its highest bit set */
2418*af03003cSMatthias Ringwald     vli_clear(modMultiple);
2419*af03003cSMatthias Ringwald 
2420*af03003cSMatthias Ringwald     for (i = 0; i <= uECC_BYTES * 8; ++i) {
2421*af03003cSMatthias Ringwald         uECC_word_t borrow = vli2_sub(v[1 - index], v[index], modMultiple);
2422*af03003cSMatthias Ringwald         index = !(index ^ borrow); /* Swap the index if there was no borrow */
2423*af03003cSMatthias Ringwald         vli2_rshift1(modMultiple);
2424*af03003cSMatthias Ringwald     }
2425*af03003cSMatthias Ringwald     vli_set(result, v[index]);
2426*af03003cSMatthias Ringwald }
2427*af03003cSMatthias Ringwald #endif /* (uECC_CURVE != uECC_secp160r1) */
2428*af03003cSMatthias Ringwald 
2429*af03003cSMatthias Ringwald static int uECC_sign_with_k(const uint8_t private_key[uECC_BYTES],
2430*af03003cSMatthias Ringwald                             const uint8_t message_hash[uECC_BYTES],
2431*af03003cSMatthias Ringwald                             uECC_word_t k[uECC_N_WORDS],
2432*af03003cSMatthias Ringwald                             uint8_t signature[uECC_BYTES*2]) {
2433*af03003cSMatthias Ringwald     uECC_word_t tmp[uECC_N_WORDS];
2434*af03003cSMatthias Ringwald     uECC_word_t s[uECC_N_WORDS];
2435*af03003cSMatthias Ringwald     uECC_word_t *k2[2] = {tmp, s};
2436*af03003cSMatthias Ringwald     EccPoint p;
2437*af03003cSMatthias Ringwald     uECC_word_t carry;
2438*af03003cSMatthias Ringwald     uECC_word_t tries;
2439*af03003cSMatthias Ringwald 
2440*af03003cSMatthias Ringwald     /* Make sure 0 < k < curve_n */
2441*af03003cSMatthias Ringwald     if (vli_isZero(k) || vli_cmp_n(curve_n, k) != 1) {
2442*af03003cSMatthias Ringwald         return 0;
2443*af03003cSMatthias Ringwald     }
2444*af03003cSMatthias Ringwald 
2445*af03003cSMatthias Ringwald #if (uECC_CURVE == uECC_secp160r1)
2446*af03003cSMatthias Ringwald     /* Make sure that we don't leak timing information about k.
2447*af03003cSMatthias Ringwald        See http://eprint.iacr.org/2011/232.pdf */
2448*af03003cSMatthias Ringwald     vli_add_n(tmp, k, curve_n);
2449*af03003cSMatthias Ringwald     carry = (tmp[uECC_WORDS] & 0x02);
2450*af03003cSMatthias Ringwald     vli_add_n(s, tmp, curve_n);
2451*af03003cSMatthias Ringwald 
2452*af03003cSMatthias Ringwald     /* p = k * G */
2453*af03003cSMatthias Ringwald     EccPoint_mult(&p, &curve_G, k2[!carry], 0, (uECC_BYTES * 8) + 2);
2454*af03003cSMatthias Ringwald #else
2455*af03003cSMatthias Ringwald     /* Make sure that we don't leak timing information about k.
2456*af03003cSMatthias Ringwald        See http://eprint.iacr.org/2011/232.pdf */
2457*af03003cSMatthias Ringwald     carry = vli_add(tmp, k, curve_n);
2458*af03003cSMatthias Ringwald     vli_add(s, tmp, curve_n);
2459*af03003cSMatthias Ringwald 
2460*af03003cSMatthias Ringwald     /* p = k * G */
2461*af03003cSMatthias Ringwald     EccPoint_mult(&p, &curve_G, k2[!carry], 0, (uECC_BYTES * 8) + 1);
2462*af03003cSMatthias Ringwald 
2463*af03003cSMatthias Ringwald     /* r = x1 (mod n) */
2464*af03003cSMatthias Ringwald     if (vli_cmp(curve_n, p.x) != 1) {
2465*af03003cSMatthias Ringwald         vli_sub(p.x, p.x, curve_n);
2466*af03003cSMatthias Ringwald     }
2467*af03003cSMatthias Ringwald #endif
2468*af03003cSMatthias Ringwald     if (vli_isZero(p.x)) {
2469*af03003cSMatthias Ringwald         return 0;
2470*af03003cSMatthias Ringwald     }
2471*af03003cSMatthias Ringwald 
2472*af03003cSMatthias Ringwald     // Attempt to get a random number to prevent side channel analysis of k.
2473*af03003cSMatthias Ringwald     // If the RNG fails every time (eg it was not defined), we continue so that
2474*af03003cSMatthias Ringwald     // deterministic signing can still work (with reduced security) without
2475*af03003cSMatthias Ringwald     // an RNG defined.
2476*af03003cSMatthias Ringwald     carry = 0; // use to signal that the RNG succeeded at least once.
2477*af03003cSMatthias Ringwald     for (tries = 0; tries < MAX_TRIES; ++tries) {
2478*af03003cSMatthias Ringwald         if (!g_rng_function((uint8_t *)tmp, sizeof(tmp))) {
2479*af03003cSMatthias Ringwald             continue;
2480*af03003cSMatthias Ringwald         }
2481*af03003cSMatthias Ringwald         carry = 1;
2482*af03003cSMatthias Ringwald         if (!vli_isZero(tmp)) {
2483*af03003cSMatthias Ringwald             break;
2484*af03003cSMatthias Ringwald         }
2485*af03003cSMatthias Ringwald     }
2486*af03003cSMatthias Ringwald     if (!carry) {
2487*af03003cSMatthias Ringwald         vli_clear(tmp);
2488*af03003cSMatthias Ringwald         tmp[0] = 1;
2489*af03003cSMatthias Ringwald     }
2490*af03003cSMatthias Ringwald 
2491*af03003cSMatthias Ringwald     /* Prevent side channel analysis of vli_modInv() to determine
2492*af03003cSMatthias Ringwald        bits of k / the private key by premultiplying by a random number */
2493*af03003cSMatthias Ringwald     vli_modMult_n(k, k, tmp); /* k' = rand * k */
2494*af03003cSMatthias Ringwald     vli_modInv_n(k, k, curve_n); /* k = 1 / k' */
2495*af03003cSMatthias Ringwald     vli_modMult_n(k, k, tmp); /* k = 1 / k */
2496*af03003cSMatthias Ringwald 
2497*af03003cSMatthias Ringwald     vli_nativeToBytes(signature, p.x); /* store r */
2498*af03003cSMatthias Ringwald 
2499*af03003cSMatthias Ringwald     tmp[uECC_N_WORDS - 1] = 0;
2500*af03003cSMatthias Ringwald     vli_bytesToNative(tmp, private_key); /* tmp = d */
2501*af03003cSMatthias Ringwald     s[uECC_N_WORDS - 1] = 0;
2502*af03003cSMatthias Ringwald     vli_set(s, p.x);
2503*af03003cSMatthias Ringwald     vli_modMult_n(s, tmp, s); /* s = r*d */
2504*af03003cSMatthias Ringwald 
2505*af03003cSMatthias Ringwald     vli_bytesToNative(tmp, message_hash);
2506*af03003cSMatthias Ringwald     vli_modAdd_n(s, tmp, s, curve_n); /* s = e + r*d */
2507*af03003cSMatthias Ringwald     vli_modMult_n(s, s, k); /* s = (e + r*d) / k */
2508*af03003cSMatthias Ringwald #if (uECC_CURVE == uECC_secp160r1)
2509*af03003cSMatthias Ringwald     if (s[uECC_N_WORDS - 1]) {
2510*af03003cSMatthias Ringwald         return 0;
2511*af03003cSMatthias Ringwald     }
2512*af03003cSMatthias Ringwald #endif
2513*af03003cSMatthias Ringwald     vli_nativeToBytes(signature + uECC_BYTES, s);
2514*af03003cSMatthias Ringwald     return 1;
2515*af03003cSMatthias Ringwald }
2516*af03003cSMatthias Ringwald 
2517*af03003cSMatthias Ringwald int uECC_sign(const uint8_t private_key[uECC_BYTES],
2518*af03003cSMatthias Ringwald               const uint8_t message_hash[uECC_BYTES],
2519*af03003cSMatthias Ringwald               uint8_t signature[uECC_BYTES*2]) {
2520*af03003cSMatthias Ringwald     uECC_word_t k[uECC_N_WORDS];
2521*af03003cSMatthias Ringwald     uECC_word_t tries;
2522*af03003cSMatthias Ringwald 
2523*af03003cSMatthias Ringwald     for (tries = 0; tries < MAX_TRIES; ++tries) {
2524*af03003cSMatthias Ringwald         if(g_rng_function((uint8_t *)k, sizeof(k))) {
2525*af03003cSMatthias Ringwald         #if (uECC_CURVE == uECC_secp160r1)
2526*af03003cSMatthias Ringwald             k[uECC_WORDS] &= 0x01;
2527*af03003cSMatthias Ringwald         #endif
2528*af03003cSMatthias Ringwald             if (uECC_sign_with_k(private_key, message_hash, k, signature)) {
2529*af03003cSMatthias Ringwald                 return 1;
2530*af03003cSMatthias Ringwald             }
2531*af03003cSMatthias Ringwald         }
2532*af03003cSMatthias Ringwald     }
2533*af03003cSMatthias Ringwald     return 0;
2534*af03003cSMatthias Ringwald }
2535*af03003cSMatthias Ringwald 
2536*af03003cSMatthias Ringwald /* Compute an HMAC using K as a key (as in RFC 6979). Note that K is always
2537*af03003cSMatthias Ringwald    the same size as the hash result size. */
2538*af03003cSMatthias Ringwald static void HMAC_init(uECC_HashContext *hash_context, const uint8_t *K) {
2539*af03003cSMatthias Ringwald     uint8_t *pad = hash_context->tmp + 2 * hash_context->result_size;
2540*af03003cSMatthias Ringwald     unsigned i;
2541*af03003cSMatthias Ringwald     for (i = 0; i < hash_context->result_size; ++i)
2542*af03003cSMatthias Ringwald         pad[i] = K[i] ^ 0x36;
2543*af03003cSMatthias Ringwald     for (; i < hash_context->block_size; ++i)
2544*af03003cSMatthias Ringwald         pad[i] = 0x36;
2545*af03003cSMatthias Ringwald 
2546*af03003cSMatthias Ringwald     hash_context->init_hash(hash_context);
2547*af03003cSMatthias Ringwald     hash_context->update_hash(hash_context, pad, hash_context->block_size);
2548*af03003cSMatthias Ringwald }
2549*af03003cSMatthias Ringwald 
2550*af03003cSMatthias Ringwald static void HMAC_update(uECC_HashContext *hash_context,
2551*af03003cSMatthias Ringwald                         const uint8_t *message,
2552*af03003cSMatthias Ringwald                         unsigned message_size) {
2553*af03003cSMatthias Ringwald     hash_context->update_hash(hash_context, message, message_size);
2554*af03003cSMatthias Ringwald }
2555*af03003cSMatthias Ringwald 
2556*af03003cSMatthias Ringwald static void HMAC_finish(uECC_HashContext *hash_context, const uint8_t *K, uint8_t *result) {
2557*af03003cSMatthias Ringwald     uint8_t *pad = hash_context->tmp + 2 * hash_context->result_size;
2558*af03003cSMatthias Ringwald     unsigned i;
2559*af03003cSMatthias Ringwald     for (i = 0; i < hash_context->result_size; ++i)
2560*af03003cSMatthias Ringwald         pad[i] = K[i] ^ 0x5c;
2561*af03003cSMatthias Ringwald     for (; i < hash_context->block_size; ++i)
2562*af03003cSMatthias Ringwald         pad[i] = 0x5c;
2563*af03003cSMatthias Ringwald 
2564*af03003cSMatthias Ringwald     hash_context->finish_hash(hash_context, result);
2565*af03003cSMatthias Ringwald 
2566*af03003cSMatthias Ringwald     hash_context->init_hash(hash_context);
2567*af03003cSMatthias Ringwald     hash_context->update_hash(hash_context, pad, hash_context->block_size);
2568*af03003cSMatthias Ringwald     hash_context->update_hash(hash_context, result, hash_context->result_size);
2569*af03003cSMatthias Ringwald     hash_context->finish_hash(hash_context, result);
2570*af03003cSMatthias Ringwald }
2571*af03003cSMatthias Ringwald 
2572*af03003cSMatthias Ringwald /* V = HMAC_K(V) */
2573*af03003cSMatthias Ringwald static void update_V(uECC_HashContext *hash_context, uint8_t *K, uint8_t *V) {
2574*af03003cSMatthias Ringwald     HMAC_init(hash_context, K);
2575*af03003cSMatthias Ringwald     HMAC_update(hash_context, V, hash_context->result_size);
2576*af03003cSMatthias Ringwald     HMAC_finish(hash_context, K, V);
2577*af03003cSMatthias Ringwald }
2578*af03003cSMatthias Ringwald 
2579*af03003cSMatthias Ringwald /* Deterministic signing, similar to RFC 6979. Differences are:
2580*af03003cSMatthias Ringwald     * We just use (truncated) H(m) directly rather than bits2octets(H(m))
2581*af03003cSMatthias Ringwald       (it is not reduced modulo curve_n).
2582*af03003cSMatthias Ringwald     * We generate a value for k (aka T) directly rather than converting endianness.
2583*af03003cSMatthias Ringwald 
2584*af03003cSMatthias Ringwald    Layout of hash_context->tmp: <K> | <V> | (1 byte overlapped 0x00 or 0x01) / <HMAC pad> */
2585*af03003cSMatthias Ringwald int uECC_sign_deterministic(const uint8_t private_key[uECC_BYTES],
2586*af03003cSMatthias Ringwald                             const uint8_t message_hash[uECC_BYTES],
2587*af03003cSMatthias Ringwald                             uECC_HashContext *hash_context,
2588*af03003cSMatthias Ringwald                             uint8_t signature[uECC_BYTES*2]) {
2589*af03003cSMatthias Ringwald     uint8_t *K = hash_context->tmp;
2590*af03003cSMatthias Ringwald     uint8_t *V = K + hash_context->result_size;
2591*af03003cSMatthias Ringwald     uECC_word_t tries;
2592*af03003cSMatthias Ringwald     unsigned i;
2593*af03003cSMatthias Ringwald     for (i = 0; i < hash_context->result_size; ++i) {
2594*af03003cSMatthias Ringwald         V[i] = 0x01;
2595*af03003cSMatthias Ringwald         K[i] = 0;
2596*af03003cSMatthias Ringwald     }
2597*af03003cSMatthias Ringwald 
2598*af03003cSMatthias Ringwald     // K = HMAC_K(V || 0x00 || int2octets(x) || h(m))
2599*af03003cSMatthias Ringwald     HMAC_init(hash_context, K);
2600*af03003cSMatthias Ringwald     V[hash_context->result_size] = 0x00;
2601*af03003cSMatthias Ringwald     HMAC_update(hash_context, V, hash_context->result_size + 1);
2602*af03003cSMatthias Ringwald     HMAC_update(hash_context, private_key, uECC_BYTES);
2603*af03003cSMatthias Ringwald     HMAC_update(hash_context, message_hash, uECC_BYTES);
2604*af03003cSMatthias Ringwald     HMAC_finish(hash_context, K, K);
2605*af03003cSMatthias Ringwald 
2606*af03003cSMatthias Ringwald     update_V(hash_context, K, V);
2607*af03003cSMatthias Ringwald 
2608*af03003cSMatthias Ringwald     // K = HMAC_K(V || 0x01 || int2octets(x) || h(m))
2609*af03003cSMatthias Ringwald     HMAC_init(hash_context, K);
2610*af03003cSMatthias Ringwald     V[hash_context->result_size] = 0x01;
2611*af03003cSMatthias Ringwald     HMAC_update(hash_context, V, hash_context->result_size + 1);
2612*af03003cSMatthias Ringwald     HMAC_update(hash_context, private_key, uECC_BYTES);
2613*af03003cSMatthias Ringwald     HMAC_update(hash_context, message_hash, uECC_BYTES);
2614*af03003cSMatthias Ringwald     HMAC_finish(hash_context, K, K);
2615*af03003cSMatthias Ringwald 
2616*af03003cSMatthias Ringwald     update_V(hash_context, K, V);
2617*af03003cSMatthias Ringwald 
2618*af03003cSMatthias Ringwald     for (tries = 0; tries < MAX_TRIES; ++tries) {
2619*af03003cSMatthias Ringwald         uECC_word_t T[uECC_N_WORDS];
2620*af03003cSMatthias Ringwald         uint8_t *T_ptr = (uint8_t *)T;
2621*af03003cSMatthias Ringwald         unsigned T_bytes = 0;
2622*af03003cSMatthias Ringwald         while (T_bytes < sizeof(T)) {
2623*af03003cSMatthias Ringwald             update_V(hash_context, K, V);
2624*af03003cSMatthias Ringwald             for (i = 0; i < hash_context->result_size && T_bytes < sizeof(T); ++i, ++T_bytes) {
2625*af03003cSMatthias Ringwald                 T_ptr[T_bytes] = V[i];
2626*af03003cSMatthias Ringwald             }
2627*af03003cSMatthias Ringwald         }
2628*af03003cSMatthias Ringwald     #if (uECC_CURVE == uECC_secp160r1)
2629*af03003cSMatthias Ringwald         T[uECC_WORDS] &= 0x01;
2630*af03003cSMatthias Ringwald     #endif
2631*af03003cSMatthias Ringwald 
2632*af03003cSMatthias Ringwald         if (uECC_sign_with_k(private_key, message_hash, T, signature)) {
2633*af03003cSMatthias Ringwald             return 1;
2634*af03003cSMatthias Ringwald         }
2635*af03003cSMatthias Ringwald 
2636*af03003cSMatthias Ringwald         // K = HMAC_K(V || 0x00)
2637*af03003cSMatthias Ringwald         HMAC_init(hash_context, K);
2638*af03003cSMatthias Ringwald         V[hash_context->result_size] = 0x00;
2639*af03003cSMatthias Ringwald         HMAC_update(hash_context, V, hash_context->result_size + 1);
2640*af03003cSMatthias Ringwald         HMAC_finish(hash_context, K, K);
2641*af03003cSMatthias Ringwald 
2642*af03003cSMatthias Ringwald         update_V(hash_context, K, V);
2643*af03003cSMatthias Ringwald     }
2644*af03003cSMatthias Ringwald     return 0;
2645*af03003cSMatthias Ringwald }
2646*af03003cSMatthias Ringwald 
2647*af03003cSMatthias Ringwald static bitcount_t smax(bitcount_t a, bitcount_t b) {
2648*af03003cSMatthias Ringwald     return (a > b ? a : b);
2649*af03003cSMatthias Ringwald }
2650*af03003cSMatthias Ringwald 
2651*af03003cSMatthias Ringwald int uECC_verify(const uint8_t public_key[uECC_BYTES*2],
2652*af03003cSMatthias Ringwald                 const uint8_t hash[uECC_BYTES],
2653*af03003cSMatthias Ringwald                 const uint8_t signature[uECC_BYTES*2]) {
2654*af03003cSMatthias Ringwald     uECC_word_t u1[uECC_N_WORDS], u2[uECC_N_WORDS];
2655*af03003cSMatthias Ringwald     uECC_word_t z[uECC_N_WORDS];
2656*af03003cSMatthias Ringwald     EccPoint public, sum;
2657*af03003cSMatthias Ringwald     uECC_word_t rx[uECC_WORDS];
2658*af03003cSMatthias Ringwald     uECC_word_t ry[uECC_WORDS];
2659*af03003cSMatthias Ringwald     uECC_word_t tx[uECC_WORDS];
2660*af03003cSMatthias Ringwald     uECC_word_t ty[uECC_WORDS];
2661*af03003cSMatthias Ringwald     uECC_word_t tz[uECC_WORDS];
2662*af03003cSMatthias Ringwald     const EccPoint *points[4];
2663*af03003cSMatthias Ringwald     const EccPoint *point;
2664*af03003cSMatthias Ringwald     bitcount_t numBits;
2665*af03003cSMatthias Ringwald     bitcount_t i;
2666*af03003cSMatthias Ringwald     uECC_word_t r[uECC_N_WORDS], s[uECC_N_WORDS];
2667*af03003cSMatthias Ringwald     r[uECC_N_WORDS - 1] = 0;
2668*af03003cSMatthias Ringwald     s[uECC_N_WORDS - 1] = 0;
2669*af03003cSMatthias Ringwald 
2670*af03003cSMatthias Ringwald     vli_bytesToNative(public.x, public_key);
2671*af03003cSMatthias Ringwald     vli_bytesToNative(public.y, public_key + uECC_BYTES);
2672*af03003cSMatthias Ringwald     vli_bytesToNative(r, signature);
2673*af03003cSMatthias Ringwald     vli_bytesToNative(s, signature + uECC_BYTES);
2674*af03003cSMatthias Ringwald 
2675*af03003cSMatthias Ringwald     if (vli_isZero(r) || vli_isZero(s)) { /* r, s must not be 0. */
2676*af03003cSMatthias Ringwald         return 0;
2677*af03003cSMatthias Ringwald     }
2678*af03003cSMatthias Ringwald 
2679*af03003cSMatthias Ringwald #if (uECC_CURVE != uECC_secp160r1)
2680*af03003cSMatthias Ringwald     if (vli_cmp(curve_n, r) != 1 || vli_cmp(curve_n, s) != 1) { /* r, s must be < n. */
2681*af03003cSMatthias Ringwald         return 0;
2682*af03003cSMatthias Ringwald     }
2683*af03003cSMatthias Ringwald #endif
2684*af03003cSMatthias Ringwald 
2685*af03003cSMatthias Ringwald     /* Calculate u1 and u2. */
2686*af03003cSMatthias Ringwald     vli_modInv_n(z, s, curve_n); /* Z = s^-1 */
2687*af03003cSMatthias Ringwald     u1[uECC_N_WORDS - 1] = 0;
2688*af03003cSMatthias Ringwald     vli_bytesToNative(u1, hash);
2689*af03003cSMatthias Ringwald     vli_modMult_n(u1, u1, z); /* u1 = e/s */
2690*af03003cSMatthias Ringwald     vli_modMult_n(u2, r, z); /* u2 = r/s */
2691*af03003cSMatthias Ringwald 
2692*af03003cSMatthias Ringwald     /* Calculate sum = G + Q. */
2693*af03003cSMatthias Ringwald     vli_set(sum.x, public.x);
2694*af03003cSMatthias Ringwald     vli_set(sum.y, public.y);
2695*af03003cSMatthias Ringwald     vli_set(tx, curve_G.x);
2696*af03003cSMatthias Ringwald     vli_set(ty, curve_G.y);
2697*af03003cSMatthias Ringwald     vli_modSub_fast(z, sum.x, tx); /* Z = x2 - x1 */
2698*af03003cSMatthias Ringwald     XYcZ_add(tx, ty, sum.x, sum.y);
2699*af03003cSMatthias Ringwald     vli_modInv(z, z, curve_p); /* Z = 1/Z */
2700*af03003cSMatthias Ringwald     apply_z(sum.x, sum.y, z);
2701*af03003cSMatthias Ringwald 
2702*af03003cSMatthias Ringwald     /* Use Shamir's trick to calculate u1*G + u2*Q */
2703*af03003cSMatthias Ringwald     points[0] = 0;
2704*af03003cSMatthias Ringwald     points[1] = &curve_G;
2705*af03003cSMatthias Ringwald     points[2] = &public;
2706*af03003cSMatthias Ringwald     points[3] = &sum;
2707*af03003cSMatthias Ringwald     numBits = smax(vli_numBits(u1, uECC_N_WORDS), vli_numBits(u2, uECC_N_WORDS));
2708*af03003cSMatthias Ringwald 
2709*af03003cSMatthias Ringwald     point = points[(!!vli_testBit(u1, numBits - 1)) | ((!!vli_testBit(u2, numBits - 1)) << 1)];
2710*af03003cSMatthias Ringwald     vli_set(rx, point->x);
2711*af03003cSMatthias Ringwald     vli_set(ry, point->y);
2712*af03003cSMatthias Ringwald     vli_clear(z);
2713*af03003cSMatthias Ringwald     z[0] = 1;
2714*af03003cSMatthias Ringwald 
2715*af03003cSMatthias Ringwald     for (i = numBits - 2; i >= 0; --i) {
2716*af03003cSMatthias Ringwald         uECC_word_t index;
2717*af03003cSMatthias Ringwald         EccPoint_double_jacobian(rx, ry, z);
2718*af03003cSMatthias Ringwald 
2719*af03003cSMatthias Ringwald         index = (!!vli_testBit(u1, i)) | ((!!vli_testBit(u2, i)) << 1);
2720*af03003cSMatthias Ringwald         point = points[index];
2721*af03003cSMatthias Ringwald         if (point) {
2722*af03003cSMatthias Ringwald             vli_set(tx, point->x);
2723*af03003cSMatthias Ringwald             vli_set(ty, point->y);
2724*af03003cSMatthias Ringwald             apply_z(tx, ty, z);
2725*af03003cSMatthias Ringwald             vli_modSub_fast(tz, rx, tx); /* Z = x2 - x1 */
2726*af03003cSMatthias Ringwald             XYcZ_add(tx, ty, rx, ry);
2727*af03003cSMatthias Ringwald             vli_modMult_fast(z, z, tz);
2728*af03003cSMatthias Ringwald         }
2729*af03003cSMatthias Ringwald     }
2730*af03003cSMatthias Ringwald 
2731*af03003cSMatthias Ringwald     vli_modInv(z, z, curve_p); /* Z = 1/Z */
2732*af03003cSMatthias Ringwald     apply_z(rx, ry, z);
2733*af03003cSMatthias Ringwald 
2734*af03003cSMatthias Ringwald     /* v = x1 (mod n) */
2735*af03003cSMatthias Ringwald #if (uECC_CURVE != uECC_secp160r1)
2736*af03003cSMatthias Ringwald     if (vli_cmp(curve_n, rx) != 1) {
2737*af03003cSMatthias Ringwald         vli_sub(rx, rx, curve_n);
2738*af03003cSMatthias Ringwald     }
2739*af03003cSMatthias Ringwald #endif
2740*af03003cSMatthias Ringwald 
2741*af03003cSMatthias Ringwald     /* Accept only if v == r. */
2742*af03003cSMatthias Ringwald     return vli_equal(rx, r);
2743*af03003cSMatthias Ringwald }
2744