iPXE
cbc.h
Go to the documentation of this file.
1 #ifndef _IPXE_CBC_H
2 #define _IPXE_CBC_H
3 
4 /** @file
5  *
6  * Cipher-block chaining
7  *
8  */
9 
10 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11 
12 #include <ipxe/crypto.h>
13 
14 /**
15  * Set key
16  *
17  * @v ctx Context
18  * @v key Key
19  * @v keylen Key length
20  * @v raw_cipher Underlying cipher algorithm
21  * @v cbc_ctx CBC context
22  * @ret rc Return status code
23  */
24 static inline int cbc_setkey ( void *ctx, const void *key, size_t keylen,
25  struct cipher_algorithm *raw_cipher,
26  void *cbc_ctx __unused ) {
27 
28  return cipher_setkey ( raw_cipher, ctx, key, keylen );
29 }
30 
31 /**
32  * Set initialisation vector
33  *
34  * @v ctx Context
35  * @v iv Initialisation vector
36  * @v ivlen Initialisation vector length
37  * @v raw_cipher Underlying cipher algorithm
38  * @v cbc_ctx CBC context
39  */
40 static inline void cbc_setiv ( void *ctx __unused,
41  const void *iv, size_t ivlen,
42  struct cipher_algorithm *raw_cipher,
43  void *cbc_ctx ) {
44  assert ( ivlen == raw_cipher->blocksize );
45  memcpy ( cbc_ctx, iv, raw_cipher->blocksize );
46 }
47 
48 extern void cbc_encrypt ( void *ctx, const void *src, void *dst,
49  size_t len, struct cipher_algorithm *raw_cipher,
50  void *cbc_ctx );
51 extern void cbc_decrypt ( void *ctx, const void *src, void *dst,
52  size_t len, struct cipher_algorithm *raw_cipher,
53  void *cbc_ctx );
54 
55 /**
56  * Create a cipher-block chaining mode of behaviour of an existing cipher
57  *
58  * @v _cbc_name Name for the new CBC cipher
59  * @v _cbc_cipher New cipher algorithm
60  * @v _raw_cipher Underlying cipher algorithm
61  * @v _raw_context Context structure for the underlying cipher
62  * @v _blocksize Cipher block size
63  */
64 #define CBC_CIPHER( _cbc_name, _cbc_cipher, _raw_cipher, _raw_context, \
65  _blocksize ) \
66 struct _cbc_name ## _context { \
67  _raw_context raw_ctx; \
68  uint8_t cbc_ctx[_blocksize]; \
69 }; \
70 static int _cbc_name ## _setkey ( void *ctx, const void *key, \
71  size_t keylen ) { \
72  struct _cbc_name ## _context * _cbc_name ## _ctx = ctx; \
73  return cbc_setkey ( &_cbc_name ## _ctx->raw_ctx, key, keylen, \
74  &_raw_cipher, &_cbc_name ## _ctx->cbc_ctx );\
75 } \
76 static void _cbc_name ## _setiv ( void *ctx, const void *iv, \
77  size_t ivlen ) { \
78  struct _cbc_name ## _context * _cbc_name ## _ctx = ctx; \
79  cbc_setiv ( &_cbc_name ## _ctx->raw_ctx, iv, ivlen, \
80  &_raw_cipher, &_cbc_name ## _ctx->cbc_ctx ); \
81 } \
82 static void _cbc_name ## _encrypt ( void *ctx, const void *src, \
83  void *dst, size_t len ) { \
84  struct _cbc_name ## _context * _cbc_name ## _ctx = ctx; \
85  cbc_encrypt ( &_cbc_name ## _ctx->raw_ctx, src, dst, len, \
86  &_raw_cipher, &_cbc_name ## _ctx->cbc_ctx ); \
87 } \
88 static void _cbc_name ## _decrypt ( void *ctx, const void *src, \
89  void *dst, size_t len ) { \
90  struct _cbc_name ## _context * _cbc_name ## _ctx = ctx; \
91  cbc_decrypt ( &_cbc_name ## _ctx->raw_ctx, src, dst, len, \
92  &_raw_cipher, &_cbc_name ## _ctx->cbc_ctx ); \
93 } \
94 struct cipher_algorithm _cbc_cipher = { \
95  .name = #_cbc_name, \
96  .ctxsize = sizeof ( struct _cbc_name ## _context ), \
97  .blocksize = _blocksize, \
98  .alignsize = _blocksize, \
99  .authsize = 0, \
100  .setkey = _cbc_name ## _setkey, \
101  .setiv = _cbc_name ## _setiv, \
102  .encrypt = _cbc_name ## _encrypt, \
103  .decrypt = _cbc_name ## _decrypt, \
104  .auth = cipher_null_auth, \
105 };
106 
107 #endif /* _IPXE_CBC_H */
size_t blocksize
Block size.
Definition: crypto.h:60
static const void * src
Definition: string.h:47
void cbc_encrypt(void *ctx, const void *src, void *dst, size_t len, struct cipher_algorithm *raw_cipher, void *cbc_ctx)
Encrypt data.
Definition: cbc.c:66
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
Cryptographic API.
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
u8 iv[16]
Initialization vector.
Definition: wpa.h:60
void * memcpy(void *dest, const void *src, size_t len) __nonnull
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
A cipher algorithm.
Definition: crypto.h:50
void cbc_decrypt(void *ctx, const void *src, void *dst, size_t len, struct cipher_algorithm *raw_cipher, void *cbc_ctx)
Decrypt data.
Definition: cbc.c:92
static void cbc_setiv(void *ctx __unused, const void *iv, size_t ivlen, struct cipher_algorithm *raw_cipher, void *cbc_ctx)
Set initialisation vector.
Definition: cbc.h:40
static int cbc_setkey(void *ctx, const void *key, size_t keylen, struct cipher_algorithm *raw_cipher, void *cbc_ctx __unused)
Set key.
Definition: cbc.h:24
uint32_t len
Length.
Definition: ena.h:14
union @383 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:217