e_sm4.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright 2017-2020 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 "internal/cryptlib.h"
  12. #ifndef OPENSSL_NO_SM4
  13. # include <openssl/evp.h>
  14. # include <openssl/modes.h>
  15. # include "crypto/sm4.h"
  16. # include "crypto/evp.h"
  17. # include "evp_local.h"
  18. typedef struct {
  19. SM4_KEY ks;
  20. } EVP_SM4_KEY;
  21. static int sm4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  22. const unsigned char *iv, int enc)
  23. {
  24. ossl_sm4_set_key(key, EVP_CIPHER_CTX_get_cipher_data(ctx));
  25. return 1;
  26. }
  27. static void sm4_cbc_encrypt(const unsigned char *in, unsigned char *out,
  28. size_t len, const SM4_KEY *key,
  29. unsigned char *ivec, const int enc)
  30. {
  31. if (enc)
  32. CRYPTO_cbc128_encrypt(in, out, len, key, ivec,
  33. (block128_f)ossl_sm4_encrypt);
  34. else
  35. CRYPTO_cbc128_decrypt(in, out, len, key, ivec,
  36. (block128_f)ossl_sm4_decrypt);
  37. }
  38. static void sm4_cfb128_encrypt(const unsigned char *in, unsigned char *out,
  39. size_t length, const SM4_KEY *key,
  40. unsigned char *ivec, int *num, const int enc)
  41. {
  42. CRYPTO_cfb128_encrypt(in, out, length, key, ivec, num, enc,
  43. (block128_f)ossl_sm4_encrypt);
  44. }
  45. static void sm4_ecb_encrypt(const unsigned char *in, unsigned char *out,
  46. const SM4_KEY *key, const int enc)
  47. {
  48. if (enc)
  49. ossl_sm4_encrypt(in, out, key);
  50. else
  51. ossl_sm4_decrypt(in, out, key);
  52. }
  53. static void sm4_ofb128_encrypt(const unsigned char *in, unsigned char *out,
  54. size_t length, const SM4_KEY *key,
  55. unsigned char *ivec, int *num)
  56. {
  57. CRYPTO_ofb128_encrypt(in, out, length, key, ivec, num,
  58. (block128_f)ossl_sm4_encrypt);
  59. }
  60. IMPLEMENT_BLOCK_CIPHER(sm4, ks, sm4, EVP_SM4_KEY, NID_sm4,
  61. 16, 16, 16, 128, EVP_CIPH_FLAG_DEFAULT_ASN1,
  62. sm4_init_key, 0, 0, 0, 0)
  63. static int sm4_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  64. const unsigned char *in, size_t len)
  65. {
  66. unsigned int num = EVP_CIPHER_CTX_num(ctx);
  67. EVP_SM4_KEY *dat = EVP_C_DATA(EVP_SM4_KEY, ctx);
  68. CRYPTO_ctr128_encrypt(in, out, len, &dat->ks, ctx->iv,
  69. EVP_CIPHER_CTX_buf_noconst(ctx), &num,
  70. (block128_f)ossl_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