17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6d5f5246SZdenek Kotala  * Common Development and Distribution License (the "License").
6*6d5f5246SZdenek Kotala  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*6d5f5246SZdenek Kotala  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <pthread.h>
277c478bd9Sstevel@tonic-gate #include <stdlib.h>
287c478bd9Sstevel@tonic-gate #include <string.h>
297c478bd9Sstevel@tonic-gate #include <strings.h>
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <security/cryptoki.h>
327c478bd9Sstevel@tonic-gate #include <security/pkcs11.h>
337c478bd9Sstevel@tonic-gate #include <arcfour.h>
347c478bd9Sstevel@tonic-gate #include "softSession.h"
357c478bd9Sstevel@tonic-gate #include "softObject.h"
367c478bd9Sstevel@tonic-gate #include "softCrypt.h"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * Allocate the ARCFour key stream for the active encryption or decryption
417c478bd9Sstevel@tonic-gate  * operation.
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate CK_RV
soft_arcfour_crypt_init(soft_session_t * session_p,CK_MECHANISM_PTR pMechanism,soft_object_t * key_p,boolean_t encrypt)447c478bd9Sstevel@tonic-gate soft_arcfour_crypt_init(soft_session_t *session_p, CK_MECHANISM_PTR pMechanism,
457c478bd9Sstevel@tonic-gate     soft_object_t *key_p, boolean_t encrypt)
467c478bd9Sstevel@tonic-gate {
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate 	uint8_t *keyval;
497c478bd9Sstevel@tonic-gate 	int keyvallen;
507c478bd9Sstevel@tonic-gate 	ARCFour_key *keystream;
517c478bd9Sstevel@tonic-gate 	crypto_active_op_t *active_op;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #ifdef	__sparcv9
547c478bd9Sstevel@tonic-gate 	/* LINTED */
557c478bd9Sstevel@tonic-gate 	keyvallen = (int)OBJ_SEC_VALUE_LEN(key_p);
567c478bd9Sstevel@tonic-gate #else	/* !__sparcv9 */
577c478bd9Sstevel@tonic-gate 	keyvallen = OBJ_SEC_VALUE_LEN(key_p);
587c478bd9Sstevel@tonic-gate #endif	/* __sparcv9 */
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	if ((keyvallen < ARCFOUR_MIN_KEY_BYTES) ||
617c478bd9Sstevel@tonic-gate 	    (keyvallen > ARCFOUR_MAX_KEY_BYTES))
627c478bd9Sstevel@tonic-gate 		return (CKR_KEY_SIZE_RANGE);
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	keyval = OBJ_SEC_VALUE(key_p);
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	if (keyval == NULL)
677c478bd9Sstevel@tonic-gate 		return (CKR_KEY_TYPE_INCONSISTENT);
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	keystream = malloc(sizeof (ARCFour_key));
707c478bd9Sstevel@tonic-gate 	if (keystream == NULL) {
717c478bd9Sstevel@tonic-gate 		return (CKR_HOST_MEMORY);
727c478bd9Sstevel@tonic-gate 	}
737c478bd9Sstevel@tonic-gate 	arcfour_key_init(keystream, keyval, keyvallen);
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&session_p->session_mutex);
767c478bd9Sstevel@tonic-gate 	active_op = (encrypt) ? &(session_p->encrypt) : &(session_p->decrypt);
777c478bd9Sstevel@tonic-gate 	active_op->context = keystream;
787c478bd9Sstevel@tonic-gate 	active_op->mech.mechanism = pMechanism->mechanism;
797c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&session_p->session_mutex);
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	return (CKR_OK);
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate /*
867c478bd9Sstevel@tonic-gate  * soft_arcfour_crypt()
877c478bd9Sstevel@tonic-gate  *
887c478bd9Sstevel@tonic-gate  * Arguments:
89*6d5f5246SZdenek Kotala  *      active_op:	pointer to the active operation in the session
907c478bd9Sstevel@tonic-gate  *	input:		pointer to the input data to be transformed
917c478bd9Sstevel@tonic-gate  *	inputlen:	length of the input.
927c478bd9Sstevel@tonic-gate  *	output:		pointer to the output storage.
937c478bd9Sstevel@tonic-gate  *	outputlenp:	pointer to the length of the output
947c478bd9Sstevel@tonic-gate  *
957c478bd9Sstevel@tonic-gate  * Description:
967c478bd9Sstevel@tonic-gate  *      Encrypts/Decrypts the 'input' and gets the result in the 'output'
977c478bd9Sstevel@tonic-gate  *
987c478bd9Sstevel@tonic-gate  * Returns:
997c478bd9Sstevel@tonic-gate  *      CKR_OK: success
1007c478bd9Sstevel@tonic-gate  *      CKR_BUFFER_TOO_SMALL: the output buffer provided by application
1017c478bd9Sstevel@tonic-gate  *			      is too small
102*6d5f5246SZdenek Kotala  *      CKR_ARGUMENTS_BAD: keystream is a NULL pointer, cipher is not
103*6d5f5246SZdenek Kotala  *                         initialized
1047c478bd9Sstevel@tonic-gate  */
1057c478bd9Sstevel@tonic-gate CK_RV
soft_arcfour_crypt(crypto_active_op_t * active_op,CK_BYTE_PTR input,CK_ULONG inputlen,CK_BYTE_PTR output,CK_ULONG_PTR outputlenp)1067c478bd9Sstevel@tonic-gate soft_arcfour_crypt(crypto_active_op_t *active_op, CK_BYTE_PTR input,
1077c478bd9Sstevel@tonic-gate     CK_ULONG inputlen, CK_BYTE_PTR output, CK_ULONG_PTR outputlenp)
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate 	ARCFour_key *keystream = active_op->context;
1107c478bd9Sstevel@tonic-gate 
111*6d5f5246SZdenek Kotala 	if (keystream == NULL) {
112*6d5f5246SZdenek Kotala 		return (CKR_ARGUMENTS_BAD);
113*6d5f5246SZdenek Kotala 	}
114*6d5f5246SZdenek Kotala 
1157c478bd9Sstevel@tonic-gate 	/*
1167c478bd9Sstevel@tonic-gate 	 * If application asks for the length of the output buffer
1177c478bd9Sstevel@tonic-gate 	 * to hold the transformed text
1187c478bd9Sstevel@tonic-gate 	 */
1197c478bd9Sstevel@tonic-gate 	if (output == NULL) {
1207c478bd9Sstevel@tonic-gate 		*outputlenp = inputlen;
1217c478bd9Sstevel@tonic-gate 		return (CKR_OK);
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	/* Is the application-supplied buffer large enough? */
1257c478bd9Sstevel@tonic-gate 	if (*outputlenp < inputlen) {
1267c478bd9Sstevel@tonic-gate 		*outputlenp = inputlen;
1277c478bd9Sstevel@tonic-gate 		return (CKR_BUFFER_TOO_SMALL);
1287c478bd9Sstevel@tonic-gate 	}
1297c478bd9Sstevel@tonic-gate 	arcfour_crypt(keystream, input, output, inputlen);
1307c478bd9Sstevel@tonic-gate 	*outputlenp = inputlen;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	return (CKR_OK);
1337c478bd9Sstevel@tonic-gate }
134