1
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
3
* LibTomCrypt is a library that provides various cryptographic
4
* algorithms in a highly modular and flexible manner.
6
* The library is free for all purposes without any express
9
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
15
int ctr_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_CTR *ctr)
23
if ((err = cipher_is_valid(ctr->cipher)) != CRYPT_OK) {
27
/* is blocklen/padlen valid? */
28
if (ctr->blocklen < 0 || ctr->blocklen > (int)sizeof(ctr->ctr) ||
29
ctr->padlen < 0 || ctr->padlen > (int)sizeof(ctr->pad)) {
30
return CRYPT_INVALID_ARG;
34
/* is the pad empty? */
35
if (ctr->padlen == ctr->blocklen) {
36
/* increment counter */
39
for (x = 0; x < ctr->blocklen; x++) {
40
ctr->ctr[x] = (ctr->ctr[x] + (unsigned char)1) & (unsigned char)255;
41
if (ctr->ctr[x] != (unsigned char)0) {
47
for (x = ctr->blocklen-1; x >= 0; x--) {
48
ctr->ctr[x] = (ctr->ctr[x] + (unsigned char)1) & (unsigned char)255;
49
if (ctr->ctr[x] != (unsigned char)0) {
56
cipher_descriptor[ctr->cipher].ecb_encrypt(ctr->ctr, ctr->pad, &ctr->key);
59
*ct++ = *pt++ ^ ctr->pad[ctr->padlen++];