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 #include <assert.h>
15 #include <ipxe/asn1.h>
16 
17 /** A message digest algorithm */
19  /** Algorithm name */
20  const char *name;
21  /** Context size */
22  size_t ctxsize;
23  /** Block size */
24  size_t blocksize;
25  /** Digest size */
26  size_t digestsize;
27  /** Initialise digest
28  *
29  * @v ctx Context
30  */
31  void ( * init ) ( void *ctx );
32  /** Update digest with new data
33  *
34  * @v ctx Context
35  * @v src Data to digest
36  * @v len Length of data
37  *
38  * @v len is not necessarily a multiple of @c blocksize.
39  */
40  void ( * update ) ( void *ctx, const void *src, size_t len );
41  /** Finalise digest
42  *
43  * @v ctx Context
44  * @v out Buffer for digest output
45  */
46  void ( * final ) ( void *ctx, void *out );
47 };
48 
49 /** A cipher algorithm */
51  /** Algorithm name */
52  const char *name;
53  /** Context size */
54  size_t ctxsize;
55  /** Block size
56  *
57  * Every call to encrypt() or decrypt() must be for a multiple
58  * of this size.
59  */
60  size_t blocksize;
61  /** Alignment size
62  *
63  * Every call to encrypt() or decrypt() must begin at a
64  * multiple of this offset from the start of the stream.
65  * (Equivalently: all but the last call to encrypt() or
66  * decrypt() must be for a multiple of this size.)
67  *
68  * For ciphers supporting additional data, the main data
69  * stream and additional data stream are both considered to
70  * begin at offset zero.
71  */
72  size_t alignsize;
73  /** Authentication tag size */
74  size_t authsize;
75  /** Set key
76  *
77  * @v ctx Context
78  * @v key Key
79  * @v keylen Key length
80  * @ret rc Return status code
81  */
82  int ( * setkey ) ( void *ctx, const void *key, size_t keylen );
83  /** Set initialisation vector
84  *
85  * @v ctx Context
86  * @v iv Initialisation vector
87  * @v ivlen Initialisation vector length
88  */
89  void ( * setiv ) ( void *ctx, const void *iv, size_t ivlen );
90  /** Encrypt data
91  *
92  * @v ctx Context
93  * @v src Data to encrypt
94  * @v dst Buffer for encrypted data, or NULL for additional data
95  * @v len Length of data
96  *
97  * @v len is guaranteed to be a multiple of @c blocksize.
98  */
99  void ( * encrypt ) ( void *ctx, const void *src, void *dst,
100  size_t len );
101  /** Decrypt data
102  *
103  * @v ctx Context
104  * @v src Data to decrypt
105  * @v dst Buffer for decrypted data, or NULL for additional data
106  * @v len Length of data
107  *
108  * @v len is guaranteed to be a multiple of @c blocksize.
109  */
110  void ( * decrypt ) ( void *ctx, const void *src, void *dst,
111  size_t len );
112  /** Generate authentication tag
113  *
114  * @v ctx Context
115  * @v auth Authentication tag
116  */
117  void ( * auth ) ( void *ctx, void *auth );
118 };
119 
120 /** A public key algorithm */
122  /** Algorithm name */
123  const char *name;
124  /** Calculate maximum output length
125  *
126  * @v key Key
127  * @ret max_len Maximum output length
128  */
129  size_t ( * max_len ) ( const struct asn1_cursor *key );
130  /** Encrypt
131  *
132  * @v key Key
133  * @v plaintext Plaintext
134  * @v plaintext_len Length of plaintext
135  * @v ciphertext Ciphertext
136  * @ret ciphertext_len Length of ciphertext, or negative error
137  */
138  int ( * encrypt ) ( const struct asn1_cursor *key, const void *data,
139  size_t len, void *out );
140  /** Decrypt
141  *
142  * @v key Key
143  * @v ciphertext Ciphertext
144  * @v ciphertext_len Ciphertext length
145  * @v plaintext Plaintext
146  * @ret plaintext_len Plaintext length, or negative error
147  */
148  int ( * decrypt ) ( const struct asn1_cursor *key, const void *data,
149  size_t len, void *out );
150  /** Sign digest value
151  *
152  * @v key Key
153  * @v digest Digest algorithm
154  * @v value Digest value
155  * @v signature Signature
156  * @ret signature_len Signature length, or negative error
157  */
158  int ( * sign ) ( const struct asn1_cursor *key,
159  struct digest_algorithm *digest, const void *value,
160  void *signature );
161  /** Verify signed digest value
162  *
163  * @v key Key
164  * @v digest Digest algorithm
165  * @v value Digest value
166  * @v signature Signature
167  * @v signature_len Signature length
168  * @ret rc Return status code
169  */
170  int ( * verify ) ( const struct asn1_cursor *key,
171  struct digest_algorithm *digest, const void *value,
172  const void *signature, size_t signature_len );
173  /** Check that public key matches private key
174  *
175  * @v private_key Private key
176  * @v public_key Public key
177  * @ret rc Return status code
178  */
179  int ( * match ) ( const struct asn1_cursor *private_key,
180  const struct asn1_cursor *public_key );
181 };
182 
183 /** An elliptic curve */
185  /** Curve name */
186  const char *name;
187  /** Key size */
188  size_t keysize;
189  /** Multiply scalar by curve point
190  *
191  * @v base Base point (or NULL to use generator)
192  * @v scalar Scalar multiple
193  * @v result Result point to fill in
194  * @ret rc Return status code
195  */
196  int ( * multiply ) ( const void *base, const void *scalar,
197  void *result );
198 };
199 
200 static inline __attribute__ (( always_inline )) void
201 digest_init ( struct digest_algorithm *digest, void *ctx ) {
202  digest->init ( ctx );
203 }
204 
205 static inline __attribute__ (( always_inline )) void
206 digest_update ( struct digest_algorithm *digest, void *ctx,
207  const void *data, size_t len ) {
208  digest->update ( ctx, data, len );
209 }
210 
211 static inline __attribute__ (( always_inline )) void
212 digest_final ( struct digest_algorithm *digest, void *ctx, void *out ) {
213  digest->final ( ctx, out );
214 }
215 
216 static inline __attribute__ (( always_inline )) int
217 cipher_setkey ( struct cipher_algorithm *cipher, void *ctx,
218  const void *key, size_t keylen ) {
219  return cipher->setkey ( ctx, key, keylen );
220 }
221 
222 static inline __attribute__ (( always_inline )) void
223 cipher_setiv ( struct cipher_algorithm *cipher, void *ctx,
224  const void *iv, size_t ivlen ) {
225  cipher->setiv ( ctx, iv, ivlen );
226 }
227 
228 static inline __attribute__ (( always_inline )) void
229 cipher_encrypt ( struct cipher_algorithm *cipher, void *ctx,
230  const void *src, void *dst, size_t len ) {
231  cipher->encrypt ( ctx, src, dst, len );
232 }
233 #define cipher_encrypt( cipher, ctx, src, dst, len ) do { \
234  assert ( ( (len) & ( (cipher)->blocksize - 1 ) ) == 0 ); \
235  cipher_encrypt ( (cipher), (ctx), (src), (dst), (len) ); \
236  } while ( 0 )
237 
238 static inline __attribute__ (( always_inline )) void
239 cipher_decrypt ( struct cipher_algorithm *cipher, void *ctx,
240  const void *src, void *dst, size_t len ) {
241  cipher->decrypt ( ctx, src, dst, len );
242 }
243 #define cipher_decrypt( cipher, ctx, src, dst, len ) do { \
244  assert ( ( (len) & ( (cipher)->blocksize - 1 ) ) == 0 ); \
245  cipher_decrypt ( (cipher), (ctx), (src), (dst), (len) ); \
246  } while ( 0 )
247 
248 static inline __attribute__ (( always_inline )) void
249 cipher_auth ( struct cipher_algorithm *cipher, void *ctx, void *auth ) {
250  cipher->auth ( ctx, auth );
251 }
252 
253 static inline __attribute__ (( always_inline )) int
254 is_stream_cipher ( struct cipher_algorithm *cipher ) {
255  return ( cipher->blocksize == 1 );
256 }
257 
258 static inline __attribute__ (( always_inline )) int
259 is_block_cipher ( struct cipher_algorithm *cipher ) {
260  return ( cipher->blocksize > 1 );
261 }
262 
263 static inline __attribute__ (( always_inline )) int
264 is_auth_cipher ( struct cipher_algorithm *cipher ) {
265  return cipher->authsize;
266 }
267 
268 static inline __attribute__ (( always_inline )) size_t
270  const struct asn1_cursor *key ) {
271  return pubkey->max_len ( key );
272 }
273 
274 static inline __attribute__ (( always_inline )) int
275 pubkey_encrypt ( struct pubkey_algorithm *pubkey, const struct asn1_cursor *key,
276  const void *data, size_t len, void *out ) {
277  return pubkey->encrypt ( key, data, len, out );
278 }
279 
280 static inline __attribute__ (( always_inline )) int
281 pubkey_decrypt ( struct pubkey_algorithm *pubkey, const struct asn1_cursor *key,
282  const void *data, size_t len, void *out ) {
283  return pubkey->decrypt ( key, data, len, out );
284 }
285 
286 static inline __attribute__ (( always_inline )) int
287 pubkey_sign ( struct pubkey_algorithm *pubkey, const struct asn1_cursor *key,
288  struct digest_algorithm *digest, const void *value,
289  void *signature ) {
290  return pubkey->sign ( key, digest, value, signature );
291 }
292 
293 static inline __attribute__ (( always_inline )) int
294 pubkey_verify ( struct pubkey_algorithm *pubkey, const struct asn1_cursor *key,
295  struct digest_algorithm *digest, const void *value,
296  const void *signature, size_t signature_len ) {
297  return pubkey->verify ( key, digest, value, signature, signature_len );
298 }
299 
300 static inline __attribute__ (( always_inline )) int
302  const struct asn1_cursor *private_key,
303  const struct asn1_cursor *public_key ) {
304  return pubkey->match ( private_key, public_key );
305 }
306 
307 static inline __attribute__ (( always_inline )) int
309  const void *base, const void *scalar, void *result ) {
310  return curve->multiply ( base, scalar, result );
311 }
312 
313 extern void digest_null_init ( void *ctx );
314 extern void digest_null_update ( void *ctx, const void *src, size_t len );
315 extern void digest_null_final ( void *ctx, void *out );
316 
317 extern int cipher_null_setkey ( void *ctx, const void *key, size_t keylen );
318 extern void cipher_null_setiv ( void *ctx, const void *iv, size_t ivlen );
319 extern void cipher_null_encrypt ( void *ctx, const void *src, void *dst,
320  size_t len );
321 extern void cipher_null_decrypt ( void *ctx, const void *src, void *dst,
322  size_t len );
323 extern void cipher_null_auth ( void *ctx, void *auth );
324 
325 extern size_t pubkey_null_max_len ( const struct asn1_cursor *key );
326 extern int pubkey_null_encrypt ( const struct asn1_cursor *key,
327  const void *plaintext, size_t plaintext_len,
328  void *ciphertext );
329 extern int pubkey_null_decrypt ( const struct asn1_cursor *key,
330  const void *ciphertext, size_t ciphertext_len,
331  void *plaintext );
332 extern int pubkey_null_sign ( const struct asn1_cursor *key,
333  struct digest_algorithm *digest,
334  const void *value, void *signature );
335 extern int pubkey_null_verify ( const struct asn1_cursor *key,
336  struct digest_algorithm *digest,
337  const void *value, const void *signature ,
338  size_t signature_len );
339 
340 extern struct digest_algorithm digest_null;
341 extern struct cipher_algorithm cipher_null;
342 extern struct pubkey_algorithm pubkey_null;
343 
344 #endif /* _IPXE_CRYPTO_H */
int(* multiply)(const void *base, const void *scalar, void *result)
Multiply scalar by curve point.
Definition: crypto.h:196
#define __attribute__(x)
Definition: compiler.h:10
uint32_t base
Base.
Definition: librm.h:252
static int is_auth_cipher(struct cipher_algorithm *cipher)
Definition: crypto.h:264
size_t blocksize
Block size.
Definition: crypto.h:60
static const void * src
Definition: string.h:47
static void digest_update(struct digest_algorithm *digest, void *ctx, const void *data, size_t len)
Definition: crypto.h:206
static int pubkey_verify(struct pubkey_algorithm *pubkey, const struct asn1_cursor *key, struct digest_algorithm *digest, const void *value, const void *signature, size_t signature_len)
Definition: crypto.h:294
void(* decrypt)(void *ctx, const void *src, void *dst, size_t len)
Decrypt data.
Definition: crypto.h:110
static int elliptic_multiply(struct elliptic_curve *curve, const void *base, const void *scalar, void *result)
Definition: crypto.h:308
struct cipher_algorithm cipher_null
Definition: crypto_null.c:83
int(* decrypt)(const struct asn1_cursor *key, const void *data, size_t len, void *out)
Decrypt.
Definition: crypto.h:148
static int pubkey_encrypt(struct pubkey_algorithm *pubkey, const struct asn1_cursor *key, const void *data, size_t len, void *out)
Definition: crypto.h:275
__SIZE_TYPE__ size_t
Definition: stdint.h:6
int(* encrypt)(const struct asn1_cursor *key, const void *data, size_t len, void *out)
Encrypt.
Definition: crypto.h:138
static void digest_final(struct digest_algorithm *digest, void *ctx, void *out)
Definition: crypto.h:212
void(* init)(void *ctx)
Initialise digest.
Definition: crypto.h:31
size_t alignsize
Alignment size.
Definition: crypto.h:72
void cipher_null_setiv(void *ctx, const void *iv, size_t ivlen)
int pubkey_null_sign(const struct asn1_cursor *key, struct digest_algorithm *digest, const void *value, void *signature)
static int pubkey_decrypt(struct pubkey_algorithm *pubkey, const struct asn1_cursor *key, const void *data, size_t len, void *out)
Definition: crypto.h:281
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
void(* final)(void *ctx, void *out)
Finalise digest.
Definition: crypto.h:46
u8 iv[16]
Initialization vector.
Definition: wpa.h:60
static int pubkey_match(struct pubkey_algorithm *pubkey, const struct asn1_cursor *private_key, const struct asn1_cursor *public_key)
Definition: crypto.h:301
int(* sign)(const struct asn1_cursor *key, struct digest_algorithm *digest, const void *value, void *signature)
Sign digest value.
Definition: crypto.h:158
struct digest_algorithm digest_null
Definition: crypto_null.c:48
int pubkey_null_verify(const struct asn1_cursor *key, struct digest_algorithm *digest, const void *value, const void *signature, size_t signature_len)
void cipher_null_auth(void *ctx, void *auth)
void(* encrypt)(void *ctx, const void *src, void *dst, size_t len)
Encrypt data.
Definition: crypto.h:99
#define cipher_encrypt(cipher, ctx, src, dst, len)
Definition: crypto.h:233
size_t authsize
Authentication tag size.
Definition: crypto.h:74
size_t blocksize
Block size.
Definition: crypto.h:24
void(* setiv)(void *ctx, const void *iv, size_t ivlen)
Set initialisation vector.
Definition: crypto.h:89
Assertions.
void digest_null_update(void *ctx, const void *src, size_t len)
ASN.1 encoding.
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
__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:40
struct pubkey_algorithm pubkey_null
Definition: crypto_null.c:128
const char * name
Curve name.
Definition: crypto.h:186
int(* match)(const struct asn1_cursor *private_key, const struct asn1_cursor *public_key)
Check that public key matches private key.
Definition: crypto.h:179
static void digest_init(struct digest_algorithm *digest, void *ctx)
Definition: crypto.h:201
size_t keysize
Key size.
Definition: crypto.h:188
void cipher_null_encrypt(void *ctx, const void *src, void *dst, size_t len)
static void cipher_setiv(struct cipher_algorithm *cipher, void *ctx, const void *iv, size_t ivlen)
Definition: crypto.h:223
void(* auth)(void *ctx, void *auth)
Generate authentication tag.
Definition: crypto.h:117
static int is_stream_cipher(struct cipher_algorithm *cipher)
Definition: crypto.h:254
size_t ctxsize
Context size.
Definition: crypto.h:54
#define cipher_decrypt(cipher, ctx, src, dst, len)
Definition: crypto.h:243
uint16_t result
Definition: hyperv.h:33
void digest_null_final(void *ctx, void *out)
int(* verify)(const struct asn1_cursor *key, struct digest_algorithm *digest, const void *value, const void *signature, size_t signature_len)
Verify signed digest value.
Definition: crypto.h:170
int pubkey_null_encrypt(const struct asn1_cursor *key, const void *plaintext, size_t plaintext_len, void *ciphertext)
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
An elliptic curve.
Definition: crypto.h:184
size_t ctxsize
Context size.
Definition: crypto.h:22
int(* setkey)(void *ctx, const void *key, size_t keylen)
Set key.
Definition: crypto.h:82
size_t digestsize
Digest size.
Definition: crypto.h:26
const char * name
Algorithm name.
Definition: crypto.h:20
int cipher_null_setkey(void *ctx, const void *key, size_t keylen)
void cipher_null_decrypt(void *ctx, const void *src, void *dst, size_t len)
A message digest algorithm.
Definition: crypto.h:18
uint8_t data[48]
Additional event data.
Definition: ena.h:22
A cipher algorithm.
Definition: crypto.h:50
A private key.
Definition: privkey.h:16
void digest_null_init(void *ctx)
static void cipher_auth(struct cipher_algorithm *cipher, void *ctx, void *auth)
Definition: crypto.h:249
static size_t pubkey_max_len(struct pubkey_algorithm *pubkey, const struct asn1_cursor *key)
Definition: crypto.h:269
const char * name
Algorithm name.
Definition: crypto.h:52
static int pubkey_sign(struct pubkey_algorithm *pubkey, const struct asn1_cursor *key, struct digest_algorithm *digest, const void *value, void *signature)
Definition: crypto.h:287
u8 signature
CPU signature.
Definition: CIB_PRM.h:35
uint32_t len
Length.
Definition: ena.h:14
struct private_key private_key
Private key.
Definition: privkey.c:67
An ASN.1 object cursor.
Definition: asn1.h:20
A public key algorithm.
Definition: crypto.h:121
size_t(* max_len)(const struct asn1_cursor *key)
Calculate maximum output length.
Definition: crypto.h:129
union @383 key
Sense key.
Definition: scsi.h:18
size_t pubkey_null_max_len(const struct asn1_cursor *key)
const char * name
Algorithm name.
Definition: crypto.h:123
static int cipher_setkey(struct cipher_algorithm *cipher, void *ctx, const void *key, size_t keylen)
Definition: crypto.h:217
static int is_block_cipher(struct cipher_algorithm *cipher)
Definition: crypto.h:259
int pubkey_null_decrypt(const struct asn1_cursor *key, const void *ciphertext, size_t ciphertext_len, void *plaintext)