Mercurial > dropbear
comparison src/modes/cbc/cbc_encrypt.c @ 191:1c15b283127b libtomcrypt-orig
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 06 May 2005 13:23:02 +0000 |
parents | |
children | 39d5d58461d6 |
comparison
equal
deleted
inserted
replaced
143:5d99163f7e32 | 191:1c15b283127b |
---|---|
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 "tomcrypt.h" | |
12 | |
13 /** | |
14 @file cbc_encrypt.c | |
15 CBC implementation, encrypt block, Tom St Denis | |
16 */ | |
17 | |
18 | |
19 #ifdef CBC | |
20 | |
21 /** | |
22 CBC encrypt | |
23 @param pt Plaintext | |
24 @param ct [out] Ciphertext | |
25 @param len The number of bytes to process (must be multiple of block length) | |
26 @param cbc CBC state | |
27 @return CRYPT_OK if successful | |
28 */ | |
29 int cbc_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_CBC *cbc) | |
30 { | |
31 int x, err; | |
32 | |
33 LTC_ARGCHK(pt != NULL); | |
34 LTC_ARGCHK(ct != NULL); | |
35 LTC_ARGCHK(cbc != NULL); | |
36 | |
37 if ((err = cipher_is_valid(cbc->cipher)) != CRYPT_OK) { | |
38 return err; | |
39 } | |
40 | |
41 /* is blocklen valid? */ | |
42 if (cbc->blocklen < 0 || cbc->blocklen > (int)sizeof(cbc->IV)) { | |
43 return CRYPT_INVALID_ARG; | |
44 } | |
45 | |
46 if (len % cbc->blocklen) { | |
47 return CRYPT_INVALID_ARG; | |
48 } | |
49 #ifdef LTC_FAST | |
50 if (len % sizeof(LTC_FAST_TYPE)) { | |
51 return CRYPT_INVALID_ARG; | |
52 } | |
53 #endif | |
54 | |
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); | |
57 } else { | |
58 while (len) { | |
59 /* xor IV against plaintext */ | |
60 #if defined(LTC_FAST) | |
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)); | |
63 } | |
64 #else | |
65 for (x = 0; x < cbc->blocklen; x++) { | |
66 cbc->IV[x] ^= pt[x]; | |
67 } | |
68 #endif | |
69 | |
70 /* encrypt */ | |
71 cipher_descriptor[cbc->cipher].ecb_encrypt(cbc->IV, ct, &cbc->key); | |
72 | |
73 /* store IV [ciphertext] for a future block */ | |
74 #if defined(LTC_FAST) | |
75 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)); | |
77 } | |
78 #else | |
79 for (x = 0; x < cbc->blocklen; x++) { | |
80 cbc->IV[x] = ct[x]; | |
81 } | |
82 #endif | |
83 | |
84 ct += cbc->blocklen; | |
85 pt += cbc->blocklen; | |
86 len -= cbc->blocklen; | |
87 } | |
88 } | |
89 return CRYPT_OK; | |
90 } | |
91 | |
92 #endif |