📄 cipher-blowfish.c
字号:
0xb39a460a, 0x6445c0dd, 0x586cdecf, 0x1c20c8ae, 0x5bbef7dd, 0x1b588d40, 0xccd2017f, 0x6bb4e3bb, 0xdda26a7e, 0x3a59ff45, 0x3e350a44, 0xbcb4cdd5, 0x72eacea8, 0xfa6484bb, 0x8d6612ae, 0xbf3c6f47, 0xd29be463, 0x542f5d9e, 0xaec2771b, 0xf64e6370, 0x740e0d8d, 0xe75b1357, 0xf8721671, 0xaf537d5d, 0x4040cb08, 0x4eb4e2cc, 0x34d2466a, 0x0115af84, 0xe1b00428, 0x95983a1d, 0x06b89fb4, 0xce6ea048, 0x6f3f3b82, 0x3520ab82, 0x011a1d4b, 0x277227f8, 0x611560b1, 0xe7933fdc, 0xbb3a792b, 0x344525bd, 0xa08839e1, 0x51ce794b, 0x2f32c9b7, 0xa01fbac9, 0xe01cc87e, 0xbcc7d1f6, 0xcf0111c3, 0xa1e8aac7, 0x1a908749, 0xd44fbd9a, 0xd0dadecb, 0xd50ada38, 0x0339c32a, 0xc6913667, 0x8df9317c, 0xe0b12b4f, 0xf79e59b7, 0x43f5bb3a, 0xf2d519ff, 0x27d9459c, 0xbf97222c, 0x15e6fc2a, 0x0f91fc71, 0x9b941525, 0xfae59361, 0xceb69ceb, 0xc2a86459, 0x12baa8d1, 0xb6c1075e, 0xe3056a0c, 0x10d25065, 0xcb03a442, 0xe0ec6e0e, 0x1698db3b, 0x4c98a0be, 0x3278e964, 0x9f1f9532, 0xe0d392df, 0xd3a0342b, 0x8971f21e, 0x1b0a7441, 0x4ba3348c, 0xc5be7120, 0xc37632d8, 0xdf359f8d, 0x9b992f2e, 0xe60b6f47, 0x0fe3f11d, 0xe54cda54, 0x1edad891, 0xce6279cf, 0xcd3e7e6f, 0x1618b166, 0xfd2c1d05, 0x848fd2c5, 0xf6fb2299, 0xf523f357, 0xa6327623, 0x93a83531, 0x56cccd02, 0xacf08162, 0x5a75ebb5, 0x6e163697, 0x88d273cc, 0xde966292, 0x81b949d0, 0x4c50901b, 0x71c65614, 0xe6c6c7bd, 0x327a140a, 0x45e1d006, 0xc3f27b9a, 0xc9aa53fd, 0x62a80f00, 0xbb25bfe2, 0x35bdd2f6, 0x71126905, 0xb2040222, 0xb6cbcf7c, 0xcd769c2b, 0x53113ec0, 0x1640e3d3, 0x38abbd60, 0x2547adf0, 0xba38209c, 0xf746ce76, 0x77afa1c5, 0x20756060, 0x85cbfe4e, 0x8ae88dd8, 0x7aaaf9b0, 0x4cf9aa7e, 0x1948c25c, 0x02fb8a8c, 0x01c36ae4, 0xd6ebe1f9, 0x90d4f869, 0xa65cdea0, 0x3f09252d, 0xc208e69f, 0xb74e6132, 0xce77e25b, 0x578fdfe3, 0x3ac372e6,};typedef struct { u32 P[18]; u32 S[1024];} blowfish_key_t;/* * Round loop unrolling macros, S is a pointer to a S-Box array * organized in 4 unsigned longs at a row. */#define GET32_3(x) (((x) & 0xff))#define GET32_2(x) (((x) >> (8)) & (0xff))#define GET32_1(x) (((x) >> (16)) & (0xff))#define GET32_0(x) (((x) >> (24)) & (0xff))#define bf_F(x) (((S[GET32_0(x)] + S[256 + GET32_1(x)]) ^ \ S[512 + GET32_2(x)]) + S[768 + GET32_3(x)])#define ROUND(a, b, n) b ^= P[n]; a ^= bf_F (b)/* * The blowfish encipher, processes 64-bit blocks. * NOTE: This function MUSTN'T respect endianess */inline static void_blowfish_encrypt_block (const u32 in[2], u32 out[2], const blowfish_key_t *key){ const u32 *P = key->P; const u32 *S = key->S; register u32 yl = in[0]; register u32 yr = in[1]; ROUND (yr, yl, 0); ROUND (yl, yr, 1); ROUND (yr, yl, 2); ROUND (yl, yr, 3); ROUND (yr, yl, 4); ROUND (yl, yr, 5); ROUND (yr, yl, 6); ROUND (yl, yr, 7); ROUND (yr, yl, 8); ROUND (yl, yr, 9); ROUND (yr, yl, 10); ROUND (yl, yr, 11); ROUND (yr, yl, 12); ROUND (yl, yr, 13); ROUND (yr, yl, 14); ROUND (yl, yr, 15); /* yl and yr are switched */ yl ^= P[16]; yr ^= P[17]; out[0] = yr; out[1] = yl;}static intblowfish_encrypt (struct cipher_context *cx, const u8 *in8, u8 *out8, int size, int atomic){ const blowfish_key_t *key = (blowfish_key_t *) cx->keyinfo; const u32 *in_blk = (const u32 *) in8; u32 * const out_blk = (u32 *) out8; u32 _in32[2], _out32[2]; if (size != 8) return 1; _in32[0] = be32_to_cpu (in_blk[0]); _in32[1] = be32_to_cpu (in_blk[1]); _blowfish_encrypt_block (_in32, _out32, key); out_blk[0] = cpu_to_be32 (_out32[0]); out_blk[1] = cpu_to_be32 (_out32[1]); return 0;}static intblowfish_decrypt (struct cipher_context *cx, const u8 *in8, u8 *out8, int size, int atomic){ const blowfish_key_t *key = (blowfish_key_t *) cx->keyinfo; const u32 *in_blk = (const u32 *) in8; u32 * const out_blk = (u32 *) out8; const u32 *P = key->P; const u32 *S = key->S; if (size != 8) return 1; { register u32 yl = be32_to_cpu (in_blk[0]); register u32 yr = be32_to_cpu (in_blk[1]); ROUND (yr, yl, 17); ROUND (yl, yr, 16); ROUND (yr, yl, 15); ROUND (yl, yr, 14); ROUND (yr, yl, 13); ROUND (yl, yr, 12); ROUND (yr, yl, 11); ROUND (yl, yr, 10); ROUND (yr, yl, 9); ROUND (yl, yr, 8); ROUND (yr, yl, 7); ROUND (yl, yr, 6); ROUND (yr, yl, 5); ROUND (yl, yr, 4); ROUND (yr, yl, 3); ROUND (yl, yr, 2); /* yl and yr are switched */ yl ^= P[1]; yr ^= P[0]; out_blk[0] = cpu_to_be32 (yr); out_blk[1] = cpu_to_be32 (yl); } return 0;}/* Sets the blowfish S and P boxes for encryption and decryption. */static intblowfish_set_key (struct cipher_context *cx, const u8 *key, int key_len, int atomic){ blowfish_key_t *key2 = (blowfish_key_t *) cx->keyinfo; short i; short j; short count; u32 data[2]; u32 temp; u32 *P = key2->P; u32 *S = key2->S; /* Check key length. */ if (key_len != 8 && key_len != 16 && key_len != 20 && key_len != 24 && key_len != 32) return -EINVAL; /* unsupported key length */ cx->key_length = key_len; /* Copy the initialization s-boxes */ for (i = 0, count = 0; i < 256; i++) for (j = 0; j < 4; j++, count++) S[count] = bf_sbox[count]; /* Set the p-boxes */ for (i = 0; i < 16 + 2; i++) P[i] = bf_pbox[i]; /* Actual subkey generation */ for (j = 0, i = 0; i < 16 + 2; i++) { temp = (((u32) key[j] << 24) | ((u32) key[(j + 1) % key_len] << 16) | ((u32) key[(j + 2) % key_len] << 8) | ((u32) key[(j + 3) % key_len])); P[i] = P[i] ^ temp; j = (j + 4) % key_len; } data[0] = 0x00000000; data[1] = 0x00000000; for (i = 0; i < 16 + 2; i += 2) { _blowfish_encrypt_block (data, data, key2); P[i] = data[0]; P[i + 1] = data[1]; } for (i = 0; i < 4; i++) { for (j = 0, count = i * 256; j < 256; j += 2, count += 2) { _blowfish_encrypt_block (data, data, key2); S[count] = data[0]; S[count + 1] = data[1]; } } return 0;}#define CIPHER_ID blowfish#define CIPHER_BLOCKSIZE 64#define CIPHER_KEY_SIZE_MASK CIPHER_KEYSIZE_128 | CIPHER_KEYSIZE_160 | CIPHER_KEYSIZE_192 | CIPHER_KEYSIZE_256#define CIPHER_KEY_SCHEDULE_SIZE sizeof (blowfish_key_t)#include "gen-cipher.h"EXPORT_NO_SYMBOLS;/* eof */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -