2
0

e_sm4.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 OpenSSL license (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 "internal/cryptlib.h"
  12. #ifndef OPENSSL_NO_SM4
  13. # include <openssl/evp.h>
  14. # include <openssl/modes.h>
  15. # include "internal/sm4.h"
  16. # include "internal/evp_int.h"
  17. typedef struct {
  18. SM4_KEY ks;
  19. } EVP_SM4_KEY;
  20. static int sm4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  21. const unsigned char *iv, int enc)
  22. {
  23. SM4_set_key(key, EVP_CIPHER_CTX_get_cipher_data(ctx));
  24. return 1;
  25. }
  26. static void sm4_cbc_encrypt(const unsigned char *in, unsigned char *out,
  27. size_t len, const SM4_KEY *key,
  28. unsigned char *ivec, const int enc)
  29. {
  30. if (enc)
  31. CRYPTO_cbc128_encrypt(in, out, len, key, ivec,
  32. (block128_f)SM4_encrypt);
  33. else
  34. CRYPTO_cbc128_decrypt(in, out, len, key, ivec,
  35. (block128_f)SM4_decrypt);
  36. }
  37. static void sm4_cfb128_encrypt(const unsigned char *in, unsigned char *out,
  38. size_t length, const SM4_KEY *key,
  39. unsigned char *ivec, int *num, const int enc)
  40. {
  41. CRYPTO_cfb128_encrypt(in, out, length, key, ivec, num, enc,
  42. (block128_f)SM4_encrypt);
  43. }
  44. static void sm4_ecb_encrypt(const unsigned char *in, unsigned char *out,
  45. const SM4_KEY *key, const int enc)
  46. {
  47. if (enc)
  48. SM4_encrypt(in, out, key);
  49. else
  50. SM4_decrypt(in, out, key);
  51. }
  52. static void sm4_ofb128_encrypt(const unsigned char *in, unsigned char *out,
  53. size_t length, const SM4_KEY *key,
  54. unsigned char *ivec, int *num)
  55. {
  56. CRYPTO_ofb128_encrypt(in, out, length, key, ivec, num,
  57. (block128_f)SM4_encrypt);
  58. }
  59. IMPLEMENT_BLOCK_CIPHER(sm4, ks, sm4, EVP_SM4_KEY, NID_sm4,
  60. 16, 16, 16, 128, EVP_CIPH_FLAG_DEFAULT_ASN1,
  61. sm4_init_key, 0, 0, 0, 0)
  62. static int sm4_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  63. const unsigned char *in, size_t len)
  64. {
  65. unsigned int num = EVP_CIPHER_CTX_num(ctx);
  66. EVP_SM4_KEY *dat = EVP_C_DATA(EVP_SM4_KEY, ctx);
  67. CRYPTO_ctr128_encrypt(in, out, len, &dat->ks,
  68. EVP_CIPHER_CTX_iv_noconst(ctx),
  69. EVP_CIPHER_CTX_buf_noconst(ctx), &num,
  70. (block128_f)SM4_encrypt);
  71. EVP_CIPHER_CTX_set_num(ctx, num);
  72. return 1;
  73. }
  74. static const EVP_CIPHER sm4_ctr_mode = {
  75. NID_sm4_ctr, 1, 16, 16,
  76. EVP_CIPH_CTR_MODE,
  77. sm4_init_key,
  78. sm4_ctr_cipher,
  79. NULL,
  80. sizeof(EVP_SM4_KEY),
  81. NULL, NULL, NULL, NULL
  82. };
  83. const EVP_CIPHER *EVP_sm4_ctr(void)
  84. {
  85. return &sm4_ctr_mode;
  86. }
  87. #endif