| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
/** |
|
2
|
|
|
|
|
|
|
* @file pstmnt.c |
|
3
|
|
|
|
|
|
|
* @version 950bba4 (HEAD -> master) |
|
4
|
|
|
|
|
|
|
* |
|
5
|
|
|
|
|
|
|
* Multiprecision number implementation: constant time montgomery. |
|
6
|
|
|
|
|
|
|
*/ |
|
7
|
|
|
|
|
|
|
/* |
|
8
|
|
|
|
|
|
|
* Copyright (c) 2013-2017 INSIDE Secure Corporation |
|
9
|
|
|
|
|
|
|
* Copyright (c) PeerSec Networks, 2002-2011 |
|
10
|
|
|
|
|
|
|
* All Rights Reserved |
|
11
|
|
|
|
|
|
|
* |
|
12
|
|
|
|
|
|
|
* The latest version of this code is available at http://www.matrixssl.org |
|
13
|
|
|
|
|
|
|
* |
|
14
|
|
|
|
|
|
|
* This software is open source; you can redistribute it and/or modify |
|
15
|
|
|
|
|
|
|
* it under the terms of the GNU General Public License as published by |
|
16
|
|
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
|
17
|
|
|
|
|
|
|
* (at your option) any later version. |
|
18
|
|
|
|
|
|
|
* |
|
19
|
|
|
|
|
|
|
* This General Public License does NOT permit incorporating this software |
|
20
|
|
|
|
|
|
|
* into proprietary programs. If you are unable to comply with the GPL, a |
|
21
|
|
|
|
|
|
|
* commercial license for this software may be purchased from INSIDE at |
|
22
|
|
|
|
|
|
|
* http://www.insidesecure.com/ |
|
23
|
|
|
|
|
|
|
* |
|
24
|
|
|
|
|
|
|
* This program is distributed in WITHOUT ANY WARRANTY; without even the |
|
25
|
|
|
|
|
|
|
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
26
|
|
|
|
|
|
|
* See the GNU General Public License for more details. |
|
27
|
|
|
|
|
|
|
* |
|
28
|
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License |
|
29
|
|
|
|
|
|
|
* along with this program; if not, write to the Free Software |
|
30
|
|
|
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
31
|
|
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html |
|
32
|
|
|
|
|
|
|
*/ |
|
33
|
|
|
|
|
|
|
/******************************************************************************/ |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
#ifndef INCLUDE_GUARD_PSTMNT_H |
|
36
|
|
|
|
|
|
|
# define INCLUDE_GUARD_PSTMNT_H |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# include "../cryptoApi.h" |
|
39
|
|
|
|
|
|
|
# include "pstm.h" |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
/* Before defining pstmnt, ensure pstm has been processed. |
|
42
|
|
|
|
|
|
|
In some configurations the pstm is disabled, |
|
43
|
|
|
|
|
|
|
which also disables pstmnt. */ |
|
44
|
|
|
|
|
|
|
# ifndef PSTM_AVAILABLE |
|
45
|
|
|
|
|
|
|
# undef USE_CONSTANT_TIME_MODEXP |
|
46
|
|
|
|
|
|
|
# endif /* PSTM_AVAILABLE */ |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# ifdef USE_CONSTANT_TIME_MODEXP |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
/* pstmnt uses data types subtly different from pstm: |
|
51
|
|
|
|
|
|
|
all limbs specified in the API are always multiples of uint32_t. */ |
|
52
|
|
|
|
|
|
|
# define pstmnt_bits uint32_t |
|
53
|
|
|
|
|
|
|
# define pstmnt_word uint32_t |
|
54
|
|
|
|
|
|
|
# define pstmnt_words uint32_t |
|
55
|
|
|
|
|
|
|
# define pstmnt_dword uint64_t |
|
56
|
|
|
|
|
|
|
# define PSTMNT_WORD_BITS 32 |
|
57
|
|
|
|
|
|
|
# define PSTMNT_WORD_BYTES 4 |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
/* Inline functions for conversion between types. */ |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
__inline static |
|
62
|
33888
|
|
|
|
|
|
const pstmnt_word *pstmnt_const_ptr(const pstm_int *A_const) |
|
63
|
|
|
|
|
|
|
{ |
|
64
|
33888
|
|
|
|
|
|
return (const pstmnt_word *) A_const->dp; |
|
65
|
|
|
|
|
|
|
} |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
__inline static |
|
68
|
16944
|
|
|
|
|
|
pstmnt_word *pstmnt_ptr(pstm_int *A) |
|
69
|
|
|
|
|
|
|
{ |
|
70
|
16944
|
|
|
|
|
|
return (pstmnt_word *) A->dp; |
|
71
|
|
|
|
|
|
|
} |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
__inline static |
|
74
|
16944
|
|
|
|
|
|
pstmnt_words pstmnt_size(const pstm_int *A) |
|
75
|
|
|
|
|
|
|
{ |
|
76
|
16944
|
|
|
|
|
|
return ((A->used) * DIGIT_BIT) / PSTMNT_WORD_BITS; |
|
77
|
|
|
|
|
|
|
} |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
__inline static |
|
80
|
11296
|
|
|
|
|
|
unsigned int pstmnt_size_bytes(const pstm_int *A) |
|
81
|
|
|
|
|
|
|
{ |
|
82
|
11296
|
|
|
|
|
|
return ((A->used) * DIGIT_BIT) / 8; |
|
83
|
|
|
|
|
|
|
} |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
/* Function attributes. */ |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# define PSTMNT_RESTORED /* Parameter is temporary changed during function call, |
|
88
|
|
|
|
|
|
|
but restored before the function returns. */ |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
/* Function API. */ |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
/* Compute small inverse constant for montgomery operations. */ |
|
93
|
|
|
|
|
|
|
pstmnt_word pstmnt_neg_small_inv(const pstmnt_word *a_p); |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
/* Convert values to montgomery format for processing modular exponentiation. */ |
|
96
|
|
|
|
|
|
|
void |
|
97
|
|
|
|
|
|
|
pstmnt_montgomery_input( |
|
98
|
|
|
|
|
|
|
const pstmnt_word Input[] /* NWords */, |
|
99
|
|
|
|
|
|
|
PSTMNT_RESTORED pstmnt_word Prime[] /* NWords */, |
|
100
|
|
|
|
|
|
|
pstmnt_word TempLarge[] /* NWords * 6 */, /* Note: differs from |
|
101
|
|
|
|
|
|
|
pstmnt_montgomery_input. */ |
|
102
|
|
|
|
|
|
|
pstmnt_word Target[] /* NWords */, |
|
103
|
|
|
|
|
|
|
pstmnt_words NWords, |
|
104
|
|
|
|
|
|
|
pstmnt_word PrimeSmallInv); |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
/* Convert values back from montgomery format. */ |
|
107
|
|
|
|
|
|
|
void |
|
108
|
|
|
|
|
|
|
pstmnt_montgomery_output( |
|
109
|
|
|
|
|
|
|
const pstmnt_word Input[] /* NWords */, |
|
110
|
|
|
|
|
|
|
pstmnt_word Output[] /* NWords */, |
|
111
|
|
|
|
|
|
|
const pstmnt_word Prime[] /* NWords */, |
|
112
|
|
|
|
|
|
|
pstmnt_word TempLarge[] /* NWords * 2 */, |
|
113
|
|
|
|
|
|
|
pstmnt_words NWords, |
|
114
|
|
|
|
|
|
|
pstmnt_word PrimeSmallInv); |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
/* Compute modular exponentiation with values in montgomery format. |
|
117
|
|
|
|
|
|
|
The modular exponentiation attempts to use constant time amthematical |
|
118
|
|
|
|
|
|
|
operations. */ |
|
119
|
|
|
|
|
|
|
void |
|
120
|
|
|
|
|
|
|
pstmnt_mod_exp_montgomery_skip( |
|
121
|
|
|
|
|
|
|
const pstmnt_word a[], |
|
122
|
|
|
|
|
|
|
const pstmnt_word x[], |
|
123
|
|
|
|
|
|
|
pstmnt_word r[], |
|
124
|
|
|
|
|
|
|
const pstmnt_word start_bit, |
|
125
|
|
|
|
|
|
|
const pstmnt_word bits, |
|
126
|
|
|
|
|
|
|
const pstmnt_word m[], |
|
127
|
|
|
|
|
|
|
pstmnt_word temp[] /* len * 4 */, |
|
128
|
|
|
|
|
|
|
pstmnt_word mp, |
|
129
|
|
|
|
|
|
|
pstmnt_words len); |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
# endif /* INCLUDE_GUARD_PSTMNT_H */ |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
#endif /* USE_CONSTANT_TIME_MODEXP */ |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
/* end of file pstmnt.h */ |