sm3_locl.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright 2017 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 sm3_update
  18. #define HASH_TRANSFORM sm3_transform
  19. #define HASH_FINAL 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. #define HASH_BLOCK_DATA_ORDER sm3_block_data_order
  33. void sm3_transform(SM3_CTX *c, const unsigned char *data);
  34. #include "internal/md32_common.h"
  35. #define P0(X) (X ^ ROTATE(X, 9) ^ ROTATE(X, 17))
  36. #define P1(X) (X ^ ROTATE(X, 15) ^ ROTATE(X, 23))
  37. #define FF0(X,Y,Z) (X ^ Y ^ Z)
  38. #define GG0(X,Y,Z) (X ^ Y ^ Z)
  39. #define FF1(X,Y,Z) ((X & Y) | ((X | Y) & Z))
  40. #define GG1(X,Y,Z) ((Z ^ (X & (Y ^ Z))))
  41. #define EXPAND(W0,W7,W13,W3,W10) \
  42. (P1(W0 ^ W7 ^ ROTATE(W13, 15)) ^ ROTATE(W3, 7) ^ W10)
  43. #define RND(A, B, C, D, E, F, G, H, TJ, Wi, Wj, FF, GG) \
  44. do { \
  45. const SM3_WORD A12 = ROTATE(A, 12); \
  46. const SM3_WORD A12_SM = A12 + E + TJ; \
  47. const SM3_WORD SS1 = ROTATE(A12_SM, 7); \
  48. const SM3_WORD TT1 = FF(A, B, C) + D + (SS1 ^ A12) + (Wj); \
  49. const SM3_WORD TT2 = GG(E, F, G) + H + SS1 + Wi; \
  50. B = ROTATE(B, 9); \
  51. D = TT1; \
  52. F = ROTATE(F, 19); \
  53. H = P0(TT2); \
  54. } while(0)
  55. #define R1(A,B,C,D,E,F,G,H,TJ,Wi,Wj) \
  56. RND(A,B,C,D,E,F,G,H,TJ,Wi,Wj,FF0,GG0)
  57. #define R2(A,B,C,D,E,F,G,H,TJ,Wi,Wj) \
  58. RND(A,B,C,D,E,F,G,H,TJ,Wi,Wj,FF1,GG1)
  59. #define SM3_A 0x7380166fUL
  60. #define SM3_B 0x4914b2b9UL
  61. #define SM3_C 0x172442d7UL
  62. #define SM3_D 0xda8a0600UL
  63. #define SM3_E 0xa96f30bcUL
  64. #define SM3_F 0x163138aaUL
  65. #define SM3_G 0xe38dee4dUL
  66. #define SM3_H 0xb0fb0e4eUL