Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#define NX_CRYPTO_SOURCE_CODE
#include "stdio.h"
#include "nx_crypto_huge_number.h"
...
...
_nx_crypto_huge_number_add_digit(NX_CRYPTO_HUGE_NUMBER *, ULONG)
...
...
_nx_crypto_huge_number_subtract_digit(NX_CRYPTO_HUGE_NUMBER *, ULONG)
...
...
_nx_crypto_huge_number_power_modulus(NX_CRYPTO_HUGE_NUMBER *, NX_CRYPTO_HUGE_NUMBER *, NX_CRYPTO_HUGE_NUMBER *, NX_CRYPTO_HUGE_NUMBER *, ULONG *)
Files
loading...
CodeScopeSTM32 Libraries and Samplesnetxduocrypto_libraries/src/nx_crypto_huge_number_extended.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* Copyright (c) Microsoft Corporation. All rights reserved. */ /* */ /* This software is licensed under the Microsoft Software License */ /* Terms for Microsoft Azure RTOS. Full text of the license can be */ /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ /* and in the root directory of this software. */ /* */... /**************************************************************************/ ... /**************************************************************************/ /**************************************************************************/ /** */ /** NetX Crypto Component */ /** */ /** Huge Number */ /** */... /**************************************************************************/ /**************************************************************************/ #define NX_CRYPTO_SOURCE_CODE /* Include necessary system files. */ #include "stdio.h" #include "nx_crypto_huge_number.h" #ifndef NX_CRYPTO_SELF_TEST... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_crypto_huge_number_add_digit PORTABLE C */ /* 6.1.7 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function calculates addition for huge number and digit. */ /* */ /* Note: Result is stored in value. */ /* */ /* INPUT */ /* */ /* value Huge number number */ /* digit Digit value */ /* */ /* OUTPUT */ /* */ /* None */ /* */ /* CALLS */ /* */ /* _nx_crypto_huge_number_add_digit_unsigned */ /* Calculate addition for */ /* unsigned huge numbers */ /* _nx_crypto_huge_number_subtract_digit_unsigned */ /* Calculate subtraction for */ /* unsigned huge numbers */ /* */ /* CALLED BY */ /* */ /* Application Code */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* resulting in version 6.1 */ /* 06-02-2021 Bhupendra Naphade Modified comment(s), */ /* renamed FIPS symbol to */ /* self-test, */ /* resulting in version 6.1.7 */ /* */... /**************************************************************************/ NX_CRYPTO_KEEP VOID _nx_crypto_huge_number_add_digit(NX_CRYPTO_HUGE_NUMBER *value, HN_UBASE digit) { if (value -> nx_crypto_huge_number_is_negative) { if ((value -> nx_crypto_huge_number_size > 1) || (value -> nx_crypto_huge_number_data[0] >= digit)) { /* |value| >= digit */ /* value = -(|value| - digit) */ _nx_crypto_huge_number_subtract_digit_unsigned(value, digit); }if ((value -> nx_crypto_huge_number_size > 1) || (value -> nx_crypto_huge_number_data[0] >= digit)) { ... } else { /* value < digit */ /* value = digit - |value| */ value -> nx_crypto_huge_number_data[0] = digit - value -> nx_crypto_huge_number_data[0]; value -> nx_crypto_huge_number_is_negative = NX_CRYPTO_FALSE; }else { ... } }if (value -> nx_crypto_huge_number_is_negative) { ... } else { /* value = value + digit */ _nx_crypto_huge_number_add_digit_unsigned(value, digit); }else { ... } }{ ... } .../**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_crypto_huge_number_subtract_digit PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function calculates subtraction for huge number and digit. */ /* */ /* Note: Result is stored in value. */ /* */ /* INPUT */ /* */ /* value Huge number number */ /* digit Digit value */ /* */ /* OUTPUT */ /* */ /* None */ /* */ /* CALLS */ /* */ /* _nx_crypto_huge_number_add_digit_unsigned */ /* Calculate addition for */ /* unsigned huge numbers */ /* _nx_crypto_huge_number_subtract_digit_unsigned */ /* Calculate subtraction for */ /* unsigned huge numbers */ /* */ /* CALLED BY */ /* */ /* Application Code */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ NX_CRYPTO_KEEP VOID _nx_crypto_huge_number_subtract_digit(NX_CRYPTO_HUGE_NUMBER *value, HN_UBASE digit) { if (value -> nx_crypto_huge_number_is_negative) { /* value = -(|value| + digit) */ _nx_crypto_huge_number_add_digit_unsigned(value, digit); }if (value -> nx_crypto_huge_number_is_negative) { ... } else { if ((value -> nx_crypto_huge_number_size > 1) || (value -> nx_crypto_huge_number_data[0] >= digit)) { /* value >= digit */ /* value = value - digit */ _nx_crypto_huge_number_subtract_digit_unsigned(value, digit); }if ((value -> nx_crypto_huge_number_size > 1) || (value -> nx_crypto_huge_number_data[0] >= digit)) { ... } else { /* value < digit */ /* value = -(digit - value) */ value -> nx_crypto_huge_number_data[0] = digit - value -> nx_crypto_huge_number_data[0]; value -> nx_crypto_huge_number_is_negative = NX_CRYPTO_TRUE; }else { ... } }else { ... } }{ ... } .../**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_crypto_huge_number_power_modulus PORTABLE C */ /* 6.1.9 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function raises a huge number to the power of a second huge */ /* number using a third huge number as a modulus. The result is placed */ /* in a fourth huge number. */ /* */ /* NOTE: This function makes use of the Huge Number scratch buffers. */ /* Each operand and the result must have the */ /* nx_crypto_huge_number_scratch member set to point to a buffer */ /* of size equal to the number data buffer and that size stored */ /* in the nx_crypto_huge_buffer_size member. */ /* */ /* INPUT */ /* */ /* number Number being exponentiated */ /* exponent Exponent number */ /* modulus Modulus number */ /* result Result buffer */ /* */ /* OUTPUT */ /* */ /* None */ /* */ /* CALLS */ /* */ /* NX_CRYPTO_HUGE_NUMBER_COPY Copy huge number */ /* NX_CRYPTO_HUGE_NUMBER_INITIALIZE Initialize the buffer of */ /* huge number */ /* NX_CRYPTO_HUGE_NUMBER_SET_DIGIT Set value of huge number */ /* between 0 and (HN_RADIX - 1)*/ /* _nx_crypto_huge_number_multiply Multiply a huge number by */ /* a second huge number */ /* _nx_crypto_huge_number_modulus Calculate the remainder after */ /* dividing a huge number by */ /* another huge number */ /* _nx_crypto_huge_number_square Compute the square of a value */ /* */ /* CALLED BY */ /* */ /* Application Code */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* resulting in version 6.1 */ /* 10-15-2021 Bhupendra Naphade Modified comment(s), */ /* resulting in version 6.1.9 */ /* */... /**************************************************************************/ NX_CRYPTO_KEEP VOID _nx_crypto_huge_number_power_modulus(NX_CRYPTO_HUGE_NUMBER *number, NX_CRYPTO_HUGE_NUMBER *exponent, NX_CRYPTO_HUGE_NUMBER *modulus, NX_CRYPTO_HUGE_NUMBER *result, HN_UBASE *scratch) { UINT index, bit; HN_UBASE cur_block; HN_UBASE *val; UINT exp_size; NX_CRYPTO_HUGE_NUMBER temp; NX_CRYPTO_HUGE_NUMBER_INITIALIZE(&temp, scratch, number -> nx_crypto_huge_buffer_size); /* Clear out the result since we need it to start with a simple value. */ NX_CRYPTO_HUGE_NUMBER_SET_DIGIT(result, 1); exp_size = exponent -> nx_crypto_huge_number_size; val = exponent -> nx_crypto_huge_number_data; /* Loop through the bits of the exponent. For each bit set, multiply the result by the running square. */ for (index = 0; index < exp_size; index++) { /* Current byte in the exponent determines whether we multiply or not. */ cur_block = val[index]; /* Loop over the bits in the current byte to see whether we add or not. */ for (bit = 0; bit < HN_SHIFT; ++bit) { /* A non-zero bit means we need to multiply. */ if (cur_block & 1) { /* Multiply the result by the running square (number) and put the result in a scratch buffer. */ _nx_crypto_huge_number_multiply(result, number, &temp); NX_CRYPTO_HUGE_NUMBER_COPY(result, &temp); /* Take the modulus of the product we just calculated. If we didn't do this here the result would quickly grow larger than our buffers (or the universe) could store. Put the result in our result buffer. *//* ... */ _nx_crypto_huge_number_modulus(result, modulus); }if (cur_block & 1) { ... } /* Shift the data to go to the next bit. */ cur_block = cur_block >> 1; _nx_crypto_huge_number_square(number, &temp); _nx_crypto_huge_number_modulus(&temp, modulus); NX_CRYPTO_HUGE_NUMBER_COPY(number, &temp); /* Take the modulus of our squared product and put the result into our number buffer. */ }for (bit = 0; bit < HN_SHIFT; ++bit) { ... } }for (index = 0; index < exp_size; index++) { ... } }{ ... } ...#endif/* ... */