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
13 CTR implementation, start chain, Tom St Denis
20 Initialize a CTR context
21 @param cipher The index of the cipher desired
22 @param IV The initialization vector
23 @param key The secret key
24 @param keylen The length of the secret key (octets)
25 @param num_rounds Number of rounds in the cipher desired (0 for default)
26 @param ctr_mode The counter mode (CTR_COUNTER_LITTLE_ENDIAN or CTR_COUNTER_BIG_ENDIAN)
27 @param ctr The CTR state to initialize
28 @return CRYPT_OK if successful
30 int ctr_start( int cipher,
31 const unsigned char *IV,
32 const unsigned char *key, int keylen,
33 int num_rounds, int ctr_mode,
38 LTC_ARGCHK(IV != NULL);
39 LTC_ARGCHK(key != NULL);
40 LTC_ARGCHK(ctr != NULL);
43 if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
47 /* ctrlen == counter width */
48 ctr->ctrlen = (ctr_mode & 255) ? (ctr_mode & 255) : cipher_descriptor[cipher].block_length;
49 if (ctr->ctrlen > cipher_descriptor[cipher].block_length) {
50 return CRYPT_INVALID_ARG;
53 if ((ctr_mode & 0x1000) == CTR_COUNTER_BIG_ENDIAN) {
54 ctr->ctrlen = cipher_descriptor[cipher].block_length - ctr->ctrlen;
58 if ((err = cipher_descriptor[cipher].setup(key, keylen, num_rounds, &ctr->key)) != CRYPT_OK) {
63 ctr->blocklen = cipher_descriptor[cipher].block_length;
66 ctr->mode = ctr_mode & 0x1000;
67 for (x = 0; x < ctr->blocklen; x++) {
71 if (ctr_mode & LTC_CTR_RFC3686) {
72 /* increment the IV as per RFC 3686 */
73 if (ctr->mode == CTR_COUNTER_LITTLE_ENDIAN) {
75 for (x = 0; x < ctr->ctrlen; x++) {
76 ctr->ctr[x] = (ctr->ctr[x] + (unsigned char)1) & (unsigned char)255;
77 if (ctr->ctr[x] != (unsigned char)0) {
83 for (x = ctr->blocklen-1; x >= ctr->ctrlen; x--) {
84 ctr->ctr[x] = (ctr->ctr[x] + (unsigned char)1) & (unsigned char)255;
85 if (ctr->ctr[x] != (unsigned char)0) {
92 return cipher_descriptor[ctr->cipher].ecb_encrypt(ctr->ctr, ctr->pad, &ctr->key);
97 /* ref: $Format:%D$ */
98 /* git commit: $Format:%H$ */
99 /* commit time: $Format:%ai$ */