Transform - EME-encrypt or EME-decrypt, according to "direction" (defined in the constants directionEncrypt and directionDecrypt). The data in "P" is en- or decrypted with the block ciper "bc" under tweak "T". The result is returned in a freshly allocated slice.
| 217 | // The data in "P" is en- or decrypted with the block ciper "bc" under tweak "T". |
| 218 | // The result is returned in a freshly allocated slice. |
| 219 | bool EmeTransform(const EmeCryptContext *eme_context, const BYTE *T, const BYTE *P, int len, bool direction, EmeBuffer_t& buffer) { |
| 220 | |
| 221 | BYTE *C = NULL; |
| 222 | |
| 223 | bool error = false; |
| 224 | |
| 225 | try { |
| 226 | if (len % 16 != 0) { |
| 227 | panic(L"Data length is not a multiple of 16"); |
| 228 | } |
| 229 | int m = len / 16; |
| 230 | if (m == 0 || m > 16 * 8) { |
| 231 | panic(L"EME operates on 1-128 block-cipher blocks"); |
| 232 | } |
| 233 | |
| 234 | C = buffer.get(len+1); // +1 so caller can add a null terminator if necessary without any trouble |
| 235 | |
| 236 | BYTE **LTable = eme_context->m_LTable; |
| 237 | |
| 238 | BYTE PPj[16]; |
| 239 | |
| 240 | for (int j = 0; j < m; j++) { |
| 241 | BYTE Pj[16]; |
| 242 | memcpy(Pj, P + j * 16, 16); |
| 243 | /* PPj = 2**(j-1)*L xor Pj */ |
| 244 | xorBlocks(PPj, Pj, LTable[j], 16); |
| 245 | /* PPPj = AESenc(K; PPj) */ |
| 246 | aesTransform(C + j * 16, PPj, direction, 16, eme_context); |
| 247 | } |
| 248 | |
| 249 | /* MP =(xorSum PPPj) xor T */ |
| 250 | BYTE MP[16]; |
| 251 | xorBlocks(MP, C, T, 16); |
| 252 | for (int j = 1; j < m; j++) { |
| 253 | xorBlocks(MP, MP, C + j * 16, 16); |
| 254 | } |
| 255 | |
| 256 | /* MC = AESenc(K; MP) */ |
| 257 | BYTE MC[16]; |
| 258 | aesTransform(MC, MP, direction, 16, eme_context); |
| 259 | |
| 260 | /* M = MP xor MC */ |
| 261 | BYTE M[16]; |
| 262 | xorBlocks(M, MP, MC, 16); |
| 263 | BYTE CCCj[16]; |
| 264 | for (int j = 1; j < m; j++) { |
| 265 | multByTwo(M, M, 16); |
| 266 | /* CCCj = 2**(j-1)*M xor PPPj */ |
| 267 | xorBlocks(CCCj, C + j * 16, M, 16); |
| 268 | memcpy(C + j * 16, CCCj, 16); |
| 269 | } |
| 270 | |
| 271 | /* CCC1 = (xorSum CCCj) xor T xor MC */ |
| 272 | |
| 273 | BYTE CCC1[16]; |
| 274 | xorBlocks(CCC1, MC, T, 16); |
| 275 | for (int j = 1; j < m; j++) { |
| 276 | xorBlocks(CCC1, CCC1, C + j * 16, 16); |
no test coverage detected