]> pd.if.org Git - zpackage/blob - libtomcrypt/src/modes/lrw/lrw_start.c
e13d3bdd3624c28409033d3387bac8367217f4df
[zpackage] / libtomcrypt / src / modes / lrw / lrw_start.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_start.c
13    LRW_MODE implementation, start mode, Tom St Denis
14 */
15
16 #ifdef LTC_LRW_MODE
17
18 /**
19   Initialize the LRW context
20   @param cipher        The cipher desired, must be a 128-bit block cipher
21   @param IV            The index value, must be 128-bits
22   @param key           The cipher key
23   @param keylen        The length of the cipher key in octets
24   @param tweak         The tweak value (second key), must be 128-bits
25   @param num_rounds    The number of rounds for the cipher (0 == default)
26   @param lrw           [out] The LRW state
27   @return CRYPT_OK on success.
28 */
29 int lrw_start(               int   cipher,
30               const unsigned char *IV,
31               const unsigned char *key,       int keylen,
32               const unsigned char *tweak,
33                              int  num_rounds,
34                    symmetric_LRW *lrw)
35 {
36    int           err;
37 #ifdef LTC_LRW_TABLES
38    unsigned char B[16];
39    int           x, y, z, t;
40 #endif
41
42    LTC_ARGCHK(IV    != NULL);
43    LTC_ARGCHK(key   != NULL);
44    LTC_ARGCHK(tweak != NULL);
45    LTC_ARGCHK(lrw   != NULL);
46
47 #ifdef LTC_FAST
48    if (16 % sizeof(LTC_FAST_TYPE)) {
49       return CRYPT_INVALID_ARG;
50    }
51 #endif
52
53    /* is cipher valid? */
54    if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
55       return err;
56    }
57    if (cipher_descriptor[cipher].block_length != 16) {
58       return CRYPT_INVALID_CIPHER;
59    }
60
61    /* schedule key */
62    if ((err = cipher_descriptor[cipher].setup(key, keylen, num_rounds, &lrw->key)) != CRYPT_OK) {
63       return err;
64    }
65    lrw->cipher = cipher;
66
67    /* copy the IV and tweak */
68    XMEMCPY(lrw->tweak, tweak, 16);
69
70 #ifdef LTC_LRW_TABLES
71    /* setup tables */
72    /* generate the first table as it has no shifting (from which we make the other tables) */
73    zeromem(B, 16);
74    for (y = 0; y < 256; y++) {
75         B[0] = y;
76         gcm_gf_mult(tweak, B, &lrw->PC[0][y][0]);
77    }
78
79    /* now generate the rest of the tables based the previous table */
80    for (x = 1; x < 16; x++) {
81       for (y = 0; y < 256; y++) {
82          /* now shift it right by 8 bits */
83          t = lrw->PC[x-1][y][15];
84          for (z = 15; z > 0; z--) {
85              lrw->PC[x][y][z] = lrw->PC[x-1][y][z-1];
86          }
87          lrw->PC[x][y][0]  = gcm_shift_table[t<<1];
88          lrw->PC[x][y][1] ^= gcm_shift_table[(t<<1)+1];
89       }
90    }
91 #endif
92
93    /* generate first pad */
94    return lrw_setiv(IV, 16, lrw);
95 }
96
97
98 #endif
99 /* ref:         $Format:%D$ */
100 /* git commit:  $Format:%H$ */
101 /* commit time: $Format:%ai$ */