1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 #include "tomcrypt_private.h"
4 
5 /**
6    @file lrw_setiv.c
7    LRW_MODE implementation, Set the current IV, Tom St Denis
8 */
9 
10 #ifdef LTC_LRW_MODE
11 
12 /**
13   Set the IV for LRW
14   @param IV      The IV, must be 16 octets
15   @param len     Length ... must be 16 :-)
16   @param lrw     The LRW state to update
17   @return CRYPT_OK if successful
18 */
lrw_setiv(const unsigned char * IV,unsigned long len,symmetric_LRW * lrw)19 int lrw_setiv(const unsigned char *IV, unsigned long len, symmetric_LRW *lrw)
20 {
21    int           err;
22 #ifdef LTC_LRW_TABLES
23    unsigned char T[16];
24    int           x, y;
25 #endif
26    LTC_ARGCHK(IV != NULL);
27    LTC_ARGCHK(lrw != NULL);
28 
29    if (len != 16) {
30       return CRYPT_INVALID_ARG;
31    }
32 
33    if ((err = cipher_is_valid(lrw->cipher)) != CRYPT_OK) {
34       return err;
35    }
36 
37    /* copy the IV */
38    XMEMCPY(lrw->IV, IV, 16);
39 
40    /* check if we have to actually do work */
41    if (cipher_descriptor[lrw->cipher]->accel_lrw_encrypt != NULL && cipher_descriptor[lrw->cipher]->accel_lrw_decrypt != NULL) {
42        /* we have accelerators, let's bail since they don't use lrw->pad anyways */
43        return CRYPT_OK;
44    }
45 
46 #ifdef LTC_LRW_TABLES
47    XMEMCPY(T, &lrw->PC[0][IV[0]][0], 16);
48    for (x = 1; x < 16; x++) {
49 #ifdef LTC_FAST
50        for (y = 0; y < 16; y += sizeof(LTC_FAST_TYPE)) {
51            *(LTC_FAST_TYPE_PTR_CAST(T + y)) ^= *(LTC_FAST_TYPE_PTR_CAST(&lrw->PC[x][IV[x]][y]));
52        }
53 #else
54        for (y = 0; y < 16; y++) {
55            T[y] ^= lrw->PC[x][IV[x]][y];
56        }
57 #endif
58    }
59    XMEMCPY(lrw->pad, T, 16);
60 #else
61    gcm_gf_mult(lrw->tweak, IV, lrw->pad);
62 #endif
63 
64    return CRYPT_OK;
65 }
66 
67 
68 #endif
69