crypto.h

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2010, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  * \brief Provide cryptographic signature routines
00021  */
00022 
00023 #ifndef _ASTERISK_CRYPTO_H
00024 #define _ASTERISK_CRYPTO_H
00025 
00026 #if defined(__cplusplus) || defined(c_plusplus)
00027 extern "C" {
00028 #endif
00029 
00030 #include "asterisk/optional_api.h"
00031 #include "asterisk/logger.h"
00032 
00033 #ifdef HAVE_CRYPTO
00034 #include "openssl/aes.h"
00035 typedef AES_KEY ast_aes_encrypt_key;
00036 typedef AES_KEY ast_aes_decrypt_key;
00037 #else /* !HAVE_CRYPTO */
00038 typedef char ast_aes_encrypt_key;
00039 typedef char ast_aes_decrypt_key;
00040 #endif /* HAVE_CRYPTO */
00041 
00042 #define AST_KEY_PUBLIC  (1 << 0)
00043 #define AST_KEY_PRIVATE (1 << 1)
00044 
00045 struct ast_key;
00046 
00047 /*!
00048  * \brief Retrieve a key
00049  * \param key Name of the key we are retrieving
00050  * \param type Intger type of key (AST_KEY_PUBLIC or AST_KEY_PRIVATE)
00051  *
00052  * \retval the key on success.
00053  * \retval NULL on failure.
00054  */
00055 AST_OPTIONAL_API(struct ast_key *, ast_key_get, (const char *key, int type), { return NULL; });
00056 
00057 /*!
00058  * \brief Check the authenticity of a message signature using a given public key
00059  * \param key a public key to use to verify
00060  * \param msg the message that has been signed
00061  * \param sig the proposed valid signature in mime64-like encoding
00062  *
00063  * \retval 0 if the signature is valid.
00064  * \retval -1 otherwise.
00065  *
00066  */
00067 AST_OPTIONAL_API(int, ast_check_signature, (struct ast_key *key, const char *msg, const char *sig), { return -1; });
00068 
00069 /*!
00070  * \brief Check the authenticity of a message signature using a given public key
00071  * \param key a public key to use to verify
00072  * \param msg the message that has been signed
00073  * \param msglen
00074  * \param sig the proposed valid signature in raw binary representation
00075  *
00076  * \retval 0 if the signature is valid.
00077  * \retval -1 otherwise.
00078  *
00079  */
00080 AST_OPTIONAL_API(int, ast_check_signature_bin, (struct ast_key *key, const char *msg, int msglen, const unsigned char *sig), { return -1; });
00081 
00082 /*!
00083  * \brief Sign a message signature using a given private key
00084  * \param key a private key to use to create the signature
00085  * \param msg the message to sign
00086  * \param sig a pointer to a buffer of at least 256 bytes in which the
00087  * mime64-like encoded signature will be stored
00088  *
00089  * \retval 0 on success.
00090  * \retval -1 on failure.
00091  *
00092  */
00093 AST_OPTIONAL_API(int, ast_sign, (struct ast_key *key, char *msg, char *sig), { return -1; });
00094 
00095 /*!
00096  * \brief Sign a message signature using a given private key
00097  * \param key a private key to use to create the signature
00098  * \param msg the message to sign
00099  * \param msglen
00100  * \param sig a pointer to a buffer of at least 128 bytes in which the
00101  * raw encoded signature will be stored
00102  *
00103  * \retval 0 on success.
00104  * \retval -1 on failure.
00105  *
00106  */
00107 AST_OPTIONAL_API(int, ast_sign_bin, (struct ast_key *key, const char *msg, int msglen, unsigned char *sig), { return -1; });
00108 
00109 /*!
00110  * \brief Encrypt a message using a given private key
00111  * \param key a private key to use to encrypt
00112  * \param src the message to encrypt
00113  * \param srclen the length of the message to encrypt
00114  * \param dst a pointer to a buffer of at least srclen * 1.5 bytes in which the encrypted
00115  * answer will be stored
00116  *
00117  * \retval length of encrypted data on success.
00118  * \retval -1 on failure.
00119  *
00120  */
00121 AST_OPTIONAL_API(int, ast_encrypt_bin, (unsigned char *dst, const unsigned char *src, int srclen, struct ast_key *key), { return -1; });
00122 
00123 /*!
00124  * \brief Decrypt a message using a given private key
00125  * \param key a private key to use to decrypt
00126  * \param src the message to decrypt
00127  * \param srclen the length of the message to decrypt
00128  * \param dst a pointer to a buffer of at least srclen bytes in which the decrypted
00129  * answer will be stored
00130  *
00131  * \retval length of dencrypted data on success.
00132  * \retval -1 on failure.
00133  *
00134  */
00135 AST_OPTIONAL_API(int, ast_decrypt_bin, (unsigned char *dst, const unsigned char *src, int srclen, struct ast_key *key), { return -1; });
00136 
00137 /*!
00138  * \brief Set an encryption key
00139  * \param key a 16 char key
00140  * \param ctx address of an aes encryption context
00141  *
00142  * \retval 0 success
00143  * \retval nonzero failure
00144  */
00145 AST_OPTIONAL_API(int, ast_aes_set_encrypt_key,
00146    (const unsigned char *key, ast_aes_encrypt_key *ctx),
00147    { ast_log(LOG_WARNING, "AES encryption disabled. Install OpenSSL.\n"); return -1; });
00148 
00149 /*!
00150  * \brief Set a decryption key
00151  * \param key a 16 char key
00152  * \param ctx address of an aes encryption context
00153  *
00154  * \retval 0 success
00155  * \retval nonzero failure
00156  */
00157 AST_OPTIONAL_API(int, ast_aes_set_decrypt_key,
00158    (const unsigned char *key, ast_aes_decrypt_key *ctx),
00159    { ast_log(LOG_WARNING, "AES encryption disabled. Install OpenSSL.\n"); return -1; });
00160 
00161 /*!
00162  * \brief AES encrypt data
00163  * \param in data to be encrypted
00164  * \param out pointer to a buffer to hold the encrypted output
00165  * \param ctx address of an aes encryption context filled in with ast_aes_set_encrypt_key
00166  */
00167 AST_OPTIONAL_API(void, ast_aes_encrypt,
00168    (const unsigned char *in, unsigned char *out, const ast_aes_encrypt_key *ctx),
00169    { ast_log(LOG_WARNING, "AES encryption disabled. Install OpenSSL.\n");return; });
00170 
00171 /*!
00172  * \brief AES decrypt data
00173  * \param in encrypted data
00174  * \param out pointer to a buffer to hold the decrypted output
00175  * \param ctx address of an aes encryption context filled in with ast_aes_set_decrypt_key
00176  */
00177 AST_OPTIONAL_API(void, ast_aes_decrypt,
00178    (const unsigned char *in, unsigned char *out, const ast_aes_decrypt_key *ctx),
00179    { ast_log(LOG_WARNING, "AES encryption disabled. Install OpenSSL.\n");return; });
00180 
00181 AST_OPTIONAL_API(int, ast_crypto_loaded, (void), { return 0; });
00182 
00183 #if defined(__cplusplus) || defined(c_plusplus)
00184 }
00185 #endif
00186 
00187 #endif /* _ASTERISK_CRYPTO_H */

Generated on Thu Apr 16 06:27:32 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6