1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
13 LRW_MODE implementation, start mode, Tom St Denis
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.
29 int lrw_start( int cipher,
30 const unsigned char *IV,
31 const unsigned char *key, int keylen,
32 const unsigned char *tweak,
42 LTC_ARGCHK(IV != NULL);
43 LTC_ARGCHK(key != NULL);
44 LTC_ARGCHK(tweak != NULL);
45 LTC_ARGCHK(lrw != NULL);
48 if (16 % sizeof(LTC_FAST_TYPE)) {
49 return CRYPT_INVALID_ARG;
53 /* is cipher valid? */
54 if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
57 if (cipher_descriptor[cipher].block_length != 16) {
58 return CRYPT_INVALID_CIPHER;
62 if ((err = cipher_descriptor[cipher].setup(key, keylen, num_rounds, &lrw->key)) != CRYPT_OK) {
67 /* copy the IV and tweak */
68 XMEMCPY(lrw->tweak, tweak, 16);
72 /* generate the first table as it has no shifting (from which we make the other tables) */
74 for (y = 0; y < 256; y++) {
76 gcm_gf_mult(tweak, B, &lrw->PC[0][y][0]);
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];
87 lrw->PC[x][y][0] = gcm_shift_table[t<<1];
88 lrw->PC[x][y][1] ^= gcm_shift_table[(t<<1)+1];
93 /* generate first pad */
94 return lrw_setiv(IV, 16, lrw);
99 /* ref: $Format:%D$ */
100 /* git commit: $Format:%H$ */
101 /* commit time: $Format:%ai$ */