evp.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. # This file is dual licensed under the terms of the Apache License, Version
  2. # 2.0, and the BSD License. See the LICENSE file in the root of this repository
  3. # for complete details.
  4. from __future__ import absolute_import, division, print_function
  5. INCLUDES = """
  6. #include <openssl/evp.h>
  7. """
  8. TYPES = """
  9. typedef ... EVP_CIPHER;
  10. typedef ... EVP_CIPHER_CTX;
  11. typedef ... EVP_MD;
  12. typedef ... EVP_MD_CTX;
  13. typedef ... EVP_PKEY;
  14. typedef ... EVP_PKEY_CTX;
  15. static const int EVP_PKEY_RSA;
  16. static const int EVP_PKEY_DSA;
  17. static const int EVP_PKEY_DH;
  18. static const int EVP_PKEY_DHX;
  19. static const int EVP_PKEY_EC;
  20. static const int EVP_PKEY_X25519;
  21. static const int EVP_PKEY_ED25519;
  22. static const int EVP_PKEY_X448;
  23. static const int EVP_PKEY_ED448;
  24. static const int EVP_PKEY_POLY1305;
  25. static const int EVP_MAX_MD_SIZE;
  26. static const int EVP_CTRL_AEAD_SET_IVLEN;
  27. static const int EVP_CTRL_AEAD_GET_TAG;
  28. static const int EVP_CTRL_AEAD_SET_TAG;
  29. static const int Cryptography_HAS_SCRYPT;
  30. static const int Cryptography_HAS_EVP_PKEY_DHX;
  31. static const int Cryptography_HAS_EVP_PKEY_get_set_tls_encodedpoint;
  32. static const int Cryptography_HAS_ONESHOT_EVP_DIGEST_SIGN_VERIFY;
  33. static const long Cryptography_HAS_RAW_KEY;
  34. static const long Cryptography_HAS_EVP_DIGESTFINAL_XOF;
  35. """
  36. FUNCTIONS = """
  37. const EVP_CIPHER *EVP_get_cipherbyname(const char *);
  38. int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *, int);
  39. int EVP_CipherInit_ex(EVP_CIPHER_CTX *, const EVP_CIPHER *, ENGINE *,
  40. const unsigned char *, const unsigned char *, int);
  41. int EVP_CipherUpdate(EVP_CIPHER_CTX *, unsigned char *, int *,
  42. const unsigned char *, int);
  43. int EVP_CipherFinal_ex(EVP_CIPHER_CTX *, unsigned char *, int *);
  44. int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *);
  45. EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void);
  46. void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *);
  47. int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *, int);
  48. const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *);
  49. int EVP_MD_CTX_copy_ex(EVP_MD_CTX *, const EVP_MD_CTX *);
  50. int EVP_DigestInit_ex(EVP_MD_CTX *, const EVP_MD *, ENGINE *);
  51. int EVP_DigestUpdate(EVP_MD_CTX *, const void *, size_t);
  52. int EVP_DigestFinal_ex(EVP_MD_CTX *, unsigned char *, unsigned int *);
  53. int EVP_DigestFinalXOF(EVP_MD_CTX *, unsigned char *, size_t);
  54. const EVP_MD *EVP_get_digestbyname(const char *);
  55. EVP_PKEY *EVP_PKEY_new(void);
  56. void EVP_PKEY_free(EVP_PKEY *);
  57. int EVP_PKEY_type(int);
  58. int EVP_PKEY_size(EVP_PKEY *);
  59. RSA *EVP_PKEY_get1_RSA(EVP_PKEY *);
  60. DSA *EVP_PKEY_get1_DSA(EVP_PKEY *);
  61. DH *EVP_PKEY_get1_DH(EVP_PKEY *);
  62. int EVP_PKEY_encrypt(EVP_PKEY_CTX *, unsigned char *, size_t *,
  63. const unsigned char *, size_t);
  64. int EVP_PKEY_decrypt(EVP_PKEY_CTX *, unsigned char *, size_t *,
  65. const unsigned char *, size_t);
  66. int EVP_SignInit(EVP_MD_CTX *, const EVP_MD *);
  67. int EVP_SignUpdate(EVP_MD_CTX *, const void *, size_t);
  68. int EVP_SignFinal(EVP_MD_CTX *, unsigned char *, unsigned int *, EVP_PKEY *);
  69. int EVP_VerifyInit(EVP_MD_CTX *, const EVP_MD *);
  70. int EVP_VerifyUpdate(EVP_MD_CTX *, const void *, size_t);
  71. int EVP_VerifyFinal(EVP_MD_CTX *, const unsigned char *, unsigned int,
  72. EVP_PKEY *);
  73. int EVP_DigestSignInit(EVP_MD_CTX *, EVP_PKEY_CTX **, const EVP_MD *,
  74. ENGINE *, EVP_PKEY *);
  75. int EVP_DigestSignUpdate(EVP_MD_CTX *, const void *, size_t);
  76. int EVP_DigestSignFinal(EVP_MD_CTX *, unsigned char *, size_t *);
  77. int EVP_DigestVerifyInit(EVP_MD_CTX *, EVP_PKEY_CTX **, const EVP_MD *,
  78. ENGINE *, EVP_PKEY *);
  79. EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *, ENGINE *);
  80. EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int, ENGINE *);
  81. EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *);
  82. void EVP_PKEY_CTX_free(EVP_PKEY_CTX *);
  83. int EVP_PKEY_sign_init(EVP_PKEY_CTX *);
  84. int EVP_PKEY_sign(EVP_PKEY_CTX *, unsigned char *, size_t *,
  85. const unsigned char *, size_t);
  86. int EVP_PKEY_verify_init(EVP_PKEY_CTX *);
  87. int EVP_PKEY_verify(EVP_PKEY_CTX *, const unsigned char *, size_t,
  88. const unsigned char *, size_t);
  89. int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *);
  90. int EVP_PKEY_verify_recover(EVP_PKEY_CTX *, unsigned char *,
  91. size_t *, const unsigned char *, size_t);
  92. int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *);
  93. int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *);
  94. int EVP_PKEY_set1_RSA(EVP_PKEY *, RSA *);
  95. int EVP_PKEY_set1_DSA(EVP_PKEY *, DSA *);
  96. int EVP_PKEY_set1_DH(EVP_PKEY *, DH *);
  97. int EVP_PKEY_cmp(const EVP_PKEY *, const EVP_PKEY *);
  98. int EVP_PKEY_keygen_init(EVP_PKEY_CTX *);
  99. int EVP_PKEY_keygen(EVP_PKEY_CTX *, EVP_PKEY **);
  100. int EVP_PKEY_derive_init(EVP_PKEY_CTX *);
  101. int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *, EVP_PKEY *);
  102. int EVP_PKEY_derive(EVP_PKEY_CTX *, unsigned char *, size_t *);
  103. int EVP_PKEY_set_type(EVP_PKEY *, int);
  104. int EVP_PKEY_id(const EVP_PKEY *);
  105. int Cryptography_EVP_PKEY_id(const EVP_PKEY *);
  106. EVP_MD_CTX *EVP_MD_CTX_new(void);
  107. void EVP_MD_CTX_free(EVP_MD_CTX *);
  108. /* Backwards compat aliases for pyOpenSSL */
  109. EVP_MD_CTX *Cryptography_EVP_MD_CTX_new(void);
  110. void Cryptography_EVP_MD_CTX_free(EVP_MD_CTX *);
  111. /* Added in 1.1.1 */
  112. int EVP_DigestSign(EVP_MD_CTX *, unsigned char *, size_t *,
  113. const unsigned char *, size_t);
  114. int EVP_DigestVerify(EVP_MD_CTX *, const unsigned char *, size_t,
  115. const unsigned char *, size_t);
  116. /* Added in 1.1.0 */
  117. size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY *, unsigned char **);
  118. int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *, const unsigned char *,
  119. size_t);
  120. /* EVP_PKEY * became const in 1.1.0 */
  121. int EVP_PKEY_bits(EVP_PKEY *);
  122. void OpenSSL_add_all_algorithms(void);
  123. int EVP_PKEY_assign_RSA(EVP_PKEY *, RSA *);
  124. EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *);
  125. int EVP_PKEY_set1_EC_KEY(EVP_PKEY *, EC_KEY *);
  126. int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *, int, int, void *);
  127. int PKCS5_PBKDF2_HMAC(const char *, int, const unsigned char *, int, int,
  128. const EVP_MD *, int, unsigned char *);
  129. int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *, const EVP_MD *);
  130. int EVP_PBE_scrypt(const char *, size_t, const unsigned char *, size_t,
  131. uint64_t, uint64_t, uint64_t, uint64_t, unsigned char *,
  132. size_t);
  133. EVP_PKEY *EVP_PKEY_new_raw_private_key(int, ENGINE *, const unsigned char *,
  134. size_t);
  135. EVP_PKEY *EVP_PKEY_new_raw_public_key(int, ENGINE *, const unsigned char *,
  136. size_t);
  137. int EVP_PKEY_get_raw_private_key(const EVP_PKEY *, unsigned char *, size_t *);
  138. int EVP_PKEY_get_raw_public_key(const EVP_PKEY *, unsigned char *, size_t *);
  139. """
  140. CUSTOMIZATIONS = """
  141. #ifdef EVP_PKEY_DHX
  142. const long Cryptography_HAS_EVP_PKEY_DHX = 1;
  143. #else
  144. const long Cryptography_HAS_EVP_PKEY_DHX = 0;
  145. const long EVP_PKEY_DHX = -1;
  146. #endif
  147. int Cryptography_EVP_PKEY_id(const EVP_PKEY *key) {
  148. return EVP_PKEY_id(key);
  149. }
  150. EVP_MD_CTX *Cryptography_EVP_MD_CTX_new(void) {
  151. return EVP_MD_CTX_new();
  152. }
  153. void Cryptography_EVP_MD_CTX_free(EVP_MD_CTX *md) {
  154. EVP_MD_CTX_free(md);
  155. }
  156. #if CRYPTOGRAPHY_IS_LIBRESSL || defined(OPENSSL_NO_SCRYPT)
  157. static const long Cryptography_HAS_SCRYPT = 0;
  158. int (*EVP_PBE_scrypt)(const char *, size_t, const unsigned char *, size_t,
  159. uint64_t, uint64_t, uint64_t, uint64_t, unsigned char *,
  160. size_t) = NULL;
  161. #else
  162. static const long Cryptography_HAS_SCRYPT = 1;
  163. #endif
  164. #if !CRYPTOGRAPHY_IS_LIBRESSL
  165. static const long Cryptography_HAS_EVP_PKEY_get_set_tls_encodedpoint = 1;
  166. #else
  167. static const long Cryptography_HAS_EVP_PKEY_get_set_tls_encodedpoint = 0;
  168. size_t (*EVP_PKEY_get1_tls_encodedpoint)(EVP_PKEY *, unsigned char **) = NULL;
  169. int (*EVP_PKEY_set1_tls_encodedpoint)(EVP_PKEY *, const unsigned char *,
  170. size_t) = NULL;
  171. #endif
  172. #if CRYPTOGRAPHY_OPENSSL_LESS_THAN_111
  173. static const long Cryptography_HAS_ONESHOT_EVP_DIGEST_SIGN_VERIFY = 0;
  174. static const long Cryptography_HAS_RAW_KEY = 0;
  175. static const long Cryptography_HAS_EVP_DIGESTFINAL_XOF = 0;
  176. int (*EVP_DigestFinalXOF)(EVP_MD_CTX *, unsigned char *, size_t) = NULL;
  177. int (*EVP_DigestSign)(EVP_MD_CTX *, unsigned char *, size_t *,
  178. const unsigned char *tbs, size_t) = NULL;
  179. int (*EVP_DigestVerify)(EVP_MD_CTX *, const unsigned char *, size_t,
  180. const unsigned char *, size_t) = NULL;
  181. EVP_PKEY *(*EVP_PKEY_new_raw_private_key)(int, ENGINE *, const unsigned char *,
  182. size_t) = NULL;
  183. EVP_PKEY *(*EVP_PKEY_new_raw_public_key)(int, ENGINE *, const unsigned char *,
  184. size_t) = NULL;
  185. int (*EVP_PKEY_get_raw_private_key)(const EVP_PKEY *, unsigned char *,
  186. size_t *) = NULL;
  187. int (*EVP_PKEY_get_raw_public_key)(const EVP_PKEY *, unsigned char *,
  188. size_t *) = NULL;
  189. #else
  190. static const long Cryptography_HAS_ONESHOT_EVP_DIGEST_SIGN_VERIFY = 1;
  191. static const long Cryptography_HAS_RAW_KEY = 1;
  192. static const long Cryptography_HAS_EVP_DIGESTFINAL_XOF = 1;
  193. #endif
  194. /* OpenSSL 1.1.0+ does this define for us, but if not present we'll do it */
  195. #if !defined(EVP_CTRL_AEAD_SET_IVLEN)
  196. # define EVP_CTRL_AEAD_SET_IVLEN EVP_CTRL_GCM_SET_IVLEN
  197. #endif
  198. #if !defined(EVP_CTRL_AEAD_GET_TAG)
  199. # define EVP_CTRL_AEAD_GET_TAG EVP_CTRL_GCM_GET_TAG
  200. #endif
  201. #if !defined(EVP_CTRL_AEAD_SET_TAG)
  202. # define EVP_CTRL_AEAD_SET_TAG EVP_CTRL_GCM_SET_TAG
  203. #endif
  204. /* This is tied to X25519 support so we reuse the Cryptography_HAS_X25519
  205. conditional to remove it. OpenSSL 1.1.0 didn't have this define, but
  206. 1.1.1 will when it is released. We can remove this in the distant
  207. future when we drop 1.1.0 support. */
  208. #ifndef EVP_PKEY_X25519
  209. #define EVP_PKEY_X25519 NID_X25519
  210. #endif
  211. /* This is tied to X448 support so we reuse the Cryptography_HAS_X448
  212. conditional to remove it. OpenSSL 1.1.1 adds this define. We can remove
  213. this in the distant future when we drop 1.1.0 support. */
  214. #ifndef EVP_PKEY_X448
  215. #define EVP_PKEY_X448 NID_X448
  216. #endif
  217. /* This is tied to ED25519 support so we reuse the Cryptography_HAS_ED25519
  218. conditional to remove it. */
  219. #ifndef EVP_PKEY_ED25519
  220. #define EVP_PKEY_ED25519 NID_ED25519
  221. #endif
  222. /* This is tied to ED448 support so we reuse the Cryptography_HAS_ED448
  223. conditional to remove it. */
  224. #ifndef EVP_PKEY_ED448
  225. #define EVP_PKEY_ED448 NID_ED448
  226. #endif
  227. /* This is tied to poly1305 support so we reuse the Cryptography_HAS_POLY1305
  228. conditional to remove it. */
  229. #ifndef EVP_PKEY_POLY1305
  230. #define EVP_PKEY_POLY1305 NID_poly1305
  231. #endif
  232. """