crypto.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2010, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. * \brief Provide cryptographic signature routines
  20. */
  21. #ifndef _ASTERISK_CRYPTO_H
  22. #define _ASTERISK_CRYPTO_H
  23. #if defined(__cplusplus) || defined(c_plusplus)
  24. extern "C" {
  25. #endif
  26. #include "asterisk/optional_api.h"
  27. #include "asterisk/logger.h"
  28. #ifdef HAVE_CRYPTO
  29. #include "openssl/aes.h"
  30. typedef AES_KEY ast_aes_encrypt_key;
  31. typedef AES_KEY ast_aes_decrypt_key;
  32. #else /* !HAVE_CRYPTO */
  33. typedef char ast_aes_encrypt_key;
  34. typedef char ast_aes_decrypt_key;
  35. #endif /* HAVE_CRYPTO */
  36. #define AST_KEY_PUBLIC (1 << 0)
  37. #define AST_KEY_PRIVATE (1 << 1)
  38. struct ast_key;
  39. /*!
  40. * \brief Retrieve a key
  41. * \param key Name of the key we are retrieving
  42. * \param type Intger type of key (AST_KEY_PUBLIC or AST_KEY_PRIVATE)
  43. *
  44. * \retval the key on success.
  45. * \retval NULL on failure.
  46. */
  47. AST_OPTIONAL_API(struct ast_key *, ast_key_get, (const char *key, int type), { return NULL; });
  48. /*!
  49. * \brief Check the authenticity of a message signature using a given public key
  50. * \param key a public key to use to verify
  51. * \param msg the message that has been signed
  52. * \param sig the proposed valid signature in mime64-like encoding
  53. *
  54. * \retval 0 if the signature is valid.
  55. * \retval -1 otherwise.
  56. *
  57. */
  58. AST_OPTIONAL_API(int, ast_check_signature, (struct ast_key *key, const char *msg, const char *sig), { return -1; });
  59. /*!
  60. * \brief Check the authenticity of a message signature using a given public key
  61. * \param key a public key to use to verify
  62. * \param msg the message that has been signed
  63. * \param msglen
  64. * \param sig the proposed valid signature in raw binary representation
  65. *
  66. * \retval 0 if the signature is valid.
  67. * \retval -1 otherwise.
  68. *
  69. */
  70. AST_OPTIONAL_API(int, ast_check_signature_bin, (struct ast_key *key, const char *msg, int msglen, const unsigned char *sig), { return -1; });
  71. /*!
  72. * \brief Sign a message signature using a given private key
  73. * \param key a private key to use to create the signature
  74. * \param msg the message to sign
  75. * \param sig a pointer to a buffer of at least 256 bytes in which the
  76. * mime64-like encoded signature will be stored
  77. *
  78. * \retval 0 on success.
  79. * \retval -1 on failure.
  80. *
  81. */
  82. AST_OPTIONAL_API(int, ast_sign, (struct ast_key *key, char *msg, char *sig), { return -1; });
  83. /*!
  84. * \brief Sign a message signature using a given private key
  85. * \param key a private key to use to create the signature
  86. * \param msg the message to sign
  87. * \param msglen
  88. * \param sig a pointer to a buffer of at least 128 bytes in which the
  89. * raw encoded signature will be stored
  90. *
  91. * \retval 0 on success.
  92. * \retval -1 on failure.
  93. *
  94. */
  95. AST_OPTIONAL_API(int, ast_sign_bin, (struct ast_key *key, const char *msg, int msglen, unsigned char *sig), { return -1; });
  96. /*!
  97. * \brief Encrypt a message using a given private key
  98. * \param key a private key to use to encrypt
  99. * \param src the message to encrypt
  100. * \param srclen the length of the message to encrypt
  101. * \param dst a pointer to a buffer of at least srclen * 1.5 bytes in which the encrypted
  102. * answer will be stored
  103. *
  104. * \retval length of encrypted data on success.
  105. * \retval -1 on failure.
  106. *
  107. */
  108. AST_OPTIONAL_API(int, ast_encrypt_bin, (unsigned char *dst, const unsigned char *src, int srclen, struct ast_key *key), { return -1; });
  109. /*!
  110. * \brief Decrypt a message using a given private key
  111. * \param key a private key to use to decrypt
  112. * \param src the message to decrypt
  113. * \param srclen the length of the message to decrypt
  114. * \param dst a pointer to a buffer of at least srclen bytes in which the decrypted
  115. * answer will be stored
  116. *
  117. * \retval length of dencrypted data on success.
  118. * \retval -1 on failure.
  119. *
  120. */
  121. AST_OPTIONAL_API(int, ast_decrypt_bin, (unsigned char *dst, const unsigned char *src, int srclen, struct ast_key *key), { return -1; });
  122. /*!
  123. * \brief Set an encryption key
  124. * \param key a 16 char key
  125. * \param ctx address of an aes encryption context
  126. *
  127. * \retval 0 success
  128. * \retval nonzero failure
  129. */
  130. AST_OPTIONAL_API(int, ast_aes_set_encrypt_key,
  131. (const unsigned char *key, ast_aes_encrypt_key *ctx),
  132. { ast_log(LOG_WARNING, "AES encryption disabled. Install OpenSSL.\n"); return -1; });
  133. /*!
  134. * \brief Set a decryption key
  135. * \param key a 16 char key
  136. * \param ctx address of an aes encryption context
  137. *
  138. * \retval 0 success
  139. * \retval nonzero failure
  140. */
  141. AST_OPTIONAL_API(int, ast_aes_set_decrypt_key,
  142. (const unsigned char *key, ast_aes_decrypt_key *ctx),
  143. { ast_log(LOG_WARNING, "AES encryption disabled. Install OpenSSL.\n"); return -1; });
  144. /*!
  145. * \brief AES encrypt data
  146. * \param in data to be encrypted
  147. * \param out pointer to a buffer to hold the encrypted output
  148. * \param ctx address of an aes encryption context filled in with ast_aes_set_encrypt_key
  149. */
  150. AST_OPTIONAL_API(void, ast_aes_encrypt,
  151. (const unsigned char *in, unsigned char *out, const ast_aes_encrypt_key *ctx),
  152. { ast_log(LOG_WARNING, "AES encryption disabled. Install OpenSSL.\n");return; });
  153. /*!
  154. * \brief AES decrypt data
  155. * \param in encrypted data
  156. * \param out pointer to a buffer to hold the decrypted output
  157. * \param ctx address of an aes encryption context filled in with ast_aes_set_decrypt_key
  158. */
  159. AST_OPTIONAL_API(void, ast_aes_decrypt,
  160. (const unsigned char *in, unsigned char *out, const ast_aes_decrypt_key *ctx),
  161. { ast_log(LOG_WARNING, "AES encryption disabled. Install OpenSSL.\n");return; });
  162. AST_OPTIONAL_API(int, ast_crypto_loaded, (void), { return 0; });
  163. #if defined(__cplusplus) || defined(c_plusplus)
  164. }
  165. #endif
  166. #endif /* _ASTERISK_CRYPTO_H */