1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include "arcfour.h" 30*0Sstevel@tonic-gate 31*0Sstevel@tonic-gate /* Initialize the key stream 'key' using the key value */ 32*0Sstevel@tonic-gate void 33*0Sstevel@tonic-gate arcfour_key_init(ARCFour_key *key, uchar_t *keyval, int keyvallen) 34*0Sstevel@tonic-gate { 35*0Sstevel@tonic-gate /* EXPORT DELETE START */ 36*0Sstevel@tonic-gate 37*0Sstevel@tonic-gate uchar_t ext_keyval[256]; 38*0Sstevel@tonic-gate uchar_t tmp; 39*0Sstevel@tonic-gate int i, j; 40*0Sstevel@tonic-gate 41*0Sstevel@tonic-gate for (i = j = 0; i < 256; i++, j++) { 42*0Sstevel@tonic-gate if (j == keyvallen) 43*0Sstevel@tonic-gate j = 0; 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate ext_keyval[i] = keyval[j]; 46*0Sstevel@tonic-gate } 47*0Sstevel@tonic-gate for (i = 0; i < 256; i++) 48*0Sstevel@tonic-gate key->arr[i] = (uchar_t)i; 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gate j = 0; 51*0Sstevel@tonic-gate for (i = 0; i < 256; i++) { 52*0Sstevel@tonic-gate j = (j + key->arr[i] + ext_keyval[i]) % 256; 53*0Sstevel@tonic-gate tmp = key->arr[i]; 54*0Sstevel@tonic-gate key->arr[i] = key->arr[j]; 55*0Sstevel@tonic-gate key->arr[j] = tmp; 56*0Sstevel@tonic-gate } 57*0Sstevel@tonic-gate key->i = 0; 58*0Sstevel@tonic-gate key->j = 0; 59*0Sstevel@tonic-gate 60*0Sstevel@tonic-gate /* EXPORT DELETE END */ 61*0Sstevel@tonic-gate } 62*0Sstevel@tonic-gate 63*0Sstevel@tonic-gate 64*0Sstevel@tonic-gate /* 65*0Sstevel@tonic-gate * Encipher 'in' using 'key. 66*0Sstevel@tonic-gate * in and out can point to the same location 67*0Sstevel@tonic-gate */ 68*0Sstevel@tonic-gate void 69*0Sstevel@tonic-gate arcfour_crypt(ARCFour_key *key, uchar_t *in, uchar_t *out, size_t len) 70*0Sstevel@tonic-gate { 71*0Sstevel@tonic-gate size_t ii; 72*0Sstevel@tonic-gate uchar_t tmp, i, j; 73*0Sstevel@tonic-gate 74*0Sstevel@tonic-gate /* EXPORT DELETE START */ 75*0Sstevel@tonic-gate 76*0Sstevel@tonic-gate /* 77*0Sstevel@tonic-gate * The sun4u has a version of arcfour_crypt_aligned() hand-tuned for 78*0Sstevel@tonic-gate * the cases where the input and output buffers are aligned on 79*0Sstevel@tonic-gate * a multiple of 8-byte boundary. 80*0Sstevel@tonic-gate */ 81*0Sstevel@tonic-gate #ifdef sun4u 82*0Sstevel@tonic-gate int index; 83*0Sstevel@tonic-gate 84*0Sstevel@tonic-gate index = (((uint64_t)in) & 0x7); 85*0Sstevel@tonic-gate 86*0Sstevel@tonic-gate /* Get the 'in' on an 8-byte alignment */ 87*0Sstevel@tonic-gate if (index > 0) { 88*0Sstevel@tonic-gate i = key->i; 89*0Sstevel@tonic-gate j = key->j; 90*0Sstevel@tonic-gate for (index = 8 - (uint64_t)in & 0x7; (index-- > 0) && len > 0; 91*0Sstevel@tonic-gate len--, in++, out++) { 92*0Sstevel@tonic-gate i = i + 1; 93*0Sstevel@tonic-gate j = j + key->arr[i]; 94*0Sstevel@tonic-gate tmp = key->arr[i]; 95*0Sstevel@tonic-gate key->arr[i] = key->arr[j]; 96*0Sstevel@tonic-gate key->arr[j] = tmp; 97*0Sstevel@tonic-gate tmp = key->arr[i] + key->arr[j]; 98*0Sstevel@tonic-gate *out = *in ^ key->arr[tmp]; 99*0Sstevel@tonic-gate } 100*0Sstevel@tonic-gate key->i = i; 101*0Sstevel@tonic-gate key->j = j; 102*0Sstevel@tonic-gate 103*0Sstevel@tonic-gate } 104*0Sstevel@tonic-gate if (len == 0) 105*0Sstevel@tonic-gate return; 106*0Sstevel@tonic-gate 107*0Sstevel@tonic-gate /* See if we're fortunate and 'out' got aligned as well */ 108*0Sstevel@tonic-gate 109*0Sstevel@tonic-gate if ((((uint64_t)out) & 7) != 0) { 110*0Sstevel@tonic-gate #endif /* sun4u */ 111*0Sstevel@tonic-gate i = key->i; 112*0Sstevel@tonic-gate j = key->j; 113*0Sstevel@tonic-gate for (ii = 0; ii < len; ii++) { 114*0Sstevel@tonic-gate i = i + 1; 115*0Sstevel@tonic-gate j = j + key->arr[i]; 116*0Sstevel@tonic-gate tmp = key->arr[i]; 117*0Sstevel@tonic-gate key->arr[i] = key->arr[j]; 118*0Sstevel@tonic-gate key->arr[j] = tmp; 119*0Sstevel@tonic-gate tmp = key->arr[i] + key->arr[j]; 120*0Sstevel@tonic-gate out[ii] = in[ii] ^ key->arr[tmp]; 121*0Sstevel@tonic-gate } 122*0Sstevel@tonic-gate key->i = i; 123*0Sstevel@tonic-gate key->j = j; 124*0Sstevel@tonic-gate #ifdef sun4u 125*0Sstevel@tonic-gate } else { 126*0Sstevel@tonic-gate arcfour_crypt_aligned(key, len, in, out); 127*0Sstevel@tonic-gate } 128*0Sstevel@tonic-gate #endif /* sun4u */ 129*0Sstevel@tonic-gate 130*0Sstevel@tonic-gate /* EXPORT DELETE END */ 131*0Sstevel@tonic-gate } 132