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
12 GCM implementation, initialize state, by Tom St Denis
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
26 int gcm_init(gcm_state *gcm, int cipher,
27 const unsigned char *key, int keylen)
35 LTC_ARGCHK(gcm != NULL);
36 LTC_ARGCHK(key != NULL);
39 if (16 % sizeof(LTC_FAST_TYPE)) {
40 return CRYPT_INVALID_ARG;
44 /* is cipher valid? */
45 if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
48 if (cipher_descriptor[cipher].block_length != 16) {
49 return CRYPT_INVALID_CIPHER;
53 if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, &gcm->K)) != CRYPT_OK) {
59 if ((err = cipher_descriptor[cipher].ecb_encrypt(B, gcm->H, &gcm->K)) != CRYPT_OK) {
64 zeromem(gcm->buf, sizeof(gcm->buf));
65 zeromem(gcm->X, sizeof(gcm->X));
67 gcm->mode = LTC_GCM_MODE_IV;
76 /* generate the first table as it has no shifting (from which we make the other tables) */
78 for (y = 0; y < 256; y++) {
80 gcm_gf_mult(gcm->H, B, &gcm->PC[0][y][0]);
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];
91 gcm->PC[x][y][0] = gcm_shift_table[t<<1];
92 gcm->PC[x][y][1] ^= gcm_shift_table[(t<<1)+1];
103 /* ref: $Format:%D$ */
104 /* git commit: $Format:%H$ */
105 /* commit time: $Format:%ai$ */