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