Mercurial > dropbear
comparison libtomcrypt/src/modes/ctr/ctr_encrypt.c @ 1733:d529a52b2f7c coverity coverity
merge coverity from main
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 26 Jun 2020 21:07:34 +0800 |
parents | e9dba7abd939 |
children |
comparison
equal
deleted
inserted
replaced
1643:b59623a64678 | 1733:d529a52b2f7c |
---|---|
15 | 15 |
16 | 16 |
17 #ifdef LTC_CTR_MODE | 17 #ifdef LTC_CTR_MODE |
18 | 18 |
19 /** | 19 /** |
20 CTR encrypt | 20 CTR encrypt software implementation |
21 @param pt Plaintext | 21 @param pt Plaintext |
22 @param ct [out] Ciphertext | 22 @param ct [out] Ciphertext |
23 @param len Length of plaintext (octets) | 23 @param len Length of plaintext (octets) |
24 @param ctr CTR state | 24 @param ctr CTR state |
25 @return CRYPT_OK if successful | 25 @return CRYPT_OK if successful |
26 */ | 26 */ |
27 int ctr_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_CTR *ctr) | 27 static int _ctr_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_CTR *ctr) |
28 { | 28 { |
29 int x, err; | 29 int x, err; |
30 | |
31 LTC_ARGCHK(pt != NULL); | |
32 LTC_ARGCHK(ct != NULL); | |
33 LTC_ARGCHK(ctr != NULL); | |
34 | |
35 if ((err = cipher_is_valid(ctr->cipher)) != CRYPT_OK) { | |
36 return err; | |
37 } | |
38 | |
39 /* is blocklen/padlen valid? */ | |
40 if (ctr->blocklen < 1 || ctr->blocklen > (int)sizeof(ctr->ctr) || | |
41 ctr->padlen < 0 || ctr->padlen > (int)sizeof(ctr->pad)) { | |
42 return CRYPT_INVALID_ARG; | |
43 } | |
44 | |
45 #ifdef LTC_FAST | |
46 if (ctr->blocklen % sizeof(LTC_FAST_TYPE)) { | |
47 return CRYPT_INVALID_ARG; | |
48 } | |
49 #endif | |
50 | |
51 /* handle acceleration only if pad is empty, accelerator is present and length is >= a block size */ | |
52 if ((ctr->padlen == ctr->blocklen) && cipher_descriptor[ctr->cipher].accel_ctr_encrypt != NULL && (len >= (unsigned long)ctr->blocklen)) { | |
53 if ((err = cipher_descriptor[ctr->cipher].accel_ctr_encrypt(pt, ct, len/ctr->blocklen, ctr->ctr, ctr->mode, &ctr->key)) != CRYPT_OK) { | |
54 return err; | |
55 } | |
56 pt += (len / ctr->blocklen) * ctr->blocklen; | |
57 ct += (len / ctr->blocklen) * ctr->blocklen; | |
58 len %= ctr->blocklen; | |
59 } | |
60 | 30 |
61 while (len) { | 31 while (len) { |
62 /* is the pad empty? */ | 32 /* is the pad empty? */ |
63 if (ctr->padlen == ctr->blocklen) { | 33 if (ctr->padlen == ctr->blocklen) { |
64 /* increment counter */ | 34 /* increment counter */ |
85 return err; | 55 return err; |
86 } | 56 } |
87 ctr->padlen = 0; | 57 ctr->padlen = 0; |
88 } | 58 } |
89 #ifdef LTC_FAST | 59 #ifdef LTC_FAST |
90 if (ctr->padlen == 0 && len >= (unsigned long)ctr->blocklen) { | 60 if ((ctr->padlen == 0) && (len >= (unsigned long)ctr->blocklen)) { |
91 for (x = 0; x < ctr->blocklen; x += sizeof(LTC_FAST_TYPE)) { | 61 for (x = 0; x < ctr->blocklen; x += sizeof(LTC_FAST_TYPE)) { |
92 *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)ct + x)) = *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)pt + x)) ^ | 62 *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)ct + x)) = *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)pt + x)) ^ |
93 *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)ctr->pad + x)); | 63 *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)ctr->pad + x)); |
94 } | 64 } |
95 pt += ctr->blocklen; | 65 pt += ctr->blocklen; |
103 --len; | 73 --len; |
104 } | 74 } |
105 return CRYPT_OK; | 75 return CRYPT_OK; |
106 } | 76 } |
107 | 77 |
78 /** | |
79 CTR encrypt | |
80 @param pt Plaintext | |
81 @param ct [out] Ciphertext | |
82 @param len Length of plaintext (octets) | |
83 @param ctr CTR state | |
84 @return CRYPT_OK if successful | |
85 */ | |
86 int ctr_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_CTR *ctr) | |
87 { | |
88 int err, fr; | |
89 | |
90 LTC_ARGCHK(pt != NULL); | |
91 LTC_ARGCHK(ct != NULL); | |
92 LTC_ARGCHK(ctr != NULL); | |
93 | |
94 if ((err = cipher_is_valid(ctr->cipher)) != CRYPT_OK) { | |
95 return err; | |
96 } | |
97 | |
98 /* is blocklen/padlen valid? */ | |
99 if ((ctr->blocklen < 1) || (ctr->blocklen > (int)sizeof(ctr->ctr)) || | |
100 (ctr->padlen < 0) || (ctr->padlen > (int)sizeof(ctr->pad))) { | |
101 return CRYPT_INVALID_ARG; | |
102 } | |
103 | |
104 #ifdef LTC_FAST | |
105 if (ctr->blocklen % sizeof(LTC_FAST_TYPE)) { | |
106 return CRYPT_INVALID_ARG; | |
107 } | |
108 #endif | |
109 | |
110 /* handle acceleration only if pad is empty, accelerator is present and length is >= a block size */ | |
111 if ((cipher_descriptor[ctr->cipher].accel_ctr_encrypt != NULL) && (len >= (unsigned long)ctr->blocklen)) { | |
112 if (ctr->padlen < ctr->blocklen) { | |
113 fr = ctr->blocklen - ctr->padlen; | |
114 if ((err = _ctr_encrypt(pt, ct, fr, ctr)) != CRYPT_OK) { | |
115 return err; | |
116 } | |
117 pt += fr; | |
118 ct += fr; | |
119 len -= fr; | |
120 } | |
121 | |
122 if (len >= (unsigned long)ctr->blocklen) { | |
123 if ((err = cipher_descriptor[ctr->cipher].accel_ctr_encrypt(pt, ct, len/ctr->blocklen, ctr->ctr, ctr->mode, &ctr->key)) != CRYPT_OK) { | |
124 return err; | |
125 } | |
126 pt += (len / ctr->blocklen) * ctr->blocklen; | |
127 ct += (len / ctr->blocklen) * ctr->blocklen; | |
128 len %= ctr->blocklen; | |
129 } | |
130 } | |
131 | |
132 return _ctr_encrypt(pt, ct, len, ctr); | |
133 } | |
134 | |
108 #endif | 135 #endif |
109 | 136 |
110 /* ref: $Format:%D$ */ | 137 /* ref: $Format:%D$ */ |
111 /* git commit: $Format:%H$ */ | 138 /* git commit: $Format:%H$ */ |
112 /* commit time: $Format:%ai$ */ | 139 /* commit time: $Format:%ai$ */ |