MCPcopy Create free account
hub / github.com/bailey27/cppcryptfs / write_block

Function write_block

libcppcryptfs/file/cryptio.cpp:117–202  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

115}
116
117int
118write_block(CryptContext *con, unsigned char *cipher_buf, HANDLE hfile, const unsigned char *fileid, unsigned long long block, const unsigned char *ptbuf, int ptlen, EVP_CIPHER_CTX* openssl_crypt_context, const unsigned char *iv)
119{
120
121
122 long long offset = FILE_HEADER_LEN + block*CIPHER_BS;
123
124 if (!iv)
125 return -1;
126
127 OVERLAPPED ov;
128
129 if (hfile != INVALID_HANDLE_VALUE) {
130 SetOverlapped(&ov, offset);
131 }
132
133 unsigned long long be_block = MakeBigEndian(block);
134
135 unsigned char auth_data[sizeof(be_block) + FILE_ID_LEN];
136
137 memcpy(auth_data, &be_block, sizeof(be_block));
138
139 memcpy(auth_data + sizeof(be_block), fileid, FILE_ID_LEN);
140
141 unsigned char tag[BLOCK_TAG_LEN];
142
143 if (!con->GetConfig()->m_reverse) {
144 memcpy(cipher_buf, iv, BLOCK_IV_LEN);
145 } else {
146 const unsigned char* block0iv = iv;
147
148 // On a 128-bit big-endian machine, this would be the low-order 64 bits
149 // hence the name block0IVlow
150 unsigned long long block0IVlow;
151
152 static_assert(BLOCK_SIV_LEN == 16, "BLOCK_SIV_LEN != 16.");
153 static_assert(sizeof(block0IVlow) == 8, "sizeof(block0IVlow) != 8.");
154 memcpy(&block0IVlow, block0iv + 8, sizeof(block0IVlow));
155
156 block0IVlow = MakeBigEndianNative(block0IVlow);
157
158 block0IVlow += block;
159
160 block0IVlow = MakeBigEndian(block0IVlow);
161
162 memcpy(cipher_buf, block0iv, 8);
163 memcpy(cipher_buf + 8, &block0IVlow, sizeof(block0IVlow));
164
165
166 }
167
168 bool siv = con->GetConfig()->m_AESSIV;
169
170 int ctlen;
171
172 if (siv) {
173 ctlen = encrypt_siv(ptbuf, ptlen, auth_data, sizeof(auth_data),
174 cipher_buf, cipher_buf + BLOCK_IV_LEN + BLOCK_SIV_LEN, cipher_buf + BLOCK_IV_LEN, &con->m_siv);

Callers 3

WriteMethod · 0.85
SetEndOfFileMethod · 0.85
ReadMethod · 0.85

Calls 7

SetOverlappedFunction · 0.85
MakeBigEndianFunction · 0.85
MakeBigEndianNativeFunction · 0.85
encrypt_sivFunction · 0.85
encryptFunction · 0.85
GetConfigMethod · 0.80
GetGcmContentKeyMethod · 0.80

Tested by

no test coverage detected