fix aes encrypter stream (cng)

Wed, 04 Dec 2019 15:48:58 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 04 Dec 2019 15:48:58 +0100
changeset 690
ce253cfb9127
parent 689
b1f7d83f6e69
child 691
48ec0ab17011

fix aes encrypter stream (cng)

libidav/crypto.c file | annotate | diff | comparison | revisions
--- a/libidav/crypto.c	Wed Dec 04 15:18:15 2019 +0100
+++ b/libidav/crypto.c	Wed Dec 04 15:48:58 2019 +0100
@@ -1205,7 +1205,11 @@
     }
     
     size_t remaining = len % 16;
-    len -= remaining; 
+    len -= remaining;
+    
+    if(len > 256) {
+        len -= 16; // optimization for avoiding tmp buffer usage
+    }
     
     size_t inalloc = len;
     ULONG  inlen = 0;
@@ -1234,7 +1238,12 @@
     char *out = malloc(outalloc);
     
     // encrypt
-    if(BCryptEncrypt(enc->ctx.hKey, in, inlen, NULL, enc->ctx.pbIV, 16, out, outalloc, &outlen, BCRYPT_BLOCK_PADDING)) {
+    int flags = 0;
+    if(inlen % 16 != 0) {
+        flags = BCRYPT_BLOCK_PADDING;
+        enc->end = 1;
+    }
+    if(BCryptEncrypt(enc->ctx.hKey, in, inlen, NULL, enc->ctx.pbIV, 16, out, outalloc, &outlen, flags)) {
         fprintf(stderr, "Error: BCryptEncrypt failed\n");
     }
     

mercurial