当前位置:   article > 正文

【5G/4G】128-EIA2与128-NIA2算法详解_128 nia2

128 nia2

博主未授权任何人或组织机构转载博主任何原创文章,感谢各位对原创的支持!
博主链接

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


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

【5G/4G】128-EIA2与128-NIA2算法详解

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;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

nia2_eia2_stream.c

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

#include <openssl/aes.h>
#include <openssl/cmac.h>
#include <openssl/evp.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/bio.h>

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

/*!
 * @brief Create integrity cmac t for a given message.
 * @param[in] stream_cipher Structure containing various variables to setup encoding
 * @param[out] out For EIA2 the output string is 32 bits long
 */
int nia2_eia2(stream_cipher_t*stream_cipher, uint8_t out[4])
{
  uint8_t          *m            = NULL;
  uint32_t          local_count;
  size_t            size         = 4;
  uint8_t           data[16];
  CMAC_CTX         *cmac_ctx     = NULL;
  const EVP_CIPHER *cipher       = EVP_aes_128_cbc();
  uint32_t          zero_bit     = 0;
  uint32_t          m_length;
  int               ret;

  assert(stream_cipher != NULL);
  assert(stream_cipher->key != NULL);
  assert(stream_cipher->key_length > 0);
  assert(out != NULL);

  memset(data, 0, 16);

  zero_bit = stream_cipher->blength & 0x7;

  m_length = stream_cipher->blength >> 3;

  if (zero_bit > 0)
    m_length += 1;

  local_count = hton_int32(stream_cipher->count);

  m = calloc(m_length + 8, sizeof(uint8_t));

  memcpy(&m[0], &local_count, 4);
  m[4] = ((stream_cipher->bearer & 0x1F) << 3) | ((stream_cipher->direction & 0x01) << 2);

  memcpy(&m[8], stream_cipher->message, m_length);
  cmac_ctx = CMAC_CTX_new();
  ret = CMAC_Init(cmac_ctx, stream_cipher->key, stream_cipher->key_length, cipher, NULL);
  ret = CMAC_Update(cmac_ctx, m, m_length + 8);
  (void)ret; /* avoid gcc warning "set but not used" */
  CMAC_Final(cmac_ctx, data, &size);
  CMAC_CTX_free(cmac_ctx);
  memcpy(out, data, 4);
  free(m);

  return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66

《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


在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/51946
推荐阅读
相关标签
  

闽ICP备14008679号