Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/tomcrypt/src/encauth/gcm/gcm_memory.c
5972 views
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_memory.c
12
GCM implementation, process a packet, by Tom St Denis
13
*/
14
#include "tomcrypt.h"
15
16
#ifdef LTC_GCM_MODE
17
18
/**
19
Process an entire GCM packet in one call.
20
@param cipher Index of cipher to use
21
@param key The secret key
22
@param keylen The length of the secret key
23
@param IV The initialization vector
24
@param IVlen The length of the initialization vector
25
@param adata The additional authentication data (header)
26
@param adatalen The length of the adata
27
@param pt The plaintext
28
@param ptlen The length of the plaintext (ciphertext length is the same)
29
@param ct The ciphertext
30
@param tag [out] The MAC tag
31
@param taglen [in/out] The MAC tag length
32
@param direction Encrypt or Decrypt mode (GCM_ENCRYPT or GCM_DECRYPT)
33
@return CRYPT_OK on success
34
*/
35
int gcm_memory( int cipher,
36
const unsigned char *key, unsigned long keylen,
37
const unsigned char *IV, unsigned long IVlen,
38
const unsigned char *adata, unsigned long adatalen,
39
unsigned char *pt, unsigned long ptlen,
40
unsigned char *ct,
41
unsigned char *tag, unsigned long *taglen,
42
int direction)
43
{
44
void *orig;
45
gcm_state *gcm;
46
int err;
47
48
if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
49
return err;
50
}
51
52
if (cipher_descriptor[cipher].accel_gcm_memory != NULL) {
53
return cipher_descriptor[cipher].accel_gcm_memory
54
(key, keylen,
55
IV, IVlen,
56
adata, adatalen,
57
pt, ptlen,
58
ct,
59
tag, taglen,
60
direction);
61
}
62
63
64
65
#ifndef LTC_GCM_TABLES_SSE2
66
orig = gcm = XMALLOC(sizeof(*gcm));
67
#else
68
orig = gcm = XMALLOC(sizeof(*gcm) + 16);
69
#endif
70
if (gcm == NULL) {
71
return CRYPT_MEM;
72
}
73
74
/* Force GCM to be on a multiple of 16 so we can use 128-bit aligned operations
75
* note that we only modify gcm and keep orig intact. This code is not portable
76
* but again it's only for SSE2 anyways, so who cares?
77
*/
78
#ifdef LTC_GCM_TABLES_SSE2
79
if ((unsigned long)gcm & 15) {
80
gcm = (gcm_state *)((unsigned long)gcm + (16 - ((unsigned long)gcm & 15)));
81
}
82
#endif
83
84
if ((err = gcm_init(gcm, cipher, key, keylen)) != CRYPT_OK) {
85
goto LTC_ERR;
86
}
87
if ((err = gcm_add_iv(gcm, IV, IVlen)) != CRYPT_OK) {
88
goto LTC_ERR;
89
}
90
if ((err = gcm_add_aad(gcm, adata, adatalen)) != CRYPT_OK) {
91
goto LTC_ERR;
92
}
93
if ((err = gcm_process(gcm, pt, ptlen, ct, direction)) != CRYPT_OK) {
94
goto LTC_ERR;
95
}
96
err = gcm_done(gcm, tag, taglen);
97
LTC_ERR:
98
XFREE(orig);
99
return err;
100
}
101
#endif
102
103