📄 blowfish.c
字号:
0x1948C25CUL, 0x02FB8A8CUL, 0x01C36AE4UL, 0xD6EBE1F9UL, 0x90D4F869UL, 0xA65CDEA0UL, 0x3F09252DUL, 0xC208E69FUL, 0xB74E6132UL, 0xCE77E25BUL, 0x578FDFE3UL, 0x3AC372E6UL }}; /** Initialize the Blowfish block cipher @param key The symmetric key you wish to pass @param keylen The key length in bytes @param num_rounds The number of rounds desired (0 for default) @param skey The key in as scheduled by this function. @return CRYPT_OK if successful */int blowfish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey){ ulong32 x, y, z, A; unsigned char B[8]; LTC_ARGCHK(key != NULL); LTC_ARGCHK(skey != NULL); /* check key length */ if (keylen < 8 || keylen > 56) { return CRYPT_INVALID_KEYSIZE; } /* check rounds */ if (num_rounds != 0 && num_rounds != 16) { return CRYPT_INVALID_ROUNDS; } /* load in key bytes (Supplied by David Hopwood) */ for (x = y = 0; x < 18; x++) { A = 0; for (z = 0; z < 4; z++) { A = (A << 8) | ((ulong32)key[y++] & 255); if (y == (ulong32)keylen) { y = 0; } } skey->blowfish.K[x] = ORIG_P[x] ^ A; } /* copy sboxes */ for (x = 0; x < 4; x++) { for (y = 0; y < 256; y++) { skey->blowfish.S[x][y] = ORIG_S[x][y]; } } /* encrypt K array */ for (x = 0; x < 8; x++) { B[x] = 0; } for (x = 0; x < 18; x += 2) { /* encrypt it */ blowfish_ecb_encrypt(B, B, skey); /* copy it */ LOAD32H(skey->blowfish.K[x], &B[0]); LOAD32H(skey->blowfish.K[x+1], &B[4]); } /* encrypt S array */ for (x = 0; x < 4; x++) { for (y = 0; y < 256; y += 2) { /* encrypt it */ blowfish_ecb_encrypt(B, B, skey); /* copy it */ LOAD32H(skey->blowfish.S[x][y], &B[0]); LOAD32H(skey->blowfish.S[x][y+1], &B[4]); } }#ifdef LTC_CLEAN_STACK zeromem(B, sizeof(B));#endif return CRYPT_OK;}#ifndef __GNUC__#define F(x) ((S1[byte(x,3)] + S2[byte(x,2)]) ^ S3[byte(x,1)]) + S4[byte(x,0)]#else#define F(x) ((skey->blowfish.S[0][byte(x,3)] + skey->blowfish.S[1][byte(x,2)]) ^ skey->blowfish.S[2][byte(x,1)]) + skey->blowfish.S[3][byte(x,0)]#endif/** Encrypts a block of text with Blowfish @param pt The input plaintext (8 bytes) @param ct The output ciphertext (8 bytes) @param skey The key as scheduled*/#ifdef LTC_CLEAN_STACKstatic void _blowfish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)#elsevoid blowfish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)#endif{ ulong32 L, R; int r;#ifndef __GNUC__ ulong32 *S1, *S2, *S3, *S4;#endif LTC_ARGCHK(pt != NULL); LTC_ARGCHK(ct != NULL); LTC_ARGCHK(skey != NULL);#ifndef __GNUC__ S1 = skey->blowfish.S[0]; S2 = skey->blowfish.S[1]; S3 = skey->blowfish.S[2]; S4 = skey->blowfish.S[3];#endif /* load it */ LOAD32H(L, &pt[0]); LOAD32H(R, &pt[4]); /* do 16 rounds */ for (r = 0; r < 16; ) { L ^= skey->blowfish.K[r++]; R ^= F(L); R ^= skey->blowfish.K[r++]; L ^= F(R); L ^= skey->blowfish.K[r++]; R ^= F(L); R ^= skey->blowfish.K[r++]; L ^= F(R); } /* last keying */ R ^= skey->blowfish.K[17]; L ^= skey->blowfish.K[16]; /* store */ STORE32H(R, &ct[0]); STORE32H(L, &ct[4]);}#ifdef LTC_CLEAN_STACKvoid blowfish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey){ _blowfish_ecb_encrypt(pt, ct, skey); burn_stack(sizeof(ulong32) * 2 + sizeof(int));}#endif/** Decrypts a block of text with Blowfish @param ct The input ciphertext (8 bytes) @param pt The output plaintext (8 bytes) @param skey The key as scheduled */#ifdef LTC_CLEAN_STACKstatic void _blowfish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)#elsevoid blowfish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)#endif{ ulong32 L, R; int r;#ifndef __GNUC__ ulong32 *S1, *S2, *S3, *S4;#endif LTC_ARGCHK(pt != NULL); LTC_ARGCHK(ct != NULL); LTC_ARGCHK(skey != NULL); #ifndef __GNUC__ S1 = skey->blowfish.S[0]; S2 = skey->blowfish.S[1]; S3 = skey->blowfish.S[2]; S4 = skey->blowfish.S[3];#endif /* load it */ LOAD32H(R, &ct[0]); LOAD32H(L, &ct[4]); /* undo last keying */ R ^= skey->blowfish.K[17]; L ^= skey->blowfish.K[16]; /* do 16 rounds */ for (r = 15; r > 0; ) { L ^= F(R); R ^= skey->blowfish.K[r--]; R ^= F(L); L ^= skey->blowfish.K[r--]; L ^= F(R); R ^= skey->blowfish.K[r--]; R ^= F(L); L ^= skey->blowfish.K[r--]; } /* store */ STORE32H(L, &pt[0]); STORE32H(R, &pt[4]);}#ifdef LTC_CLEAN_STACKvoid blowfish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey){ _blowfish_ecb_decrypt(ct, pt, skey); burn_stack(sizeof(ulong32) * 2 + sizeof(int));}#endif/** Performs a self-test of the Blowfish block cipher @return CRYPT_OK if functional, CRYPT_NOP if self-test has been disabled*/int blowfish_test(void){ #ifndef LTC_TEST return CRYPT_NOP; #else int err; symmetric_key key; static const struct { unsigned char key[8], pt[8], ct[8]; } tests[] = { { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, { 0x4E, 0xF9, 0x97, 0x45, 0x61, 0x98, 0xDD, 0x78} }, { { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, { 0x51, 0x86, 0x6F, 0xD5, 0xB8, 0x5E, 0xCB, 0x8A} }, { { 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, { 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}, { 0x7D, 0x85, 0x6F, 0x9A, 0x61, 0x30, 0x63, 0xF2} } }; unsigned char tmp[2][8]; int x, y; for (x = 0; x < (int)(sizeof(tests) / sizeof(tests[0])); x++) { /* setup key */ if ((err = blowfish_setup(tests[x].key, 8, 16, &key)) != CRYPT_OK) { return err; } /* encrypt and decrypt */ blowfish_ecb_encrypt(tests[x].pt, tmp[0], &key); blowfish_ecb_decrypt(tmp[0], tmp[1], &key); /* compare */ if ((memcmp(tmp[0], tests[x].ct, 8) != 0) || (memcmp(tmp[1], tests[x].pt, 8) != 0)) { return CRYPT_FAIL_TESTVECTOR; } /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */ for (y = 0; y < 8; y++) tmp[0][y] = 0; for (y = 0; y < 1000; y++) blowfish_ecb_encrypt(tmp[0], tmp[0], &key); for (y = 0; y < 1000; y++) blowfish_ecb_decrypt(tmp[0], tmp[0], &key); for (y = 0; y < 8; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR; } return CRYPT_OK; #endif}/** Terminate the context @param skey The scheduled key*/void blowfish_done(symmetric_key *skey){}/** Gets suitable key size @param keysize [in/out] The length of the recommended key (in bytes). This function will store the suitable size back in this variable. @return CRYPT_OK if the input key size is acceptable.*/int blowfish_keysize(int *keysize){ LTC_ARGCHK(keysize != NULL); if (*keysize < 8) { return CRYPT_INVALID_KEYSIZE; } else if (*keysize > 56) { *keysize = 56; } return CRYPT_OK;}#endif/* $Source: /cvs/libtom/libtomcrypt/src/ciphers/blowfish.c,v $ *//* $Revision: 1.7 $ *//* $Date: 2005/05/05 14:35:58 $ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -