aboutsummaryrefslogtreecommitdiff
path: root/src/model/c/src/simple_tests.c
blob: 78d5b326a4cd98c64433780454c535ca31657bc3 (plain) (blame)
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
//======================================================================
//
// simple_tests.h
// --------------
// Header fil to export the simple tests of the modexp C model.
//
//
// Author: Joachim Strombergson
// Copyright (c) 2015, NORDUnet A/S
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
// - Redistributions of source code must retain the above copyright notice,
//   this list of conditions and the following disclaimer.
//
// - Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in the
//   documentation and/or other materials provided with the distribution.
//
// - Neither the name of the NORDUnet nor the names of its contributors may
//   be used to endorse or promote products derived from this software
//   without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//======================================================================

#include <stdio.h>
#include <stdlib.h>
#include "montgomery_array.h"
#include "bignum_uint32_t.h"

void simple_3_7_11(void) {
  printf("=== Simple test with X = 3, E = 7 and M = 11 ===\n");
  uint32_t X[] = { 0x3 };
  uint32_t E[] = { 0x7 };
  uint32_t M[] = { 0xb };
  uint32_t expected[] = { 0x9 };
  uint32_t Z[] = { 0x00000000 };
  mod_exp_array(1, X, E, M, Z);
  assertArrayEquals(1, expected, Z);
}

void simple_251_251_257(void) {
  printf("=== Simple test with X = 251, E = 251 and M = 257 ===\n");
  uint32_t X[] = { 0xfb };
  uint32_t E[] = { 0xfb };
  uint32_t M[] = { 0x101 };
  uint32_t expected[] = { 0xb7 };
  uint32_t Z[] = { 0x00000000 };
  mod_exp_array(1, X, E, M, Z);
  assertArrayEquals(1, expected, Z);
}


void bigger_test(void)
{
  printf("=== Bigger test with 128 bit operands.\n");
  uint32_t exponent[] = {0x3285c343, 0x2acbcb0f, 0x4d023228, 0x2ecc73db};
  uint32_t modulus[]  = {0x267d2f2e, 0x51c216a7, 0xda752ead, 0x48d22d89};
  uint32_t message[]  = {0x29462882, 0x12caa2d5, 0xb80e1c66, 0x1006807f};
  uint32_t expected[] = {0x0ddc404d, 0x91600596, 0x7425a8d8, 0xa066ca56};
  uint32_t result[]   = {0x00000000, 0x00000000, 0x00000000, 0x00000000};

  mod_exp_array(4, message, exponent, modulus, result);
  assertArrayEquals(4, expected, result);
}


void small_e_64_mod(void)
{
  printf("=== Simple test with small e and 64 bit modulus  ===\n");
  uint32_t X[] = { 0x00000000, 0xdb5a7e09, 0x86b98bfb };
  uint32_t E[] = { 0x00000000, 0x00000000, 0x00010001 };
  uint32_t M[] = { 0x00000000, 0xb3164743, 0xe1de267d };
  uint32_t expected[] = { 0x00000000, 0x9fc7f328, 0x3ba0ae18 };
  uint32_t Z[] = { 0x00000000, 0x00000000, 0x00000000 };
  mod_exp_array(3, X, E, M, Z);
  assertArrayEquals(3, expected, Z);
}

void small_e_256_mod(void) {
  printf("=== Simple test with small e and 256 bit modulus  ===\n");
  uint32_t X[] = {0x00000000, 0xbd589a51, 0x2ba97013,
                  0xc4736649, 0xe233fd5c, 0x39fcc5e5,
                  0x2d60b324, 0x1112f2d0, 0x1177c62b};
  uint32_t E[] = {0x00000000, 0x00000000, 0x00000000,
                  0x00000000, 0x00000000, 0x00000000,
                  0x00000000, 0x00000000, 0x00010001};

  uint32_t M[] = {0x00000000, 0xf169d36e, 0xbe2ce61d,
                   0xc2e87809, 0x4fed15c3, 0x7c70eac5,
                   0xa123e643, 0x299b36d2, 0x788e583b };

  uint32_t expected[] = {0x00000000, 0x7c5f0fee, 0x73028fc5,
                         0xc4fe57c4, 0x91a6f5be, 0x33a5c174,
                         0x2d2c2bcd, 0xda80e7d6, 0xfb4c889f};

  uint32_t Z[] = {0x00000000, 0x00000000, 0x00000000,
                  0x00000000, 0x00000000, 0x00000000,
                  0x00000000, 0x00000000, 0x00000000};

  mod_exp_array(9, X, E, M, Z);
  assertArrayEquals(9, expected, Z);
}

void rob_dec_1024(void)
{
  uint32_t exponent[] = {0x00000000, 0x3ff26c9e, 0x32685b93, 0x66570228, 0xf0603c4e,
                         0x04a717c1, 0x8038b116, 0xeb48325e, 0xcada992a,
                         0x920bb241, 0x5aee4afe, 0xe2a37e87, 0xb35b9519,
                         0xb335775d, 0x989553e9, 0x1326f46e, 0x2cdf6b7b,
                         0x84aabfa9, 0xef24c600, 0xb56872ad, 0x5edb9041,
                         0xe8ecd7f8, 0x535133fb, 0xdefc92c7, 0x42384226,
                         0x7d40e5f5, 0xc91bd745, 0x9578e460, 0xfc858374,
                         0x3172bed3, 0x73b6957c, 0xc0d6a68e, 0x33156a61};


  uint32_t modulus[] = {0x00000000, 0xd075ec0a, 0x95048ef8, 0xcaa69073, 0x8d9d58e9,
                        0x1764b437, 0x50b58cad, 0x8a6e3199, 0x135f80ee,
                        0x84eb2bde, 0x58d38ee3, 0x5825e91e, 0xafdeb1ba,
                        0xa15a160b, 0x0057c47c, 0xc7765e31, 0x868a3e15,
                        0x5ee57cef, 0xb008c4dd, 0x6a0a89ee, 0x98a4ee9c,
                        0x971a07de, 0x61e5b0d3, 0xcf70e1cd, 0xc6a0de5b,
                        0x451f2fb9, 0xdb995196, 0x9f2f884b, 0x4b09749a,
                        0xe6c4ddbe, 0x7ee61f79, 0x265c6adf, 0xb16b3015};


  uint32_t message[] = {0x00000000, 0x0001ffff, 0xffffffff, 0xffffffff, 0xffffffff,
                        0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
                        0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
                        0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
                        0xffffffff, 0xffffffff, 0xffffffff, 0x00303130,
                        0x0d060960, 0x86480165, 0x03040201, 0x05000420,
                        0x8e36fc9a, 0xa31724c3, 0x2416263c, 0x0366a175,
                        0xfabbb92b, 0x741ca649, 0x6107074d, 0x0343b597};


  uint32_t expected[] = {0x00000000, 0x06339a64, 0x367db02a, 0xf41158cc, 0x95e76049,
                         0x4519c165, 0x111184be, 0xe41d8ee2, 0x2ae5f5d1,
                         0x1da7f962, 0xac93ac88, 0x915eee13, 0xa3350c22,
                         0xf0dfa62e, 0xfdfc2b62, 0x29f26e27, 0xbebdc84e,
                         0x4746df79, 0x7b387ad2, 0x13423c9f, 0x98e8a146,
                         0xff486b6c, 0x1a85414e, 0x73117121, 0xb700e547,
                         0xab4e07b2, 0x21b988b8, 0x24dd77c2, 0x046b0a20,
                         0xcddb986a, 0xac75c2f2, 0xb044ed59, 0xea565879};


  uint32_t target[] = {0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
                       0x00000000, 0x00000000, 0x00000000, 0x00000000,
                       0x00000000, 0x00000000, 0x00000000, 0x00000000,
                       0x00000000, 0x00000000, 0x00000000, 0x00000000,
                       0x00000000, 0x00000000, 0x00000000, 0x00000000,
                       0x00000000, 0x00000000, 0x00000000, 0x00000000,
                       0x00000000, 0x00000000, 0x00000000, 0x00000000,
                       0x00000000, 0x00000000, 0x00000000, 0x00000000};

  printf("=== 1024 bit decipher/sign test from Robs RSA code. ===\n");

  mod_exp_array(33, message, exponent, modulus, target);
  assertArrayEquals(33, expected, target);
}


void rob_enc_1024(void)
{
  uint32_t exponent[] = {0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
                         0x00000000, 0x00000000, 0x00000000, 0x00000000,
                         0x00000000, 0x00000000, 0x00000000, 0x00000000,
                         0x00000000, 0x00000000, 0x00000000, 0x00000000,
                         0x00000000, 0x00000000, 0x00000000, 0x00000000,
                         0x00000000, 0x00000000, 0x00000000, 0x00000000,
                         0x00000000, 0x00000000, 0x00000000, 0x00000000,
                         0x00000000, 0x00000000, 0x00000000, 0x00010001};


  uint32_t modulus[] = {0x00000000, 0xd075ec0a, 0x95048ef8, 0xcaa69073, 0x8d9d58e9,
                        0x1764b437, 0x50b58cad, 0x8a6e3199, 0x135f80ee,
                        0x84eb2bde, 0x58d38ee3, 0x5825e91e, 0xafdeb1ba,
                        0xa15a160b, 0x0057c47c, 0xc7765e31, 0x868a3e15,
                        0x5ee57cef, 0xb008c4dd, 0x6a0a89ee, 0x98a4ee9c,
                        0x971a07de, 0x61e5b0d3, 0xcf70e1cd, 0xc6a0de5b,
                        0x451f2fb9, 0xdb995196, 0x9f2f884b, 0x4b09749a,
                        0xe6c4ddbe, 0x7ee61f79, 0x265c6adf, 0xb16b3015};


  uint32_t message[] = {0x00000000, 0x0001ffff, 0xffffffff, 0xffffffff, 0xffffffff,
                        0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
                        0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
                        0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
                        0xffffffff, 0xffffffff, 0xffffffff, 0x00303130,
                        0x0d060960, 0x86480165, 0x03040201, 0x05000420,
                        0x8e36fc9a, 0xa31724c3, 0x2416263c, 0x0366a175,
                        0xfabbb92b, 0x741ca649, 0x6107074d, 0x0343b597};


  uint32_t expected[] = {0x00000000, 0x06339a64, 0x367db02a, 0xf41158cc, 0x95e76049,
                         0x4519c165, 0x111184be, 0xe41d8ee2, 0x2ae5f5d1,
                         0x1da7f962, 0xac93ac88, 0x915eee13, 0xa3350c22,
                         0xf0dfa62e, 0xfdfc2b62, 0x29f26e27, 0xbebdc84e,
                         0x4746df79, 0x7b387ad2, 0x13423c9f, 0x98e8a146,
                         0xff486b6c, 0x1a85414e, 0x73117121, 0xb700e547,
                         0xab4e07b2, 0x21b988b8, 0x24dd77c2, 0x046b0a20,
                         0xcddb986a, 0xac75c2f2, 0xb044ed59, 0xea565879};


  uint32_t target[] = {0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
                       0x00000000, 0x00000000, 0x00000000, 0x00000000,
                       0x00000000, 0x00000000, 0x00000000, 0x00000000,
                       0x00000000, 0x00000000, 0x00000000, 0x00000000,
                       0x00000000, 0x00000000, 0x00000000, 0x00000000,
                       0x00000000, 0x00000000, 0x00000000, 0x00000000,
                       0x00000000, 0x00000000, 0x00000000, 0x00000000,
                       0x00000000, 0x00000000, 0x00000000, 0x00000000};

  printf("=== 1024 bit encipher/verify test from Robs RSA code. ===\n");

  mod_exp_array(33, expected, exponent, modulus, target);
  assertArrayEquals(33, message, target);
}


void simple_tests(void) {
//  simple_3_7_11();
//  simple_251_251_257();
//  bigger_test();
//  small_e_256_mod();
//  small_e_64_mod();
  rob_enc_1024();
  rob_dec_1024();
  //  small_e_256_mod2();
}

//======================================================================
// EOF simple_tests.c
//======================================================================