Mercurial > dropbear
comparison libtomcrypt/src/modes/cbc/cbc_encrypt.c @ 398:59c7938af2bd
merge of '1250b8af44b62d8f4fe0f8d9fc7e7a1cc34e7e1c'
and '7f8670ac3bb975f40967f3979d09d2199b7e90c8'
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 03 Feb 2007 08:20:30 +0000 |
parents | 0cbe8f6dbf9e |
children | f849a5ca2efc |
comparison
equal
deleted
inserted
replaced
396:e7c1a77d2921 | 398:59c7938af2bd |
---|---|
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_encrypt.c | 14 @file cbc_encrypt.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 encrypt | 22 CBC encrypt |
23 @param pt Plaintext | 23 @param pt Plaintext |
24 @param ct [out] Ciphertext | 24 @param ct [out] Ciphertext |
37 if ((err = cipher_is_valid(cbc->cipher)) != CRYPT_OK) { | 37 if ((err = cipher_is_valid(cbc->cipher)) != CRYPT_OK) { |
38 return err; | 38 return err; |
39 } | 39 } |
40 | 40 |
41 /* is blocklen valid? */ | 41 /* is blocklen valid? */ |
42 if (cbc->blocklen < 0 || cbc->blocklen > (int)sizeof(cbc->IV)) { | 42 if (cbc->blocklen < 1 || cbc->blocklen > (int)sizeof(cbc->IV)) { |
43 return CRYPT_INVALID_ARG; | 43 return CRYPT_INVALID_ARG; |
44 } | 44 } |
45 | 45 |
46 if (len % cbc->blocklen) { | 46 if (len % cbc->blocklen) { |
47 return CRYPT_INVALID_ARG; | 47 return CRYPT_INVALID_ARG; |
48 } | 48 } |
49 #ifdef LTC_FAST | 49 #ifdef LTC_FAST |
50 if (len % sizeof(LTC_FAST_TYPE)) { | 50 if (cbc->blocklen % sizeof(LTC_FAST_TYPE)) { |
51 return CRYPT_INVALID_ARG; | 51 return CRYPT_INVALID_ARG; |
52 } | 52 } |
53 #endif | 53 #endif |
54 | 54 |
55 if (cipher_descriptor[cbc->cipher].accel_cbc_encrypt != NULL) { | 55 if (cipher_descriptor[cbc->cipher].accel_cbc_encrypt != NULL) { |
56 cipher_descriptor[cbc->cipher].accel_cbc_encrypt(pt, ct, len / cbc->blocklen, cbc->IV, &cbc->key); | 56 return cipher_descriptor[cbc->cipher].accel_cbc_encrypt(pt, ct, len / cbc->blocklen, cbc->IV, &cbc->key); |
57 } else { | 57 } else { |
58 while (len) { | 58 while (len) { |
59 /* xor IV against plaintext */ | 59 /* xor IV against plaintext */ |
60 #if defined(LTC_FAST) | 60 #if defined(LTC_FAST) |
61 for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) { | 61 for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) { |
62 *((LTC_FAST_TYPE*)((unsigned char *)cbc->IV + x)) ^= *((LTC_FAST_TYPE*)((unsigned char *)pt + x)); | 62 *((LTC_FAST_TYPE*)((unsigned char *)cbc->IV + x)) ^= *((LTC_FAST_TYPE*)((unsigned char *)pt + x)); |
63 } | 63 } |
64 #else | 64 #else |
65 for (x = 0; x < cbc->blocklen; x++) { | 65 for (x = 0; x < cbc->blocklen; x++) { |
66 cbc->IV[x] ^= pt[x]; | 66 cbc->IV[x] ^= pt[x]; |
67 } | 67 } |
68 #endif | 68 #endif |
69 | 69 |
70 /* encrypt */ | 70 /* encrypt */ |
71 cipher_descriptor[cbc->cipher].ecb_encrypt(cbc->IV, ct, &cbc->key); | 71 if ((err = cipher_descriptor[cbc->cipher].ecb_encrypt(cbc->IV, ct, &cbc->key)) != CRYPT_OK) { |
72 return err; | |
73 } | |
72 | 74 |
73 /* store IV [ciphertext] for a future block */ | 75 /* store IV [ciphertext] for a future block */ |
74 #if defined(LTC_FAST) | 76 #if defined(LTC_FAST) |
75 for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) { | 77 for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) { |
76 *((LTC_FAST_TYPE*)((unsigned char *)cbc->IV + x)) = *((LTC_FAST_TYPE*)((unsigned char *)ct + x)); | 78 *((LTC_FAST_TYPE*)((unsigned char *)cbc->IV + x)) = *((LTC_FAST_TYPE*)((unsigned char *)ct + x)); |
77 } | 79 } |
78 #else | 80 #else |
79 for (x = 0; x < cbc->blocklen; x++) { | 81 for (x = 0; x < cbc->blocklen; x++) { |
80 cbc->IV[x] = ct[x]; | 82 cbc->IV[x] = ct[x]; |
81 } | 83 } |
82 #endif | 84 #endif |
83 | 85 |
84 ct += cbc->blocklen; | 86 ct += cbc->blocklen; |
85 pt += cbc->blocklen; | 87 pt += cbc->blocklen; |
86 len -= cbc->blocklen; | 88 len -= cbc->blocklen; |
87 } | 89 } |
90 } | 92 } |
91 | 93 |
92 #endif | 94 #endif |
93 | 95 |
94 /* $Source: /cvs/libtom/libtomcrypt/src/modes/cbc/cbc_encrypt.c,v $ */ | 96 /* $Source: /cvs/libtom/libtomcrypt/src/modes/cbc/cbc_encrypt.c,v $ */ |
95 /* $Revision: 1.7 $ */ | 97 /* $Revision: 1.13 $ */ |
96 /* $Date: 2005/05/05 14:35:59 $ */ | 98 /* $Date: 2006/11/21 00:18:23 $ */ |