1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3  *
4  * LibTomCrypt is a library that provides various cryptographic
5  * algorithms in a highly modular and flexible manner.
6  *
7  * The library is free for all purposes without any express
8  * guarantee it works.
9  */
10 #include "tomcrypt_private.h"
11 
12 /**
13   @file ctr_setiv.c
14   CTR implementation, set IV, Tom St Denis
15 */
16 
17 #ifdef LTC_CTR_MODE
18 
19 /**
20    Set an initialization vector
21    @param IV   The initialization vector
22    @param len  The length of the vector (in octets)
23    @param ctr  The CTR state
24    @return CRYPT_OK if successful
25 */
ctr_setiv(const unsigned char * IV,unsigned long len,symmetric_CTR * ctr)26 int ctr_setiv(const unsigned char *IV, unsigned long len, symmetric_CTR *ctr)
27 {
28    int err;
29 
30    LTC_ARGCHK(IV  != NULL);
31    LTC_ARGCHK(ctr != NULL);
32 
33    /* bad param? */
34    if ((err = cipher_is_valid(ctr->cipher)) != CRYPT_OK) {
35       return err;
36    }
37 
38    if (len != (unsigned long)ctr->blocklen) {
39       return CRYPT_INVALID_ARG;
40    }
41 
42    /* set IV */
43    XMEMCPY(ctr->ctr, IV, len);
44 
45    /* force next block */
46    ctr->padlen = 0;
47    return cipher_descriptor[ctr->cipher]->ecb_encrypt(IV, ctr->pad, &ctr->key);
48 }
49 
50 #endif
51 
52 
53 /* ref:         $Format:%D$ */
54 /* git commit:  $Format:%H$ */
55 /* commit time: $Format:%ai$ */
56