comparison cbc_decrypt.c @ 0:d7da3b1e1540 libtomcrypt

put back the 0.95 makefile which was inadvertently merged over
author Matt Johnston <matt@ucc.asn.au>
date Mon, 31 May 2004 18:21:40 +0000
parents
children 6362d3854bb4
comparison
equal deleted inserted replaced
-1:000000000000 0:d7da3b1e1540
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_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_CBC *cbc)
16 {
17 int x, err;
18 unsigned char tmp[MAXBLOCKSIZE], tmp2[MAXBLOCKSIZE];
19
20 _ARGCHK(pt != NULL);
21 _ARGCHK(ct != NULL);
22 _ARGCHK(cbc != NULL);
23
24 /* decrypt the block from ct into tmp */
25 if ((err = cipher_is_valid(cbc->cipher)) != CRYPT_OK) {
26 return err;
27 }
28 cipher_descriptor[cbc->cipher].ecb_decrypt(ct, tmp, &cbc->key);
29
30 /* is blocklen valid? */
31 if (cbc->blocklen < 0 || cbc->blocklen > (int)sizeof(cbc->IV)) {
32 return CRYPT_INVALID_ARG;
33 }
34
35 /* xor IV against the plaintext of the previous step */
36 for (x = 0; x < cbc->blocklen; x++) {
37 /* copy CT in case ct == pt */
38 tmp2[x] = ct[x];
39
40 /* actually decrypt the byte */
41 pt[x] = tmp[x] ^ cbc->IV[x];
42 }
43
44 /* replace IV with this current ciphertext */
45 for (x = 0; x < cbc->blocklen; x++) {
46 cbc->IV[x] = tmp2[x];
47 }
48 #ifdef CLEAN_STACK
49 zeromem(tmp, sizeof(tmp));
50 zeromem(tmp2, sizeof(tmp2));
51 #endif
52 return CRYPT_OK;
53 }
54
55 #endif
56