49#define GCM_FL_ENCRYPT 0x00ff
58#define GCM_FL_IV 0x0100
114#define gcm_offset( field ) offsetof ( struct gcm_context, field )
127 for ( mask = 1 ; mask ; mask <<= 1 ) {
157static inline void gcm_xor (
const void *src1,
const void *src2,
void *dst,
160 const uint8_t *src1_bytes = src1;
161 const uint8_t *src2_bytes = src2;
165 *(dst_bytes++) = ( *(src1_bytes++) ^ *(src2_bytes++) );
197 for ( i = 0,
carry = 0 ; i <
sizeof ( res->
byte ) ; i++ ) {
198 byte = mult->
byte[i];
199 res->
byte[i] = ( (
carry << 7 ) | (
byte >> 1 ) );
200 carry = (
byte & 0x01 );
227 for ( i = 1 ; i < 256 ; i++ ) {
237 other = (
this & 0x7f );
247 other = (
this << 1 );
272 byte = &poly->
byte[
sizeof ( poly->
byte ) - 1 ];
276 for ( ;
byte > &poly->
byte[0] ;
byte-- )
277 *
byte = *(
byte - 1 );
300 byte = &poly->
byte[
sizeof ( poly->
byte ) - 1 ];
302 for (
byte-- ;
byte >= &poly->
byte[0] ;
byte-- ) {
308 memcpy ( poly, &res,
sizeof ( *poly ) );
322 DBGC2 ( context,
"GCM %p len(A)||len(C):\n", context );
328 DBGC2 ( context,
"GCM %p GHASH(H,A,C):\n", context );
343 const void *
src,
void *dst,
size_t len ) {
346 unsigned int flags = context->gcm.flags;
353 block = ( ( ( context->gcm.len.len.add + 8 *
sizeof (
tmp ) - 1 ) /
354 ( 8 *
sizeof (
tmp ) ) ) +
355 ( ( context->gcm.len.len.data + 8 *
sizeof (
tmp ) - 1 ) /
356 ( 8 *
sizeof (
tmp ) ) ) + 1 );
360 &context->gcm.len.len.data : &context->gcm.len.len.add );
361 *total += (
len * 8 );
368 if ( frag_len >
sizeof (
tmp ) )
369 frag_len =
sizeof (
tmp );
372 gcm_xor (
src, &context->gcm.hash, &context->gcm.hash,
382 DBGC2 ( context,
"GCM %p Y[%d]:\n", context,
block );
383 DBGC2_HDA ( context, 0, &context->gcm.ctr,
384 sizeof ( context->gcm.ctr ) );
386 &context->gcm.ctr, &
tmp,
388 DBGC2 ( context,
"GCM %p E(K,Y[%d]):\n",
397 gcm_xor ( &
tmp, &context->gcm.hash, &context->gcm.hash,
398 ( frag_len &
flags ) );
403 DBGC2 ( context,
"GCM %p X[%d]:\n", context,
block );
404 DBGC2_HDA ( context, 0, &context->gcm.hash,
405 sizeof ( context->gcm.hash ) );
419 const void *
key,
size_t keylen ) {
425 memset ( &context->gcm, 0, sizeof ( context->gcm ) );
434 &context->gcm.key, sizeof ( context->gcm.key ) );
435 DBGC2 ( context,
"GCM %p H:\n", context );
436 DBGC2_HDA ( context, 0, &context->gcm.key,
437 sizeof ( context->gcm.key ) );
458 const void *
iv,
size_t ivlen ) {
471 if ( ivlen ==
sizeof ( context->gcm.ctr.ctr.iv ) ) {
474 memcpy ( context->gcm.ctr.ctr.iv,
iv, ivlen );
481 gcm_hash ( &context->gcm, &context->gcm.ctr );
482 assert ( context->gcm.len.len.add == 0 );
491 DBGC2 ( context,
"GCM %p Y[0]:\n", context );
492 DBGC2_HDA ( context, 0, &context->gcm.ctr,
493 sizeof ( context->gcm.ctr ) );
507 const void *
src,
void *dst,
size_t len ) {
525 const void *
src,
void *dst,
size_t len ) {
529 context->gcm.flags = 0;
552 offset = ( ( -context->gcm.len.len.data ) / ( 8 *
sizeof (
tmp ) ) );
556 DBGC2 ( context,
"GCM %p E(K,Y[0]):\n", context );
561 DBGC2 ( context,
"GCM %p T:\n", context );
#define NULL
NULL pointer (VOID *).
struct golan_eq_context ctx
union @162305117151260234136356364136041353210355154177 key
struct arbelprm_rc_send_wqe rc
pseudo_bit_t value[0x00020]
pseudo_bit_t hash[0x00010]
unsigned long long uint64_t
#define build_assert(condition)
Assert a condition at build time (after dead code elimination).
#define assert(condition)
Assert a condition at run-time.
uint16_t offset
Offset to command line.
uint64_t tag
Identity tag.
static void gcm_hash(struct gcm_context *context, union gcm_block *hash)
Construct hash.
int gcm_setiv(struct cipher_algorithm *cipher, void *ctx, const void *iv, size_t ivlen)
Set initialisation vector.
static union gcm_block gcm_cached_mult[256]
Cached multiplication table (M0) for Shoup's method.
void gcm_decrypt(struct cipher_algorithm *cipher, void *ctx, const void *src, void *dst, size_t len)
Decrypt data.
static const union gcm_block * gcm_cached_key
Hash key for which multiplication tables are cached.
static uint8_t gcm_reverse(const uint8_t byte)
Reverse bits in a byte.
static void gcm_xor_block(const union gcm_block *src, union gcm_block *dst)
XOR whole data block in situ.
static void gcm_cache(const union gcm_block *key)
Construct cached tables.
#define GCM_FL_ENCRYPT
Perform encryption.
static void gcm_multiply_key(const union gcm_block *key, union gcm_block *poly)
Multiply polynomial by hash key in situ.
void gcm_auth(struct cipher_algorithm *cipher, void *ctx, void *auth)
Generate authentication tag.
#define gcm_offset(field)
Offset of a field within GCM context.
static void gcm_xor(const void *src1, const void *src2, void *dst, size_t len)
XOR partial data block.
static void gcm_count(union gcm_block *ctr, uint32_t delta)
Update GCM counter.
void gcm_encrypt(struct cipher_algorithm *cipher, void *ctx, const void *src, void *dst, size_t len)
Encrypt data.
static void gcm_process(struct cipher_algorithm *cipher, void *ctx, const void *src, void *dst, size_t len)
Encrypt/decrypt/authenticate data.
static uint16_t gcm_cached_reduce[256]
Cached reduction table (R) for Shoup's method.
static void gcm_multiply_x_8(union gcm_block *poly)
Multiply polynomial by (x^8) in situ.
#define GCM_POLY
GCM field polynomial.
#define GCM_FL_IV
Calculate hash over an initialisation vector value.
int gcm_setkey(struct cipher_algorithm *cipher, void *ctx, const void *key, size_t keylen)
Set key.
static void gcm_multiply_x(const union gcm_block *mult, union gcm_block *res)
Multiply polynomial by (x).
Galois/Counter Mode (GCM).
#define gcm_context_t(ctxsize)
A GCM mode context.
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
#define be32_to_cpu(value)
#define cpu_to_be16(value)
#define cpu_to_be32(value)
#define cpu_to_be64(value)
#define be16_to_cpu(value)
static int cipher_setkey(struct cipher_algorithm *cipher, void *ctx, const void *key, size_t keylen)
#define cipher_encrypt(cipher, ctx, src, dst, len)
void * memcpy(void *dest, const void *src, size_t len) __nonnull
void * memset(void *dest, int character, size_t len) __nonnull
uint8_t block[3][8]
DES-encrypted blocks.
void * priv
Algorithm private data.
size_t ctxsize
Context size.
void(* auth)(struct cipher_algorithm *cipher, void *ctx, void *auth)
Generate authentication tag.
union gcm_block key
Hash key (H).
union gcm_block hash
Accumulated hash (X).
union gcm_block len
Accumulated lengths.
uint32_t value
Counter value.
uint64_t data
Data length.
uint64_t add
Additional data length.
uint8_t byte[16]
Raw bytes.
struct gcm_counter ctr
Counter.
uint32_t dword[4]
Raw dwords.
struct gcm_lengths len
Lengths.
uint16_t word[8]
Raw words.
u8 iv[16]
Initialization vector.