+
+void AES::encryptCTR(const uint8_t *in, uint8_t *out, int length)
+{
+ int i;
+ uint32_t tin[4], tout[4], iv[4];
+
+ memcpy(iv, _iv, AES_IV_SIZE);
+ for (i = 0; i < 4; i++)
+ tout[i] = crypto_ntohl(iv[i]);
+
+ for (length -= AES_BLOCKSIZE; length >= 0; length -= AES_BLOCKSIZE)
+ {
+ uint32_t msg_32[4];
+ uint32_t out_32[4];
+ memcpy(msg_32, in, AES_BLOCKSIZE);
+ in += AES_BLOCKSIZE;
+
+ for (i = 0; i < 4; i++)
+ tin[i] = crypto_ntohl(tout[i]);
+
+ AES::encrypt(tin);
+
+ for (i = 0; i < 4; i++)
+ {
+
+ out_32[i] = crypto_htonl(tin[i])^msg_32[i];
+ }
+ // tout[0]= tout[0]+1;
+
+ memcpy(out, out_32, AES_BLOCKSIZE);
+ out += AES_BLOCKSIZE;
+ }
+
+ for (i = 0; i < 4; i++)
+ iv[i] = crypto_htonl(tout[i])+1;
+ memcpy(_iv, iv, AES_IV_SIZE);
+}
+
+
+