C学习 - Kasumi F8/F9算法实现

Linux上的可运行代码,废话不说,直接上C源码

1. f8.c

/*-------------------------------------------------------------------
 *				F8 - Confidentiality Algorithm
 *-------------------------------------------------------------------
 *
 *	A sample implementation of f8, the 3GPP Confidentiality algorithm.
 *
 *	This has been coded for clarity, not necessarily for efficiency.
 *
 *	This will compile and run correctly on both Intel (little endian)
 *  and Sparc (big endian) machines. (Compilers used supported 32-bit ints)
 *
 *	Version 1.0		05 November  1999
 *
 *-------------------------------------------------------------------*/

#include "kasumi.h"
#include <stdio.h>

/*---------------------------------------------------------
 * f8()
 *		Given key, count, bearer, direction,  data,
 *		and bit length  encrypt the bit stream
 *---------------------------------------------------------*/
void f8( u8 *key, int count, int bearer, int dir, u8 *data, int length )
{
	REGISTER64 A;		/* the modifier			*/
	REGISTER64 temp;		/* The working register	*/
	int i, n;
	u8  ModKey[16];		/* Modified key		*/
	u16 blkcnt;			/* The block counter */

	/* Start by building our global modifier */

	temp.b32[0]  = temp.b32[1]  = 0;
	A.b32[0]     = A.b32[1]     = 0;

	/* initialise register in an endian correct manner*/

	A.b8[0]  = (u8) (count>>24);
	A.b8[1]  = (u8) (count>>16);
	A.b8[2]  = (u8) (count>>8);
	A.b8[3]  = (u8) (count);
	A.b8[4]  = (u8) (bearer<<3);
	A.b8[4] |= (u8) (dir<<2);

	/* Construct the modified key and then "kasumi" A */

	for( n=0; n<16; ++n )
		ModKey[n] = (u8)(key[n] ^ 0x55);
	KeySchedule( ModKey );

	Kasumi( A.b8 );	/* First encryption to create modifier */

	/* Final initialisation steps */

	blkcnt = 0;
	KeySchedule( key );

	/* Now run the block cipher */

	while( length > 0 )
	{
		/* First we calculate the next 64-bits of keystream */

		/* XOR in A and BLKCNT to last value */

		temp.b32[0] ^= A.b32[0];
		temp.b32[1] ^= A.b32[1];
		temp.b8[7] ^= (u8)  blkcnt;
		temp.b8[6] ^= (u8) (blkcnt>>8);

		/* KASUMI it to produce the next block of keystream */

		Kasumi( temp.b8 );

		/* Set <n> to the number of bytes of input data	*
		 * we have to modify.  (=8 if length <= 64)		*/

		if( length >= 64 )
			n = 8;
		else
			n = (length+7)/8;

		/* XOR the keystream with the input data stream */

		for( i=0; i<n; ++i )
			*data++ ^= temp.b8[i];
		length -= 64;	/* done another 64 bits	*/
		++blkcnt;		/* increment BLKCNT		*/
	}
}

/*-----------------------------------------------------------
 *			e n d    o f    f 8 . c
 *-----------------------------------------------------------*/

2. f9.c

/*-------------------------------------------------------------------
 *				F9 - Integrity Algorithm
 *-------------------------------------------------------------------
 *
 *	A sample implementation of f9, the 3GPP Integrity algorithm.
 *
 *	This has been coded for clarity, not necessarily for efficiency.
 *
 *	This will compile and run correctly on both Intel (little endian)
 *  and Sparc (big endian) machines. (Compilers used supported 32-bit ints)
 *
 *	Version 1.1		05 September  2000
 *
 *-------------------------------------------------------------------*/

#include "kasumi.h"
#include <stdio.h>

/*---------------------------------------------------------
 * f9()
 *		Given key, count, fresh, direction, data,
 
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值