Thu Apr 28 2011 17:13:55

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/aes.h"
Include dependency graph for func_aes.c:

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
__MODULE_INFO_SECTION 
__mod_info = { __MODULE_INFO_GLOBALS .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "AES dialplan functions" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, }
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 35 of file func_aes.c.

Referenced by aes_helper().


Function Documentation

static void __reg_module ( void  ) [static]

Definition at line 173 of file func_aes.c.

static void __unreg_module ( void  ) [static]

Definition at line 173 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 83 of file func_aes.c.

References AES_BLOCK_SIZE, ast_aes_decrypt, ast_aes_encrypt, 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.

{
   unsigned char curblock[AES_BLOCK_SIZE] = { 0, };
   char *tmp;
   char *tmpP;
   int data_len, encrypt;
   ast_aes_encrypt_key ecx;                        /*  AES 128 Encryption context */
   ast_aes_decrypt_key dcx;

   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: %s(<key>,<data>) - missing argument!\n", cmd);
      return -1;
   }

   if (strlen(args.key) != AES_BLOCK_SIZE) {        /* key must be of 16 characters in length, 128 bits */
      ast_log(LOG_WARNING, "Syntax: %s(<key>,<data>) - <key> parameter must be exactly 16 characters!\n", cmd);
      return -1;
   }

   ast_aes_encrypt_key((unsigned char *) args.key, &ecx);   /* encryption:  plaintext -> encryptedtext -> base64 */
   ast_aes_decrypt_key((unsigned char *) args.key, &dcx);   /* decryption:  base64 -> encryptedtext -> plaintext */
   tmp = ast_calloc(1, len);                     /* requires a tmp buffer for the base64 decode */
   tmpP = tmp;
   encrypt = strcmp("AES_DECRYPT", cmd);           /* -1 if encrypting, 0 if decrypting */

   if (encrypt) {                                  /* if decrypting first decode src to base64 */
      ast_copy_string(tmp, args.data, len);
      data_len = strlen(tmp);
   } else {
      data_len = ast_base64decode((unsigned char *) tmp, args.data, len);
   }

   if (data_len >= len) {                        /* make sure to not go over buffer len */
      ast_log(LOG_WARNING, "Syntax: %s(<keys>,<data>) - <data> exceeds buffer length.  Result may be truncated!\n", cmd);
      data_len = len - 1;
   }

   while (data_len > 0) {
      memset(curblock, 0, AES_BLOCK_SIZE);
      memcpy(curblock, tmpP, (data_len < AES_BLOCK_SIZE) ? data_len : AES_BLOCK_SIZE);
      if (encrypt) {
         ast_aes_encrypt(curblock, (unsigned char *) tmpP, &ecx);
      } else {
         ast_aes_decrypt(curblock, (unsigned char *) tmpP, &dcx);
      }
      tmpP += AES_BLOCK_SIZE;
      data_len -= AES_BLOCK_SIZE;
   }

   if (encrypt) {                            /* if encrypting encode result to base64 */
      ast_base64encode(buf, (unsigned char *) tmp, strlen(tmp), len);
   } else {
      memcpy(buf, tmp, len);
   }
   ast_free(tmp);

   return 0;
}
static int unload_module ( void  ) [static]

Variable Documentation

struct ast_module_info __MODULE_INFO_SECTION __mod_info = { __MODULE_INFO_GLOBALS .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "AES dialplan functions" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, } [static]

Definition at line 173 of file func_aes.c.

Initial value:
 {
   .name = "AES_DECRYPT",
   .read = aes_helper,
}

Definition at line 155 of file func_aes.c.

Initial value:
 {
   .name = "AES_ENCRYPT",
   .read = aes_helper,
}

Definition at line 150 of file func_aes.c.

Definition at line 173 of file func_aes.c.