Skip to content

Commit e04d8c9

Browse files
committed
clear buffer if cipher operation fails per sqlcipher#304
1 parent 1ca606d commit e04d8c9

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/crypto.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,10 @@ static void* sqlite3Codec(void *iCtx, void *data, Pgno pgno, int mode) {
683683
memcpy(buffer, plaintext_header_sz ? pData : (void *) SQLITE_FILE_HEADER, offset);
684684

685685
rc = sqlcipher_page_cipher(ctx, cctx, pgno, CIPHER_DECRYPT, page_sz - offset, pData + offset, (unsigned char*)buffer + offset);
686-
if(rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, rc);
686+
if(rc != SQLITE_OK) { /* clear results of failed cipher operation and set error */
687+
sqlcipher_memset(buffer+offset, 0, page_sz-offset);
688+
sqlcipher_codec_ctx_set_error(ctx, rc);
689+
}
687690
memcpy(pData, buffer, page_sz); /* copy buffer data back to pData and return */
688691
return pData;
689692
break;
@@ -702,7 +705,10 @@ static void* sqlite3Codec(void *iCtx, void *data, Pgno pgno, int mode) {
702705
memcpy(buffer, plaintext_header_sz ? pData : kdf_salt, offset);
703706
}
704707
rc = sqlcipher_page_cipher(ctx, cctx, pgno, CIPHER_ENCRYPT, page_sz - offset, pData + offset, (unsigned char*)buffer + offset);
705-
if(rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, rc);
708+
if(rc != SQLITE_OK) { /* clear results of failed cipher operation and set error */
709+
sqlcipher_memset(buffer+offset, 0, page_sz-offset);
710+
sqlcipher_codec_ctx_set_error(ctx, rc);
711+
}
706712
return buffer; /* return persistent buffer data, pData remains intact */
707713
break;
708714

0 commit comments

Comments
 (0)