5G/4G128-EIA3与128-NIA3算法详解

Posted 从善若水

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了5G/4G128-EIA3与128-NIA3算法详解相关的知识,希望对你有一定的参考价值。

本人就职于国际知名终端厂商,负责modem芯片研发。
在5G早期负责终端数据业务层、核心网相关的开发工作,目前牵头6G算力网络技术标准研究。


博客内容主要围绕:
       5G协议讲解
       算力网络讲解(云计算,边缘计算,端计算)
       高级C语言讲解
       Rust语言讲解

【5G/4G】128-EIA3与128-NIA3算法详解

secu_defs.h

typedef struct {
  uint8_t *key;
  uint32_t key_length;
  uint32_t count;
  uint8_t  bearer;
  uint8_t  direction;
  uint8_t  *message;
  /* length in bits */
  uint32_t  blength;
} stream_cipher_t;

nia3_eia3_stream.c

#include <stdlib.h>
#include <assert.h>
#include "secu_defs.h"
#include <arpa/inet.h>

//这两个函数定义在《ZUC算法源码介绍》博文中
extern void Initialization(uint8_t*,uint8_t*);
extern void GenerateKeystream(uint32_t*,int);

/* ---------------------------- EEA3 ----------------------------------------- */
/* The ZUC algorithm 《ZUC算法源码介绍》*/
void ZUC(uint8_t* k, uint8_t* iv, uint32_t* ks, int len)
{
	/* The initialization of ZUC, see page 17 of ref. [3]*/
	Initialization(k, iv);
	
	/*  The procedure of generating keystream of ZUC, see page 18 of ref. [3]*/
	GenerateKeystream(ks, len);
}

uint32_tGET_WORD(uint32_t* DATA, uint32_t i)
{
	uint32_t WORD, ti;
	
	ti	= i % 32;
	if (ti == 0) {
		WORD = DATA[i/32];
	}
	else {
		WORD = (DATA[i/32]<<ti) | (DATA[i/32+1]>>(32-ti));
	}
	
	return WORD;
}

uint8_t GET_BIT(uint32_t* DATA, uint32_t i)
{
	return (htonl(DATA[i/32]) & (1<<(31-(i%32)))) ? 1 : 0;
}

int nia3_eia3(stream_cipher_t *stream_cipher, uint8_t out[4])
{
	uint32_t *z, N, L, T, i;
	uint8_t IV[16];
	
	IV[0]	= (stream_cipher->count>>24) & 0xFF;
	IV[1]	= (stream_cipher->count>>16) & 0xFF;
	IV[2]	= (stream_cipher->count>>8)  & 0xFF;
	IV[3]	= stream_cipher->count       & 0xFF;
	
	IV[4]	= (stream_cipher->bearer << 3) & 0xF8;
	IV[5]	= IV[6] = IV[7] = 0;
	
	IV[8]	= ((stream_cipher->count>>24) & 0xFF) ^ ((stream_cipher->direction&1)<<7);
	IV[9]	= (stream_cipher->count>>16)  & 0xFF;
	IV[10]	= (stream_cipher->count>>8)   & 0xFF;
	IV[11]	= stream_cipher->count        & 0xFF;
	
	IV[12]	= IV[4];
	IV[13]	= IV[5];
	IV[14]	= IV[6] ^ ((stream_cipher->direction&1)<<7);
	IV[15]	= IV[7];
	
	N	= stream_cipher->blength + 64;
	L	= (N + 31) / 32;
	z	= (uint32_t *) malloc(L*sizeof(uint32_t));
	assert(z!=NULL);
	ZUC(stream_cipher->key, IV, z, L);
	
	T = 0;
	for (i=0; i<stream_cipher->blength; i++) {
		if (GET_BIT((uint32_t*)(stream_cipher->message),i)) {
			T ^= GET_WORD(z,i);
		}
	}
	T ^= GET_WORD(z,stream_cipher->blength);
	
	*((uint32_t*)out) = T ^ z[L-1];
	free(z);
	return 0;
}

《Snow 3G算法源码介绍》
《128-bit AES算法源码介绍》
《ZUC算法源码介绍》

【5G/4G】128-EEA1与128-NEA1算法详解
【5G/4G】128-EEA2与128-NEA2算法详解
【5G/4G】128-EEA3与128-NEA3算法详解

【5G/4G】128-EIA1与128-NIA1算法详解
【5G/4G】128-EIA2与128-NIA2算法详解
【5G/4G】128-EIA3与128-NIA3算法详解

【5G安全系列】加解密+完整性保护安全算法测试cases


以上是关于5G/4G128-EIA3与128-NIA3算法详解的主要内容,如果未能解决你的问题,请参考以下文章

5G/4G128-EIA1与128-NIA1算法详解

5G/4G128-EEA3与128-NEA3算法详解

5G/4G128-EEA2与128-NEA2算法详解

5G/4G128-EEA3与128-NEA3算法详解

5G/4G128-EEA2与128-NEA2算法详解

5G/4G128-EIA1与128-NIA1算法详解