summaryrefslogtreecommitdiff
path: root/package/openssl/patches/patch-crypto_engine_eng_cryptodev_c
blob: 7b8898a5dcf4ee493f05b781060c7e48bf2e32de (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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
--- openssl-1.0.1e.orig/crypto/engine/eng_cryptodev.c	2013-02-11 16:26:04.000000000 +0100
+++ openssl-1.0.1e/crypto/engine/eng_cryptodev.c	2013-08-09 16:51:49.915851335 +0200
@@ -2,6 +2,7 @@
  * Copyright (c) 2002 Bob Beck <beck@openbsd.org>
  * Copyright (c) 2002 Theo de Raadt
  * Copyright (c) 2002 Markus Friedl
+ * Copyright (c) 2012 Nikos Mavrogiannopoulos
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -74,9 +75,7 @@ struct dev_crypto_state {
 	int d_fd;
 
 #ifdef USE_CRYPTODEV_DIGESTS
-	char dummy_mac_key[HASH_MAX_LEN];
-
-	unsigned char digest_res[HASH_MAX_LEN];
+	unsigned char digest_res[64];
 	char *mac_data;
 	int mac_len;
 #endif
@@ -157,15 +156,21 @@ static struct {
 static struct {
 	int	id;
 	int	nid;
-	int 	keylen;
+	int 	digestlen;
 } digests[] = {
+#if 0
+        /* HMAC is not supported */
 	{ CRYPTO_MD5_HMAC,		NID_hmacWithMD5,	16},
 	{ CRYPTO_SHA1_HMAC,		NID_hmacWithSHA1,	20},
-	{ CRYPTO_RIPEMD160_HMAC,	NID_ripemd160,		16/*?*/},
-	{ CRYPTO_MD5_KPDK,		NID_undef,		0},
-	{ CRYPTO_SHA1_KPDK,		NID_undef,		0},
+	{ CRYPTO_SHA2_256_HMAC,		NID_hmacWithSHA256,	32},
+	{ CRYPTO_SHA2_384_HMAC,		NID_hmacWithSHA384,	48},
+	{ CRYPTO_SHA2_512_HMAC,		NID_hmacWithSHA512,	64},
+#endif
 	{ CRYPTO_MD5,			NID_md5,		16},
 	{ CRYPTO_SHA1,			NID_sha1,		20},
+	{ CRYPTO_SHA2_256,		NID_sha256,		32},
+	{ CRYPTO_SHA2_384,		NID_sha384,		48},
+	{ CRYPTO_SHA2_512,		NID_sha512,		64},
 	{ 0,				NID_undef,		0},
 };
 #endif
@@ -182,7 +187,7 @@ open_dev_crypto(void)
 		if ((fd = open("/dev/crypto", O_RDWR, 0)) == -1)
 			return (-1);
 		/* close on exec */
-		if (fcntl(fd, F_SETFD, 1) == -1) {
+		if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
 			close(fd);
 			fd = -1;
 			return (-1);
@@ -243,13 +248,14 @@ get_cryptodev_ciphers(const int **cnids)
 	static int nids[CRYPTO_ALGORITHM_MAX];
 	struct session_op sess;
 	int fd, i, count = 0;
+	unsigned char fake_key[EVP_MAX_KEY_LENGTH];
 
 	if ((fd = get_dev_crypto()) < 0) {
 		*cnids = NULL;
 		return (0);
 	}
 	memset(&sess, 0, sizeof(sess));
-	sess.key = (caddr_t)"123456789abcdefghijklmno";
+	sess.key = (void*)fake_key;
 
 	for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
 		if (ciphers[i].nid == NID_undef)
@@ -281,6 +287,7 @@ static int
 get_cryptodev_digests(const int **cnids)
 {
 	static int nids[CRYPTO_ALGORITHM_MAX];
+	unsigned char fake_key[EVP_MAX_KEY_LENGTH];
 	struct session_op sess;
 	int fd, i, count = 0;
 
@@ -289,12 +296,12 @@ get_cryptodev_digests(const int **cnids)
 		return (0);
 	}
 	memset(&sess, 0, sizeof(sess));
-	sess.mackey = (caddr_t)"123456789abcdefghijklmno";
+	sess.mackey = fake_key;
 	for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
 		if (digests[i].nid == NID_undef)
 			continue;
 		sess.mac = digests[i].id;
-		sess.mackeylen = digests[i].keylen;
+		sess.mackeylen = 8;
 		sess.cipher = 0;
 		if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
 		    ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
@@ -382,14 +389,14 @@ cryptodev_cipher(EVP_CIPHER_CTX *ctx, un
 	cryp.ses = sess->ses;
 	cryp.flags = 0;
 	cryp.len = inl;
-	cryp.src = (caddr_t) in;
-	cryp.dst = (caddr_t) out;
+	cryp.src = (void*) in;
+	cryp.dst = (void*) out;
 	cryp.mac = 0;
 
 	cryp.op = ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
 
 	if (ctx->cipher->iv_len) {
-		cryp.iv = (caddr_t) ctx->iv;
+		cryp.iv = (void*) ctx->iv;
 		if (!ctx->encrypt) {
 			iiv = in + inl - ctx->cipher->iv_len;
 			memcpy(save_iv, iiv, ctx->cipher->iv_len);
@@ -440,7 +447,7 @@ cryptodev_init_key(EVP_CIPHER_CTX *ctx,
 	if ((state->d_fd = get_dev_crypto()) < 0)
 		return (0);
 
-	sess->key = (caddr_t)key;
+	sess->key = (void*)key;
 	sess->keylen = ctx->key_len;
 	sess->cipher = cipher;
 
@@ -660,18 +667,6 @@ digest_nid_to_cryptodev(int nid)
 }
 
 
-static int
-digest_key_length(int nid)
-{
-	int i;
-
-	for (i = 0; digests[i].id; i++)
-		if (digests[i].nid == nid)
-			return digests[i].keylen;
-	return (0);
-}
-
-
 static int cryptodev_digest_init(EVP_MD_CTX *ctx)
 {
 	struct dev_crypto_state *state = ctx->md_data;
@@ -682,7 +677,6 @@ static int cryptodev_digest_init(EVP_MD_
 		printf("cryptodev_digest_init: Can't get digest \n");
 		return (0);
 	}
-
 	memset(state, 0, sizeof(struct dev_crypto_state));
 
 	if ((state->d_fd = get_dev_crypto()) < 0) {
@@ -690,8 +684,8 @@ static int cryptodev_digest_init(EVP_MD_
 		return (0);
 	}
 
-	sess->mackey = state->dummy_mac_key;
-	sess->mackeylen = digest_key_length(ctx->digest->type);
+	sess->mackey = NULL;
+	sess->mackeylen = 0;
 	sess->mac = digest;
 
 	if (ioctl(state->d_fd, CIOCGSESSION, sess) < 0) {
@@ -707,8 +701,8 @@ static int cryptodev_digest_init(EVP_MD_
 static int cryptodev_digest_update(EVP_MD_CTX *ctx, const void *data,
 		size_t count)
 {
-	struct crypt_op cryp;
 	struct dev_crypto_state *state = ctx->md_data;
+	struct crypt_op cryp;
 	struct session_op *sess = &state->d_sess;
 
 	if (!data || state->d_fd < 0) {
@@ -717,7 +711,7 @@ static int cryptodev_digest_update(EVP_M
 	}
 
 	if (!count) {
-		return (0);
+		return (1);
 	}
 
 	if (!(ctx->flags & EVP_MD_CTX_FLAG_ONESHOT)) {
@@ -740,9 +734,9 @@ static int cryptodev_digest_update(EVP_M
 	cryp.ses = sess->ses;
 	cryp.flags = 0;
 	cryp.len = count;
-	cryp.src = (caddr_t) data;
+	cryp.src = (void*) data;
 	cryp.dst = NULL;
-	cryp.mac = (caddr_t) state->digest_res;
+	cryp.mac = (void*) state->digest_res;
 	if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
 		printf("cryptodev_digest_update: digest failed\n");
 		return (0);
@@ -757,8 +751,6 @@ static int cryptodev_digest_final(EVP_MD
 	struct dev_crypto_state *state = ctx->md_data;
 	struct session_op *sess = &state->d_sess;
 
-	int ret = 1;
-
 	if (!md || state->d_fd < 0) {
 		printf("cryptodev_digest_final: illegal input\n");
 		return(0);
@@ -772,7 +764,7 @@ static int cryptodev_digest_final(EVP_MD
 		cryp.len = state->mac_len;
 		cryp.src = state->mac_data;
 		cryp.dst = NULL;
-		cryp.mac = (caddr_t)md;
+		cryp.mac = (void*)md;
 		if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) {
 			printf("cryptodev_digest_final: digest failed\n");
 			return (0);
@@ -783,7 +775,7 @@ static int cryptodev_digest_final(EVP_MD
 
 	memcpy(md, state->digest_res, ctx->digest->md_size);
 
-	return (ret);
+	return 1;
 }
 
 
@@ -835,8 +827,8 @@ static int cryptodev_digest_copy(EVP_MD_
 
 	digest = digest_nid_to_cryptodev(to->digest->type);
 
-	sess->mackey = dstate->dummy_mac_key;
-	sess->mackeylen = digest_key_length(to->digest->type);
+	sess->mackey = NULL;
+	sess->mackeylen = 0;
 	sess->mac = digest;
 
 	dstate->d_fd = get_dev_crypto();
@@ -861,34 +853,79 @@ static int cryptodev_digest_copy(EVP_MD_
 }
 
 
-const EVP_MD cryptodev_sha1 = {
+static const EVP_MD cryptodev_sha1 = {
 	NID_sha1,
-	NID_undef, 
+	NID_sha1WithRSAEncryption,
 	SHA_DIGEST_LENGTH, 
-	EVP_MD_FLAG_ONESHOT,
+	EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|EVP_MD_FLAG_DIGALGID_ABSENT|EVP_MD_FLAG_ONESHOT,
 	cryptodev_digest_init,
 	cryptodev_digest_update,
 	cryptodev_digest_final,
 	cryptodev_digest_copy,
 	cryptodev_digest_cleanup,
-	EVP_PKEY_NULL_method,
+	EVP_PKEY_RSA_method,
 	SHA_CBLOCK,
-	sizeof(struct dev_crypto_state),
+	sizeof(EVP_MD *)+sizeof(struct dev_crypto_state),
 };
 
-const EVP_MD cryptodev_md5 = {
+static const EVP_MD cryptodev_sha256 = {
+	NID_sha256,
+	NID_sha256WithRSAEncryption,
+	SHA256_DIGEST_LENGTH, 
+	EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|EVP_MD_FLAG_DIGALGID_ABSENT|EVP_MD_FLAG_ONESHOT,
+	cryptodev_digest_init,
+	cryptodev_digest_update,
+	cryptodev_digest_final,
+	cryptodev_digest_copy,
+	cryptodev_digest_cleanup,
+	EVP_PKEY_RSA_method,
+	SHA256_CBLOCK,
+	sizeof(EVP_MD *)+sizeof(struct dev_crypto_state),
+};
+
+static const EVP_MD cryptodev_sha384 = {
+	NID_sha384,
+	NID_sha384WithRSAEncryption, 
+	SHA384_DIGEST_LENGTH, 
+	EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|EVP_MD_FLAG_DIGALGID_ABSENT|EVP_MD_FLAG_ONESHOT,
+	cryptodev_digest_init,
+	cryptodev_digest_update,
+	cryptodev_digest_final,
+	cryptodev_digest_copy,
+	cryptodev_digest_cleanup,
+	EVP_PKEY_RSA_method,
+	SHA512_CBLOCK,
+	sizeof(EVP_MD *)+sizeof(struct dev_crypto_state),
+};
+
+static const EVP_MD cryptodev_sha512 = {
+	NID_sha512,
+	NID_sha512WithRSAEncryption, 
+	SHA512_DIGEST_LENGTH, 
+	EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|EVP_MD_FLAG_DIGALGID_ABSENT|EVP_MD_FLAG_ONESHOT,
+	cryptodev_digest_init,
+	cryptodev_digest_update,
+	cryptodev_digest_final,
+	cryptodev_digest_copy,
+	cryptodev_digest_cleanup,
+	EVP_PKEY_RSA_method,
+	SHA512_CBLOCK,
+	sizeof(EVP_MD *)+sizeof(struct dev_crypto_state),
+};
+
+static const EVP_MD cryptodev_md5 = {
 	NID_md5,
-	NID_undef, 
+	NID_md5WithRSAEncryption, 
 	16 /* MD5_DIGEST_LENGTH */, 
-	EVP_MD_FLAG_ONESHOT,
+	EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|EVP_MD_FLAG_DIGALGID_ABSENT|EVP_MD_FLAG_ONESHOT,
 	cryptodev_digest_init,
 	cryptodev_digest_update,
 	cryptodev_digest_final,
 	cryptodev_digest_copy,
 	cryptodev_digest_cleanup,
-	EVP_PKEY_NULL_method,
+	EVP_PKEY_RSA_method,
 	64 /* MD5_CBLOCK */,
-	sizeof(struct dev_crypto_state),
+	sizeof(EVP_MD *)+sizeof(struct dev_crypto_state),
 };
 
 #endif /* USE_CRYPTODEV_DIGESTS */
@@ -909,6 +946,15 @@ cryptodev_engine_digests(ENGINE *e, cons
 	case NID_sha1:
 		*digest = &cryptodev_sha1;
  		break;
+	case NID_sha256:
+		*digest = &cryptodev_sha256;
+ 		break;
+	case NID_sha384:
+		*digest = &cryptodev_sha384;
+ 		break;
+	case NID_sha512:
+		*digest = &cryptodev_sha512;
+ 		break;
 	default:
 #endif /* USE_CRYPTODEV_DIGESTS */
 		*digest = NULL;
@@ -940,7 +986,7 @@ bn2crparam(const BIGNUM *a, struct crpar
 		return (1);
 	memset(b, 0, bytes);
 
-	crp->crp_p = (caddr_t) b;
+	crp->crp_p = (void*) b;
 	crp->crp_nbits = bits;
 
 	for (i = 0, j = 0; i < a->top; i++) {
@@ -1193,7 +1239,7 @@ cryptodev_dsa_do_sign(const unsigned cha
 	kop.crk_op = CRK_DSA_SIGN;
 
 	/* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
-	kop.crk_param[0].crp_p = (caddr_t)dgst;
+	kop.crk_param[0].crp_p = (void*)dgst;
 	kop.crk_param[0].crp_nbits = dlen * 8;
 	if (bn2crparam(dsa->p, &kop.crk_param[1]))
 		goto err;
@@ -1233,7 +1279,7 @@ cryptodev_dsa_verify(const unsigned char
 	kop.crk_op = CRK_DSA_VERIFY;
 
 	/* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
-	kop.crk_param[0].crp_p = (caddr_t)dgst;
+	kop.crk_param[0].crp_p = (void*)dgst;
 	kop.crk_param[0].crp_nbits = dlen * 8;
 	if (bn2crparam(dsa->p, &kop.crk_param[1]))
 		goto err;
@@ -1311,7 +1357,7 @@ cryptodev_dh_compute_key(unsigned char *
 		goto err;
 	kop.crk_iparams = 3;
 
-	kop.crk_param[3].crp_p = (caddr_t) key;
+	kop.crk_param[3].crp_p = (void*) key;
 	kop.crk_param[3].crp_nbits = keylen * 8;
 	kop.crk_oparams = 1;
 
@@ -1385,7 +1431,7 @@ ENGINE_load_cryptodev(void)
 	put_dev_crypto(fd);
 
 	if (!ENGINE_set_id(engine, "cryptodev") ||
-	    !ENGINE_set_name(engine, "BSD cryptodev engine") ||
+	    !ENGINE_set_name(engine, "cryptodev engine") ||
 	    !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||
 	    !ENGINE_set_digests(engine, cryptodev_engine_digests) ||
 	    !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) ||