annotate libtomcrypt/src/modes/ctr/ctr_encrypt.c @ 1938:77bc00dcc19f default tip main master

Bump version to 2022.82
author Matt Johnston <matt@ucc.asn.au>
date Fri, 01 Apr 2022 14:43:27 +0800
parents e9dba7abd939
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
285
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 *
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 * LibTomCrypt is a library that provides various cryptographic
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 * algorithms in a highly modular and flexible manner.
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 *
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 * The library is free for all purposes without any express
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 * guarantee it works.
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 */
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 #include "tomcrypt.h"
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 /**
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 @file ctr_encrypt.c
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 CTR implementation, encrypt data, Tom St Denis
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 */
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16
382
0cbe8f6dbf9e propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 2af22fb4e878750b88f80f90d439b316d229796f)
Matt Johnston <matt@ucc.asn.au>
parents: 285
diff changeset
17 #ifdef LTC_CTR_MODE
285
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 /**
1711
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
20 CTR encrypt software implementation
285
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 @param pt Plaintext
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 @param ct [out] Ciphertext
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 @param len Length of plaintext (octets)
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 @param ctr CTR state
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 @return CRYPT_OK if successful
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 */
1711
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
27 static int _ctr_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_CTR *ctr)
285
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 {
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 int x, err;
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 while (len) {
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 /* is the pad empty? */
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 if (ctr->padlen == ctr->blocklen) {
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 /* increment counter */
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 if (ctr->mode == CTR_COUNTER_LITTLE_ENDIAN) {
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 /* little-endian */
1435
f849a5ca2efc update to libtomcrypt 1.17 (with Dropbear changes)
Matt Johnston <matt@ucc.asn.au>
parents: 382
diff changeset
37 for (x = 0; x < ctr->ctrlen; x++) {
285
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 ctr->ctr[x] = (ctr->ctr[x] + (unsigned char)1) & (unsigned char)255;
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 if (ctr->ctr[x] != (unsigned char)0) {
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 break;
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 }
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 }
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 } else {
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 /* big-endian */
1435
f849a5ca2efc update to libtomcrypt 1.17 (with Dropbear changes)
Matt Johnston <matt@ucc.asn.au>
parents: 382
diff changeset
45 for (x = ctr->blocklen-1; x >= ctr->ctrlen; x--) {
285
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 ctr->ctr[x] = (ctr->ctr[x] + (unsigned char)1) & (unsigned char)255;
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 if (ctr->ctr[x] != (unsigned char)0) {
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 break;
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 }
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 }
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 }
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 /* encrypt it */
382
0cbe8f6dbf9e propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 2af22fb4e878750b88f80f90d439b316d229796f)
Matt Johnston <matt@ucc.asn.au>
parents: 285
diff changeset
54 if ((err = cipher_descriptor[ctr->cipher].ecb_encrypt(ctr->ctr, ctr->pad, &ctr->key)) != CRYPT_OK) {
0cbe8f6dbf9e propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 2af22fb4e878750b88f80f90d439b316d229796f)
Matt Johnston <matt@ucc.asn.au>
parents: 285
diff changeset
55 return err;
0cbe8f6dbf9e propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 2af22fb4e878750b88f80f90d439b316d229796f)
Matt Johnston <matt@ucc.asn.au>
parents: 285
diff changeset
56 }
285
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 ctr->padlen = 0;
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 }
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 #ifdef LTC_FAST
1711
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
60 if ((ctr->padlen == 0) && (len >= (unsigned long)ctr->blocklen)) {
285
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 for (x = 0; x < ctr->blocklen; x += sizeof(LTC_FAST_TYPE)) {
1471
6dba84798cd5 Update to libtomcrypt 1.18.1, merged with Dropbear changes
Matt Johnston <matt@ucc.asn.au>
parents: 1435
diff changeset
62 *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)ct + x)) = *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)pt + x)) ^
6dba84798cd5 Update to libtomcrypt 1.18.1, merged with Dropbear changes
Matt Johnston <matt@ucc.asn.au>
parents: 1435
diff changeset
63 *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)ctr->pad + x));
285
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 }
382
0cbe8f6dbf9e propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 2af22fb4e878750b88f80f90d439b316d229796f)
Matt Johnston <matt@ucc.asn.au>
parents: 285
diff changeset
65 pt += ctr->blocklen;
0cbe8f6dbf9e propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 2af22fb4e878750b88f80f90d439b316d229796f)
Matt Johnston <matt@ucc.asn.au>
parents: 285
diff changeset
66 ct += ctr->blocklen;
0cbe8f6dbf9e propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 2af22fb4e878750b88f80f90d439b316d229796f)
Matt Johnston <matt@ucc.asn.au>
parents: 285
diff changeset
67 len -= ctr->blocklen;
0cbe8f6dbf9e propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 2af22fb4e878750b88f80f90d439b316d229796f)
Matt Johnston <matt@ucc.asn.au>
parents: 285
diff changeset
68 ctr->padlen = ctr->blocklen;
0cbe8f6dbf9e propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 2af22fb4e878750b88f80f90d439b316d229796f)
Matt Johnston <matt@ucc.asn.au>
parents: 285
diff changeset
69 continue;
0cbe8f6dbf9e propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 2af22fb4e878750b88f80f90d439b316d229796f)
Matt Johnston <matt@ucc.asn.au>
parents: 285
diff changeset
70 }
1471
6dba84798cd5 Update to libtomcrypt 1.18.1, merged with Dropbear changes
Matt Johnston <matt@ucc.asn.au>
parents: 1435
diff changeset
71 #endif
382
0cbe8f6dbf9e propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 2af22fb4e878750b88f80f90d439b316d229796f)
Matt Johnston <matt@ucc.asn.au>
parents: 285
diff changeset
72 *ct++ = *pt++ ^ ctr->pad[ctr->padlen++];
0cbe8f6dbf9e propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 2af22fb4e878750b88f80f90d439b316d229796f)
Matt Johnston <matt@ucc.asn.au>
parents: 285
diff changeset
73 --len;
285
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 }
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 return CRYPT_OK;
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 }
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77
1711
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
78 /**
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
79 CTR encrypt
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
80 @param pt Plaintext
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
81 @param ct [out] Ciphertext
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
82 @param len Length of plaintext (octets)
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
83 @param ctr CTR state
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
84 @return CRYPT_OK if successful
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
85 */
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
86 int ctr_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_CTR *ctr)
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
87 {
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
88 int err, fr;
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
89
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
90 LTC_ARGCHK(pt != NULL);
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
91 LTC_ARGCHK(ct != NULL);
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
92 LTC_ARGCHK(ctr != NULL);
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
93
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
94 if ((err = cipher_is_valid(ctr->cipher)) != CRYPT_OK) {
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
95 return err;
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
96 }
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
97
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
98 /* is blocklen/padlen valid? */
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
99 if ((ctr->blocklen < 1) || (ctr->blocklen > (int)sizeof(ctr->ctr)) ||
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
100 (ctr->padlen < 0) || (ctr->padlen > (int)sizeof(ctr->pad))) {
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
101 return CRYPT_INVALID_ARG;
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
102 }
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
103
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
104 #ifdef LTC_FAST
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
105 if (ctr->blocklen % sizeof(LTC_FAST_TYPE)) {
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
106 return CRYPT_INVALID_ARG;
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
107 }
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
108 #endif
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
109
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
110 /* handle acceleration only if pad is empty, accelerator is present and length is >= a block size */
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
111 if ((cipher_descriptor[ctr->cipher].accel_ctr_encrypt != NULL) && (len >= (unsigned long)ctr->blocklen)) {
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
112 if (ctr->padlen < ctr->blocklen) {
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
113 fr = ctr->blocklen - ctr->padlen;
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
114 if ((err = _ctr_encrypt(pt, ct, fr, ctr)) != CRYPT_OK) {
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
115 return err;
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
116 }
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
117 pt += fr;
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
118 ct += fr;
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
119 len -= fr;
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
120 }
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
121
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
122 if (len >= (unsigned long)ctr->blocklen) {
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
123 if ((err = cipher_descriptor[ctr->cipher].accel_ctr_encrypt(pt, ct, len/ctr->blocklen, ctr->ctr, ctr->mode, &ctr->key)) != CRYPT_OK) {
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
124 return err;
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
125 }
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
126 pt += (len / ctr->blocklen) * ctr->blocklen;
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
127 ct += (len / ctr->blocklen) * ctr->blocklen;
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
128 len %= ctr->blocklen;
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
129 }
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
130 }
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
131
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
132 return _ctr_encrypt(pt, ct, len, ctr);
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
133 }
e9dba7abd939 Merge libtomcrypt v1.18.2
Matt Johnston <matt@ucc.asn.au>
parents: 1471
diff changeset
134
285
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
135 #endif
1b9e69c058d2 propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
136
1471
6dba84798cd5 Update to libtomcrypt 1.18.1, merged with Dropbear changes
Matt Johnston <matt@ucc.asn.au>
parents: 1435
diff changeset
137 /* ref: $Format:%D$ */
6dba84798cd5 Update to libtomcrypt 1.18.1, merged with Dropbear changes
Matt Johnston <matt@ucc.asn.au>
parents: 1435
diff changeset
138 /* git commit: $Format:%H$ */
6dba84798cd5 Update to libtomcrypt 1.18.1, merged with Dropbear changes
Matt Johnston <matt@ucc.asn.au>
parents: 1435
diff changeset
139 /* commit time: $Format:%ai$ */