10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*11448SZdenek.Kotala@Sun.COM * Common Development and Distribution License (the "License").
6*11448SZdenek.Kotala@Sun.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*11448SZdenek.Kotala@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <pthread.h>
270Sstevel@tonic-gate #include <stdlib.h>
280Sstevel@tonic-gate #include <string.h>
290Sstevel@tonic-gate #include <strings.h>
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <security/cryptoki.h>
320Sstevel@tonic-gate #include <security/pkcs11.h>
330Sstevel@tonic-gate #include <arcfour.h>
340Sstevel@tonic-gate #include "softSession.h"
350Sstevel@tonic-gate #include "softObject.h"
360Sstevel@tonic-gate #include "softCrypt.h"
370Sstevel@tonic-gate
380Sstevel@tonic-gate
390Sstevel@tonic-gate /*
400Sstevel@tonic-gate * Allocate the ARCFour key stream for the active encryption or decryption
410Sstevel@tonic-gate * operation.
420Sstevel@tonic-gate */
430Sstevel@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)440Sstevel@tonic-gate soft_arcfour_crypt_init(soft_session_t *session_p, CK_MECHANISM_PTR pMechanism,
450Sstevel@tonic-gate soft_object_t *key_p, boolean_t encrypt)
460Sstevel@tonic-gate {
470Sstevel@tonic-gate
480Sstevel@tonic-gate uint8_t *keyval;
490Sstevel@tonic-gate int keyvallen;
500Sstevel@tonic-gate ARCFour_key *keystream;
510Sstevel@tonic-gate crypto_active_op_t *active_op;
520Sstevel@tonic-gate
530Sstevel@tonic-gate #ifdef __sparcv9
540Sstevel@tonic-gate /* LINTED */
550Sstevel@tonic-gate keyvallen = (int)OBJ_SEC_VALUE_LEN(key_p);
560Sstevel@tonic-gate #else /* !__sparcv9 */
570Sstevel@tonic-gate keyvallen = OBJ_SEC_VALUE_LEN(key_p);
580Sstevel@tonic-gate #endif /* __sparcv9 */
590Sstevel@tonic-gate
600Sstevel@tonic-gate if ((keyvallen < ARCFOUR_MIN_KEY_BYTES) ||
610Sstevel@tonic-gate (keyvallen > ARCFOUR_MAX_KEY_BYTES))
620Sstevel@tonic-gate return (CKR_KEY_SIZE_RANGE);
630Sstevel@tonic-gate
640Sstevel@tonic-gate keyval = OBJ_SEC_VALUE(key_p);
650Sstevel@tonic-gate
660Sstevel@tonic-gate if (keyval == NULL)
670Sstevel@tonic-gate return (CKR_KEY_TYPE_INCONSISTENT);
680Sstevel@tonic-gate
690Sstevel@tonic-gate keystream = malloc(sizeof (ARCFour_key));
700Sstevel@tonic-gate if (keystream == NULL) {
710Sstevel@tonic-gate return (CKR_HOST_MEMORY);
720Sstevel@tonic-gate }
730Sstevel@tonic-gate arcfour_key_init(keystream, keyval, keyvallen);
740Sstevel@tonic-gate
750Sstevel@tonic-gate (void) pthread_mutex_lock(&session_p->session_mutex);
760Sstevel@tonic-gate active_op = (encrypt) ? &(session_p->encrypt) : &(session_p->decrypt);
770Sstevel@tonic-gate active_op->context = keystream;
780Sstevel@tonic-gate active_op->mech.mechanism = pMechanism->mechanism;
790Sstevel@tonic-gate (void) pthread_mutex_unlock(&session_p->session_mutex);
800Sstevel@tonic-gate
810Sstevel@tonic-gate return (CKR_OK);
820Sstevel@tonic-gate }
830Sstevel@tonic-gate
840Sstevel@tonic-gate
850Sstevel@tonic-gate /*
860Sstevel@tonic-gate * soft_arcfour_crypt()
870Sstevel@tonic-gate *
880Sstevel@tonic-gate * Arguments:
89*11448SZdenek.Kotala@Sun.COM * active_op: pointer to the active operation in the session
900Sstevel@tonic-gate * input: pointer to the input data to be transformed
910Sstevel@tonic-gate * inputlen: length of the input.
920Sstevel@tonic-gate * output: pointer to the output storage.
930Sstevel@tonic-gate * outputlenp: pointer to the length of the output
940Sstevel@tonic-gate *
950Sstevel@tonic-gate * Description:
960Sstevel@tonic-gate * Encrypts/Decrypts the 'input' and gets the result in the 'output'
970Sstevel@tonic-gate *
980Sstevel@tonic-gate * Returns:
990Sstevel@tonic-gate * CKR_OK: success
1000Sstevel@tonic-gate * CKR_BUFFER_TOO_SMALL: the output buffer provided by application
1010Sstevel@tonic-gate * is too small
102*11448SZdenek.Kotala@Sun.COM * CKR_ARGUMENTS_BAD: keystream is a NULL pointer, cipher is not
103*11448SZdenek.Kotala@Sun.COM * initialized
1040Sstevel@tonic-gate */
1050Sstevel@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)1060Sstevel@tonic-gate soft_arcfour_crypt(crypto_active_op_t *active_op, CK_BYTE_PTR input,
1070Sstevel@tonic-gate CK_ULONG inputlen, CK_BYTE_PTR output, CK_ULONG_PTR outputlenp)
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate ARCFour_key *keystream = active_op->context;
1100Sstevel@tonic-gate
111*11448SZdenek.Kotala@Sun.COM if (keystream == NULL) {
112*11448SZdenek.Kotala@Sun.COM return (CKR_ARGUMENTS_BAD);
113*11448SZdenek.Kotala@Sun.COM }
114*11448SZdenek.Kotala@Sun.COM
1150Sstevel@tonic-gate /*
1160Sstevel@tonic-gate * If application asks for the length of the output buffer
1170Sstevel@tonic-gate * to hold the transformed text
1180Sstevel@tonic-gate */
1190Sstevel@tonic-gate if (output == NULL) {
1200Sstevel@tonic-gate *outputlenp = inputlen;
1210Sstevel@tonic-gate return (CKR_OK);
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate /* Is the application-supplied buffer large enough? */
1250Sstevel@tonic-gate if (*outputlenp < inputlen) {
1260Sstevel@tonic-gate *outputlenp = inputlen;
1270Sstevel@tonic-gate return (CKR_BUFFER_TOO_SMALL);
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate arcfour_crypt(keystream, input, output, inputlen);
1300Sstevel@tonic-gate *outputlenp = inputlen;
1310Sstevel@tonic-gate
1320Sstevel@tonic-gate return (CKR_OK);
1330Sstevel@tonic-gate }
134