summaryrefslogtreecommitdiff
path: root/other/wrez/isolation/cipher-mrsa-interface.c
blob: 40968865086e0aba3bdcc5d642bdeb11fab9a0ed (plain)
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
303
304
/* cipher-mrsa-interface.c - interface module to the mrsa module
 *
 * works around some limitations, bugs and cryptographical weaknesses within
 * the mrsa library
 */

#ifdef	WREZ
#include "int80.h"
#include "common.h"
#include "wrutil.h"
#else
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#endif

#include "cipher-mrsa.h"
#include "cipher-mrsa-interface.h"


#ifndef	WREZ
void
hexdump (char *desc, unsigned char *data, unsigned int amount);
#endif

#define	CLEAR_GAP_KEY	RSA_SIZE
#define	CLEAR_GAP_MSG	1

int
rsa_keypair_generate (rsa_pubkey *pub, rsa_privkey *priv)
{
	rsa_key	key;
	NN	msgtest,	/* test en/decryption */
		msgorig;
	int	fd = -1,
		try_max = 10;
#ifdef	WREZ
	char *	rndfilename;

	STRINGPTR (rndfilename, "/dev/urandom");
#else
	char *	rndfilename = "/dev/urandom";
#endif

new_key:
	fd = open (rndfilename, O_RDONLY);
	if (fd < 0)
		return (-1);

	memset (pub, 0x00, sizeof (*pub));
	memset (priv, 0x00, sizeof (*priv));

	if (read (fd, msgtest, sizeof (msgtest)) != sizeof (msgtest))
		goto bail;

try_key:
	memset (&key, 0x00, sizeof (key));
	if (read (fd, key.p, sizeof (key.p)) != sizeof (key.p) ||
		read (fd, key.q, sizeof (key.q)) != sizeof (key.q))
	{
		goto bail;
	}

	/* NUL out highest bit (i think this works around a stall bug in
	 * mrsa, though i'm not quite sure what triggers it). it works with
	 * this code at least.
	 */
#define	CLEAR_UPPER(count,key) \
	{ \
		int	cu, co = count; \
		for (cu = (sizeof (key) - 1) ; co > 0 ; --co, --cu) \
			((unsigned char *) key)[cu] = 0x00; \
	}

	CLEAR_UPPER (CLEAR_GAP_KEY, key.p);
	CLEAR_UPPER (CLEAR_GAP_KEY, key.q);

	/* try to generate a key out of the random data. if that fails, try
	 * again or give up when we had enough tries already.
	 */
	if (rsa_gen (&key) == 0) {
		try_max -= 1;
		if (try_max > 0)
			goto try_key;

		goto bail;
	}

	close (fd);

	/* public key components
	 */
	cp (pub->pq, key.pq);
	cp (pub->e, key.e);

	/* private key components
	 */
	cp (priv->d, key.d);
	cp (priv->p, key.p);
	cp (priv->q, key.q);
	cp (priv->dp, key.dp);
	cp (priv->dq, key.dq);
	cp (priv->qp, key.qp);

	/* test key to work around bug in mrsa.
	 * XXX: once this test succeeds the keys are safe to use and work with
	 *      all possible input
	 * XXX: forbid d = 1, this is silly
	 */
	if (priv->d[RSA_SIZE - 1] == 0x01)
		goto new_key;
	memcpy (msgorig, msgtest, sizeof (msgorig));
	rsa_encrypt (pub, msgtest);
	rsa_decrypt (priv, msgtest);
	if (memcmp (msgtest, msgorig, sizeof (msgtest) != 0))
		goto new_key;

	return (0);

bail:
	if (fd != -1)
		close (fd);

	return (-1);
}


void
rsa_keypair_dump (int fd, rsa_pubkey *pub, rsa_privkey *priv)
{
	char	str[RSA_SIZE * 4 + 3];

#define	KEYOUT(key) \
	nh (str, key); \
	str[strlen (str) + 1] = '\0'; \
	str[strlen (str)] = '\n'; \
	write (fd, str, strlen (str));

	if (pub != NULL) {
		KEYOUT (pub->pq);
		KEYOUT (pub->e);
	}

	if (priv != NULL) {
		KEYOUT (priv->d);
		KEYOUT (priv->p);
		KEYOUT (priv->q);
		KEYOUT (priv->dp);
		KEYOUT (priv->dq);
		KEYOUT (priv->qp);
	}
#undef	KEYOUT

	return;
}


/* rsa_keypair_import
 *
 * import a 512 bit rsa keypair from `fd' in human readable format to the
 * `pub' and `priv' structures. each can be set to NULL to skip them.
 *
 * return in any case
 */

void
rsa_keypair_import (int fd, rsa_pubkey *pub, rsa_privkey *priv)
{
	/* TODO */
}


void
rsa_encrypt (rsa_pubkey *pub, unsigned char *data)
{
	rsa_key	key;

	memset (&key, 0x00, sizeof (key));
	cp (key.pq, pub->pq);
	cp (key.e, pub->e);

	rsa_enc ((N) data, &key);
}


void
rsa_decrypt (rsa_privkey *priv, unsigned char *data)
{
	rsa_key	key;

	memset (&key, 0x00, sizeof (key));
	cp (key.d, priv->d);
	cp (key.p, priv->p);
	cp (key.q, priv->q);
	cp (key.dp, priv->dp);
	cp (key.dq, priv->dq);
	cp (key.qp, priv->qp);

	rsa_dec ((N) data, &key);
}


#ifdef	TESTING
int
main (int argc, char *argv[])
{
	int		n,
			fd;
	rsa_pubkey	pub;
	rsa_privkey	priv;

	NN		msg;
	NN		msg_orig;
	int		testcount = 100,
			matchcount = 0;

	n = rsa_keypair_generate (&pub, &priv);
	printf ("rsa_keypair_generate () = %d\n", n);

	/* dump keypair
	 */
	printf ("keypair\n");
	rsa_keypair_dump (2, &pub, &priv);
	printf ("\n");

	/* test, get random message and encrypt / decrypt it
	 */
testcrypt:
	fd = open ("/dev/urandom", O_RDONLY);
	read (fd, msg, sizeof (msg));
	close (fd);
	CLEAR_UPPER (CLEAR_GAP_MSG, msg);
	memcpy (msg_orig, msg, sizeof (msg_orig));
//	hexdump ("message", (void *) msg, sizeof (msg));

	rsa_encrypt (&pub, (unsigned char *) msg);
//	hexdump ("message-encrypted", (void *) msg, sizeof (msg));
	rsa_decrypt (&priv, (unsigned char *) msg);
//	hexdump ("message-decrypted", (void *) msg, sizeof (msg));

	if (memcmp (msg, msg_orig, sizeof (msg)) == 0)
		matchcount += 1;

	if (--testcount > 0)
		goto testcrypt;

	printf ("\n%d matched\n", matchcount);

	return (0);
}


void
hexdump (char *desc, unsigned char *data, unsigned int amount)
{
	unsigned int	dp, p;	/* data pointer */
	const char	trans[] =
		"................................ !\"#$%&'()*+,-./0123456789"
		":;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklm"
		"nopqrstuvwxyz{|}~...................................."
		"....................................................."
		"........................................";


	printf ("/* %s, %u bytes */\n", desc, amount);

	for (dp = 1; dp <= amount; dp++) {
		fprintf (stderr, "%02x ", data[dp-1]);
		if ((dp % 8) == 0)
			fprintf (stderr, " ");
		if ((dp % 16) == 0) {
			fprintf (stderr, "| ");
			p = dp;
			for (dp -= 16; dp < p; dp++)
				fprintf (stderr, "%c", trans[data[dp]]);
			fflush (stderr);
			fprintf (stderr, "\n");
		}
		fflush (stderr);
	}
	if ((amount % 16) != 0) {
		p = dp = 16 - (amount % 16);
		for (dp = p; dp > 0; dp--) {
			fprintf (stderr, "   ");
			if (((dp % 8) == 0) && (p != 8))
				fprintf (stderr, " ");
			fflush (stderr);
		}
		fprintf (stderr, " | ");
		for (dp = (amount - (16 - p)); dp < amount; dp++)
			fprintf (stderr, "%c", trans[data[dp]]);
		fflush (stderr);
	}
	fprintf (stderr, "\n");

	return;
}

#endif