Review Board 1.7.16


added AES_ENCRYPT and AES_DECRYPT dialplan functions

Review Request #128 - Created Jan. 22, 2009 and submitted

David Vossel
trunk
0014301
Reviewers
asterisk-dev
mmichelson, russell
Asterisk
Allows data to be encrypted and decrypted using AES in the dialplan.  

 

Diff revision 2

This is not the most recent revision of the diff. The latest diff is revision 5. See what's changed.

1 2 3 4 5
1 2 3 4 5

  1. /trunk/funcs/func_aes.c: Loading...
/trunk/funcs/func_aes.c
New File

    
   
1
/*

    
   
2
 * Asterisk -- An open source telephony toolkit.

    
   
3
 *

    
   
4
 * Copyright (C) 1999-2009, Digium, Inc.

    
   
5
 *

    
   
6
 * See http://www.asterisk.org for more information about

    
   
7
 * the Asterisk project. Please do not directly contact

    
   
8
 * any of the maintainers of this project for assistance;

    
   
9
 * the project provides a web site, mailing lists and IRC

    
   
10
 * channels for your use.

    
   
11
 *

    
   
12
 * This program is free software, distributed under the terms of

    
   
13
 * the GNU General Public License Version 2. See the LICENSE file

    
   
14
 * at the top of the source tree.

    
   
15
 */

    
   
16

   

    
   
17
/*! \file

    
   
18
 *

    
   
19
 * \brief AES encryption/decryption dialplan functions

    
   
20
 *

    
   
21
 * \author David Vossel <dvossel@digium.com>

    
   
22
 * \ingroup functions

    
   
23
 */

    
   
24

   

    
   
25

   

    
   
26
#include "asterisk.h"

    
   
27

   

    
   
28
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")

    
   
29

   

    
   
30
#include "asterisk/module.h"

    
   
31
#include "asterisk/pbx.h"

    
   
32
#include "asterisk/app.h"

    
   
33
#include "asterisk/aes.h"

    
   
34

   

    
   
35

   

    
   
36
static int aes_encrypt_read(struct ast_channel *chan, const char *cmd, char *data,

    
   
37
	       char *buf, size_t len)

    
   
38
{

    
   
39
	unsigned char curblock[16] = { 0, };

    
   
40
	char *src;

    
   
41
	char *dst;

    
   
42
	int data_len;

    
   
43
	ast_aes_encrypt_key ecx;                       /*  AES 128 Encryption context */

    
   
44

   

    
   
45
	AST_DECLARE_APP_ARGS(args,

    
   
46
		AST_APP_ARG(key);

    
   
47
		AST_APP_ARG(data);

    
   
48
	);

    
   
49

   

    
   
50
	AST_STANDARD_APP_ARGS(args, data);

    
   
51

   

    
   
52
	if (ast_strlen_zero(args.data) || ast_strlen_zero(args.key)) {

    
   
53
		ast_log(LOG_WARNING, "Syntax: AES_ENCRYPT(<key>,<data>) - missing argument!\n");

    
   
54
		return -1;

    
   
55
	}

    
   
56

   

    
   
57
	if (strlen(args.key) != 16 ) {         /* key must be of 16 characters in length, 128 bits */

    
   
58
		ast_log(LOG_WARNING, "Syntax: AES_ENCRYPT(<key>,<data>) - <key> parameter must be exactly 16 characters!\n");

    
   
59
		return -1;

    
   
60
	}

    
   
61

   

    
   
62
	ast_aes_encrypt_key(args.key, &ecx);

    
   
63

   

    
   
64
	dst = buf;

    
   
65
	src = args.data;

    
   
66
	data_len = strlen(args.data);

    
   
67

   

    
   
68
	if (data_len >= len) {                           /* make sure to not go over buffer len */

    
   
69
		ast_log(LOG_WARNING, "Syntax: AES_ENCRYPT(<keys>,<data>) - <data> exceeds buffer length.  Result may be truncated!\n");

    
   
70
		data_len = len - 1;

    
   
71
	}

    
   
72

   

    
   
73
	while (data_len > 0) {

    
   
74
		memset(curblock,0,sizeof(curblock));

    
   
75
		if (data_len < 16) {

    
   
76
			memcpy(curblock,src,data_len);

    
   
77
		} else {

    
   
78
			memcpy(curblock,src,16);

    
   
79
		}

    
   
80
		ast_aes_encrypt(curblock, dst, &ecx);

    
   
81
		dst += 16;

    
   
82
		src += 16;

    
   
83
		data_len -= 16;

    
   
84
	}

    
   
85

   

    
   
86
	return 0;

    
   
87
}

    
   
88
static int aes_decrypt_read(struct ast_channel *chan, const char *cmd, char *data,

    
   
89
	char *buf, size_t len)

    
   
90
{

    
   
91
	unsigned char curblock[16] = { 0, };

    
   
92
	char *src;

    
   
93
	char *dst;

    
   
94
	int data_len;

    
   
95
	ast_aes_decrypt_key dcx;                       /*!< AES 128 Encryption context */

    
   
96

   

    
   
97
	AST_DECLARE_APP_ARGS(args,

    
   
98
			AST_APP_ARG(key);

    
   
99
			AST_APP_ARG(data);

    
   
100
	);

    
   
101

   

    
   
102
	AST_STANDARD_APP_ARGS(args, data);

    
   
103

   

    
   
104
	if (ast_strlen_zero(args.data) || ast_strlen_zero(args.key)) {

    
   
105
		ast_log(LOG_WARNING, "Syntax: AES_DECRYPT(<key>,<data>) - missing argument!\n");

    
   
106
		return -1;

    
   
107
	}

    
   
108

   

    
   
109
	if (strlen(args.key) != 16 ) {         /* key must be of 16 characters in length, 128 bits */

    
   
110
		ast_log(LOG_WARNING, "Syntax: AES_DECRYPT(<key>,<data>) - <key> parameter must be exactly 16 characters!\n");

    
   
111
		return -1;

    
   
112
	}

    
   
113

   

    
   
114
	ast_aes_decrypt_key(args.key, &dcx);

    
   
115

   

    
   
116
	dst = buf;

    
   
117
	src = args.data;

    
   
118
	data_len = strlen(args.data);

    
   
119

   

    
   
120
	if (data_len >= len) {                       /* make sure to not go over buffer len */

    
   
121
		ast_log(LOG_WARNING, "Syntax: AES_DECRYPT(<keys>,<data>) - <data> exceeds buffer length.  Result may be truncated!\n");

    
   
122
		data_len = len - 1;

    
   
123
	}

    
   
124

   

    
   
125
	while (data_len > 0) {

    
   
126
		memset(curblock,0,sizeof(curblock));

    
   
127
		if (data_len < 16) {

    
   
128
			memcpy(curblock,src,data_len);

    
   
129
		} else {

    
   
130
			memcpy(curblock,src,16);

    
   
131
		}

    
   
132
		ast_aes_decrypt(curblock, dst, &dcx);

    
   
133
		dst += 16;

    
   
134
		src += 16;

    
   
135
		data_len -= 16;

    
   
136
	}

    
   
137

   

    
   
138
	return 0;

    
   
139
}

    
   
140

   

    
   
141
static struct ast_custom_function aes_encrypt_function = {

    
   
142
	.name = "AES_ENCRYPT",

    
   
143
	.read = aes_encrypt_read,

    
   
144
};

    
   
145

   

    
   
146
static struct ast_custom_function aes_decrypt_function = {

    
   
147
	.name = "AES_DECRYPT",

    
   
148
	.read = aes_decrypt_read,

    
   
149
};

    
   
150

   

    
   
151

   

    
   
152
static int unload_module(void)

    
   
153
{

    
   
154
	int res = ast_custom_function_unregister(&aes_decrypt_function);

    
   
155
	return res | ast_custom_function_unregister(&aes_encrypt_function);

    
   
156
}

    
   
157

   

    
   
158
static int load_module(void)

    
   
159
{

    
   
160
	int res = ast_custom_function_register(&aes_decrypt_function);

    
   
161
	res |= ast_custom_function_register(&aes_encrypt_function);

    
   
162
	return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;

    
   
163
}

    
   
164

   

    
   
165
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "AES dialplan functions");
  1. /trunk/funcs/func_aes.c: Loading...

https://reviewboard.asterisk.org/ runs on a server provided by Digium, Inc. and uses bandwidth donated to the open source Asterisk community by API Digital Communications in Huntsville, AL USA.
Please report problems with this site to asteriskteam@digium.com.