iPXE
crypto.h
Go to the documentation of this file.
1 #ifndef _IPXE_CRYPTO_H
2 #define _IPXE_CRYPTO_H
3 
4 /** @file
5  *
6  * Cryptographic API
7  *
8  */
9 
10 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11 
12 #include <stdint.h>
13 #include <stddef.h>
14 
15 /** A message digest algorithm */
17  /** Algorithm name */
18  const char *name;
19  /** Context size */
20  size_t ctxsize;
21  /** Block size */
22  size_t blocksize;
23  /** Digest size */
24  size_t digestsize;
25  /** Initialise digest
26  *
27  * @v ctx Context
28  */
29  void ( * init ) ( void *ctx );
30  /** Update digest with new data
31  *
32  * @v ctx Context
33  * @v src Data to digest
34  * @v len Length of data
35  *
36  * @v len is not necessarily a multiple of @c blocksize.
37  */
38  void ( * update ) ( void *ctx, const void *src, size_t len );
39  /** Finalise digest
40  *
41  * @v ctx Context
42  * @v out Buffer for digest output
43  */
44  void ( * final ) ( void *ctx, void *out );
45 };
46 
47 /** A cipher algorithm */
49  /** Algorithm name */
50  const char *name;
51  /** Context size */
52  size_t ctxsize;
53  /** Block size */
54  size_t blocksize;
55  /** Set key
56  *
57  * @v ctx Context
58  * @v key Key
59  * @v keylen Key length
60  * @ret rc Return status code
61  */
62  int ( * setkey ) ( void *ctx, const void *key, size_t keylen );
63  /** Set initialisation vector
64  *
65  * @v ctx Context
66  * @v iv Initialisation vector
67  */
68  void ( * setiv ) ( void *ctx, const void *iv );
69  /** Encrypt data
70  *
71  * @v ctx Context
72  * @v src Data to encrypt
73  * @v dst Buffer for encrypted data
74  * @v len Length of data
75  *
76  * @v len is guaranteed to be a multiple of @c blocksize.
77  */
78  void ( * encrypt ) ( void *ctx, const void *src, void *dst,
79  size_t len );
80  /** Decrypt data
81  *
82  * @v ctx Context
83  * @v src Data to decrypt
84  * @v dst Buffer for decrypted data
85  * @v len Length of data
86  *
87  * @v len is guaranteed to be a multiple of @c blocksize.
88  */
89  void ( * decrypt ) ( void *ctx, const void *src, void *dst,
90  size_t len );
91 };
92 
93 /** A public key algorithm */
95  /** Algorithm name */
96  const char *name;
97  /** Context size */
98  size_t ctxsize;
99  /** Initialise algorithm
100  *
101  * @v ctx Context
102  * @v key Key
103  * @v key_len Length of key
104  * @ret rc Return status code
105  */
106  int ( * init ) ( void *ctx, const void *key, size_t key_len );
107  /** Calculate maximum output length
108  *
109  * @v ctx Context
110  * @ret max_len Maximum output length
111  */
112  size_t ( * max_len ) ( void *ctx );
113  /** Encrypt
114  *
115  * @v ctx Context
116  * @v plaintext Plaintext
117  * @v plaintext_len Length of plaintext
118  * @v ciphertext Ciphertext
119  * @ret ciphertext_len Length of ciphertext, or negative error
120  */
121  int ( * encrypt ) ( void *ctx, const void *data, size_t len,
122  void *out );
123  /** Decrypt
124  *
125  * @v ctx Context
126  * @v ciphertext Ciphertext
127  * @v ciphertext_len Ciphertext length
128  * @v plaintext Plaintext
129  * @ret plaintext_len Plaintext length, or negative error
130  */
131  int ( * decrypt ) ( void *ctx, const void *data, size_t len,
132  void *out );
133  /** Sign digest value
134  *
135  * @v ctx Context
136  * @v digest Digest algorithm
137  * @v value Digest value
138  * @v signature Signature
139  * @ret signature_len Signature length, or negative error
140  */
141  int ( * sign ) ( void *ctx, struct digest_algorithm *digest,
142  const void *value, void *signature );
143  /** Verify signed digest value
144  *
145  * @v ctx Context
146  * @v digest Digest algorithm
147  * @v value Digest value
148  * @v signature Signature
149  * @v signature_len Signature length
150  * @ret rc Return status code
151  */
152  int ( * verify ) ( void *ctx, struct digest_algorithm *digest,
153  const void *value, const void *signature,
154  size_t signature_len );
155  /** Finalise algorithm
156  *
157  * @v ctx Context
158  */
159  void ( * final ) ( void *ctx );
160  /** Check that public key matches private key
161  *
162  * @v private_key Private key
163  * @v private_key_len Private key length
164  * @v public_key Public key
165  * @v public_key_len Public key length
166  * @ret rc Return status code
167  */
168  int ( * match ) ( const void *private_key, size_t private_key_len,
169  const void *public_key, size_t public_key_len );
170 };
171 
172 static inline void digest_init ( struct digest_algorithm *digest,
173  void *ctx ) {
174  digest->init ( ctx );
175 }
176 
177 static inline void digest_update ( struct digest_algorithm *digest,
178  void *ctx, const void *data, size_t len ) {
179  digest->update ( ctx, data, len );
180 }
181 
182 static inline void digest_final ( struct digest_algorithm *digest,
183  void *ctx, void *out ) {
184  digest->final ( ctx, out );
185 }
186 
187 static inline int cipher_setkey ( struct cipher_algorithm *cipher,
188  void *ctx, const void *key, size_t keylen ) {
189  return cipher->setkey ( ctx, key, keylen );
190 }
191 
192 static inline void cipher_setiv ( struct cipher_algorithm *cipher,
193  void *ctx, const void *iv ) {
194  cipher->setiv ( ctx, iv );
195 }
196 
197 static inline void cipher_encrypt ( struct cipher_algorithm *cipher,
198  void *ctx, const void *src, void *dst,
199  size_t len ) {
200  cipher->encrypt ( ctx, src, dst, len );
201 }
202 #define cipher_encrypt( cipher, ctx, src, dst, len ) do { \
203  assert ( ( (len) & ( (cipher)->blocksize - 1 ) ) == 0 ); \
204  cipher_encrypt ( (cipher), (ctx), (src), (dst), (len) ); \
205  } while ( 0 )
206 
207 static inline void cipher_decrypt ( struct cipher_algorithm *cipher,
208  void *ctx, const void *src, void *dst,
209  size_t len ) {
210  cipher->decrypt ( ctx, src, dst, len );
211 }
212 #define cipher_decrypt( cipher, ctx, src, dst, len ) do { \
213  assert ( ( (len) & ( (cipher)->blocksize - 1 ) ) == 0 ); \
214  cipher_decrypt ( (cipher), (ctx), (src), (dst), (len) ); \
215  } while ( 0 )
216 
217 static inline int is_stream_cipher ( struct cipher_algorithm *cipher ) {
218  return ( cipher->blocksize == 1 );
219 }
220 
221 static inline int pubkey_init ( struct pubkey_algorithm *pubkey, void *ctx,
222  const void *key, size_t key_len ) {
223  return pubkey->init ( ctx, key, key_len );
224 }
225 
226 static inline size_t pubkey_max_len ( struct pubkey_algorithm *pubkey,
227  void *ctx ) {
228  return pubkey->max_len ( ctx );
229 }
230 
231 static inline int pubkey_encrypt ( struct pubkey_algorithm *pubkey, void *ctx,
232  const void *data, size_t len, void *out ) {
233  return pubkey->encrypt ( ctx, data, len, out );
234 }
235 
236 static inline int pubkey_decrypt ( struct pubkey_algorithm *pubkey, void *ctx,
237  const void *data, size_t len, void *out ) {
238  return pubkey->decrypt ( ctx, data, len, out );
239 }
240 
241 static inline int pubkey_sign ( struct pubkey_algorithm *pubkey, void *ctx,
242  struct digest_algorithm *digest,
243  const void *value, void *signature ) {
244  return pubkey->sign ( ctx, digest, value, signature );
245 }
246 
247 static inline int pubkey_verify ( struct pubkey_algorithm *pubkey, void *ctx,
248  struct digest_algorithm *digest,
249  const void *value, const void *signature,
250  size_t signature_len ) {
251  return pubkey->verify ( ctx, digest, value, signature, signature_len );
252 }
253 
254 static inline void pubkey_final ( struct pubkey_algorithm *pubkey, void *ctx ) {
255  pubkey->final ( ctx );
256 }
257 
258 static inline int pubkey_match ( struct pubkey_algorithm *pubkey,
259  const void *private_key,
260  size_t private_key_len, const void *public_key,
261  size_t public_key_len ) {
262  return pubkey->match ( private_key, private_key_len, public_key,
263  public_key_len );
264 }
265 
266 extern struct digest_algorithm digest_null;
267 extern struct cipher_algorithm cipher_null;
268 extern struct pubkey_algorithm pubkey_null;
269 
270 #endif /* _IPXE_CRYPTO_H */
size_t blocksize
Block size.
Definition: crypto.h:54
static void digest_update(struct digest_algorithm *digest, void *ctx, const void *data, size_t len)
Definition: crypto.h:177
void(* decrypt)(void *ctx, const void *src, void *dst, size_t len)
Decrypt data.
Definition: crypto.h:89
struct cipher_algorithm cipher_null
Definition: crypto_null.c:79
static int pubkey_decrypt(struct pubkey_algorithm *pubkey, void *ctx, const void *data, size_t len, void *out)
Definition: crypto.h:236
__SIZE_TYPE__ size_t
Definition: stdint.h:6
static int pubkey_match(struct pubkey_algorithm *pubkey, const void *private_key, size_t private_key_len, const void *public_key, size_t public_key_len)
Definition: crypto.h:258
static void digest_final(struct digest_algorithm *digest, void *ctx, void *out)
Definition: crypto.h:182
void(* init)(void *ctx)
Initialise digest.
Definition: crypto.h:29
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
struct md4_digest digest
Digest of data already processed.
Definition: md4.h:12
u8 iv[16]
Initialization vector.
Definition: wpa.h:60
size_t ctxsize
Context size.
Definition: crypto.h:98
struct digest_algorithm digest_null
Definition: crypto_null.c:48
int(* match)(const void *private_key, size_t private_key_len, const void *public_key, size_t public_key_len)
Check that public key matches private key.
Definition: crypto.h:168
void(* encrypt)(void *ctx, const void *src, void *dst, size_t len)
Encrypt data.
Definition: crypto.h:78
#define cipher_encrypt(cipher, ctx, src, dst, len)
Definition: crypto.h:202
int(* verify)(void *ctx, struct digest_algorithm *digest, const void *value, const void *signature, size_t signature_len)
Verify signed digest value.
Definition: crypto.h:152
static size_t pubkey_max_len(struct pubkey_algorithm *pubkey, void *ctx)
Definition: crypto.h:226
u8 signature
Definition: CIB_PRM.h:35
size_t blocksize
Block size.
Definition: crypto.h:22
static int pubkey_verify(struct pubkey_algorithm *pubkey, void *ctx, struct digest_algorithm *digest, const void *value, const void *signature, size_t signature_len)
Definition: crypto.h:247
char private_key_len[]
void(* setiv)(void *ctx, const void *iv)
Set initialisation vector.
Definition: crypto.h:68
__be32 out[4]
Definition: CIB_PRM.h:36
void(* update)(void *ctx, const void *src, size_t len)
Update digest with new data.
Definition: crypto.h:38
struct pubkey_algorithm pubkey_null
Definition: crypto_null.c:131
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
static __always_inline void off_t userptr_t src
Definition: efi_uaccess.h:66
static void digest_init(struct digest_algorithm *digest, void *ctx)
Definition: crypto.h:172
int(* encrypt)(void *ctx, const void *data, size_t len, void *out)
Encrypt.
Definition: crypto.h:121
void(* final)(void *ctx)
Finalise algorithm.
Definition: crypto.h:159
size_t(* max_len)(void *ctx)
Calculate maximum output length.
Definition: crypto.h:112
static int is_stream_cipher(struct cipher_algorithm *cipher)
Definition: crypto.h:217
size_t ctxsize
Context size.
Definition: crypto.h:52
#define cipher_decrypt(cipher, ctx, src, dst, len)
Definition: crypto.h:212
int(* decrypt)(void *ctx, const void *data, size_t len, void *out)
Decrypt.
Definition: crypto.h:131
struct asn1_cursor private_key
Private key.
Definition: privkey.c:67
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
uint32_t len
Length.
Definition: ena.h:14
size_t ctxsize
Context size.
Definition: crypto.h:20
int(* setkey)(void *ctx, const void *key, size_t keylen)
Set key.
Definition: crypto.h:62
size_t digestsize
Digest size.
Definition: crypto.h:24
const char * name
Algorithm name.
Definition: crypto.h:18
static int pubkey_sign(struct pubkey_algorithm *pubkey, void *ctx, struct digest_algorithm *digest, const void *value, void *signature)
Definition: crypto.h:241
int(* init)(void *ctx, const void *key, size_t key_len)
Initialise algorithm.
Definition: crypto.h:106
A message digest algorithm.
Definition: crypto.h:16
A cipher algorithm.
Definition: crypto.h:48
static int pubkey_encrypt(struct pubkey_algorithm *pubkey, void *ctx, const void *data, size_t len, void *out)
Definition: crypto.h:231
static void pubkey_final(struct pubkey_algorithm *pubkey, void *ctx)
Definition: crypto.h:254
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12
const char * name
Algorithm name.
Definition: crypto.h:50
A public key algorithm.
Definition: crypto.h:94
union @375 key
Sense key.
Definition: scsi.h:18
const char * name
Algorithm name.
Definition: crypto.h:96
static int cipher_setkey(struct cipher_algorithm *cipher, void *ctx, const void *key, size_t keylen)
Definition: crypto.h:187
static void cipher_setiv(struct cipher_algorithm *cipher, void *ctx, const void *iv)
Definition: crypto.h:192
static int pubkey_init(struct pubkey_algorithm *pubkey, void *ctx, const void *key, size_t key_len)
Definition: crypto.h:221
int(* sign)(void *ctx, struct digest_algorithm *digest, const void *value, void *signature)
Sign digest value.
Definition: crypto.h:141