Mercurial > dropbear
comparison libtomcrypt/src/modes/cbc/cbc_encrypt.c @ 1511:5916af64acd4 fuzz
merge from main
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 17 Feb 2018 19:29:51 +0800 |
parents | 6dba84798cd5 |
children |
comparison
equal
deleted
inserted
replaced
1457:32f990cc96b1 | 1511:5916af64acd4 |
---|---|
3 * LibTomCrypt is a library that provides various cryptographic | 3 * LibTomCrypt is a library that provides various cryptographic |
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 * | |
9 * Tom St Denis, [email protected], http://libtom.org | |
10 */ | 8 */ |
11 #include "tomcrypt.h" | 9 #include "tomcrypt.h" |
12 | 10 |
13 /** | 11 /** |
14 @file cbc_encrypt.c | 12 @file cbc_encrypt.c |
35 LTC_ARGCHK(cbc != NULL); | 33 LTC_ARGCHK(cbc != NULL); |
36 | 34 |
37 if ((err = cipher_is_valid(cbc->cipher)) != CRYPT_OK) { | 35 if ((err = cipher_is_valid(cbc->cipher)) != CRYPT_OK) { |
38 return err; | 36 return err; |
39 } | 37 } |
40 | 38 |
41 /* is blocklen valid? */ | 39 /* is blocklen valid? */ |
42 if (cbc->blocklen < 1 || cbc->blocklen > (int)sizeof(cbc->IV)) { | 40 if (cbc->blocklen < 1 || cbc->blocklen > (int)sizeof(cbc->IV)) { |
43 return CRYPT_INVALID_ARG; | 41 return CRYPT_INVALID_ARG; |
44 } | 42 } |
45 | 43 |
46 if (len % cbc->blocklen) { | 44 if (len % cbc->blocklen) { |
47 return CRYPT_INVALID_ARG; | 45 return CRYPT_INVALID_ARG; |
48 } | 46 } |
49 #ifdef LTC_FAST | 47 #ifdef LTC_FAST |
50 if (cbc->blocklen % sizeof(LTC_FAST_TYPE)) { | 48 if (cbc->blocklen % sizeof(LTC_FAST_TYPE)) { |
51 return CRYPT_INVALID_ARG; | 49 return CRYPT_INVALID_ARG; |
52 } | 50 } |
53 #endif | 51 #endif |
54 | 52 |
55 if (cipher_descriptor[cbc->cipher].accel_cbc_encrypt != NULL) { | 53 if (cipher_descriptor[cbc->cipher].accel_cbc_encrypt != NULL) { |
56 return cipher_descriptor[cbc->cipher].accel_cbc_encrypt(pt, ct, len / cbc->blocklen, cbc->IV, &cbc->key); | 54 return cipher_descriptor[cbc->cipher].accel_cbc_encrypt(pt, ct, len / cbc->blocklen, cbc->IV, &cbc->key); |
57 } else { | 55 } else { |
58 while (len) { | 56 while (len) { |
59 /* xor IV against plaintext */ | 57 /* xor IV against plaintext */ |
60 #if defined(LTC_FAST) | 58 #if defined(LTC_FAST) |
61 for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) { | 59 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)); | 60 *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)cbc->IV + x)) ^= *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)pt + x)); |
63 } | 61 } |
64 #else | 62 #else |
65 for (x = 0; x < cbc->blocklen; x++) { | 63 for (x = 0; x < cbc->blocklen; x++) { |
66 cbc->IV[x] ^= pt[x]; | 64 cbc->IV[x] ^= pt[x]; |
67 } | 65 } |
68 #endif | 66 #endif |
69 | 67 |
70 /* encrypt */ | 68 /* encrypt */ |
71 if ((err = cipher_descriptor[cbc->cipher].ecb_encrypt(cbc->IV, ct, &cbc->key)) != CRYPT_OK) { | 69 if ((err = cipher_descriptor[cbc->cipher].ecb_encrypt(cbc->IV, ct, &cbc->key)) != CRYPT_OK) { |
72 return err; | 70 return err; |
73 } | 71 } |
74 | 72 |
75 /* store IV [ciphertext] for a future block */ | 73 /* store IV [ciphertext] for a future block */ |
76 #if defined(LTC_FAST) | 74 #if defined(LTC_FAST) |
77 for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) { | 75 for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) { |
78 *((LTC_FAST_TYPE*)((unsigned char *)cbc->IV + x)) = *((LTC_FAST_TYPE*)((unsigned char *)ct + x)); | 76 *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)cbc->IV + x)) = *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)ct + x)); |
79 } | 77 } |
80 #else | 78 #else |
81 for (x = 0; x < cbc->blocklen; x++) { | 79 for (x = 0; x < cbc->blocklen; x++) { |
82 cbc->IV[x] = ct[x]; | 80 cbc->IV[x] = ct[x]; |
83 } | 81 } |
84 #endif | 82 #endif |
85 | 83 |
86 ct += cbc->blocklen; | 84 ct += cbc->blocklen; |
87 pt += cbc->blocklen; | 85 pt += cbc->blocklen; |
88 len -= cbc->blocklen; | 86 len -= cbc->blocklen; |
89 } | 87 } |
90 } | 88 } |
91 return CRYPT_OK; | 89 return CRYPT_OK; |
92 } | 90 } |
93 | 91 |
94 #endif | 92 #endif |
95 | 93 |
96 /* $Source$ */ | 94 /* ref: $Format:%D$ */ |
97 /* $Revision$ */ | 95 /* git commit: $Format:%H$ */ |
98 /* $Date$ */ | 96 /* commit time: $Format:%ai$ */ |