iPXE
Data Structures | Macros | Functions | Variables
gcm.h File Reference

Galois/Counter Mode (GCM) More...

#include <stdint.h>
#include <ipxe/crypto.h>

Go to the source code of this file.

Data Structures

struct  gcm_counter
 A GCM counter. More...
 
struct  gcm_lengths
 A GCM length pair. More...
 
union  gcm_block
 A GCM block. More...
 
struct  gcm_context
 GCM context. More...
 

Macros

#define GCM_CIPHER(_gcm_name, _gcm_cipher, _raw_cipher, _raw_context, _blocksize)
 Create a GCM mode of behaviour of an existing cipher. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
struct gcm_counter __attribute__ ((packed))
 
void gcm_tag (struct gcm_context *context, union gcm_block *tag)
 Construct tag. More...
 
int gcm_setkey (struct gcm_context *context, const void *key, size_t keylen, struct cipher_algorithm *raw_cipher)
 Set key. More...
 
void gcm_setiv (struct gcm_context *context, const void *iv, size_t ivlen)
 Set initialisation vector. More...
 
void gcm_encrypt (struct gcm_context *context, const void *src, void *dst, size_t len)
 Encrypt data. More...
 
void gcm_decrypt (struct gcm_context *context, const void *src, void *dst, size_t len)
 Decrypt data. More...
 

Variables

uint8_t iv [12]
 Initialisation vector. More...
 
uint32_t value
 Counter value. More...
 
uint64_t add
 Additional data length. More...
 
uint64_t data
 Data length. More...
 
uint8_t byte [16]
 Raw bytes. More...
 
uint16_t word [8]
 Raw words. More...
 
uint32_t dword [4]
 Raw dwords. More...
 
struct gcm_counter ctr
 Counter. More...
 
struct gcm_lengths len
 Lengths. More...
 
struct gcm_context __attribute__
 

Detailed Description

Galois/Counter Mode (GCM)

Definition in file gcm.h.

Macro Definition Documentation

◆ GCM_CIPHER

#define GCM_CIPHER (   _gcm_name,
  _gcm_cipher,
  _raw_cipher,
  _raw_context,
  _blocksize 
)

Create a GCM mode of behaviour of an existing cipher.

Parameters
_cbc_nameName for the new CBC cipher
_cbc_cipherNew cipher algorithm
_raw_cipherUnderlying cipher algorithm
_raw_contextContext structure for the underlying cipher
_blocksizeCipher block size

Definition at line 80 of file gcm.h.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ __attribute__()

struct gcm_counter __attribute__ ( (packed)  )

◆ gcm_tag()

void gcm_tag ( struct gcm_context context,
union gcm_block tag 
)

Construct tag.

Parameters
contextContext
tagTag

Definition at line 408 of file gcm.c.

408  {
409  union gcm_block tmp;
411 
412  /* Construct hash */
413  gcm_hash ( context, tag );
414 
415  /* Construct encrypted initial counter value */
416  memcpy ( &tmp, &context->ctr, sizeof ( tmp ) );
417  offset = ( ( -context->len.len.data ) / ( 8 * sizeof ( tmp ) ) );
418  gcm_count ( &tmp, offset );
419  cipher_encrypt ( context->raw_cipher, &context->raw_ctx, &tmp,
420  &tmp, sizeof ( tmp ) );
421  DBGC2 ( context, "GCM %p E(K,Y[0]):\n", context );
422  DBGC2_HDA ( context, 0, &tmp, sizeof ( tmp ) );
423 
424  /* Construct tag */
425  gcm_xor_block ( &tmp, tag );
426  DBGC2 ( context, "GCM %p T:\n", context );
427  DBGC2_HDA ( context, 0, tag, sizeof ( *tag ) );
428 }
A GCM block.
Definition: gcm.h:32
union gcm_block len
Accumulated lengths.
Definition: gcm.h:50
uint8_t raw_ctx[0]
Underlying block cipher context.
Definition: gcm.h:58
struct gcm_lengths len
Lengths.
Definition: gcm.h:42
static void gcm_hash(struct gcm_context *context, union gcm_block *hash)
Construct hash.
Definition: gcm.c:387
static void gcm_xor_block(const union gcm_block *src, union gcm_block *dst)
XOR whole data block in situ.
Definition: gcm.c:173
struct cipher_algorithm * raw_cipher
Underlying block cipher.
Definition: gcm.h:56
unsigned long tmp
Definition: linux_pci.h:53
#define cipher_encrypt(cipher, ctx, src, dst, len)
Definition: crypto.h:248
void * memcpy(void *dest, const void *src, size_t len) __nonnull
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
uint64_t data
Data length.
Definition: gcm.h:28
#define DBGC2_HDA(...)
Definition: compiler.h:523
unsigned int uint32_t
Definition: stdint.h:12
union gcm_block ctr
Counter (Y)
Definition: gcm.h:52
#define DBGC2(...)
Definition: compiler.h:522
uint64_t tag
Identity tag.
Definition: edd.h:30

References cipher_encrypt, gcm_context::ctr, gcm_lengths::data, DBGC2, DBGC2_HDA, gcm_hash(), gcm_xor_block(), gcm_context::len, gcm_block::len, memcpy(), offset, gcm_context::raw_cipher, gcm_context::raw_ctx, tag, and tmp.

◆ gcm_setkey()

int gcm_setkey ( struct gcm_context context,
const void *  key,
size_t  keylen,
struct cipher_algorithm raw_cipher 
)

Set key.

Parameters
contextContext
keyKey
keylenKey length
raw_cipherUnderlying cipher
Return values
rcReturn status code

Definition at line 439 of file gcm.c.

440  {
441  int rc;
442 
443  /* Initialise GCM context */
444  memset ( context, 0, sizeof ( *context ) );
445  context->raw_cipher = raw_cipher;
446 
447  /* Set underlying block cipher key */
448  if ( ( rc = cipher_setkey ( raw_cipher, context->raw_ctx, key,
449  keylen ) ) != 0 )
450  return rc;
451 
452  /* Construct GCM hash key */
453  cipher_encrypt ( raw_cipher, context->raw_ctx, &context->ctr,
454  &context->key, sizeof ( context->key ) );
455  DBGC2 ( context, "GCM %p H:\n", context );
456  DBGC2_HDA ( context, 0, &context->key, sizeof ( context->key ) );
457 
458  /* Reset counter */
459  context->ctr.ctr.value = cpu_to_be32 ( 1 );
460 
461  /* Construct cached tables */
462  gcm_cache ( &context->key );
463 
464  return 0;
465 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
uint8_t raw_ctx[0]
Underlying block cipher context.
Definition: gcm.h:58
static void gcm_cache(const union gcm_block *key)
Construct cached tables.
Definition: gcm.c:213
uint32_t value
Counter value.
Definition: gcm.h:20
struct cipher_algorithm * raw_cipher
Underlying block cipher.
Definition: gcm.h:56
#define cipher_encrypt(cipher, ctx, src, dst, len)
Definition: crypto.h:248
static void const void size_t keylen
Definition: crypto.h:233
union gcm_block key
Hash key (H)
Definition: gcm.h:54
struct gcm_counter ctr
Counter.
Definition: gcm.h:40
#define DBGC2_HDA(...)
Definition: compiler.h:523
#define cpu_to_be32(value)
Definition: byteswap.h:110
union gcm_block ctr
Counter (Y)
Definition: gcm.h:52
#define DBGC2(...)
Definition: compiler.h:522
union @382 key
Sense key.
Definition: crypto.h:284
void * memset(void *dest, int character, size_t len) __nonnull

References cipher_encrypt, cpu_to_be32, gcm_context::ctr, gcm_block::ctr, DBGC2, DBGC2_HDA, gcm_cache(), gcm_context::key, key, keylen, memset(), gcm_context::raw_cipher, gcm_context::raw_ctx, rc, and gcm_counter::value.

◆ gcm_setiv()

void gcm_setiv ( struct gcm_context context,
const void *  iv,
size_t  ivlen 
)

Set initialisation vector.

Parameters
ctxContext
ivInitialisation vector
ivlenInitialisation vector length

Definition at line 474 of file gcm.c.

474  {
475 
476  /* Reset non-key state */
477  memset ( context, 0, gcm_offset ( key ) );
478  build_assert ( gcm_offset ( key ) > gcm_offset ( hash ) );
479  build_assert ( gcm_offset ( key ) > gcm_offset ( len ) );
480  build_assert ( gcm_offset ( key ) > gcm_offset ( ctr ) );
481  build_assert ( gcm_offset ( key ) < gcm_offset ( raw_cipher ) );
482  build_assert ( gcm_offset ( key ) < gcm_offset ( raw_ctx ) );
483 
484  /* Reset counter */
485  context->ctr.ctr.value = cpu_to_be32 ( 1 );
486 
487  /* Process initialisation vector */
488  if ( ivlen == sizeof ( context->ctr.ctr.iv ) ) {
489 
490  /* Initialisation vector is exactly 96 bits, use it as-is */
491  memcpy ( context->ctr.ctr.iv, iv, ivlen );
492 
493  } else {
494 
495  /* Calculate hash over initialisation vector */
496  gcm_process ( context, iv, NULL, ivlen, GCM_FL_IV );
497  gcm_hash ( context, &context->ctr );
498  assert ( context->len.len.add == 0 );
499 
500  /* Reset non-key, non-counter state */
501  memset ( context, 0, gcm_offset ( ctr ) );
502  build_assert ( gcm_offset ( ctr ) > gcm_offset ( hash ) );
503  build_assert ( gcm_offset ( ctr ) > gcm_offset ( len ) );
504  build_assert ( gcm_offset ( ctr ) < gcm_offset ( key ) );
505  build_assert ( gcm_offset ( ctr ) < gcm_offset ( raw_cipher ) );
506  build_assert ( gcm_offset ( ctr ) < gcm_offset ( raw_ctx ) );
507  }
508 
509  DBGC2 ( context, "GCM %p Y[0]:\n", context );
510  DBGC2_HDA ( context, 0, &context->ctr, sizeof ( context->ctr ) );
511 }
union gcm_block len
Accumulated lengths.
Definition: gcm.h:50
struct gcm_lengths len
Lengths.
Definition: gcm.h:42
uint8_t iv[12]
Initialisation vector.
Definition: gcm.h:18
static void gcm_hash(struct gcm_context *context, union gcm_block *hash)
Construct hash.
Definition: gcm.c:387
#define GCM_FL_IV
Calculate hash over an initialisation vector value.
Definition: gcm.c:57
uint32_t value
Counter value.
Definition: gcm.h:20
static void const void size_t ivlen
Definition: crypto.h:239
void * memcpy(void *dest, const void *src, size_t len) __nonnull
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define build_assert(condition)
Assert a condition at build time (after dead code elimination)
Definition: assert.h:76
static void gcm_process(struct gcm_context *context, const void *src, void *dst, size_t len, unsigned int flags)
Encrypt/decrypt/authenticate data.
Definition: gcm.c:319
struct gcm_counter ctr
Counter.
Definition: gcm.h:40
#define DBGC2_HDA(...)
Definition: compiler.h:523
pseudo_bit_t hash[0x00010]
Hash algorithm.
Definition: arbel.h:13
#define gcm_offset(field)
Offset of a field within GCM context.
Definition: gcm.c:113
#define cpu_to_be32(value)
Definition: byteswap.h:110
union gcm_block ctr
Counter (Y)
Definition: gcm.h:52
uint32_t len
Length.
Definition: ena.h:14
static void const void * iv
Definition: crypto.h:238
#define DBGC2(...)
Definition: compiler.h:522
struct gcm_counter ctr
Counter.
Definition: gcm.h:18
uint64_t add
Additional data length.
Definition: gcm.h:26
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
union @382 key
Sense key.
Definition: crypto.h:284
void * memset(void *dest, int character, size_t len) __nonnull

References gcm_lengths::add, assert(), build_assert, cpu_to_be32, gcm_context::ctr, ctr, gcm_block::ctr, DBGC2, DBGC2_HDA, GCM_FL_IV, gcm_hash(), gcm_offset, gcm_process(), hash, gcm_counter::iv, iv, ivlen, key, len, gcm_context::len, gcm_block::len, memcpy(), memset(), NULL, and gcm_counter::value.

◆ gcm_encrypt()

void gcm_encrypt ( struct gcm_context context,
const void *  src,
void *  dst,
size_t  len 
)

Encrypt data.

Parameters
contextContext
srcData to encrypt
dstBuffer for encrypted data, or NULL for additional data
lenLength of data

Definition at line 521 of file gcm.c.

522  {
523 
524  /* Process data */
525  gcm_process ( context, src, dst, len, GCM_FL_ENCRYPT );
526 }
#define GCM_FL_ENCRYPT
Perform encryption.
Definition: gcm.c:48
static void const void void * dst
Definition: crypto.h:244
static void const void * src
Definition: crypto.h:244
static void gcm_process(struct gcm_context *context, const void *src, void *dst, size_t len, unsigned int flags)
Encrypt/decrypt/authenticate data.
Definition: gcm.c:319
uint32_t len
Length.
Definition: ena.h:14

References dst, GCM_FL_ENCRYPT, gcm_process(), len, and src.

◆ gcm_decrypt()

void gcm_decrypt ( struct gcm_context context,
const void *  src,
void *  dst,
size_t  len 
)

Decrypt data.

Parameters
contextContext
srcData to decrypt
dstBuffer for decrypted data, or NULL for additional data
lenLength of data

Definition at line 536 of file gcm.c.

537  {
538 
539  /* Process data */
540  gcm_process ( context, src, dst, len, 0 );
541 }
static void const void void * dst
Definition: crypto.h:244
static void const void * src
Definition: crypto.h:244
static void gcm_process(struct gcm_context *context, const void *src, void *dst, size_t len, unsigned int flags)
Encrypt/decrypt/authenticate data.
Definition: gcm.c:319
uint32_t len
Length.
Definition: ena.h:14

References dst, gcm_process(), len, and src.

Variable Documentation

◆ iv

uint8_t iv[12]

Initialisation vector.

Definition at line 12 of file gcm.h.

◆ value

uint32_t value

Counter value.

Definition at line 14 of file gcm.h.

◆ add

uint64_t add

Additional data length.

Definition at line 12 of file gcm.h.

Referenced by hvm_ioremap().

◆ data

uint64_t data

Data length.

Definition at line 14 of file gcm.h.

◆ byte

Raw bytes.

Definition at line 12 of file gcm.h.

◆ word

Raw words.

Definition at line 14 of file gcm.h.

◆ dword

Raw dwords.

Definition at line 16 of file gcm.h.

◆ ctr

struct gcm_counter ctr

Counter.

Definition at line 18 of file gcm.h.

Referenced by ccmp_ctr_xor(), and gcm_setiv().

◆ len

struct gcm_lengths len

Lengths.

Definition at line 20 of file gcm.h.

◆ __attribute__