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

Posted 从善若水

tags:

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

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


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

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

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;

conversions.h

/* Endianness conversions for 16 and 32 bits integers from host to network order */
#if (BYTE_ORDER == LITTLE_ENDIAN)
# define hton_int32(x)   \\
    (((x & 0x000000FF) << 24) | ((x & 0x0000FF00) << 8) |  \\
    ((x & 0x00FF0000) >> 8) | ((x & 0xFF000000) >> 24))

# define hton_int16(x)   \\
    (((x & 0x00FF) << 8) | ((x & 0xFF00) >> 8)

# define ntoh_int32_buf(bUF)        \\
    ((*(bUF)) << 24) | ((*((bUF) + 1)) << 16) | ((*((bUF) + 2)) << 8)   \\
  | (*((bUF) + 3))
#else
# define hton_int32(x) (x)
# define hton_int16(x) (x)
#endif

nea2_eea2_stream.c

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>

#include <nettle/nettle-meta.h>
#include <nettle/aes.h>
#include <nettle/ctr.h>

#include "assert.h"
#include "conversions.h"
#include "secu_defs.h"

// #define SECU_DEBUG

int nea2_eea2(stream_cipher_t*stream_cipher, uint8_t *out)
{
  uint8_t m[16];
  uint32_t local_count;

  void *ctx;
  uint8_t *data;

  uint32_t zero_bit = 0;
  uint32_t byte_length;

  assert(stream_cipher != NULL);
  assert(out != NULL);

  zero_bit = stream_cipher->blength & 0x7;

  byte_length = stream_cipher->blength >> 3;

  if (zero_bit > 0)
    byte_length += 1;

  ctx = malloc(nettle_aes128.context_size);
  data = malloc(byte_length);

  local_count = hton_int32(stream_cipher->count);

  memset(m, 0, sizeof(m));
  memcpy(&m[0], &local_count, 4);
  m[4] = ((stream_cipher->bearer & 0x1F) << 3) |
         ((stream_cipher->direction & 0x01) << 2);
  /* Other bits are 0 */

#if NETTLE_VERSION_MAJOR < 3
  nettle_aes128.set_encrypt_key(ctx, stream_cipher->key_length,
                                stream_cipher->key);
#else
  nettle_aes128.set_encrypt_key(ctx,
                                stream_cipher->key);
#endif

  nettle_ctr_crypt(ctx, nettle_aes128.encrypt,
                   nettle_aes128.block_size, m,
                   byte_length, data, stream_cipher->message);

  if (zero_bit > 0)
    data[byte_length - 1] = data[byte_length - 1] & (uint8_t)(0xFF << (8 - zero_bit));


  memcpy(out, data, byte_length);
  free(data);
  free(ctx);

  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-EEA2与128-NEA2算法详解的主要内容,如果未能解决你的问题,请参考以下文章

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

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

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

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

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

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