📄 stream.c
字号:
b=halfrow[2*(j+8+i)+1]; halfrow[2*(j+i)+1] = (t&0x00ff00ff00ff00ffULL) | ((b&0x00ff00ff00ff00ffULL)<<8); halfrow[2*(j+8+i)+1] =((t&0xff00ff00ff00ff00ULL)>>8) | (b&0xff00ff00ff00ff00ULL); } } for(j=0;j<64;j+=8){ unsigned long long int t,b; for(i=0;i<4;i++){ t=halfrow[2*(j+i)]; b=halfrow[2*(j+4+i)]; halfrow[2*(j+i)] =((t&0x0f0f0f0f0f0f0f0fULL)<<4) | (b&0x0f0f0f0f0f0f0f0fULL); halfrow[2*(j+4+i)] = (t&0xf0f0f0f0f0f0f0f0ULL) | ((b&0xf0f0f0f0f0f0f0f0ULL)>>4); t=halfrow[2*(j+i)+1]; b=halfrow[2*(j+4+i)+1]; halfrow[2*(j+i)+1] =((t&0x0f0f0f0f0f0f0f0fULL)<<4) | (b&0x0f0f0f0f0f0f0f0fULL); halfrow[2*(j+4+i)+1] = (t&0xf0f0f0f0f0f0f0f0ULL) | ((b&0xf0f0f0f0f0f0f0f0ULL)>>4); } } for(j=0;j<64;j+=4){ unsigned long long int t,b; for(i=0;i<2;i++){ t=halfrow[2*(j+i)]; b=halfrow[2*(j+2+i)]; halfrow[2*(j+i)] =((t&0x3333333333333333ULL)<<2) | (b&0x3333333333333333ULL); halfrow[2*(j+2+i)] = (t&0xccccccccccccccccULL) | ((b&0xccccccccccccccccULL)>>2); t=halfrow[2*(j+i)+1]; b=halfrow[2*(j+2+i)+1]; halfrow[2*(j+i)+1] =((t&0x3333333333333333ULL)<<2) | (b&0x3333333333333333ULL); halfrow[2*(j+2+i)+1] = (t&0xccccccccccccccccULL) | ((b&0xccccccccccccccccULL)>>2); } } for(j=0;j<64;j+=2){ unsigned long long int t,b; for(i=0;i<1;i++){ t=halfrow[2*(j+i)]; b=halfrow[2*(j+1+i)]; halfrow[2*(j+i)] =((t&0x5555555555555555ULL)<<1) | (b&0x5555555555555555ULL); halfrow[2*(j+1+i)] = (t&0xaaaaaaaaaaaaaaaaULL) | ((b&0xaaaaaaaaaaaaaaaaULL)>>1); t=halfrow[2*(j+i)+1]; b=halfrow[2*(j+1+i)+1]; halfrow[2*(j+i)+1] =((t&0x5555555555555555ULL)<<1) | (b&0x5555555555555555ULL); halfrow[2*(j+1+i)+1] = (t&0xaaaaaaaaaaaaaaaaULL) | ((b&0xaaaaaaaaaaaaaaaaULL)>>1); } }#undef halfrow}static inline void trasp64_128_88cw(unsigned char *data){/* 64 rows of 128 bits transposition (bytes transp. - 8x8 rotate clockwise)*/#define halfrow ((unsigned long long int *)data) int i,j; for(j=0;j<64;j+=64){ unsigned long long int t,b; for(i=0;i<32;i++){ t=halfrow[2*(j+i)]; b=halfrow[2*(j+32+i)]; halfrow[2*(j+i)] = (t&0x00000000ffffffffULL) | ((b )<<32); halfrow[2*(j+32+i)]=((t )>>32) | (b&0xffffffff00000000ULL) ; t=halfrow[2*(j+i)+1]; b=halfrow[2*(j+32+i)+1]; halfrow[2*(j+i)+1] = (t&0x00000000ffffffffULL) | ((b )<<32); halfrow[2*(j+32+i)+1]=((t )>>32) | (b&0xffffffff00000000ULL) ; } } for(j=0;j<64;j+=32){ unsigned long long int t,b; for(i=0;i<16;i++){ t=halfrow[2*(j+i)]; b=halfrow[2*(j+16+i)]; halfrow[2*(j+i)] = (t&0x0000ffff0000ffffULL) | ((b&0x0000ffff0000ffffULL)<<16); halfrow[2*(j+16+i)]=((t&0xffff0000ffff0000ULL)>>16) | (b&0xffff0000ffff0000ULL) ; t=halfrow[2*(j+i)+1]; b=halfrow[2*(j+16+i)+1]; halfrow[2*(j+i)+1] = (t&0x0000ffff0000ffffULL) | ((b&0x0000ffff0000ffffULL)<<16); halfrow[2*(j+16+i)+1]=((t&0xffff0000ffff0000ULL)>>16) | (b&0xffff0000ffff0000ULL) ; } } for(j=0;j<64;j+=16){ unsigned long long int t,b; for(i=0;i<8;i++){ t=halfrow[2*(j+i)]; b=halfrow[2*(j+8+i)]; halfrow[2*(j+i)] = (t&0x00ff00ff00ff00ffULL) | ((b&0x00ff00ff00ff00ffULL)<<8); halfrow[2*(j+8+i)] =((t&0xff00ff00ff00ff00ULL)>>8) | (b&0xff00ff00ff00ff00ULL); t=halfrow[2*(j+i)+1]; b=halfrow[2*(j+8+i)+1]; halfrow[2*(j+i)+1] = (t&0x00ff00ff00ff00ffULL) | ((b&0x00ff00ff00ff00ffULL)<<8); halfrow[2*(j+8+i)+1] =((t&0xff00ff00ff00ff00ULL)>>8) | (b&0xff00ff00ff00ff00ULL); } } for(j=0;j<64;j+=8){ unsigned long long int t,b; for(i=0;i<4;i++){ t=halfrow[2*(j+i)]; b=halfrow[2*(j+4+i)]; halfrow[2*(j+i)] =((t&0xf0f0f0f0f0f0f0f0ULL)>>4) | (b&0xf0f0f0f0f0f0f0f0ULL); halfrow[2*(j+4+i)] = (t&0x0f0f0f0f0f0f0f0fULL) | ((b&0x0f0f0f0f0f0f0f0fULL)<<4); t=halfrow[2*(j+i)+1]; b=halfrow[2*(j+4+i)+1]; halfrow[2*(j+i)+1] =((t&0xf0f0f0f0f0f0f0f0ULL)>>4) | (b&0xf0f0f0f0f0f0f0f0ULL); halfrow[2*(j+4+i)+1] = (t&0x0f0f0f0f0f0f0f0fULL) | ((b&0x0f0f0f0f0f0f0f0fULL)<<4); } } for(j=0;j<64;j+=4){ unsigned long long int t,b; for(i=0;i<2;i++){ t=halfrow[2*(j+i)]; b=halfrow[2*(j+2+i)]; halfrow[2*(j+i)] =((t&0xccccccccccccccccULL)>>2) | (b&0xccccccccccccccccULL); halfrow[2*(j+2+i)] = (t&0x3333333333333333ULL) | ((b&0x3333333333333333ULL)<<2); t=halfrow[2*(j+i)+1]; b=halfrow[2*(j+2+i)+1]; halfrow[2*(j+i)+1] =((t&0xccccccccccccccccULL)>>2) | (b&0xccccccccccccccccULL); halfrow[2*(j+2+i)+1] = (t&0x3333333333333333ULL) | ((b&0x3333333333333333ULL)<<2); } } for(j=0;j<64;j+=2){ unsigned long long int t,b; for(i=0;i<1;i++){ t=halfrow[2*(j+i)]; b=halfrow[2*(j+1+i)]; halfrow[2*(j+i)] =((t&0xaaaaaaaaaaaaaaaaULL)>>1) | (b&0xaaaaaaaaaaaaaaaaULL); halfrow[2*(j+1+i)] = (t&0x5555555555555555ULL) | ((b&0x5555555555555555ULL)<<1); t=halfrow[2*(j+i)+1]; b=halfrow[2*(j+1+i)+1]; halfrow[2*(j+i)+1] =((t&0xaaaaaaaaaaaaaaaaULL)>>1) | (b&0xaaaaaaaaaaaaaaaaULL); halfrow[2*(j+1+i)+1] = (t&0x5555555555555555ULL) | ((b&0x5555555555555555ULL)<<1); } }#undef halfrow}#endif#ifdef STREAM_INITvoid stream_cypher_group_init( struct stream_regs *regs, group iA[8][4], // [In] iA00,iA01,...iA73 32 groups | Derived from key. group iB[8][4], // [In] iB00,iB01,...iB73 32 groups | Derived from key. unsigned char *sb) // [In] (SB0,SB1,...SB7)...x32 32*8 bytes | Extra input.#endif#ifdef STREAM_NORMALvoid stream_cypher_group_normal( struct stream_regs *regs, unsigned char *cb) // [Out] (CB0,CB1,...CB7)...x32 32*8 bytes | Output.#endif{#ifdef STREAM_INIT group in1[4]; group in2[4];#endif group extra_B[4]; group fa,fb,fc,fd,fe; group s1a,s1b,s2a,s2b,s3a,s3b,s4a,s4b,s5a,s5b,s6a,s6b,s7a,s7b; group next_E[4]; group tmp0,tmp1,tmp2,tmp3,tmp4;#ifdef STREAM_INIT group *sb_g=(group *)sb;#endif#ifdef STREAM_NORMAL group *cb_g=(group *)cb;#endif int aboff; int i,j,k,b; int dbg;#ifdef STREAM_INIT DBG(fprintf(stderr,":::::::::: BEGIN STREAM INIT\n"));#endif#ifdef STREAM_NORMAL DBG(fprintf(stderr,":::::::::: BEGIN STREAM NORMAL\n"));#endif#ifdef STREAM_INITfor(j=0;j<64;j++){ DBG(fprintf(stderr,"precall prerot stream_in[%2i]=",j)); DBG(dump_mem("",sb+BYPG*j,BYPG,BYPG));}DBG(dump_mem("stream_prerot ",sb,GROUP_PARALLELISM*8,BYPG));#if GROUP_PARALLELISM==32trasp64_32_88ccw(sb);#endif#if GROUP_PARALLELISM==64trasp64_64_88ccw(sb);#endif#if GROUP_PARALLELISM==128trasp64_128_88ccw(sb);#endifDBG(dump_mem("stream_postrot",sb,GROUP_PARALLELISM*8,BYPG));for(j=0;j<64;j++){ DBG(fprintf(stderr,"precall stream_in[%2i]=",j)); DBG(dump_mem("",sb+BYPG*j,BYPG,BYPG));}#endif aboff=32;#ifdef STREAM_INIT // load first 32 bits of ck into A[aboff+0]..A[aboff+7] // load last 32 bits of ck into B[aboff+0]..B[aboff+7] // all other regs = 0 for(i=0;i<8;i++){ for(b=0;b<4;b++){DBG(fprintf(stderr,"dbg from iA A[%i][%i]=",i,b));DBG(dump_mem("",(unsigned char *)&iA[i][b],BYPG,BYPG));DBG(fprintf(stderr," dbg from iB B[%i][%i]=",i,b));DBG(dump_mem("",(unsigned char *)&iB[i][b],BYPG,BYPG)); regs->A[aboff+i][b]=iA[i][b]; regs->B[aboff+i][b]=iB[i][b]; } } for(b=0;b<4;b++){ regs->A[aboff+8][b]=FF0(); regs->A[aboff+9][b]=FF0(); regs->B[aboff+8][b]=FF0(); regs->B[aboff+9][b]=FF0(); } for(b=0;b<4;b++){ regs->X[b]=FF0(); regs->Y[b]=FF0(); regs->Z[b]=FF0(); regs->D[b]=FF0(); regs->E[b]=FF0(); regs->F[b]=FF0(); } regs->p=FF0(); regs->q=FF0(); regs->r=FF0();#endiffor(dbg=0;dbg<4;dbg++){ DBG(fprintf(stderr,"dbg A0[%i]=",dbg)); DBG(dump_mem("",(unsigned char *)®s->A[aboff+0][dbg],BYPG,BYPG)); DBG(fprintf(stderr,"dbg B0[%i]=",dbg)); DBG(dump_mem("",(unsigned char *)®s->B[aboff+0][dbg],BYPG,BYPG));}//////////////////////////////////////////////////////////////////////////////// // EXTERNAL LOOP - 8 bytes per operation for(i=0;i<8;i++){ DBG(fprintf(stderr,"--BEGIN EXTERNAL LOOP %i\n",i));#ifdef STREAM_INIT for(b=0;b<4;b++){ in1[b]=sb_g[8*i+4+b]; in2[b]=sb_g[8*i+b]; }#endif // INTERNAL LOOP - 2 bits per iteration for(j=0; j<4; j++){ DBG(fprintf(stderr,"---BEGIN INTERNAL LOOP %i (EXT %i, INT %i)\n",j,i,j)); // from A0..A9, 35 bits are selected as inputs to 7 s-boxes // 5 bits input per s-box, 2 bits output per s-box // we can select bits with zero masking and shifting operations // and synthetize s-boxes with optimized boolean functions. // this is the actual reason we do all the crazy transposition // stuff to switch between normal and bit slice representations. // this code really flies. fe=regs->A[aboff+3][0];fa=regs->A[aboff+0][2];fb=regs->A[aboff+5][1];fc=regs->A[aboff+6][3];fd=regs->A[aboff+8][0];/* 1000 1110 1110 0001 : lev 7: */ //tmp0=( fa^( fb^( ( ( ( fa|fb )^fc )|( fc^fd ) )^ALL_ONES ) ) );/* 1110 0010 0011 0011 : lev 6: */ //tmp1=( ( fa|fb )^( ( fc&( fa|( fb^fd ) ) )^ALL_ONES ) );/* 0011 0110 1000 1101 : lev 5: */ //tmp2=( fa^( ( fb&fd )^( ( fa&fd )|fc ) ) );/* 0101 0101 1001 0011 : lev 5: */ //tmp3=( ( fa&fc )^( fa^( ( fa&fb )|fd ) ) );/* 1000 1110 1110 0001 : lev 7: */ tmp0=FFXOR(fa,FFXOR(fb,FFXOR(FFOR(FFXOR(FFOR(fa,fb),fc),FFXOR(fc,fd)),FF1())));/* 1110 0010 0011 0011 : lev 6: */ tmp1=FFXOR(FFOR(fa,fb),FFXOR(FFAND(fc,FFOR(fa,FFXOR(fb,fd))),FF1()));/* 0011 0110 1000 1101 : lev 5: */ tmp2=FFXOR(fa,FFXOR(FFAND(fb,fd),FFOR(FFAND(fa,fd),fc)));/* 0101 0101 1001 0011 : lev 5: */ tmp3=FFXOR(FFAND(fa,fc),FFXOR(fa,FFOR(FFAND(fa,fb),fd))); s1a=FFXOR(tmp0,FFAND(fe,tmp1)); s1b=FFXOR(tmp2,FFAND(fe,tmp3));//dump_mem("s1as1b-fe",&fe,BYPG,BYPG);//dump_mem("s1as1b-fa",&fa,BYPG,BYPG);//dump_mem("s1as1b-fb",&fb,BYPG,BYPG);//dump_mem("s1as1b-fc",&fc,BYPG,BYPG);//dump_mem("s1as1b-fd",&fd,BYPG,BYPG); fe=regs->A[aboff+1][1];fa=regs->A[aboff+2][2];fb=regs->A[aboff+5][3];fc=regs->A[aboff+6][0];fd=regs->A[aboff+8][1];/* 1001 1110 0110 0001 : lev 6: */ //tmp0=( fa^( ( fb&( fc|fd ) )^( fc^( fd^ALL_ONES ) ) ) );/* 0000 0011 0111 1011 : lev 5: */ //tmp1=( ( fa&( fb^fd ) )|( ( fa|fb )&fc ) );/* 1100 0110 1101 0010 : lev 6: */ //tmp2=( ( fb&fd )^( ( fa&fd )|( fb^( fc^ALL_ONES ) ) ) );/* 0001 1110 1111 0101 : lev 5: */ //tmp3=( ( fa&fd )|( fa^( fb^( fc&fd ) ) ) );/* 1001 1110 0110 0001 : lev 6: */ tmp0=FFXOR(fa,FFXOR(FFAND(fb,FFOR(fc,fd)),FFXOR(fc,FFXOR(fd,FF1()))));/* 0000 0011 0111 1011 : lev 5: */ tmp1=FFOR(FFAND(fa,FFXOR(fb,fd)),FFAND(FFOR(fa,fb),fc));/* 1100 0110 1101 0010 : lev 6: */ tmp2=FFXOR(FFAND(fb,fd),FFOR(FFAND(fa,fd),FFXOR(fb,FFXOR(fc,FF1()))));/* 0001 1110 1111 0101 : lev 5: */ tmp3=FFOR(FFAND(fa,fd),FFXOR(fa,FFXOR(fb,FFAND(fc,fd)))); s2a=FFXOR(tmp0,FFAND(fe,tmp1)); s2b=FFXOR(tmp2,FFAND(fe,tmp3)); fe=regs->A[aboff+0][3];fa=regs->A[aboff+1][0];fb=regs->A[aboff+4][1];fc=regs->A[aboff+4][3];fd=regs->A[aboff+5][2];/* 0100 1011 1001 0110 : lev 5: */ //tmp0=( fa^( fb^( ( fc&( fa|fd ) )^fd ) ) );/* 1101 0101 1000 1100 : lev 7: */ //tmp1=( ( fa&fc )^( ( fa^fd )|( ( fb|fc )^( fd^ALL_ONES ) ) ) );/* 0010 0111 1101 1000 : lev 4: */ //tmp2=( fa^( ( ( fb^fc )&fd )^fc ) );/* 1111 1111 1111 1111 : lev 0: */ //tmp3=ALL_ONES;/* 0100 1011 1001 0110 : lev 5: */ tmp0=FFXOR(fa,FFXOR(fb,FFXOR(FFAND(fc,FFOR(fa,fd)),fd)));/* 1101 0101 1000 1100 : lev 7: */ tmp1=FFXOR(FFAND(fa,fc),FFOR(FFXOR(fa,fd),FFXOR(FFOR(fb,fc),FFXOR(fd,FF1()))));/* 0010 0111 1101 1000 : lev 4: */ tmp2=FFXOR(fa,FFXOR(FFAND(FFXOR(fb,fc),fd),fc));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -