Improved modular exponentiation (automatic window optimisation if there is enough room).
This commit is contained in:
parent
a7e6409c37
commit
556e525d62
6 changed files with 268 additions and 44 deletions
|
|
@ -1,6 +1,6 @@
|
|||
# Automatically generated rules. Use 'mkrules.sh' to modify/regenerate.
|
||||
|
||||
OBJ = $(OBJDIR)$Pccopy$O $(OBJDIR)$Pdec16be$O $(OBJDIR)$Pdec16le$O $(OBJDIR)$Pdec32be$O $(OBJDIR)$Pdec32le$O $(OBJDIR)$Pdec64be$O $(OBJDIR)$Pdec64le$O $(OBJDIR)$Penc16be$O $(OBJDIR)$Penc16le$O $(OBJDIR)$Penc32be$O $(OBJDIR)$Penc32le$O $(OBJDIR)$Penc64be$O $(OBJDIR)$Penc64le$O $(OBJDIR)$Ppemdec$O $(OBJDIR)$Pec_all_m15$O $(OBJDIR)$Pec_all_m31$O $(OBJDIR)$Pec_c25519_i15$O $(OBJDIR)$Pec_c25519_i31$O $(OBJDIR)$Pec_c25519_m15$O $(OBJDIR)$Pec_c25519_m31$O $(OBJDIR)$Pec_curve25519$O $(OBJDIR)$Pec_p256_m15$O $(OBJDIR)$Pec_p256_m31$O $(OBJDIR)$Pec_prime_i15$O $(OBJDIR)$Pec_prime_i31$O $(OBJDIR)$Pec_secp256r1$O $(OBJDIR)$Pec_secp384r1$O $(OBJDIR)$Pec_secp521r1$O $(OBJDIR)$Pecdsa_atr$O $(OBJDIR)$Pecdsa_i15_bits$O $(OBJDIR)$Pecdsa_i15_sign_asn1$O $(OBJDIR)$Pecdsa_i15_sign_raw$O $(OBJDIR)$Pecdsa_i15_vrfy_asn1$O $(OBJDIR)$Pecdsa_i15_vrfy_raw$O $(OBJDIR)$Pecdsa_i31_bits$O $(OBJDIR)$Pecdsa_i31_sign_asn1$O $(OBJDIR)$Pecdsa_i31_sign_raw$O $(OBJDIR)$Pecdsa_i31_vrfy_asn1$O $(OBJDIR)$Pecdsa_i31_vrfy_raw$O $(OBJDIR)$Pecdsa_rta$O $(OBJDIR)$Pdig_oid$O $(OBJDIR)$Pdig_size$O $(OBJDIR)$Pghash_ctmul$O $(OBJDIR)$Pghash_ctmul32$O $(OBJDIR)$Pghash_ctmul64$O $(OBJDIR)$Pmd5$O $(OBJDIR)$Pmd5sha1$O $(OBJDIR)$Pmultihash$O $(OBJDIR)$Psha1$O $(OBJDIR)$Psha2big$O $(OBJDIR)$Psha2small$O $(OBJDIR)$Pi15_add$O $(OBJDIR)$Pi15_bitlen$O $(OBJDIR)$Pi15_decmod$O $(OBJDIR)$Pi15_decode$O $(OBJDIR)$Pi15_decred$O $(OBJDIR)$Pi15_encode$O $(OBJDIR)$Pi15_fmont$O $(OBJDIR)$Pi15_iszero$O $(OBJDIR)$Pi15_modpow$O $(OBJDIR)$Pi15_montmul$O $(OBJDIR)$Pi15_mulacc$O $(OBJDIR)$Pi15_muladd$O $(OBJDIR)$Pi15_ninv15$O $(OBJDIR)$Pi15_reduce$O $(OBJDIR)$Pi15_rshift$O $(OBJDIR)$Pi15_sub$O $(OBJDIR)$Pi15_tmont$O $(OBJDIR)$Pi31_add$O $(OBJDIR)$Pi31_bitlen$O $(OBJDIR)$Pi31_decmod$O $(OBJDIR)$Pi31_decode$O $(OBJDIR)$Pi31_decred$O $(OBJDIR)$Pi31_encode$O $(OBJDIR)$Pi31_fmont$O $(OBJDIR)$Pi31_iszero$O $(OBJDIR)$Pi31_modpow$O $(OBJDIR)$Pi31_montmul$O $(OBJDIR)$Pi31_mulacc$O $(OBJDIR)$Pi31_muladd$O $(OBJDIR)$Pi31_ninv31$O $(OBJDIR)$Pi31_reduce$O $(OBJDIR)$Pi31_rshift$O $(OBJDIR)$Pi31_sub$O $(OBJDIR)$Pi31_tmont$O $(OBJDIR)$Pi32_add$O $(OBJDIR)$Pi32_bitlen$O $(OBJDIR)$Pi32_decmod$O $(OBJDIR)$Pi32_decode$O $(OBJDIR)$Pi32_decred$O $(OBJDIR)$Pi32_div32$O $(OBJDIR)$Pi32_encode$O $(OBJDIR)$Pi32_fmont$O $(OBJDIR)$Pi32_iszero$O $(OBJDIR)$Pi32_modpow$O $(OBJDIR)$Pi32_montmul$O $(OBJDIR)$Pi32_mulacc$O $(OBJDIR)$Pi32_muladd$O $(OBJDIR)$Pi32_ninv32$O $(OBJDIR)$Pi32_reduce$O $(OBJDIR)$Pi32_sub$O $(OBJDIR)$Pi32_tmont$O $(OBJDIR)$Phmac$O $(OBJDIR)$Phmac_ct$O $(OBJDIR)$Phmac_drbg$O $(OBJDIR)$Prsa_i15_pkcs1_sign$O $(OBJDIR)$Prsa_i15_pkcs1_vrfy$O $(OBJDIR)$Prsa_i15_priv$O $(OBJDIR)$Prsa_i15_pub$O $(OBJDIR)$Prsa_i31_pkcs1_sign$O $(OBJDIR)$Prsa_i31_pkcs1_vrfy$O $(OBJDIR)$Prsa_i31_priv$O $(OBJDIR)$Prsa_i31_pub$O $(OBJDIR)$Prsa_i32_pkcs1_sign$O $(OBJDIR)$Prsa_i32_pkcs1_vrfy$O $(OBJDIR)$Prsa_i32_priv$O $(OBJDIR)$Prsa_i32_pub$O $(OBJDIR)$Prsa_pkcs1_sig_pad$O $(OBJDIR)$Prsa_pkcs1_sig_unpad$O $(OBJDIR)$Prsa_ssl_decrypt$O $(OBJDIR)$Pprf$O $(OBJDIR)$Pprf_md5sha1$O $(OBJDIR)$Pprf_sha256$O $(OBJDIR)$Pprf_sha384$O $(OBJDIR)$Pssl_ccert_single_ec$O $(OBJDIR)$Pssl_ccert_single_rsa$O $(OBJDIR)$Pssl_client$O $(OBJDIR)$Pssl_client_full$O $(OBJDIR)$Pssl_engine$O $(OBJDIR)$Pssl_hashes$O $(OBJDIR)$Pssl_hs_client$O $(OBJDIR)$Pssl_hs_server$O $(OBJDIR)$Pssl_io$O $(OBJDIR)$Pssl_lru$O $(OBJDIR)$Pssl_rec_cbc$O $(OBJDIR)$Pssl_rec_chapol$O $(OBJDIR)$Pssl_rec_gcm$O $(OBJDIR)$Pssl_scert_single_ec$O $(OBJDIR)$Pssl_scert_single_rsa$O $(OBJDIR)$Pssl_server$O $(OBJDIR)$Pssl_server_full_ec$O $(OBJDIR)$Pssl_server_full_rsa$O $(OBJDIR)$Pssl_server_mine2c$O $(OBJDIR)$Pssl_server_mine2g$O $(OBJDIR)$Pssl_server_minf2c$O $(OBJDIR)$Pssl_server_minf2g$O $(OBJDIR)$Pssl_server_minr2g$O $(OBJDIR)$Pssl_server_minu2g$O $(OBJDIR)$Pssl_server_minv2g$O $(OBJDIR)$Paes_big_cbcdec$O $(OBJDIR)$Paes_big_cbcenc$O $(OBJDIR)$Paes_big_ctr$O $(OBJDIR)$Paes_big_dec$O $(OBJDIR)$Paes_big_enc$O $(OBJDIR)$Paes_common$O $(OBJDIR)$Paes_ct$O $(OBJDIR)$Paes_ct64$O $(OBJDIR)$Paes_ct64_cbcdec$O $(OBJDIR)$Paes_ct64_cbcenc$O $(OBJDIR)$Paes_ct64_ctr$O $(OBJDIR)$Paes_ct64_dec$O $(OBJDIR)$Paes_ct64_enc$O $(OBJDIR)$Paes_ct_cbcdec$O $(OBJDIR)$Paes_ct_cbcenc$O $(OBJDIR)$Paes_ct_ctr$O $(OBJDIR)$Paes_ct_dec$O $(OBJDIR)$Paes_ct_enc$O $(OBJDIR)$Paes_small_cbcdec$O $(OBJDIR)$Paes_small_cbcenc$O $(OBJDIR)$Paes_small_ctr$O $(OBJDIR)$Paes_small_dec$O $(OBJDIR)$Paes_small_enc$O $(OBJDIR)$Pchacha20_ct$O $(OBJDIR)$Pdes_ct$O $(OBJDIR)$Pdes_ct_cbcdec$O $(OBJDIR)$Pdes_ct_cbcenc$O $(OBJDIR)$Pdes_support$O $(OBJDIR)$Pdes_tab$O $(OBJDIR)$Pdes_tab_cbcdec$O $(OBJDIR)$Pdes_tab_cbcenc$O $(OBJDIR)$Ppoly1305_ctmul$O $(OBJDIR)$Ppoly1305_ctmul32$O $(OBJDIR)$Ppoly1305_i15$O $(OBJDIR)$Pskey_decoder$O $(OBJDIR)$Px509_decoder$O $(OBJDIR)$Px509_knownkey$O $(OBJDIR)$Px509_minimal$O $(OBJDIR)$Px509_minimal_full$O
|
||||
OBJ = $(OBJDIR)$Pccopy$O $(OBJDIR)$Pdec16be$O $(OBJDIR)$Pdec16le$O $(OBJDIR)$Pdec32be$O $(OBJDIR)$Pdec32le$O $(OBJDIR)$Pdec64be$O $(OBJDIR)$Pdec64le$O $(OBJDIR)$Penc16be$O $(OBJDIR)$Penc16le$O $(OBJDIR)$Penc32be$O $(OBJDIR)$Penc32le$O $(OBJDIR)$Penc64be$O $(OBJDIR)$Penc64le$O $(OBJDIR)$Ppemdec$O $(OBJDIR)$Pec_all_m15$O $(OBJDIR)$Pec_all_m31$O $(OBJDIR)$Pec_c25519_i15$O $(OBJDIR)$Pec_c25519_i31$O $(OBJDIR)$Pec_c25519_m15$O $(OBJDIR)$Pec_c25519_m31$O $(OBJDIR)$Pec_curve25519$O $(OBJDIR)$Pec_p256_m15$O $(OBJDIR)$Pec_p256_m31$O $(OBJDIR)$Pec_prime_i15$O $(OBJDIR)$Pec_prime_i31$O $(OBJDIR)$Pec_secp256r1$O $(OBJDIR)$Pec_secp384r1$O $(OBJDIR)$Pec_secp521r1$O $(OBJDIR)$Pecdsa_atr$O $(OBJDIR)$Pecdsa_i15_bits$O $(OBJDIR)$Pecdsa_i15_sign_asn1$O $(OBJDIR)$Pecdsa_i15_sign_raw$O $(OBJDIR)$Pecdsa_i15_vrfy_asn1$O $(OBJDIR)$Pecdsa_i15_vrfy_raw$O $(OBJDIR)$Pecdsa_i31_bits$O $(OBJDIR)$Pecdsa_i31_sign_asn1$O $(OBJDIR)$Pecdsa_i31_sign_raw$O $(OBJDIR)$Pecdsa_i31_vrfy_asn1$O $(OBJDIR)$Pecdsa_i31_vrfy_raw$O $(OBJDIR)$Pecdsa_rta$O $(OBJDIR)$Pdig_oid$O $(OBJDIR)$Pdig_size$O $(OBJDIR)$Pghash_ctmul$O $(OBJDIR)$Pghash_ctmul32$O $(OBJDIR)$Pghash_ctmul64$O $(OBJDIR)$Pmd5$O $(OBJDIR)$Pmd5sha1$O $(OBJDIR)$Pmultihash$O $(OBJDIR)$Psha1$O $(OBJDIR)$Psha2big$O $(OBJDIR)$Psha2small$O $(OBJDIR)$Pi15_add$O $(OBJDIR)$Pi15_bitlen$O $(OBJDIR)$Pi15_decmod$O $(OBJDIR)$Pi15_decode$O $(OBJDIR)$Pi15_decred$O $(OBJDIR)$Pi15_encode$O $(OBJDIR)$Pi15_fmont$O $(OBJDIR)$Pi15_iszero$O $(OBJDIR)$Pi15_modpow$O $(OBJDIR)$Pi15_modpow2$O $(OBJDIR)$Pi15_montmul$O $(OBJDIR)$Pi15_mulacc$O $(OBJDIR)$Pi15_muladd$O $(OBJDIR)$Pi15_ninv15$O $(OBJDIR)$Pi15_reduce$O $(OBJDIR)$Pi15_rshift$O $(OBJDIR)$Pi15_sub$O $(OBJDIR)$Pi15_tmont$O $(OBJDIR)$Pi31_add$O $(OBJDIR)$Pi31_bitlen$O $(OBJDIR)$Pi31_decmod$O $(OBJDIR)$Pi31_decode$O $(OBJDIR)$Pi31_decred$O $(OBJDIR)$Pi31_encode$O $(OBJDIR)$Pi31_fmont$O $(OBJDIR)$Pi31_iszero$O $(OBJDIR)$Pi31_modpow$O $(OBJDIR)$Pi31_montmul$O $(OBJDIR)$Pi31_mulacc$O $(OBJDIR)$Pi31_muladd$O $(OBJDIR)$Pi31_ninv31$O $(OBJDIR)$Pi31_reduce$O $(OBJDIR)$Pi31_rshift$O $(OBJDIR)$Pi31_sub$O $(OBJDIR)$Pi31_tmont$O $(OBJDIR)$Pi32_add$O $(OBJDIR)$Pi32_bitlen$O $(OBJDIR)$Pi32_decmod$O $(OBJDIR)$Pi32_decode$O $(OBJDIR)$Pi32_decred$O $(OBJDIR)$Pi32_div32$O $(OBJDIR)$Pi32_encode$O $(OBJDIR)$Pi32_fmont$O $(OBJDIR)$Pi32_iszero$O $(OBJDIR)$Pi32_modpow$O $(OBJDIR)$Pi32_montmul$O $(OBJDIR)$Pi32_mulacc$O $(OBJDIR)$Pi32_muladd$O $(OBJDIR)$Pi32_ninv32$O $(OBJDIR)$Pi32_reduce$O $(OBJDIR)$Pi32_sub$O $(OBJDIR)$Pi32_tmont$O $(OBJDIR)$Phmac$O $(OBJDIR)$Phmac_ct$O $(OBJDIR)$Phmac_drbg$O $(OBJDIR)$Prsa_i15_pkcs1_sign$O $(OBJDIR)$Prsa_i15_pkcs1_vrfy$O $(OBJDIR)$Prsa_i15_priv$O $(OBJDIR)$Prsa_i15_pub$O $(OBJDIR)$Prsa_i31_pkcs1_sign$O $(OBJDIR)$Prsa_i31_pkcs1_vrfy$O $(OBJDIR)$Prsa_i31_priv$O $(OBJDIR)$Prsa_i31_pub$O $(OBJDIR)$Prsa_i32_pkcs1_sign$O $(OBJDIR)$Prsa_i32_pkcs1_vrfy$O $(OBJDIR)$Prsa_i32_priv$O $(OBJDIR)$Prsa_i32_pub$O $(OBJDIR)$Prsa_pkcs1_sig_pad$O $(OBJDIR)$Prsa_pkcs1_sig_unpad$O $(OBJDIR)$Prsa_ssl_decrypt$O $(OBJDIR)$Pprf$O $(OBJDIR)$Pprf_md5sha1$O $(OBJDIR)$Pprf_sha256$O $(OBJDIR)$Pprf_sha384$O $(OBJDIR)$Pssl_ccert_single_ec$O $(OBJDIR)$Pssl_ccert_single_rsa$O $(OBJDIR)$Pssl_client$O $(OBJDIR)$Pssl_client_full$O $(OBJDIR)$Pssl_engine$O $(OBJDIR)$Pssl_hashes$O $(OBJDIR)$Pssl_hs_client$O $(OBJDIR)$Pssl_hs_server$O $(OBJDIR)$Pssl_io$O $(OBJDIR)$Pssl_lru$O $(OBJDIR)$Pssl_rec_cbc$O $(OBJDIR)$Pssl_rec_chapol$O $(OBJDIR)$Pssl_rec_gcm$O $(OBJDIR)$Pssl_scert_single_ec$O $(OBJDIR)$Pssl_scert_single_rsa$O $(OBJDIR)$Pssl_server$O $(OBJDIR)$Pssl_server_full_ec$O $(OBJDIR)$Pssl_server_full_rsa$O $(OBJDIR)$Pssl_server_mine2c$O $(OBJDIR)$Pssl_server_mine2g$O $(OBJDIR)$Pssl_server_minf2c$O $(OBJDIR)$Pssl_server_minf2g$O $(OBJDIR)$Pssl_server_minr2g$O $(OBJDIR)$Pssl_server_minu2g$O $(OBJDIR)$Pssl_server_minv2g$O $(OBJDIR)$Paes_big_cbcdec$O $(OBJDIR)$Paes_big_cbcenc$O $(OBJDIR)$Paes_big_ctr$O $(OBJDIR)$Paes_big_dec$O $(OBJDIR)$Paes_big_enc$O $(OBJDIR)$Paes_common$O $(OBJDIR)$Paes_ct$O $(OBJDIR)$Paes_ct64$O $(OBJDIR)$Paes_ct64_cbcdec$O $(OBJDIR)$Paes_ct64_cbcenc$O $(OBJDIR)$Paes_ct64_ctr$O $(OBJDIR)$Paes_ct64_dec$O $(OBJDIR)$Paes_ct64_enc$O $(OBJDIR)$Paes_ct_cbcdec$O $(OBJDIR)$Paes_ct_cbcenc$O $(OBJDIR)$Paes_ct_ctr$O $(OBJDIR)$Paes_ct_dec$O $(OBJDIR)$Paes_ct_enc$O $(OBJDIR)$Paes_small_cbcdec$O $(OBJDIR)$Paes_small_cbcenc$O $(OBJDIR)$Paes_small_ctr$O $(OBJDIR)$Paes_small_dec$O $(OBJDIR)$Paes_small_enc$O $(OBJDIR)$Pchacha20_ct$O $(OBJDIR)$Pdes_ct$O $(OBJDIR)$Pdes_ct_cbcdec$O $(OBJDIR)$Pdes_ct_cbcenc$O $(OBJDIR)$Pdes_support$O $(OBJDIR)$Pdes_tab$O $(OBJDIR)$Pdes_tab_cbcdec$O $(OBJDIR)$Pdes_tab_cbcenc$O $(OBJDIR)$Ppoly1305_ctmul$O $(OBJDIR)$Ppoly1305_ctmul32$O $(OBJDIR)$Ppoly1305_i15$O $(OBJDIR)$Pskey_decoder$O $(OBJDIR)$Px509_decoder$O $(OBJDIR)$Px509_knownkey$O $(OBJDIR)$Px509_minimal$O $(OBJDIR)$Px509_minimal_full$O
|
||||
OBJBRSSL = $(OBJDIR)$Pbrssl$O $(OBJDIR)$Pcerts$O $(OBJDIR)$Pchain$O $(OBJDIR)$Pclient$O $(OBJDIR)$Perrors$O $(OBJDIR)$Pfiles$O $(OBJDIR)$Pkeys$O $(OBJDIR)$Pnames$O $(OBJDIR)$Pserver$O $(OBJDIR)$Pskey$O $(OBJDIR)$Psslio$O $(OBJDIR)$Pta$O $(OBJDIR)$Pvector$O $(OBJDIR)$Pverify$O $(OBJDIR)$Pxmem$O
|
||||
OBJTESTCRYPTO = $(OBJDIR)$Ptest_crypto$O
|
||||
OBJTESTSPEED = $(OBJDIR)$Ptest_speed$O
|
||||
|
|
@ -239,6 +239,9 @@ $(OBJDIR)$Pi15_iszero$O: src$Pint$Pi15_iszero.c $(HEADERSPRIV)
|
|||
$(OBJDIR)$Pi15_modpow$O: src$Pint$Pi15_modpow.c $(HEADERSPRIV)
|
||||
$(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi15_modpow$O src$Pint$Pi15_modpow.c
|
||||
|
||||
$(OBJDIR)$Pi15_modpow2$O: src$Pint$Pi15_modpow2.c $(HEADERSPRIV)
|
||||
$(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi15_modpow2$O src$Pint$Pi15_modpow2.c
|
||||
|
||||
$(OBJDIR)$Pi15_montmul$O: src$Pint$Pi15_montmul.c $(HEADERSPRIV)
|
||||
$(CC) $(CFLAGS) $(INCFLAGS) $(CCOUT)$(OBJDIR)$Pi15_montmul$O src$Pint$Pi15_montmul.c
|
||||
|
||||
|
|
|
|||
|
|
@ -109,6 +109,7 @@ coresrc=" \
|
|||
src/int/i15_fmont.c \
|
||||
src/int/i15_iszero.c \
|
||||
src/int/i15_modpow.c \
|
||||
src/int/i15_modpow2.c \
|
||||
src/int/i15_montmul.c \
|
||||
src/int/i15_mulacc.c \
|
||||
src/int/i15_muladd.c \
|
||||
|
|
|
|||
|
|
@ -1102,6 +1102,9 @@ void br_i15_to_monty(uint16_t *x, const uint16_t *m);
|
|||
void br_i15_modpow(uint16_t *x, const unsigned char *e, size_t elen,
|
||||
const uint16_t *m, uint16_t m0i, uint16_t *t1, uint16_t *t2);
|
||||
|
||||
uint32_t br_i15_modpow_opt(uint16_t *x, const unsigned char *e, size_t elen,
|
||||
const uint16_t *m, uint16_t m0i, uint16_t *tmp, size_t twlen);
|
||||
|
||||
void br_i15_encode(void *dst, size_t len, const uint16_t *x);
|
||||
|
||||
uint32_t br_i15_decode_mod(uint16_t *x,
|
||||
|
|
|
|||
159
src/int/i15_modpow2.c
Normal file
159
src/int/i15_modpow2.c
Normal file
|
|
@ -0,0 +1,159 @@
|
|||
/*
|
||||
* Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see inner.h */
|
||||
uint32_t
|
||||
br_i15_modpow_opt(uint16_t *x,
|
||||
const unsigned char *e, size_t elen,
|
||||
const uint16_t *m, uint16_t m0i, uint16_t *tmp, size_t twlen)
|
||||
{
|
||||
size_t mlen, mwlen;
|
||||
uint16_t *t1, *t2, *base;
|
||||
size_t u, v;
|
||||
uint32_t acc;
|
||||
int acc_len, win_len;
|
||||
|
||||
/*
|
||||
* Get modulus size.
|
||||
*/
|
||||
mwlen = (m[0] + 31) >> 4;
|
||||
mlen = mwlen * sizeof m[0];
|
||||
t1 = tmp;
|
||||
t2 = tmp + mwlen;
|
||||
|
||||
/*
|
||||
* Compute possible window size, with a maximum of 5 bits.
|
||||
* When the window has size 1 bit, we use a specific code
|
||||
* that requires only two temporaries. Otherwise, for a
|
||||
* window of k bits, we need 2^k+1 temporaries.
|
||||
*/
|
||||
if (twlen < (mwlen << 1)) {
|
||||
return 0;
|
||||
}
|
||||
for (win_len = 5; win_len > 1; win_len --) {
|
||||
if ((((uint32_t)1 << win_len) + 1) * mwlen <= twlen) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Everything is done in Montgomery representation.
|
||||
*/
|
||||
br_i15_to_monty(x, m);
|
||||
|
||||
/*
|
||||
* Compute window contents. If the window has size one bit only,
|
||||
* then t2 is set to x; otherwise, t2[0] is left untouched, and
|
||||
* t2[k] is set to x^k (for k >= 1).
|
||||
*/
|
||||
if (win_len == 1) {
|
||||
memcpy(t2, x, mlen);
|
||||
} else {
|
||||
memcpy(t2 + mwlen, x, mlen);
|
||||
base = t2 + mwlen;
|
||||
for (u = 2; u < ((unsigned)1 << win_len); u ++) {
|
||||
br_i15_montymul(base + mwlen, base, x, m, m0i);
|
||||
base += mwlen;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* We need to set x to 1, in Montgomery representation. This can
|
||||
* be done efficiently by setting the high word to 1, then doing
|
||||
* one word-sized shift.
|
||||
*/
|
||||
br_i15_zero(x, m[0]);
|
||||
x[mwlen - 1] = 1;
|
||||
br_i15_muladd_small(x, 0, m);
|
||||
|
||||
/*
|
||||
* We process bits from most to least significant. At each
|
||||
* loop iteration, we have acc_len bits in acc.
|
||||
*/
|
||||
acc = 0;
|
||||
acc_len = 0;
|
||||
while (acc_len > 0 || elen > 0) {
|
||||
int i, k;
|
||||
uint32_t bits;
|
||||
|
||||
/*
|
||||
* Get the next bits.
|
||||
*/
|
||||
k = win_len;
|
||||
if (acc_len < win_len) {
|
||||
if (elen > 0) {
|
||||
acc = (acc << 8) | *e ++;
|
||||
elen --;
|
||||
acc_len += 8;
|
||||
} else {
|
||||
k = acc_len;
|
||||
}
|
||||
}
|
||||
bits = (acc >> (acc_len - k)) & (((uint32_t)1 << k) - 1);
|
||||
acc_len -= k;
|
||||
|
||||
/*
|
||||
* We could get exactly k bits. Compute k squarings.
|
||||
*/
|
||||
for (i = 0; i < k; i ++) {
|
||||
br_i15_montymul(t1, x, x, m, m0i);
|
||||
memcpy(x, t1, mlen);
|
||||
}
|
||||
|
||||
/*
|
||||
* Window lookup: we want to set t2 to the window
|
||||
* lookup value, assuming the bits are non-zero. If
|
||||
* the window length is 1 bit only, then t2 is
|
||||
* already set; otherwise, we do a constant-time lookup.
|
||||
*/
|
||||
if (win_len > 1) {
|
||||
br_i15_zero(t2, m[0]);
|
||||
base = t2 + mwlen;
|
||||
for (u = 1; u < ((uint32_t)1 << k); u ++) {
|
||||
uint32_t m;
|
||||
|
||||
m = -EQ(u, bits);
|
||||
for (v = 1; v < mwlen; v ++) {
|
||||
t2[v] |= m & base[v];
|
||||
}
|
||||
base += mwlen;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Multiply with the looked-up value. We keep the
|
||||
* product only if the exponent bits are not all-zero.
|
||||
*/
|
||||
br_i15_montymul(t1, x, t2, m, m0i);
|
||||
CCOPY(NEQ(bits, 0), x, t1, mlen);
|
||||
}
|
||||
|
||||
/*
|
||||
* Convert back from Montgomery representation, and exit.
|
||||
*/
|
||||
br_i15_from_monty(x, m, m0i);
|
||||
return 1;
|
||||
}
|
||||
|
|
@ -25,6 +25,25 @@
|
|||
#include "inner.h"
|
||||
|
||||
#define U (1 + ((BR_MAX_RSA_FACTOR + 14) / 15))
|
||||
#define TLEN (8 * U)
|
||||
|
||||
/* obsolete
|
||||
static void
|
||||
print_int(const char *name, const uint16_t *x)
|
||||
{
|
||||
extern int printf(const char *fmt, ...);
|
||||
unsigned char tmp[1000];
|
||||
size_t u, len;
|
||||
|
||||
len = (x[0] - (x[0] >> 4) + 7) >> 3;
|
||||
br_i15_encode(tmp, len, x);
|
||||
printf("%s = ", name);
|
||||
for (u = 0; u < len; u ++) {
|
||||
printf("%02X", tmp[u]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
*/
|
||||
|
||||
/* see bearssl_rsa.h */
|
||||
uint32_t
|
||||
|
|
@ -32,30 +51,18 @@ br_rsa_i15_private(unsigned char *x, const br_rsa_private_key *sk)
|
|||
{
|
||||
const unsigned char *p, *q;
|
||||
size_t plen, qlen;
|
||||
uint16_t tmp[6 * U];
|
||||
uint16_t *mp, *mq, *s1, *s2, *t1, *t2, *t3;
|
||||
size_t fwlen;
|
||||
uint16_t p0i, q0i;
|
||||
size_t xlen;
|
||||
uint16_t tmp[TLEN];
|
||||
long z;
|
||||
uint16_t *mp, *mq, *s1, *s2, *t1, *t2, *t3;
|
||||
uint32_t r;
|
||||
|
||||
/*
|
||||
* All our temporary buffers are from the tmp[] array.
|
||||
*
|
||||
* The mp, mq, s1, s2, t1 and t2 buffers are large enough to
|
||||
* contain a RSA factor. The t3 buffer can contain a complete
|
||||
* RSA modulus. t3 shares its storage space with s2, s1 and t1,
|
||||
* in that order (this is important, see below).
|
||||
*/
|
||||
mq = tmp;
|
||||
mp = tmp + U;
|
||||
t2 = tmp + 2 * U;
|
||||
s2 = tmp + 3 * U;
|
||||
s1 = tmp + 4 * U;
|
||||
t1 = tmp + 5 * U;
|
||||
t3 = s2;
|
||||
|
||||
/*
|
||||
* Compute the actual lengths (in bytes) of p and q, and check
|
||||
* that they fit within our stack buffers.
|
||||
* Compute the actual lengths of p and q, in bytes.
|
||||
* These lengths are not considered secret (we cannot really hide
|
||||
* them anyway in constant-time code).
|
||||
*/
|
||||
p = sk->p;
|
||||
plen = sk->plen;
|
||||
|
|
@ -69,17 +76,23 @@ br_rsa_i15_private(unsigned char *x, const br_rsa_private_key *sk)
|
|||
q ++;
|
||||
qlen --;
|
||||
}
|
||||
if (plen > (BR_MAX_RSA_FACTOR >> 3)
|
||||
|| qlen > (BR_MAX_RSA_FACTOR >> 3))
|
||||
{
|
||||
return 0;
|
||||
|
||||
/*
|
||||
* Compute the maximum factor length, in words.
|
||||
*/
|
||||
z = (long)(plen > qlen ? plen : qlen) << 3;
|
||||
fwlen = 1;
|
||||
while (z > 0) {
|
||||
z -= 15;
|
||||
fwlen ++;
|
||||
}
|
||||
|
||||
/*
|
||||
* Decode p and q.
|
||||
* We need to fit at least 6 values in the stack buffer.
|
||||
*/
|
||||
br_i15_decode(mp, p, plen);
|
||||
br_i15_decode(mq, q, qlen);
|
||||
if (6 * fwlen > TLEN) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Compute signature length (in bytes).
|
||||
|
|
@ -87,18 +100,34 @@ br_rsa_i15_private(unsigned char *x, const br_rsa_private_key *sk)
|
|||
xlen = (sk->n_bitlen + 7) >> 3;
|
||||
|
||||
/*
|
||||
* Compute s1 = x^dp mod p.
|
||||
* Decode q.
|
||||
*/
|
||||
p0i = br_i15_ninv15(mp[1]);
|
||||
br_i15_decode_reduce(s1, x, xlen, mp);
|
||||
br_i15_modpow(s1, sk->dp, sk->dplen, mp, p0i, t1, t2);
|
||||
mq = tmp;
|
||||
br_i15_decode(mq, q, qlen);
|
||||
|
||||
/*
|
||||
* Compute s2 = x^dq mod q.
|
||||
*/
|
||||
q0i = br_i15_ninv15(mq[1]);
|
||||
s2 = tmp + fwlen;
|
||||
br_i15_decode_reduce(s2, x, xlen, mq);
|
||||
br_i15_modpow(s2, sk->dq, sk->dqlen, mq, q0i, t1, t2);
|
||||
r = br_i15_modpow_opt(s2, sk->dq, sk->dqlen, mq, q0i,
|
||||
tmp + 2 * fwlen, TLEN - 2 * fwlen);
|
||||
|
||||
/*
|
||||
* Decode p.
|
||||
*/
|
||||
mp = tmp + 2 * fwlen;
|
||||
br_i15_decode(mp, p, plen);
|
||||
|
||||
/*
|
||||
* Compute s1 = x^dq mod q.
|
||||
*/
|
||||
p0i = br_i15_ninv15(mp[1]);
|
||||
s1 = tmp + 3 * fwlen;
|
||||
br_i15_decode_reduce(s1, x, xlen, mp);
|
||||
r &= br_i15_modpow_opt(s1, sk->dp, sk->dplen, mp, p0i,
|
||||
tmp + 4 * fwlen, TLEN - 4 * fwlen);
|
||||
|
||||
/*
|
||||
* Compute:
|
||||
|
|
@ -113,6 +142,8 @@ br_rsa_i15_private(unsigned char *x, const br_rsa_private_key *sk)
|
|||
* inverse of q modulo p), we also tolerate improperly large
|
||||
* values for this parameter.
|
||||
*/
|
||||
t1 = tmp + 4 * fwlen;
|
||||
t2 = tmp + 5 * fwlen;
|
||||
br_i15_reduce(t2, s2, mp);
|
||||
br_i15_add(s1, mp, br_i15_sub(s1, t2, 1));
|
||||
br_i15_to_monty(s1, mp);
|
||||
|
|
@ -125,11 +156,13 @@ br_rsa_i15_private(unsigned char *x, const br_rsa_private_key *sk)
|
|||
* All these operations are non-modular.
|
||||
*
|
||||
* We need mq, s2 and t2. We use the t3 buffer as destination.
|
||||
* The buffers mp, s1 and t1 are no longer needed. Moreover,
|
||||
* the first step is to copy s2 into the destination buffer t3.
|
||||
* We thus arranged for t3 to actually share space with s2, and
|
||||
* to be followed by the space formerly used by s1 and t1.
|
||||
* The buffers mp, s1 and t1 are no longer needed, so we can
|
||||
* reuse them for t3. Moreover, the first step of the computation
|
||||
* is to copy s2 into t3, after which s2 is not needed. Right
|
||||
* now, mq is in slot 0, s2 is in slot 1, and t2 in slot 5.
|
||||
* Therefore, we have ample room for t3 by simply using s2.
|
||||
*/
|
||||
t3 = s2;
|
||||
br_i15_mulacc(t3, mq, t2);
|
||||
|
||||
/*
|
||||
|
|
@ -142,5 +175,5 @@ br_rsa_i15_private(unsigned char *x, const br_rsa_private_key *sk)
|
|||
* The only error conditions remaining at that point are invalid
|
||||
* values for p and q (even integers).
|
||||
*/
|
||||
return p0i & q0i & 1;
|
||||
return p0i & q0i & r;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,6 +24,12 @@
|
|||
|
||||
#include "inner.h"
|
||||
|
||||
/*
|
||||
* As a strict minimum, we need four buffers that can hold a
|
||||
* modular integer.
|
||||
*/
|
||||
#define TLEN (4 * (1 + ((BR_MAX_RSA_SIZE + 14) / 15)))
|
||||
|
||||
/* see bearssl_rsa.h */
|
||||
uint32_t
|
||||
br_rsa_i15_public(unsigned char *x, size_t xlen,
|
||||
|
|
@ -31,10 +37,10 @@ br_rsa_i15_public(unsigned char *x, size_t xlen,
|
|||
{
|
||||
const unsigned char *n;
|
||||
size_t nlen;
|
||||
uint16_t m[1 + ((BR_MAX_RSA_SIZE + 14) / 15)];
|
||||
uint16_t a[1 + ((BR_MAX_RSA_SIZE + 14) / 15)];
|
||||
uint16_t t1[1 + ((BR_MAX_RSA_SIZE + 14) / 15)];
|
||||
uint16_t t2[1 + ((BR_MAX_RSA_SIZE + 14) / 15)];
|
||||
uint16_t tmp[TLEN];
|
||||
uint16_t *m, *a, *t;
|
||||
size_t fwlen;
|
||||
long z;
|
||||
uint16_t m0i;
|
||||
uint32_t r;
|
||||
|
||||
|
|
@ -51,6 +57,25 @@ br_rsa_i15_public(unsigned char *x, size_t xlen,
|
|||
if (nlen == 0 || nlen > (BR_MAX_RSA_SIZE >> 3) || xlen != nlen) {
|
||||
return 0;
|
||||
}
|
||||
z = (long)nlen << 3;
|
||||
fwlen = 1;
|
||||
while (z > 0) {
|
||||
z -= 15;
|
||||
fwlen ++;
|
||||
}
|
||||
|
||||
/*
|
||||
* The modulus gets decoded into m[].
|
||||
* The value to exponentiate goes into a[].
|
||||
* The temporaries for modular exponentiations are in t[].
|
||||
*/
|
||||
m = tmp;
|
||||
a = tmp + fwlen;
|
||||
t = tmp + 2 * fwlen;
|
||||
|
||||
/*
|
||||
* Decode the modulus.
|
||||
*/
|
||||
br_i15_decode(m, n, nlen);
|
||||
m0i = br_i15_ninv15(m[1]);
|
||||
|
||||
|
|
@ -68,7 +93,7 @@ br_rsa_i15_public(unsigned char *x, size_t xlen,
|
|||
/*
|
||||
* Compute the modular exponentiation.
|
||||
*/
|
||||
br_i15_modpow(a, pk->e, pk->elen, m, m0i, t1, t2);
|
||||
br_i15_modpow_opt(a, pk->e, pk->elen, m, m0i, t, TLEN - 2 * fwlen);
|
||||
|
||||
/*
|
||||
* Encode the result.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue