📄 blowfish.c
字号:
2263052895UL, 1917689273UL, 448879540UL, 3550394620UL, 3981727096UL, 150775221UL, 3627908307UL, 1303187396UL, 508620638UL, 2975983352UL, 2726630617UL, 1817252668UL, 1876281319UL, 1457606340UL, 908771278UL, 3720792119UL, 3617206836UL, 2455994898UL, 1729034894UL, 1080033504UL}, { 976866871UL, 3556439503UL, 2881648439UL, 1522871579UL, 1555064734UL, 1336096578UL, 3548522304UL, 2579274686UL, 3574697629UL, 3205460757UL, 3593280638UL, 3338716283UL, 3079412587UL, 564236357UL, 2993598910UL, 1781952180UL, 1464380207UL, 3163844217UL, 3332601554UL, 1699332808UL, 1393555694UL, 1183702653UL, 3581086237UL, 1288719814UL, 691649499UL, 2847557200UL, 2895455976UL, 3193889540UL, 2717570544UL, 1781354906UL, 1676643554UL, 2592534050UL, 3230253752UL, 1126444790UL, 2770207658UL, 2633158820UL, 2210423226UL, 2615765581UL, 2414155088UL, 3127139286UL, 673620729UL, 2805611233UL, 1269405062UL, 4015350505UL, 3341807571UL, 4149409754UL, 1057255273UL, 2012875353UL, 2162469141UL, 2276492801UL, 2601117357UL, 993977747UL, 3918593370UL, 2654263191UL, 753973209UL, 36408145UL, 2530585658UL, 25011837UL, 3520020182UL, 2088578344UL, 530523599UL, 2918365339UL, 1524020338UL, 1518925132UL, 3760827505UL, 3759777254UL, 1202760957UL, 3985898139UL, 3906192525UL, 674977740UL, 4174734889UL, 2031300136UL, 2019492241UL, 3983892565UL, 4153806404UL, 3822280332UL, 352677332UL, 2297720250UL, 60907813UL, 90501309UL, 3286998549UL, 1016092578UL, 2535922412UL, 2839152426UL, 457141659UL, 509813237UL, 4120667899UL, 652014361UL, 1966332200UL, 2975202805UL, 55981186UL, 2327461051UL, 676427537UL, 3255491064UL, 2882294119UL, 3433927263UL, 1307055953UL, 942726286UL, 933058658UL, 2468411793UL, 3933900994UL, 4215176142UL, 1361170020UL, 2001714738UL, 2830558078UL, 3274259782UL, 1222529897UL, 1679025792UL, 2729314320UL, 3714953764UL, 1770335741UL, 151462246UL, 3013232138UL, 1682292957UL, 1483529935UL, 471910574UL, 1539241949UL, 458788160UL, 3436315007UL, 1807016891UL, 3718408830UL, 978976581UL, 1043663428UL, 3165965781UL, 1927990952UL, 4200891579UL, 2372276910UL, 3208408903UL, 3533431907UL, 1412390302UL, 2931980059UL, 4132332400UL, 1947078029UL, 3881505623UL, 4168226417UL, 2941484381UL, 1077988104UL, 1320477388UL, 886195818UL, 18198404UL, 3786409000UL, 2509781533UL, 112762804UL, 3463356488UL, 1866414978UL, 891333506UL, 18488651UL, 661792760UL, 1628790961UL, 3885187036UL, 3141171499UL, 876946877UL, 2693282273UL, 1372485963UL, 791857591UL, 2686433993UL, 3759982718UL, 3167212022UL, 3472953795UL, 2716379847UL, 445679433UL, 3561995674UL, 3504004811UL, 3574258232UL, 54117162UL, 3331405415UL, 2381918588UL, 3769707343UL, 4154350007UL, 1140177722UL, 4074052095UL, 668550556UL, 3214352940UL, 367459370UL, 261225585UL, 2610173221UL, 4209349473UL, 3468074219UL, 3265815641UL, 314222801UL, 3066103646UL, 3808782860UL, 282218597UL, 3406013506UL, 3773591054UL, 379116347UL, 1285071038UL, 846784868UL, 2669647154UL, 3771962079UL, 3550491691UL, 2305946142UL, 453669953UL, 1268987020UL, 3317592352UL, 3279303384UL, 3744833421UL, 2610507566UL, 3859509063UL, 266596637UL, 3847019092UL, 517658769UL, 3462560207UL, 3443424879UL, 370717030UL, 4247526661UL, 2224018117UL, 4143653529UL, 4112773975UL, 2788324899UL, 2477274417UL, 1456262402UL, 2901442914UL, 1517677493UL, 1846949527UL, 2295493580UL, 3734397586UL, 2176403920UL, 1280348187UL, 1908823572UL, 3871786941UL, 846861322UL, 1172426758UL, 3287448474UL, 3383383037UL, 1655181056UL, 3139813346UL, 901632758UL, 1897031941UL, 2986607138UL, 3066810236UL, 3447102507UL, 1393639104UL, 373351379UL, 950779232UL, 625454576UL, 3124240540UL, 4148612726UL, 2007998917UL, 544563296UL, 2244738638UL, 2330496472UL, 2058025392UL, 1291430526UL, 424198748UL, 50039436UL, 29584100UL, 3605783033UL, 2429876329UL, 2791104160UL, 1057563949UL, 3255363231UL, 3075367218UL, 3463963227UL, 1469046755UL, 985887462UL}} ;/* sLb(s,n) allows us to subsript s by byte offsets, which allows us to avoid a subscript scaling.*/#define sub(s,n) *((IntU32 *)((IntP)s+(n)))/* Below is one BlowFish round including the F function */#define bf_round(l,r,n) \ l ^= P[n]; \ r ^= ( (sub(S[0],l>>22 & 0x3fc) + sub(S[1],l>>14 & 0x3fc)) \ ^ sub(S[2],l>>6 & 0x3fc) ) +S[3][l & 0xff] /* This function requires the block to be two 32 bit integers, in whatever endian form the machine uses. On little endian machines use crypt_8bytes() on user data. make_bfkey should call crypt_blockon either endian machine. Pass direction 0 to encrypt, 1 to decrypt.*/static void crypt_block(IntU32 block[2], BFkey_type *bfkey, int direction) { register IntU32 left, right, (*S)[256], *P ; left = block[0] ; right = block[1] ; S = bfkey->sbox ; P = bfkey->p[direction] ; bf_round( left, right, 0 ) ; bf_round( right, left, 1 ) ; bf_round( left, right, 2 ) ; bf_round( right, left, 3 ) ; bf_round( left, right, 4 ) ; bf_round( right, left, 5 ) ; bf_round( left, right, 6 ) ; bf_round( right, left, 7 ) ; bf_round( left, right, 8 ) ; bf_round( right, left, 9 ) ; bf_round( left, right, 10 ) ; bf_round( right, left, 11 ) ; bf_round( left, right, 12 ) ; bf_round( right, left, 13 ) ; bf_round( left, right, 14 ) ; bf_round( right, left, 15 ) ; left = left ^ P[NROUNDS] ; right = right ^ P[NROUNDS+1] ; block[0] = right ; block[1] = left ;}/* The following should be allignment and endian independent. I have not tested it on a little-endian machine. It takes the input block from source, and puts the output in dest. They can be the same. It takes the same direction parameter as crypt_block().*/static void crypt_8bytes(IntU8 *source, IntU8 *dest, BFkey_type *bfkey, int direction){ IntU32 block[2] ; block[0] = source[3] | source[2]<<8 | source[1]<<16 | source[0]<<24 ; block[1] = source[7] | source[6]<<8 | source[5]<<16 | source[4]<<24 ; crypt_block( block, bfkey, direction ) ; dest[0]= block[0]>>24 ; dest[1]= block[0]>>16 & 0xff ; dest[2]= block[0]>>8 & 0xff ; dest[3]= block[0] & 0xff ; dest[4]= block[1]>>24 ; dest[5]= block[1]>>16 & 0xff ; dest[6]= block[1]>> 8 & 0xff ; dest[7]= block[1] & 0xff ;}/* make_bfkey() takes the address of the key data as a char*, and the length of the key in bytes. It generates and returns a pointer to an object of BFkey_type, which can be passed to the crypt functions. It does some simple testing of the init data and crypt routine, and returns 0 on error.*/static void make_bfkey(unsigned char *key_string, int keylength, BFkey_type *bfkey){ int i, j, k ; IntU32 dspace[2], checksum=0 ; /* Copy constant initial data to P vector */ for( i=0 ; i<NROUNDS+2 ; ++i ) { bfkey->p[0][i] = p_init[i] ; bfkey->p[1][NROUNDS+1-i] = p_init[i] ; checksum = (checksum<<1 | checksum>>31)+p_init[i] ; } /* Copy constant initial data to sboxes */ for( i=0 ; i<4 ; ++i ) for( j=0 ; j<256 ; ++j ) { bfkey->sbox[i][j] = s_init[i][j] ; checksum = ((checksum*13)<<11 | (checksum*13)>>21) + s_init[i][j] ; } /* Test init data. */ if( checksum != 0x55861a61 ) { PyErr_SetString(PyExc_SystemError, "Blowfish: Bad initialization data"); return; } dspace[0] = 0 ; dspace[1] = 0 ; /* Test the crypt_block() routine. */ for( i=0 ; i<10 ; ++i ) crypt_block( dspace, bfkey, 0 ) ; checksum = dspace[0] ; for( i=0 ; i<10 ; ++i ) crypt_block( dspace, bfkey, 1 ) ; if( (checksum!=0xaafe4ebd) || dspace[0] || dspace[1] ) { PyErr_SetString(PyExc_SystemError, "Blowfish: Error in crypt_block routine"); return; } /* Xor key string into encryption key vector */ j = 0 ; for (i=0 ; i<NROUNDS+2 ; ++i) { IntU32 data; data = 0 ; for (k=0 ; k<4 ; ++k ) data = (data << 8) | key_string[j++ % keylength]; (bfkey->p)[0][i] ^= data; } for (i = 0 ; i<NROUNDS+2 ; i+=2) { crypt_block( dspace, bfkey, 0 ) ; bfkey->p[0][i] = dspace[0] ; bfkey->p[1][NROUNDS+1-i] = dspace[0] ; bfkey->p[0][i+1] = dspace[1] ; bfkey->p[1][NROUNDS-i] = dspace[1] ; } for ( i=0 ; i<4 ; ++i ) for ( j=0 ; j<256 ; j+=2 ) { crypt_block( dspace, bfkey, 0 ) ; bfkey->sbox[i][j] = dspace[0] ; bfkey->sbox[i][j+1] = dspace[1] ; }}static voidblock_encrypt(block_state *self, unsigned char *in, unsigned char *out){ crypt_8bytes(in, out, &(self->bfkey), 0);}static voidblock_decrypt(block_state *self, unsigned char *in, unsigned char *out){ crypt_8bytes(in, out, &(self->bfkey), 1);}static void block_init(block_state *self, unsigned char *key, int keylength){ make_bfkey(key, keylength, &(self->bfkey));}#include "block_template.c"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -