sl@0
|
1 |
/*
|
sl@0
|
2 |
* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
* This component and the accompanying materials are made available
|
sl@0
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
sl@0
|
6 |
* which accompanies this distribution, and is available
|
sl@0
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* Initial Contributors:
|
sl@0
|
10 |
* Nokia Corporation - initial contribution.
|
sl@0
|
11 |
*
|
sl@0
|
12 |
* Contributors:
|
sl@0
|
13 |
*
|
sl@0
|
14 |
* Description:
|
sl@0
|
15 |
* Generates RSA test vectors.
|
sl@0
|
16 |
*
|
sl@0
|
17 |
*/
|
sl@0
|
18 |
|
sl@0
|
19 |
|
sl@0
|
20 |
|
sl@0
|
21 |
|
sl@0
|
22 |
/**
|
sl@0
|
23 |
@file
|
sl@0
|
24 |
*/
|
sl@0
|
25 |
|
sl@0
|
26 |
#include <stdio.h>
|
sl@0
|
27 |
#include <string.h>
|
sl@0
|
28 |
|
sl@0
|
29 |
#include "openssl/e_os.h"
|
sl@0
|
30 |
|
sl@0
|
31 |
#include <openssl/crypto.h>
|
sl@0
|
32 |
#include <openssl/rsa.h>
|
sl@0
|
33 |
|
sl@0
|
34 |
#include "utils.h"
|
sl@0
|
35 |
#include "keys.h"
|
sl@0
|
36 |
|
sl@0
|
37 |
void printPublicKey(RSA* key)
|
sl@0
|
38 |
{
|
sl@0
|
39 |
printf("\t\t<modulus>");
|
sl@0
|
40 |
printBN(key->n);
|
sl@0
|
41 |
printf("</modulus>\n");
|
sl@0
|
42 |
printf("\t\t<publicExponent>");
|
sl@0
|
43 |
printBN(key->e);
|
sl@0
|
44 |
printf("</publicExponent>\n");
|
sl@0
|
45 |
}
|
sl@0
|
46 |
|
sl@0
|
47 |
void printPrivateKey(RSA* key)
|
sl@0
|
48 |
{
|
sl@0
|
49 |
printf("\t\t<modulus>");
|
sl@0
|
50 |
printBN(key->n);
|
sl@0
|
51 |
printf("</modulus>\n");
|
sl@0
|
52 |
printf("\t\t<privateExponent>");
|
sl@0
|
53 |
printBN(key->d);
|
sl@0
|
54 |
printf("</privateExponent>\n");
|
sl@0
|
55 |
}
|
sl@0
|
56 |
|
sl@0
|
57 |
/**
|
sl@0
|
58 |
* Generate encrypt and decrypt vectors for a plaintext.
|
sl@0
|
59 |
*/
|
sl@0
|
60 |
|
sl@0
|
61 |
static void generateEncryptionVector(RSA* key, unsigned char* ptext_ex, int plen, BOOL passes)
|
sl@0
|
62 |
{
|
sl@0
|
63 |
unsigned char ctext[RSA_size(key)];
|
sl@0
|
64 |
int num;
|
sl@0
|
65 |
|
sl@0
|
66 |
setOurRandom();
|
sl@0
|
67 |
num = RSA_public_encrypt(plen, ptext_ex, ctext, key, RSA_PKCS1_PADDING);
|
sl@0
|
68 |
if (num == -1)
|
sl@0
|
69 |
processError();
|
sl@0
|
70 |
|
sl@0
|
71 |
if (!passes)
|
sl@0
|
72 |
scramble(ctext, num);
|
sl@0
|
73 |
|
sl@0
|
74 |
printActionHeader("RSA test vector", "RSAEncryptVector");
|
sl@0
|
75 |
printPublicKey(key);
|
sl@0
|
76 |
printHexElement("plaintext", ptext_ex, plen);
|
sl@0
|
77 |
printHexElement("ciphertext", ctext, num);
|
sl@0
|
78 |
printActionFooter(passes);
|
sl@0
|
79 |
|
sl@0
|
80 |
printActionHeader("RSA test vector", "RSADecryptVector");
|
sl@0
|
81 |
printPrivateKey(key);
|
sl@0
|
82 |
printHexElement("ciphertext", ctext, num);
|
sl@0
|
83 |
printHexElement("plaintext", ptext_ex, plen);
|
sl@0
|
84 |
printActionFooter(passes);
|
sl@0
|
85 |
}
|
sl@0
|
86 |
|
sl@0
|
87 |
/**
|
sl@0
|
88 |
* Sign a digest - the digest is unformatted, ie we're not dealing with
|
sl@0
|
89 |
* algotrithm identifiers here.
|
sl@0
|
90 |
*/
|
sl@0
|
91 |
|
sl@0
|
92 |
static void generateSignatureVector(RSA* key, unsigned char* ptext_ex, int plen, BOOL passes)
|
sl@0
|
93 |
{
|
sl@0
|
94 |
unsigned char ctext[RSA_size(key)];
|
sl@0
|
95 |
int num;
|
sl@0
|
96 |
|
sl@0
|
97 |
num = RSA_private_encrypt(plen, ptext_ex, ctext, key, RSA_PKCS1_PADDING);
|
sl@0
|
98 |
if (num == -1)
|
sl@0
|
99 |
processError();
|
sl@0
|
100 |
|
sl@0
|
101 |
if (!passes)
|
sl@0
|
102 |
scramble(ctext, num);
|
sl@0
|
103 |
|
sl@0
|
104 |
printActionHeader("RSA test vector", "RSASignVector");
|
sl@0
|
105 |
printPrivateKey(key);
|
sl@0
|
106 |
printHexElement("digestInfo", ptext_ex, plen);
|
sl@0
|
107 |
printHexElement("signature", ctext, num);
|
sl@0
|
108 |
printActionFooter(passes);
|
sl@0
|
109 |
|
sl@0
|
110 |
printActionHeader("RSA test vector", "RSAVerifyVector");
|
sl@0
|
111 |
printPublicKey(key);
|
sl@0
|
112 |
printHexElement("digestInfo", ptext_ex, plen);
|
sl@0
|
113 |
printHexElement("signature", ctext, num);
|
sl@0
|
114 |
printActionFooter(passes);
|
sl@0
|
115 |
}
|
sl@0
|
116 |
|
sl@0
|
117 |
/* Plaintext from openssl test code. */
|
sl@0
|
118 |
static unsigned char ptext1[] = "\x54\x85\x9b\x34\x2c\x49\xea\x2a";
|
sl@0
|
119 |
static int plen1 = sizeof(ptext1) - 1;
|
sl@0
|
120 |
|
sl@0
|
121 |
/* 16 byte random plaintext. */
|
sl@0
|
122 |
static unsigned char ptext2[] =
|
sl@0
|
123 |
"\x47\xab\x92\x76\x09\xfd\x75\xa7\xe2\x08\x85\xeb\x7e\x4c\xff\x0a";
|
sl@0
|
124 |
static int plen2 = sizeof(ptext2) - 1;
|
sl@0
|
125 |
|
sl@0
|
126 |
/* 32 byte random plaintext. */
|
sl@0
|
127 |
static unsigned char ptext3[] =
|
sl@0
|
128 |
"\x0b\x0a\x7c\xeb\x6c\x17\x45\x53\x1d\xa7\x24\xad\x43\x8b\xf7\x46"
|
sl@0
|
129 |
"\x89\xc3\x9f\x09\x5e\x88\x3e\xd8\x8e\x04\x36\x38\x49\xc0\x0f\x41";
|
sl@0
|
130 |
static int plen3 = sizeof(ptext3) - 1;
|
sl@0
|
131 |
|
sl@0
|
132 |
/* One byte plaintext. */
|
sl@0
|
133 |
static unsigned char short_ptext[] = "\x23";
|
sl@0
|
134 |
static int short_plen = sizeof(short_ptext) - 1;
|
sl@0
|
135 |
|
sl@0
|
136 |
/* Longest possible plaintexts, one for each key. */
|
sl@0
|
137 |
static unsigned char long_ptext1[] =
|
sl@0
|
138 |
"\x66\x79\xf3\x84\x82\x06\x99\x06\xcd\xf1\xdf\x3f\xdd\xb5\x37\x74"
|
sl@0
|
139 |
"\x46\x76\xba\x0d\xb8\xd6\x82\xb6\x82\x6f\x31\xb1\xd8\x23\x0c\xca"
|
sl@0
|
140 |
"\x4e\x39\x28\x77\x05\x3f\xac\x5a\x13\xff\x3a\x39\x35\x2e\xaf\xb1"
|
sl@0
|
141 |
"\x85\xe4\xd0\x60\xf4";
|
sl@0
|
142 |
|
sl@0
|
143 |
static int long_plen1 = sizeof(long_ptext1) - 1;
|
sl@0
|
144 |
|
sl@0
|
145 |
static unsigned char long_ptext2[] =
|
sl@0
|
146 |
"\xcd\xa2\x2c\x4b\x6a\x20\x00\x0e\xad\xad\x74\xbd\xb3\x04\xbd\xc5"
|
sl@0
|
147 |
"\x72\x73\x02\x11\x9d\x6d\x37\x75\x66\x5a\xf2\xe6\x47\x65\x79\x80"
|
sl@0
|
148 |
"\x7c\x92\xec\x09\xf5\x33\xea";
|
sl@0
|
149 |
|
sl@0
|
150 |
static int long_plen2 = sizeof(long_ptext2) - 1;
|
sl@0
|
151 |
|
sl@0
|
152 |
static unsigned char long_ptext3[] =
|
sl@0
|
153 |
"\x0e\x25\x61\xaf\x55\xeb\x9c\x10\x90\x4f\xd4\x27\xfd\x0d\x1d\xf4"
|
sl@0
|
154 |
"\x38\xbd\x9e\xd0\xc7\x1c\x48\x0b\x50\xa1\xd3\xf1\xb4\xdb\xba\x2d"
|
sl@0
|
155 |
"\x00\x81\x59\x6e\x61\x43\x35\x50\xf9\x5f\x70\x20\xb2\x47\x48\x7f"
|
sl@0
|
156 |
"\x32\xf7\xe8\x2e\x50\xc1\x80\x45\x4b\x5c\xf8\x45\x6a\xa0\x0f\x33"
|
sl@0
|
157 |
"\xf1\xec\x9a\xb1\x79\xf5\xcc\x92\x1c\x30\x12\xb0\x55\x7b\x49\x06"
|
sl@0
|
158 |
"\x93\xa8\x30\x5a\x68\x79\x8a\x21\x9a\xd7\x68\x70\xf8\xa1\xf1\x0a"
|
sl@0
|
159 |
"\x52\x85\x75\xf9\x2d\x26\xd3\x1b\x37\xdc\xdc\x60\x87\x77\xcb\x97"
|
sl@0
|
160 |
"\x57\x00\x4f\xf1\x81";
|
sl@0
|
161 |
|
sl@0
|
162 |
static int long_plen3 = sizeof(long_ptext3) - 1;
|
sl@0
|
163 |
|
sl@0
|
164 |
int main(int argc, char *argv[])
|
sl@0
|
165 |
{
|
sl@0
|
166 |
initKeys();
|
sl@0
|
167 |
|
sl@0
|
168 |
setOurRandom();
|
sl@0
|
169 |
testOurRandom();
|
sl@0
|
170 |
|
sl@0
|
171 |
/** Public encryption: */
|
sl@0
|
172 |
|
sl@0
|
173 |
/** Encrypt openssl test plaintext with each key. */
|
sl@0
|
174 |
generateEncryptionVector(key1, ptext1, plen1, TRUE);
|
sl@0
|
175 |
generateEncryptionVector(key2, ptext1, plen1, TRUE);
|
sl@0
|
176 |
generateEncryptionVector(key3, ptext1, plen1, TRUE);
|
sl@0
|
177 |
|
sl@0
|
178 |
/** Encrypt 16 byte test plaintext with each key. */
|
sl@0
|
179 |
generateEncryptionVector(key1, ptext2, plen2, TRUE);
|
sl@0
|
180 |
generateEncryptionVector(key2, ptext2, plen2, TRUE);
|
sl@0
|
181 |
generateEncryptionVector(key3, ptext2, plen2, TRUE);
|
sl@0
|
182 |
|
sl@0
|
183 |
/** Encrypt 32 byte test plaintext with each key. */
|
sl@0
|
184 |
generateEncryptionVector(key1, ptext3, plen3, TRUE);
|
sl@0
|
185 |
generateEncryptionVector(key2, ptext3, plen3, TRUE);
|
sl@0
|
186 |
generateEncryptionVector(key3, ptext3, plen3, TRUE);
|
sl@0
|
187 |
|
sl@0
|
188 |
/** Encypt one byte plaintext with each key. */
|
sl@0
|
189 |
generateEncryptionVector(key1, short_ptext, short_plen, TRUE);
|
sl@0
|
190 |
generateEncryptionVector(key2, short_ptext, short_plen, TRUE);
|
sl@0
|
191 |
generateEncryptionVector(key3, short_ptext, short_plen, TRUE);
|
sl@0
|
192 |
|
sl@0
|
193 |
/** Encrypt longest possible plaintext for each key. */
|
sl@0
|
194 |
generateEncryptionVector(key1, long_ptext1, long_plen1, TRUE);
|
sl@0
|
195 |
generateEncryptionVector(key2, long_ptext2, long_plen2, TRUE);
|
sl@0
|
196 |
generateEncryptionVector(key3, long_ptext3, long_plen3, TRUE);
|
sl@0
|
197 |
|
sl@0
|
198 |
/** Negative encryption vectors. */
|
sl@0
|
199 |
generateEncryptionVector(key1, ptext1, plen1, FALSE);
|
sl@0
|
200 |
generateEncryptionVector(key2, ptext1, plen1, FALSE);
|
sl@0
|
201 |
generateEncryptionVector(key3, ptext1, plen1, FALSE);
|
sl@0
|
202 |
|
sl@0
|
203 |
/** Signing: */
|
sl@0
|
204 |
|
sl@0
|
205 |
/** Sign openssl test plaintext with each key. */
|
sl@0
|
206 |
generateSignatureVector(key1, ptext1, plen1, TRUE);
|
sl@0
|
207 |
generateSignatureVector(key2, ptext1, plen1, TRUE);
|
sl@0
|
208 |
generateSignatureVector(key3, ptext1, plen1, TRUE);
|
sl@0
|
209 |
|
sl@0
|
210 |
/** Sign 16 byte digest with each key. */
|
sl@0
|
211 |
generateSignatureVector(key1, ptext2, plen2, TRUE);
|
sl@0
|
212 |
generateSignatureVector(key2, ptext2, plen2, TRUE);
|
sl@0
|
213 |
generateSignatureVector(key3, ptext2, plen2, TRUE);
|
sl@0
|
214 |
|
sl@0
|
215 |
/** Sign 32 byte digest with each key. */
|
sl@0
|
216 |
generateSignatureVector(key1, ptext3, plen3, TRUE);
|
sl@0
|
217 |
generateSignatureVector(key2, ptext3, plen3, TRUE);
|
sl@0
|
218 |
generateSignatureVector(key3, ptext3, plen3, TRUE);
|
sl@0
|
219 |
|
sl@0
|
220 |
/** Sign one byte digest with each key. */
|
sl@0
|
221 |
generateSignatureVector(key1, short_ptext, short_plen, TRUE);
|
sl@0
|
222 |
generateSignatureVector(key2, short_ptext, short_plen, TRUE);
|
sl@0
|
223 |
generateSignatureVector(key3, short_ptext, short_plen, TRUE);
|
sl@0
|
224 |
|
sl@0
|
225 |
/** Sign longest possible digests for each key. */
|
sl@0
|
226 |
generateSignatureVector(key1, long_ptext1, long_plen1, TRUE);
|
sl@0
|
227 |
generateSignatureVector(key2, long_ptext2, long_plen2, TRUE);
|
sl@0
|
228 |
generateSignatureVector(key3, long_ptext3, long_plen3, TRUE);
|
sl@0
|
229 |
|
sl@0
|
230 |
/** Negative signature vectors. */
|
sl@0
|
231 |
generateSignatureVector(key1, ptext1, plen1, FALSE);
|
sl@0
|
232 |
generateSignatureVector(key2, ptext1, plen1, FALSE);
|
sl@0
|
233 |
generateSignatureVector(key3, ptext1, plen1, FALSE);
|
sl@0
|
234 |
|
sl@0
|
235 |
return 0;
|
sl@0
|
236 |
}
|