-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMtE.cc
45 lines (33 loc) · 1.04 KB
/
MtE.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "ciphermodes/MtE.h"
#include "mac/HMAC.h"
namespace CK {
MtE::MtE(BlockCipherMode *c, HMAC* h)
: cipher(c),
hmac(h),
authenticated(false) {
}
MtE::~MtE() {
delete hmac;
delete cipher;
}
coder::ByteArray MtE::decrypt(const coder::ByteArray& ciphertext,
const coder::ByteArray& key) {
coder::ByteArray ptm(cipher->decrypt(ciphertext, key));
unsigned digestLength = hmac->getDigestLength();
unsigned hmacOffset = ptm.getLength() - digestLength;
coder::ByteArray mac(ptm.range(hmacOffset, digestLength));
coder::ByteArray message(ptm.range(0, hmacOffset));
hmac->setKey(key);
hmac->setMessage(message);
authenticated = hmac->authenticate(mac);
return message;
}
coder::ByteArray MtE::encrypt(const coder::ByteArray& plaintext,
const coder::ByteArray& key) {
hmac->setKey(key);
hmac->setMessage(plaintext);
coder::ByteArray ptm(plaintext);
ptm.append(hmac->getHMAC());
return cipher->encrypt(ptm, key);
}
}