Path: blob/main/contrib/bearssl/src/ec/ec_p256_m15.c
39507 views
/*1* Copyright (c) 2017 Thomas Pornin <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining4* a copy of this software and associated documentation files (the5* "Software"), to deal in the Software without restriction, including6* without limitation the rights to use, copy, modify, merge, publish,7* distribute, sublicense, and/or sell copies of the Software, and to8* permit persons to whom the Software is furnished to do so, subject to9* the following conditions:10*11* The above copyright notice and this permission notice shall be12* included in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS18* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN19* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*/2324#include "inner.h"2526/*27* If BR_NO_ARITH_SHIFT is undefined, or defined to 0, then we _assume_28* that right-shifting a signed negative integer copies the sign bit29* (arithmetic right-shift). This is "implementation-defined behaviour",30* i.e. it is not undefined, but it may differ between compilers. Each31* compiler is supposed to document its behaviour in that respect. GCC32* explicitly defines that an arithmetic right shift is used. We expect33* all other compilers to do the same, because underlying CPU offer an34* arithmetic right shift opcode that could not be used otherwise.35*/36#if BR_NO_ARITH_SHIFT37#define ARSH(x, n) (((uint32_t)(x) >> (n)) \38| ((-((uint32_t)(x) >> 31)) << (32 - (n))))39#else40#define ARSH(x, n) ((*(int32_t *)&(x)) >> (n))41#endif4243/*44* Convert an integer from unsigned big-endian encoding to a sequence of45* 13-bit words in little-endian order. The final "partial" word is46* returned.47*/48static uint32_t49be8_to_le13(uint32_t *dst, const unsigned char *src, size_t len)50{51uint32_t acc;52int acc_len;5354acc = 0;55acc_len = 0;56while (len -- > 0) {57acc |= (uint32_t)src[len] << acc_len;58acc_len += 8;59if (acc_len >= 13) {60*dst ++ = acc & 0x1FFF;61acc >>= 13;62acc_len -= 13;63}64}65return acc;66}6768/*69* Convert an integer (13-bit words, little-endian) to unsigned70* big-endian encoding. The total encoding length is provided; all71* the destination bytes will be filled.72*/73static void74le13_to_be8(unsigned char *dst, size_t len, const uint32_t *src)75{76uint32_t acc;77int acc_len;7879acc = 0;80acc_len = 0;81while (len -- > 0) {82if (acc_len < 8) {83acc |= (*src ++) << acc_len;84acc_len += 13;85}86dst[len] = (unsigned char)acc;87acc >>= 8;88acc_len -= 8;89}90}9192/*93* Normalise an array of words to a strict 13 bits per word. Returned94* value is the resulting carry. The source (w) and destination (d)95* arrays may be identical, but shall not overlap partially.96*/97static inline uint32_t98norm13(uint32_t *d, const uint32_t *w, size_t len)99{100size_t u;101uint32_t cc;102103cc = 0;104for (u = 0; u < len; u ++) {105int32_t z;106107z = w[u] + cc;108d[u] = z & 0x1FFF;109cc = ARSH(z, 13);110}111return cc;112}113114/*115* mul20() multiplies two 260-bit integers together. Each word must fit116* on 13 bits; source operands use 20 words, destination operand117* receives 40 words. All overlaps allowed.118*119* square20() computes the square of a 260-bit integer. Each word must120* fit on 13 bits; source operand uses 20 words, destination operand121* receives 40 words. All overlaps allowed.122*/123124#if BR_SLOW_MUL15125126static void127mul20(uint32_t *d, const uint32_t *a, const uint32_t *b)128{129/*130* Two-level Karatsuba: turns a 20x20 multiplication into131* nine 5x5 multiplications. We use 13-bit words but do not132* propagate carries immediately, so words may expand:133*134* - First Karatsuba decomposition turns the 20x20 mul on135* 13-bit words into three 10x10 muls, two on 13-bit words136* and one on 14-bit words.137*138* - Second Karatsuba decomposition further splits these into:139*140* * four 5x5 muls on 13-bit words141* * four 5x5 muls on 14-bit words142* * one 5x5 mul on 15-bit words143*144* Highest word value is 8191, 16382 or 32764, for 13-bit, 14-bit145* or 15-bit words, respectively.146*/147uint32_t u[45], v[45], w[90];148uint32_t cc;149int i;150151#define ZADD(dw, d_off, s1w, s1_off, s2w, s2_off) do { \152(dw)[5 * (d_off) + 0] = (s1w)[5 * (s1_off) + 0] \153+ (s2w)[5 * (s2_off) + 0]; \154(dw)[5 * (d_off) + 1] = (s1w)[5 * (s1_off) + 1] \155+ (s2w)[5 * (s2_off) + 1]; \156(dw)[5 * (d_off) + 2] = (s1w)[5 * (s1_off) + 2] \157+ (s2w)[5 * (s2_off) + 2]; \158(dw)[5 * (d_off) + 3] = (s1w)[5 * (s1_off) + 3] \159+ (s2w)[5 * (s2_off) + 3]; \160(dw)[5 * (d_off) + 4] = (s1w)[5 * (s1_off) + 4] \161+ (s2w)[5 * (s2_off) + 4]; \162} while (0)163164#define ZADDT(dw, d_off, sw, s_off) do { \165(dw)[5 * (d_off) + 0] += (sw)[5 * (s_off) + 0]; \166(dw)[5 * (d_off) + 1] += (sw)[5 * (s_off) + 1]; \167(dw)[5 * (d_off) + 2] += (sw)[5 * (s_off) + 2]; \168(dw)[5 * (d_off) + 3] += (sw)[5 * (s_off) + 3]; \169(dw)[5 * (d_off) + 4] += (sw)[5 * (s_off) + 4]; \170} while (0)171172#define ZSUB2F(dw, d_off, s1w, s1_off, s2w, s2_off) do { \173(dw)[5 * (d_off) + 0] -= (s1w)[5 * (s1_off) + 0] \174+ (s2w)[5 * (s2_off) + 0]; \175(dw)[5 * (d_off) + 1] -= (s1w)[5 * (s1_off) + 1] \176+ (s2w)[5 * (s2_off) + 1]; \177(dw)[5 * (d_off) + 2] -= (s1w)[5 * (s1_off) + 2] \178+ (s2w)[5 * (s2_off) + 2]; \179(dw)[5 * (d_off) + 3] -= (s1w)[5 * (s1_off) + 3] \180+ (s2w)[5 * (s2_off) + 3]; \181(dw)[5 * (d_off) + 4] -= (s1w)[5 * (s1_off) + 4] \182+ (s2w)[5 * (s2_off) + 4]; \183} while (0)184185#define CPR1(w, cprcc) do { \186uint32_t cprz = (w) + cprcc; \187(w) = cprz & 0x1FFF; \188cprcc = cprz >> 13; \189} while (0)190191#define CPR(dw, d_off) do { \192uint32_t cprcc; \193cprcc = 0; \194CPR1((dw)[(d_off) + 0], cprcc); \195CPR1((dw)[(d_off) + 1], cprcc); \196CPR1((dw)[(d_off) + 2], cprcc); \197CPR1((dw)[(d_off) + 3], cprcc); \198CPR1((dw)[(d_off) + 4], cprcc); \199CPR1((dw)[(d_off) + 5], cprcc); \200CPR1((dw)[(d_off) + 6], cprcc); \201CPR1((dw)[(d_off) + 7], cprcc); \202CPR1((dw)[(d_off) + 8], cprcc); \203(dw)[(d_off) + 9] = cprcc; \204} while (0)205206memcpy(u, a, 20 * sizeof *a);207ZADD(u, 4, a, 0, a, 1);208ZADD(u, 5, a, 2, a, 3);209ZADD(u, 6, a, 0, a, 2);210ZADD(u, 7, a, 1, a, 3);211ZADD(u, 8, u, 6, u, 7);212213memcpy(v, b, 20 * sizeof *b);214ZADD(v, 4, b, 0, b, 1);215ZADD(v, 5, b, 2, b, 3);216ZADD(v, 6, b, 0, b, 2);217ZADD(v, 7, b, 1, b, 3);218ZADD(v, 8, v, 6, v, 7);219220/*221* Do the eight first 8x8 muls. Source words are at most 16382222* each, so we can add product results together "as is" in 32-bit223* words.224*/225for (i = 0; i < 40; i += 5) {226w[(i << 1) + 0] = MUL15(u[i + 0], v[i + 0]);227w[(i << 1) + 1] = MUL15(u[i + 0], v[i + 1])228+ MUL15(u[i + 1], v[i + 0]);229w[(i << 1) + 2] = MUL15(u[i + 0], v[i + 2])230+ MUL15(u[i + 1], v[i + 1])231+ MUL15(u[i + 2], v[i + 0]);232w[(i << 1) + 3] = MUL15(u[i + 0], v[i + 3])233+ MUL15(u[i + 1], v[i + 2])234+ MUL15(u[i + 2], v[i + 1])235+ MUL15(u[i + 3], v[i + 0]);236w[(i << 1) + 4] = MUL15(u[i + 0], v[i + 4])237+ MUL15(u[i + 1], v[i + 3])238+ MUL15(u[i + 2], v[i + 2])239+ MUL15(u[i + 3], v[i + 1])240+ MUL15(u[i + 4], v[i + 0]);241w[(i << 1) + 5] = MUL15(u[i + 1], v[i + 4])242+ MUL15(u[i + 2], v[i + 3])243+ MUL15(u[i + 3], v[i + 2])244+ MUL15(u[i + 4], v[i + 1]);245w[(i << 1) + 6] = MUL15(u[i + 2], v[i + 4])246+ MUL15(u[i + 3], v[i + 3])247+ MUL15(u[i + 4], v[i + 2]);248w[(i << 1) + 7] = MUL15(u[i + 3], v[i + 4])249+ MUL15(u[i + 4], v[i + 3]);250w[(i << 1) + 8] = MUL15(u[i + 4], v[i + 4]);251w[(i << 1) + 9] = 0;252}253254/*255* For the 9th multiplication, source words are up to 32764,256* so we must do some carry propagation. If we add up to257* 4 products and the carry is no more than 524224, then the258* result fits in 32 bits, and the next carry will be no more259* than 524224 (because 4*(32764^2)+524224 < 8192*524225).260*261* We thus just skip one of the products in the middle word,262* then do a carry propagation (this reduces words to 13 bits263* each, except possibly the last, which may use up to 17 bits264* or so), then add the missing product.265*/266w[80 + 0] = MUL15(u[40 + 0], v[40 + 0]);267w[80 + 1] = MUL15(u[40 + 0], v[40 + 1])268+ MUL15(u[40 + 1], v[40 + 0]);269w[80 + 2] = MUL15(u[40 + 0], v[40 + 2])270+ MUL15(u[40 + 1], v[40 + 1])271+ MUL15(u[40 + 2], v[40 + 0]);272w[80 + 3] = MUL15(u[40 + 0], v[40 + 3])273+ MUL15(u[40 + 1], v[40 + 2])274+ MUL15(u[40 + 2], v[40 + 1])275+ MUL15(u[40 + 3], v[40 + 0]);276w[80 + 4] = MUL15(u[40 + 0], v[40 + 4])277+ MUL15(u[40 + 1], v[40 + 3])278+ MUL15(u[40 + 2], v[40 + 2])279+ MUL15(u[40 + 3], v[40 + 1]);280/* + MUL15(u[40 + 4], v[40 + 0]) */281w[80 + 5] = MUL15(u[40 + 1], v[40 + 4])282+ MUL15(u[40 + 2], v[40 + 3])283+ MUL15(u[40 + 3], v[40 + 2])284+ MUL15(u[40 + 4], v[40 + 1]);285w[80 + 6] = MUL15(u[40 + 2], v[40 + 4])286+ MUL15(u[40 + 3], v[40 + 3])287+ MUL15(u[40 + 4], v[40 + 2]);288w[80 + 7] = MUL15(u[40 + 3], v[40 + 4])289+ MUL15(u[40 + 4], v[40 + 3]);290w[80 + 8] = MUL15(u[40 + 4], v[40 + 4]);291292CPR(w, 80);293294w[80 + 4] += MUL15(u[40 + 4], v[40 + 0]);295296/*297* The products on 14-bit words in slots 6 and 7 yield values298* up to 5*(16382^2) each, and we need to subtract two such299* values from the higher word. We need the subtraction to fit300* in a _signed_ 32-bit integer, i.e. 31 bits + a sign bit.301* However, 10*(16382^2) does not fit. So we must perform a302* bit of reduction here.303*/304CPR(w, 60);305CPR(w, 70);306307/*308* Recompose results.309*/310311/* 0..1*0..1 into 0..3 */312ZSUB2F(w, 8, w, 0, w, 2);313ZSUB2F(w, 9, w, 1, w, 3);314ZADDT(w, 1, w, 8);315ZADDT(w, 2, w, 9);316317/* 2..3*2..3 into 4..7 */318ZSUB2F(w, 10, w, 4, w, 6);319ZSUB2F(w, 11, w, 5, w, 7);320ZADDT(w, 5, w, 10);321ZADDT(w, 6, w, 11);322323/* (0..1+2..3)*(0..1+2..3) into 12..15 */324ZSUB2F(w, 16, w, 12, w, 14);325ZSUB2F(w, 17, w, 13, w, 15);326ZADDT(w, 13, w, 16);327ZADDT(w, 14, w, 17);328329/* first-level recomposition */330ZSUB2F(w, 12, w, 0, w, 4);331ZSUB2F(w, 13, w, 1, w, 5);332ZSUB2F(w, 14, w, 2, w, 6);333ZSUB2F(w, 15, w, 3, w, 7);334ZADDT(w, 2, w, 12);335ZADDT(w, 3, w, 13);336ZADDT(w, 4, w, 14);337ZADDT(w, 5, w, 15);338339/*340* Perform carry propagation to bring all words down to 13 bits.341*/342cc = norm13(d, w, 40);343d[39] += (cc << 13);344345#undef ZADD346#undef ZADDT347#undef ZSUB2F348#undef CPR1349#undef CPR350}351352static inline void353square20(uint32_t *d, const uint32_t *a)354{355mul20(d, a, a);356}357358#else359360static void361mul20(uint32_t *d, const uint32_t *a, const uint32_t *b)362{363uint32_t t[39];364365t[ 0] = MUL15(a[ 0], b[ 0]);366t[ 1] = MUL15(a[ 0], b[ 1])367+ MUL15(a[ 1], b[ 0]);368t[ 2] = MUL15(a[ 0], b[ 2])369+ MUL15(a[ 1], b[ 1])370+ MUL15(a[ 2], b[ 0]);371t[ 3] = MUL15(a[ 0], b[ 3])372+ MUL15(a[ 1], b[ 2])373+ MUL15(a[ 2], b[ 1])374+ MUL15(a[ 3], b[ 0]);375t[ 4] = MUL15(a[ 0], b[ 4])376+ MUL15(a[ 1], b[ 3])377+ MUL15(a[ 2], b[ 2])378+ MUL15(a[ 3], b[ 1])379+ MUL15(a[ 4], b[ 0]);380t[ 5] = MUL15(a[ 0], b[ 5])381+ MUL15(a[ 1], b[ 4])382+ MUL15(a[ 2], b[ 3])383+ MUL15(a[ 3], b[ 2])384+ MUL15(a[ 4], b[ 1])385+ MUL15(a[ 5], b[ 0]);386t[ 6] = MUL15(a[ 0], b[ 6])387+ MUL15(a[ 1], b[ 5])388+ MUL15(a[ 2], b[ 4])389+ MUL15(a[ 3], b[ 3])390+ MUL15(a[ 4], b[ 2])391+ MUL15(a[ 5], b[ 1])392+ MUL15(a[ 6], b[ 0]);393t[ 7] = MUL15(a[ 0], b[ 7])394+ MUL15(a[ 1], b[ 6])395+ MUL15(a[ 2], b[ 5])396+ MUL15(a[ 3], b[ 4])397+ MUL15(a[ 4], b[ 3])398+ MUL15(a[ 5], b[ 2])399+ MUL15(a[ 6], b[ 1])400+ MUL15(a[ 7], b[ 0]);401t[ 8] = MUL15(a[ 0], b[ 8])402+ MUL15(a[ 1], b[ 7])403+ MUL15(a[ 2], b[ 6])404+ MUL15(a[ 3], b[ 5])405+ MUL15(a[ 4], b[ 4])406+ MUL15(a[ 5], b[ 3])407+ MUL15(a[ 6], b[ 2])408+ MUL15(a[ 7], b[ 1])409+ MUL15(a[ 8], b[ 0]);410t[ 9] = MUL15(a[ 0], b[ 9])411+ MUL15(a[ 1], b[ 8])412+ MUL15(a[ 2], b[ 7])413+ MUL15(a[ 3], b[ 6])414+ MUL15(a[ 4], b[ 5])415+ MUL15(a[ 5], b[ 4])416+ MUL15(a[ 6], b[ 3])417+ MUL15(a[ 7], b[ 2])418+ MUL15(a[ 8], b[ 1])419+ MUL15(a[ 9], b[ 0]);420t[10] = MUL15(a[ 0], b[10])421+ MUL15(a[ 1], b[ 9])422+ MUL15(a[ 2], b[ 8])423+ MUL15(a[ 3], b[ 7])424+ MUL15(a[ 4], b[ 6])425+ MUL15(a[ 5], b[ 5])426+ MUL15(a[ 6], b[ 4])427+ MUL15(a[ 7], b[ 3])428+ MUL15(a[ 8], b[ 2])429+ MUL15(a[ 9], b[ 1])430+ MUL15(a[10], b[ 0]);431t[11] = MUL15(a[ 0], b[11])432+ MUL15(a[ 1], b[10])433+ MUL15(a[ 2], b[ 9])434+ MUL15(a[ 3], b[ 8])435+ MUL15(a[ 4], b[ 7])436+ MUL15(a[ 5], b[ 6])437+ MUL15(a[ 6], b[ 5])438+ MUL15(a[ 7], b[ 4])439+ MUL15(a[ 8], b[ 3])440+ MUL15(a[ 9], b[ 2])441+ MUL15(a[10], b[ 1])442+ MUL15(a[11], b[ 0]);443t[12] = MUL15(a[ 0], b[12])444+ MUL15(a[ 1], b[11])445+ MUL15(a[ 2], b[10])446+ MUL15(a[ 3], b[ 9])447+ MUL15(a[ 4], b[ 8])448+ MUL15(a[ 5], b[ 7])449+ MUL15(a[ 6], b[ 6])450+ MUL15(a[ 7], b[ 5])451+ MUL15(a[ 8], b[ 4])452+ MUL15(a[ 9], b[ 3])453+ MUL15(a[10], b[ 2])454+ MUL15(a[11], b[ 1])455+ MUL15(a[12], b[ 0]);456t[13] = MUL15(a[ 0], b[13])457+ MUL15(a[ 1], b[12])458+ MUL15(a[ 2], b[11])459+ MUL15(a[ 3], b[10])460+ MUL15(a[ 4], b[ 9])461+ MUL15(a[ 5], b[ 8])462+ MUL15(a[ 6], b[ 7])463+ MUL15(a[ 7], b[ 6])464+ MUL15(a[ 8], b[ 5])465+ MUL15(a[ 9], b[ 4])466+ MUL15(a[10], b[ 3])467+ MUL15(a[11], b[ 2])468+ MUL15(a[12], b[ 1])469+ MUL15(a[13], b[ 0]);470t[14] = MUL15(a[ 0], b[14])471+ MUL15(a[ 1], b[13])472+ MUL15(a[ 2], b[12])473+ MUL15(a[ 3], b[11])474+ MUL15(a[ 4], b[10])475+ MUL15(a[ 5], b[ 9])476+ MUL15(a[ 6], b[ 8])477+ MUL15(a[ 7], b[ 7])478+ MUL15(a[ 8], b[ 6])479+ MUL15(a[ 9], b[ 5])480+ MUL15(a[10], b[ 4])481+ MUL15(a[11], b[ 3])482+ MUL15(a[12], b[ 2])483+ MUL15(a[13], b[ 1])484+ MUL15(a[14], b[ 0]);485t[15] = MUL15(a[ 0], b[15])486+ MUL15(a[ 1], b[14])487+ MUL15(a[ 2], b[13])488+ MUL15(a[ 3], b[12])489+ MUL15(a[ 4], b[11])490+ MUL15(a[ 5], b[10])491+ MUL15(a[ 6], b[ 9])492+ MUL15(a[ 7], b[ 8])493+ MUL15(a[ 8], b[ 7])494+ MUL15(a[ 9], b[ 6])495+ MUL15(a[10], b[ 5])496+ MUL15(a[11], b[ 4])497+ MUL15(a[12], b[ 3])498+ MUL15(a[13], b[ 2])499+ MUL15(a[14], b[ 1])500+ MUL15(a[15], b[ 0]);501t[16] = MUL15(a[ 0], b[16])502+ MUL15(a[ 1], b[15])503+ MUL15(a[ 2], b[14])504+ MUL15(a[ 3], b[13])505+ MUL15(a[ 4], b[12])506+ MUL15(a[ 5], b[11])507+ MUL15(a[ 6], b[10])508+ MUL15(a[ 7], b[ 9])509+ MUL15(a[ 8], b[ 8])510+ MUL15(a[ 9], b[ 7])511+ MUL15(a[10], b[ 6])512+ MUL15(a[11], b[ 5])513+ MUL15(a[12], b[ 4])514+ MUL15(a[13], b[ 3])515+ MUL15(a[14], b[ 2])516+ MUL15(a[15], b[ 1])517+ MUL15(a[16], b[ 0]);518t[17] = MUL15(a[ 0], b[17])519+ MUL15(a[ 1], b[16])520+ MUL15(a[ 2], b[15])521+ MUL15(a[ 3], b[14])522+ MUL15(a[ 4], b[13])523+ MUL15(a[ 5], b[12])524+ MUL15(a[ 6], b[11])525+ MUL15(a[ 7], b[10])526+ MUL15(a[ 8], b[ 9])527+ MUL15(a[ 9], b[ 8])528+ MUL15(a[10], b[ 7])529+ MUL15(a[11], b[ 6])530+ MUL15(a[12], b[ 5])531+ MUL15(a[13], b[ 4])532+ MUL15(a[14], b[ 3])533+ MUL15(a[15], b[ 2])534+ MUL15(a[16], b[ 1])535+ MUL15(a[17], b[ 0]);536t[18] = MUL15(a[ 0], b[18])537+ MUL15(a[ 1], b[17])538+ MUL15(a[ 2], b[16])539+ MUL15(a[ 3], b[15])540+ MUL15(a[ 4], b[14])541+ MUL15(a[ 5], b[13])542+ MUL15(a[ 6], b[12])543+ MUL15(a[ 7], b[11])544+ MUL15(a[ 8], b[10])545+ MUL15(a[ 9], b[ 9])546+ MUL15(a[10], b[ 8])547+ MUL15(a[11], b[ 7])548+ MUL15(a[12], b[ 6])549+ MUL15(a[13], b[ 5])550+ MUL15(a[14], b[ 4])551+ MUL15(a[15], b[ 3])552+ MUL15(a[16], b[ 2])553+ MUL15(a[17], b[ 1])554+ MUL15(a[18], b[ 0]);555t[19] = MUL15(a[ 0], b[19])556+ MUL15(a[ 1], b[18])557+ MUL15(a[ 2], b[17])558+ MUL15(a[ 3], b[16])559+ MUL15(a[ 4], b[15])560+ MUL15(a[ 5], b[14])561+ MUL15(a[ 6], b[13])562+ MUL15(a[ 7], b[12])563+ MUL15(a[ 8], b[11])564+ MUL15(a[ 9], b[10])565+ MUL15(a[10], b[ 9])566+ MUL15(a[11], b[ 8])567+ MUL15(a[12], b[ 7])568+ MUL15(a[13], b[ 6])569+ MUL15(a[14], b[ 5])570+ MUL15(a[15], b[ 4])571+ MUL15(a[16], b[ 3])572+ MUL15(a[17], b[ 2])573+ MUL15(a[18], b[ 1])574+ MUL15(a[19], b[ 0]);575t[20] = MUL15(a[ 1], b[19])576+ MUL15(a[ 2], b[18])577+ MUL15(a[ 3], b[17])578+ MUL15(a[ 4], b[16])579+ MUL15(a[ 5], b[15])580+ MUL15(a[ 6], b[14])581+ MUL15(a[ 7], b[13])582+ MUL15(a[ 8], b[12])583+ MUL15(a[ 9], b[11])584+ MUL15(a[10], b[10])585+ MUL15(a[11], b[ 9])586+ MUL15(a[12], b[ 8])587+ MUL15(a[13], b[ 7])588+ MUL15(a[14], b[ 6])589+ MUL15(a[15], b[ 5])590+ MUL15(a[16], b[ 4])591+ MUL15(a[17], b[ 3])592+ MUL15(a[18], b[ 2])593+ MUL15(a[19], b[ 1]);594t[21] = MUL15(a[ 2], b[19])595+ MUL15(a[ 3], b[18])596+ MUL15(a[ 4], b[17])597+ MUL15(a[ 5], b[16])598+ MUL15(a[ 6], b[15])599+ MUL15(a[ 7], b[14])600+ MUL15(a[ 8], b[13])601+ MUL15(a[ 9], b[12])602+ MUL15(a[10], b[11])603+ MUL15(a[11], b[10])604+ MUL15(a[12], b[ 9])605+ MUL15(a[13], b[ 8])606+ MUL15(a[14], b[ 7])607+ MUL15(a[15], b[ 6])608+ MUL15(a[16], b[ 5])609+ MUL15(a[17], b[ 4])610+ MUL15(a[18], b[ 3])611+ MUL15(a[19], b[ 2]);612t[22] = MUL15(a[ 3], b[19])613+ MUL15(a[ 4], b[18])614+ MUL15(a[ 5], b[17])615+ MUL15(a[ 6], b[16])616+ MUL15(a[ 7], b[15])617+ MUL15(a[ 8], b[14])618+ MUL15(a[ 9], b[13])619+ MUL15(a[10], b[12])620+ MUL15(a[11], b[11])621+ MUL15(a[12], b[10])622+ MUL15(a[13], b[ 9])623+ MUL15(a[14], b[ 8])624+ MUL15(a[15], b[ 7])625+ MUL15(a[16], b[ 6])626+ MUL15(a[17], b[ 5])627+ MUL15(a[18], b[ 4])628+ MUL15(a[19], b[ 3]);629t[23] = MUL15(a[ 4], b[19])630+ MUL15(a[ 5], b[18])631+ MUL15(a[ 6], b[17])632+ MUL15(a[ 7], b[16])633+ MUL15(a[ 8], b[15])634+ MUL15(a[ 9], b[14])635+ MUL15(a[10], b[13])636+ MUL15(a[11], b[12])637+ MUL15(a[12], b[11])638+ MUL15(a[13], b[10])639+ MUL15(a[14], b[ 9])640+ MUL15(a[15], b[ 8])641+ MUL15(a[16], b[ 7])642+ MUL15(a[17], b[ 6])643+ MUL15(a[18], b[ 5])644+ MUL15(a[19], b[ 4]);645t[24] = MUL15(a[ 5], b[19])646+ MUL15(a[ 6], b[18])647+ MUL15(a[ 7], b[17])648+ MUL15(a[ 8], b[16])649+ MUL15(a[ 9], b[15])650+ MUL15(a[10], b[14])651+ MUL15(a[11], b[13])652+ MUL15(a[12], b[12])653+ MUL15(a[13], b[11])654+ MUL15(a[14], b[10])655+ MUL15(a[15], b[ 9])656+ MUL15(a[16], b[ 8])657+ MUL15(a[17], b[ 7])658+ MUL15(a[18], b[ 6])659+ MUL15(a[19], b[ 5]);660t[25] = MUL15(a[ 6], b[19])661+ MUL15(a[ 7], b[18])662+ MUL15(a[ 8], b[17])663+ MUL15(a[ 9], b[16])664+ MUL15(a[10], b[15])665+ MUL15(a[11], b[14])666+ MUL15(a[12], b[13])667+ MUL15(a[13], b[12])668+ MUL15(a[14], b[11])669+ MUL15(a[15], b[10])670+ MUL15(a[16], b[ 9])671+ MUL15(a[17], b[ 8])672+ MUL15(a[18], b[ 7])673+ MUL15(a[19], b[ 6]);674t[26] = MUL15(a[ 7], b[19])675+ MUL15(a[ 8], b[18])676+ MUL15(a[ 9], b[17])677+ MUL15(a[10], b[16])678+ MUL15(a[11], b[15])679+ MUL15(a[12], b[14])680+ MUL15(a[13], b[13])681+ MUL15(a[14], b[12])682+ MUL15(a[15], b[11])683+ MUL15(a[16], b[10])684+ MUL15(a[17], b[ 9])685+ MUL15(a[18], b[ 8])686+ MUL15(a[19], b[ 7]);687t[27] = MUL15(a[ 8], b[19])688+ MUL15(a[ 9], b[18])689+ MUL15(a[10], b[17])690+ MUL15(a[11], b[16])691+ MUL15(a[12], b[15])692+ MUL15(a[13], b[14])693+ MUL15(a[14], b[13])694+ MUL15(a[15], b[12])695+ MUL15(a[16], b[11])696+ MUL15(a[17], b[10])697+ MUL15(a[18], b[ 9])698+ MUL15(a[19], b[ 8]);699t[28] = MUL15(a[ 9], b[19])700+ MUL15(a[10], b[18])701+ MUL15(a[11], b[17])702+ MUL15(a[12], b[16])703+ MUL15(a[13], b[15])704+ MUL15(a[14], b[14])705+ MUL15(a[15], b[13])706+ MUL15(a[16], b[12])707+ MUL15(a[17], b[11])708+ MUL15(a[18], b[10])709+ MUL15(a[19], b[ 9]);710t[29] = MUL15(a[10], b[19])711+ MUL15(a[11], b[18])712+ MUL15(a[12], b[17])713+ MUL15(a[13], b[16])714+ MUL15(a[14], b[15])715+ MUL15(a[15], b[14])716+ MUL15(a[16], b[13])717+ MUL15(a[17], b[12])718+ MUL15(a[18], b[11])719+ MUL15(a[19], b[10]);720t[30] = MUL15(a[11], b[19])721+ MUL15(a[12], b[18])722+ MUL15(a[13], b[17])723+ MUL15(a[14], b[16])724+ MUL15(a[15], b[15])725+ MUL15(a[16], b[14])726+ MUL15(a[17], b[13])727+ MUL15(a[18], b[12])728+ MUL15(a[19], b[11]);729t[31] = MUL15(a[12], b[19])730+ MUL15(a[13], b[18])731+ MUL15(a[14], b[17])732+ MUL15(a[15], b[16])733+ MUL15(a[16], b[15])734+ MUL15(a[17], b[14])735+ MUL15(a[18], b[13])736+ MUL15(a[19], b[12]);737t[32] = MUL15(a[13], b[19])738+ MUL15(a[14], b[18])739+ MUL15(a[15], b[17])740+ MUL15(a[16], b[16])741+ MUL15(a[17], b[15])742+ MUL15(a[18], b[14])743+ MUL15(a[19], b[13]);744t[33] = MUL15(a[14], b[19])745+ MUL15(a[15], b[18])746+ MUL15(a[16], b[17])747+ MUL15(a[17], b[16])748+ MUL15(a[18], b[15])749+ MUL15(a[19], b[14]);750t[34] = MUL15(a[15], b[19])751+ MUL15(a[16], b[18])752+ MUL15(a[17], b[17])753+ MUL15(a[18], b[16])754+ MUL15(a[19], b[15]);755t[35] = MUL15(a[16], b[19])756+ MUL15(a[17], b[18])757+ MUL15(a[18], b[17])758+ MUL15(a[19], b[16]);759t[36] = MUL15(a[17], b[19])760+ MUL15(a[18], b[18])761+ MUL15(a[19], b[17]);762t[37] = MUL15(a[18], b[19])763+ MUL15(a[19], b[18]);764t[38] = MUL15(a[19], b[19]);765d[39] = norm13(d, t, 39);766}767768static void769square20(uint32_t *d, const uint32_t *a)770{771uint32_t t[39];772773t[ 0] = MUL15(a[ 0], a[ 0]);774t[ 1] = ((MUL15(a[ 0], a[ 1])) << 1);775t[ 2] = MUL15(a[ 1], a[ 1])776+ ((MUL15(a[ 0], a[ 2])) << 1);777t[ 3] = ((MUL15(a[ 0], a[ 3])778+ MUL15(a[ 1], a[ 2])) << 1);779t[ 4] = MUL15(a[ 2], a[ 2])780+ ((MUL15(a[ 0], a[ 4])781+ MUL15(a[ 1], a[ 3])) << 1);782t[ 5] = ((MUL15(a[ 0], a[ 5])783+ MUL15(a[ 1], a[ 4])784+ MUL15(a[ 2], a[ 3])) << 1);785t[ 6] = MUL15(a[ 3], a[ 3])786+ ((MUL15(a[ 0], a[ 6])787+ MUL15(a[ 1], a[ 5])788+ MUL15(a[ 2], a[ 4])) << 1);789t[ 7] = ((MUL15(a[ 0], a[ 7])790+ MUL15(a[ 1], a[ 6])791+ MUL15(a[ 2], a[ 5])792+ MUL15(a[ 3], a[ 4])) << 1);793t[ 8] = MUL15(a[ 4], a[ 4])794+ ((MUL15(a[ 0], a[ 8])795+ MUL15(a[ 1], a[ 7])796+ MUL15(a[ 2], a[ 6])797+ MUL15(a[ 3], a[ 5])) << 1);798t[ 9] = ((MUL15(a[ 0], a[ 9])799+ MUL15(a[ 1], a[ 8])800+ MUL15(a[ 2], a[ 7])801+ MUL15(a[ 3], a[ 6])802+ MUL15(a[ 4], a[ 5])) << 1);803t[10] = MUL15(a[ 5], a[ 5])804+ ((MUL15(a[ 0], a[10])805+ MUL15(a[ 1], a[ 9])806+ MUL15(a[ 2], a[ 8])807+ MUL15(a[ 3], a[ 7])808+ MUL15(a[ 4], a[ 6])) << 1);809t[11] = ((MUL15(a[ 0], a[11])810+ MUL15(a[ 1], a[10])811+ MUL15(a[ 2], a[ 9])812+ MUL15(a[ 3], a[ 8])813+ MUL15(a[ 4], a[ 7])814+ MUL15(a[ 5], a[ 6])) << 1);815t[12] = MUL15(a[ 6], a[ 6])816+ ((MUL15(a[ 0], a[12])817+ MUL15(a[ 1], a[11])818+ MUL15(a[ 2], a[10])819+ MUL15(a[ 3], a[ 9])820+ MUL15(a[ 4], a[ 8])821+ MUL15(a[ 5], a[ 7])) << 1);822t[13] = ((MUL15(a[ 0], a[13])823+ MUL15(a[ 1], a[12])824+ MUL15(a[ 2], a[11])825+ MUL15(a[ 3], a[10])826+ MUL15(a[ 4], a[ 9])827+ MUL15(a[ 5], a[ 8])828+ MUL15(a[ 6], a[ 7])) << 1);829t[14] = MUL15(a[ 7], a[ 7])830+ ((MUL15(a[ 0], a[14])831+ MUL15(a[ 1], a[13])832+ MUL15(a[ 2], a[12])833+ MUL15(a[ 3], a[11])834+ MUL15(a[ 4], a[10])835+ MUL15(a[ 5], a[ 9])836+ MUL15(a[ 6], a[ 8])) << 1);837t[15] = ((MUL15(a[ 0], a[15])838+ MUL15(a[ 1], a[14])839+ MUL15(a[ 2], a[13])840+ MUL15(a[ 3], a[12])841+ MUL15(a[ 4], a[11])842+ MUL15(a[ 5], a[10])843+ MUL15(a[ 6], a[ 9])844+ MUL15(a[ 7], a[ 8])) << 1);845t[16] = MUL15(a[ 8], a[ 8])846+ ((MUL15(a[ 0], a[16])847+ MUL15(a[ 1], a[15])848+ MUL15(a[ 2], a[14])849+ MUL15(a[ 3], a[13])850+ MUL15(a[ 4], a[12])851+ MUL15(a[ 5], a[11])852+ MUL15(a[ 6], a[10])853+ MUL15(a[ 7], a[ 9])) << 1);854t[17] = ((MUL15(a[ 0], a[17])855+ MUL15(a[ 1], a[16])856+ MUL15(a[ 2], a[15])857+ MUL15(a[ 3], a[14])858+ MUL15(a[ 4], a[13])859+ MUL15(a[ 5], a[12])860+ MUL15(a[ 6], a[11])861+ MUL15(a[ 7], a[10])862+ MUL15(a[ 8], a[ 9])) << 1);863t[18] = MUL15(a[ 9], a[ 9])864+ ((MUL15(a[ 0], a[18])865+ MUL15(a[ 1], a[17])866+ MUL15(a[ 2], a[16])867+ MUL15(a[ 3], a[15])868+ MUL15(a[ 4], a[14])869+ MUL15(a[ 5], a[13])870+ MUL15(a[ 6], a[12])871+ MUL15(a[ 7], a[11])872+ MUL15(a[ 8], a[10])) << 1);873t[19] = ((MUL15(a[ 0], a[19])874+ MUL15(a[ 1], a[18])875+ MUL15(a[ 2], a[17])876+ MUL15(a[ 3], a[16])877+ MUL15(a[ 4], a[15])878+ MUL15(a[ 5], a[14])879+ MUL15(a[ 6], a[13])880+ MUL15(a[ 7], a[12])881+ MUL15(a[ 8], a[11])882+ MUL15(a[ 9], a[10])) << 1);883t[20] = MUL15(a[10], a[10])884+ ((MUL15(a[ 1], a[19])885+ MUL15(a[ 2], a[18])886+ MUL15(a[ 3], a[17])887+ MUL15(a[ 4], a[16])888+ MUL15(a[ 5], a[15])889+ MUL15(a[ 6], a[14])890+ MUL15(a[ 7], a[13])891+ MUL15(a[ 8], a[12])892+ MUL15(a[ 9], a[11])) << 1);893t[21] = ((MUL15(a[ 2], a[19])894+ MUL15(a[ 3], a[18])895+ MUL15(a[ 4], a[17])896+ MUL15(a[ 5], a[16])897+ MUL15(a[ 6], a[15])898+ MUL15(a[ 7], a[14])899+ MUL15(a[ 8], a[13])900+ MUL15(a[ 9], a[12])901+ MUL15(a[10], a[11])) << 1);902t[22] = MUL15(a[11], a[11])903+ ((MUL15(a[ 3], a[19])904+ MUL15(a[ 4], a[18])905+ MUL15(a[ 5], a[17])906+ MUL15(a[ 6], a[16])907+ MUL15(a[ 7], a[15])908+ MUL15(a[ 8], a[14])909+ MUL15(a[ 9], a[13])910+ MUL15(a[10], a[12])) << 1);911t[23] = ((MUL15(a[ 4], a[19])912+ MUL15(a[ 5], a[18])913+ MUL15(a[ 6], a[17])914+ MUL15(a[ 7], a[16])915+ MUL15(a[ 8], a[15])916+ MUL15(a[ 9], a[14])917+ MUL15(a[10], a[13])918+ MUL15(a[11], a[12])) << 1);919t[24] = MUL15(a[12], a[12])920+ ((MUL15(a[ 5], a[19])921+ MUL15(a[ 6], a[18])922+ MUL15(a[ 7], a[17])923+ MUL15(a[ 8], a[16])924+ MUL15(a[ 9], a[15])925+ MUL15(a[10], a[14])926+ MUL15(a[11], a[13])) << 1);927t[25] = ((MUL15(a[ 6], a[19])928+ MUL15(a[ 7], a[18])929+ MUL15(a[ 8], a[17])930+ MUL15(a[ 9], a[16])931+ MUL15(a[10], a[15])932+ MUL15(a[11], a[14])933+ MUL15(a[12], a[13])) << 1);934t[26] = MUL15(a[13], a[13])935+ ((MUL15(a[ 7], a[19])936+ MUL15(a[ 8], a[18])937+ MUL15(a[ 9], a[17])938+ MUL15(a[10], a[16])939+ MUL15(a[11], a[15])940+ MUL15(a[12], a[14])) << 1);941t[27] = ((MUL15(a[ 8], a[19])942+ MUL15(a[ 9], a[18])943+ MUL15(a[10], a[17])944+ MUL15(a[11], a[16])945+ MUL15(a[12], a[15])946+ MUL15(a[13], a[14])) << 1);947t[28] = MUL15(a[14], a[14])948+ ((MUL15(a[ 9], a[19])949+ MUL15(a[10], a[18])950+ MUL15(a[11], a[17])951+ MUL15(a[12], a[16])952+ MUL15(a[13], a[15])) << 1);953t[29] = ((MUL15(a[10], a[19])954+ MUL15(a[11], a[18])955+ MUL15(a[12], a[17])956+ MUL15(a[13], a[16])957+ MUL15(a[14], a[15])) << 1);958t[30] = MUL15(a[15], a[15])959+ ((MUL15(a[11], a[19])960+ MUL15(a[12], a[18])961+ MUL15(a[13], a[17])962+ MUL15(a[14], a[16])) << 1);963t[31] = ((MUL15(a[12], a[19])964+ MUL15(a[13], a[18])965+ MUL15(a[14], a[17])966+ MUL15(a[15], a[16])) << 1);967t[32] = MUL15(a[16], a[16])968+ ((MUL15(a[13], a[19])969+ MUL15(a[14], a[18])970+ MUL15(a[15], a[17])) << 1);971t[33] = ((MUL15(a[14], a[19])972+ MUL15(a[15], a[18])973+ MUL15(a[16], a[17])) << 1);974t[34] = MUL15(a[17], a[17])975+ ((MUL15(a[15], a[19])976+ MUL15(a[16], a[18])) << 1);977t[35] = ((MUL15(a[16], a[19])978+ MUL15(a[17], a[18])) << 1);979t[36] = MUL15(a[18], a[18])980+ ((MUL15(a[17], a[19])) << 1);981t[37] = ((MUL15(a[18], a[19])) << 1);982t[38] = MUL15(a[19], a[19]);983d[39] = norm13(d, t, 39);984}985986#endif987988/*989* Modulus for field F256 (field for point coordinates in curve P-256).990*/991static const uint32_t F256[] = {9920x1FFF, 0x1FFF, 0x1FFF, 0x1FFF, 0x1FFF, 0x1FFF, 0x1FFF, 0x001F,9930x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0400, 0x0000,9940x0000, 0x1FF8, 0x1FFF, 0x01FF995};996997/*998* The 'b' curve equation coefficient for P-256.999*/1000static const uint32_t P256_B[] = {10010x004B, 0x1E93, 0x0F89, 0x1C78, 0x03BC, 0x187B, 0x114E, 0x1619,10020x1D06, 0x0328, 0x01AF, 0x0D31, 0x1557, 0x15DE, 0x1ECF, 0x127C,10030x0A3A, 0x0EC5, 0x118D, 0x00B51004};10051006/*1007* Perform a "short reduction" in field F256 (field for curve P-256).1008* The source value should be less than 262 bits; on output, it will1009* be at most 257 bits, and less than twice the modulus.1010*/1011static void1012reduce_f256(uint32_t *d)1013{1014uint32_t x;10151016x = d[19] >> 9;1017d[19] &= 0x01FF;1018d[17] += x << 3;1019d[14] -= x << 10;1020d[7] -= x << 5;1021d[0] += x;1022norm13(d, d, 20);1023}10241025/*1026* Perform a "final reduction" in field F256 (field for curve P-256).1027* The source value must be less than twice the modulus. If the value1028* is not lower than the modulus, then the modulus is subtracted and1029* this function returns 1; otherwise, it leaves it untouched and it1030* returns 0.1031*/1032static uint32_t1033reduce_final_f256(uint32_t *d)1034{1035uint32_t t[20];1036uint32_t cc;1037int i;10381039memcpy(t, d, sizeof t);1040cc = 0;1041for (i = 0; i < 20; i ++) {1042uint32_t w;10431044w = t[i] - F256[i] - cc;1045cc = w >> 31;1046t[i] = w & 0x1FFF;1047}1048cc ^= 1;1049CCOPY(cc, d, t, sizeof t);1050return cc;1051}10521053/*1054* Perform a multiplication of two integers modulo1055* 2^256-2^224+2^192+2^96-1 (for NIST curve P-256). Operands are arrays1056* of 20 words, each containing 13 bits of data, in little-endian order.1057* On input, upper word may be up to 13 bits (hence value up to 2^260-1);1058* on output, value fits on 257 bits and is lower than twice the modulus.1059*/1060static void1061mul_f256(uint32_t *d, const uint32_t *a, const uint32_t *b)1062{1063uint32_t t[40], cc;1064int i;10651066/*1067* Compute raw multiplication. All result words fit in 13 bits1068* each.1069*/1070mul20(t, a, b);10711072/*1073* Modular reduction: each high word in added/subtracted where1074* necessary.1075*1076* The modulus is:1077* p = 2^256 - 2^224 + 2^192 + 2^96 - 11078* Therefore:1079* 2^256 = 2^224 - 2^192 - 2^96 + 1 mod p1080*1081* For a word x at bit offset n (n >= 256), we have:1082* x*2^n = x*2^(n-32) - x*2^(n-64)1083* - x*2^(n - 160) + x*2^(n-256) mod p1084*1085* Thus, we can nullify the high word if we reinject it at some1086* proper emplacements.1087*/1088for (i = 39; i >= 20; i --) {1089uint32_t x;10901091x = t[i];1092t[i - 2] += ARSH(x, 6);1093t[i - 3] += (x << 7) & 0x1FFF;1094t[i - 4] -= ARSH(x, 12);1095t[i - 5] -= (x << 1) & 0x1FFF;1096t[i - 12] -= ARSH(x, 4);1097t[i - 13] -= (x << 9) & 0x1FFF;1098t[i - 19] += ARSH(x, 9);1099t[i - 20] += (x << 4) & 0x1FFF;1100}11011102/*1103* Propagate carries. This is a signed propagation, and the1104* result may be negative. The loop above may enlarge values,1105* but not two much: worst case is the chain involving t[i - 3],1106* in which a value may be added to itself up to 7 times. Since1107* starting values are 13-bit each, all words fit on 20 bits1108* (21 to account for the sign bit).1109*/1110cc = norm13(t, t, 20);11111112/*1113* Perform modular reduction again for the bits beyond 256 (the carry1114* and the bits 256..259). Since the largest shift below is by 101115* bits, and the values fit on 21 bits, values fit in 32-bit words,1116* thereby allowing injecting full word values.1117*/1118cc = (cc << 4) | (t[19] >> 9);1119t[19] &= 0x01FF;1120t[17] += cc << 3;1121t[14] -= cc << 10;1122t[7] -= cc << 5;1123t[0] += cc;11241125/*1126* If the carry is negative, then after carry propagation, we may1127* end up with a value which is negative, and we don't want that.1128* Thus, in that case, we add the modulus. Note that the subtraction1129* result, when the carry is negative, is always smaller than the1130* modulus, so the extra addition will not make the value exceed1131* twice the modulus.1132*/1133cc >>= 31;1134t[0] -= cc;1135t[7] += cc << 5;1136t[14] += cc << 10;1137t[17] -= cc << 3;1138t[19] += cc << 9;11391140norm13(d, t, 20);1141}11421143/*1144* Square an integer modulo 2^256-2^224+2^192+2^96-1 (for NIST curve1145* P-256). Operand is an array of 20 words, each containing 13 bits of1146* data, in little-endian order. On input, upper word may be up to 131147* bits (hence value up to 2^260-1); on output, value fits on 257 bits1148* and is lower than twice the modulus.1149*/1150static void1151square_f256(uint32_t *d, const uint32_t *a)1152{1153uint32_t t[40], cc;1154int i;11551156/*1157* Compute raw square. All result words fit in 13 bits each.1158*/1159square20(t, a);11601161/*1162* Modular reduction: each high word in added/subtracted where1163* necessary.1164*1165* The modulus is:1166* p = 2^256 - 2^224 + 2^192 + 2^96 - 11167* Therefore:1168* 2^256 = 2^224 - 2^192 - 2^96 + 1 mod p1169*1170* For a word x at bit offset n (n >= 256), we have:1171* x*2^n = x*2^(n-32) - x*2^(n-64)1172* - x*2^(n - 160) + x*2^(n-256) mod p1173*1174* Thus, we can nullify the high word if we reinject it at some1175* proper emplacements.1176*/1177for (i = 39; i >= 20; i --) {1178uint32_t x;11791180x = t[i];1181t[i - 2] += ARSH(x, 6);1182t[i - 3] += (x << 7) & 0x1FFF;1183t[i - 4] -= ARSH(x, 12);1184t[i - 5] -= (x << 1) & 0x1FFF;1185t[i - 12] -= ARSH(x, 4);1186t[i - 13] -= (x << 9) & 0x1FFF;1187t[i - 19] += ARSH(x, 9);1188t[i - 20] += (x << 4) & 0x1FFF;1189}11901191/*1192* Propagate carries. This is a signed propagation, and the1193* result may be negative. The loop above may enlarge values,1194* but not two much: worst case is the chain involving t[i - 3],1195* in which a value may be added to itself up to 7 times. Since1196* starting values are 13-bit each, all words fit on 20 bits1197* (21 to account for the sign bit).1198*/1199cc = norm13(t, t, 20);12001201/*1202* Perform modular reduction again for the bits beyond 256 (the carry1203* and the bits 256..259). Since the largest shift below is by 101204* bits, and the values fit on 21 bits, values fit in 32-bit words,1205* thereby allowing injecting full word values.1206*/1207cc = (cc << 4) | (t[19] >> 9);1208t[19] &= 0x01FF;1209t[17] += cc << 3;1210t[14] -= cc << 10;1211t[7] -= cc << 5;1212t[0] += cc;12131214/*1215* If the carry is negative, then after carry propagation, we may1216* end up with a value which is negative, and we don't want that.1217* Thus, in that case, we add the modulus. Note that the subtraction1218* result, when the carry is negative, is always smaller than the1219* modulus, so the extra addition will not make the value exceed1220* twice the modulus.1221*/1222cc >>= 31;1223t[0] -= cc;1224t[7] += cc << 5;1225t[14] += cc << 10;1226t[17] -= cc << 3;1227t[19] += cc << 9;12281229norm13(d, t, 20);1230}12311232/*1233* Jacobian coordinates for a point in P-256: affine coordinates (X,Y)1234* are such that:1235* X = x / z^21236* Y = y / z^31237* For the point at infinity, z = 0.1238* Each point thus admits many possible representations.1239*1240* Coordinates are represented in arrays of 32-bit integers, each holding1241* 13 bits of data. Values may also be slightly greater than the modulus,1242* but they will always be lower than twice the modulus.1243*/1244typedef struct {1245uint32_t x[20];1246uint32_t y[20];1247uint32_t z[20];1248} p256_jacobian;12491250/*1251* Convert a point to affine coordinates:1252* - If the point is the point at infinity, then all three coordinates1253* are set to 0.1254* - Otherwise, the 'z' coordinate is set to 1, and the 'x' and 'y'1255* coordinates are the 'X' and 'Y' affine coordinates.1256* The coordinates are guaranteed to be lower than the modulus.1257*/1258static void1259p256_to_affine(p256_jacobian *P)1260{1261uint32_t t1[20], t2[20];1262int i;12631264/*1265* Invert z with a modular exponentiation: the modulus is1266* p = 2^256 - 2^224 + 2^192 + 2^96 - 1, and the exponent is1267* p-2. Exponent bit pattern (from high to low) is:1268* - 32 bits of value 11269* - 31 bits of value 01270* - 1 bit of value 11271* - 96 bits of value 01272* - 94 bits of value 11273* - 1 bit of value 01274* - 1 bit of value 11275* Thus, we precompute z^(2^31-1) to speed things up.1276*1277* If z = 0 (point at infinity) then the modular exponentiation1278* will yield 0, which leads to the expected result (all three1279* coordinates set to 0).1280*/12811282/*1283* A simple square-and-multiply for z^(2^31-1). We could save about1284* two dozen multiplications here with an addition chain, but1285* this would require a bit more code, and extra stack buffers.1286*/1287memcpy(t1, P->z, sizeof P->z);1288for (i = 0; i < 30; i ++) {1289square_f256(t1, t1);1290mul_f256(t1, t1, P->z);1291}12921293/*1294* Square-and-multiply. Apart from the squarings, we have a few1295* multiplications to set bits to 1; we multiply by the original z1296* for setting 1 bit, and by t1 for setting 31 bits.1297*/1298memcpy(t2, P->z, sizeof P->z);1299for (i = 1; i < 256; i ++) {1300square_f256(t2, t2);1301switch (i) {1302case 31:1303case 190:1304case 221:1305case 252:1306mul_f256(t2, t2, t1);1307break;1308case 63:1309case 253:1310case 255:1311mul_f256(t2, t2, P->z);1312break;1313}1314}13151316/*1317* Now that we have 1/z, multiply x by 1/z^2 and y by 1/z^3.1318*/1319mul_f256(t1, t2, t2);1320mul_f256(P->x, t1, P->x);1321mul_f256(t1, t1, t2);1322mul_f256(P->y, t1, P->y);1323reduce_final_f256(P->x);1324reduce_final_f256(P->y);13251326/*1327* Multiply z by 1/z. If z = 0, then this will yield 0, otherwise1328* this will set z to 1.1329*/1330mul_f256(P->z, P->z, t2);1331reduce_final_f256(P->z);1332}13331334/*1335* Double a point in P-256. This function works for all valid points,1336* including the point at infinity.1337*/1338static void1339p256_double(p256_jacobian *Q)1340{1341/*1342* Doubling formulas are:1343*1344* s = 4*x*y^21345* m = 3*(x + z^2)*(x - z^2)1346* x' = m^2 - 2*s1347* y' = m*(s - x') - 8*y^41348* z' = 2*y*z1349*1350* These formulas work for all points, including points of order 21351* and points at infinity:1352* - If y = 0 then z' = 0. But there is no such point in P-2561353* anyway.1354* - If z = 0 then z' = 0.1355*/1356uint32_t t1[20], t2[20], t3[20], t4[20];1357int i;13581359/*1360* Compute z^2 in t1.1361*/1362square_f256(t1, Q->z);13631364/*1365* Compute x-z^2 in t2 and x+z^2 in t1.1366*/1367for (i = 0; i < 20; i ++) {1368t2[i] = (F256[i] << 1) + Q->x[i] - t1[i];1369t1[i] += Q->x[i];1370}1371norm13(t1, t1, 20);1372norm13(t2, t2, 20);13731374/*1375* Compute 3*(x+z^2)*(x-z^2) in t1.1376*/1377mul_f256(t3, t1, t2);1378for (i = 0; i < 20; i ++) {1379t1[i] = MUL15(3, t3[i]);1380}1381norm13(t1, t1, 20);13821383/*1384* Compute 4*x*y^2 (in t2) and 2*y^2 (in t3).1385*/1386square_f256(t3, Q->y);1387for (i = 0; i < 20; i ++) {1388t3[i] <<= 1;1389}1390norm13(t3, t3, 20);1391mul_f256(t2, Q->x, t3);1392for (i = 0; i < 20; i ++) {1393t2[i] <<= 1;1394}1395norm13(t2, t2, 20);1396reduce_f256(t2);13971398/*1399* Compute x' = m^2 - 2*s.1400*/1401square_f256(Q->x, t1);1402for (i = 0; i < 20; i ++) {1403Q->x[i] += (F256[i] << 2) - (t2[i] << 1);1404}1405norm13(Q->x, Q->x, 20);1406reduce_f256(Q->x);14071408/*1409* Compute z' = 2*y*z.1410*/1411mul_f256(t4, Q->y, Q->z);1412for (i = 0; i < 20; i ++) {1413Q->z[i] = t4[i] << 1;1414}1415norm13(Q->z, Q->z, 20);1416reduce_f256(Q->z);14171418/*1419* Compute y' = m*(s - x') - 8*y^4. Note that we already have1420* 2*y^2 in t3.1421*/1422for (i = 0; i < 20; i ++) {1423t2[i] += (F256[i] << 1) - Q->x[i];1424}1425norm13(t2, t2, 20);1426mul_f256(Q->y, t1, t2);1427square_f256(t4, t3);1428for (i = 0; i < 20; i ++) {1429Q->y[i] += (F256[i] << 2) - (t4[i] << 1);1430}1431norm13(Q->y, Q->y, 20);1432reduce_f256(Q->y);1433}14341435/*1436* Add point P2 to point P1.1437*1438* This function computes the wrong result in the following cases:1439*1440* - If P1 == 0 but P2 != 01441* - If P1 != 0 but P2 == 01442* - If P1 == P21443*1444* In all three cases, P1 is set to the point at infinity.1445*1446* Returned value is 0 if one of the following occurs:1447*1448* - P1 and P2 have the same Y coordinate1449* - P1 == 0 and P2 == 01450* - The Y coordinate of one of the points is 0 and the other point is1451* the point at infinity.1452*1453* The third case cannot actually happen with valid points, since a point1454* with Y == 0 is a point of order 2, and there is no point of order 2 on1455* curve P-256.1456*1457* Therefore, assuming that P1 != 0 and P2 != 0 on input, then the caller1458* can apply the following:1459*1460* - If the result is not the point at infinity, then it is correct.1461* - Otherwise, if the returned value is 1, then this is a case of1462* P1+P2 == 0, so the result is indeed the point at infinity.1463* - Otherwise, P1 == P2, so a "double" operation should have been1464* performed.1465*/1466static uint32_t1467p256_add(p256_jacobian *P1, const p256_jacobian *P2)1468{1469/*1470* Addtions formulas are:1471*1472* u1 = x1 * z2^21473* u2 = x2 * z1^21474* s1 = y1 * z2^31475* s2 = y2 * z1^31476* h = u2 - u11477* r = s2 - s11478* x3 = r^2 - h^3 - 2 * u1 * h^21479* y3 = r * (u1 * h^2 - x3) - s1 * h^31480* z3 = h * z1 * z21481*/1482uint32_t t1[20], t2[20], t3[20], t4[20], t5[20], t6[20], t7[20];1483uint32_t ret;1484int i;14851486/*1487* Compute u1 = x1*z2^2 (in t1) and s1 = y1*z2^3 (in t3).1488*/1489square_f256(t3, P2->z);1490mul_f256(t1, P1->x, t3);1491mul_f256(t4, P2->z, t3);1492mul_f256(t3, P1->y, t4);14931494/*1495* Compute u2 = x2*z1^2 (in t2) and s2 = y2*z1^3 (in t4).1496*/1497square_f256(t4, P1->z);1498mul_f256(t2, P2->x, t4);1499mul_f256(t5, P1->z, t4);1500mul_f256(t4, P2->y, t5);15011502/*1503* Compute h = h2 - u1 (in t2) and r = s2 - s1 (in t4).1504* We need to test whether r is zero, so we will do some extra1505* reduce.1506*/1507for (i = 0; i < 20; i ++) {1508t2[i] += (F256[i] << 1) - t1[i];1509t4[i] += (F256[i] << 1) - t3[i];1510}1511norm13(t2, t2, 20);1512norm13(t4, t4, 20);1513reduce_f256(t4);1514reduce_final_f256(t4);1515ret = 0;1516for (i = 0; i < 20; i ++) {1517ret |= t4[i];1518}1519ret = (ret | -ret) >> 31;15201521/*1522* Compute u1*h^2 (in t6) and h^3 (in t5);1523*/1524square_f256(t7, t2);1525mul_f256(t6, t1, t7);1526mul_f256(t5, t7, t2);15271528/*1529* Compute x3 = r^2 - h^3 - 2*u1*h^2.1530*/1531square_f256(P1->x, t4);1532for (i = 0; i < 20; i ++) {1533P1->x[i] += (F256[i] << 3) - t5[i] - (t6[i] << 1);1534}1535norm13(P1->x, P1->x, 20);1536reduce_f256(P1->x);15371538/*1539* Compute y3 = r*(u1*h^2 - x3) - s1*h^3.1540*/1541for (i = 0; i < 20; i ++) {1542t6[i] += (F256[i] << 1) - P1->x[i];1543}1544norm13(t6, t6, 20);1545mul_f256(P1->y, t4, t6);1546mul_f256(t1, t5, t3);1547for (i = 0; i < 20; i ++) {1548P1->y[i] += (F256[i] << 1) - t1[i];1549}1550norm13(P1->y, P1->y, 20);1551reduce_f256(P1->y);15521553/*1554* Compute z3 = h*z1*z2.1555*/1556mul_f256(t1, P1->z, P2->z);1557mul_f256(P1->z, t1, t2);15581559return ret;1560}15611562/*1563* Add point P2 to point P1. This is a specialised function for the1564* case when P2 is a non-zero point in affine coordinate.1565*1566* This function computes the wrong result in the following cases:1567*1568* - If P1 == 01569* - If P1 == P21570*1571* In both cases, P1 is set to the point at infinity.1572*1573* Returned value is 0 if one of the following occurs:1574*1575* - P1 and P2 have the same Y coordinate1576* - The Y coordinate of P2 is 0 and P1 is the point at infinity.1577*1578* The second case cannot actually happen with valid points, since a point1579* with Y == 0 is a point of order 2, and there is no point of order 2 on1580* curve P-256.1581*1582* Therefore, assuming that P1 != 0 on input, then the caller1583* can apply the following:1584*1585* - If the result is not the point at infinity, then it is correct.1586* - Otherwise, if the returned value is 1, then this is a case of1587* P1+P2 == 0, so the result is indeed the point at infinity.1588* - Otherwise, P1 == P2, so a "double" operation should have been1589* performed.1590*/1591static uint32_t1592p256_add_mixed(p256_jacobian *P1, const p256_jacobian *P2)1593{1594/*1595* Addtions formulas are:1596*1597* u1 = x11598* u2 = x2 * z1^21599* s1 = y11600* s2 = y2 * z1^31601* h = u2 - u11602* r = s2 - s11603* x3 = r^2 - h^3 - 2 * u1 * h^21604* y3 = r * (u1 * h^2 - x3) - s1 * h^31605* z3 = h * z11606*/1607uint32_t t1[20], t2[20], t3[20], t4[20], t5[20], t6[20], t7[20];1608uint32_t ret;1609int i;16101611/*1612* Compute u1 = x1 (in t1) and s1 = y1 (in t3).1613*/1614memcpy(t1, P1->x, sizeof t1);1615memcpy(t3, P1->y, sizeof t3);16161617/*1618* Compute u2 = x2*z1^2 (in t2) and s2 = y2*z1^3 (in t4).1619*/1620square_f256(t4, P1->z);1621mul_f256(t2, P2->x, t4);1622mul_f256(t5, P1->z, t4);1623mul_f256(t4, P2->y, t5);16241625/*1626* Compute h = h2 - u1 (in t2) and r = s2 - s1 (in t4).1627* We need to test whether r is zero, so we will do some extra1628* reduce.1629*/1630for (i = 0; i < 20; i ++) {1631t2[i] += (F256[i] << 1) - t1[i];1632t4[i] += (F256[i] << 1) - t3[i];1633}1634norm13(t2, t2, 20);1635norm13(t4, t4, 20);1636reduce_f256(t4);1637reduce_final_f256(t4);1638ret = 0;1639for (i = 0; i < 20; i ++) {1640ret |= t4[i];1641}1642ret = (ret | -ret) >> 31;16431644/*1645* Compute u1*h^2 (in t6) and h^3 (in t5);1646*/1647square_f256(t7, t2);1648mul_f256(t6, t1, t7);1649mul_f256(t5, t7, t2);16501651/*1652* Compute x3 = r^2 - h^3 - 2*u1*h^2.1653*/1654square_f256(P1->x, t4);1655for (i = 0; i < 20; i ++) {1656P1->x[i] += (F256[i] << 3) - t5[i] - (t6[i] << 1);1657}1658norm13(P1->x, P1->x, 20);1659reduce_f256(P1->x);16601661/*1662* Compute y3 = r*(u1*h^2 - x3) - s1*h^3.1663*/1664for (i = 0; i < 20; i ++) {1665t6[i] += (F256[i] << 1) - P1->x[i];1666}1667norm13(t6, t6, 20);1668mul_f256(P1->y, t4, t6);1669mul_f256(t1, t5, t3);1670for (i = 0; i < 20; i ++) {1671P1->y[i] += (F256[i] << 1) - t1[i];1672}1673norm13(P1->y, P1->y, 20);1674reduce_f256(P1->y);16751676/*1677* Compute z3 = h*z1*z2.1678*/1679mul_f256(P1->z, P1->z, t2);16801681return ret;1682}16831684/*1685* Decode a P-256 point. This function does not support the point at1686* infinity. Returned value is 0 if the point is invalid, 1 otherwise.1687*/1688static uint32_t1689p256_decode(p256_jacobian *P, const void *src, size_t len)1690{1691const unsigned char *buf;1692uint32_t tx[20], ty[20], t1[20], t2[20];1693uint32_t bad;1694int i;16951696if (len != 65) {1697return 0;1698}1699buf = src;17001701/*1702* First byte must be 0x04 (uncompressed format). We could support1703* "hybrid format" (first byte is 0x06 or 0x07, and encodes the1704* least significant bit of the Y coordinate), but it is explicitly1705* forbidden by RFC 5480 (section 2.2).1706*/1707bad = NEQ(buf[0], 0x04);17081709/*1710* Decode the coordinates, and check that they are both lower1711* than the modulus.1712*/1713tx[19] = be8_to_le13(tx, buf + 1, 32);1714ty[19] = be8_to_le13(ty, buf + 33, 32);1715bad |= reduce_final_f256(tx);1716bad |= reduce_final_f256(ty);17171718/*1719* Check curve equation.1720*/1721square_f256(t1, tx);1722mul_f256(t1, tx, t1);1723square_f256(t2, ty);1724for (i = 0; i < 20; i ++) {1725t1[i] += (F256[i] << 3) - MUL15(3, tx[i]) + P256_B[i] - t2[i];1726}1727norm13(t1, t1, 20);1728reduce_f256(t1);1729reduce_final_f256(t1);1730for (i = 0; i < 20; i ++) {1731bad |= t1[i];1732}17331734/*1735* Copy coordinates to the point structure.1736*/1737memcpy(P->x, tx, sizeof tx);1738memcpy(P->y, ty, sizeof ty);1739memset(P->z, 0, sizeof P->z);1740P->z[0] = 1;1741return EQ(bad, 0);1742}17431744/*1745* Encode a point into a buffer. This function assumes that the point is1746* valid, in affine coordinates, and not the point at infinity.1747*/1748static void1749p256_encode(void *dst, const p256_jacobian *P)1750{1751unsigned char *buf;17521753buf = dst;1754buf[0] = 0x04;1755le13_to_be8(buf + 1, 32, P->x);1756le13_to_be8(buf + 33, 32, P->y);1757}17581759/*1760* Multiply a curve point by an integer. The integer is assumed to be1761* lower than the curve order, and the base point must not be the point1762* at infinity.1763*/1764static void1765p256_mul(p256_jacobian *P, const unsigned char *x, size_t xlen)1766{1767/*1768* qz is a flag that is initially 1, and remains equal to 11769* as long as the point is the point at infinity.1770*1771* We use a 2-bit window to handle multiplier bits by pairs.1772* The precomputed window really is the points P2 and P3.1773*/1774uint32_t qz;1775p256_jacobian P2, P3, Q, T, U;17761777/*1778* Compute window values.1779*/1780P2 = *P;1781p256_double(&P2);1782P3 = *P;1783p256_add(&P3, &P2);17841785/*1786* We start with Q = 0. We process multiplier bits 2 by 2.1787*/1788memset(&Q, 0, sizeof Q);1789qz = 1;1790while (xlen -- > 0) {1791int k;17921793for (k = 6; k >= 0; k -= 2) {1794uint32_t bits;1795uint32_t bnz;17961797p256_double(&Q);1798p256_double(&Q);1799T = *P;1800U = Q;1801bits = (*x >> k) & (uint32_t)3;1802bnz = NEQ(bits, 0);1803CCOPY(EQ(bits, 2), &T, &P2, sizeof T);1804CCOPY(EQ(bits, 3), &T, &P3, sizeof T);1805p256_add(&U, &T);1806CCOPY(bnz & qz, &Q, &T, sizeof Q);1807CCOPY(bnz & ~qz, &Q, &U, sizeof Q);1808qz &= ~bnz;1809}1810x ++;1811}1812*P = Q;1813}18141815/*1816* Precomputed window: k*G points, where G is the curve generator, and k1817* is an integer from 1 to 15 (inclusive). The X and Y coordinates of1818* the point are encoded as 20 words of 13 bits each (little-endian1819* order); 13-bit words are then grouped 2-by-2 into 32-bit words1820* (little-endian order within each word).1821*/1822static const uint32_t Gwin[15][20] = {18231824{ 0x04C60296, 0x02721176, 0x19D00F4A, 0x102517AC,18250x13B8037D, 0x0748103C, 0x1E730E56, 0x08481FE2,18260x0F97012C, 0x00D605F4, 0x1DFA11F5, 0x0C801A0D,18270x0F670CBB, 0x0AED0CC5, 0x115E0E33, 0x181F0785,18280x13F514A7, 0x0FF30E3B, 0x17171E1A, 0x009F18D0 },18291830{ 0x1B341978, 0x16911F11, 0x0D9A1A60, 0x1C4E1FC8,18310x1E040969, 0x096A06B0, 0x091C0030, 0x09EF1A29,18320x18C40D03, 0x00F91C9E, 0x13C313D1, 0x096F0748,18330x011419E0, 0x1CC713A6, 0x1DD31DAD, 0x1EE80C36,18340x1ECD0C69, 0x1A0800A4, 0x08861B8E, 0x000E1DD5 },18351836{ 0x173F1D6C, 0x02CC06F1, 0x14C21FB4, 0x043D1EB6,18370x0F3606B7, 0x1A971C59, 0x1BF71951, 0x01481323,18380x068D0633, 0x00BD12F9, 0x13EA1032, 0x136209E8,18390x1C1E19A7, 0x06C7013E, 0x06C10AB0, 0x14C908BB,18400x05830CE1, 0x1FEF18DD, 0x00620998, 0x010E0D19 },18411842{ 0x18180852, 0x0604111A, 0x0B771509, 0x1B6F0156,18430x00181FE2, 0x1DCC0AF4, 0x16EF0659, 0x11F70E80,18440x11A912D0, 0x01C414D2, 0x027618C6, 0x05840FC6,18450x100215C4, 0x187E0C3B, 0x12771C96, 0x150C0B5D,18460x0FF705FD, 0x07981C67, 0x1AD20C63, 0x01C11C55 },18471848{ 0x1E8113ED, 0x0A940370, 0x12920215, 0x1FA31D6F,18490x1F7C0C82, 0x10CD03F7, 0x02640560, 0x081A0B5E,18500x1BD21151, 0x00A21642, 0x0D0B0DA4, 0x0176113F,18510x04440D1D, 0x001A1360, 0x1068012F, 0x1F141E49,18520x10DF136B, 0x0E4F162B, 0x0D44104A, 0x01C1105F },18531854{ 0x011411A9, 0x01551A4F, 0x0ADA0C6B, 0x01BD0EC8,18550x18120C74, 0x112F1778, 0x099202CB, 0x0C05124B,18560x195316A4, 0x01600685, 0x1E3B1FE2, 0x189014E3,18570x0B5E1FD7, 0x0E0311F8, 0x08E000F7, 0x174E00DE,18580x160702DF, 0x1B5A15BF, 0x03A11237, 0x01D01704 },18591860{ 0x0C3D12A3, 0x0C501C0C, 0x17AD1300, 0x1715003F,18610x03F719F8, 0x18031ED8, 0x1D980667, 0x0F681896,18620x1B7D00BF, 0x011C14CE, 0x0FA000B4, 0x1C3501B0,18630x0D901C55, 0x06790C10, 0x029E0736, 0x0DEB0400,18640x034F183A, 0x030619B4, 0x0DEF0033, 0x00E71AC7 },18651866{ 0x1B7D1393, 0x1B3B1076, 0x0BED1B4D, 0x13011F3A,18670x0E0E1238, 0x156A132B, 0x013A02D3, 0x160A0D01,18680x1CED1EE9, 0x00C5165D, 0x184C157E, 0x08141A83,18690x153C0DA5, 0x1ED70F9D, 0x05170D51, 0x02CF13B8,18700x18AE1771, 0x1B04113F, 0x05EC11E9, 0x015A16B3 },18711872{ 0x04A41EE0, 0x1D1412E4, 0x1C591D79, 0x118511B7,18730x14F00ACB, 0x1AE31E1C, 0x049C0D51, 0x016E061E,18740x1DB71EDF, 0x01D41A35, 0x0E8208FA, 0x14441293,18750x011F1E85, 0x1D54137A, 0x026B114F, 0x151D0832,18760x00A50964, 0x1F9C1E1C, 0x064B12C9, 0x005409D1 },18771878{ 0x062B123F, 0x0C0D0501, 0x183704C3, 0x08E31120,18790x0A2E0A6C, 0x14440FED, 0x090A0D1E, 0x13271964,18800x0B590A3A, 0x019D1D9B, 0x05780773, 0x09770A91,18810x0F770CA3, 0x053F19D4, 0x02C80DED, 0x1A761304,18820x091E0DD9, 0x15D201B8, 0x151109AA, 0x010F0198 },18831884{ 0x05E101D1, 0x072314DD, 0x045F1433, 0x1A041541,18850x10B3142E, 0x01840736, 0x1C1B19DB, 0x098B0418,18860x1DBC083B, 0x007D1444, 0x01511740, 0x11DD1F3A,18870x04ED0E2F, 0x1B4B1A62, 0x10480D04, 0x09E911A2,18880x04211AFA, 0x19140893, 0x04D60CC4, 0x01210648 },18891890{ 0x112703C4, 0x018B1BA1, 0x164C1D50, 0x05160BE0,18910x0BCC1830, 0x01CB1554, 0x13291732, 0x1B2B1918,18920x0DED0817, 0x00E80775, 0x0A2401D3, 0x0BFE08B3,18930x0E531199, 0x058616E9, 0x04770B91, 0x110F0C55,18940x19C11554, 0x0BFB1159, 0x03541C38, 0x000E1C2D },18951896{ 0x10390C01, 0x02BB0751, 0x0AC5098E, 0x096C17AB,18970x03C90E28, 0x10BD18BF, 0x002E1F2D, 0x092B0986,18980x1BD700AC, 0x002E1F20, 0x1E3D1FD8, 0x077718BB,18990x06F919C4, 0x187407ED, 0x11370E14, 0x081E139C,19000x00481ADB, 0x14AB0289, 0x066A0EBE, 0x00C70ED6 },19011902{ 0x0694120B, 0x124E1CC9, 0x0E2F0570, 0x17CF081A,19030x078906AC, 0x066D17CF, 0x1B3207F4, 0x0C5705E9,19040x10001C38, 0x00A919DE, 0x06851375, 0x0F900BD8,19050x080401BA, 0x0EEE0D42, 0x1B8B11EA, 0x0B4519F0,19060x090F18C0, 0x062E1508, 0x0DD909F4, 0x01EB067C },19071908{ 0x0CDC1D5F, 0x0D1818F9, 0x07781636, 0x125B18E8,19090x0D7003AF, 0x13110099, 0x1D9B1899, 0x175C1EB7,19100x0E34171A, 0x01E01153, 0x081A0F36, 0x0B391783,19110x1D1F147E, 0x19CE16D7, 0x11511B21, 0x1F2C10F9,19120x12CA0E51, 0x05A31D39, 0x171A192E, 0x016B0E4F }1913};19141915/*1916* Lookup one of the Gwin[] values, by index. This is constant-time.1917*/1918static void1919lookup_Gwin(p256_jacobian *T, uint32_t idx)1920{1921uint32_t xy[20];1922uint32_t k;1923size_t u;19241925memset(xy, 0, sizeof xy);1926for (k = 0; k < 15; k ++) {1927uint32_t m;19281929m = -EQ(idx, k + 1);1930for (u = 0; u < 20; u ++) {1931xy[u] |= m & Gwin[k][u];1932}1933}1934for (u = 0; u < 10; u ++) {1935T->x[(u << 1) + 0] = xy[u] & 0xFFFF;1936T->x[(u << 1) + 1] = xy[u] >> 16;1937T->y[(u << 1) + 0] = xy[u + 10] & 0xFFFF;1938T->y[(u << 1) + 1] = xy[u + 10] >> 16;1939}1940memset(T->z, 0, sizeof T->z);1941T->z[0] = 1;1942}19431944/*1945* Multiply the generator by an integer. The integer is assumed non-zero1946* and lower than the curve order.1947*/1948static void1949p256_mulgen(p256_jacobian *P, const unsigned char *x, size_t xlen)1950{1951/*1952* qz is a flag that is initially 1, and remains equal to 11953* as long as the point is the point at infinity.1954*1955* We use a 4-bit window to handle multiplier bits by groups1956* of 4. The precomputed window is constant static data, with1957* points in affine coordinates; we use a constant-time lookup.1958*/1959p256_jacobian Q;1960uint32_t qz;19611962memset(&Q, 0, sizeof Q);1963qz = 1;1964while (xlen -- > 0) {1965int k;1966unsigned bx;19671968bx = *x ++;1969for (k = 0; k < 2; k ++) {1970uint32_t bits;1971uint32_t bnz;1972p256_jacobian T, U;19731974p256_double(&Q);1975p256_double(&Q);1976p256_double(&Q);1977p256_double(&Q);1978bits = (bx >> 4) & 0x0F;1979bnz = NEQ(bits, 0);1980lookup_Gwin(&T, bits);1981U = Q;1982p256_add_mixed(&U, &T);1983CCOPY(bnz & qz, &Q, &T, sizeof Q);1984CCOPY(bnz & ~qz, &Q, &U, sizeof Q);1985qz &= ~bnz;1986bx <<= 4;1987}1988}1989*P = Q;1990}19911992static const unsigned char P256_G[] = {19930x04, 0x6B, 0x17, 0xD1, 0xF2, 0xE1, 0x2C, 0x42, 0x47, 0xF8,19940xBC, 0xE6, 0xE5, 0x63, 0xA4, 0x40, 0xF2, 0x77, 0x03, 0x7D,19950x81, 0x2D, 0xEB, 0x33, 0xA0, 0xF4, 0xA1, 0x39, 0x45, 0xD8,19960x98, 0xC2, 0x96, 0x4F, 0xE3, 0x42, 0xE2, 0xFE, 0x1A, 0x7F,19970x9B, 0x8E, 0xE7, 0xEB, 0x4A, 0x7C, 0x0F, 0x9E, 0x16, 0x2B,19980xCE, 0x33, 0x57, 0x6B, 0x31, 0x5E, 0xCE, 0xCB, 0xB6, 0x40,19990x68, 0x37, 0xBF, 0x51, 0xF52000};20012002static const unsigned char P256_N[] = {20030xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF,20040xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xBC, 0xE6, 0xFA, 0xAD,20050xA7, 0x17, 0x9E, 0x84, 0xF3, 0xB9, 0xCA, 0xC2, 0xFC, 0x63,20060x25, 0x512007};20082009static const unsigned char *2010api_generator(int curve, size_t *len)2011{2012(void)curve;2013*len = sizeof P256_G;2014return P256_G;2015}20162017static const unsigned char *2018api_order(int curve, size_t *len)2019{2020(void)curve;2021*len = sizeof P256_N;2022return P256_N;2023}20242025static size_t2026api_xoff(int curve, size_t *len)2027{2028(void)curve;2029*len = 32;2030return 1;2031}20322033static uint32_t2034api_mul(unsigned char *G, size_t Glen,2035const unsigned char *x, size_t xlen, int curve)2036{2037uint32_t r;2038p256_jacobian P;20392040(void)curve;2041if (Glen != 65) {2042return 0;2043}2044r = p256_decode(&P, G, Glen);2045p256_mul(&P, x, xlen);2046p256_to_affine(&P);2047p256_encode(G, &P);2048return r;2049}20502051static size_t2052api_mulgen(unsigned char *R,2053const unsigned char *x, size_t xlen, int curve)2054{2055p256_jacobian P;20562057(void)curve;2058p256_mulgen(&P, x, xlen);2059p256_to_affine(&P);2060p256_encode(R, &P);2061return 65;2062}20632064static uint32_t2065api_muladd(unsigned char *A, const unsigned char *B, size_t len,2066const unsigned char *x, size_t xlen,2067const unsigned char *y, size_t ylen, int curve)2068{2069p256_jacobian P, Q;2070uint32_t r, t, z;2071int i;20722073(void)curve;2074if (len != 65) {2075return 0;2076}2077r = p256_decode(&P, A, len);2078p256_mul(&P, x, xlen);2079if (B == NULL) {2080p256_mulgen(&Q, y, ylen);2081} else {2082r &= p256_decode(&Q, B, len);2083p256_mul(&Q, y, ylen);2084}20852086/*2087* The final addition may fail in case both points are equal.2088*/2089t = p256_add(&P, &Q);2090reduce_final_f256(P.z);2091z = 0;2092for (i = 0; i < 20; i ++) {2093z |= P.z[i];2094}2095z = EQ(z, 0);2096p256_double(&Q);20972098/*2099* If z is 1 then either P+Q = 0 (t = 1) or P = Q (t = 0). So we2100* have the following:2101*2102* z = 0, t = 0 return P (normal addition)2103* z = 0, t = 1 return P (normal addition)2104* z = 1, t = 0 return Q (a 'double' case)2105* z = 1, t = 1 report an error (P+Q = 0)2106*/2107CCOPY(z & ~t, &P, &Q, sizeof Q);2108p256_to_affine(&P);2109p256_encode(A, &P);2110r &= ~(z & t);2111return r;2112}21132114/* see bearssl_ec.h */2115const br_ec_impl br_ec_p256_m15 = {2116(uint32_t)0x00800000,2117&api_generator,2118&api_order,2119&api_xoff,2120&api_mul,2121&api_mulgen,2122&api_muladd2123};212421252126