sm4.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Common values for the SM4 algorithm
  4. * Copyright (C) 2018 ARM Limited or its affiliates.
  5. * Copyright (c) 2021 Tianjia Zhang <[email protected]>
  6. */
  7. #ifndef _CRYPTO_SM4_H
  8. #define _CRYPTO_SM4_H
  9. #include <linux/types.h>
  10. #include <linux/crypto.h>
  11. #define SM4_KEY_SIZE 16
  12. #define SM4_BLOCK_SIZE 16
  13. #define SM4_RKEY_WORDS 32
  14. struct sm4_ctx {
  15. u32 rkey_enc[SM4_RKEY_WORDS];
  16. u32 rkey_dec[SM4_RKEY_WORDS];
  17. };
  18. extern const u32 crypto_sm4_fk[];
  19. extern const u32 crypto_sm4_ck[];
  20. extern const u8 crypto_sm4_sbox[];
  21. /**
  22. * sm4_expandkey - Expands the SM4 key as described in GB/T 32907-2016
  23. * @ctx: The location where the computed key will be stored.
  24. * @in_key: The supplied key.
  25. * @key_len: The length of the supplied key.
  26. *
  27. * Returns 0 on success. The function fails only if an invalid key size (or
  28. * pointer) is supplied.
  29. */
  30. int sm4_expandkey(struct sm4_ctx *ctx, const u8 *in_key,
  31. unsigned int key_len);
  32. /**
  33. * sm4_crypt_block - Encrypt or decrypt a single SM4 block
  34. * @rk: The rkey_enc for encrypt or rkey_dec for decrypt
  35. * @out: Buffer to store output data
  36. * @in: Buffer containing the input data
  37. */
  38. void sm4_crypt_block(const u32 *rk, u8 *out, const u8 *in);
  39. #endif