libidav/crypto.c

changeset 690
ce253cfb9127
parent 689
b1f7d83f6e69
child 691
48ec0ab17011
equal deleted inserted replaced
689:b1f7d83f6e69 690:ce253cfb9127
1203 if(enc->end) { 1203 if(enc->end) {
1204 return 0; 1204 return 0;
1205 } 1205 }
1206 1206
1207 size_t remaining = len % 16; 1207 size_t remaining = len % 16;
1208 len -= remaining; 1208 len -= remaining;
1209
1210 if(len > 256) {
1211 len -= 16; // optimization for avoiding tmp buffer usage
1212 }
1209 1213
1210 size_t inalloc = len; 1214 size_t inalloc = len;
1211 ULONG inlen = 0; 1215 ULONG inlen = 0;
1212 unsigned char *in = malloc(inalloc); 1216 unsigned char *in = malloc(inalloc);
1213 1217
1232 ULONG outalloc = inlen + 16; 1236 ULONG outalloc = inlen + 16;
1233 ULONG outlen = 0; 1237 ULONG outlen = 0;
1234 char *out = malloc(outalloc); 1238 char *out = malloc(outalloc);
1235 1239
1236 // encrypt 1240 // encrypt
1237 if(BCryptEncrypt(enc->ctx.hKey, in, inlen, NULL, enc->ctx.pbIV, 16, out, outalloc, &outlen, BCRYPT_BLOCK_PADDING)) { 1241 int flags = 0;
1242 if(inlen % 16 != 0) {
1243 flags = BCRYPT_BLOCK_PADDING;
1244 enc->end = 1;
1245 }
1246 if(BCryptEncrypt(enc->ctx.hKey, in, inlen, NULL, enc->ctx.pbIV, 16, out, outalloc, &outlen, flags)) {
1238 fprintf(stderr, "Error: BCryptEncrypt failed\n"); 1247 fprintf(stderr, "Error: BCryptEncrypt failed\n");
1239 } 1248 }
1240 1249
1241 // check if the output fits in buf, if not, save the remaining bytes in tmp 1250 // check if the output fits in buf, if not, save the remaining bytes in tmp
1242 if(outlen > len) { 1251 if(outlen > len) {

mercurial