iPXE
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_context_t(ctxsize)
 A GCM mode context.
#define GCM_CIPHER(_gcm_name, _gcm_cipher, _raw_cipher, _raw_context, _blocksize)
 Create a GCM mode of behaviour of an existing cipher.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FILE_SECBOOT (PERMITTED)
int gcm_setkey (struct cipher_algorithm *cipher, void *ctx, const void *key, size_t keylen)
 Set key.
int gcm_setiv (struct cipher_algorithm *cipher, void *ctx, const void *iv, size_t ivlen)
 Set initialisation vector.
void gcm_encrypt (struct cipher_algorithm *cipher, void *ctx, const void *src, void *dst, size_t len)
 Encrypt data.
void gcm_decrypt (struct cipher_algorithm *cipher, void *ctx, const void *src, void *dst, size_t len)
 Decrypt data.
void gcm_auth (struct cipher_algorithm *cipher, void *ctx, void *auth)
 Generate authentication tag.

Detailed Description

Galois/Counter Mode (GCM).

Definition in file gcm.h.

Macro Definition Documentation

◆ gcm_context_t

#define gcm_context_t ( ctxsize)
Value:
struct { \
struct gcm_context gcm; \
uint8_t raw[ (ctxsize) - \
sizeof ( struct gcm_context ) ]; \
}
__be32 raw[7]
Definition CIB_PRM.h:0
unsigned char uint8_t
Definition stdint.h:10
GCM context.
Definition gcm.h:47

A GCM mode context.

Definition at line 61 of file gcm.h.

61#define gcm_context_t( ctxsize ) \
62 struct { \
63 struct gcm_context gcm; \
64 uint8_t raw[ (ctxsize) - \
65 sizeof ( struct gcm_context ) ]; \
66 }

Referenced by gcm_auth(), gcm_decrypt(), gcm_encrypt(), gcm_process(), gcm_setiv(), and gcm_setkey().

◆ GCM_CIPHER

#define GCM_CIPHER ( _gcm_name,
_gcm_cipher,
_raw_cipher,
_raw_context,
_blocksize )
Value:
static_assert ( _blocksize == sizeof ( union gcm_block ) ); \
struct cipher_algorithm _gcm_cipher = { \
.name = #_gcm_name, \
.ctxsize = ( sizeof ( struct gcm_context ) + \
sizeof ( _raw_context ) ), \
.blocksize = 1, \
.alignsize = sizeof ( union gcm_block ), \
.authsize = sizeof ( union gcm_block ), \
.confidential = 1, \
.setkey = gcm_setkey, \
.setiv = gcm_setiv, \
.encrypt = gcm_encrypt, \
.decrypt = gcm_decrypt, \
.auth = gcm_auth, \
.priv = &_raw_cipher, \
};
int gcm_setiv(struct cipher_algorithm *cipher, void *ctx, const void *iv, size_t ivlen)
Set initialisation vector.
Definition gcm.c:457
void gcm_decrypt(struct cipher_algorithm *cipher, void *ctx, const void *src, void *dst, size_t len)
Decrypt data.
Definition gcm.c:524
void gcm_auth(struct cipher_algorithm *cipher, void *ctx, void *auth)
Generate authentication tag.
Definition gcm.c:540
void gcm_encrypt(struct cipher_algorithm *cipher, void *ctx, const void *src, void *dst, size_t len)
Encrypt data.
Definition gcm.c:506
int gcm_setkey(struct cipher_algorithm *cipher, void *ctx, const void *key, size_t keylen)
Set key.
Definition gcm.c:418
A cipher algorithm.
Definition crypto.h:58
A GCM block.
Definition gcm.h:33

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

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ gcm_setkey()

int gcm_setkey ( struct cipher_algorithm * cipher,
void * ctx,
const void * key,
size_t keylen )
extern

Set key.

Parameters
cipherCipher algorithm
ctxContext
keyKey
keylenKey length
Return values
rcReturn status code

Definition at line 418 of file gcm.c.

419 {
420 struct cipher_algorithm *raw_cipher = cipher->priv;
421 gcm_context_t ( cipher->ctxsize ) *context = ctx;
422 int rc;
423
424 /* Initialise GCM context */
425 memset ( &context->gcm, 0, sizeof ( context->gcm ) );
426
427 /* Set underlying block cipher key */
428 if ( ( rc = cipher_setkey ( raw_cipher, context->raw, key,
429 keylen ) ) != 0 )
430 return rc;
431
432 /* Construct GCM hash key */
433 cipher_encrypt ( raw_cipher, context->raw, &context->gcm.ctr,
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 ) );
438
439 /* Reset counter */
440 context->gcm.ctr.ctr.value = cpu_to_be32 ( 1 );
441
442 /* Construct cached tables */
443 gcm_cache ( &context->gcm.key );
444
445 return 0;
446}
struct golan_eq_context ctx
Definition CIB_PRM.h:0
union @162305117151260234136356364136041353210355154177 key
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
static void gcm_cache(const union gcm_block *key)
Construct cached tables.
Definition gcm.c:214
#define gcm_context_t(ctxsize)
A GCM mode context.
Definition gcm.h:61
#define DBGC2(...)
Definition compiler.h:547
#define DBGC2_HDA(...)
Definition compiler.h:548
#define cpu_to_be32(value)
Definition byteswap.h:111
static int cipher_setkey(struct cipher_algorithm *cipher, void *ctx, const void *key, size_t keylen)
Definition crypto.h:310
#define cipher_encrypt(cipher, ctx, src, dst, len)
Definition crypto.h:326
void * memset(void *dest, int character, size_t len) __nonnull
void * priv
Algorithm private data.
Definition crypto.h:138
size_t ctxsize
Context size.
Definition crypto.h:62

References cipher_encrypt, cipher_setkey(), cpu_to_be32, ctx, cipher_algorithm::ctxsize, DBGC2, DBGC2_HDA, gcm_cache(), gcm_context_t, key, memset(), cipher_algorithm::priv, and rc.

◆ gcm_setiv()

int gcm_setiv ( struct cipher_algorithm * cipher,
void * ctx,
const void * iv,
size_t ivlen )
extern

Set initialisation vector.

Parameters
cipherCipher algorithm
ctxContext
ivInitialisation vector
ivlenInitialisation vector length
Return values
rcReturn status code

Definition at line 457 of file gcm.c.

458 {
459 gcm_context_t ( cipher->ctxsize ) *context = ctx;
460
461 /* Reset non-key state */
462 memset ( &context->gcm, 0, gcm_offset ( key ) );
465 build_assert ( gcm_offset ( key ) > gcm_offset ( ctr ) );
466
467 /* Reset counter */
468 context->gcm.ctr.ctr.value = cpu_to_be32 ( 1 );
469
470 /* Process initialisation vector */
471 if ( ivlen == sizeof ( context->gcm.ctr.ctr.iv ) ) {
472
473 /* Initialisation vector is exactly 96 bits, use it as-is */
474 memcpy ( context->gcm.ctr.ctr.iv, iv, ivlen );
475
476 } else {
477
478 /* Calculate hash over initialisation vector */
479 context->gcm.flags = GCM_FL_IV;
480 gcm_process ( cipher, ctx, iv, NULL, ivlen );
481 gcm_hash ( &context->gcm, &context->gcm.ctr );
482 assert ( context->gcm.len.len.add == 0 );
483
484 /* Reset non-key, non-counter state */
485 memset ( &context->gcm, 0, gcm_offset ( ctr ) );
486 build_assert ( gcm_offset ( ctr ) > gcm_offset ( hash ) );
487 build_assert ( gcm_offset ( ctr ) > gcm_offset ( len ) );
488 build_assert ( gcm_offset ( ctr ) < gcm_offset ( key ) );
489 }
490
491 DBGC2 ( context, "GCM %p Y[0]:\n", context );
492 DBGC2_HDA ( context, 0, &context->gcm.ctr,
493 sizeof ( context->gcm.ctr ) );
494 return 0;
495}
#define NULL
NULL pointer (VOID *).
Definition Base.h:321
pseudo_bit_t hash[0x00010]
Definition arbel.h:2
#define build_assert(condition)
Assert a condition at build time (after dead code elimination).
Definition assert.h:88
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:61
ring len
Length.
Definition dwmac.h:226
static void gcm_hash(struct gcm_context *context, union gcm_block *hash)
Construct hash.
Definition gcm.c:317
#define gcm_offset(field)
Offset of a field within GCM context.
Definition gcm.c:114
static void gcm_process(struct cipher_algorithm *cipher, void *ctx, const void *src, void *dst, size_t len)
Encrypt/decrypt/authenticate data.
Definition gcm.c:342
#define GCM_FL_IV
Calculate hash over an initialisation vector value.
Definition gcm.c:58
void * memcpy(void *dest, const void *src, size_t len) __nonnull
u8 iv[16]
Initialization vector.
Definition wpa.h:33

References assert, build_assert, cpu_to_be32, ctx, cipher_algorithm::ctxsize, DBGC2, DBGC2_HDA, gcm_context_t, GCM_FL_IV, gcm_hash(), gcm_offset, gcm_process(), hash, iv, key, len, memcpy(), memset(), and NULL.

◆ gcm_encrypt()

void gcm_encrypt ( struct cipher_algorithm * cipher,
void * ctx,
const void * src,
void * dst,
size_t len )
extern

Encrypt data.

Parameters
cipherCipher algorithm
ctxContext
srcData to encrypt
dstBuffer for encrypted data, or NULL for additional data
lenLength of data

Definition at line 506 of file gcm.c.

507 {
508 gcm_context_t ( cipher->ctxsize ) *context = ctx;
509
510 /* Process data */
511 context->gcm.flags = GCM_FL_ENCRYPT;
512 gcm_process ( cipher, ctx, src, dst, len );
513}
static const void * src
Definition string.h:48
#define GCM_FL_ENCRYPT
Perform encryption.
Definition gcm.c:49

References ctx, cipher_algorithm::ctxsize, gcm_context_t, GCM_FL_ENCRYPT, gcm_process(), len, and src.

◆ gcm_decrypt()

void gcm_decrypt ( struct cipher_algorithm * cipher,
void * ctx,
const void * src,
void * dst,
size_t len )
extern

Decrypt data.

Parameters
cipherCipher algorithm
ctxContext
srcData to decrypt
dstBuffer for decrypted data, or NULL for additional data
lenLength of data

Definition at line 524 of file gcm.c.

525 {
526 gcm_context_t ( cipher->ctxsize ) *context = ctx;
527
528 /* Process data */
529 context->gcm.flags = 0;
530 gcm_process ( cipher, ctx, src, dst, len );
531}

References ctx, cipher_algorithm::ctxsize, gcm_context_t, gcm_process(), len, and src.

◆ gcm_auth()

void gcm_auth ( struct cipher_algorithm * cipher,
void * ctx,
void * auth )
extern

Generate authentication tag.

Parameters
cipherCipher algorithm
ctxContext
authAuthentication tag

Definition at line 540 of file gcm.c.

540 {
541 struct cipher_algorithm *raw_cipher = cipher->priv;
542 gcm_context_t ( cipher->ctxsize ) *context = ctx;
543 union gcm_block *tag = auth;
544 union gcm_block tmp;
546
547 /* Construct hash */
548 gcm_hash ( &context->gcm, tag );
549
550 /* Construct encrypted initial counter value */
551 memcpy ( &tmp, &context->gcm.ctr, sizeof ( tmp ) );
552 offset = ( ( -context->gcm.len.len.data ) / ( 8 * sizeof ( tmp ) ) );
553 gcm_count ( &tmp, offset );
554 cipher_encrypt ( raw_cipher, &context->raw, &tmp, &tmp,
555 sizeof ( tmp ) );
556 DBGC2 ( context, "GCM %p E(K,Y[0]):\n", context );
557 DBGC2_HDA ( context, 0, &tmp, sizeof ( tmp ) );
558
559 /* Construct tag */
560 gcm_xor_block ( &tmp, tag );
561 DBGC2 ( context, "GCM %p T:\n", context );
562 DBGC2_HDA ( context, 0, tag, sizeof ( *tag ) );
563}
unsigned int uint32_t
Definition stdint.h:12
uint16_t offset
Offset to command line.
Definition bzimage.h:3
uint64_t tag
Identity tag.
Definition edd.h:1
static void gcm_xor_block(const union gcm_block *src, union gcm_block *dst)
XOR whole data block in situ.
Definition gcm.c:174
static void gcm_count(union gcm_block *ctr, uint32_t delta)
Update GCM counter.
Definition gcm.c:142
unsigned long tmp
Definition linux_pci.h:65

References cipher_encrypt, ctx, cipher_algorithm::ctxsize, DBGC2, DBGC2_HDA, gcm_context_t, gcm_count(), gcm_hash(), gcm_xor_block(), memcpy(), offset, cipher_algorithm::priv, tag, and tmp.