Fix memleak with repeated [gc]cm_setkey()
diff --git a/ChangeLog b/ChangeLog
index d44463f..160c0fb 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -35,6 +35,8 @@
      errors on use of deprecated functions.
 
 Bugfix
+   * Fix memory leak when gcm_setkey() and ccm_setkey() are used more than
+     once on the same context.
    * Fix bug in ssl_mail_client when password is longer that username (found
      by Bruno Pape).
    * Fix undefined behaviour (memcmp( NULL, NULL, 0 );) in X.509 modules
diff --git a/library/ccm.c b/library/ccm.c
index 87f1886..e397e0a 100644
--- a/library/ccm.c
+++ b/library/ccm.c
@@ -78,6 +78,8 @@
     if( cipher_info->block_size != 16 )
         return( POLARSSL_ERR_CCM_BAD_INPUT );
 
+    cipher_free( &ctx->cipher_ctx );
+
     if( ( ret = cipher_init_ctx( &ctx->cipher_ctx, cipher_info ) ) != 0 )
         return( ret );
 
diff --git a/library/gcm.c b/library/gcm.c
index f4f735b..b537b02 100644
--- a/library/gcm.c
+++ b/library/gcm.c
@@ -168,6 +168,8 @@
     if( cipher_info->block_size != 16 )
         return( POLARSSL_ERR_GCM_BAD_INPUT );
 
+    cipher_free( &ctx->cipher_ctx );
+
     if( ( ret = cipher_init_ctx( &ctx->cipher_ctx, cipher_info ) ) != 0 )
         return( ret );