zuc.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the License); you may
  5. * not use this file except in compliance with the License.
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. */
  9. #ifndef GMSSL_ZUC_H
  10. #define GMSSL_ZUC_H
  11. #include <stdlib.h>
  12. #include <stdint.h>
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. /*
  17. ZUC Public API
  18. ZUC_KEY_SIZE
  19. ZUC_IV_SIZE
  20. ZUC_MAC_SIZE
  21. ZUC_CTX
  22. zuc_encrypt_init
  23. zuc_encrypt_update
  24. zuc_encrypt_finish
  25. zuc_decrypt_init
  26. zuc_decrypt_update
  27. zuc_decrypt_finish
  28. ZUC_MAC_CTX
  29. zuc_mac_init
  30. zuc_mac_update
  31. zuc_mac_finish
  32. zuc_eea_encrypt
  33. zuc_eia_generate_mac
  34. */
  35. # define ZUC_KEY_SIZE 16
  36. # define ZUC_IV_SIZE 16
  37. # define ZUC_MAC_SIZE 4
  38. typedef uint32_t ZUC_BIT;
  39. typedef uint32_t ZUC_UINT5;
  40. typedef uint8_t ZUC_UINT6;
  41. typedef uint32_t ZUC_UINT15;
  42. typedef uint32_t ZUC_UINT31;
  43. typedef uint32_t ZUC_UINT32;
  44. typedef struct {
  45. ZUC_UINT31 LFSR[16];
  46. ZUC_UINT32 R1;
  47. ZUC_UINT32 R2;
  48. } ZUC_STATE;
  49. void zuc_init(ZUC_STATE *state, const uint8_t key[ZUC_KEY_SIZE], const uint8_t iv[ZUC_IV_SIZE]);
  50. void zuc_generate_keystream(ZUC_STATE *state, size_t nwords, ZUC_UINT32 *words);
  51. ZUC_UINT32 zuc_generate_keyword(ZUC_STATE *state);
  52. void zuc_encrypt(ZUC_STATE *state, const uint8_t *in, size_t inlen, uint8_t *out);
  53. typedef struct ZUC_MAC_CTX_st {
  54. ZUC_UINT31 LFSR[16];
  55. ZUC_UINT32 R1;
  56. ZUC_UINT32 R2;
  57. ZUC_UINT32 T;
  58. ZUC_UINT32 K0;
  59. uint8_t buf[4];
  60. size_t buflen;
  61. } ZUC_MAC_CTX;
  62. void zuc_mac_init(ZUC_MAC_CTX *ctx, const uint8_t key[ZUC_KEY_SIZE], const uint8_t iv[ZUC_IV_SIZE]);
  63. void zuc_mac_update(ZUC_MAC_CTX *ctx, const uint8_t *data, size_t len);
  64. void zuc_mac_finish(ZUC_MAC_CTX *ctx, const uint8_t *data, size_t nbits, uint8_t mac[ZUC_MAC_SIZE]);
  65. #define ZUC_EEA_ENCRYPT_NWORDS(nbits) ((nbits + 31)/32)
  66. #define ZUC_EEA_ENCRYPT_NBYTES(nbits) (ZUC_EEA_ENCRYPT_NWORDS(nbits)*4)
  67. void zuc_eea_encrypt(const ZUC_UINT32 *in, ZUC_UINT32 *out, size_t nbits,
  68. const uint8_t key[ZUC_KEY_SIZE], ZUC_UINT32 count, ZUC_UINT5 bearer,
  69. ZUC_BIT direction);
  70. ZUC_UINT32 zuc_eia_generate_mac(const ZUC_UINT32 *data, size_t nbits,
  71. const uint8_t key[ZUC_KEY_SIZE], ZUC_UINT32 count, ZUC_UINT5 bearer,
  72. ZUC_BIT direction);
  73. # define ZUC256_KEY_SIZE 32
  74. # define ZUC256_IV_SIZE 23
  75. # define ZUC256_MAC32_SIZE 4
  76. # define ZUC256_MAC64_SIZE 8
  77. # define ZUC256_MAC128_SIZE 16
  78. # define ZUC256_MIN_MAC_SIZE ZUC256_MAC32_SIZE
  79. # define ZUC256_MAX_MAC_SIZE ZUC256_MAC128_SIZE
  80. typedef ZUC_STATE ZUC256_STATE;
  81. void zuc256_init(ZUC256_STATE *state, const uint8_t key[ZUC256_KEY_SIZE], const uint8_t iv[ZUC256_IV_SIZE]);
  82. #define zuc256_generate_keystream(state,nwords,words) zuc_generate_keystream(state,nwords,words)
  83. #define zuc256_generate_keyword(state) zuc_generate_keyword(state)
  84. typedef struct ZUC256_MAC_CTX_st {
  85. ZUC_UINT31 LFSR[16];
  86. ZUC_UINT32 R1;
  87. ZUC_UINT32 R2;
  88. ZUC_UINT32 T[4];
  89. ZUC_UINT32 K0[4];
  90. uint8_t buf[4];
  91. size_t buflen;
  92. int macbits;
  93. } ZUC256_MAC_CTX;
  94. void zuc256_mac_init(ZUC256_MAC_CTX *ctx, const uint8_t key[ZUC256_KEY_SIZE],
  95. const uint8_t iv[ZUC256_IV_SIZE], int macbits);
  96. void zuc256_mac_update(ZUC256_MAC_CTX *ctx, const uint8_t *data, size_t len);
  97. void zuc256_mac_finish(ZUC256_MAC_CTX *ctx, const uint8_t *data, size_t nbits, uint8_t mac[ZUC_MAC_SIZE]);
  98. // Public API
  99. typedef struct {
  100. ZUC_STATE zuc_state;
  101. uint8_t block[4];
  102. size_t block_nbytes;
  103. } ZUC_CTX;
  104. int zuc_encrypt_init(ZUC_CTX *ctx, const uint8_t key[ZUC_KEY_SIZE], const uint8_t iv[ZUC_IV_SIZE]);
  105. int zuc_encrypt_update(ZUC_CTX *ctx, const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
  106. int zuc_encrypt_finish(ZUC_CTX *ctx, uint8_t *out, size_t *outlen);
  107. #define zuc_decrypt_init(ctx,key,iv) zuc_encrypt_init(ctx,key,iv)
  108. #define zuc_decrypt_update(ctx,in,inlen,out,outlen) zuc_encrypt_update(ctx,in,inlen,out,outlen)
  109. #define zuc_decrypt_finish(ctx,out,outlen) zuc_encrypt_finish(ctx,out,outlen)
  110. #ifdef __cplusplus
  111. }
  112. #endif
  113. #endif