sm4_platform.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #ifndef OSSL_SM4_PLATFORM_H
  10. # define OSSL_SM4_PLATFORM_H
  11. # pragma once
  12. # if defined(OPENSSL_CPUID_OBJ)
  13. # if defined(__aarch64__) || defined (_M_ARM64)
  14. # include "arm_arch.h"
  15. extern unsigned int OPENSSL_arm_midr;
  16. static inline int vpsm4_capable(void)
  17. {
  18. return (OPENSSL_armcap_P & ARMV8_CPUID) &&
  19. (MIDR_IS_CPU_MODEL(OPENSSL_arm_midr, ARM_CPU_IMP_ARM, ARM_CPU_PART_V1) ||
  20. MIDR_IS_CPU_MODEL(OPENSSL_arm_midr, ARM_CPU_IMP_ARM, ARM_CPU_PART_N1));
  21. }
  22. static inline int vpsm4_ex_capable(void)
  23. {
  24. return (OPENSSL_armcap_P & ARMV8_CPUID) &&
  25. (MIDR_IS_CPU_MODEL(OPENSSL_arm_midr, HISI_CPU_IMP, HISI_CPU_PART_KP920));
  26. }
  27. # if defined(VPSM4_ASM)
  28. # define VPSM4_CAPABLE vpsm4_capable()
  29. # define VPSM4_EX_CAPABLE vpsm4_ex_capable()
  30. # endif
  31. # define HWSM4_CAPABLE (OPENSSL_armcap_P & ARMV8_SM4)
  32. # define HWSM4_set_encrypt_key sm4_v8_set_encrypt_key
  33. # define HWSM4_set_decrypt_key sm4_v8_set_decrypt_key
  34. # define HWSM4_encrypt sm4_v8_encrypt
  35. # define HWSM4_decrypt sm4_v8_decrypt
  36. # define HWSM4_cbc_encrypt sm4_v8_cbc_encrypt
  37. # define HWSM4_ecb_encrypt sm4_v8_ecb_encrypt
  38. # define HWSM4_ctr32_encrypt_blocks sm4_v8_ctr32_encrypt_blocks
  39. # elif defined(__riscv) && __riscv_xlen == 64
  40. /* RV64 support */
  41. # include "riscv_arch.h"
  42. /* Zvksed extension (vector crypto SM4). */
  43. int rv64i_zvksed_sm4_set_encrypt_key(const unsigned char *userKey,
  44. SM4_KEY *key);
  45. int rv64i_zvksed_sm4_set_decrypt_key(const unsigned char *userKey,
  46. SM4_KEY *key);
  47. void rv64i_zvksed_sm4_encrypt(const unsigned char *in, unsigned char *out,
  48. const SM4_KEY *key);
  49. void rv64i_zvksed_sm4_decrypt(const unsigned char *in, unsigned char *out,
  50. const SM4_KEY *key);
  51. # endif /* RV64 */
  52. # endif /* OPENSSL_CPUID_OBJ */
  53. # if defined(HWSM4_CAPABLE)
  54. int HWSM4_set_encrypt_key(const unsigned char *userKey, SM4_KEY *key);
  55. int HWSM4_set_decrypt_key(const unsigned char *userKey, SM4_KEY *key);
  56. void HWSM4_encrypt(const unsigned char *in, unsigned char *out,
  57. const SM4_KEY *key);
  58. void HWSM4_decrypt(const unsigned char *in, unsigned char *out,
  59. const SM4_KEY *key);
  60. void HWSM4_cbc_encrypt(const unsigned char *in, unsigned char *out,
  61. size_t length, const SM4_KEY *key,
  62. unsigned char *ivec, const int enc);
  63. void HWSM4_ecb_encrypt(const unsigned char *in, unsigned char *out,
  64. size_t length, const SM4_KEY *key,
  65. const int enc);
  66. void HWSM4_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out,
  67. size_t len, const void *key,
  68. const unsigned char ivec[16]);
  69. # endif /* HWSM4_CAPABLE */
  70. # ifdef VPSM4_CAPABLE
  71. int vpsm4_set_encrypt_key(const unsigned char *userKey, SM4_KEY *key);
  72. int vpsm4_set_decrypt_key(const unsigned char *userKey, SM4_KEY *key);
  73. void vpsm4_encrypt(const unsigned char *in, unsigned char *out,
  74. const SM4_KEY *key);
  75. void vpsm4_decrypt(const unsigned char *in, unsigned char *out,
  76. const SM4_KEY *key);
  77. void vpsm4_cbc_encrypt(const unsigned char *in, unsigned char *out,
  78. size_t length, const SM4_KEY *key,
  79. unsigned char *ivec, const int enc);
  80. void vpsm4_ecb_encrypt(const unsigned char *in, unsigned char *out,
  81. size_t length, const SM4_KEY *key,
  82. const int enc);
  83. void vpsm4_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out,
  84. size_t len, const void *key,
  85. const unsigned char ivec[16]);
  86. void vpsm4_xts_encrypt(const unsigned char *in, unsigned char *out,
  87. size_t len, const SM4_KEY *key1, const SM4_KEY *key2,
  88. const unsigned char ivec[16], const int enc);
  89. void vpsm4_xts_encrypt_gb(const unsigned char *in, unsigned char *out,
  90. size_t len, const SM4_KEY *key1, const SM4_KEY *key2,
  91. const unsigned char ivec[16], const int enc);
  92. # endif /* VPSM4_CAPABLE */
  93. # ifdef VPSM4_EX_CAPABLE
  94. int vpsm4_ex_set_encrypt_key(const unsigned char *userKey, SM4_KEY *key);
  95. int vpsm4_ex_set_decrypt_key(const unsigned char *userKey, SM4_KEY *key);
  96. void vpsm4_ex_encrypt(const unsigned char *in, unsigned char *out,
  97. const SM4_KEY *key);
  98. void vpsm4_ex_decrypt(const unsigned char *in, unsigned char *out,
  99. const SM4_KEY *key);
  100. void vpsm4_ex_cbc_encrypt(const unsigned char *in, unsigned char *out,
  101. size_t length, const SM4_KEY *key,
  102. unsigned char *ivec, const int enc);
  103. void vpsm4_ex_ecb_encrypt(const unsigned char *in, unsigned char *out,
  104. size_t length, const SM4_KEY *key,
  105. const int enc);
  106. void vpsm4_ex_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out,
  107. size_t len, const void *key,
  108. const unsigned char ivec[16]);
  109. void vpsm4_ex_xts_encrypt(const unsigned char *in, unsigned char *out,
  110. size_t len, const SM4_KEY *key1, const SM4_KEY *key2,
  111. const unsigned char ivec[16], const int enc);
  112. void vpsm4_ex_xts_encrypt_gb(const unsigned char *in, unsigned char *out,
  113. size_t len, const SM4_KEY *key1,
  114. const SM4_KEY *key2, const unsigned char ivec[16],
  115. const int enc);
  116. # endif /* VPSM4_EX_CAPABLE */
  117. #endif /* OSSL_SM4_PLATFORM_H */