sm4_platform.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright 2022 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(__arm__) || defined(__arm) || defined(__aarch64__))
  14. # include "arm_arch.h"
  15. # if __ARM_MAX_ARCH__>=8
  16. extern unsigned int OPENSSL_arm_midr;
  17. static inline int vpsm4_capable(void)
  18. {
  19. return (OPENSSL_armcap_P & ARMV8_CPUID) &&
  20. (MIDR_IS_CPU_MODEL(OPENSSL_arm_midr, ARM_CPU_IMP_ARM, ARM_CPU_PART_V1) ||
  21. MIDR_IS_CPU_MODEL(OPENSSL_arm_midr, ARM_CPU_IMP_ARM, ARM_CPU_PART_N1) ||
  22. MIDR_IS_CPU_MODEL(OPENSSL_arm_midr, HISI_CPU_IMP, HISI_CPU_PART_KP920));
  23. }
  24. # if defined(VPSM4_ASM)
  25. # define VPSM4_CAPABLE vpsm4_capable()
  26. # endif
  27. # define HWSM4_CAPABLE (OPENSSL_armcap_P & ARMV8_SM4)
  28. # define HWSM4_set_encrypt_key sm4_v8_set_encrypt_key
  29. # define HWSM4_set_decrypt_key sm4_v8_set_decrypt_key
  30. # define HWSM4_encrypt sm4_v8_encrypt
  31. # define HWSM4_decrypt sm4_v8_decrypt
  32. # define HWSM4_cbc_encrypt sm4_v8_cbc_encrypt
  33. # define HWSM4_ecb_encrypt sm4_v8_ecb_encrypt
  34. # define HWSM4_ctr32_encrypt_blocks sm4_v8_ctr32_encrypt_blocks
  35. # endif
  36. # endif
  37. # endif /* OPENSSL_CPUID_OBJ */
  38. # if defined(HWSM4_CAPABLE)
  39. int HWSM4_set_encrypt_key(const unsigned char *userKey, SM4_KEY *key);
  40. int HWSM4_set_decrypt_key(const unsigned char *userKey, SM4_KEY *key);
  41. void HWSM4_encrypt(const unsigned char *in, unsigned char *out,
  42. const SM4_KEY *key);
  43. void HWSM4_decrypt(const unsigned char *in, unsigned char *out,
  44. const SM4_KEY *key);
  45. void HWSM4_cbc_encrypt(const unsigned char *in, unsigned char *out,
  46. size_t length, const SM4_KEY *key,
  47. unsigned char *ivec, const int enc);
  48. void HWSM4_ecb_encrypt(const unsigned char *in, unsigned char *out,
  49. size_t length, const SM4_KEY *key,
  50. const int enc);
  51. void HWSM4_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out,
  52. size_t len, const void *key,
  53. const unsigned char ivec[16]);
  54. # endif /* HWSM4_CAPABLE */
  55. #ifdef VPSM4_CAPABLE
  56. int vpsm4_set_encrypt_key(const unsigned char *userKey, SM4_KEY *key);
  57. int vpsm4_set_decrypt_key(const unsigned char *userKey, SM4_KEY *key);
  58. void vpsm4_encrypt(const unsigned char *in, unsigned char *out,
  59. const SM4_KEY *key);
  60. void vpsm4_decrypt(const unsigned char *in, unsigned char *out,
  61. const SM4_KEY *key);
  62. void vpsm4_cbc_encrypt(const unsigned char *in, unsigned char *out,
  63. size_t length, const SM4_KEY *key,
  64. unsigned char *ivec, const int enc);
  65. void vpsm4_ecb_encrypt(const unsigned char *in, unsigned char *out,
  66. size_t length, const SM4_KEY *key,
  67. const int enc);
  68. void vpsm4_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out,
  69. size_t len, const void *key,
  70. const unsigned char ivec[16]);
  71. # endif /* VPSM4_CAPABLE */
  72. #endif /* OSSL_SM4_PLATFORM_H */