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.  

 

Changes between revision 1 and 2

1 2 3 4 5
1 2 3 4 5

  1. /trunk/funcs/func_aes.c: Loading...
/trunk/funcs/func_aes.c
Diff Revision 1 Diff Revision 2
[20] 14 lines
[+20]
15
 */
15
 */
16

    
   
16

   
17
/*! \file
17
/*! \file
18
 *
18
 *
19
 * \brief AES encryption/decryption dialplan functions
19
 * \brief AES encryption/decryption dialplan functions
20
 * 
20
 *

    
   
21
 * \author David Vossel <dvossel@digium.com>
21
 * \ingroup functions
22
 * \ingroup functions
22
 */
23
 */
23

    
   
24

   
24

    
   
25

   
25
#include "asterisk.h"
26
#include "asterisk.h"
26

    
   
27

   
27
ASTERISK_FILE_VERSION(__FILE__, "$Revision: 170151 $")
28
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
28

    
   
29

   
29
#include "asterisk/module.h"
30
#include "asterisk/module.h"
30
#include "asterisk/pbx.h"
31
#include "asterisk/pbx.h"
31
#include "asterisk/app.h"
32
#include "asterisk/app.h"
32
#include "asterisk/aes.h"
33
#include "asterisk/aes.h"
33

    
   
34

   
34

    
   
35

   
35
static int aes_encrypt_read(struct ast_channel *chan, const char *cmd, char *data,
36
static int aes_encrypt_read(struct ast_channel *chan, const char *cmd, char *data,
36
	       char *buf, size_t len)
37
	       char *buf, size_t len)
37
{
38
{
38
	
39
	unsigned char curblock[16] = { 0, };
39
	unsigned char curblock[16];

   
40
	char *src;
40
	char *src;
41
	char *dst;
41
	char *dst;
42
	int x, data_len; 
42
	int data_len;
43
	ast_aes_encrypt_key ecx;                       /*  AES 128 Encryption context */
43
	ast_aes_encrypt_key ecx;                       /*  AES 128 Encryption context */
44
	
44

   
45
	AST_DECLARE_APP_ARGS(args,  
45
	AST_DECLARE_APP_ARGS(args,
46
		AST_APP_ARG(key);
46
		AST_APP_ARG(key);
47
		AST_APP_ARG(data);
47
		AST_APP_ARG(data);
48
	);
48
	);
49
	
49

   
50
	AST_STANDARD_APP_ARGS(args, data);
50
	AST_STANDARD_APP_ARGS(args, data);
51
	
51

   
52
	if (ast_strlen_zero(args.data) || ast_strlen_zero(args.key)) {
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");
53
		ast_log(LOG_WARNING, "Syntax: AES_ENCRYPT(<key>,<data>) - missing argument!\n");
54
		return -1;
54
		return -1;
55
	}
55
	}
56
	
56

   
57
	if (strlen(args.key) != 16 ) {         /* key must be of 16 characters in length, 128 bits */
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");
58
		ast_log(LOG_WARNING, "Syntax: AES_ENCRYPT(<key>,<data>) - <key> parameter must be exactly 16 characters!\n");
59
		return -1;
59
		return -1;
60
	}
60
	}
61
	
61

   
62
	ast_aes_encrypt_key(args.key, &ecx);
62
	ast_aes_encrypt_key(args.key, &ecx);
63
	
63

   
64
	dst = buf;
64
	dst = buf;
65
	src = args.data; 
65
	src = args.data;
66
	data_len = strlen(args.data);
66
	data_len = strlen(args.data);
67
	memset(curblock, '\0', sizeof(curblock));
67

   
68
	

   
69
	if (data_len >= len) {                           /* make sure to not go over buffer len */
68
	if (data_len >= len) {                           /* make sure to not go over buffer len */
70
		data_len = (len-1);
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
	}
71
	}
72
	
72

   
73
	while (data_len > 0) {
73
	while (data_len > 0) {
74
		for (x = 0; x < 16; x++)
74
		memset(curblock,0,sizeof(curblock));
75
			curblock[x] = src[x];
75
		if (data_len < 16) {

    
   
76
			memcpy(curblock,src,data_len);

    
   
77
		} else {

    
   
78
			memcpy(curblock,src,16);

    
   
79
		}
76
		ast_aes_encrypt(curblock, dst, &ecx);
80
		ast_aes_encrypt(curblock, dst, &ecx);
77
		memcpy(curblock, dst, sizeof(curblock));

   
78
		dst += 16;
81
		dst += 16;
79
		src += 16;
82
		src += 16;
80
		data_len -= 16;
83
		data_len -= 16;
81
	}
84
	}
82
	
85

   
83
	return 0;
86
	return 0;
84
}
87
}
85
static int aes_decrypt_read(struct ast_channel *chan, const char *cmd, char *data,
88
static int aes_decrypt_read(struct ast_channel *chan, const char *cmd, char *data,
86
	char *buf, size_t len)
89
	char *buf, size_t len)
87
{
90
{
88

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

   
90
	char *src;
92
	char *src;
91
	char *dst;
93
	char *dst;
92
	int x, data_len; 	
94
	int data_len;
93
	ast_aes_decrypt_key dcx;                       /*!< AES 128 Encryption context */
95
	ast_aes_decrypt_key dcx;                       /*!< AES 128 Encryption context */
94
	
96

   
95
	AST_DECLARE_APP_ARGS(args,  
97
	AST_DECLARE_APP_ARGS(args,
96
			AST_APP_ARG(key);
98
			AST_APP_ARG(key);
97
			AST_APP_ARG(data);
99
			AST_APP_ARG(data);
98
	);
100
	);
99
	
101

   
100
	AST_STANDARD_APP_ARGS(args, data);
102
	AST_STANDARD_APP_ARGS(args, data);
101
	
103

   
102
	if (ast_strlen_zero(args.data) || ast_strlen_zero(args.key)) {
104
	if (ast_strlen_zero(args.data) || ast_strlen_zero(args.key)) {
103
		ast_log(LOG_WARNING, "Syntax: AES_DECRYPT(<key>, <data>) - missing argument!\n");
105
		ast_log(LOG_WARNING, "Syntax: AES_DECRYPT(<key>,<data>) - missing argument!\n");
104
		return -1;
106
		return -1;
105
	}
107
	}
106

    
   
108

   
107
	if (strlen(args.key) != 16 ) {         /* key must be of 16 characters in length, 128 bits */
109
	if (strlen(args.key) != 16 ) {         /* key must be of 16 characters in length, 128 bits */
108
		ast_log(LOG_WARNING, "Syntax: AES_DECRYPT(<key>, <data>) - <key> parameter must be exactly 16 characters!\n");
110
		ast_log(LOG_WARNING, "Syntax: AES_DECRYPT(<key>,<data>) - <key> parameter must be exactly 16 characters!\n");
109
		return -1;
111
		return -1;
110
	}
112
	}
111
	
113

   
112
	ast_aes_decrypt_key(args.key, &dcx);
114
	ast_aes_decrypt_key(args.key, &dcx);
113
	
115

   
114
	dst = buf;
116
	dst = buf;
115
	src = args.data; 
117
	src = args.data;
116
	data_len = strlen(args.data);
118
	data_len = strlen(args.data);
117
	memset(curblock, '\0', sizeof(curblock));
119

   
118
	

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

    
   
122
		data_len = len - 1;
121
	}
123
	}
122
	
124

   
123
	while (data_len > 0) {
125
	while (data_len > 0) {
124
		for (x = 0; x < 16; x++)
126
		memset(curblock,0,sizeof(curblock));
125
			curblock[x] = src[x];
127
		if (data_len < 16) {

    
   
128
			memcpy(curblock,src,data_len);

    
   
129
		} else {

    
   
130
			memcpy(curblock,src,16);

    
   
131
		}
126
		ast_aes_decrypt(curblock, dst, &dcx);
132
		ast_aes_decrypt(curblock, dst, &dcx);
127
		memcpy(curblock, dst, sizeof(curblock));

   
128
		dst += 16;
133
		dst += 16;
129
		src += 16;
134
		src += 16;
130
		data_len -= 16;
135
		data_len -= 16;
131
	}
136
	}
132
	
137

   
133
	return 0;
138
	return 0;
134
}
139
}
135

    
   
140

   
136
static struct ast_custom_function aes_encrypt_function = {
141
static struct ast_custom_function aes_encrypt_function = {
137
	.name = "AES_ENCRYPT",
142
	.name = "AES_ENCRYPT",
[+20] [20] 7 lines
[+20] [+] static struct ast_custom_function aes_decrypt_function = {
145

    
   
150

   
146

    
   
151

   
147
static int unload_module(void)
152
static int unload_module(void)
148
{
153
{
149
	int res = ast_custom_function_unregister(&aes_decrypt_function);
154
	int res = ast_custom_function_unregister(&aes_decrypt_function);
150
	return res |= ast_custom_function_unregister(&aes_encrypt_function);
155
	return res | ast_custom_function_unregister(&aes_encrypt_function);
151
}
156
}
152

    
   
157

   
153
static int load_module(void)
158
static int load_module(void)
154
{
159
{
155
	int res = ast_custom_function_register(&aes_decrypt_function);
160
	int res = ast_custom_function_register(&aes_decrypt_function);
156
	return res |= ast_custom_function_register(&aes_encrypt_function);
161
	res |= ast_custom_function_register(&aes_encrypt_function);

    
   
162
	return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
157
}
163
}
158

    
   
164

   
159
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "AES dialplan functions");
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.