comparison libtomcrypt/src/modes/cbc/cbc_decrypt.c @ 382:0cbe8f6dbf9e

propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 2af22fb4e878750b88f80f90d439b316d229796f) to branch 'au.asn.ucc.matt.dropbear' (head 02c413252c90e9de8e03d91e9939dde3029f5c0a)
author Matt Johnston <matt@ucc.asn.au>
date Thu, 11 Jan 2007 02:41:05 +0000
parents 1b9e69c058d2
children f849a5ca2efc
comparison
equal deleted inserted replaced
379:b66a00272a90 382:0cbe8f6dbf9e
4 * algorithms in a highly modular and flexible manner. 4 * algorithms in a highly modular and flexible manner.
5 * 5 *
6 * The library is free for all purposes without any express 6 * The library is free for all purposes without any express
7 * guarantee it works. 7 * guarantee it works.
8 * 8 *
9 * Tom St Denis, [email protected], http://libtomcrypt.org 9 * Tom St Denis, [email protected], http://libtomcrypt.com
10 */ 10 */
11 #include "tomcrypt.h" 11 #include "tomcrypt.h"
12 12
13 /** 13 /**
14 @file cbc_decrypt.c 14 @file cbc_decrypt.c
15 CBC implementation, encrypt block, Tom St Denis 15 CBC implementation, encrypt block, Tom St Denis
16 */ 16 */
17 17
18 18
19 #ifdef CBC 19 #ifdef LTC_CBC_MODE
20 20
21 /** 21 /**
22 CBC decrypt 22 CBC decrypt
23 @param ct Ciphertext 23 @param ct Ciphertext
24 @param pt [out] Plaintext 24 @param pt [out] Plaintext
43 if ((err = cipher_is_valid(cbc->cipher)) != CRYPT_OK) { 43 if ((err = cipher_is_valid(cbc->cipher)) != CRYPT_OK) {
44 return err; 44 return err;
45 } 45 }
46 46
47 /* is blocklen valid? */ 47 /* is blocklen valid? */
48 if (cbc->blocklen < 0 || cbc->blocklen > (int)sizeof(cbc->IV)) { 48 if (cbc->blocklen < 1 || cbc->blocklen > (int)sizeof(cbc->IV)) {
49 return CRYPT_INVALID_ARG; 49 return CRYPT_INVALID_ARG;
50 } 50 }
51 51
52 if (len % cbc->blocklen) { 52 if (len % cbc->blocklen) {
53 return CRYPT_INVALID_ARG; 53 return CRYPT_INVALID_ARG;
54 } 54 }
55 #ifdef LTC_FAST 55 #ifdef LTC_FAST
56 if (len % sizeof(LTC_FAST_TYPE)) { 56 if (cbc->blocklen % sizeof(LTC_FAST_TYPE)) {
57 return CRYPT_INVALID_ARG; 57 return CRYPT_INVALID_ARG;
58 } 58 }
59 #endif 59 #endif
60 60
61 if (cipher_descriptor[cbc->cipher].accel_cbc_decrypt != NULL) { 61 if (cipher_descriptor[cbc->cipher].accel_cbc_decrypt != NULL) {
62 cipher_descriptor[cbc->cipher].accel_cbc_decrypt(ct, pt, len / cbc->blocklen, cbc->IV, &cbc->key); 62 return cipher_descriptor[cbc->cipher].accel_cbc_decrypt(ct, pt, len / cbc->blocklen, cbc->IV, &cbc->key);
63 } else { 63 } else {
64 while (len) { 64 while (len) {
65 /* decrypt */ 65 /* decrypt */
66 cipher_descriptor[cbc->cipher].ecb_decrypt(ct, tmp, &cbc->key); 66 if ((err = cipher_descriptor[cbc->cipher].ecb_decrypt(ct, tmp, &cbc->key)) != CRYPT_OK) {
67 return err;
68 }
67 69
68 /* xor IV against plaintext */ 70 /* xor IV against plaintext */
69 #if defined(LTC_FAST) 71 #if defined(LTC_FAST)
70 for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) { 72 for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) {
71 tmpy = *((LTC_FAST_TYPE*)((unsigned char *)cbc->IV + x)) ^ *((LTC_FAST_TYPE*)((unsigned char *)tmp + x)); 73 tmpy = *((LTC_FAST_TYPE*)((unsigned char *)cbc->IV + x)) ^ *((LTC_FAST_TYPE*)((unsigned char *)tmp + x));
72 *((LTC_FAST_TYPE*)((unsigned char *)cbc->IV + x)) = *((LTC_FAST_TYPE*)((unsigned char *)ct + x)); 74 *((LTC_FAST_TYPE*)((unsigned char *)cbc->IV + x)) = *((LTC_FAST_TYPE*)((unsigned char *)ct + x));
73 *((LTC_FAST_TYPE*)((unsigned char *)pt + x)) = tmpy; 75 *((LTC_FAST_TYPE*)((unsigned char *)pt + x)) = tmpy;
74 } 76 }
75 #else 77 #else
76 for (x = 0; x < cbc->blocklen; x++) { 78 for (x = 0; x < cbc->blocklen; x++) {
77 tmpy = tmp[x] ^ cbc->IV[x]; 79 tmpy = tmp[x] ^ cbc->IV[x];
78 cbc->IV[x] = ct[x]; 80 cbc->IV[x] = ct[x];
79 pt[x] = tmpy; 81 pt[x] = tmpy;
80 } 82 }
81 #endif 83 #endif
82 84
83 ct += cbc->blocklen; 85 ct += cbc->blocklen;
84 pt += cbc->blocklen; 86 pt += cbc->blocklen;
85 len -= cbc->blocklen; 87 len -= cbc->blocklen;
86 } 88 }
89 } 91 }
90 92
91 #endif 93 #endif
92 94
93 /* $Source: /cvs/libtom/libtomcrypt/src/modes/cbc/cbc_decrypt.c,v $ */ 95 /* $Source: /cvs/libtom/libtomcrypt/src/modes/cbc/cbc_decrypt.c,v $ */
94 /* $Revision: 1.9 $ */ 96 /* $Revision: 1.15 $ */
95 /* $Date: 2005/05/05 14:35:59 $ */ 97 /* $Date: 2006/11/21 00:18:23 $ */