comparison libtomcrypt/src/encauth/gcm/gcm_done.c @ 285:1b9e69c058d2

propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3) to branch 'au.asn.ucc.matt.dropbear' (head fdf4a7a3b97ae5046139915de7e40399cceb2c01)
author Matt Johnston <matt@ucc.asn.au>
date Wed, 08 Mar 2006 13:23:58 +0000
parents
children 0cbe8f6dbf9e
comparison
equal deleted inserted replaced
281:997e6f7dc01e 285:1b9e69c058d2
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_done.c
14 GCM implementation, Terminate the stream, by Tom St Denis
15 */
16 #include "tomcrypt.h"
17
18 #ifdef GCM_MODE
19
20 /**
21 Terminate a GCM stream
22 @param gcm The GCM state
23 @param tag [out] The destination for the MAC tag
24 @param taglen [in/out] The length of the MAC tag
25 @return CRYPT_OK on success
26 */
27 int gcm_done(gcm_state *gcm,
28 unsigned char *tag, unsigned long *taglen)
29 {
30 unsigned long x;
31 int err;
32
33 LTC_ARGCHK(gcm != NULL);
34 LTC_ARGCHK(tag != NULL);
35 LTC_ARGCHK(taglen != NULL);
36
37 if (gcm->buflen > 16 || gcm->buflen < 0) {
38 return CRYPT_INVALID_ARG;
39 }
40
41 if ((err = cipher_is_valid(gcm->cipher)) != CRYPT_OK) {
42 return err;
43 }
44
45
46 if (gcm->mode != GCM_MODE_TEXT) {
47 return CRYPT_INVALID_ARG;
48 }
49
50 /* handle remaining ciphertext */
51 if (gcm->buflen) {
52 gcm->pttotlen += gcm->buflen * CONST64(8);
53 gcm_mult_h(gcm, gcm->X);
54 }
55
56 /* length */
57 STORE64H(gcm->totlen, gcm->buf);
58 STORE64H(gcm->pttotlen, gcm->buf+8);
59 for (x = 0; x < 16; x++) {
60 gcm->X[x] ^= gcm->buf[x];
61 }
62 gcm_mult_h(gcm, gcm->X);
63
64 /* encrypt original counter */
65 cipher_descriptor[gcm->cipher].ecb_encrypt(gcm->Y_0, gcm->buf, &gcm->K);
66 for (x = 0; x < 16 && x < *taglen; x++) {
67 tag[x] = gcm->buf[x] ^ gcm->X[x];
68 }
69 *taglen = x;
70
71 cipher_descriptor[gcm->cipher].done(&gcm->K);
72
73 return CRYPT_OK;
74 }
75
76 #endif
77
78
79 /* $Source: /cvs/libtom/libtomcrypt/src/encauth/gcm/gcm_done.c,v $ */
80 /* $Revision: 1.7 $ */
81 /* $Date: 2005/05/05 14:35:58 $ */