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