-
Notifications
You must be signed in to change notification settings - Fork 9
/
gcm-mul.h
96 lines (69 loc) · 2.7 KB
/
gcm-mul.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*****************************************************************************
* gcm-mul.h
*
* Functions to support GCM mode.
****************************************************************************/
#ifndef GCM_MUL_H
#define GCM_MUL_H
/*****************************************************************************
* Includes
****************************************************************************/
#include "aes-min.h"
#include "gcm-mul-cfg.h"
/*****************************************************************************
* Defines
****************************************************************************/
#ifndef GCM_U128_ELEMENT_SIZE
#define GCM_U128_ELEMENT_SIZE 4
#endif
#define GCM_U128_NUM_ELEMENTS (AES_BLOCK_SIZE / GCM_U128_ELEMENT_SIZE)
/*****************************************************************************
* Types
****************************************************************************/
#if GCM_U128_ELEMENT_SIZE == 1
typedef uint8_t gcm_u128_element_t;
#elif GCM_U128_ELEMENT_SIZE == 2
typedef uint16_t gcm_u128_element_t;
#elif GCM_U128_ELEMENT_SIZE == 4
typedef uint32_t gcm_u128_element_t;
#elif GCM_U128_ELEMENT_SIZE == 8
typedef uint64_t gcm_u128_element_t;
#else
#error Invalid GCM_U128_ELEMENT_SIZE
#endif
/*
* This struct is basically to enable big-integer calculations in the 128-bit
* Galois field. The struct is fixed size for this purpose. The functions that
* operate on it are specialised to do the bit-reversed operations needed
* specifically for the Galois 128-bit multiply used in the GCM algorithm.
*/
typedef union
{
gcm_u128_element_t element[GCM_U128_NUM_ELEMENTS];
uint16_t reduce_bytes;
uint8_t bytes[AES_BLOCK_SIZE];
} gcm_u128_struct_t;
typedef struct
{
gcm_u128_struct_t key_data[255];
} gcm_mul_table8_t;
typedef struct
{
gcm_u128_struct_t key_data_hi[15];
gcm_u128_struct_t key_data_lo[15];
} gcm_mul_table4_t;
/*****************************************************************************
* Functions
****************************************************************************/
#ifdef GCM_MUL_BIT_BY_BIT
void gcm_mul(uint8_t p_block[AES_BLOCK_SIZE], const uint8_t p_key[AES_BLOCK_SIZE]);
#endif
#ifdef GCM_MUL_TABLE_8
void gcm_mul_prepare_table8(gcm_mul_table8_t * restrict p_table, const uint8_t p_key[AES_BLOCK_SIZE]);
void gcm_mul_table8(uint8_t p_block[AES_BLOCK_SIZE], const gcm_mul_table8_t * p_table);
#endif
#ifdef GCM_MUL_TABLE_4
void gcm_mul_prepare_table4(gcm_mul_table4_t * restrict p_table, const uint8_t p_key[AES_BLOCK_SIZE]);
void gcm_mul_table4(uint8_t p_block[AES_BLOCK_SIZE], const gcm_mul_table4_t * p_table);
#endif
#endif /* !defined(GCM_MUL_H) */