sm3_local.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright 2017-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright 2017 Ribose Inc. All Rights Reserved.
  4. * Ported from Ribose contributions from Botan.
  5. *
  6. * Licensed under the Apache License 2.0 (the "License"). You may not use
  7. * this file except in compliance with the License. You can obtain a copy
  8. * in the file LICENSE in the source distribution or at
  9. * https://www.openssl.org/source/license.html
  10. */
  11. #include <string.h>
  12. #include "internal/sm3.h"
  13. #define DATA_ORDER_IS_BIG_ENDIAN
  14. #define HASH_LONG SM3_WORD
  15. #define HASH_CTX SM3_CTX
  16. #define HASH_CBLOCK SM3_CBLOCK
  17. #define HASH_UPDATE ossl_sm3_update
  18. #define HASH_TRANSFORM ossl_sm3_transform
  19. #define HASH_FINAL ossl_sm3_final
  20. #define HASH_MAKE_STRING(c, s) \
  21. do { \
  22. unsigned long ll; \
  23. ll=(c)->A; (void)HOST_l2c(ll, (s)); \
  24. ll=(c)->B; (void)HOST_l2c(ll, (s)); \
  25. ll=(c)->C; (void)HOST_l2c(ll, (s)); \
  26. ll=(c)->D; (void)HOST_l2c(ll, (s)); \
  27. ll=(c)->E; (void)HOST_l2c(ll, (s)); \
  28. ll=(c)->F; (void)HOST_l2c(ll, (s)); \
  29. ll=(c)->G; (void)HOST_l2c(ll, (s)); \
  30. ll=(c)->H; (void)HOST_l2c(ll, (s)); \
  31. } while (0)
  32. #if defined(OPENSSL_SM3_ASM)
  33. # if defined(__aarch64__)
  34. # include "crypto/arm_arch.h"
  35. # define HWSM3_CAPABLE (OPENSSL_armcap_P & ARMV8_SM3)
  36. void ossl_hwsm3_block_data_order(SM3_CTX *c, const void *p, size_t num);
  37. # endif
  38. #endif
  39. #if defined(HWSM3_CAPABLE)
  40. # define HASH_BLOCK_DATA_ORDER (HWSM3_CAPABLE ? ossl_hwsm3_block_data_order \
  41. : ossl_sm3_block_data_order)
  42. #else
  43. # define HASH_BLOCK_DATA_ORDER ossl_sm3_block_data_order
  44. #endif
  45. void ossl_sm3_block_data_order(SM3_CTX *c, const void *p, size_t num);
  46. void ossl_sm3_transform(SM3_CTX *c, const unsigned char *data);
  47. #include "crypto/md32_common.h"
  48. #ifndef PEDANTIC
  49. # if defined(__GNUC__) && __GNUC__>=2 && \
  50. !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
  51. # if defined(__riscv_zksh)
  52. # define P0(x) ({ MD32_REG_T ret; \
  53. asm ("sm3p0 %0, %1" \
  54. : "=r"(ret) \
  55. : "r"(x)); ret; })
  56. # define P1(x) ({ MD32_REG_T ret; \
  57. asm ("sm3p1 %0, %1" \
  58. : "=r"(ret) \
  59. : "r"(x)); ret; })
  60. # endif
  61. # endif
  62. #endif
  63. #ifndef P0
  64. # define P0(X) (X ^ ROTATE(X, 9) ^ ROTATE(X, 17))
  65. #endif
  66. #ifndef P1
  67. # define P1(X) (X ^ ROTATE(X, 15) ^ ROTATE(X, 23))
  68. #endif
  69. #define FF0(X,Y,Z) (X ^ Y ^ Z)
  70. #define GG0(X,Y,Z) (X ^ Y ^ Z)
  71. #define FF1(X,Y,Z) ((X & Y) | ((X | Y) & Z))
  72. #define GG1(X,Y,Z) ((Z ^ (X & (Y ^ Z))))
  73. #define EXPAND(W0,W7,W13,W3,W10) \
  74. (P1(W0 ^ W7 ^ ROTATE(W13, 15)) ^ ROTATE(W3, 7) ^ W10)
  75. #define RND(A, B, C, D, E, F, G, H, TJ, Wi, Wj, FF, GG) \
  76. do { \
  77. const SM3_WORD A12 = ROTATE(A, 12); \
  78. const SM3_WORD A12_SM = A12 + E + TJ; \
  79. const SM3_WORD SS1 = ROTATE(A12_SM, 7); \
  80. const SM3_WORD TT1 = FF(A, B, C) + D + (SS1 ^ A12) + (Wj); \
  81. const SM3_WORD TT2 = GG(E, F, G) + H + SS1 + Wi; \
  82. B = ROTATE(B, 9); \
  83. D = TT1; \
  84. F = ROTATE(F, 19); \
  85. H = P0(TT2); \
  86. } while(0)
  87. #define R1(A,B,C,D,E,F,G,H,TJ,Wi,Wj) \
  88. RND(A,B,C,D,E,F,G,H,TJ,Wi,Wj,FF0,GG0)
  89. #define R2(A,B,C,D,E,F,G,H,TJ,Wi,Wj) \
  90. RND(A,B,C,D,E,F,G,H,TJ,Wi,Wj,FF1,GG1)
  91. #define SM3_A 0x7380166fUL
  92. #define SM3_B 0x4914b2b9UL
  93. #define SM3_C 0x172442d7UL
  94. #define SM3_D 0xda8a0600UL
  95. #define SM3_E 0xa96f30bcUL
  96. #define SM3_F 0x163138aaUL
  97. #define SM3_G 0xe38dee4dUL
  98. #define SM3_H 0xb0fb0e4eUL