sm2.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright 2017-2021 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. #ifndef OSSL_CRYPTO_SM2_H
  12. # define OSSL_CRYPTO_SM2_H
  13. # include <openssl/opensslconf.h>
  14. # ifndef OPENSSL_NO_SM2
  15. # include <openssl/ec.h>
  16. /* The default user id as specified in GM/T 0009-2012 */
  17. # define SM2_DEFAULT_USERID "1234567812345678"
  18. int sm2_compute_z_digest(uint8_t *out,
  19. const EVP_MD *digest,
  20. const uint8_t *id,
  21. const size_t id_len,
  22. const EC_KEY *key);
  23. /*
  24. * SM2 signature operation. Computes Z and then signs H(Z || msg) using SM2
  25. */
  26. ECDSA_SIG *sm2_do_sign(const EC_KEY *key,
  27. const EVP_MD *digest,
  28. const uint8_t *id,
  29. const size_t id_len,
  30. const uint8_t *msg, size_t msg_len);
  31. int sm2_do_verify(const EC_KEY *key,
  32. const EVP_MD *digest,
  33. const ECDSA_SIG *signature,
  34. const uint8_t *id,
  35. const size_t id_len,
  36. const uint8_t *msg, size_t msg_len);
  37. /*
  38. * SM2 signature generation.
  39. */
  40. int sm2_sign(const unsigned char *dgst, int dgstlen,
  41. unsigned char *sig, unsigned int *siglen, EC_KEY *eckey);
  42. /*
  43. * SM2 signature verification.
  44. */
  45. int sm2_verify(const unsigned char *dgst, int dgstlen,
  46. const unsigned char *sig, int siglen, EC_KEY *eckey);
  47. /*
  48. * SM2 encryption
  49. */
  50. int sm2_ciphertext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len,
  51. size_t *ct_size);
  52. int sm2_plaintext_size(const unsigned char *ct, size_t ct_size, size_t *pt_size);
  53. int sm2_encrypt(const EC_KEY *key,
  54. const EVP_MD *digest,
  55. const uint8_t *msg,
  56. size_t msg_len,
  57. uint8_t *ciphertext_buf, size_t *ciphertext_len);
  58. int sm2_decrypt(const EC_KEY *key,
  59. const EVP_MD *digest,
  60. const uint8_t *ciphertext,
  61. size_t ciphertext_len, uint8_t *ptext_buf, size_t *ptext_len);
  62. # endif /* OPENSSL_NO_SM2 */
  63. #endif