sm2.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 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. #ifndef OSSL_CRYPTO_SM2_H
  12. # define OSSL_CRYPTO_SM2_H
  13. # pragma once
  14. # include <openssl/opensslconf.h>
  15. # if !defined(OPENSSL_NO_SM2) && !defined(FIPS_MODULE)
  16. # include <openssl/ec.h>
  17. # include "crypto/types.h"
  18. int ossl_sm2_key_private_check(const EC_KEY *eckey);
  19. /* The default user id as specified in GM/T 0009-2012 */
  20. # define SM2_DEFAULT_USERID "1234567812345678"
  21. int ossl_sm2_compute_z_digest(uint8_t *out,
  22. const EVP_MD *digest,
  23. const uint8_t *id,
  24. const size_t id_len,
  25. const EC_KEY *key);
  26. /*
  27. * SM2 signature operation. Computes Z and then signs H(Z || msg) using SM2
  28. */
  29. ECDSA_SIG *ossl_sm2_do_sign(const EC_KEY *key,
  30. const EVP_MD *digest,
  31. const uint8_t *id,
  32. const size_t id_len,
  33. const uint8_t *msg, size_t msg_len);
  34. int ossl_sm2_do_verify(const EC_KEY *key,
  35. const EVP_MD *digest,
  36. const ECDSA_SIG *signature,
  37. const uint8_t *id,
  38. const size_t id_len,
  39. const uint8_t *msg, size_t msg_len);
  40. /*
  41. * SM2 signature generation.
  42. */
  43. int ossl_sm2_internal_sign(const unsigned char *dgst, int dgstlen,
  44. unsigned char *sig, unsigned int *siglen,
  45. EC_KEY *eckey);
  46. /*
  47. * SM2 signature verification.
  48. */
  49. int ossl_sm2_internal_verify(const unsigned char *dgst, int dgstlen,
  50. const unsigned char *sig, int siglen,
  51. EC_KEY *eckey);
  52. /*
  53. * SM2 encryption
  54. */
  55. int ossl_sm2_ciphertext_size(const EC_KEY *key, const EVP_MD *digest,
  56. size_t msg_len, size_t *ct_size);
  57. int ossl_sm2_plaintext_size(const unsigned char *ct, size_t ct_size,
  58. size_t *pt_size);
  59. int ossl_sm2_encrypt(const EC_KEY *key,
  60. const EVP_MD *digest,
  61. const uint8_t *msg, size_t msg_len,
  62. uint8_t *ciphertext_buf, size_t *ciphertext_len);
  63. int ossl_sm2_decrypt(const EC_KEY *key,
  64. const EVP_MD *digest,
  65. const uint8_t *ciphertext, size_t ciphertext_len,
  66. uint8_t *ptext_buf, size_t *ptext_len);
  67. const unsigned char *ossl_sm2_algorithmidentifier_encoding(int md_nid,
  68. size_t *len);
  69. # endif /* OPENSSL_NO_SM2 */
  70. #endif