comparison src/modes/ctr/ctr_encrypt.c @ 380:d5faf4814ddb libtomcrypt-orig libtomcrypt-1.16

Update to LibTomCrypt 1.16
author Matt Johnston <matt@ucc.asn.au>
date Thu, 11 Jan 2007 02:22:00 +0000
parents 59400faa4b44
children
comparison
equal deleted inserted replaced
280:59400faa4b44 380:d5faf4814ddb
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 ctr_encrypt.c 14 @file ctr_encrypt.c
15 CTR implementation, encrypt data, Tom St Denis 15 CTR implementation, encrypt data, Tom St Denis
16 */ 16 */
17 17
18 18
19 #ifdef CTR 19 #ifdef LTC_CTR_MODE
20 20
21 /** 21 /**
22 CTR encrypt 22 CTR 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(ctr->cipher)) != CRYPT_OK) { 37 if ((err = cipher_is_valid(ctr->cipher)) != CRYPT_OK) {
38 return err; 38 return err;
39 } 39 }
40 40
41 /* is blocklen/padlen valid? */ 41 /* is blocklen/padlen valid? */
42 if (ctr->blocklen < 0 || ctr->blocklen > (int)sizeof(ctr->ctr) || 42 if (ctr->blocklen < 1 || ctr->blocklen > (int)sizeof(ctr->ctr) ||
43 ctr->padlen < 0 || ctr->padlen > (int)sizeof(ctr->pad)) { 43 ctr->padlen < 0 || ctr->padlen > (int)sizeof(ctr->pad)) {
44 return CRYPT_INVALID_ARG; 44 return CRYPT_INVALID_ARG;
45 } 45 }
46 46
47 #ifdef LTC_FAST 47 #ifdef LTC_FAST
50 } 50 }
51 #endif 51 #endif
52 52
53 /* handle acceleration only if pad is empty, accelerator is present and length is >= a block size */ 53 /* handle acceleration only if pad is empty, accelerator is present and length is >= a block size */
54 if ((ctr->padlen == ctr->blocklen) && cipher_descriptor[ctr->cipher].accel_ctr_encrypt != NULL && (len >= (unsigned long)ctr->blocklen)) { 54 if ((ctr->padlen == ctr->blocklen) && cipher_descriptor[ctr->cipher].accel_ctr_encrypt != NULL && (len >= (unsigned long)ctr->blocklen)) {
55 cipher_descriptor[ctr->cipher].accel_ctr_encrypt(pt, ct, len/ctr->blocklen, ctr->ctr, ctr->mode, &ctr->key); 55 if ((err = cipher_descriptor[ctr->cipher].accel_ctr_encrypt(pt, ct, len/ctr->blocklen, ctr->ctr, ctr->mode, &ctr->key)) != CRYPT_OK) {
56 return err;
57 }
56 len %= ctr->blocklen; 58 len %= ctr->blocklen;
57 } 59 }
58 60
59 while (len) { 61 while (len) {
60 /* is the pad empty? */ 62 /* is the pad empty? */
77 } 79 }
78 } 80 }
79 } 81 }
80 82
81 /* encrypt it */ 83 /* encrypt it */
82 cipher_descriptor[ctr->cipher].ecb_encrypt(ctr->ctr, ctr->pad, &ctr->key); 84 if ((err = cipher_descriptor[ctr->cipher].ecb_encrypt(ctr->ctr, ctr->pad, &ctr->key)) != CRYPT_OK) {
85 return err;
86 }
83 ctr->padlen = 0; 87 ctr->padlen = 0;
84 } 88 }
85 #ifdef LTC_FAST 89 #ifdef LTC_FAST
86 if (ctr->padlen == 0 && len >= (unsigned long)ctr->blocklen) { 90 if (ctr->padlen == 0 && len >= (unsigned long)ctr->blocklen) {
87 for (x = 0; x < ctr->blocklen; x += sizeof(LTC_FAST_TYPE)) { 91 for (x = 0; x < ctr->blocklen; x += sizeof(LTC_FAST_TYPE)) {
88 *((LTC_FAST_TYPE*)((unsigned char *)ct + x)) = *((LTC_FAST_TYPE*)((unsigned char *)pt + x)) ^ 92 *((LTC_FAST_TYPE*)((unsigned char *)ct + x)) = *((LTC_FAST_TYPE*)((unsigned char *)pt + x)) ^
89 *((LTC_FAST_TYPE*)((unsigned char *)ctr->pad + x)); 93 *((LTC_FAST_TYPE*)((unsigned char *)ctr->pad + x));
90 } 94 }
91 pt += ctr->blocklen; 95 pt += ctr->blocklen;
92 ct += ctr->blocklen; 96 ct += ctr->blocklen;
93 len -= ctr->blocklen; 97 len -= ctr->blocklen;
94 ctr->padlen = ctr->blocklen; 98 ctr->padlen = ctr->blocklen;
95 continue; 99 continue;
96 } 100 }
97 #endif 101 #endif
98 *ct++ = *pt++ ^ ctr->pad[ctr->padlen++]; 102 *ct++ = *pt++ ^ ctr->pad[ctr->padlen++];
99 --len; 103 --len;
100 } 104 }
101 return CRYPT_OK; 105 return CRYPT_OK;
102 } 106 }
103 107
104 #endif 108 #endif
105 109
106 /* $Source: /cvs/libtom/libtomcrypt/src/modes/ctr/ctr_encrypt.c,v $ */ 110 /* $Source: /cvs/libtom/libtomcrypt/src/modes/ctr/ctr_encrypt.c,v $ */
107 /* $Revision: 1.13 $ */ 111 /* $Revision: 1.20 $ */
108 /* $Date: 2005/05/05 14:35:59 $ */ 112 /* $Date: 2006/11/21 00:18:23 $ */