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 4 and 5

1 2 3 4 5
1 2 3 4 5

  1. /trunk/funcs/func_aes.c: Loading...
/trunk/funcs/func_aes.c
Diff Revision 4 Diff Revision 5
[20] 95 lines
[+20] [+] static int aes_helper(struct ast_channel *chan, const char *cmd, char *data,
96
	if (strlen(args.key) != AES_BLOCK_SIZE) {        /* key must be of 16 characters in length, 128 bits */
96
	if (strlen(args.key) != AES_BLOCK_SIZE) {        /* key must be of 16 characters in length, 128 bits */
97
		ast_log(LOG_WARNING, "Syntax: %s(<key>,<data>) - <key> parameter must be exactly 16 characters!\n", cmd);
97
		ast_log(LOG_WARNING, "Syntax: %s(<key>,<data>) - <key> parameter must be exactly 16 characters!\n", cmd);
98
		return -1;
98
		return -1;
99
	}
99
	}
100

    
   
100

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

    
   
106

   
107
	if (encrypt) {                                  /* if decrypting first decode src to base64 */
107
	if (encrypt) {                                  /* if decrypting first decode src to base64 */
108
		memcpy(tmp, args.data, strlen(args.data));  /* use same tmp buffer for encrypt as well to keep things simple */
108
		memcpy(tmp, args.data, strlen(args.data) < len ? strlen(args.data) : len); /* use same tmp buffer for encrypt as well to keep things simple */
109
		data_len = strlen(tmp);
109
		data_len = strlen(tmp);
110
	} else {
110
	} else {
111
		ast_base64decode((unsigned char *) tmp, args.data, len);
111
		ast_base64decode((unsigned char *) tmp, args.data, len);
112
		data_len = strlen(tmp);
112
		data_len = strlen(tmp);
113
	}
113
	}
[+20] [20] 4 lines
[+20] static int aes_helper(struct ast_channel *chan, const char *cmd, char *data,
118
	}
118
	}
119

    
   
119

   
120
	while (data_len > 0) {
120
	while (data_len > 0) {
121
		memset(curblock, 0, AES_BLOCK_SIZE);
121
		memset(curblock, 0, AES_BLOCK_SIZE);
122
		memcpy(curblock, tmpP, (data_len < AES_BLOCK_SIZE) ? data_len : AES_BLOCK_SIZE);
122
		memcpy(curblock, tmpP, (data_len < AES_BLOCK_SIZE) ? data_len : AES_BLOCK_SIZE);
123
		if (encrypt == 0) {
123
		if (encrypt) {
124
			ast_aes_encrypt(curblock, tmpP, &ecx);
124
			ast_aes_encrypt(curblock, tmpP, &ecx);
125
		} else {
125
		} else {
126
			ast_aes_decrypt(curblock, tmpP, &dcx);
126
			ast_aes_decrypt(curblock, tmpP, &dcx);
127
		}
127
		}
128
		tmpP += AES_BLOCK_SIZE;
128
		tmpP += AES_BLOCK_SIZE;
129
		data_len -= AES_BLOCK_SIZE;
129
		data_len -= AES_BLOCK_SIZE;
130
	}
130
	}
131

    
   
131

   
132
	if (encrypt) {                            /* if encrypting encode result to base64 */
132
	if (encrypt) {                            /* if encrypting encode result to base64 */
133
		ast_base64encode(buf, (unsigned char *) tmp, len, len);
133
		ast_base64encode(buf, (unsigned char *) tmp, strlen(tmp), len);
134
	} else {
134
	} else {
135
		memcpy(buf, tmp, len);
135
		memcpy(buf, tmp, len);
136
	}
136
	}
137
	free(tmp);
137
	ast_free(tmp);
138

    
   
138

   
139
	return 0;
139
	return 0;
140
}
140
}
141

    
   
141

   
142
static struct ast_custom_function aes_encrypt_function = {
142
static struct ast_custom_function aes_encrypt_function = {
[+20] [20] 24 lines
  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.