| 37 | |
| 38 | |
| 39 | int |
| 40 | read_block(CryptContext *con, HANDLE hfile, BYTE *inputbuf, int bytesinbuf, int *bytes_consumed, const unsigned char *fileid, unsigned long long block, unsigned char *ptbuf, EVP_CIPHER_CTX* openssl_crypt_context) |
| 41 | { |
| 42 | static_assert(BLOCK_IV_LEN == BLOCK_SIV_LEN, "BLOCK_IV_LEN != BLOCK_SIV_LEN."); |
| 43 | static_assert(BLOCK_SIV_LEN == BLOCK_TAG_LEN, "BLOCK_SIV_LEN != BLOCK_TAG_LEN."); |
| 44 | |
| 45 | long long offset = FILE_HEADER_LEN + block*CIPHER_BS; |
| 46 | |
| 47 | OVERLAPPED ov; |
| 48 | |
| 49 | if (hfile != INVALID_HANDLE_VALUE) { |
| 50 | SetOverlapped(&ov, offset); |
| 51 | } |
| 52 | |
| 53 | unsigned long long be_block = MakeBigEndian(block); |
| 54 | |
| 55 | unsigned char auth_data[sizeof(be_block) + FILE_ID_LEN]; |
| 56 | |
| 57 | memcpy(auth_data, &be_block, sizeof(be_block)); |
| 58 | |
| 59 | memcpy(auth_data + sizeof(be_block), fileid, FILE_ID_LEN); |
| 60 | |
| 61 | unsigned char buf[CIPHER_BS]; |
| 62 | |
| 63 | DWORD nread = 0; |
| 64 | |
| 65 | if (hfile == INVALID_HANDLE_VALUE && inputbuf) { |
| 66 | int to_consume = min(CIPHER_BS, bytesinbuf); |
| 67 | if (bytes_consumed != NULL) |
| 68 | *bytes_consumed = to_consume; |
| 69 | nread = to_consume; |
| 70 | } else { |
| 71 | if (!ReadFile(hfile, buf, sizeof(buf), &nread, &ov)) { |
| 72 | DWORD error = GetLastError(); |
| 73 | if (error == ERROR_HANDLE_EOF) { |
| 74 | return 0; |
| 75 | } else { |
| 76 | return -1; |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | if (nread == 0) |
| 82 | return 0; |
| 83 | |
| 84 | if (nread <= CIPHER_BLOCK_OVERHEAD) { |
| 85 | SetLastError(ERROR_INVALID_DATA); |
| 86 | return -1; |
| 87 | } |
| 88 | |
| 89 | int ptlen; |
| 90 | |
| 91 | if (con->GetConfig()->m_AESSIV) { |
| 92 | ptlen = decrypt_siv((inputbuf ? inputbuf : buf) + BLOCK_IV_LEN + BLOCK_SIV_LEN, nread - BLOCK_IV_LEN - BLOCK_SIV_LEN, auth_data, sizeof(auth_data), |
| 93 | (inputbuf ? inputbuf : buf) + BLOCK_IV_LEN, (inputbuf ? inputbuf : buf), ptbuf, &con->m_siv); |
| 94 | } else { |
| 95 | ptlen = decrypt((inputbuf ? inputbuf : buf) + BLOCK_IV_LEN, nread - BLOCK_IV_LEN - BLOCK_TAG_LEN, auth_data, sizeof(auth_data), |
| 96 | (inputbuf ? inputbuf : buf) + nread - BLOCK_TAG_LEN, con->GetConfig()->GetGcmContentKey(), (inputbuf ? inputbuf : buf), ptbuf, openssl_crypt_context); |
no test coverage detected