Path: blob/master/libs/tomcrypt/src/pk/ecc/ltc_ecc_mulmod.c
4396 views
/* LibTomCrypt, modular cryptographic library -- Tom St Denis1*2* LibTomCrypt is a library that provides various cryptographic3* algorithms in a highly modular and flexible manner.4*5* The library is free for all purposes without any express6* guarantee it works.7*/89/* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b10*11* All curves taken from NIST recommendation paper of July 199912* Available at http://csrc.nist.gov/cryptval/dss.htm13*/14#include "tomcrypt.h"1516/**17@file ltc_ecc_mulmod.c18ECC Crypto, Tom St Denis19*/2021#ifdef LTC_MECC22#ifndef LTC_ECC_TIMING_RESISTANT2324/* size of sliding window, don't change this! */25#define WINSIZE 42627/**28Perform a point multiplication29@param k The scalar to multiply by30@param G The base point31@param R [out] Destination for kG32@param modulus The modulus of the field the ECC curve is in33@param map Boolean whether to map back to affine or not (1==map, 0 == leave in projective)34@return CRYPT_OK on success35*/36int ltc_ecc_mulmod(void *k, ecc_point *G, ecc_point *R, void *modulus, int map)37{38ecc_point *tG, *M[8];39int i, j, err;40void *mu, *mp;41ltc_mp_digit buf;42int first, bitbuf, bitcpy, bitcnt, mode, digidx;4344LTC_ARGCHK(k != NULL);45LTC_ARGCHK(G != NULL);46LTC_ARGCHK(R != NULL);47LTC_ARGCHK(modulus != NULL);4849/* init montgomery reduction */50if ((err = mp_montgomery_setup(modulus, &mp)) != CRYPT_OK) {51return err;52}53if ((err = mp_init(&mu)) != CRYPT_OK) {54mp_montgomery_free(mp);55return err;56}57if ((err = mp_montgomery_normalization(mu, modulus)) != CRYPT_OK) {58mp_montgomery_free(mp);59mp_clear(mu);60return err;61}6263/* alloc ram for window temps */64for (i = 0; i < 8; i++) {65M[i] = ltc_ecc_new_point();66if (M[i] == NULL) {67for (j = 0; j < i; j++) {68ltc_ecc_del_point(M[j]);69}70mp_montgomery_free(mp);71mp_clear(mu);72return CRYPT_MEM;73}74}7576/* make a copy of G incase R==G */77tG = ltc_ecc_new_point();78if (tG == NULL) { err = CRYPT_MEM; goto done; }7980/* tG = G and convert to montgomery */81if (mp_cmp_d(mu, 1) == LTC_MP_EQ) {82if ((err = mp_copy(G->x, tG->x)) != CRYPT_OK) { goto done; }83if ((err = mp_copy(G->y, tG->y)) != CRYPT_OK) { goto done; }84if ((err = mp_copy(G->z, tG->z)) != CRYPT_OK) { goto done; }85} else {86if ((err = mp_mulmod(G->x, mu, modulus, tG->x)) != CRYPT_OK) { goto done; }87if ((err = mp_mulmod(G->y, mu, modulus, tG->y)) != CRYPT_OK) { goto done; }88if ((err = mp_mulmod(G->z, mu, modulus, tG->z)) != CRYPT_OK) { goto done; }89}90mp_clear(mu);91mu = NULL;9293/* calc the M tab, which holds kG for k==8..15 */94/* M[0] == 8G */95if ((err = ltc_mp.ecc_ptdbl(tG, M[0], modulus, mp)) != CRYPT_OK) { goto done; }96if ((err = ltc_mp.ecc_ptdbl(M[0], M[0], modulus, mp)) != CRYPT_OK) { goto done; }97if ((err = ltc_mp.ecc_ptdbl(M[0], M[0], modulus, mp)) != CRYPT_OK) { goto done; }9899/* now find (8+k)G for k=1..7 */100for (j = 9; j < 16; j++) {101if ((err = ltc_mp.ecc_ptadd(M[j-9], tG, M[j-8], modulus, mp)) != CRYPT_OK) { goto done; }102}103104/* setup sliding window */105mode = 0;106bitcnt = 1;107buf = 0;108digidx = mp_get_digit_count(k) - 1;109bitcpy = bitbuf = 0;110first = 1;111112/* perform ops */113for (;;) {114/* grab next digit as required */115if (--bitcnt == 0) {116if (digidx == -1) {117break;118}119buf = mp_get_digit(k, digidx);120bitcnt = (int) ltc_mp.bits_per_digit;121--digidx;122}123124/* grab the next msb from the ltiplicand */125i = (buf >> (ltc_mp.bits_per_digit - 1)) & 1;126buf <<= 1;127128/* skip leading zero bits */129if (mode == 0 && i == 0) {130continue;131}132133/* if the bit is zero and mode == 1 then we double */134if (mode == 1 && i == 0) {135if ((err = ltc_mp.ecc_ptdbl(R, R, modulus, mp)) != CRYPT_OK) { goto done; }136continue;137}138139/* else we add it to the window */140bitbuf |= (i << (WINSIZE - ++bitcpy));141mode = 2;142143if (bitcpy == WINSIZE) {144/* if this is the first window we do a simple copy */145if (first == 1) {146/* R = kG [k = first window] */147if ((err = mp_copy(M[bitbuf-8]->x, R->x)) != CRYPT_OK) { goto done; }148if ((err = mp_copy(M[bitbuf-8]->y, R->y)) != CRYPT_OK) { goto done; }149if ((err = mp_copy(M[bitbuf-8]->z, R->z)) != CRYPT_OK) { goto done; }150first = 0;151} else {152/* normal window */153/* ok window is filled so double as required and add */154/* double first */155for (j = 0; j < WINSIZE; j++) {156if ((err = ltc_mp.ecc_ptdbl(R, R, modulus, mp)) != CRYPT_OK) { goto done; }157}158159/* then add, bitbuf will be 8..15 [8..2^WINSIZE] guaranteed */160if ((err = ltc_mp.ecc_ptadd(R, M[bitbuf-8], R, modulus, mp)) != CRYPT_OK) { goto done; }161}162/* empty window and reset */163bitcpy = bitbuf = 0;164mode = 1;165}166}167168/* if bits remain then double/add */169if (mode == 2 && bitcpy > 0) {170/* double then add */171for (j = 0; j < bitcpy; j++) {172/* only double if we have had at least one add first */173if (first == 0) {174if ((err = ltc_mp.ecc_ptdbl(R, R, modulus, mp)) != CRYPT_OK) { goto done; }175}176177bitbuf <<= 1;178if ((bitbuf & (1 << WINSIZE)) != 0) {179if (first == 1){180/* first add, so copy */181if ((err = mp_copy(tG->x, R->x)) != CRYPT_OK) { goto done; }182if ((err = mp_copy(tG->y, R->y)) != CRYPT_OK) { goto done; }183if ((err = mp_copy(tG->z, R->z)) != CRYPT_OK) { goto done; }184first = 0;185} else {186/* then add */187if ((err = ltc_mp.ecc_ptadd(R, tG, R, modulus, mp)) != CRYPT_OK) { goto done; }188}189}190}191}192193/* map R back from projective space */194if (map) {195err = ltc_ecc_map(R, modulus, mp);196} else {197err = CRYPT_OK;198}199done:200if (mu != NULL) {201mp_clear(mu);202}203mp_montgomery_free(mp);204ltc_ecc_del_point(tG);205for (i = 0; i < 8; i++) {206ltc_ecc_del_point(M[i]);207}208return err;209}210211#endif212213#undef WINSIZE214215#endif216217218