tls: avoid using int16 in pstm code

function                                             old     new   delta
pstm_div                                            1472    1522     +50
psRsaEncryptPub                                      403     413     +10
pstm_2expt                                            91      96      +5
pstm_clear                                            68      72      +4
pstm_init                                             39      42      +3
pstm_unsigned_bin_size                                36      37      +1
pstm_montgomery_reduce                               398     399      +1
pstm_init_size                                        45      46      +1
pstm_zero                                             39      38      -1
pstm_set                                              35      34      -1
pstm_read_unsigned_bin                               112     109      -3
pstm_mulmod                                          123     120      -3
pstm_mod                                             116     113      -3
pstm_cmp                                              57      54      -3
pstm_sub                                             107     102      -5
pstm_to_unsigned_bin                                 157     151      -6
pstm_clamp                                            63      57      -6
pstm_add                                             116     108      -8
pstm_grow                                             81      72      -9
pstm_count_bits                                       57      48      -9
pstm_init_copy                                        84      72     -12
pstm_cmp_mag                                          93      78     -15
pstm_sqr_comba                                       567     551     -16
pstm_montgomery_calc_normalization                   158     140     -18
pstm_copy                                            115      92     -23
pstm_lshd                                            133     109     -24
pstm_mul_comba                                       525     500     -25
pstm_mul_d                                           251     224     -27
s_pstm_sub                                           256     228     -28
s_pstm_add                                           370     337     -33
pstm_div_2d                                          444     409     -35
pstm_mul_2                                           195     156     -39
pstm_rshd                                            154     104     -50
pstm_mul_2d                                          247     186     -61
pstm_exptmod                                        1524    1463     -61
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 8/27 up/down: 75/-524)         Total: -449 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2017-04-03 21:53:29 +02:00
parent 636c3b627c
commit 229d3c467d
3 changed files with 49 additions and 49 deletions

View File

@ -475,7 +475,7 @@ static void prf_hmac_sha256(/*tls_state_t *tls,*/
uint8_t a[TLS_MAX_MAC_SIZE]; uint8_t a[TLS_MAX_MAC_SIZE];
uint8_t *out_p = outbuf; uint8_t *out_p = outbuf;
unsigned label_size = strlen(label); unsigned label_size = strlen(label);
unsigned MAC_size = SHA256_OUTSIZE;///tls->MAC_size; unsigned MAC_size = SHA256_OUTSIZE;
/* In P_hash() calculation, "seed" is "label + seed": */ /* In P_hash() calculation, "seed" is "label + seed": */
#define SEED label, label_size, seed, seed_size #define SEED label, label_size, seed, seed_size
@ -486,7 +486,7 @@ static void prf_hmac_sha256(/*tls_state_t *tls,*/
hmac_sha256(/*tls,*/ a, SECRET, SEED, NULL); hmac_sha256(/*tls,*/ a, SECRET, SEED, NULL);
//TODO: convert hmac to precomputed //TODO: convert hmac to precomputed
for(;;) { for (;;) {
/* HMAC_hash(secret, A(1) + seed) */ /* HMAC_hash(secret, A(1) + seed) */
if (outbuf_size <= MAC_size) { if (outbuf_size <= MAC_size) {
/* Last, possibly incomplete, block */ /* Last, possibly incomplete, block */

View File

@ -47,7 +47,7 @@
//#include "../cryptoApi.h" //#include "../cryptoApi.h"
#ifndef DISABLE_PSTM #ifndef DISABLE_PSTM
static int32 pstm_mul_2d(pstm_int *a, int16 b, pstm_int *c); static int32 pstm_mul_2d(pstm_int *a, int b, pstm_int *c); //bbox: was int16 b
/******************************************************************************/ /******************************************************************************/
/* /*
@ -64,7 +64,7 @@ int32 pstm_init_size(psPool_t *pool, pstm_int * a, uint32 size)
a->dp = xzalloc(sizeof (pstm_digit) * size);//bbox a->dp = xzalloc(sizeof (pstm_digit) * size);//bbox
//bbox a->pool = pool; //bbox a->pool = pool;
a->used = 0; a->used = 0;
a->alloc = (int16)size; a->alloc = size;
a->sign = PSTM_ZPOS; a->sign = PSTM_ZPOS;
/* /*
zero the digits zero the digits
@ -111,9 +111,9 @@ int32 pstm_init(psPool_t *pool, pstm_int * a)
/* /*
Grow as required Grow as required
*/ */
int32 pstm_grow(pstm_int * a, int16 size) int32 pstm_grow(pstm_int * a, int size)
{ {
int16 i; int i; //bbox: was int16
pstm_digit *tmp; pstm_digit *tmp;
/* /*
@ -298,7 +298,7 @@ void pstm_zero(pstm_int * a)
*/ */
int32 pstm_cmp_mag(pstm_int * a, pstm_int * b) int32 pstm_cmp_mag(pstm_int * a, pstm_int * b)
{ {
int16 n; int n; //bbox: was int16
pstm_digit *tmpa, *tmpb; pstm_digit *tmpa, *tmpb;
/* /*
@ -406,7 +406,7 @@ int32 pstm_read_unsigned_bin(pstm_int *a, unsigned char *b, int32 c)
c -= excess; c -= excess;
b += excess; b += excess;
} }
a->used = (int16)((c + sizeof(pstm_digit) - 1)/sizeof(pstm_digit)); a->used = ((c + sizeof(pstm_digit) - 1)/sizeof(pstm_digit));
if (a->alloc < a->used) { if (a->alloc < a->used) {
if (pstm_grow(a, a->used) != PSTM_OKAY) { if (pstm_grow(a, a->used) != PSTM_OKAY) {
return PSTM_MEM; return PSTM_MEM;
@ -460,9 +460,9 @@ int32 pstm_read_unsigned_bin(pstm_int *a, unsigned char *b, int32 c)
/******************************************************************************/ /******************************************************************************/
/* /*
*/ */
int16 pstm_count_bits (pstm_int * a) int pstm_count_bits (pstm_int * a)
{ {
int16 r; int r; //bbox: was int16
pstm_digit q; pstm_digit q;
if (a->used == 0) { if (a->used == 0) {
@ -500,9 +500,9 @@ void pstm_set(pstm_int *a, pstm_digit b)
/* /*
Right shift Right shift
*/ */
void pstm_rshd(pstm_int *a, int16 x) void pstm_rshd(pstm_int *a, int x)
{ {
int16 y; int y; //bbox: was int16
/* too many digits just zero and return */ /* too many digits just zero and return */
if (x >= a->used) { if (x >= a->used) {
@ -529,9 +529,9 @@ void pstm_rshd(pstm_int *a, int16 x)
/* /*
Shift left a certain amount of digits. Shift left a certain amount of digits.
*/ */
int32 pstm_lshd(pstm_int * a, int16 b) int32 pstm_lshd(pstm_int * a, int b)
{ {
int16 x; int x; //bbox: was int16
int32 res; int32 res;
/* /*
@ -582,9 +582,9 @@ int32 pstm_lshd(pstm_int * a, int16 b)
/* /*
computes a = 2**b computes a = 2**b
*/ */
int32 pstm_2expt(pstm_int *a, int16 b) int32 pstm_2expt(pstm_int *a, int b)
{ {
int16 z; int z; //bbox: was int16
/* zero a as per default */ /* zero a as per default */
pstm_zero (a); pstm_zero (a);
@ -619,7 +619,7 @@ int32 pstm_2expt(pstm_int *a, int16 b)
int32 pstm_mul_2(pstm_int * a, pstm_int * b) int32 pstm_mul_2(pstm_int * a, pstm_int * b)
{ {
int32 res; int32 res;
int16 x, oldused; int x, oldused; //bbox: was int16
/* /*
grow to accomodate result grow to accomodate result
@ -684,7 +684,7 @@ int32 pstm_mul_2(pstm_int * a, pstm_int * b)
*/ */
int32 s_pstm_sub(pstm_int *a, pstm_int *b, pstm_int *c) int32 s_pstm_sub(pstm_int *a, pstm_int *b, pstm_int *c)
{ {
int16 oldbused, oldused; int oldbused, oldused; //bbox: was int16
int32 x; int32 x;
pstm_word t; pstm_word t;
@ -724,7 +724,7 @@ int32 s_pstm_sub(pstm_int *a, pstm_int *b, pstm_int *c)
*/ */
static int32 s_pstm_add(pstm_int *a, pstm_int *b, pstm_int *c) static int32 s_pstm_add(pstm_int *a, pstm_int *b, pstm_int *c)
{ {
int16 x, y, oldused; int x, y, oldused; //bbox: was int16
register pstm_word t, adp, bdp; register pstm_word t, adp, bdp;
y = a->used; y = a->used;
@ -781,8 +781,8 @@ static int32 s_pstm_add(pstm_int *a, pstm_int *b, pstm_int *c)
*/ */
int32 pstm_sub(pstm_int *a, pstm_int *b, pstm_int *c) int32 pstm_sub(pstm_int *a, pstm_int *b, pstm_int *c)
{ {
int32 res; int32 res;
int16 sa, sb; int sa, sb; //bbox: was int16
sa = a->sign; sa = a->sign;
sb = b->sign; sb = b->sign;
@ -881,7 +881,7 @@ int32 pstm_montgomery_setup(pstm_int *a, pstm_digit *rho)
int32 pstm_montgomery_calc_normalization(pstm_int *a, pstm_int *b) int32 pstm_montgomery_calc_normalization(pstm_int *a, pstm_int *b)
{ {
int32 x; int32 x;
int16 bits; int bits; //bbox: was int16
/* how many bits of last digit does b use */ /* how many bits of last digit does b use */
bits = pstm_count_bits (b) % DIGIT_BIT; bits = pstm_count_bits (b) % DIGIT_BIT;
@ -916,10 +916,10 @@ int32 pstm_montgomery_calc_normalization(pstm_int *a, pstm_int *b)
/* /*
c = a * 2**d c = a * 2**d
*/ */
static int32 pstm_mul_2d(pstm_int *a, int16 b, pstm_int *c) static int32 pstm_mul_2d(pstm_int *a, int b, pstm_int *c)
{ {
pstm_digit carry, carrytmp, shift; pstm_digit carry, carrytmp, shift;
int16 x; int x; //bbox: was int16
/* copy it */ /* copy it */
if (pstm_copy(a, c) != PSTM_OKAY) { if (pstm_copy(a, c) != PSTM_OKAY) {
@ -961,9 +961,9 @@ static int32 pstm_mul_2d(pstm_int *a, int16 b, pstm_int *c)
/* /*
c = a mod 2**d c = a mod 2**d
*/ */
static int32 pstm_mod_2d(pstm_int *a, int16 b, pstm_int *c) static int32 pstm_mod_2d(pstm_int *a, int b, pstm_int *c) //bbox: was int16 b
{ {
int16 x; int x; //bbox: was int16
/* zero if count less than or equal to zero */ /* zero if count less than or equal to zero */
if (b <= 0) { if (b <= 0) {
@ -1001,7 +1001,7 @@ int32 pstm_mul_d(pstm_int *a, pstm_digit b, pstm_int *c)
{ {
pstm_word w; pstm_word w;
int32 res; int32 res;
int16 x, oldused; int x, oldused; //bbox: was int16
if (c->alloc < a->used + 1) { if (c->alloc < a->used + 1) {
if ((res = pstm_grow (c, a->used + 1)) != PSTM_OKAY) { if ((res = pstm_grow (c, a->used + 1)) != PSTM_OKAY) {
@ -1032,12 +1032,12 @@ int32 pstm_mul_d(pstm_int *a, pstm_digit b, pstm_int *c)
/* /*
c = a / 2**b c = a / 2**b
*/ */
int32 pstm_div_2d(psPool_t *pool, pstm_int *a, int16 b, pstm_int *c, int32 pstm_div_2d(psPool_t *pool, pstm_int *a, int b, pstm_int *c,
pstm_int *d) pstm_int *d)
{ {
pstm_digit D, r, rr; pstm_digit D, r, rr;
int32 res; int32 res;
int16 x; int x; //bbox: was int16
pstm_int t; pstm_int t;
/* if the shift count is <= 0 then we do no work */ /* if the shift count is <= 0 then we do no work */
@ -1120,7 +1120,7 @@ LBL_DONE:
*/ */
int32 pstm_div_2(pstm_int * a, pstm_int * b) int32 pstm_div_2(pstm_int * a, pstm_int * b)
{ {
int16 x, oldused; int x, oldused; //bbox: was int16
if (b->alloc < a->used) { if (b->alloc < a->used) {
if (pstm_grow(b, a->used) != PSTM_OKAY) { if (pstm_grow(b, a->used) != PSTM_OKAY) {
@ -1166,9 +1166,9 @@ int32 pstm_div_2(pstm_int * a, pstm_int * b)
/* /*
Creates "a" then copies b into it Creates "a" then copies b into it
*/ */
int32 pstm_init_copy(psPool_t *pool, pstm_int * a, pstm_int * b, int16 toSqr) int32 pstm_init_copy(psPool_t *pool, pstm_int * a, pstm_int * b, int toSqr)
{ {
int16 x; int x; //bbox: was int16
int32 res; int32 res;
if (a == b) { if (a == b) {
@ -1279,7 +1279,7 @@ int32 pstm_div(psPool_t *pool, pstm_int *a, pstm_int *b, pstm_int *c,
{ {
pstm_int q, x, y, t1, t2; pstm_int q, x, y, t1, t2;
int32 res; int32 res;
int16 n, t, i, norm, neg; int n, t, i, norm, neg; //bbox: was int16
/* is divisor zero ? */ /* is divisor zero ? */
if (pstm_iszero (b) == 1) { if (pstm_iszero (b) == 1) {
@ -1531,7 +1531,7 @@ int32 pstm_mulmod(psPool_t *pool, pstm_int *a, pstm_int *b, pstm_int *c,
pstm_int *d) pstm_int *d)
{ {
int32 res; int32 res;
int16 size; int size; //bbox: was int16
pstm_int tmp; pstm_int tmp;
/* /*
@ -1567,7 +1567,7 @@ int32 pstm_exptmod(psPool_t *pool, pstm_int *G, pstm_int *X, pstm_int *P,
pstm_digit buf, mp; pstm_digit buf, mp;
pstm_digit *paD; pstm_digit *paD;
int32 err, bitbuf; int32 err, bitbuf;
int16 bitcpy, bitcnt, mode, digidx, x, y, winsize; int bitcpy, bitcnt, mode, digidx, x, y, winsize; //bbox: was int16
uint32 paDlen; uint32 paDlen;
/* set window size from what user set as optimization */ /* set window size from what user set as optimization */
@ -1804,7 +1804,7 @@ LBL_RES:pstm_clear(&res);
int32 pstm_add(pstm_int *a, pstm_int *b, pstm_int *c) int32 pstm_add(pstm_int *a, pstm_int *b, pstm_int *c)
{ {
int32 res; int32 res;
int16 sa, sb; int sa, sb; //bbox: was int16
/* get sign of both inputs */ /* get sign of both inputs */
sa = a->sign; sa = a->sign;
@ -1817,7 +1817,7 @@ int32 pstm_add(pstm_int *a, pstm_int *b, pstm_int *c)
if ((res = s_pstm_add (a, b, c)) != PSTM_OKAY) { if ((res = s_pstm_add (a, b, c)) != PSTM_OKAY) {
return res; return res;
} }
} else { } else {
/* /*
one positive, the other negative one positive, the other negative
subtract the one with the greater magnitude from the one of the lesser subtract the one with the greater magnitude from the one of the lesser
@ -1842,7 +1842,7 @@ int32 pstm_add(pstm_int *a, pstm_int *b, pstm_int *c)
/* /*
reverse an array, used for radix code reverse an array, used for radix code
*/ */
static void pstm_reverse (unsigned char *s, int16 len) static void pstm_reverse (unsigned char *s, int len) //bbox: was int16 len
{ {
int32 ix, iy; int32 ix, iy;
unsigned char t; unsigned char t;
@ -1865,7 +1865,7 @@ static void pstm_reverse (unsigned char *s, int16 len)
int32 pstm_to_unsigned_bin_nr(psPool_t *pool, pstm_int *a, unsigned char *b) int32 pstm_to_unsigned_bin_nr(psPool_t *pool, pstm_int *a, unsigned char *b)
{ {
int32 res; int32 res;
int16 x; int x; //bbox: was int16
pstm_int t = { 0 }; pstm_int t = { 0 };
if ((res = pstm_init_copy(pool, &t, a, 0)) != PSTM_OKAY) { if ((res = pstm_init_copy(pool, &t, a, 0)) != PSTM_OKAY) {
@ -1890,7 +1890,7 @@ int32 pstm_to_unsigned_bin_nr(psPool_t *pool, pstm_int *a, unsigned char *b)
int32 pstm_to_unsigned_bin(psPool_t *pool, pstm_int *a, unsigned char *b) int32 pstm_to_unsigned_bin(psPool_t *pool, pstm_int *a, unsigned char *b)
{ {
int32 res; int32 res;
int16 x; int x; //bbox: was int16
pstm_int t = { 0 }; pstm_int t = { 0 };
if ((res = pstm_init_copy(pool, &t, a, 0)) != PSTM_OKAY) { if ((res = pstm_init_copy(pool, &t, a, 0)) != PSTM_OKAY) {

View File

@ -122,7 +122,7 @@
#define PSTM_MAX_SIZE 4096 #define PSTM_MAX_SIZE 4096
typedef struct { typedef struct {
int16 used, alloc, sign; int used, alloc, sign; //bbox: was int16
pstm_digit *dp; pstm_digit *dp;
//bbox psPool_t *pool; //bbox psPool_t *pool;
} pstm_int; } pstm_int;
@ -154,9 +154,9 @@ extern int32 pstm_init_size(psPool_t *pool, pstm_int * a, uint32 size);
#define pstm_init_copy(pool, a, b, toSqr) \ #define pstm_init_copy(pool, a, b, toSqr) \
pstm_init_copy( a, b, toSqr) pstm_init_copy( a, b, toSqr)
extern int32 pstm_init_copy(psPool_t *pool, pstm_int * a, pstm_int * b, extern int32 pstm_init_copy(psPool_t *pool, pstm_int * a, pstm_int * b,
int16 toSqr); int toSqr); //bbox: was int16 toSqr
extern int16 pstm_count_bits (pstm_int * a); extern int pstm_count_bits (pstm_int * a); //bbox: was returning int16
//bbox: pool unused //bbox: pool unused
#define pstm_init_for_read_unsigned_bin(pool, a, len) \ #define pstm_init_for_read_unsigned_bin(pool, a, len) \
@ -178,7 +178,7 @@ extern void pstm_clear_multi(pstm_int *mp0, pstm_int *mp1, pstm_int *mp2,
pstm_int *mp3, pstm_int *mp4, pstm_int *mp5, pstm_int *mp6, pstm_int *mp3, pstm_int *mp4, pstm_int *mp5, pstm_int *mp6,
pstm_int *mp7); pstm_int *mp7);
extern int32 pstm_grow(pstm_int * a, int16 size); extern int32 pstm_grow(pstm_int * a, int size); //bbox: was int16 size
extern void pstm_clamp(pstm_int * a); extern void pstm_clamp(pstm_int * a);
@ -186,9 +186,9 @@ extern int32 pstm_cmp(pstm_int * a, pstm_int * b);
extern int32 pstm_cmp_mag(pstm_int * a, pstm_int * b); extern int32 pstm_cmp_mag(pstm_int * a, pstm_int * b);
extern void pstm_rshd(pstm_int *a, int16 x); extern void pstm_rshd(pstm_int *a, int x); //bbox: was int16 x
extern int32 pstm_lshd(pstm_int * a, int16 b); extern int32 pstm_lshd(pstm_int * a, int b); //bbox: was int16 b
//bbox: pool unused //bbox: pool unused
#define pstm_div(pool, a, b, c, d) \ #define pstm_div(pool, a, b, c, d) \
@ -199,8 +199,8 @@ extern int32 pstm_div(psPool_t *pool, pstm_int *a, pstm_int *b, pstm_int *c,
//bbox: pool unused //bbox: pool unused
#define pstm_div_2d(pool, a, b, c, d) \ #define pstm_div_2d(pool, a, b, c, d) \
pstm_div_2d( a, b, c, d) pstm_div_2d( a, b, c, d)
extern int32 pstm_div_2d(psPool_t *pool, pstm_int *a, int16 b, pstm_int *c, extern int32 pstm_div_2d(psPool_t *pool, pstm_int *a, int b, pstm_int *c,
pstm_int *d); pstm_int *d); //bbox: was int16 b
extern int32 pstm_div_2(pstm_int * a, pstm_int * b); extern int32 pstm_div_2(pstm_int * a, pstm_int * b);
@ -232,7 +232,7 @@ extern int32 pstm_mulmod(psPool_t *pool, pstm_int *a, pstm_int *b, pstm_int *c,
extern int32 pstm_exptmod(psPool_t *pool, pstm_int *G, pstm_int *X, pstm_int *P, extern int32 pstm_exptmod(psPool_t *pool, pstm_int *G, pstm_int *X, pstm_int *P,
pstm_int *Y); pstm_int *Y);
extern int32 pstm_2expt(pstm_int *a, int16 b); extern int32 pstm_2expt(pstm_int *a, int b); //bbox: was int16 b
extern int32 pstm_add(pstm_int *a, pstm_int *b, pstm_int *c); extern int32 pstm_add(pstm_int *a, pstm_int *b, pstm_int *c);