/* * Blowfish encryption/decryption for mISDN_dsp. * * Copyright Andreas Eversberg ([email protected]) * * This software may be used and distributed according to the terms * of the GNU General Public License, incorporated herein by reference. * */ #include <linux/mISDNif.h> #include <linux/mISDNdsp.h> #include "core.h" #include "dsp.h" /* * how to encode a sample stream to 64-bit blocks that will be encryped * * first of all, data is collected until a block of 9 samples are received. * of course, a packet may have much more than 9 sample, but is may have * not excacly the multiple of 9 samples. if there is a rest, the next * received data will complete the block. * * the block is then converted to 9 uLAW samples without the least sigificant * bit. the result is a 7-bit encoded sample. * * the samples will be reoganised to form 8 bytes of data: * (5(6) means: encoded sample no. 5, bit 6) * * 0(6) 0(5) 0(4) 0(3) 0(2) 0(1) 0(0) 1(6) * 1(5) 1(4) 1(3) 1(2) 1(1) 1(0) 2(6) 2(5) * 2(4) 2(3) 2(2) 2(1) 2(0) 3(6) 3(5) 3(4) * 3(3) 3(2) 3(1) 3(0) 4(6) 4(5) 4(4) 4(3) * 4(2) 4(1) 4(0) 5(6) 5(5) 5(4) 5(3) 5(2) * 5(1) 5(0) 6(6) 6(5) 6(4) 6(3) 6(2) 6(1) * 6(0) 7(6) 7(5) 7(4) 7(3) 7(2) 7(1) 7(0) * 8(6) 8(5) 8(4) 8(3) 8(2) 8(1) 8(0) * * the missing bit 0 of the last byte is filled with some * random noise, to fill all 8 bytes. * * the 8 bytes will be encrypted using blowfish. * * the result will be converted into 9 bytes. the bit 7 is used for * checksumme (CS) for sync (0, 1) and for the last bit: * (5(6) means: crypted byte 5, bit 6) * * 1 0(7) 0(6) 0(5) 0(4) 0(3) 0(2) 0(1) * 0 0(0) 1(7) 1(6) 1(5) 1(4) 1(3) 1(2) * 0 1(1) 1(0) 2(7) 2(6) 2(5) 2(4) 2(3) * 0 2(2) 2(1) 2(0) 3(7) 3(6) 3(5) 3(4) * 0 3(3) 3(2) 3(1) 3(0) 4(7) 4(6) 4(5) * CS 4(4) 4(3) 4(2) 4(1) 4(0) 5(7) 5(6) * CS 5(5) 5(4) 5(3) 5(2) 5(1) 5(0) 6(7) * CS 6(6) 6(5) 6(4) 6(3) 6(2) 6(1) 6(0) * 7(0) 7(6) 7(5) 7(4) 7(3) 7(2) 7(1) 7(0) * * the checksum is used to detect transmission errors and frame drops. * * synchronisation of received block is done by shifting the upper bit of each * byte (bit 7) to a shift register. if the rigister has the first five bits * (10000), this is used to find the sync. only if sync has been found, the * current block of 9 received bytes are decrypted. before that the check * sum is calculated. if it is incorrect the block is dropped. * this will avoid loud noise due to corrupt encrypted data. * * if the last block is corrupt, the current decoded block is repeated * until a valid block has been received. */ /* * some blowfish parts are taken from the * crypto-api for faster implementation */ static const u32 bf_pbox[16 + 2] = …; static const u32 bf_sbox[256 * 4] = …; /* * 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) … #define GET32_2(x) … #define GET32_1(x) … #define GET32_0(x) … #define bf_F(x) … #define EROUND(a, b, n) … #define DROUND(a, b, n) … /* * encrypt isdn data frame * every block with 9 samples is encrypted */ void dsp_bf_encrypt(struct dsp *dsp, u8 *data, int len) { … } /* * decrypt isdn data frame * every block with 9 bytes is decrypted */ void dsp_bf_decrypt(struct dsp *dsp, u8 *data, int len) { … } /* used to encrypt S and P boxes */ static inline void encrypt_block(const u32 *P, const u32 *S, u32 *dst, u32 *src) { … } /* * initialize the dsp for encryption and decryption using the same key * Calculates the blowfish S and P boxes for encryption and decryption. * The margin of keylen must be 4-56 bytes. * returns 0 if ok. */ int dsp_bf_init(struct dsp *dsp, const u8 *key, uint keylen) { … } /* * turn encryption off */ void dsp_bf_cleanup(struct dsp *dsp) { … }