Path: blob/main/contrib/bearssl/src/ec/ec_c25519_m62.c
39536 views
/*1* Copyright (c) 2018 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#if BR_INT128 || BR_UMUL1282728#if BR_UMUL12829#include <intrin.h>30#endif3132static const unsigned char GEN[] = {330x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,340x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,350x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,360x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0037};3839static const unsigned char ORDER[] = {400x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,410xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,420xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,430xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF44};4546static const unsigned char *47api_generator(int curve, size_t *len)48{49(void)curve;50*len = 32;51return GEN;52}5354static const unsigned char *55api_order(int curve, size_t *len)56{57(void)curve;58*len = 32;59return ORDER;60}6162static size_t63api_xoff(int curve, size_t *len)64{65(void)curve;66*len = 32;67return 0;68}6970/*71* A field element is encoded as five 64-bit integers, in basis 2^51.72* Limbs may be occasionally larger than 2^51, to save on carry73* propagation costs.74*/7576#define MASK51 (((uint64_t)1 << 51) - (uint64_t)1)7778/*79* Swap two field elements, conditionally on a flag.80*/81static inline void82f255_cswap(uint64_t *a, uint64_t *b, uint32_t ctl)83{84uint64_t m, w;8586m = -(uint64_t)ctl;87w = m & (a[0] ^ b[0]); a[0] ^= w; b[0] ^= w;88w = m & (a[1] ^ b[1]); a[1] ^= w; b[1] ^= w;89w = m & (a[2] ^ b[2]); a[2] ^= w; b[2] ^= w;90w = m & (a[3] ^ b[3]); a[3] ^= w; b[3] ^= w;91w = m & (a[4] ^ b[4]); a[4] ^= w; b[4] ^= w;92}9394/*95* Addition with no carry propagation. Limbs double in size.96*/97static inline void98f255_add(uint64_t *d, const uint64_t *a, const uint64_t *b)99{100d[0] = a[0] + b[0];101d[1] = a[1] + b[1];102d[2] = a[2] + b[2];103d[3] = a[3] + b[3];104d[4] = a[4] + b[4];105}106107/*108* Subtraction.109* On input, limbs must fit on 60 bits each. On output, result is110* partially reduced, with max value 2^255+19456; moreover, all111* limbs will fit on 51 bits, except the low limb, which may have112* value up to 2^51+19455.113*/114static inline void115f255_sub(uint64_t *d, const uint64_t *a, const uint64_t *b)116{117uint64_t cc, w;118119/*120* We compute d = (2^255-19)*1024 + a - b. Since the limbs121* fit on 60 bits, the maximum value of operands are slightly122* more than 2^264, but much less than 2^265-19456. This123* ensures that the result is positive.124*/125126/*127* Initial carry is 19456, since we add 2^265-19456. Each128* individual subtraction may yield a carry up to 513.129*/130w = a[0] - b[0] - 19456;131d[0] = w & MASK51;132cc = -(w >> 51) & 0x3FF;133w = a[1] - b[1] - cc;134d[1] = w & MASK51;135cc = -(w >> 51) & 0x3FF;136w = a[2] - b[2] - cc;137d[2] = w & MASK51;138cc = -(w >> 51) & 0x3FF;139w = a[3] - b[3] - cc;140d[3] = w & MASK51;141cc = -(w >> 51) & 0x3FF;142d[4] = ((uint64_t)1 << 61) + a[4] - b[4] - cc;143144/*145* Partial reduction. The intermediate result may be up to146* slightly above 2^265, but less than 2^265+2^255. When we147* truncate to 255 bits, the upper bits will be at most 1024.148*/149d[0] += 19 * (d[4] >> 51);150d[4] &= MASK51;151}152153/*154* UMUL51(hi, lo, x, y) computes:155*156* hi = floor((x * y) / (2^51))157* lo = x * y mod 2^51158*159* Note that lo < 2^51, but "hi" may be larger, if the input operands are160* larger.161*/162#if BR_INT128163164#define UMUL51(hi, lo, x, y) do { \165unsigned __int128 umul_tmp; \166umul_tmp = (unsigned __int128)(x) * (unsigned __int128)(y); \167(hi) = (uint64_t)(umul_tmp >> 51); \168(lo) = (uint64_t)umul_tmp & MASK51; \169} while (0)170171#elif BR_UMUL128172173#define UMUL51(hi, lo, x, y) do { \174uint64_t umul_hi, umul_lo; \175umul_lo = _umul128((x), (y), &umul_hi); \176(hi) = (umul_hi << 13) | (umul_lo >> 51); \177(lo) = umul_lo & MASK51; \178} while (0)179180#endif181182/*183* Multiplication.184* On input, limbs must fit on 54 bits each.185* On output, limb 0 is at most 2^51 + 155647, and other limbs fit186* on 51 bits each.187*/188static inline void189f255_mul(uint64_t *d, uint64_t *a, uint64_t *b)190{191uint64_t t[10], hi, lo, w, cc;192193/*194* Perform cross products, accumulating values without carry195* propagation.196*197* Since input limbs fit on 54 bits each, each individual198* UMUL51 will produce a "hi" of less than 2^57. The maximum199* sum will be at most 5*(2^57-1) + 4*(2^51-1) (for t[5]),200* i.e. less than 324*2^51.201*/202203UMUL51(t[1], t[0], a[0], b[0]);204205UMUL51(t[2], lo, a[1], b[0]); t[1] += lo;206UMUL51(hi, lo, a[0], b[1]); t[1] += lo; t[2] += hi;207208UMUL51(t[3], lo, a[2], b[0]); t[2] += lo;209UMUL51(hi, lo, a[1], b[1]); t[2] += lo; t[3] += hi;210UMUL51(hi, lo, a[0], b[2]); t[2] += lo; t[3] += hi;211212UMUL51(t[4], lo, a[3], b[0]); t[3] += lo;213UMUL51(hi, lo, a[2], b[1]); t[3] += lo; t[4] += hi;214UMUL51(hi, lo, a[1], b[2]); t[3] += lo; t[4] += hi;215UMUL51(hi, lo, a[0], b[3]); t[3] += lo; t[4] += hi;216217UMUL51(t[5], lo, a[4], b[0]); t[4] += lo;218UMUL51(hi, lo, a[3], b[1]); t[4] += lo; t[5] += hi;219UMUL51(hi, lo, a[2], b[2]); t[4] += lo; t[5] += hi;220UMUL51(hi, lo, a[1], b[3]); t[4] += lo; t[5] += hi;221UMUL51(hi, lo, a[0], b[4]); t[4] += lo; t[5] += hi;222223UMUL51(t[6], lo, a[4], b[1]); t[5] += lo;224UMUL51(hi, lo, a[3], b[2]); t[5] += lo; t[6] += hi;225UMUL51(hi, lo, a[2], b[3]); t[5] += lo; t[6] += hi;226UMUL51(hi, lo, a[1], b[4]); t[5] += lo; t[6] += hi;227228UMUL51(t[7], lo, a[4], b[2]); t[6] += lo;229UMUL51(hi, lo, a[3], b[3]); t[6] += lo; t[7] += hi;230UMUL51(hi, lo, a[2], b[4]); t[6] += lo; t[7] += hi;231232UMUL51(t[8], lo, a[4], b[3]); t[7] += lo;233UMUL51(hi, lo, a[3], b[4]); t[7] += lo; t[8] += hi;234235UMUL51(t[9], lo, a[4], b[4]); t[8] += lo;236237/*238* The upper words t[5]..t[9] are folded back into the lower239* words, using the rule that 2^255 = 19 in the field.240*241* Since each t[i] is less than 324*2^51, the additions below242* will yield less than 6480*2^51 in each limb; this fits in243* 64 bits (6480*2^51 < 8192*2^51 = 2^64), hence there is244* no overflow.245*/246t[0] += 19 * t[5];247t[1] += 19 * t[6];248t[2] += 19 * t[7];249t[3] += 19 * t[8];250t[4] += 19 * t[9];251252/*253* Propagate carries.254*/255w = t[0];256d[0] = w & MASK51;257cc = w >> 51;258w = t[1] + cc;259d[1] = w & MASK51;260cc = w >> 51;261w = t[2] + cc;262d[2] = w & MASK51;263cc = w >> 51;264w = t[3] + cc;265d[3] = w & MASK51;266cc = w >> 51;267w = t[4] + cc;268d[4] = w & MASK51;269cc = w >> 51;270271/*272* Since the limbs were 64-bit values, the top carry is at273* most 8192 (in practice, that cannot be reached). We simply274* performed a partial reduction.275*/276d[0] += 19 * cc;277}278279/*280* Multiplication by A24 = 121665.281* Input must have limbs of 60 bits at most.282*/283static inline void284f255_mul_a24(uint64_t *d, const uint64_t *a)285{286uint64_t t[5], cc, w;287288/*289* 121665 = 15 * 8111. We first multiply by 15, with carry290* propagation and partial reduction.291*/292w = a[0] * 15;293t[0] = w & MASK51;294cc = w >> 51;295w = a[1] * 15 + cc;296t[1] = w & MASK51;297cc = w >> 51;298w = a[2] * 15 + cc;299t[2] = w & MASK51;300cc = w >> 51;301w = a[3] * 15 + cc;302t[3] = w & MASK51;303cc = w >> 51;304w = a[4] * 15 + cc;305t[4] = w & MASK51;306t[0] += 19 * (w >> 51);307308/*309* Then multiplication by 8111. At that point, we known that310* t[0] is less than 2^51 + 19*8192, and other limbs are less311* than 2^51; thus, there will be no overflow.312*/313w = t[0] * 8111;314d[0] = w & MASK51;315cc = w >> 51;316w = t[1] * 8111 + cc;317d[1] = w & MASK51;318cc = w >> 51;319w = t[2] * 8111 + cc;320d[2] = w & MASK51;321cc = w >> 51;322w = t[3] * 8111 + cc;323d[3] = w & MASK51;324cc = w >> 51;325w = t[4] * 8111 + cc;326d[4] = w & MASK51;327d[0] += 19 * (w >> 51);328}329330/*331* Finalize reduction.332* On input, limbs must fit on 51 bits, except possibly the low limb,333* which may be slightly above 2^51.334*/335static inline void336f255_final_reduce(uint64_t *a)337{338uint64_t t[5], cc, w;339340/*341* We add 19. If the result (in t[]) is below 2^255, then a[]342* is already less than 2^255-19, thus already reduced.343* Otherwise, we subtract 2^255 from t[], in which case we344* have t = a - (2^255-19), and that's our result.345*/346w = a[0] + 19;347t[0] = w & MASK51;348cc = w >> 51;349w = a[1] + cc;350t[1] = w & MASK51;351cc = w >> 51;352w = a[2] + cc;353t[2] = w & MASK51;354cc = w >> 51;355w = a[3] + cc;356t[3] = w & MASK51;357cc = w >> 51;358w = a[4] + cc;359t[4] = w & MASK51;360cc = w >> 51;361362/*363* The bit 255 of t is in cc. If that bit is 0, when a[] must364* be unchanged; otherwise, it must be replaced with t[].365*/366cc = -cc;367a[0] ^= cc & (a[0] ^ t[0]);368a[1] ^= cc & (a[1] ^ t[1]);369a[2] ^= cc & (a[2] ^ t[2]);370a[3] ^= cc & (a[3] ^ t[3]);371a[4] ^= cc & (a[4] ^ t[4]);372}373374static uint32_t375api_mul(unsigned char *G, size_t Glen,376const unsigned char *kb, size_t kblen, int curve)377{378unsigned char k[32];379uint64_t x1[5], x2[5], z2[5], x3[5], z3[5];380uint32_t swap;381int i;382383(void)curve;384385/*386* Points are encoded over exactly 32 bytes. Multipliers must fit387* in 32 bytes as well.388*/389if (Glen != 32 || kblen > 32) {390return 0;391}392393/*394* RFC 7748 mandates that the high bit of the last point byte must395* be ignored/cleared; the "& MASK51" in the initialization for396* x1[4] clears that bit.397*/398x1[0] = br_dec64le(&G[0]) & MASK51;399x1[1] = (br_dec64le(&G[6]) >> 3) & MASK51;400x1[2] = (br_dec64le(&G[12]) >> 6) & MASK51;401x1[3] = (br_dec64le(&G[19]) >> 1) & MASK51;402x1[4] = (br_dec64le(&G[24]) >> 12) & MASK51;403404/*405* We can use memset() to clear values, because exact-width types406* like uint64_t are guaranteed to have no padding bits or407* trap representations.408*/409memset(x2, 0, sizeof x2);410x2[0] = 1;411memset(z2, 0, sizeof z2);412memcpy(x3, x1, sizeof x1);413memcpy(z3, x2, sizeof x2);414415/*416* The multiplier is provided in big-endian notation, and417* possibly shorter than 32 bytes.418*/419memset(k, 0, (sizeof k) - kblen);420memcpy(k + (sizeof k) - kblen, kb, kblen);421k[31] &= 0xF8;422k[0] &= 0x7F;423k[0] |= 0x40;424425swap = 0;426427for (i = 254; i >= 0; i --) {428uint64_t a[5], aa[5], b[5], bb[5], e[5];429uint64_t c[5], d[5], da[5], cb[5];430uint32_t kt;431432kt = (k[31 - (i >> 3)] >> (i & 7)) & 1;433swap ^= kt;434f255_cswap(x2, x3, swap);435f255_cswap(z2, z3, swap);436swap = kt;437438/*439* At that point, limbs of x_2 and z_2 are assumed to fit440* on at most 52 bits each.441*442* Each f255_add() adds one bit to the maximum range of443* the values, but f255_sub() and f255_mul() bring back444* the limbs into 52 bits. All f255_add() outputs are445* used only as inputs for f255_mul(), which ensures446* that limbs remain in the proper range.447*/448449/* A = x_2 + z_2 -- limbs fit on 53 bits each */450f255_add(a, x2, z2);451452/* AA = A^2 */453f255_mul(aa, a, a);454455/* B = x_2 - z_2 */456f255_sub(b, x2, z2);457458/* BB = B^2 */459f255_mul(bb, b, b);460461/* E = AA - BB */462f255_sub(e, aa, bb);463464/* C = x_3 + z_3 -- limbs fit on 53 bits each */465f255_add(c, x3, z3);466467/* D = x_3 - z_3 */468f255_sub(d, x3, z3);469470/* DA = D * A */471f255_mul(da, d, a);472473/* CB = C * B */474f255_mul(cb, c, b);475476/* x_3 = (DA + CB)^2 */477f255_add(x3, da, cb);478f255_mul(x3, x3, x3);479480/* z_3 = x_1 * (DA - CB)^2 */481f255_sub(z3, da, cb);482f255_mul(z3, z3, z3);483f255_mul(z3, x1, z3);484485/* x_2 = AA * BB */486f255_mul(x2, aa, bb);487488/* z_2 = E * (AA + a24 * E) */489f255_mul_a24(z2, e);490f255_add(z2, aa, z2);491f255_mul(z2, e, z2);492}493494f255_cswap(x2, x3, swap);495f255_cswap(z2, z3, swap);496497/*498* Compute 1/z2 = z2^(p-2). Since p = 2^255-19, we can mutualize499* most non-squarings. We use x1 and x3, now useless, as temporaries.500*/501memcpy(x1, z2, sizeof z2);502for (i = 0; i < 15; i ++) {503f255_mul(x1, x1, x1);504f255_mul(x1, x1, z2);505}506memcpy(x3, x1, sizeof x1);507for (i = 0; i < 14; i ++) {508int j;509510for (j = 0; j < 16; j ++) {511f255_mul(x3, x3, x3);512}513f255_mul(x3, x3, x1);514}515for (i = 14; i >= 0; i --) {516f255_mul(x3, x3, x3);517if ((0xFFEB >> i) & 1) {518f255_mul(x3, z2, x3);519}520}521522/*523* Compute x2/z2. We have 1/z2 in x3.524*/525f255_mul(x2, x2, x3);526f255_final_reduce(x2);527528/*529* Encode the final x2 value in little-endian. We first assemble530* the limbs into 64-bit values.531*/532x2[0] |= x2[1] << 51;533x2[1] = (x2[1] >> 13) | (x2[2] << 38);534x2[2] = (x2[2] >> 26) | (x2[3] << 25);535x2[3] = (x2[3] >> 39) | (x2[4] << 12);536br_enc64le(G, x2[0]);537br_enc64le(G + 8, x2[1]);538br_enc64le(G + 16, x2[2]);539br_enc64le(G + 24, x2[3]);540return 1;541}542543static size_t544api_mulgen(unsigned char *R,545const unsigned char *x, size_t xlen, int curve)546{547const unsigned char *G;548size_t Glen;549550G = api_generator(curve, &Glen);551memcpy(R, G, Glen);552api_mul(R, Glen, x, xlen, curve);553return Glen;554}555556static uint32_t557api_muladd(unsigned char *A, const unsigned char *B, size_t len,558const unsigned char *x, size_t xlen,559const unsigned char *y, size_t ylen, int curve)560{561/*562* We don't implement this method, since it is used for ECDSA563* only, and there is no ECDSA over Curve25519 (which instead564* uses EdDSA).565*/566(void)A;567(void)B;568(void)len;569(void)x;570(void)xlen;571(void)y;572(void)ylen;573(void)curve;574return 0;575}576577/* see bearssl_ec.h */578const br_ec_impl br_ec_c25519_m62 = {579(uint32_t)0x20000000,580&api_generator,581&api_order,582&api_xoff,583&api_mul,584&api_mulgen,585&api_muladd586};587588/* see bearssl_ec.h */589const br_ec_impl *590br_ec_c25519_m62_get(void)591{592return &br_ec_c25519_m62;593}594595#else596597/* see bearssl_ec.h */598const br_ec_impl *599br_ec_c25519_m62_get(void)600{601return 0;602}603604#endif605606607