Path: blob/main/contrib/bearssl/src/ec/ec_all_m31.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"2526static const unsigned char *27api_generator(int curve, size_t *len)28{29switch (curve) {30case BR_EC_secp256r1:31#if BR_INT128 || BR_UMUL12832return br_ec_p256_m64.generator(curve, len);33#else34return br_ec_p256_m31.generator(curve, len);35#endif36case BR_EC_curve25519:37#if BR_INT128 || BR_UMUL12838return br_ec_c25519_m64.generator(curve, len);39#else40return br_ec_c25519_m31.generator(curve, len);41#endif42default:43return br_ec_prime_i31.generator(curve, len);44}45}4647static const unsigned char *48api_order(int curve, size_t *len)49{50switch (curve) {51case BR_EC_secp256r1:52#if BR_INT128 || BR_UMUL12853return br_ec_p256_m64.order(curve, len);54#else55return br_ec_p256_m31.order(curve, len);56#endif57case BR_EC_curve25519:58#if BR_INT128 || BR_UMUL12859return br_ec_c25519_m64.order(curve, len);60#else61return br_ec_c25519_m31.order(curve, len);62#endif63default:64return br_ec_prime_i31.order(curve, len);65}66}6768static size_t69api_xoff(int curve, size_t *len)70{71switch (curve) {72case BR_EC_secp256r1:73#if BR_INT128 || BR_UMUL12874return br_ec_p256_m64.xoff(curve, len);75#else76return br_ec_p256_m31.xoff(curve, len);77#endif78case BR_EC_curve25519:79#if BR_INT128 || BR_UMUL12880return br_ec_c25519_m64.xoff(curve, len);81#else82return br_ec_c25519_m31.xoff(curve, len);83#endif84default:85return br_ec_prime_i31.xoff(curve, len);86}87}8889static uint32_t90api_mul(unsigned char *G, size_t Glen,91const unsigned char *kb, size_t kblen, int curve)92{93switch (curve) {94case BR_EC_secp256r1:95#if BR_INT128 || BR_UMUL12896return br_ec_p256_m64.mul(G, Glen, kb, kblen, curve);97#else98return br_ec_p256_m31.mul(G, Glen, kb, kblen, curve);99#endif100case BR_EC_curve25519:101#if BR_INT128 || BR_UMUL128102return br_ec_c25519_m64.mul(G, Glen, kb, kblen, curve);103#else104return br_ec_c25519_m31.mul(G, Glen, kb, kblen, curve);105#endif106default:107return br_ec_prime_i31.mul(G, Glen, kb, kblen, curve);108}109}110111static size_t112api_mulgen(unsigned char *R,113const unsigned char *x, size_t xlen, int curve)114{115switch (curve) {116case BR_EC_secp256r1:117#if BR_INT128 || BR_UMUL128118return br_ec_p256_m64.mulgen(R, x, xlen, curve);119#else120return br_ec_p256_m31.mulgen(R, x, xlen, curve);121#endif122case BR_EC_curve25519:123#if BR_INT128 || BR_UMUL128124return br_ec_c25519_m64.mulgen(R, x, xlen, curve);125#else126return br_ec_c25519_m31.mulgen(R, x, xlen, curve);127#endif128default:129return br_ec_prime_i31.mulgen(R, x, xlen, curve);130}131}132133static uint32_t134api_muladd(unsigned char *A, const unsigned char *B, size_t len,135const unsigned char *x, size_t xlen,136const unsigned char *y, size_t ylen, int curve)137{138switch (curve) {139case BR_EC_secp256r1:140#if BR_INT128 || BR_UMUL128141return br_ec_p256_m64.muladd(A, B, len,142x, xlen, y, ylen, curve);143#else144return br_ec_p256_m31.muladd(A, B, len,145x, xlen, y, ylen, curve);146#endif147case BR_EC_curve25519:148#if BR_INT128 || BR_UMUL128149return br_ec_c25519_m64.muladd(A, B, len,150x, xlen, y, ylen, curve);151#else152return br_ec_c25519_m31.muladd(A, B, len,153x, xlen, y, ylen, curve);154#endif155default:156return br_ec_prime_i31.muladd(A, B, len,157x, xlen, y, ylen, curve);158}159}160161/* see bearssl_ec.h */162const br_ec_impl br_ec_all_m31 = {163(uint32_t)0x23800000,164&api_generator,165&api_order,166&api_xoff,167&api_mul,168&api_mulgen,169&api_muladd170};171172173