]> pd.if.org Git - zpackage/blob - libtomcrypt/src/encauth/gcm/gcm_init.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / encauth / gcm / gcm_init.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
10 /**
11    @file gcm_init.c
12    GCM implementation, initialize state, by Tom St Denis
13 */
14 #include "tomcrypt.h"
15
16 #ifdef LTC_GCM_MODE
17
18 /**
19   Initialize a GCM state
20   @param gcm     The GCM state to initialize
21   @param cipher  The index of the cipher to use
22   @param key     The secret key
23   @param keylen  The length of the secret key
24   @return CRYPT_OK on success
25  */
26 int gcm_init(gcm_state *gcm, int cipher,
27              const unsigned char *key,  int keylen)
28 {
29    int           err;
30    unsigned char B[16];
31 #ifdef LTC_GCM_TABLES
32    int           x, y, z, t;
33 #endif
34
35    LTC_ARGCHK(gcm != NULL);
36    LTC_ARGCHK(key != NULL);
37
38 #ifdef LTC_FAST
39    if (16 % sizeof(LTC_FAST_TYPE)) {
40       return CRYPT_INVALID_ARG;
41    }
42 #endif
43
44    /* is cipher valid? */
45    if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
46       return err;
47    }
48    if (cipher_descriptor[cipher].block_length != 16) {
49       return CRYPT_INVALID_CIPHER;
50    }
51
52    /* schedule key */
53    if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, &gcm->K)) != CRYPT_OK) {
54       return err;
55    }
56
57    /* H = E(0) */
58    zeromem(B, 16);
59    if ((err = cipher_descriptor[cipher].ecb_encrypt(B, gcm->H, &gcm->K)) != CRYPT_OK) {
60       return err;
61    }
62
63    /* setup state */
64    zeromem(gcm->buf, sizeof(gcm->buf));
65    zeromem(gcm->X,   sizeof(gcm->X));
66    gcm->cipher   = cipher;
67    gcm->mode     = LTC_GCM_MODE_IV;
68    gcm->ivmode   = 0;
69    gcm->buflen   = 0;
70    gcm->totlen   = 0;
71    gcm->pttotlen = 0;
72
73 #ifdef LTC_GCM_TABLES
74    /* setup tables */
75
76    /* generate the first table as it has no shifting (from which we make the other tables) */
77    zeromem(B, 16);
78    for (y = 0; y < 256; y++) {
79         B[0] = y;
80         gcm_gf_mult(gcm->H, B, &gcm->PC[0][y][0]);
81    }
82
83    /* now generate the rest of the tables based the previous table */
84    for (x = 1; x < 16; x++) {
85       for (y = 0; y < 256; y++) {
86          /* now shift it right by 8 bits */
87          t = gcm->PC[x-1][y][15];
88          for (z = 15; z > 0; z--) {
89              gcm->PC[x][y][z] = gcm->PC[x-1][y][z-1];
90          }
91          gcm->PC[x][y][0] = gcm_shift_table[t<<1];
92          gcm->PC[x][y][1] ^= gcm_shift_table[(t<<1)+1];
93       }
94    }
95
96 #endif
97
98    return CRYPT_OK;
99 }
100
101 #endif
102
103 /* ref:         $Format:%D$ */
104 /* git commit:  $Format:%H$ */
105 /* commit time: $Format:%ai$ */