3
|
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis |
|
2 * |
|
3 * LibTomCrypt is a library that provides various cryptographic |
|
4 * algorithms in a highly modular and flexible manner. |
|
5 * |
|
6 * The library is free for all purposes without any express |
|
7 * guarantee it works. |
|
8 * |
|
9 * Tom St Denis, [email protected], http://libtomcrypt.org |
|
10 */ |
|
11 #include "mycrypt.h" |
|
12 |
|
13 #ifdef CBC |
|
14 |
|
15 int cbc_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_CBC *cbc) |
|
16 { |
|
17 int x, err; |
|
18 unsigned char tmp[MAXBLOCKSIZE]; |
|
19 |
|
20 _ARGCHK(pt != NULL); |
|
21 _ARGCHK(ct != NULL); |
|
22 _ARGCHK(cbc != NULL); |
|
23 |
|
24 if ((err = cipher_is_valid(cbc->cipher)) != CRYPT_OK) { |
|
25 return err; |
|
26 } |
|
27 |
|
28 /* is blocklen valid? */ |
|
29 if (cbc->blocklen < 0 || cbc->blocklen > (int)sizeof(cbc->IV)) { |
|
30 return CRYPT_INVALID_ARG; |
|
31 } |
|
32 |
|
33 /* xor IV against plaintext */ |
|
34 for (x = 0; x < cbc->blocklen; x++) { |
|
35 tmp[x] = pt[x] ^ cbc->IV[x]; |
|
36 } |
|
37 |
|
38 /* encrypt */ |
|
39 cipher_descriptor[cbc->cipher].ecb_encrypt(tmp, ct, &cbc->key); |
|
40 |
|
41 /* store IV [ciphertext] for a future block */ |
|
42 for (x = 0; x < cbc->blocklen; x++) { |
|
43 cbc->IV[x] = ct[x]; |
|
44 } |
|
45 |
|
46 #ifdef CLEAN_STACK |
|
47 zeromem(tmp, sizeof(tmp)); |
|
48 #endif |
|
49 return CRYPT_OK; |
|
50 } |
|
51 |
|
52 #endif |