Path: blob/main/contrib/bearssl/src/ec/ec_c25519_i31.c
39536 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* Parameters for the field:28* - field modulus p = 2^255-1929* - R^2 mod p (R = 2^(31k) for the smallest k such that R >= p)30*/3132static const uint32_t C255_P[] = {330x00000107,340x7FFFFFED, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF,350x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x0000007F36};3738#define P0I 0x286BCA1B3940static const uint32_t C255_R2[] = {410x00000107,420x00000000, 0x02D20000, 0x00000000, 0x00000000, 0x00000000,430x00000000, 0x00000000, 0x00000000, 0x0000000044};4546static const uint32_t C255_A24[] = {470x00000107,480x53000000, 0x0000468B, 0x00000000, 0x00000000, 0x00000000,490x00000000, 0x00000000, 0x00000000, 0x0000000050};5152/* obsolete53#include <stdio.h>54#include <stdlib.h>55static void56print_int_mont(const char *name, const uint32_t *x)57{58uint32_t y[10];59unsigned char tmp[32];60size_t u;6162printf("%s = ", name);63memcpy(y, x, sizeof y);64br_i31_from_monty(y, C255_P, P0I);65br_i31_encode(tmp, sizeof tmp, y);66for (u = 0; u < sizeof tmp; u ++) {67printf("%02X", tmp[u]);68}69printf("\n");70}71*/7273static const unsigned char GEN[] = {740x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,750x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,760x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,770x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0078};7980static const unsigned char ORDER[] = {810x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,820xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,830xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,840xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF85};8687static const unsigned char *88api_generator(int curve, size_t *len)89{90(void)curve;91*len = 32;92return GEN;93}9495static const unsigned char *96api_order(int curve, size_t *len)97{98(void)curve;99*len = 32;100return ORDER;101}102103static size_t104api_xoff(int curve, size_t *len)105{106(void)curve;107*len = 32;108return 0;109}110111static void112cswap(uint32_t *a, uint32_t *b, uint32_t ctl)113{114int i;115116ctl = -ctl;117for (i = 0; i < 10; i ++) {118uint32_t aw, bw, tw;119120aw = a[i];121bw = b[i];122tw = ctl & (aw ^ bw);123a[i] = aw ^ tw;124b[i] = bw ^ tw;125}126}127128static void129c255_add(uint32_t *d, const uint32_t *a, const uint32_t *b)130{131uint32_t ctl;132uint32_t t[10];133134memcpy(t, a, sizeof t);135ctl = br_i31_add(t, b, 1);136ctl |= NOT(br_i31_sub(t, C255_P, 0));137br_i31_sub(t, C255_P, ctl);138memcpy(d, t, sizeof t);139}140141static void142c255_sub(uint32_t *d, const uint32_t *a, const uint32_t *b)143{144uint32_t t[10];145146memcpy(t, a, sizeof t);147br_i31_add(t, C255_P, br_i31_sub(t, b, 1));148memcpy(d, t, sizeof t);149}150151static void152c255_mul(uint32_t *d, const uint32_t *a, const uint32_t *b)153{154uint32_t t[10];155156br_i31_montymul(t, a, b, C255_P, P0I);157memcpy(d, t, sizeof t);158}159160static void161byteswap(unsigned char *G)162{163int i;164165for (i = 0; i < 16; i ++) {166unsigned char t;167168t = G[i];169G[i] = G[31 - i];170G[31 - i] = t;171}172}173174static uint32_t175api_mul(unsigned char *G, size_t Glen,176const unsigned char *kb, size_t kblen, int curve)177{178uint32_t x1[10], x2[10], x3[10], z2[10], z3[10];179uint32_t a[10], aa[10], b[10], bb[10];180uint32_t c[10], d[10], e[10], da[10], cb[10];181unsigned char k[32];182uint32_t swap;183int i;184185(void)curve;186187/*188* Points are encoded over exactly 32 bytes. Multipliers must fit189* in 32 bytes as well.190* RFC 7748 mandates that the high bit of the last point byte must191* be ignored/cleared.192*/193if (Glen != 32 || kblen > 32) {194return 0;195}196G[31] &= 0x7F;197198/*199* Byteswap the point encoding, because it uses little-endian, and200* the generic decoding routine uses big-endian.201*/202byteswap(G);203204/*205* Decode the point ('u' coordinate). This should be reduced206* modulo p, but we prefer to avoid the dependency on207* br_i31_decode_reduce(). Instead, we use br_i31_decode_mod()208* with a synthetic modulus of value 2^255 (this must work209* since G was truncated to 255 bits), then use a conditional210* subtraction. We use br_i31_decode_mod() and not211* br_i31_decode(), because the ec_prime_i31 implementation uses212* the former but not the latter.213* br_i31_decode_reduce(a, G, 32, C255_P);214*/215br_i31_zero(b, 0x108);216b[9] = 0x0080;217br_i31_decode_mod(a, G, 32, b);218a[0] = 0x107;219br_i31_sub(a, C255_P, NOT(br_i31_sub(a, C255_P, 0)));220221/*222* Initialise variables x1, x2, z2, x3 and z3. We set all of them223* into Montgomery representation.224*/225br_i31_montymul(x1, a, C255_R2, C255_P, P0I);226memcpy(x3, x1, sizeof x1);227br_i31_zero(z2, C255_P[0]);228memcpy(x2, z2, sizeof z2);229x2[1] = 0x13000000;230memcpy(z3, x2, sizeof x2);231232/*233* kb[] is in big-endian notation, but possibly shorter than k[].234*/235memset(k, 0, (sizeof k) - kblen);236memcpy(k + (sizeof k) - kblen, kb, kblen);237k[31] &= 0xF8;238k[0] &= 0x7F;239k[0] |= 0x40;240241/* obsolete242print_int_mont("x1", x1);243*/244245swap = 0;246for (i = 254; i >= 0; i --) {247uint32_t kt;248249kt = (k[31 - (i >> 3)] >> (i & 7)) & 1;250swap ^= kt;251cswap(x2, x3, swap);252cswap(z2, z3, swap);253swap = kt;254255/* obsolete256print_int_mont("x2", x2);257print_int_mont("z2", z2);258print_int_mont("x3", x3);259print_int_mont("z3", z3);260*/261262c255_add(a, x2, z2);263c255_mul(aa, a, a);264c255_sub(b, x2, z2);265c255_mul(bb, b, b);266c255_sub(e, aa, bb);267c255_add(c, x3, z3);268c255_sub(d, x3, z3);269c255_mul(da, d, a);270c255_mul(cb, c, b);271272/* obsolete273print_int_mont("a ", a);274print_int_mont("aa", aa);275print_int_mont("b ", b);276print_int_mont("bb", bb);277print_int_mont("e ", e);278print_int_mont("c ", c);279print_int_mont("d ", d);280print_int_mont("da", da);281print_int_mont("cb", cb);282*/283284c255_add(x3, da, cb);285c255_mul(x3, x3, x3);286c255_sub(z3, da, cb);287c255_mul(z3, z3, z3);288c255_mul(z3, z3, x1);289c255_mul(x2, aa, bb);290c255_mul(z2, C255_A24, e);291c255_add(z2, z2, aa);292c255_mul(z2, e, z2);293294/* obsolete295print_int_mont("x2", x2);296print_int_mont("z2", z2);297print_int_mont("x3", x3);298print_int_mont("z3", z3);299*/300}301cswap(x2, x3, swap);302cswap(z2, z3, swap);303304/*305* Inverse z2 with a modular exponentiation. This is a simple306* square-and-multiply algorithm; we mutualise most non-squarings307* since the exponent contains almost only ones.308*/309memcpy(a, z2, sizeof z2);310for (i = 0; i < 15; i ++) {311c255_mul(a, a, a);312c255_mul(a, a, z2);313}314memcpy(b, a, sizeof a);315for (i = 0; i < 14; i ++) {316int j;317318for (j = 0; j < 16; j ++) {319c255_mul(b, b, b);320}321c255_mul(b, b, a);322}323for (i = 14; i >= 0; i --) {324c255_mul(b, b, b);325if ((0xFFEB >> i) & 1) {326c255_mul(b, z2, b);327}328}329c255_mul(b, x2, b);330331/*332* To avoid a dependency on br_i31_from_monty(), we use333* a Montgomery multiplication with 1.334* memcpy(x2, b, sizeof b);335* br_i31_from_monty(x2, C255_P, P0I);336*/337br_i31_zero(a, C255_P[0]);338a[1] = 1;339br_i31_montymul(x2, a, b, C255_P, P0I);340341br_i31_encode(G, 32, x2);342byteswap(G);343return 1;344}345346static size_t347api_mulgen(unsigned char *R,348const unsigned char *x, size_t xlen, int curve)349{350const unsigned char *G;351size_t Glen;352353G = api_generator(curve, &Glen);354memcpy(R, G, Glen);355api_mul(R, Glen, x, xlen, curve);356return Glen;357}358359static uint32_t360api_muladd(unsigned char *A, const unsigned char *B, size_t len,361const unsigned char *x, size_t xlen,362const unsigned char *y, size_t ylen, int curve)363{364/*365* We don't implement this method, since it is used for ECDSA366* only, and there is no ECDSA over Curve25519 (which instead367* uses EdDSA).368*/369(void)A;370(void)B;371(void)len;372(void)x;373(void)xlen;374(void)y;375(void)ylen;376(void)curve;377return 0;378}379380/* see bearssl_ec.h */381const br_ec_impl br_ec_c25519_i31 = {382(uint32_t)0x20000000,383&api_generator,384&api_order,385&api_xoff,386&api_mul,387&api_mulgen,388&api_muladd389};390391392