45 #if defined( AES_MODES )
46 #if defined(__cplusplus)
51 #if defined( _MSC_VER ) && ( _MSC_VER > 800 )
52 #pragma intrinsic(memcpy)
61 #define FAST_BUFFER_OPERATIONS
63 #define lp32(x) ((uint_32t*)(x))
65 #if defined( USE_VIA_ACE_IF_PRESENT )
81 #if NEH_KEY_TYPE == NEH_LOAD
82 #define kd_adr(c) ((uint_8t*)(c)->ks)
83 #elif NEH_KEY_TYPE == NEH_GENERATE
84 #define kd_adr(c) ((uint_8t*)(c)->ks + (c)->inf.b[0])
86 #define kd_adr(c) ((uint_8t*)(c)->ks + ((c)->inf.b[0] == 160 ? 160 : 0))
91 #define aligned_array(type, name, no, stride) type name[no]
92 #define aligned_auto(type, name, no, stride) type name[no]
96 #if defined( _MSC_VER ) && _MSC_VER > 1200
98 #define via_cwd(cwd, ty, dir, len) \
99 unsigned long* cwd = (dir##_##ty##_table + ((len - 128) >> 4))
103 #define via_cwd(cwd, ty, dir, len) \
104 aligned_auto(unsigned long, cwd, 4, 16); \
105 cwd[1] = cwd[2] = cwd[3] = 0; \
106 cwd[0] = neh_##dir##_##ty##_key(len)
114 uint_32t i, count_eq = 0, count_neq = 0;
119 for(i = 0; i <
n; ++i)
126 else if(qh == qf + n)
147 #if defined( USE_VIA_ACE_IF_PRESENT )
149 if(ctx->
inf.
b[1] == 0xff)
150 { uint_8t *ksp = (uint_8t*)(ctx->
ks);
151 via_cwd(cwd, hybrid, enc, 2 * ctx->
inf.
b[0] - 192);
158 via_ecb_op5(ksp,cwd,ibuf,obuf,nb);
166 int m = (nb > BFR_BLOCKS ? BFR_BLOCKS : nb);
174 via_ecb_op5(ksp,cwd,ip,op,m);
190 #if !defined( ASSUME_VIA_ACE_PRESENT )
209 #if defined( USE_VIA_ACE_IF_PRESENT )
211 if(ctx->
inf.
b[1] == 0xff)
212 { uint_8t *ksp = kd_adr(ctx);
213 via_cwd(cwd, hybrid, dec, 2 * ctx->
inf.
b[0] - 192);
220 via_ecb_op5(ksp,cwd,ibuf,obuf,nb);
228 int m = (nb > BFR_BLOCKS ? BFR_BLOCKS : nb);
236 via_ecb_op5(ksp,cwd,ip,op,m);
252 #if !defined( ASSUME_VIA_ACE_PRESENT )
271 #if defined( USE_VIA_ACE_IF_PRESENT )
273 if(ctx->
inf.
b[1] == 0xff)
274 { uint_8t *ksp = (uint_8t*)(ctx->
ks), *ivp = iv;
276 via_cwd(cwd, hybrid, enc, 2 * ctx->
inf.
b[0] - 192);
289 via_cbc_op7(ksp,cwd,ibuf,obuf,nb,ivp,ivp);
297 int m = (nb > BFR_BLOCKS ? BFR_BLOCKS : nb);
305 via_cbc_op7(ksp,cwd,ip,op,m,ivp,ivp);
324 #if !defined( ASSUME_VIA_ACE_PRESENT )
325 # ifdef FAST_BUFFER_OPERATIONS
329 lp32(iv)[0] ^= lp32(ibuf)[0];
330 lp32(iv)[1] ^= lp32(ibuf)[1];
331 lp32(iv)[2] ^= lp32(ibuf)[2];
332 lp32(iv)[3] ^= lp32(ibuf)[3];
343 iv[ 0] ^= ibuf[ 0]; iv[ 1] ^= ibuf[ 1];
344 iv[ 2] ^= ibuf[ 2]; iv[ 3] ^= ibuf[ 3];
345 iv[ 4] ^= ibuf[ 4]; iv[ 5] ^= ibuf[ 5];
346 iv[ 6] ^= ibuf[ 6]; iv[ 7] ^= ibuf[ 7];
347 iv[ 8] ^= ibuf[ 8]; iv[ 9] ^= ibuf[ 9];
348 iv[10] ^= ibuf[10]; iv[11] ^= ibuf[11];
349 iv[12] ^= ibuf[12]; iv[13] ^= ibuf[13];
350 iv[14] ^= ibuf[14]; iv[15] ^= ibuf[15];
369 #if defined( USE_VIA_ACE_IF_PRESENT )
371 if(ctx->
inf.
b[1] == 0xff)
372 { uint_8t *ksp = kd_adr(ctx), *ivp = iv;
374 via_cwd(cwd, hybrid, dec, 2 * ctx->
inf.
b[0] - 192);
387 via_cbc_op6(ksp,cwd,ibuf,obuf,nb,ivp);
395 int m = (nb > BFR_BLOCKS ? BFR_BLOCKS : nb);
403 via_cbc_op6(ksp,cwd,ip,op,m,ivp);
421 #if !defined( ASSUME_VIA_ACE_PRESENT )
422 # ifdef FAST_BUFFER_OPERATIONS
429 lp32(obuf)[0] ^= lp32(iv)[0];
430 lp32(obuf)[1] ^= lp32(iv)[1];
431 lp32(obuf)[2] ^= lp32(iv)[2];
432 lp32(obuf)[3] ^= lp32(iv)[3];
444 obuf[ 0] ^= iv[ 0]; obuf[ 1] ^= iv[ 1];
445 obuf[ 2] ^= iv[ 2]; obuf[ 3] ^= iv[ 3];
446 obuf[ 4] ^= iv[ 4]; obuf[ 5] ^= iv[ 5];
447 obuf[ 6] ^= iv[ 6]; obuf[ 7] ^= iv[ 7];
448 obuf[ 8] ^= iv[ 8]; obuf[ 9] ^= iv[ 9];
449 obuf[10] ^= iv[10]; obuf[11] ^= iv[11];
450 obuf[12] ^= iv[12]; obuf[13] ^= iv[13];
451 obuf[14] ^= iv[14]; obuf[15] ^= iv[15];
462 {
int cnt = 0, b_pos = (int)ctx->
inf.
b[2], nb;
468 *obuf++ = iv[b_pos++] ^= *ibuf++;
475 if((nb = (len - cnt) >> 4) != 0)
477 #if defined( USE_VIA_ACE_IF_PRESENT )
479 if(ctx->
inf.
b[1] == 0xff)
481 uint_8t *ksp = (uint_8t*)(ctx->
ks), *ivp = iv;
483 via_cwd(cwd, hybrid, enc, 2 * ctx->
inf.
b[0] - 192);
496 via_cfb_op7(ksp, cwd, ibuf, obuf, nb, ivp, ivp);
507 m = (nb > BFR_BLOCKS ? BFR_BLOCKS : nb), nb -= m;
515 via_cfb_op7(ksp, cwd, ip, op, m, ivp, ivp);
530 # ifdef FAST_BUFFER_OPERATIONS
537 lp32(obuf)[0] = lp32(iv)[0] ^= lp32(ibuf)[0];
538 lp32(obuf)[1] = lp32(iv)[1] ^= lp32(ibuf)[1];
539 lp32(obuf)[2] = lp32(iv)[2] ^= lp32(ibuf)[2];
540 lp32(obuf)[3] = lp32(iv)[3] ^= lp32(ibuf)[3];
552 obuf[ 0] = iv[ 0] ^= ibuf[ 0]; obuf[ 1] = iv[ 1] ^= ibuf[ 1];
553 obuf[ 2] = iv[ 2] ^= ibuf[ 2]; obuf[ 3] = iv[ 3] ^= ibuf[ 3];
554 obuf[ 4] = iv[ 4] ^= ibuf[ 4]; obuf[ 5] = iv[ 5] ^= ibuf[ 5];
555 obuf[ 6] = iv[ 6] ^= ibuf[ 6]; obuf[ 7] = iv[ 7] ^= ibuf[ 7];
556 obuf[ 8] = iv[ 8] ^= ibuf[ 8]; obuf[ 9] = iv[ 9] ^= ibuf[ 9];
557 obuf[10] = iv[10] ^= ibuf[10]; obuf[11] = iv[11] ^= ibuf[11];
558 obuf[12] = iv[12] ^= ibuf[12]; obuf[13] = iv[13] ^= ibuf[13];
559 obuf[14] = iv[14] ^= ibuf[14]; obuf[15] = iv[15] ^= ibuf[15];
574 *obuf++ = iv[b_pos++] ^= *ibuf++;
581 ctx->
inf.
b[2] = (uint_8t)b_pos;
587 {
int cnt = 0, b_pos = (int)ctx->
inf.
b[2], nb;
595 *obuf++ = (
unsigned char)(t ^ iv[b_pos]);
603 if((nb = (len - cnt) >> 4) != 0)
605 #if defined( USE_VIA_ACE_IF_PRESENT )
607 if(ctx->
inf.
b[1] == 0xff)
609 uint_8t *ksp = (uint_8t*)(ctx->
ks), *ivp = iv;
611 via_cwd(cwd, hybrid, dec, 2 * ctx->
inf.
b[0] - 192);
624 via_cfb_op6(ksp, cwd, ibuf, obuf, nb, ivp);
635 m = (nb > BFR_BLOCKS ? BFR_BLOCKS : nb), nb -= m;
643 via_cfb_op6(ksp, cwd, ip, op, m, ivp);
658 # ifdef FAST_BUFFER_OPERATIONS
666 t = lp32(ibuf)[0], lp32(obuf)[0] = t ^ lp32(iv)[0], lp32(iv)[0] = t;
667 t = lp32(ibuf)[1], lp32(obuf)[1] = t ^ lp32(iv)[1], lp32(iv)[1] = t;
668 t = lp32(ibuf)[2], lp32(obuf)[2] = t ^ lp32(iv)[2], lp32(iv)[2] = t;
669 t = lp32(ibuf)[3], lp32(obuf)[3] = t ^ lp32(iv)[3], lp32(iv)[3] = t;
682 t = ibuf[ 0], obuf[ 0] = t ^ iv[ 0], iv[ 0] = t;
683 t = ibuf[ 1], obuf[ 1] = t ^ iv[ 1], iv[ 1] = t;
684 t = ibuf[ 2], obuf[ 2] = t ^ iv[ 2], iv[ 2] = t;
685 t = ibuf[ 3], obuf[ 3] = t ^ iv[ 3], iv[ 3] = t;
686 t = ibuf[ 4], obuf[ 4] = t ^ iv[ 4], iv[ 4] = t;
687 t = ibuf[ 5], obuf[ 5] = t ^ iv[ 5], iv[ 5] = t;
688 t = ibuf[ 6], obuf[ 6] = t ^ iv[ 6], iv[ 6] = t;
689 t = ibuf[ 7], obuf[ 7] = t ^ iv[ 7], iv[ 7] = t;
690 t = ibuf[ 8], obuf[ 8] = t ^ iv[ 8], iv[ 8] = t;
691 t = ibuf[ 9], obuf[ 9] = t ^ iv[ 9], iv[ 9] = t;
692 t = ibuf[10], obuf[10] = t ^ iv[10], iv[10] = t;
693 t = ibuf[11], obuf[11] = t ^ iv[11], iv[11] = t;
694 t = ibuf[12], obuf[12] = t ^ iv[12], iv[12] = t;
695 t = ibuf[13], obuf[13] = t ^ iv[13], iv[13] = t;
696 t = ibuf[14], obuf[14] = t ^ iv[14], iv[14] = t;
697 t = ibuf[15], obuf[15] = t ^ iv[15], iv[15] = t;
714 *obuf++ = (
unsigned char)(t ^ iv[b_pos]);
722 ctx->
inf.
b[2] = (uint_8t)b_pos;
728 {
int cnt = 0, b_pos = (int)ctx->
inf.
b[2], nb;
734 *obuf++ = (
unsigned char)(iv[b_pos++] ^ *ibuf++);
741 if((nb = (len - cnt) >> 4) != 0)
743 #if defined( USE_VIA_ACE_IF_PRESENT )
745 if(ctx->
inf.
b[1] == 0xff)
747 uint_8t *ksp = (uint_8t*)(ctx->
ks), *ivp = iv;
749 via_cwd(cwd, hybrid, enc, 2 * ctx->
inf.
b[0] - 192);
762 via_ofb_op6(ksp, cwd, ibuf, obuf, nb, ivp);
773 m = (nb > BFR_BLOCKS ? BFR_BLOCKS : nb), nb -= m;
781 via_ofb_op6(ksp, cwd, ip, op, m, ivp);
796 # ifdef FAST_BUFFER_OPERATIONS
803 lp32(obuf)[0] = lp32(iv)[0] ^ lp32(ibuf)[0];
804 lp32(obuf)[1] = lp32(iv)[1] ^ lp32(ibuf)[1];
805 lp32(obuf)[2] = lp32(iv)[2] ^ lp32(ibuf)[2];
806 lp32(obuf)[3] = lp32(iv)[3] ^ lp32(ibuf)[3];
818 obuf[ 0] = iv[ 0] ^ ibuf[ 0]; obuf[ 1] = iv[ 1] ^ ibuf[ 1];
819 obuf[ 2] = iv[ 2] ^ ibuf[ 2]; obuf[ 3] = iv[ 3] ^ ibuf[ 3];
820 obuf[ 4] = iv[ 4] ^ ibuf[ 4]; obuf[ 5] = iv[ 5] ^ ibuf[ 5];
821 obuf[ 6] = iv[ 6] ^ ibuf[ 6]; obuf[ 7] = iv[ 7] ^ ibuf[ 7];
822 obuf[ 8] = iv[ 8] ^ ibuf[ 8]; obuf[ 9] = iv[ 9] ^ ibuf[ 9];
823 obuf[10] = iv[10] ^ ibuf[10]; obuf[11] = iv[11] ^ ibuf[11];
824 obuf[12] = iv[12] ^ ibuf[12]; obuf[13] = iv[13] ^ ibuf[13];
825 obuf[14] = iv[14] ^ ibuf[14]; obuf[15] = iv[15] ^ ibuf[15];
840 *obuf++ = (
unsigned char)(iv[b_pos++] ^ *ibuf++);
847 ctx->
inf.
b[2] = (uint_8t)b_pos;
851 #define BFR_LENGTH (BFR_BLOCKS * AES_BLOCK_SIZE)
856 int i, blen, b_pos = (int)(ctx->
inf.
b[2]);
858 #if defined( USE_VIA_ACE_IF_PRESENT )
863 uint_8t buf[BFR_LENGTH];
873 *obuf++ = (
unsigned char)(*ibuf++ ^ buf[b_pos++]);
878 ctr_inc(cbuf), b_pos = 0;
883 blen = (len > BFR_LENGTH ? BFR_LENGTH : len), len -= blen;
885 for(i = 0, ip = buf; i < (blen >> 4); ++i)
895 #if defined( USE_VIA_ACE_IF_PRESENT )
896 if(ctx->
inf.
b[1] == 0xff)
898 via_cwd(cwd, hybrid, enc, 2 * ctx->
inf.
b[0] - 192);
899 via_ecb_op5((ctx->
ks),cwd,buf,buf,i);
907 # ifdef FAST_BUFFER_OPERATIONS
909 while(i + AES_BLOCK_SIZE <= blen)
911 lp32(obuf)[0] = lp32(ibuf)[0] ^ lp32(ip)[0];
912 lp32(obuf)[1] = lp32(ibuf)[1] ^ lp32(ip)[1];
913 lp32(obuf)[2] = lp32(ibuf)[2] ^ lp32(ip)[2];
914 lp32(obuf)[3] = lp32(ibuf)[3] ^ lp32(ip)[3];
922 while(i + AES_BLOCK_SIZE <= blen)
924 obuf[ 0] = (
unsigned char)(ibuf[ 0] ^ ip[ 0]);
925 obuf[ 1] = (
unsigned char)(ibuf[ 1] ^ ip[ 1]);
926 obuf[ 2] = (
unsigned char)(ibuf[ 2] ^ ip[ 2]);
927 obuf[ 3] = (
unsigned char)(ibuf[ 3] ^ ip[ 3]);
928 obuf[ 4] = (
unsigned char)(ibuf[ 4] ^ ip[ 4]);
929 obuf[ 5] = (
unsigned char)(ibuf[ 5] ^ ip[ 5]);
930 obuf[ 6] = (
unsigned char)(ibuf[ 6] ^ ip[ 6]);
931 obuf[ 7] = (
unsigned char)(ibuf[ 7] ^ ip[ 7]);
932 obuf[ 8] = (
unsigned char)(ibuf[ 8] ^ ip[ 8]);
933 obuf[ 9] = (
unsigned char)(ibuf[ 9] ^ ip[ 9]);
934 obuf[10] = (
unsigned char)(ibuf[10] ^ ip[10]);
935 obuf[11] = (
unsigned char)(ibuf[11] ^ ip[11]);
936 obuf[12] = (
unsigned char)(ibuf[12] ^ ip[12]);
937 obuf[13] = (
unsigned char)(ibuf[13] ^ ip[13]);
938 obuf[14] = (
unsigned char)(ibuf[14] ^ ip[14]);
939 obuf[15] = (
unsigned char)(ibuf[15] ^ ip[15]);
947 *obuf++ = (
unsigned char)(*ibuf++ ^ ip[b_pos++]);
950 ctx->
inf.
b[2] = (uint_8t)b_pos;
954 #if defined(__cplusplus)