iPXE
Data Structures | Macros | Functions
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)
 
 FILE_SECBOOT (PERMITTED)
 
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...
 

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 81 of file gcm.h.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED  )

◆ gcm_tag()

void gcm_tag ( struct gcm_context context,
union gcm_block tag 
)

Construct tag.

Parameters
contextContext
tagTag

Definition at line 409 of file gcm.c.

409  {
410  union gcm_block tmp;
412 
413  /* Construct hash */
414  gcm_hash ( context, tag );
415 
416  /* Construct encrypted initial counter value */
417  memcpy ( &tmp, &context->ctr, sizeof ( tmp ) );
418  offset = ( ( -context->len.len.data ) / ( 8 * sizeof ( tmp ) ) );
419  gcm_count ( &tmp, offset );
420  cipher_encrypt ( context->raw_cipher, &context->raw_ctx, &tmp,
421  &tmp, sizeof ( tmp ) );
422  DBGC2 ( context, "GCM %p E(K,Y[0]):\n", context );
423  DBGC2_HDA ( context, 0, &tmp, sizeof ( tmp ) );
424 
425  /* Construct tag */
426  gcm_xor_block ( &tmp, tag );
427  DBGC2 ( context, "GCM %p T:\n", context );
428  DBGC2_HDA ( context, 0, tag, sizeof ( *tag ) );
429 }
A GCM block.
Definition: gcm.h:33
union gcm_block len
Accumulated lengths.
Definition: gcm.h:51
uint8_t raw_ctx[0]
Underlying block cipher context.
Definition: gcm.h:59
struct gcm_lengths len
Lengths.
Definition: gcm.h:43
static void gcm_hash(struct gcm_context *context, union gcm_block *hash)
Construct hash.
Definition: gcm.c:388
static void gcm_count(union gcm_block *ctr, uint32_t delta)
Update GCM counter.
Definition: gcm.c:142
static void gcm_xor_block(const union gcm_block *src, union gcm_block *dst)
XOR whole data block in situ.
Definition: gcm.c:174
struct cipher_algorithm * raw_cipher
Underlying block cipher.
Definition: gcm.h:57
unsigned long tmp
Definition: linux_pci.h:65
#define cipher_encrypt(cipher, ctx, src, dst, len)
Definition: crypto.h:251
void * memcpy(void *dest, const void *src, size_t len) __nonnull
uint64_t data
Data length.
Definition: gcm.h:29
#define DBGC2_HDA(...)
Definition: compiler.h:523
unsigned int uint32_t
Definition: stdint.h:12
union gcm_block ctr
Counter (Y)
Definition: gcm.h:53
#define DBGC2(...)
Definition: compiler.h:522
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
uint64_t tag
Identity tag.
Definition: edd.h:31

References cipher_encrypt, gcm_context::ctr, gcm_lengths::data, DBGC2, DBGC2_HDA, gcm_count(), gcm_hash(), gcm_xor_block(), gcm_block::len, gcm_context::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 440 of file gcm.c.

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

References cipher_encrypt, cipher_setkey(), cpu_to_be32, gcm_context::ctr, gcm_block::ctr, DBGC2, DBGC2_HDA, gcm_cache(), key, gcm_context::key, 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 475 of file gcm.c.

475  {
476 
477  /* Reset non-key state */
478  memset ( context, 0, gcm_offset ( key ) );
479  build_assert ( gcm_offset ( key ) > gcm_offset ( hash ) );
480  build_assert ( gcm_offset ( key ) > gcm_offset ( len ) );
481  build_assert ( gcm_offset ( key ) > gcm_offset ( ctr ) );
482  build_assert ( gcm_offset ( key ) < gcm_offset ( raw_cipher ) );
483  build_assert ( gcm_offset ( key ) < gcm_offset ( raw_ctx ) );
484 
485  /* Reset counter */
486  context->ctr.ctr.value = cpu_to_be32 ( 1 );
487 
488  /* Process initialisation vector */
489  if ( ivlen == sizeof ( context->ctr.ctr.iv ) ) {
490 
491  /* Initialisation vector is exactly 96 bits, use it as-is */
492  memcpy ( context->ctr.ctr.iv, iv, ivlen );
493 
494  } else {
495 
496  /* Calculate hash over initialisation vector */
497  gcm_process ( context, iv, NULL, ivlen, GCM_FL_IV );
498  gcm_hash ( context, &context->ctr );
499  assert ( context->len.len.add == 0 );
500 
501  /* Reset non-key, non-counter state */
502  memset ( context, 0, gcm_offset ( ctr ) );
503  build_assert ( gcm_offset ( ctr ) > gcm_offset ( hash ) );
504  build_assert ( gcm_offset ( ctr ) > gcm_offset ( len ) );
505  build_assert ( gcm_offset ( ctr ) < gcm_offset ( key ) );
506  build_assert ( gcm_offset ( ctr ) < gcm_offset ( raw_cipher ) );
507  build_assert ( gcm_offset ( ctr ) < gcm_offset ( raw_ctx ) );
508  }
509 
510  DBGC2 ( context, "GCM %p Y[0]:\n", context );
511  DBGC2_HDA ( context, 0, &context->ctr, sizeof ( context->ctr ) );
512 }
pseudo_bit_t hash[0x00010]
Definition: arbel.h:13
union gcm_block len
Accumulated lengths.
Definition: gcm.h:51
struct gcm_lengths len
Lengths.
Definition: gcm.h:43
uint8_t iv[12]
Initialisation vector.
Definition: gcm.h:19
static void gcm_hash(struct gcm_context *context, union gcm_block *hash)
Construct hash.
Definition: gcm.c:388
#define GCM_FL_IV
Calculate hash over an initialisation vector value.
Definition: gcm.c:58
uint32_t value
Counter value.
Definition: gcm.h:21
u8 iv[16]
Initialization vector.
Definition: wpa.h:60
void * memcpy(void *dest, const void *src, size_t len) __nonnull
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
ring len
Length.
Definition: dwmac.h:231
#define build_assert(condition)
Assert a condition at build time (after dead code elimination)
Definition: assert.h:77
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:320
struct gcm_counter ctr
Counter.
Definition: gcm.h:41
#define DBGC2_HDA(...)
Definition: compiler.h:523
#define gcm_offset(field)
Offset of a field within GCM context.
Definition: gcm.c:114
#define cpu_to_be32(value)
Definition: byteswap.h:111
union gcm_block ctr
Counter (Y)
Definition: gcm.h:53
#define DBGC2(...)
Definition: compiler.h:522
uint64_t add
Additional data length.
Definition: gcm.h:27
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
union @391 key
Sense key.
Definition: scsi.h:18
void * memset(void *dest, int character, size_t len) __nonnull

References gcm_lengths::add, assert(), build_assert, cpu_to_be32, gcm_context::ctr, gcm_block::ctr, DBGC2, DBGC2_HDA, GCM_FL_IV, gcm_hash(), gcm_offset, gcm_process(), hash, gcm_counter::iv, iv, key, gcm_context::len, gcm_block::len, 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 522 of file gcm.c.

523  {
524 
525  /* Process data */
526  gcm_process ( context, src, dst, len, GCM_FL_ENCRYPT );
527 }
#define GCM_FL_ENCRYPT
Perform encryption.
Definition: gcm.c:49
static const void * src
Definition: string.h:48
ring len
Length.
Definition: dwmac.h:231
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:320

References 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 537 of file gcm.c.

538  {
539 
540  /* Process data */
541  gcm_process ( context, src, dst, len, 0 );
542 }
static const void * src
Definition: string.h:48
ring len
Length.
Definition: dwmac.h:231
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:320

References gcm_process(), len, and src.