iPXE
include
ipxe
gcm.h
Go to the documentation of this file.
1
#ifndef _IPXE_GCM_H
2
#define _IPXE_GCM_H
3
4
/** @file
5
*
6
* Galois/Counter Mode (GCM)
7
*
8
*/
9
10
FILE_LICENCE
( GPL2_OR_LATER_OR_UBDL );
11
FILE_SECBOOT
( PERMITTED );
12
13
#include <
stdint.h
>
14
#include <
ipxe/crypto.h
>
15
16
/** A GCM counter */
17
struct
gcm_counter
{
18
/** Initialisation vector */
19
uint8_t
iv
[12];
20
/** Counter value */
21
uint32_t
value
;
22
}
__attribute__
(( packed ));
23
24
/** A GCM length pair */
25
struct
gcm_lengths
{
26
/** Additional data length */
27
uint64_t
add
;
28
/** Data length */
29
uint64_t
data
;
30
}
__attribute__
(( packed ));
31
32
/** A GCM block */
33
union
gcm_block
{
34
/** Raw bytes */
35
uint8_t
byte
[16];
36
/** Raw words */
37
uint16_t
word
[8];
38
/** Raw dwords */
39
uint32_t
dword
[4];
40
/** Counter */
41
struct
gcm_counter
ctr
;
42
/** Lengths */
43
struct
gcm_lengths
len
;
44
}
__attribute__
(( packed ));
45
46
/** GCM context */
47
struct
gcm_context
{
48
/** Accumulated hash (X) */
49
union
gcm_block
hash
;
50
/** Accumulated lengths */
51
union
gcm_block
len
;
52
/** Counter (Y) */
53
union
gcm_block
ctr
;
54
/** Hash key (H) */
55
union
gcm_block
key
;
56
/** Underlying block cipher */
57
struct
cipher_algorithm
*
raw_cipher
;
58
/** Underlying block cipher context */
59
uint8_t
raw_ctx
[0];
60
};
61
62
extern
void
gcm_tag
(
struct
gcm_context
*context,
union
gcm_block
*
tag
);
63
extern
int
gcm_setkey
(
struct
gcm_context
*context,
const
void
*
key
,
64
size_t
keylen,
struct
cipher_algorithm
*raw_cipher );
65
extern
void
gcm_setiv
(
struct
gcm_context
*context,
const
void
*
iv
,
66
size_t
ivlen );
67
extern
void
gcm_encrypt
(
struct
gcm_context
*context,
const
void
*
src
,
68
void
*dst,
size_t
len
);
69
extern
void
gcm_decrypt
(
struct
gcm_context
*context,
const
void
*
src
,
70
void
*dst,
size_t
len
);
71
72
/**
73
* Create a GCM mode of behaviour of an existing cipher
74
*
75
* @v _cbc_name Name for the new CBC cipher
76
* @v _cbc_cipher New cipher algorithm
77
* @v _raw_cipher Underlying cipher algorithm
78
* @v _raw_context Context structure for the underlying cipher
79
* @v _blocksize Cipher block size
80
*/
81
#define GCM_CIPHER( _gcm_name, _gcm_cipher, _raw_cipher, _raw_context, \
82
_blocksize ) \
83
struct _gcm_name ## _context { \
84
/** GCM context */
\
85
struct gcm_context gcm; \
86
/** Underlying block cipher context */
\
87
_raw_context raw; \
88
}; \
89
static int _gcm_name ## _setkey ( void *ctx, const void *key, \
90
size_t keylen ) { \
91
struct _gcm_name ## _context *context = ctx; \
92
build_assert ( _blocksize == sizeof ( context->gcm.key ) ); \
93
build_assert ( offsetof ( typeof ( *context ), gcm ) == 0 ); \
94
build_assert ( offsetof ( typeof ( *context ), raw ) == \
95
offsetof ( typeof ( *context ), gcm.raw_ctx ) ); \
96
return gcm_setkey ( &context->gcm, key, keylen, &_raw_cipher ); \
97
} \
98
static void _gcm_name ## _setiv ( void *ctx, const void *iv, \
99
size_t ivlen ) { \
100
struct _gcm_name ## _context *context = ctx; \
101
gcm_setiv ( &context->gcm, iv, ivlen ); \
102
} \
103
static void _gcm_name ## _encrypt ( void *ctx, const void *src, \
104
void *dst, size_t len ) { \
105
struct _gcm_name ## _context *context = ctx; \
106
gcm_encrypt ( &context->gcm, src, dst, len ); \
107
} \
108
static void _gcm_name ## _decrypt ( void *ctx, const void *src, \
109
void *dst, size_t len ) { \
110
struct _gcm_name ## _context *context = ctx; \
111
gcm_decrypt ( &context->gcm, src, dst, len ); \
112
} \
113
static void _gcm_name ## _auth ( void *ctx, void *auth ) { \
114
struct _gcm_name ## _context *context = ctx; \
115
union gcm_block *tag = auth; \
116
gcm_tag ( &context->gcm, tag ); \
117
} \
118
struct cipher_algorithm _gcm_cipher = { \
119
.name = #_gcm_name, \
120
.ctxsize = sizeof ( struct _gcm_name ## _context ), \
121
.blocksize = 1, \
122
.alignsize = sizeof ( union gcm_block ), \
123
.authsize = sizeof ( union gcm_block ), \
124
.setkey = _gcm_name ## _setkey, \
125
.setiv = _gcm_name ## _setiv, \
126
.encrypt = _gcm_name ## _encrypt, \
127
.decrypt = _gcm_name ## _decrypt, \
128
.auth = _gcm_name ## _auth, \
129
};
130
131
#endif
/* _IPXE_GCM_H */
__attribute__
#define __attribute__(x)
Definition:
compiler.h:10
gcm_block
A GCM block.
Definition:
gcm.h:33
uint16_t
unsigned short uint16_t
Definition:
stdint.h:11
gcm_context::len
union gcm_block len
Accumulated lengths.
Definition:
gcm.h:51
gcm_context::raw_ctx
uint8_t raw_ctx[0]
Underlying block cipher context.
Definition:
gcm.h:59
gcm_block::len
struct gcm_lengths len
Lengths.
Definition:
gcm.h:43
gcm_counter::iv
uint8_t iv[12]
Initialisation vector.
Definition:
gcm.h:19
gcm_setiv
void gcm_setiv(struct gcm_context *context, const void *iv, size_t ivlen)
Set initialisation vector.
Definition:
gcm.c:475
uint64_t
unsigned long long uint64_t
Definition:
stdint.h:13
crypto.h
Cryptographic API.
gcm_context::hash
union gcm_block hash
Accumulated hash (X)
Definition:
gcm.h:49
gcm_counter::value
uint32_t value
Counter value.
Definition:
gcm.h:21
iv
u8 iv[16]
Initialization vector.
Definition:
wpa.h:60
gcm_context::raw_cipher
struct cipher_algorithm * raw_cipher
Underlying block cipher.
Definition:
gcm.h:57
FILE_LICENCE
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
src
static const void * src
Definition:
string.h:48
gcm_lengths
A GCM length pair.
Definition:
gcm.h:25
len
ring len
Length.
Definition:
dwmac.h:231
FILE_SECBOOT
FILE_SECBOOT(PERMITTED)
gcm_context::key
union gcm_block key
Hash key (H)
Definition:
gcm.h:55
gcm_lengths::data
uint64_t data
Data length.
Definition:
gcm.h:29
gcm_block::ctr
struct gcm_counter ctr
Counter.
Definition:
gcm.h:41
gcm_context
GCM context.
Definition:
gcm.h:47
gcm_tag
void gcm_tag(struct gcm_context *context, union gcm_block *tag)
Construct tag.
Definition:
gcm.c:409
uint8_t
unsigned char uint8_t
Definition:
stdint.h:10
uint32_t
unsigned int uint32_t
Definition:
stdint.h:12
gcm_context::ctr
union gcm_block ctr
Counter (Y)
Definition:
gcm.h:53
gcm_counter
A GCM counter.
Definition:
gcm.h:17
gcm_decrypt
void gcm_decrypt(struct gcm_context *context, const void *src, void *dst, size_t len)
Decrypt data.
Definition:
gcm.c:537
word
unsigned short word
Definition:
smc9000.h:39
cipher_algorithm
A cipher algorithm.
Definition:
crypto.h:51
stdint.h
gcm_lengths::add
uint64_t add
Additional data length.
Definition:
gcm.h:27
gcm_encrypt
void gcm_encrypt(struct gcm_context *context, const void *src, void *dst, size_t len)
Encrypt data.
Definition:
gcm.c:522
tag
uint64_t tag
Identity tag.
Definition:
edd.h:31
dword
unsigned long int dword
Definition:
smc9000.h:40
key
union @391 key
Sense key.
Definition:
scsi.h:18
gcm_setkey
int gcm_setkey(struct gcm_context *context, const void *key, size_t keylen, struct cipher_algorithm *raw_cipher)
Set key.
Definition:
gcm.c:440
Generated by
1.8.15