rsa-test.c 6.89 KB
Newer Older
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
#include "testutils.h"

#if HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdio.h>

#if HAVE_LIBGMP
/* # include <gmp.h> */
# include "rsa.h"
#endif

/* Uses this key, generated by
 *
 *   lsh-keygen -a rsa -l 1000 -f advanced-hex
 *
 * (private-key (rsa-pkcs1 
 *        (n #69abd505285af665 36ddc7c8f027e6f0 ed435d6748b16088
 *            4fd60842b3a8d7fb bd8a3c98f0cc50ae 4f6a9f7dd73122cc
 *            ec8afa3f77134406 f53721973115fc2d 8cfbba23b145f28d
 *            84f81d3b6ae8ce1e 2850580c026e809b cfbb52566ea3a3b3
 *            df7edf52971872a7 e35c1451b8636d22 279a8fb299368238
 *            e545fbb4cf#)
 *        (e #0db2ad57#)
 *        (d #3240a56f4cd0dcc2 4a413eb4ea545259 5c83d771a1c2ba7b
 *            ec47c5b43eb4b374 09bd2aa1e236dd86 481eb1768811412f
 *            f8d91be3545912af b55c014cb55ceac6 54216af3b85d5c4f
 *            4a32894e3b5dfcde 5b2875aa4dc8d9a8 6afd0ca92ef50d35
 *            bd09f1c47efb4c8d c631e07698d362aa 4a83fd304e66d6c5
 *            468863c307#)
 *        (p #0a66399919be4b4d e5a78c5ea5c85bf9 aba8c013cb4a8732
 *            14557a12bd67711e bb4073fd39ad9a86 f4e80253ad809e5b
 *            f2fad3bc37f6f013 273c9552c9f489#)
 *        (q #0a294f069f118625 f5eae2538db9338c 776a298eae953329
 *            9fd1eed4eba04e82 b2593bc98ba8db27 de034da7daaea795
 *            2d55b07b5f9a5875 d1ca5f6dcab897#)
 *        (a #011b6c48eb592eee e85d1bb35cfb6e07 344ea0b5e5f03a28
 *            5b405396cbc78c5c 868e961db160ba8d 4b984250930cf79a
 *            1bf8a9f28963de53 128aa7d690eb87#)
 *        (b #0409ecf3d2557c88 214f1af5e1f17853 d8b2d63782fa5628
 *            60cf579b0833b7ff 5c0529f2a97c6452 2fa1a8878a9635ab
 *            ce56debf431bdec2 70b308fa5bf387#)
 *        (c #04e103ee925cb5e6 6653949fa5e1a462 c9e65e1adcd60058
 *            e2df9607cee95fa8 daec7a389a7d9afc 8dd21fef9d83805a
 *            40d46f49676a2f6b 2926f70c572c00#)))
 */

#define LDATA(s) strlen(s), s

#define SIGN(key, hash, msg, signature) do {	\
  hash##_update(&hash, LDATA(msg));		\
  rsa_##hash##_sign(key, &hash, signature);	\
} while(0)

#define VERIFY(key, hash, msg, signature) (	\
  hash##_update(&hash, LDATA(msg)),		\
  rsa_##hash##_verify(key, &hash, signature)	\
)

#if HAVE_LIBGMP

/* Missing in current gmp */
static void
mpz_togglebit (mpz_t x, unsigned long int bit)
{
  if (mpz_tstbit(x, bit))
    mpz_clrbit(x, bit);
  else
    mpz_setbit(x, bit);
}

#endif /* HAVE_LIBGMP */

int
test_main(void)
{
#if HAVE_LIBGMP

  struct rsa_public_key pub;
  struct rsa_private_key key;

  struct md5_ctx md5;
  struct sha1_ctx sha1;

  mpz_t signature;
  mpz_t expected;
  
  mpz_init(signature);
  mpz_init(expected);
  
  rsa_init_private_key(&key);
  rsa_init_public_key(&pub);

  md5_init(&md5);
  sha1_init(&sha1);
  
  mpz_set_str(pub.n,
	      "69abd505285af665" "36ddc7c8f027e6f0" "ed435d6748b16088"
	      "4fd60842b3a8d7fb" "bd8a3c98f0cc50ae" "4f6a9f7dd73122cc"
	      "ec8afa3f77134406" "f53721973115fc2d" "8cfbba23b145f28d"
	      "84f81d3b6ae8ce1e" "2850580c026e809b" "cfbb52566ea3a3b3"
	      "df7edf52971872a7" "e35c1451b8636d22" "279a8fb299368238"
	      "e545fbb4cf", 16);
  mpz_set_str(pub.e, "0db2ad57", 16);

  if (!rsa_prepare_public_key(&pub))
    FAIL();
  
  /* d is not used */
#if 0  
  mpz_set_str(key.d,
	      "3240a56f4cd0dcc2" "4a413eb4ea545259" "5c83d771a1c2ba7b"
	      "ec47c5b43eb4b374" "09bd2aa1e236dd86" "481eb1768811412f"
	      "f8d91be3545912af" "b55c014cb55ceac6" "54216af3b85d5c4f"
	      "4a32894e3b5dfcde" "5b2875aa4dc8d9a8" "6afd0ca92ef50d35"
	      "bd09f1c47efb4c8d" "c631e07698d362aa" "4a83fd304e66d6c5"
	      "468863c307", 16);
#endif
  
  mpz_set_str(key.p,
	      "0a66399919be4b4d" "e5a78c5ea5c85bf9" "aba8c013cb4a8732"
	      "14557a12bd67711e" "bb4073fd39ad9a86" "f4e80253ad809e5b"
	      "f2fad3bc37f6f013" "273c9552c9f489", 16);

  mpz_set_str(key.q,
	      "0a294f069f118625" "f5eae2538db9338c" "776a298eae953329"
	      "9fd1eed4eba04e82" "b2593bc98ba8db27" "de034da7daaea795"
	      "2d55b07b5f9a5875" "d1ca5f6dcab897", 16);
  
  mpz_set_str(key.a,
	      "011b6c48eb592eee" "e85d1bb35cfb6e07" "344ea0b5e5f03a28"
	      "5b405396cbc78c5c" "868e961db160ba8d" "4b984250930cf79a"
	      "1bf8a9f28963de53" "128aa7d690eb87", 16);
  
  mpz_set_str(key.b,
	      "0409ecf3d2557c88" "214f1af5e1f17853" "d8b2d63782fa5628"
	      "60cf579b0833b7ff" "5c0529f2a97c6452" "2fa1a8878a9635ab"
	      "ce56debf431bdec2" "70b308fa5bf387", 16);
  
  mpz_set_str(key.c,
	      "04e103ee925cb5e6" "6653949fa5e1a462" "c9e65e1adcd60058"
	      "e2df9607cee95fa8" "daec7a389a7d9afc" "8dd21fef9d83805a"
	      "40d46f49676a2f6b" "2926f70c572c00", 16);

  if (!rsa_prepare_private_key(&key))
    FAIL();

  if (pub.size != key.size)
    FAIL();
  
  /* Test md5 signatures */

  mpz_set_str(expected,
	      "53bf517009fa956e" "3daa6adc95e8663d" "3759002f488bbbad"
	      "e49f62792d85dbcc" "293f68e2b68ef89a" "c5bd42d98f845325"
	      "3e6c1b76fc337db5" "e0053f255c55faf3" "eb6cc568ad7f5013"
	      "5b269a64acb9eaa7" "b7f09d9bd90310e6" "4c58f6dbe673ada2"
	      "67c97a9d99e19f9d" "87960d9ce3f0d5ce" "84f401fe7e10fa24"
	      "28b9bffcf9", 16);
  
  SIGN(&key, md5, "The magic words are squeamish ossifrage", signature);

  if (verbose)
    {
      fprintf(stderr, "rsa-md5 signature: ");
      mpz_out_str(stderr, 16, signature);
      fprintf(stderr, "\n");
    }

  if (mpz_cmp(signature, expected))
    FAIL();
  
  /* Try bad data */
  if (VERIFY(&pub, md5,
	     "The magick words are squeamish ossifrage", signature))
    FAIL();

  /* Try correct data */
  if (!VERIFY(&pub, md5,
	      "The magic words are squeamish ossifrage", signature))
    FAIL();

  /* Try bad signature */
  mpz_togglebit(signature, 17);

  if (VERIFY(&pub, md5,
	     "The magic words are squeamish ossifrage", signature))
    FAIL();

  /* Test sha1 signature */
  mpz_set_str(expected,
	      "129b405ed85db88c" "55d35344c4b52854" "496516b4d63d8211"
	      "80a0c24d6ced9047" "33065a564bbd33d0" "a5cdfd204b9c6d15"
	      "78337207c2f1662d" "c73906c7a0f2bf5c" "af92cef9121957b1"
	      "dcb111ff47b92389" "888e384d0cfd1b1e" "e5d7003a8feff3fd"
	      "dd6a71d242a79272" "25234d67ba369441" "c12ae555c697754e"
	      "a17f93fa92", 16);
  
  SIGN(&key, sha1, "The magic words are squeamish ossifrage", signature);

  if (verbose)
    {
      fprintf(stderr, "rsa-sha1 signature: ");
      mpz_out_str(stderr, 16, signature);
      fprintf(stderr, "\n");
    }

  if (mpz_cmp(signature, expected))
    FAIL();
  
  /* Try bad data */
  if (VERIFY(&pub, sha1,
	     "The magick words are squeamish ossifrage", signature))
    FAIL();

  /* Try correct data */
  if (!VERIFY(&pub, sha1,
	      "The magic words are squeamish ossifrage", signature))
    FAIL();

  /* Try bad signature */
  mpz_togglebit(signature, 17);

  if (VERIFY(&pub, sha1,
	     "The magic words are squeamish ossifrage", signature))
    FAIL();

  rsa_clear_private_key(&key);
  rsa_clear_public_key(&pub);
  mpz_clear(signature);
  mpz_clear(expected);

  SUCCESS();
  
#else /* !HAVE_LIBGMP */
  SKIP();
#endif /* !HAVE_LIBGMP */
}