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 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 230Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include "arcfour.h" 300Sstevel@tonic-gate 310Sstevel@tonic-gate /* Initialize the key stream 'key' using the key value */ 320Sstevel@tonic-gate void 330Sstevel@tonic-gate arcfour_key_init(ARCFour_key *key, uchar_t *keyval, int keyvallen) 340Sstevel@tonic-gate { 350Sstevel@tonic-gate /* EXPORT DELETE START */ 360Sstevel@tonic-gate 370Sstevel@tonic-gate uchar_t ext_keyval[256]; 380Sstevel@tonic-gate uchar_t tmp; 390Sstevel@tonic-gate int i, j; 400Sstevel@tonic-gate 410Sstevel@tonic-gate for (i = j = 0; i < 256; i++, j++) { 420Sstevel@tonic-gate if (j == keyvallen) 430Sstevel@tonic-gate j = 0; 440Sstevel@tonic-gate 450Sstevel@tonic-gate ext_keyval[i] = keyval[j]; 460Sstevel@tonic-gate } 470Sstevel@tonic-gate for (i = 0; i < 256; i++) 480Sstevel@tonic-gate key->arr[i] = (uchar_t)i; 490Sstevel@tonic-gate 500Sstevel@tonic-gate j = 0; 510Sstevel@tonic-gate for (i = 0; i < 256; i++) { 520Sstevel@tonic-gate j = (j + key->arr[i] + ext_keyval[i]) % 256; 530Sstevel@tonic-gate tmp = key->arr[i]; 540Sstevel@tonic-gate key->arr[i] = key->arr[j]; 550Sstevel@tonic-gate key->arr[j] = tmp; 560Sstevel@tonic-gate } 570Sstevel@tonic-gate key->i = 0; 580Sstevel@tonic-gate key->j = 0; 590Sstevel@tonic-gate 600Sstevel@tonic-gate /* EXPORT DELETE END */ 610Sstevel@tonic-gate } 620Sstevel@tonic-gate 630Sstevel@tonic-gate 640Sstevel@tonic-gate /* 650Sstevel@tonic-gate * Encipher 'in' using 'key. 660Sstevel@tonic-gate * in and out can point to the same location 670Sstevel@tonic-gate */ 680Sstevel@tonic-gate void 690Sstevel@tonic-gate arcfour_crypt(ARCFour_key *key, uchar_t *in, uchar_t *out, size_t len) 700Sstevel@tonic-gate { 710Sstevel@tonic-gate size_t ii; 720Sstevel@tonic-gate uchar_t tmp, i, j; 730Sstevel@tonic-gate 740Sstevel@tonic-gate /* EXPORT DELETE START */ 750Sstevel@tonic-gate 760Sstevel@tonic-gate /* 770Sstevel@tonic-gate * The sun4u has a version of arcfour_crypt_aligned() hand-tuned for 780Sstevel@tonic-gate * the cases where the input and output buffers are aligned on 790Sstevel@tonic-gate * a multiple of 8-byte boundary. 800Sstevel@tonic-gate */ 810Sstevel@tonic-gate #ifdef sun4u 820Sstevel@tonic-gate int index; 830Sstevel@tonic-gate 84*416Skrishna index = (((uint64_t)(uintptr_t)in) & 0x7); 850Sstevel@tonic-gate 860Sstevel@tonic-gate /* Get the 'in' on an 8-byte alignment */ 870Sstevel@tonic-gate if (index > 0) { 880Sstevel@tonic-gate i = key->i; 890Sstevel@tonic-gate j = key->j; 90*416Skrishna for (index = 8 - (uint64_t)(uintptr_t)in & 0x7; 91*416Skrishna (index-- > 0) && len > 0; 920Sstevel@tonic-gate len--, in++, out++) { 930Sstevel@tonic-gate i = i + 1; 940Sstevel@tonic-gate j = j + key->arr[i]; 950Sstevel@tonic-gate tmp = key->arr[i]; 960Sstevel@tonic-gate key->arr[i] = key->arr[j]; 970Sstevel@tonic-gate key->arr[j] = tmp; 980Sstevel@tonic-gate tmp = key->arr[i] + key->arr[j]; 990Sstevel@tonic-gate *out = *in ^ key->arr[tmp]; 1000Sstevel@tonic-gate } 1010Sstevel@tonic-gate key->i = i; 1020Sstevel@tonic-gate key->j = j; 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate } 1050Sstevel@tonic-gate if (len == 0) 1060Sstevel@tonic-gate return; 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate /* See if we're fortunate and 'out' got aligned as well */ 1090Sstevel@tonic-gate 110*416Skrishna if ((((uint64_t)(uintptr_t)out) & 7) != 0) { 1110Sstevel@tonic-gate #endif /* sun4u */ 1120Sstevel@tonic-gate i = key->i; 1130Sstevel@tonic-gate j = key->j; 1140Sstevel@tonic-gate for (ii = 0; ii < len; ii++) { 1150Sstevel@tonic-gate i = i + 1; 1160Sstevel@tonic-gate j = j + key->arr[i]; 1170Sstevel@tonic-gate tmp = key->arr[i]; 1180Sstevel@tonic-gate key->arr[i] = key->arr[j]; 1190Sstevel@tonic-gate key->arr[j] = tmp; 1200Sstevel@tonic-gate tmp = key->arr[i] + key->arr[j]; 1210Sstevel@tonic-gate out[ii] = in[ii] ^ key->arr[tmp]; 1220Sstevel@tonic-gate } 1230Sstevel@tonic-gate key->i = i; 1240Sstevel@tonic-gate key->j = j; 1250Sstevel@tonic-gate #ifdef sun4u 1260Sstevel@tonic-gate } else { 1270Sstevel@tonic-gate arcfour_crypt_aligned(key, len, in, out); 1280Sstevel@tonic-gate } 1290Sstevel@tonic-gate #endif /* sun4u */ 1300Sstevel@tonic-gate 1310Sstevel@tonic-gate /* EXPORT DELETE END */ 1320Sstevel@tonic-gate } 133