comparison libtomcrypt/src/modes/ctr/ctr_encrypt.c @ 1471:6dba84798cd5

Update to libtomcrypt 1.18.1, merged with Dropbear changes
author Matt Johnston <matt@ucc.asn.au>
date Fri, 09 Feb 2018 21:44:05 +0800
parents f849a5ca2efc
children e9dba7abd939
comparison
equal deleted inserted replaced
1470:8bba51a55704 1471:6dba84798cd5
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 ctr_encrypt.c 12 @file ctr_encrypt.c
35 LTC_ARGCHK(ctr != NULL); 33 LTC_ARGCHK(ctr != NULL);
36 34
37 if ((err = cipher_is_valid(ctr->cipher)) != CRYPT_OK) { 35 if ((err = cipher_is_valid(ctr->cipher)) != CRYPT_OK) {
38 return err; 36 return err;
39 } 37 }
40 38
41 /* is blocklen/padlen valid? */ 39 /* is blocklen/padlen valid? */
42 if (ctr->blocklen < 1 || ctr->blocklen > (int)sizeof(ctr->ctr) || 40 if (ctr->blocklen < 1 || ctr->blocklen > (int)sizeof(ctr->ctr) ||
43 ctr->padlen < 0 || ctr->padlen > (int)sizeof(ctr->pad)) { 41 ctr->padlen < 0 || ctr->padlen > (int)sizeof(ctr->pad)) {
44 return CRYPT_INVALID_ARG; 42 return CRYPT_INVALID_ARG;
45 } 43 }
47 #ifdef LTC_FAST 45 #ifdef LTC_FAST
48 if (ctr->blocklen % sizeof(LTC_FAST_TYPE)) { 46 if (ctr->blocklen % sizeof(LTC_FAST_TYPE)) {
49 return CRYPT_INVALID_ARG; 47 return CRYPT_INVALID_ARG;
50 } 48 }
51 #endif 49 #endif
52 50
53 /* handle acceleration only if pad is empty, accelerator is present and length is >= a block size */ 51 /* 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)) { 52 if ((ctr->padlen == ctr->blocklen) && cipher_descriptor[ctr->cipher].accel_ctr_encrypt != NULL && (len >= (unsigned long)ctr->blocklen)) {
55 if ((err = cipher_descriptor[ctr->cipher].accel_ctr_encrypt(pt, ct, len/ctr->blocklen, ctr->ctr, ctr->mode, &ctr->key)) != CRYPT_OK) { 53 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; 54 return err;
57 } 55 }
56 pt += (len / ctr->blocklen) * ctr->blocklen;
57 ct += (len / ctr->blocklen) * ctr->blocklen;
58 len %= ctr->blocklen; 58 len %= ctr->blocklen;
59 } 59 }
60 60
61 while (len) { 61 while (len) {
62 /* is the pad empty? */ 62 /* is the pad empty? */
87 ctr->padlen = 0; 87 ctr->padlen = 0;
88 } 88 }
89 #ifdef LTC_FAST 89 #ifdef LTC_FAST
90 if (ctr->padlen == 0 && len >= (unsigned long)ctr->blocklen) { 90 if (ctr->padlen == 0 && len >= (unsigned long)ctr->blocklen) {
91 for (x = 0; x < ctr->blocklen; x += sizeof(LTC_FAST_TYPE)) { 91 for (x = 0; x < ctr->blocklen; x += sizeof(LTC_FAST_TYPE)) {
92 *((LTC_FAST_TYPE*)((unsigned char *)ct + x)) = *((LTC_FAST_TYPE*)((unsigned char *)pt + x)) ^ 92 *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)ct + x)) = *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)pt + x)) ^
93 *((LTC_FAST_TYPE*)((unsigned char *)ctr->pad + x)); 93 *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)ctr->pad + x));
94 } 94 }
95 pt += ctr->blocklen; 95 pt += ctr->blocklen;
96 ct += ctr->blocklen; 96 ct += ctr->blocklen;
97 len -= ctr->blocklen; 97 len -= ctr->blocklen;
98 ctr->padlen = ctr->blocklen; 98 ctr->padlen = ctr->blocklen;
99 continue; 99 continue;
100 } 100 }
101 #endif 101 #endif
102 *ct++ = *pt++ ^ ctr->pad[ctr->padlen++]; 102 *ct++ = *pt++ ^ ctr->pad[ctr->padlen++];
103 --len; 103 --len;
104 } 104 }
105 return CRYPT_OK; 105 return CRYPT_OK;
106 } 106 }
107 107
108 #endif 108 #endif
109 109
110 /* $Source$ */ 110 /* ref: $Format:%D$ */
111 /* $Revision$ */ 111 /* git commit: $Format:%H$ */
112 /* $Date$ */ 112 /* commit time: $Format:%ai$ */