summaryrefslogtreecommitdiff
path: root/usr/src/lib/pkcs11/pkcs11_softtoken/common/softBlowfishCrypt.c
blob: 82fbc54e0e525d702eba90f2b3ac462c12b7b719 (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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 * Copyright (c) 2018, Joyent, Inc.
 */

#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <security/cryptoki.h>
#include "softSession.h"
#include "softObject.h"
#include "softCrypt.h"
#include <blowfish_impl.h>

CK_RV
soft_blowfish_crypt_init_common(soft_session_t *session_p,
    CK_MECHANISM_PTR pMechanism, soft_object_t *key_p, boolean_t encrypt)
{
	size_t size;
	soft_blowfish_ctx_t *soft_blowfish_ctx;

	soft_blowfish_ctx = calloc(1, sizeof (soft_blowfish_ctx_t));
	if (soft_blowfish_ctx == NULL) {
		return (CKR_HOST_MEMORY);
	}

	soft_blowfish_ctx->key_sched = blowfish_alloc_keysched(&size, 0);

	if (soft_blowfish_ctx->key_sched == NULL) {
		free(soft_blowfish_ctx);
		return (CKR_HOST_MEMORY);
	}

	soft_blowfish_ctx->keysched_len = size;

	(void) pthread_mutex_lock(&session_p->session_mutex);
	if (encrypt) {
		/* Called by C_EncryptInit */
		session_p->encrypt.context = soft_blowfish_ctx;
		session_p->encrypt.mech.mechanism = pMechanism->mechanism;
	} else {
		/* Called by C_DecryptInit */
		session_p->decrypt.context = soft_blowfish_ctx;
		session_p->decrypt.mech.mechanism = pMechanism->mechanism;
	}
	(void) pthread_mutex_unlock(&session_p->session_mutex);

	/*
	 * If this is a non-sensitive key and it does NOT have
	 * a key schedule yet, then allocate one and expand it.
	 * Otherwise, if it's a non-sensitive key, and it DOES have
	 * a key schedule already attached to it, just copy the
	 * pre-expanded schedule to the context and avoid the
	 * extra key schedule expansion operation.
	 */
	if (!(key_p->bool_attr_mask & SENSITIVE_BOOL_ON)) {
		if (OBJ_KEY_SCHED(key_p) == NULL) {
			void *ks;

			(void) pthread_mutex_lock(&key_p->object_mutex);
			if (OBJ_KEY_SCHED(key_p) == NULL) {
				ks = blowfish_alloc_keysched(&size, 0);
				if (ks == NULL) {
					(void) pthread_mutex_unlock(
					    &key_p->object_mutex);
					free(soft_blowfish_ctx);
					return (CKR_HOST_MEMORY);
				}

				blowfish_init_keysched(OBJ_SEC_VALUE(key_p),
				    (OBJ_SEC_VALUE_LEN(key_p) * 8), ks);

				OBJ_KEY_SCHED_LEN(key_p) = size;
				OBJ_KEY_SCHED(key_p) = ks;
			}
			(void) pthread_mutex_unlock(&key_p->object_mutex);
		}
		(void) memcpy(soft_blowfish_ctx->key_sched,
		    OBJ_KEY_SCHED(key_p), OBJ_KEY_SCHED_LEN(key_p));
		soft_blowfish_ctx->keysched_len = OBJ_KEY_SCHED_LEN(key_p);

	} else {
		/*
		 * Initialize key schedule for Blowfish.
		 * blowfish_init_keysched() requires key length in bits.
		 */
		blowfish_init_keysched(OBJ_SEC_VALUE(key_p),
		    (OBJ_SEC_VALUE_LEN(key_p) * 8),
		    soft_blowfish_ctx->key_sched);
	}
	return (CKR_OK);
}


/*
 * soft_blowfish_encrypt_common()
 *
 * Arguments:
 *      session_p:	pointer to soft_session_t struct
 *	pData:		pointer to the input data to be encrypted
 *	ulDataLen:	length of the input data
 *	pEncrypted:	pointer to the output data after encryption
 *	pulEncryptedLen: pointer to the length of the output data
 *	update:		boolean flag indicates caller is soft_encrypt
 *			or soft_encrypt_update
 *
 * Description:
 *      This function calls the corresponding encrypt routine based
 *	on the mechanism.
 *
 * Returns:
 *      CKR_OK: success
 *      CKR_BUFFER_TOO_SMALL: the output buffer provided by application
 *			      is too small
 *	CKR_FUNCTION_FAILED: encrypt function failed
 *	CKR_DATA_LEN_RANGE: the input data is not a multiple of blocksize
 */
CK_RV
soft_blowfish_encrypt_common(soft_session_t *session_p, CK_BYTE_PTR pData,
    CK_ULONG ulDataLen, CK_BYTE_PTR pEncrypted, CK_ULONG_PTR pulEncryptedLen,
    boolean_t update)
{
	int rc = 0;
	CK_RV rv = CKR_OK;
	soft_blowfish_ctx_t *soft_blowfish_ctx =
	    (soft_blowfish_ctx_t *)session_p->encrypt.context;
	blowfish_ctx_t *blowfish_ctx;
	CK_BYTE *in_buf = NULL;
	CK_BYTE *out_buf = NULL;
	CK_ULONG out_len;
	CK_ULONG total_len;
	CK_ULONG remain;
	crypto_data_t out;

	/*
	 * Blowfish only takes input length that is a multiple of blocksize
	 * for C_Encrypt function with the mechanism CKM_BLOWFISH_CBC.
	 *
	 */
	if (!update) {
		if ((ulDataLen % BLOWFISH_BLOCK_LEN) != 0) {
			rv = CKR_DATA_LEN_RANGE;
			goto cleanup;
		}

		out_len = ulDataLen;
		/*
		 * If application asks for the length of the output buffer
		 * to hold the ciphertext?
		 */
		if (pEncrypted == NULL) {
			*pulEncryptedLen = out_len;
			return (CKR_OK);
		}

		/* Is the application-supplied buffer large enough? */
		if (*pulEncryptedLen < out_len) {
			*pulEncryptedLen = out_len;
			return (CKR_BUFFER_TOO_SMALL);
		}

		in_buf = pData;
		out_buf = pEncrypted;
	} else {
		/*
		 * Called by C_EncryptUpdate
		 *
		 * Add the lengths of last remaining data and current
		 * plaintext together to get the total input length.
		 */
		total_len = soft_blowfish_ctx->remain_len + ulDataLen;

		/*
		 * If the total input length is less than one blocksize,
		 * we will need to delay encryption until when more data
		 * comes in next C_EncryptUpdate or when C_EncryptFinal
		 * is called.
		 */
		if (total_len < BLOWFISH_BLOCK_LEN) {
			if (pEncrypted != NULL) {
				/*
				 * Save input data and its length in
				 * the remaining buffer of BLOWFISH context.
				 */
				(void) memcpy(soft_blowfish_ctx->data +
				    soft_blowfish_ctx->remain_len, pData,
				    ulDataLen);
				soft_blowfish_ctx->remain_len += ulDataLen;
			}

			/* Set encrypted data length to 0. */
			*pulEncryptedLen = 0;
			return (CKR_OK);
		}

		/* Compute the length of remaing data. */
		remain = total_len % BLOWFISH_BLOCK_LEN;

		/*
		 * Make sure that the output length is a multiple of
		 * blocksize.
		 */
		out_len = total_len - remain;

		/*
		 * If application asks for the length of the output buffer
		 * to hold the ciphertext?
		 */
		if (pEncrypted == NULL) {
			*pulEncryptedLen = out_len;
			return (CKR_OK);
		}

		/* Is the application-supplied buffer large enough? */
		if (*pulEncryptedLen < out_len) {
			*pulEncryptedLen = out_len;
			return (CKR_BUFFER_TOO_SMALL);
		}

		if (soft_blowfish_ctx->remain_len != 0) {
			/*
			 * Copy last remaining data and current input data
			 * to the output buffer.
			 */
			(void) memmove(pEncrypted +
			    soft_blowfish_ctx->remain_len,
			    pData, out_len - soft_blowfish_ctx->remain_len);
			(void) memcpy(pEncrypted, soft_blowfish_ctx->data,
			    soft_blowfish_ctx->remain_len);
			bzero(soft_blowfish_ctx->data,
			    soft_blowfish_ctx->remain_len);

			in_buf = pEncrypted;
		} else {
			in_buf = pData;
		}
		out_buf = pEncrypted;
	}

	/*
	 * Begin Encryption now.
	 */

	out.cd_format = CRYPTO_DATA_RAW;
	out.cd_offset = 0;
	out.cd_length = out_len;
	out.cd_raw.iov_base = (char *)out_buf;
	out.cd_raw.iov_len = out_len;

	/* Encrypt multiple blocks of data. */
	rc = blowfish_encrypt_contiguous_blocks(
	    (blowfish_ctx_t *)soft_blowfish_ctx->blowfish_cbc,
	    (char *)in_buf, out_len, &out);

	if (rc == 0) {
		*pulEncryptedLen = out_len;
		if (update) {
			/*
			 * For encrypt update, if there is remaining data,
			 * save it and it's length in the context.
			 */
			if (remain != 0)
				(void) memcpy(soft_blowfish_ctx->data, pData +
				    (ulDataLen - remain), remain);

			soft_blowfish_ctx->remain_len = remain;
			return (CKR_OK);
		}

	} else {
		*pulEncryptedLen = 0;
		rv = CKR_FUNCTION_FAILED;
	}

cleanup:
	(void) pthread_mutex_lock(&session_p->session_mutex);
	blowfish_ctx = (blowfish_ctx_t *)soft_blowfish_ctx->blowfish_cbc;
	freezero(blowfish_ctx, sizeof (cbc_ctx_t));
	freezero(soft_blowfish_ctx->key_sched,
	    soft_blowfish_ctx->keysched_len);
	freezero(session_p->encrypt.context,
	    sizeof (soft_blowfish_ctx_t));
	session_p->encrypt.context = NULL;
	(void) pthread_mutex_unlock(&session_p->session_mutex);

	return (rv);
}


CK_RV
soft_blowfish_decrypt_common(soft_session_t *session_p, CK_BYTE_PTR pEncrypted,
    CK_ULONG ulEncryptedLen, CK_BYTE_PTR pData, CK_ULONG_PTR pulDataLen,
    boolean_t update)
{
	int rc = 0;
	CK_RV rv = CKR_OK;
	soft_blowfish_ctx_t *soft_blowfish_ctx =
	    (soft_blowfish_ctx_t *)session_p->decrypt.context;
	blowfish_ctx_t *blowfish_ctx;
	CK_BYTE *in_buf = NULL;
	CK_BYTE *out_buf = NULL;
	CK_ULONG out_len;
	CK_ULONG total_len;
	CK_ULONG remain;
	crypto_data_t out;

	/*
	 * Blowfish only takes input length that is a multiple of 16 bytes
	 * for C_Decrypt function using CKM_BLOWFISH_CBC.
	 */

	if (!update) {
		/* Called by C_Decrypt */
		if ((ulEncryptedLen % BLOWFISH_BLOCK_LEN) != 0) {
			rv = CKR_ENCRYPTED_DATA_LEN_RANGE;
			goto cleanup;
		}

		/*
		 * If application asks for the length of the output buffer
		 * to hold the plaintext?
		 */
		if (pData == NULL) {
			*pulDataLen = ulEncryptedLen;
			return (CKR_OK);
		}

		/* Is the application-supplied buffer large enough? */
		if (*pulDataLen < ulEncryptedLen) {
			*pulDataLen = ulEncryptedLen;
			return (CKR_BUFFER_TOO_SMALL);
		}
		out_len = ulEncryptedLen;
		in_buf = pEncrypted;
		out_buf = pData;
	} else {
		/*
		 * Called by C_DecryptUpdate
		 *
		 * Add the lengths of last remaining data and current
		 * input data together to get the total input length.
		 */
		total_len = soft_blowfish_ctx->remain_len + ulEncryptedLen;

		if (total_len < BLOWFISH_BLOCK_LEN) {
			if (pData != NULL) {
				(void) memcpy(soft_blowfish_ctx->data +
				    soft_blowfish_ctx->remain_len,
				    pEncrypted, ulEncryptedLen);

				soft_blowfish_ctx->remain_len += ulEncryptedLen;
			}

			/* Set output data length to 0. */
			*pulDataLen = 0;
			return (CKR_OK);
		}

		/* Compute the length of remaining data. */
		remain = total_len % BLOWFISH_BLOCK_LEN;

		/*
		 * Make sure that the output length is a multiple of
		 * blocksize.
		 */
		out_len = total_len - remain;

		/*
		 * if application asks for the length of the output buffer
		 * to hold the plaintext?
		 */
		if (pData == NULL) {
			*pulDataLen = out_len;
			return (CKR_OK);
		}

		/*
		 * Is the application-supplied buffer large enough?
		 */
		if (*pulDataLen < out_len) {
			*pulDataLen = out_len;
			return (CKR_BUFFER_TOO_SMALL);
		}

		if (soft_blowfish_ctx->remain_len != 0) {
			/*
			 * Copy last remaining data and current input data
			 * to the output buffer.
			 */
			(void) memmove(pData + soft_blowfish_ctx->remain_len,
			    pEncrypted,
			    out_len - soft_blowfish_ctx->remain_len);
			(void) memcpy(pData, soft_blowfish_ctx->data,
			    soft_blowfish_ctx->remain_len);
			bzero(soft_blowfish_ctx->data,
			    soft_blowfish_ctx->remain_len);


			in_buf = pData;
		} else {
			in_buf = pEncrypted;
		}

		out_buf = pData;
	}

	out.cd_format = CRYPTO_DATA_RAW;
	out.cd_offset = 0;
	out.cd_length = out_len;
	out.cd_raw.iov_base = (char *)out_buf;
	out.cd_raw.iov_len = out_len;

	/* Decrypt multiple blocks of data. */
	rc = blowfish_decrypt_contiguous_blocks(
	    (blowfish_ctx_t *)soft_blowfish_ctx->blowfish_cbc,
	    (char *)in_buf, out_len, &out);

	if (rc == 0) {
		*pulDataLen = out_len;
		if (update) {
			/*
			 * For decrypt update, if there is remaining data,
			 * save it and its length in the context.
			 */
			if (remain != 0)
				(void) memcpy(soft_blowfish_ctx->data,
				    pEncrypted + (ulEncryptedLen - remain),
				    remain);
			soft_blowfish_ctx->remain_len = remain;
			return (CKR_OK);
		}


	} else {
		*pulDataLen = 0;
		rv = CKR_FUNCTION_FAILED;
	}

cleanup:
	(void) pthread_mutex_lock(&session_p->session_mutex);
	blowfish_ctx = (blowfish_ctx_t *)soft_blowfish_ctx->blowfish_cbc;
	free(blowfish_ctx);
	freezero(soft_blowfish_ctx->key_sched,
	    soft_blowfish_ctx->keysched_len);
	freezero(session_p->decrypt.context,
	    sizeof (soft_blowfish_ctx_t));
	session_p->decrypt.context = NULL;
	(void) pthread_mutex_unlock(&session_p->session_mutex);

	return (rv);
}

/*
 * Allocate and initialize a context for BLOWFISH CBC mode of operation.
 */

void *
blowfish_cbc_ctx_init(void *key_sched, size_t size, uint8_t *ivec)
{

	cbc_ctx_t *cbc_ctx;

	if ((cbc_ctx = calloc(1, sizeof (cbc_ctx_t))) == NULL)
		return (NULL);

	cbc_ctx->cbc_keysched = key_sched;

	(void) memcpy(&cbc_ctx->cbc_iv[0], ivec, BLOWFISH_BLOCK_LEN);

	cbc_ctx->cbc_lastp = (uint8_t *)&(cbc_ctx->cbc_iv);
	cbc_ctx->cbc_keysched_len = size;
	cbc_ctx->cbc_flags |= CBC_MODE;

	return (cbc_ctx);
}