[asterisk-commits] dvossel: branch dvossel/func_aes_trunk r170348 - /team/dvossel/func_aes_trunk...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Thu Jan 22 16:51:21 CST 2009
Author: dvossel
Date: Thu Jan 22 16:51:20 2009
New Revision: 170348
URL: http://svn.digium.com/svn-view/asterisk?view=rev&rev=170348
Log:
Fixed func_aes.c to further meet coding guidelines.
Modified:
team/dvossel/func_aes_trunk/funcs/func_aes.c
Modified: team/dvossel/func_aes_trunk/funcs/func_aes.c
URL: http://svn.digium.com/svn-view/asterisk/team/dvossel/func_aes_trunk/funcs/func_aes.c?view=diff&rev=170348&r1=170347&r2=170348
==============================================================================
--- team/dvossel/func_aes_trunk/funcs/func_aes.c (original)
+++ team/dvossel/func_aes_trunk/funcs/func_aes.c Thu Jan 22 16:51:20 2009
@@ -17,7 +17,8 @@
/*! \file
*
* \brief AES encryption/decryption dialplan functions
- *
+ *
+ * \author David Vossel <dvossel at digium.com>
* \ingroup functions
*/
@@ -35,101 +36,105 @@
static int aes_encrypt_read(struct ast_channel *chan, const char *cmd, char *data,
char *buf, size_t len)
{
-
- unsigned char curblock[16];
+ unsigned char curblock[16] = { 0, };
char *src;
char *dst;
- int x, data_len;
+ int data_len;
ast_aes_encrypt_key ecx; /* AES 128 Encryption context */
-
- AST_DECLARE_APP_ARGS(args,
+
+ AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(key);
AST_APP_ARG(data);
);
-
+
AST_STANDARD_APP_ARGS(args, data);
-
+
if (ast_strlen_zero(args.data) || ast_strlen_zero(args.key)) {
- ast_log(LOG_WARNING, "Syntax: AES_ENCRYPT(<key>, <data>) - missing argument!\n");
+ ast_log(LOG_WARNING, "Syntax: AES_ENCRYPT(<key>,<data>) - missing argument!\n");
return -1;
}
-
+
if (strlen(args.key) != 16 ) { /* key must be of 16 characters in length, 128 bits */
- ast_log(LOG_WARNING, "Syntax: AES_ENCRYPT(<key>, <data>) - <key> parameter must be exactly 16 characters!\n");
+ ast_log(LOG_WARNING, "Syntax: AES_ENCRYPT(<key>,<data>) - <key> parameter must be exactly 16 characters!\n");
return -1;
}
-
+
ast_aes_encrypt_key(args.key, &ecx);
-
+
dst = buf;
- src = args.data;
+ src = args.data;
data_len = strlen(args.data);
- memset(curblock, '\0', sizeof(curblock));
-
+
if (data_len >= len) { /* make sure to not go over buffer len */
- data_len = (len-1);
+ ast_log(LOG_WARNING, "Syntax: AES_ENCRYPT(<keys>,<data>) - <data> exceeds buffer length. Result may be truncated!\n");
+ data_len = len - 1;
}
-
+
while (data_len > 0) {
- for (x = 0; x < 16; x++)
- curblock[x] = src[x];
+ memset(curblock,0,sizeof(curblock));
+ if (data_len < 16) {
+ memcpy(curblock,src,data_len);
+ } else {
+ memcpy(curblock,src,16);
+ }
ast_aes_encrypt(curblock, dst, &ecx);
- memcpy(curblock, dst, sizeof(curblock));
dst += 16;
src += 16;
data_len -= 16;
}
-
+
return 0;
}
static int aes_decrypt_read(struct ast_channel *chan, const char *cmd, char *data,
char *buf, size_t len)
{
-
- unsigned char curblock[16];
+ unsigned char curblock[16] = { 0, };
char *src;
char *dst;
- int x, data_len;
+ int data_len;
ast_aes_decrypt_key dcx; /*!< AES 128 Encryption context */
-
- AST_DECLARE_APP_ARGS(args,
+
+ AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(key);
AST_APP_ARG(data);
);
-
+
AST_STANDARD_APP_ARGS(args, data);
-
+
if (ast_strlen_zero(args.data) || ast_strlen_zero(args.key)) {
- ast_log(LOG_WARNING, "Syntax: AES_DECRYPT(<key>, <data>) - missing argument!\n");
+ ast_log(LOG_WARNING, "Syntax: AES_DECRYPT(<key>,<data>) - missing argument!\n");
return -1;
}
if (strlen(args.key) != 16 ) { /* key must be of 16 characters in length, 128 bits */
- ast_log(LOG_WARNING, "Syntax: AES_DECRYPT(<key>, <data>) - <key> parameter must be exactly 16 characters!\n");
+ ast_log(LOG_WARNING, "Syntax: AES_DECRYPT(<key>,<data>) - <key> parameter must be exactly 16 characters!\n");
return -1;
}
-
+
ast_aes_decrypt_key(args.key, &dcx);
-
+
dst = buf;
- src = args.data;
+ src = args.data;
data_len = strlen(args.data);
- memset(curblock, '\0', sizeof(curblock));
-
+
if (data_len >= len) { /* make sure to not go over buffer len */
- data_len = (len-1);
+ ast_log(LOG_WARNING, "Syntax: AES_DECRYPT(<keys>,<data>) - <data> exceeds buffer length. Result may be truncated!\n");
+ data_len = len - 1;
}
-
+
while (data_len > 0) {
- for (x = 0; x < 16; x++)
- curblock[x] = src[x];
+ memset(curblock,0,sizeof(curblock));
+ if (data_len < 16) {
+ memcpy(curblock,src,data_len);
+ } else {
+ memcpy(curblock,src,16);
+ }
ast_aes_decrypt(curblock, dst, &dcx);
- memcpy(curblock, dst, sizeof(curblock));
dst += 16;
src += 16;
data_len -= 16;
}
-
+
return 0;
}
@@ -147,13 +152,14 @@
static int unload_module(void)
{
int res = ast_custom_function_unregister(&aes_decrypt_function);
- return res |= ast_custom_function_unregister(&aes_encrypt_function);
+ return res | ast_custom_function_unregister(&aes_encrypt_function);
}
static int load_module(void)
{
int res = ast_custom_function_register(&aes_decrypt_function);
- return res |= ast_custom_function_register(&aes_encrypt_function);
+ res |= ast_custom_function_register(&aes_encrypt_function);
+ return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
}
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "AES dialplan functions");
More information about the asterisk-commits
mailing list