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