crypto.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright (C) 2008-2012 Free Software Foundation, Inc.
  3. *
  4. * Author: Nikos Mavrogiannopoulos
  5. *
  6. * This file is part of GnuTLS.
  7. *
  8. * The GnuTLS is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * as published by the Free Software Foundation; either version 2.1 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. #ifndef GNUTLS_CRYPTO_H
  23. #define GNUTLS_CRYPTO_H
  24. #include <gnutls/gnutls.h>
  25. /* *INDENT-OFF* */
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. /* *INDENT-ON* */
  30. typedef struct api_cipher_hd_st *gnutls_cipher_hd_t;
  31. int gnutls_cipher_init(gnutls_cipher_hd_t * handle,
  32. gnutls_cipher_algorithm_t cipher,
  33. const gnutls_datum_t * key,
  34. const gnutls_datum_t * iv);
  35. int gnutls_cipher_encrypt(const gnutls_cipher_hd_t handle,
  36. void *text, size_t textlen);
  37. int gnutls_cipher_decrypt(const gnutls_cipher_hd_t handle,
  38. void *ciphertext, size_t ciphertextlen);
  39. int gnutls_cipher_decrypt2(gnutls_cipher_hd_t handle,
  40. const void *ciphertext,
  41. size_t ciphertextlen, void *text,
  42. size_t textlen);
  43. int gnutls_cipher_encrypt2(gnutls_cipher_hd_t handle,
  44. const void *text, size_t textlen,
  45. void *ciphertext, size_t ciphertextlen);
  46. void gnutls_cipher_set_iv(gnutls_cipher_hd_t handle, void *iv,
  47. size_t ivlen);
  48. int gnutls_cipher_tag(gnutls_cipher_hd_t handle, void *tag,
  49. size_t tag_size);
  50. int gnutls_cipher_add_auth(gnutls_cipher_hd_t handle,
  51. const void *text, size_t text_size);
  52. void gnutls_cipher_deinit(gnutls_cipher_hd_t handle);
  53. unsigned gnutls_cipher_get_block_size(gnutls_cipher_algorithm_t algorithm) __GNUTLS_CONST__;
  54. unsigned gnutls_cipher_get_iv_size(gnutls_cipher_algorithm_t algorithm) __GNUTLS_CONST__;
  55. unsigned gnutls_cipher_get_tag_size(gnutls_cipher_algorithm_t algorithm) __GNUTLS_CONST__;
  56. /* AEAD API
  57. */
  58. typedef struct api_aead_cipher_hd_st *gnutls_aead_cipher_hd_t;
  59. int gnutls_aead_cipher_init(gnutls_aead_cipher_hd_t * handle,
  60. gnutls_cipher_algorithm_t cipher,
  61. const gnutls_datum_t * key);
  62. int
  63. gnutls_aead_cipher_decrypt(gnutls_aead_cipher_hd_t handle,
  64. const void *nonce, size_t nonce_len,
  65. const void *auth, size_t auth_len,
  66. size_t tag_size,
  67. const void *ctext, size_t ctext_len,
  68. void *ptext, size_t *ptext_len);
  69. int
  70. gnutls_aead_cipher_encrypt(gnutls_aead_cipher_hd_t handle,
  71. const void *nonce, size_t nonce_len,
  72. const void *auth, size_t auth_len,
  73. size_t tag_size,
  74. const void *ptext, size_t ptext_len,
  75. void *ctext, size_t *ctext_len);
  76. void gnutls_aead_cipher_deinit(gnutls_aead_cipher_hd_t handle);
  77. /* Hash - MAC API */
  78. typedef struct hash_hd_st *gnutls_hash_hd_t;
  79. typedef struct hmac_hd_st *gnutls_hmac_hd_t;
  80. size_t gnutls_mac_get_nonce_size(gnutls_mac_algorithm_t algorithm) __GNUTLS_CONST__;
  81. int gnutls_hmac_init(gnutls_hmac_hd_t * dig,
  82. gnutls_mac_algorithm_t algorithm,
  83. const void *key, size_t keylen);
  84. void gnutls_hmac_set_nonce(gnutls_hmac_hd_t handle,
  85. const void *nonce, size_t nonce_len);
  86. int gnutls_hmac(gnutls_hmac_hd_t handle, const void *text, size_t textlen);
  87. void gnutls_hmac_output(gnutls_hmac_hd_t handle, void *digest);
  88. void gnutls_hmac_deinit(gnutls_hmac_hd_t handle, void *digest);
  89. unsigned gnutls_hmac_get_len(gnutls_mac_algorithm_t algorithm) __GNUTLS_CONST__;
  90. int gnutls_hmac_fast(gnutls_mac_algorithm_t algorithm,
  91. const void *key, size_t keylen,
  92. const void *text, size_t textlen, void *digest);
  93. int gnutls_hash_init(gnutls_hash_hd_t * dig,
  94. gnutls_digest_algorithm_t algorithm);
  95. int gnutls_hash(gnutls_hash_hd_t handle, const void *text, size_t textlen);
  96. void gnutls_hash_output(gnutls_hash_hd_t handle, void *digest);
  97. void gnutls_hash_deinit(gnutls_hash_hd_t handle, void *digest);
  98. unsigned gnutls_hash_get_len(gnutls_digest_algorithm_t algorithm) __GNUTLS_CONST__;
  99. int gnutls_hash_fast(gnutls_digest_algorithm_t algorithm,
  100. const void *text, size_t textlen, void *digest);
  101. /* register ciphers */
  102. /**
  103. * gnutls_rnd_level_t:
  104. * @GNUTLS_RND_NONCE: Non-predictable random number. Fatal in parts
  105. * of session if broken, i.e., vulnerable to statistical analysis.
  106. * @GNUTLS_RND_RANDOM: Pseudo-random cryptographic random number.
  107. * Fatal in session if broken. Example use: temporal keys.
  108. * @GNUTLS_RND_KEY: Fatal in many sessions if broken. Example use:
  109. * Long-term keys.
  110. *
  111. * Enumeration of random quality levels.
  112. */
  113. typedef enum gnutls_rnd_level {
  114. GNUTLS_RND_NONCE = 0,
  115. GNUTLS_RND_RANDOM = 1,
  116. GNUTLS_RND_KEY = 2
  117. } gnutls_rnd_level_t;
  118. int gnutls_rnd(gnutls_rnd_level_t level, void *data, size_t len);
  119. void gnutls_rnd_refresh(void);
  120. /* API to override ciphers and MAC algorithms
  121. */
  122. typedef int (*gnutls_cipher_init_func) (gnutls_cipher_algorithm_t, void **ctx, int enc);
  123. typedef int (*gnutls_cipher_setkey_func) (void *ctx, const void *key, size_t keysize);
  124. /* old style ciphers */
  125. typedef int (*gnutls_cipher_setiv_func) (void *ctx, const void *iv, size_t ivsize);
  126. typedef int (*gnutls_cipher_encrypt_func) (void *ctx, const void *plain, size_t plainsize,
  127. void *encr, size_t encrsize);
  128. typedef int (*gnutls_cipher_decrypt_func) (void *ctx, const void *encr, size_t encrsize,
  129. void *plain, size_t plainsize);
  130. /* aead ciphers */
  131. typedef int (*gnutls_cipher_auth_func) (void *ctx, const void *data, size_t datasize);
  132. typedef void (*gnutls_cipher_tag_func) (void *ctx, void *tag, size_t tagsize);
  133. typedef int (*gnutls_cipher_aead_encrypt_func) (void *ctx,
  134. const void *nonce, size_t noncesize,
  135. const void *auth, size_t authsize,
  136. size_t tag_size,
  137. const void *plain, size_t plainsize,
  138. void *encr, size_t encrsize);
  139. typedef int (*gnutls_cipher_aead_decrypt_func) (void *ctx,
  140. const void *nonce, size_t noncesize,
  141. const void *auth, size_t authsize,
  142. size_t tag_size,
  143. const void *encr, size_t encrsize,
  144. void *plain, size_t plainsize);
  145. typedef void (*gnutls_cipher_deinit_func) (void *ctx);
  146. int
  147. gnutls_crypto_register_cipher(gnutls_cipher_algorithm_t algorithm,
  148. int priority,
  149. gnutls_cipher_init_func init,
  150. gnutls_cipher_setkey_func setkey,
  151. gnutls_cipher_setiv_func setiv,
  152. gnutls_cipher_encrypt_func encrypt,
  153. gnutls_cipher_decrypt_func decrypt,
  154. gnutls_cipher_deinit_func deinit);
  155. int
  156. gnutls_crypto_register_aead_cipher(gnutls_cipher_algorithm_t algorithm,
  157. int priority,
  158. gnutls_cipher_init_func init,
  159. gnutls_cipher_setkey_func setkey,
  160. gnutls_cipher_aead_encrypt_func aead_encrypt,
  161. gnutls_cipher_aead_decrypt_func aead_decrypt,
  162. gnutls_cipher_deinit_func deinit);
  163. typedef int (*gnutls_mac_init_func) (gnutls_mac_algorithm_t, void **ctx);
  164. typedef int (*gnutls_mac_setkey_func) (void *ctx, const void *key, size_t keysize);
  165. typedef int (*gnutls_mac_setnonce_func) (void *ctx, const void *nonce, size_t noncesize);
  166. typedef int (*gnutls_mac_hash_func) (void *ctx, const void *text, size_t textsize);
  167. typedef int (*gnutls_mac_output_func) (void *src_ctx, void *digest, size_t digestsize);
  168. typedef void (*gnutls_mac_deinit_func) (void *ctx);
  169. typedef int (*gnutls_mac_fast_func) (gnutls_mac_algorithm_t, const void *nonce,
  170. size_t nonce_size, const void *key, size_t keysize,
  171. const void *text, size_t textsize, void *digest);
  172. int
  173. gnutls_crypto_register_mac(gnutls_mac_algorithm_t mac,
  174. int priority,
  175. gnutls_mac_init_func init,
  176. gnutls_mac_setkey_func setkey,
  177. gnutls_mac_setnonce_func setnonce,
  178. gnutls_mac_hash_func hash,
  179. gnutls_mac_output_func output,
  180. gnutls_mac_deinit_func deinit,
  181. gnutls_mac_fast_func hash_fast);
  182. typedef int (*gnutls_digest_init_func) (gnutls_digest_algorithm_t, void **ctx);
  183. typedef int (*gnutls_digest_hash_func) (void *ctx, const void *text, size_t textsize);
  184. typedef int (*gnutls_digest_output_func) (void *src_ctx, void *digest, size_t digestsize);
  185. typedef void (*gnutls_digest_deinit_func) (void *ctx);
  186. typedef int (*gnutls_digest_fast_func) (gnutls_digest_algorithm_t,
  187. const void *text, size_t textsize, void *digest);
  188. int
  189. gnutls_crypto_register_digest(gnutls_digest_algorithm_t digest,
  190. int priority,
  191. gnutls_digest_init_func init,
  192. gnutls_digest_hash_func hash,
  193. gnutls_digest_output_func output,
  194. gnutls_digest_deinit_func deinit,
  195. gnutls_digest_fast_func hash_fast);
  196. /* RSA-PKCS#1 1.5 helper functions */
  197. int
  198. gnutls_encode_ber_digest_info(gnutls_digest_algorithm_t hash,
  199. const gnutls_datum_t * digest,
  200. gnutls_datum_t * output);
  201. int
  202. gnutls_decode_ber_digest_info(const gnutls_datum_t * info,
  203. gnutls_digest_algorithm_t *hash,
  204. unsigned char *digest, unsigned int *digest_size);
  205. int gnutls_decode_rs_value(const gnutls_datum_t * sig_value, gnutls_datum_t *r, gnutls_datum_t *s);
  206. int gnutls_encode_rs_value(gnutls_datum_t * sig_value, const gnutls_datum_t * r, const gnutls_datum_t * s);
  207. /* *INDENT-OFF* */
  208. #ifdef __cplusplus
  209. }
  210. #endif
  211. /* *INDENT-ON* */
  212. #endif