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
55764Sda73024 * Common Development and Distribution License (the "License").
65764Sda73024 * 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 /*
229110Sopensolaris@drydog.com * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
269110Sopensolaris@drydog.com #define ARCFOUR_LOOP_OPTIMIZED
270Sstevel@tonic-gate
28*11141Sopensolaris@drydog.com #ifndef _KERNEL
29*11141Sopensolaris@drydog.com #include <stdint.h>
30*11141Sopensolaris@drydog.com #endif /* _KERNEL */
31*11141Sopensolaris@drydog.com
320Sstevel@tonic-gate #include "arcfour.h"
330Sstevel@tonic-gate
345764Sda73024 #if defined(__amd64)
359110Sopensolaris@drydog.com /* ARCFour_key.flag values */
369110Sopensolaris@drydog.com #define ARCFOUR_ON_INTEL 1
379110Sopensolaris@drydog.com #define ARCFOUR_ON_AMD64 0
389110Sopensolaris@drydog.com
396281Sda73024 #ifdef _KERNEL
406281Sda73024 #include <sys/x86_archext.h>
416281Sda73024 #include <sys/cpuvar.h>
425764Sda73024
436281Sda73024 #else
446281Sda73024 #include <sys/auxv.h>
456281Sda73024 #endif /* _KERNEL */
466281Sda73024 #endif /* __amd64 */
476281Sda73024
489110Sopensolaris@drydog.com #ifndef __amd64
499110Sopensolaris@drydog.com /*
509110Sopensolaris@drydog.com * Initialize the key stream 'key' using the key value.
519110Sopensolaris@drydog.com *
529110Sopensolaris@drydog.com * Input:
539110Sopensolaris@drydog.com * keyval User-provided key
549110Sopensolaris@drydog.com * keyvallen Length, in bytes, of keyval
559110Sopensolaris@drydog.com * Output:
569110Sopensolaris@drydog.com * key Initialized ARCFOUR key schedule, based on keyval
579110Sopensolaris@drydog.com */
580Sstevel@tonic-gate void
arcfour_key_init(ARCFour_key * key,uchar_t * keyval,int keyvallen)590Sstevel@tonic-gate arcfour_key_init(ARCFour_key *key, uchar_t *keyval, int keyvallen)
600Sstevel@tonic-gate {
610Sstevel@tonic-gate /* EXPORT DELETE START */
620Sstevel@tonic-gate
630Sstevel@tonic-gate uchar_t ext_keyval[256];
640Sstevel@tonic-gate uchar_t tmp;
650Sstevel@tonic-gate int i, j;
660Sstevel@tonic-gate
676281Sda73024 /* Normalize key length to 256 */
680Sstevel@tonic-gate for (i = j = 0; i < 256; i++, j++) {
690Sstevel@tonic-gate if (j == keyvallen)
700Sstevel@tonic-gate j = 0;
710Sstevel@tonic-gate ext_keyval[i] = keyval[j];
720Sstevel@tonic-gate }
736281Sda73024
740Sstevel@tonic-gate for (i = 0; i < 256; i++)
750Sstevel@tonic-gate key->arr[i] = (uchar_t)i;
760Sstevel@tonic-gate
770Sstevel@tonic-gate j = 0;
780Sstevel@tonic-gate for (i = 0; i < 256; i++) {
799110Sopensolaris@drydog.com j = (j + key->arr[i] + ext_keyval[i]) & 0xff;
800Sstevel@tonic-gate tmp = key->arr[i];
810Sstevel@tonic-gate key->arr[i] = key->arr[j];
820Sstevel@tonic-gate key->arr[j] = tmp;
830Sstevel@tonic-gate }
840Sstevel@tonic-gate key->i = 0;
850Sstevel@tonic-gate key->j = 0;
860Sstevel@tonic-gate
870Sstevel@tonic-gate /* EXPORT DELETE END */
880Sstevel@tonic-gate }
899110Sopensolaris@drydog.com #endif /* !__amd64 */
900Sstevel@tonic-gate
910Sstevel@tonic-gate
920Sstevel@tonic-gate /*
935764Sda73024 * Encipher 'in' using 'key'.
949110Sopensolaris@drydog.com *
959110Sopensolaris@drydog.com * Input:
969110Sopensolaris@drydog.com * key ARCFOUR key, initialized by arcfour_key_init()
979110Sopensolaris@drydog.com * in Input text
989110Sopensolaris@drydog.com * out Buffer to contain output text
999110Sopensolaris@drydog.com * len Length, in bytes, of the in and out buffers
1009110Sopensolaris@drydog.com *
1019110Sopensolaris@drydog.com * Output:
1029110Sopensolaris@drydog.com * out Buffer containing output text
1039110Sopensolaris@drydog.com *
1049110Sopensolaris@drydog.com * Note: in and out can point to the same location
1050Sstevel@tonic-gate */
1060Sstevel@tonic-gate void
arcfour_crypt(ARCFour_key * key,uchar_t * in,uchar_t * out,size_t len)1070Sstevel@tonic-gate arcfour_crypt(ARCFour_key *key, uchar_t *in, uchar_t *out, size_t len)
1080Sstevel@tonic-gate {
1099110Sopensolaris@drydog.com /* EXPORT DELETE START */
1109110Sopensolaris@drydog.com #ifdef __amd64
1119110Sopensolaris@drydog.com if (key->flag == ARCFOUR_ON_AMD64) {
1129110Sopensolaris@drydog.com arcfour_crypt_asm(key, in, out, len);
1139110Sopensolaris@drydog.com } else { /* Intel EM64T */
1149110Sopensolaris@drydog.com #endif /* amd64 */
1150Sstevel@tonic-gate
1169110Sopensolaris@drydog.com size_t ii;
1179110Sopensolaris@drydog.com uchar_t i, j, ti, tj;
1189110Sopensolaris@drydog.com #ifdef ARCFOUR_LOOP_OPTIMIZED
1199110Sopensolaris@drydog.com uchar_t arr_ij;
1209110Sopensolaris@drydog.com #endif
1219110Sopensolaris@drydog.com #ifdef __amd64
1229110Sopensolaris@drydog.com uint32_t *arr;
1239110Sopensolaris@drydog.com #else
1249110Sopensolaris@drydog.com uchar_t *arr;
1259110Sopensolaris@drydog.com #endif
1260Sstevel@tonic-gate
1279110Sopensolaris@drydog.com #ifdef sun4u
1280Sstevel@tonic-gate /*
1290Sstevel@tonic-gate * The sun4u has a version of arcfour_crypt_aligned() hand-tuned for
1309110Sopensolaris@drydog.com * the cases where the input and output buffers are aligned on
1310Sstevel@tonic-gate * a multiple of 8-byte boundary.
1320Sstevel@tonic-gate */
1339110Sopensolaris@drydog.com int index;
1349110Sopensolaris@drydog.com uchar_t tmp;
1350Sstevel@tonic-gate
136416Skrishna index = (((uint64_t)(uintptr_t)in) & 0x7);
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate /* Get the 'in' on an 8-byte alignment */
1390Sstevel@tonic-gate if (index > 0) {
1400Sstevel@tonic-gate i = key->i;
1410Sstevel@tonic-gate j = key->j;
142416Skrishna for (index = 8 - (uint64_t)(uintptr_t)in & 0x7;
143416Skrishna (index-- > 0) && len > 0;
1440Sstevel@tonic-gate len--, in++, out++) {
1459110Sopensolaris@drydog.com ++i;
1460Sstevel@tonic-gate j = j + key->arr[i];
1470Sstevel@tonic-gate tmp = key->arr[i];
1480Sstevel@tonic-gate key->arr[i] = key->arr[j];
1490Sstevel@tonic-gate key->arr[j] = tmp;
1500Sstevel@tonic-gate tmp = key->arr[i] + key->arr[j];
1510Sstevel@tonic-gate *out = *in ^ key->arr[tmp];
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate key->i = i;
1540Sstevel@tonic-gate key->j = j;
1559110Sopensolaris@drydog.com }
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate if (len == 0)
1580Sstevel@tonic-gate return;
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate /* See if we're fortunate and 'out' got aligned as well */
1610Sstevel@tonic-gate
162416Skrishna if ((((uint64_t)(uintptr_t)out) & 7) != 0) {
1630Sstevel@tonic-gate #endif /* sun4u */
1649110Sopensolaris@drydog.com
1659110Sopensolaris@drydog.com i = key->i;
1669110Sopensolaris@drydog.com j = key->j;
1679110Sopensolaris@drydog.com arr = key->arr;
1689110Sopensolaris@drydog.com
1699110Sopensolaris@drydog.com #ifndef ARCFOUR_LOOP_OPTIMIZED
1709110Sopensolaris@drydog.com /*
1719110Sopensolaris@drydog.com * This loop is hasn't been reordered, but is kept for reference
1729110Sopensolaris@drydog.com * purposes as it's more readable
1739110Sopensolaris@drydog.com */
1749110Sopensolaris@drydog.com for (ii = 0; ii < len; ++ii) {
1759110Sopensolaris@drydog.com ++i;
1769110Sopensolaris@drydog.com ti = arr[i];
1779110Sopensolaris@drydog.com j = j + ti;
1789110Sopensolaris@drydog.com tj = arr[j];
1799110Sopensolaris@drydog.com arr[j] = ti;
1809110Sopensolaris@drydog.com arr[i] = tj;
1819110Sopensolaris@drydog.com out[ii] = in[ii] ^ arr[(ti + tj) & 0xff];
1829110Sopensolaris@drydog.com }
1839110Sopensolaris@drydog.com
1849110Sopensolaris@drydog.com #else
1859110Sopensolaris@drydog.com /*
1869110Sopensolaris@drydog.com * This for loop is optimized by carefully spreading out
1879110Sopensolaris@drydog.com * memory access and storage to avoid conflicts,
1889110Sopensolaris@drydog.com * allowing the processor to process operations in parallel
1899110Sopensolaris@drydog.com */
1909110Sopensolaris@drydog.com
1919110Sopensolaris@drydog.com /* for loop setup */
1929110Sopensolaris@drydog.com ++i;
1939110Sopensolaris@drydog.com ti = arr[i];
1949110Sopensolaris@drydog.com j = j + ti;
1959110Sopensolaris@drydog.com tj = arr[j];
1969110Sopensolaris@drydog.com arr[j] = ti;
1979110Sopensolaris@drydog.com arr[i] = tj;
1989110Sopensolaris@drydog.com arr_ij = arr[(ti + tj) & 0xff];
1999110Sopensolaris@drydog.com --len;
2009110Sopensolaris@drydog.com
2019110Sopensolaris@drydog.com for (ii = 0; ii < len; ) {
2029110Sopensolaris@drydog.com ++i;
2039110Sopensolaris@drydog.com ti = arr[i];
2049110Sopensolaris@drydog.com j = j + ti;
2059110Sopensolaris@drydog.com tj = arr[j];
2069110Sopensolaris@drydog.com arr[j] = ti;
2079110Sopensolaris@drydog.com arr[i] = tj;
2089110Sopensolaris@drydog.com
2099110Sopensolaris@drydog.com /* save result from previous loop: */
2109110Sopensolaris@drydog.com out[ii] = in[ii] ^ arr_ij;
2119110Sopensolaris@drydog.com
2129110Sopensolaris@drydog.com ++ii;
2139110Sopensolaris@drydog.com arr_ij = arr[(ti + tj) & 0xff];
2149110Sopensolaris@drydog.com }
2159110Sopensolaris@drydog.com /* save result from last loop: */
2169110Sopensolaris@drydog.com out[ii] = in[ii] ^ arr_ij;
2179110Sopensolaris@drydog.com #endif
2189110Sopensolaris@drydog.com
2199110Sopensolaris@drydog.com key->i = i;
2209110Sopensolaris@drydog.com key->j = j;
2219110Sopensolaris@drydog.com
2220Sstevel@tonic-gate #ifdef sun4u
2230Sstevel@tonic-gate } else {
2240Sstevel@tonic-gate arcfour_crypt_aligned(key, len, in, out);
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate #endif /* sun4u */
2279110Sopensolaris@drydog.com #ifdef __amd64
2289110Sopensolaris@drydog.com }
2299110Sopensolaris@drydog.com #endif /* amd64 */
2300Sstevel@tonic-gate
2310Sstevel@tonic-gate /* EXPORT DELETE END */
2320Sstevel@tonic-gate }
2336281Sda73024
2346281Sda73024
2359110Sopensolaris@drydog.com #ifdef __amd64
2366281Sda73024 /*
2376281Sda73024 * Return 1 if executing on Intel, otherwise 0 (e.g., AMD64).
238*11141Sopensolaris@drydog.com * Cache the result, as the CPU can't change.
239*11141Sopensolaris@drydog.com *
240*11141Sopensolaris@drydog.com * Note: the userland version uses getisax() and checks for an AMD-64-only
241*11141Sopensolaris@drydog.com * feature. The kernel version uses cpuid_getvendor().
2426281Sda73024 */
2436281Sda73024 int
arcfour_crypt_on_intel(void)2446281Sda73024 arcfour_crypt_on_intel(void)
2456281Sda73024 {
246*11141Sopensolaris@drydog.com static int cached_result = -1;
247*11141Sopensolaris@drydog.com
248*11141Sopensolaris@drydog.com if (cached_result == -1) { /* first time */
2496281Sda73024 #ifdef _KERNEL
250*11141Sopensolaris@drydog.com cached_result = (cpuid_getvendor(CPU) == X86_VENDOR_Intel);
2516281Sda73024 #else
252*11141Sopensolaris@drydog.com uint_t ui;
253*11141Sopensolaris@drydog.com
254*11141Sopensolaris@drydog.com (void) getisax(&ui, 1);
255*11141Sopensolaris@drydog.com cached_result = ((ui & AV_386_AMD_MMX) == 0);
2566281Sda73024 #endif /* _KERNEL */
257*11141Sopensolaris@drydog.com }
258*11141Sopensolaris@drydog.com
259*11141Sopensolaris@drydog.com return (cached_result);
2606281Sda73024 }
2619110Sopensolaris@drydog.com #endif /* __amd64 */
262