22 * zigbee_crypt.c
33 * Copyright 2011 steiner <steiner@localhost.localdomain>
44 * zigbee convenience functions
5- *
5+ *
66 * alot of this code was "borrowed" from wireshark
77 * packet-zbee-security.c & pzcket-zbee-security.h
88 * function: zbee_sec_ccm_decrypt
99 */
10-
10+
1111// Explaination of Python Build Values http://docs.python.org/c-api/arg.html#Py_BuildValue
1212
1313#include <Python.h>
@@ -27,6 +27,7 @@ static PyObject *zigbee_crypt_encrypt_ccm(PyObject *self, PyObject *args) {
2727 const char * zigbeeData ;
2828 int sizeZigbeeData ;
2929 int i , j ;
30+ PyObject * res ;
3031
3132 char pMIC [ZBEE_SEC_CONST_MICSIZE ];
3233 char pEncMIC [ZBEE_SEC_CONST_MICSIZE ];
@@ -35,7 +36,7 @@ static PyObject *zigbee_crypt_encrypt_ccm(PyObject *self, PyObject *args) {
3536 char cipher_out [ZBEE_SEC_CONST_BLOCKSIZE ];
3637 /* Cipher Instance. */
3738 gcry_cipher_hd_t cipher_hd ;
38-
39+
3940 if (!PyArg_ParseTuple (args , "s#s#is#s#" ,
4041 & pZkey , & sizeZkey ,
4142 & pNonce , & sizeNonce ,
@@ -48,12 +49,12 @@ static PyObject *zigbee_crypt_encrypt_ccm(PyObject *self, PyObject *args) {
4849 PyErr_SetString (PyExc_ValueError , "incorrect key size (must be 16)" );
4950 return NULL ;
5051 }
51-
52+
5253 if (sizeNonce != ZBEE_SEC_CONST_NONCE_LEN ) {
5354 PyErr_SetString (PyExc_ValueError , "incorrect nonce size (must be 13)" );
5455 return NULL ;
5556 }
56-
57+
5758 if ((sizeMIC != 0 ) && (sizeMIC != 4 ) && (sizeMIC != 8 ) && (sizeMIC != 16 )) {
5859 PyErr_SetString (PyExc_ValueError , "incorrect mic size (must be 0, 4, 8, or 16 bytes)" );
5960 return NULL ;
@@ -63,7 +64,7 @@ static PyObject *zigbee_crypt_encrypt_ccm(PyObject *self, PyObject *args) {
6364 memset (pEncMIC , 0 , ZBEE_SEC_CONST_MICSIZE );
6465 pEncrypted = malloc (sizeUnencryptedData );
6566 memset (pEncrypted , 0 , sizeUnencryptedData );
66-
67+
6768 /* Open the cipher in ECB mode. */
6869 if (gcry_cipher_open (& cipher_hd , GCRY_CIPHER_AES128 , GCRY_CIPHER_MODE_ECB , 0 )) {
6970 PyErr_SetString (PyExc_Exception , "gcrypt open AES-128 ECB cipher failed" );
@@ -87,7 +88,7 @@ static PyObject *zigbee_crypt_encrypt_ccm(PyObject *self, PyObject *args) {
8788 gcry_cipher_close (cipher_hd );
8889 return NULL ;
8990 }
90-
91+
9192 j = 0 ;
9293 if (sizeZigbeeData > 0 ) {
9394 /* Process L(a) into the cipher block. */
@@ -139,10 +140,10 @@ static PyObject *zigbee_crypt_encrypt_ccm(PyObject *self, PyObject *args) {
139140 gcry_cipher_close (cipher_hd );
140141 return NULL ;
141142 }
142-
143+
143144 gcry_cipher_close (cipher_hd );
144145 memcpy (pMIC , cipher_out , sizeMIC );
145-
146+
146147 /* Create the CCM* counter block A0 */
147148 memset (cipher_in , 0 , ZBEE_SEC_CONST_BLOCKSIZE );
148149 cipher_in [0 ] = ZBEE_SEC_CCM_FLAG_L ;
@@ -187,8 +188,10 @@ static PyObject *zigbee_crypt_encrypt_ccm(PyObject *self, PyObject *args) {
187188 }
188189 /* Done with the CTR Cipher. */
189190 gcry_cipher_close (cipher_hd );
190-
191- return Py_BuildValue ("(s#s#)" , pEncrypted , sizeUnencryptedData , pEncMIC , sizeMIC );
191+
192+ res = Py_BuildValue ("(s#s#)" , pEncrypted , sizeUnencryptedData , pEncMIC , sizeMIC );
193+ free (pEncrypted );
194+ return res ;
192195};
193196
194197static PyObject * zigbee_crypt_decrypt_ccm (PyObject * self , PyObject * args ) {
@@ -202,6 +205,7 @@ static PyObject *zigbee_crypt_decrypt_ccm(PyObject *self, PyObject *args) {
202205 int sizeEncryptedData ;
203206 const char * zigbeeData ;
204207 int sizeZigbeeData ;
208+ PyObject * res ;
205209
206210 char pMIC [ZBEE_SEC_CONST_MICSIZE ];
207211 char pUnencMIC [ZBEE_SEC_CONST_MICSIZE ];
@@ -220,7 +224,7 @@ static PyObject *zigbee_crypt_decrypt_ccm(PyObject *self, PyObject *args) {
220224 PyErr_SetString (PyExc_ValueError , "incorrect key size (must be 16)" );
221225 return NULL ;
222226 }
223-
227+
224228 if (sizeNonce != ZBEE_SEC_CONST_NONCE_LEN ) {
225229 PyErr_SetString (PyExc_ValueError , "incorrect nonce size (must be 13)" );
226230 return NULL ;
@@ -233,12 +237,12 @@ static PyObject *zigbee_crypt_decrypt_ccm(PyObject *self, PyObject *args) {
233237
234238 memset (pMIC , 0 , ZBEE_SEC_CONST_MICSIZE );
235239 memcpy (pMIC , pOldMIC , sizeMIC );
236-
240+
237241 memset (pUnencMIC , 0 , ZBEE_SEC_CONST_MICSIZE );
238-
242+
239243 pUnencrypted = malloc (sizeEncryptedData );
240244 memset (pUnencrypted , 0 , sizeEncryptedData );
241-
245+
242246 /* Create the CCM* counter block A0 */
243247 memset (cipher_in , 0 , ZBEE_SEC_CONST_BLOCKSIZE );
244248 cipher_in [0 ] = ZBEE_SEC_CCM_FLAG_L ;
@@ -287,7 +291,7 @@ static PyObject *zigbee_crypt_decrypt_ccm(PyObject *self, PyObject *args) {
287291 }
288292 /* Done with the CTR Cipher. */
289293 gcry_cipher_close (cipher_hd );
290-
294+
291295 int i , j ;
292296 /* Re-open the cipher in ECB mode. */
293297 if (gcry_cipher_open (& cipher_hd , GCRY_CIPHER_AES128 , GCRY_CIPHER_MODE_ECB , 0 )) {
@@ -382,16 +386,17 @@ static PyObject *zigbee_crypt_decrypt_ccm(PyObject *self, PyObject *args) {
382386 gcry_cipher_close (cipher_hd );
383387 return NULL ;
384388 }
385-
389+
386390 gcry_cipher_close (cipher_hd );
387-
391+
388392 // now use j to indicate whether the MICs match
389393 j = 0 ;
390394 if (memcmp (cipher_out , pUnencMIC , sizeMIC ) == 0 ) {
391395 j = 1 ;
392396 }
393-
394- return Py_BuildValue ("(s#i)" , pUnencrypted , sizeEncryptedData , j );
397+ res = Py_BuildValue ("(s#i)" , pUnencrypted , sizeEncryptedData , j );
398+ free (pUnencrypted );
399+ return res ;
395400};
396401
397402static PyMethodDef zigbee_crypt_Methods [] = {
0 commit comments