comparison src/encauth/gcm/gcm_memory.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_memory.c
14 GCM implementation, process a packet, by Tom St Denis
15 */
16 #include "tomcrypt.h"
17
18 #ifdef GCM_MODE
19
20 /**
21 Process an entire GCM packet in one call.
22 @param cipher Index of cipher to use
23 @param key The secret key
24 @param keylen The length of the secret key
25 @param IV The initial vector
26 @param IVlen The length of the initial vector
27 @param adata The additional authentication data (header)
28 @param adatalen The length of the adata
29 @param pt The plaintext
30 @param ptlen The length of the plaintext (ciphertext length is the same)
31 @param ct The ciphertext
32 @param tag [out] The MAC tag
33 @param taglen [in/out] The MAC tag length
34 @param direction Encrypt or Decrypt mode (GCM_ENCRYPT or GCM_DECRYPT)
35 @return CRYPT_OK on success
36 */
37 int gcm_memory( int cipher,
38 const unsigned char *key, unsigned long keylen,
39 const unsigned char *IV, unsigned long IVlen,
40 const unsigned char *adata, unsigned long adatalen,
41 unsigned char *pt, unsigned long ptlen,
42 unsigned char *ct,
43 unsigned char *tag, unsigned long *taglen,
44 int direction)
45 {
46 gcm_state *gcm;
47 int err;
48
49 if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
50 return err;
51 }
52
53 if (cipher_descriptor[cipher].accel_gcm_memory != NULL) {
54 cipher_descriptor[cipher].accel_gcm_memory
55 (key, keylen,
56 IV, IVlen,
57 adata, adatalen,
58 pt, ptlen,
59 ct,
60 tag, taglen,
61 direction);
62 return CRYPT_OK;
63 }
64
65
66 gcm = XMALLOC(sizeof(*gcm));
67 if (gcm == NULL) {
68 return CRYPT_MEM;
69 }
70
71 if ((err = gcm_init(gcm, cipher, key, keylen)) != CRYPT_OK) {
72 goto LTC_ERR;
73 }
74 if ((err = gcm_add_iv(gcm, IV, IVlen)) != CRYPT_OK) {
75 goto LTC_ERR;
76 }
77 if ((err = gcm_add_aad(gcm, adata, adatalen)) != CRYPT_OK) {
78 goto LTC_ERR;
79 }
80 if ((err = gcm_process(gcm, pt, ptlen, ct, direction)) != CRYPT_OK) {
81 goto LTC_ERR;
82 }
83 err = gcm_done(gcm, tag, taglen);
84 LTC_ERR:
85 XFREE(gcm);
86 return err;
87 }
88 #endif
89