Mon Jun 27 16:51:14 2011

Asterisk developer's documentation


func_aes.c File Reference

AES encryption/decryption dialplan functions. More...

#include "asterisk.h"
#include "asterisk/module.h"
#include "asterisk/pbx.h"
#include "asterisk/app.h"
#include "asterisk/crypto.h"

Go to the source code of this file.

Defines

#define AES_BLOCK_SIZE   16

Functions

static void __reg_module (void)
static void __unreg_module (void)
static int aes_helper (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
static int load_module (void)
static int unload_module (void)

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "AES dialplan functions" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "8586c2a7d357cb591cc3a6607a8f62d1" , .load = load_module, .unload = unload_module, .nonoptreq = "res_crypto", }
static struct ast_custom_function aes_decrypt_function
static struct ast_custom_function aes_encrypt_function
static struct ast_module_infoast_module_info = &__mod_info


Detailed Description

AES encryption/decryption dialplan functions.

Author:
David Vossel <dvossel@digium.com>

Definition in file func_aes.c.


Define Documentation

#define AES_BLOCK_SIZE   16

Definition at line 38 of file func_aes.c.

Referenced by aes_helper().


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 180 of file func_aes.c.

static void __unreg_module ( void   )  [static]

Definition at line 180 of file func_aes.c.

static int aes_helper ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
) [static]

Definition at line 86 of file func_aes.c.

References AES_BLOCK_SIZE, args, ast_aes_decrypt(), ast_aes_encrypt(), ast_aes_set_decrypt_key(), ast_aes_set_encrypt_key(), AST_APP_ARG, ast_base64decode(), ast_base64encode(), ast_calloc, ast_copy_string(), AST_DECLARE_APP_ARGS, ast_free, ast_log(), AST_STANDARD_APP_ARGS, ast_strlen_zero(), and LOG_WARNING.

00088 {
00089    unsigned char curblock[AES_BLOCK_SIZE] = { 0, };
00090    char *tmp;
00091    char *tmpP;
00092    int data_len, encrypt;
00093    ast_aes_encrypt_key ecx;                        /*  AES 128 Encryption context */
00094    ast_aes_decrypt_key dcx;
00095 
00096    AST_DECLARE_APP_ARGS(args,
00097       AST_APP_ARG(key);
00098       AST_APP_ARG(data);
00099    );
00100 
00101    AST_STANDARD_APP_ARGS(args, data);
00102 
00103    if (ast_strlen_zero(args.data) || ast_strlen_zero(args.key)) {
00104       ast_log(LOG_WARNING, "Syntax: %s(<key>,<data>) - missing argument!\n", cmd);
00105       return -1;
00106    }
00107 
00108    if (strlen(args.key) != AES_BLOCK_SIZE) {        /* key must be of 16 characters in length, 128 bits */
00109       ast_log(LOG_WARNING, "Syntax: %s(<key>,<data>) - <key> parameter must be exactly 16 characters!\n", cmd);
00110       return -1;
00111    }
00112 
00113    ast_aes_set_encrypt_key((unsigned char *) args.key, &ecx);   /* encryption:  plaintext -> encryptedtext -> base64 */
00114    ast_aes_set_decrypt_key((unsigned char *) args.key, &dcx);   /* decryption:  base64 -> encryptedtext -> plaintext */
00115    tmp = ast_calloc(1, len);                     /* requires a tmp buffer for the base64 decode */
00116    tmpP = tmp;
00117    encrypt = strcmp("AES_DECRYPT", cmd);           /* -1 if encrypting, 0 if decrypting */
00118 
00119    if (encrypt) {                                  /* if decrypting first decode src to base64 */
00120       ast_copy_string(tmp, args.data, len);
00121       data_len = strlen(tmp);
00122    } else {
00123       data_len = ast_base64decode((unsigned char *) tmp, args.data, len);
00124    }
00125 
00126    if (data_len >= len) {                        /* make sure to not go over buffer len */
00127       ast_log(LOG_WARNING, "Syntax: %s(<keys>,<data>) - <data> exceeds buffer length.  Result may be truncated!\n", cmd);
00128       data_len = len - 1;
00129    }
00130 
00131    while (data_len > 0) {
00132       memset(curblock, 0, AES_BLOCK_SIZE);
00133       memcpy(curblock, tmpP, (data_len < AES_BLOCK_SIZE) ? data_len : AES_BLOCK_SIZE);
00134       if (encrypt) {
00135          ast_aes_encrypt(curblock, (unsigned char *) tmpP, &ecx);
00136       } else {
00137          ast_aes_decrypt(curblock, (unsigned char *) tmpP, &dcx);
00138       }
00139       tmpP += AES_BLOCK_SIZE;
00140       data_len -= AES_BLOCK_SIZE;
00141    }
00142 
00143    if (encrypt) {                            /* if encrypting encode result to base64 */
00144       ast_base64encode(buf, (unsigned char *) tmp, strlen(tmp), len);
00145    } else {
00146       memcpy(buf, tmp, len);
00147    }
00148    ast_free(tmp);
00149 
00150    return 0;
00151 }

static int load_module ( void   )  [static]

Definition at line 169 of file func_aes.c.

References aes_decrypt_function, aes_encrypt_function, ast_custom_function_register, AST_MODULE_LOAD_DECLINE, and AST_MODULE_LOAD_SUCCESS.

static int unload_module ( void   )  [static]

Definition at line 163 of file func_aes.c.

References aes_decrypt_function, aes_encrypt_function, and ast_custom_function_unregister().


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "AES dialplan functions" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "8586c2a7d357cb591cc3a6607a8f62d1" , .load = load_module, .unload = unload_module, .nonoptreq = "res_crypto", } [static]

Definition at line 180 of file func_aes.c.

struct ast_custom_function aes_decrypt_function [static]

Initial value:

 {
   .name = "AES_DECRYPT",
   .read = aes_helper,
}

Definition at line 158 of file func_aes.c.

Referenced by load_module(), and unload_module().

struct ast_custom_function aes_encrypt_function [static]

Initial value:

 {
   .name = "AES_ENCRYPT",
   .read = aes_helper,
}

Definition at line 153 of file func_aes.c.

Referenced by load_module(), and unload_module().

struct ast_module_info* ast_module_info = &__mod_info [static]

Definition at line 180 of file func_aes.c.


Generated on Mon Jun 27 16:51:14 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7