1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 #include "tomcrypt_private.h"
4
5 /**
6 @file ctr_setiv.c
7 CTR implementation, set IV, Tom St Denis
8 */
9
10 #ifdef LTC_CTR_MODE
11
12 /**
13 Set an initialization vector
14 @param IV The initialization vector
15 @param len The length of the vector (in octets)
16 @param ctr The CTR state
17 @return CRYPT_OK if successful
18 */
ctr_setiv(const unsigned char * IV,unsigned long len,symmetric_CTR * ctr)19 int ctr_setiv(const unsigned char *IV, unsigned long len, symmetric_CTR *ctr)
20 {
21 int err;
22
23 LTC_ARGCHK(IV != NULL);
24 LTC_ARGCHK(ctr != NULL);
25
26 /* bad param? */
27 if ((err = cipher_is_valid(ctr->cipher)) != CRYPT_OK) {
28 return err;
29 }
30
31 if (len != (unsigned long)ctr->blocklen) {
32 return CRYPT_INVALID_ARG;
33 }
34
35 /* set IV */
36 XMEMCPY(ctr->ctr, IV, len);
37
38 /* force next block */
39 ctr->padlen = 0;
40 return cipher_descriptor[ctr->cipher]->ecb_encrypt(IV, ctr->pad, &ctr->key);
41 }
42
43 #endif
44
45