pkcs8.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the License); you may
  5. * not use this file except in compliance with the License.
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. */
  9. // RFC 5208: PKCS #8: Private-Key Information Syntax Specification version 1.2
  10. #ifndef GMSSL_PKCS8_H
  11. #define GMSSL_PKCS8_H
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <stdint.h>
  16. #include <gmssl/sm2.h>
  17. #include <gmssl/pem.h>
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. /*
  22. id-PBKDF2 OBJECT IDENTIFIER ::= {pkcs-5 12}
  23. PBKDF2-params ::= SEQUENCE {
  24. salt CHOICE {
  25. specified OCTET STRING,
  26. otherSource AlgorithmIdentifier {{PBKDF2-SaltSources}}
  27. },
  28. iterationCount INTEGER (1..MAX),
  29. keyLength INTEGER (1..MAX) OPTIONAL, -- 这个参数可以由函数指定
  30. prf AlgorithmIdentifier {{PBKDF2-PRFs}} DEFAULT algid-hmacWithSHA1
  31. }
  32. prf must be OID_hmac_sm3
  33. cipher must be OID_sm4_cbc
  34. */
  35. int pbkdf2_params_to_der(const uint8_t *salt, size_t saltlen, int iter, int keylen, int prf,
  36. uint8_t **out, size_t *outlen);
  37. int pbkdf2_params_from_der(const uint8_t **salt, size_t *saltlen, int *iter, int *keylen, int *prf,
  38. const uint8_t **in, size_t *inlen);
  39. int pbkdf2_params_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *d, size_t dlen);
  40. int pbkdf2_algor_to_der(
  41. const uint8_t *salt, size_t saltlen,
  42. int iter,
  43. int keylen,
  44. int prf,
  45. uint8_t **out, size_t *outlen);
  46. int pbkdf2_algor_from_der(
  47. const uint8_t **salt, size_t *saltlen,
  48. int *iter,
  49. int *keylen,
  50. int *prf,
  51. const uint8_t **in, size_t *inlen);
  52. int pbkdf2_algor_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *d, size_t dlen);
  53. /*
  54. id-PBES2 OBJECT IDENTIFIER ::= {pkcs-5 13}
  55. PBES2-params ::= SEQUENCE {
  56. keyDerivationFunc AlgorithmIdentifier {{PBES2-KDFs}}, -- id-PBKDF2
  57. encryptionScheme AlgorithmIdentifier {{PBES2-Encs}}}
  58. PBES2-Encs:
  59. AES-CBC-Pad [RFC2898]
  60. RC5-CBC-Pad
  61. DES-CBC-Pad legacy
  62. DES-EDE3-CBC-Pad legacy
  63. RC2-CBC-Pad legacy
  64. */
  65. int pbes2_enc_algor_to_der(
  66. int cipher,
  67. const uint8_t *iv, size_t ivlen,
  68. uint8_t **out, size_t *outlen);
  69. int pbes2_enc_algor_from_der(
  70. int *cipher,
  71. const uint8_t **iv, size_t *ivlen,
  72. const uint8_t **in, size_t *inlen);
  73. int pbes2_enc_algor_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *d, size_t dlen);
  74. int pbes2_params_to_der(
  75. const uint8_t *salt, size_t saltlen,
  76. int iter,
  77. int keylen,
  78. int prf,
  79. int cipher,
  80. const uint8_t *iv, size_t ivlen,
  81. uint8_t **out, size_t *outlen);
  82. int pbes2_params_from_der(
  83. const uint8_t **salt, size_t *saltlen,
  84. int *iter,
  85. int *keylen,
  86. int *prf,
  87. int *cipher,
  88. const uint8_t **iv, size_t *ivlen,
  89. const uint8_t **in, size_t *inlen);
  90. int pbes2_params_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *d, size_t dlen);
  91. int pbes2_algor_to_der(
  92. const uint8_t *salt, size_t saltlen,
  93. int iter,
  94. int keylen,
  95. int prf,
  96. int cipher,
  97. const uint8_t *iv, size_t ivlen,
  98. uint8_t **out, size_t *outlen);
  99. int pbes2_algor_from_der(
  100. const uint8_t **salt, size_t *saltlen,
  101. int *iter,
  102. int *keylen,
  103. int *prf,
  104. int *cipher,
  105. const uint8_t **iv, size_t *ivlen,
  106. const uint8_t **in, size_t *inlen);
  107. int pbes2_algor_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *d, size_t dlen);
  108. /*
  109. from [RFC 5208]
  110. EncryptedPrivateKeyInfo ::= SEQUENCE {
  111. encryptionAlgorithm EncryptionAlgorithmIdentifier,
  112. encryptedData OCTET STRING }
  113. encryptionAlgorithm:
  114. id-PBES2
  115. PrivateKeyInfo ::= SEQUENCE {
  116. version INTEGER { v1(0) },
  117. privateKeyAlgorithm AlgorithmIdentifier,
  118. privateKey OCTET STRING,
  119. attributes [0] Attributes OPTIONAL }
  120. */
  121. int pkcs8_enced_private_key_info_to_der(
  122. const uint8_t *salt, size_t saltlen,
  123. int iter,
  124. int keylen,
  125. int prf,
  126. int cipher,
  127. const uint8_t *iv, size_t ivlen,
  128. const uint8_t *enced, size_t encedlen,
  129. uint8_t **out, size_t *outlen);
  130. int pkcs8_enced_private_key_info_from_der(
  131. const uint8_t **salt, size_t *saltlen,
  132. int *iter,
  133. int *keylen,
  134. int *prf,
  135. int *cipher,
  136. const uint8_t **iv, size_t *ivlen,
  137. const uint8_t **enced, size_t *encedlen,
  138. const uint8_t **in, size_t *inlen);
  139. int pkcs8_enced_private_key_info_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *d, size_t dlen);
  140. #ifdef __cplusplus
  141. }
  142. #endif
  143. #endif