comparison src/encauth/gcm/gcm_init.c @ 191:1c15b283127b libtomcrypt-orig

Import of libtomcrypt 1.02 with manual path rename rearrangement etc
author Matt Johnston <matt@ucc.asn.au>
date Fri, 06 May 2005 13:23:02 +0000
parents
children 39d5d58461d6
comparison
equal deleted inserted replaced
143:5d99163f7e32 191:1c15b283127b
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.org
10 */
11
12 /**
13 @file gcm_init.c
14 GCM implementation, initialize state, by Tom St Denis
15 */
16 #include "tomcrypt.h"
17
18 #ifdef GCM_MODE
19
20 /**
21 Initialize a GCM state
22 @param gcm The GCM state to initialize
23 @param cipher The index of the cipher to use
24 @param key The secret key
25 @param keylen The length of the secret key
26 @return CRYPT_OK on success
27 */
28 int gcm_init(gcm_state *gcm, int cipher,
29 const unsigned char *key, int keylen)
30 {
31 int err;
32 unsigned char B[16];
33 #ifdef GCM_TABLES
34 int x, y;
35 #endif
36
37 LTC_ARGCHK(gcm != NULL);
38 LTC_ARGCHK(key != NULL);
39
40 #ifdef LTC_FAST
41 if (16 % sizeof(LTC_FAST_TYPE)) {
42 return CRYPT_INVALID_ARG;
43 }
44 #endif
45
46 /* is cipher valid? */
47 if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
48 return err;
49 }
50 if (cipher_descriptor[cipher].block_length != 16) {
51 return CRYPT_INVALID_CIPHER;
52 }
53
54 /* schedule key */
55 if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, &gcm->K)) != CRYPT_OK) {
56 return err;
57 }
58
59 /* H = E(0) */
60 zeromem(B, 16);
61 cipher_descriptor[cipher].ecb_encrypt(B, gcm->H, &gcm->K);
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 = GCM_MODE_IV;
68 gcm->ivmode = 0;
69 gcm->buflen = 0;
70 gcm->totlen = 0;
71 gcm->pttotlen = 0;
72
73 #ifdef GCM_TABLES
74 /* setup tables */
75 zeromem(B, 16);
76 for (x = 0; x < 16; x++) {
77 for (y = 0; y < 256; y++) {
78 B[x] = y;
79 gcm_gf_mult(gcm->H, B, &gcm->PC[x][y][0]);
80 }
81 B[x] = 0;
82 }
83 #endif
84
85 return CRYPT_OK;
86 }
87
88 #endif