41 #include <sys/cpuvar.h>
42
43 #else
44 #include <sys/auxv.h>
45 #endif /* _KERNEL */
46 #endif /* __amd64 */
47
48 #ifndef __amd64
49 /*
50 * Initialize the key stream 'key' using the key value.
51 *
52 * Input:
53 * keyval User-provided key
54 * keyvallen Length, in bytes, of keyval
55 * Output:
56 * key Initialized ARCFOUR key schedule, based on keyval
57 */
58 void
59 arcfour_key_init(ARCFour_key *key, uchar_t *keyval, int keyvallen)
60 {
61 /* EXPORT DELETE START */
62
63 uchar_t ext_keyval[256];
64 uchar_t tmp;
65 int i, j;
66
67 /* Normalize key length to 256 */
68 for (i = j = 0; i < 256; i++, j++) {
69 if (j == keyvallen)
70 j = 0;
71 ext_keyval[i] = keyval[j];
72 }
73
74 for (i = 0; i < 256; i++)
75 key->arr[i] = (uchar_t)i;
76
77 j = 0;
78 for (i = 0; i < 256; i++) {
79 j = (j + key->arr[i] + ext_keyval[i]) & 0xff;
80 tmp = key->arr[i];
81 key->arr[i] = key->arr[j];
82 key->arr[j] = tmp;
83 }
84 key->i = 0;
85 key->j = 0;
86
87 /* EXPORT DELETE END */
88 }
89 #endif /* !__amd64 */
90
91
92 /*
93 * Encipher 'in' using 'key'.
94 *
95 * Input:
96 * key ARCFOUR key, initialized by arcfour_key_init()
97 * in Input text
98 * out Buffer to contain output text
99 * len Length, in bytes, of the in and out buffers
100 *
101 * Output:
102 * out Buffer containing output text
103 *
104 * Note: in and out can point to the same location
105 */
106 void
107 arcfour_crypt(ARCFour_key *key, uchar_t *in, uchar_t *out, size_t len)
108 {
109 /* EXPORT DELETE START */
110 #ifdef __amd64
111 if (key->flag == ARCFOUR_ON_AMD64) {
112 arcfour_crypt_asm(key, in, out, len);
113 } else { /* Intel EM64T */
114 #endif /* amd64 */
115
116 size_t ii;
117 uchar_t i, j, ti, tj;
118 #ifdef ARCFOUR_LOOP_OPTIMIZED
119 uchar_t arr_ij;
120 #endif
121 #ifdef __amd64
122 uint32_t *arr;
123 #else
124 uchar_t *arr;
125 #endif
126
127 #ifdef sun4u
128 /*
129 * The sun4u has a version of arcfour_crypt_aligned() hand-tuned for
210 out[ii] = in[ii] ^ arr_ij;
211
212 ++ii;
213 arr_ij = arr[(ti + tj) & 0xff];
214 }
215 /* save result from last loop: */
216 out[ii] = in[ii] ^ arr_ij;
217 #endif
218
219 key->i = i;
220 key->j = j;
221
222 #ifdef sun4u
223 } else {
224 arcfour_crypt_aligned(key, len, in, out);
225 }
226 #endif /* sun4u */
227 #ifdef __amd64
228 }
229 #endif /* amd64 */
230
231 /* EXPORT DELETE END */
232 }
233
234
235 #ifdef __amd64
236 /*
237 * Return 1 if executing on Intel, otherwise 0 (e.g., AMD64).
238 * Cache the result, as the CPU can't change.
239 *
240 * Note: the userland version uses getisax() and checks for an AMD-64-only
241 * feature. The kernel version uses cpuid_getvendor().
242 */
243 int
244 arcfour_crypt_on_intel(void)
245 {
246 static int cached_result = -1;
247
248 if (cached_result == -1) { /* first time */
249 #ifdef _KERNEL
250 cached_result = (cpuid_getvendor(CPU) == X86_VENDOR_Intel);
251 #else
|
41 #include <sys/cpuvar.h>
42
43 #else
44 #include <sys/auxv.h>
45 #endif /* _KERNEL */
46 #endif /* __amd64 */
47
48 #ifndef __amd64
49 /*
50 * Initialize the key stream 'key' using the key value.
51 *
52 * Input:
53 * keyval User-provided key
54 * keyvallen Length, in bytes, of keyval
55 * Output:
56 * key Initialized ARCFOUR key schedule, based on keyval
57 */
58 void
59 arcfour_key_init(ARCFour_key *key, uchar_t *keyval, int keyvallen)
60 {
61 uchar_t ext_keyval[256];
62 uchar_t tmp;
63 int i, j;
64
65 /* Normalize key length to 256 */
66 for (i = j = 0; i < 256; i++, j++) {
67 if (j == keyvallen)
68 j = 0;
69 ext_keyval[i] = keyval[j];
70 }
71
72 for (i = 0; i < 256; i++)
73 key->arr[i] = (uchar_t)i;
74
75 j = 0;
76 for (i = 0; i < 256; i++) {
77 j = (j + key->arr[i] + ext_keyval[i]) & 0xff;
78 tmp = key->arr[i];
79 key->arr[i] = key->arr[j];
80 key->arr[j] = tmp;
81 }
82 key->i = 0;
83 key->j = 0;
84 }
85 #endif /* !__amd64 */
86
87
88 /*
89 * Encipher 'in' using 'key'.
90 *
91 * Input:
92 * key ARCFOUR key, initialized by arcfour_key_init()
93 * in Input text
94 * out Buffer to contain output text
95 * len Length, in bytes, of the in and out buffers
96 *
97 * Output:
98 * out Buffer containing output text
99 *
100 * Note: in and out can point to the same location
101 */
102 void
103 arcfour_crypt(ARCFour_key *key, uchar_t *in, uchar_t *out, size_t len)
104 {
105 #ifdef __amd64
106 if (key->flag == ARCFOUR_ON_AMD64) {
107 arcfour_crypt_asm(key, in, out, len);
108 } else { /* Intel EM64T */
109 #endif /* amd64 */
110
111 size_t ii;
112 uchar_t i, j, ti, tj;
113 #ifdef ARCFOUR_LOOP_OPTIMIZED
114 uchar_t arr_ij;
115 #endif
116 #ifdef __amd64
117 uint32_t *arr;
118 #else
119 uchar_t *arr;
120 #endif
121
122 #ifdef sun4u
123 /*
124 * The sun4u has a version of arcfour_crypt_aligned() hand-tuned for
205 out[ii] = in[ii] ^ arr_ij;
206
207 ++ii;
208 arr_ij = arr[(ti + tj) & 0xff];
209 }
210 /* save result from last loop: */
211 out[ii] = in[ii] ^ arr_ij;
212 #endif
213
214 key->i = i;
215 key->j = j;
216
217 #ifdef sun4u
218 } else {
219 arcfour_crypt_aligned(key, len, in, out);
220 }
221 #endif /* sun4u */
222 #ifdef __amd64
223 }
224 #endif /* amd64 */
225 }
226
227
228 #ifdef __amd64
229 /*
230 * Return 1 if executing on Intel, otherwise 0 (e.g., AMD64).
231 * Cache the result, as the CPU can't change.
232 *
233 * Note: the userland version uses getisax() and checks for an AMD-64-only
234 * feature. The kernel version uses cpuid_getvendor().
235 */
236 int
237 arcfour_crypt_on_intel(void)
238 {
239 static int cached_result = -1;
240
241 if (cached_result == -1) { /* first time */
242 #ifdef _KERNEL
243 cached_result = (cpuid_getvendor(CPU) == X86_VENDOR_Intel);
244 #else
|