]> pd.if.org Git - zpackage/blob - libtomcrypt/src/modes/lrw/lrw_setiv.c
remove gmp math descriptor
[zpackage] / libtomcrypt / src / modes / lrw / lrw_setiv.c
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2  *
3  * LibTomCrypt is a library that provides various cryptographic
4  * algorithms in a highly modular and flexible manner.
5  *
6  * The library is free for all purposes without any express
7  * guarantee it works.
8  */
9 #include "tomcrypt.h"
10
11 /**
12    @file lrw_setiv.c
13    LRW_MODE implementation, Set the current IV, Tom St Denis
14 */
15
16 #ifdef LTC_LRW_MODE
17
18 /**
19   Set the IV for LRW
20   @param IV      The IV, must be 16 octets
21   @param len     Length ... must be 16 :-)
22   @param lrw     The LRW state to update
23   @return CRYPT_OK if successful
24 */
25 int lrw_setiv(const unsigned char *IV, unsigned long len, symmetric_LRW *lrw)
26 {
27    int           err;
28 #ifdef LTC_LRW_TABLES
29    unsigned char T[16];
30    int           x, y;
31 #endif
32    LTC_ARGCHK(IV != NULL);
33    LTC_ARGCHK(lrw != NULL);
34
35    if (len != 16) {
36       return CRYPT_INVALID_ARG;
37    }
38
39    if ((err = cipher_is_valid(lrw->cipher)) != CRYPT_OK) {
40       return err;
41    }
42
43    /* copy the IV */
44    XMEMCPY(lrw->IV, IV, 16);
45
46    /* check if we have to actually do work */
47    if (cipher_descriptor[lrw->cipher].accel_lrw_encrypt != NULL && cipher_descriptor[lrw->cipher].accel_lrw_decrypt != NULL) {
48        /* we have accelerators, let's bail since they don't use lrw->pad anyways */
49        return CRYPT_OK;
50    }
51
52 #ifdef LTC_LRW_TABLES
53    XMEMCPY(T, &lrw->PC[0][IV[0]][0], 16);
54    for (x = 1; x < 16; x++) {
55 #ifdef LTC_FAST
56        for (y = 0; y < 16; y += sizeof(LTC_FAST_TYPE)) {
57            *(LTC_FAST_TYPE_PTR_CAST(T + y)) ^= *(LTC_FAST_TYPE_PTR_CAST(&lrw->PC[x][IV[x]][y]));
58        }
59 #else
60        for (y = 0; y < 16; y++) {
61            T[y] ^= lrw->PC[x][IV[x]][y];
62        }
63 #endif
64    }
65    XMEMCPY(lrw->pad, T, 16);
66 #else
67    gcm_gf_mult(lrw->tweak, IV, lrw->pad);
68 #endif
69
70    return CRYPT_OK;
71 }
72
73
74 #endif
75 /* ref:         $Format:%D$ */
76 /* git commit:  $Format:%H$ */
77 /* commit time: $Format:%ai$ */