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  /** Point (and public key) size */
188  size_t pointsize;
189  /** Scalar (and private key) size */
190  size_t keysize;
191  /** Multiply scalar by curve point
192  *
193  * @v base Base point (or NULL to use generator)
194  * @v scalar Scalar multiple
195  * @v result Result point to fill in
196  * @ret rc Return status code
197  */
198  int ( * multiply ) ( const void *base, const void *scalar,
199  void *result );
200 };
201 
202 static inline __attribute__ (( always_inline )) void
203 digest_init ( struct digest_algorithm *digest, void *ctx ) {
204  digest->init ( ctx );
205 }
206 
207 static inline __attribute__ (( always_inline )) void
208 digest_update ( struct digest_algorithm *digest, void *ctx,
209  const void *data, size_t len ) {
210  digest->update ( ctx, data, len );
211 }
212 
213 static inline __attribute__ (( always_inline )) void
214 digest_final ( struct digest_algorithm *digest, void *ctx, void *out ) {
215  digest->final ( ctx, out );
216 }
217 
218 static inline __attribute__ (( always_inline )) int
219 cipher_setkey ( struct cipher_algorithm *cipher, void *ctx,
220  const void *key, size_t keylen ) {
221  return cipher->setkey ( ctx, key, keylen );
222 }
223 
224 static inline __attribute__ (( always_inline )) void
225 cipher_setiv ( struct cipher_algorithm *cipher, void *ctx,
226  const void *iv, size_t ivlen ) {
227  cipher->setiv ( ctx, iv, ivlen );
228 }
229 
230 static inline __attribute__ (( always_inline )) void
231 cipher_encrypt ( struct cipher_algorithm *cipher, void *ctx,
232  const void *src, void *dst, size_t len ) {
233  cipher->encrypt ( ctx, src, dst, len );
234 }
235 #define cipher_encrypt( cipher, ctx, src, dst, len ) do { \
236  assert ( ( (len) & ( (cipher)->blocksize - 1 ) ) == 0 ); \
237  cipher_encrypt ( (cipher), (ctx), (src), (dst), (len) ); \
238  } while ( 0 )
239 
240 static inline __attribute__ (( always_inline )) void
241 cipher_decrypt ( struct cipher_algorithm *cipher, void *ctx,
242  const void *src, void *dst, size_t len ) {
243  cipher->decrypt ( ctx, src, dst, len );
244 }
245 #define cipher_decrypt( cipher, ctx, src, dst, len ) do { \
246  assert ( ( (len) & ( (cipher)->blocksize - 1 ) ) == 0 ); \
247  cipher_decrypt ( (cipher), (ctx), (src), (dst), (len) ); \
248  } while ( 0 )
249 
250 static inline __attribute__ (( always_inline )) void
251 cipher_auth ( struct cipher_algorithm *cipher, void *ctx, void *auth ) {
252  cipher->auth ( ctx, auth );
253 }
254 
255 static inline __attribute__ (( always_inline )) int
256 is_stream_cipher ( struct cipher_algorithm *cipher ) {
257  return ( cipher->blocksize == 1 );
258 }
259 
260 static inline __attribute__ (( always_inline )) int
261 is_block_cipher ( struct cipher_algorithm *cipher ) {
262  return ( cipher->blocksize > 1 );
263 }
264 
265 static inline __attribute__ (( always_inline )) int
266 is_auth_cipher ( struct cipher_algorithm *cipher ) {
267  return cipher->authsize;
268 }
269 
270 static inline __attribute__ (( always_inline )) size_t
272  const struct asn1_cursor *key ) {
273  return pubkey->max_len ( key );
274 }
275 
276 static inline __attribute__ (( always_inline )) int
277 pubkey_encrypt ( struct pubkey_algorithm *pubkey, const struct asn1_cursor *key,
278  const void *data, size_t len, void *out ) {
279  return pubkey->encrypt ( key, data, len, out );
280 }
281 
282 static inline __attribute__ (( always_inline )) int
283 pubkey_decrypt ( struct pubkey_algorithm *pubkey, const struct asn1_cursor *key,
284  const void *data, size_t len, void *out ) {
285  return pubkey->decrypt ( key, data, len, out );
286 }
287 
288 static inline __attribute__ (( always_inline )) int
289 pubkey_sign ( struct pubkey_algorithm *pubkey, const struct asn1_cursor *key,
290  struct digest_algorithm *digest, const void *value,
291  void *signature ) {
292  return pubkey->sign ( key, digest, value, signature );
293 }
294 
295 static inline __attribute__ (( always_inline )) int
296 pubkey_verify ( struct pubkey_algorithm *pubkey, const struct asn1_cursor *key,
297  struct digest_algorithm *digest, const void *value,
298  const void *signature, size_t signature_len ) {
299  return pubkey->verify ( key, digest, value, signature, signature_len );
300 }
301 
302 static inline __attribute__ (( always_inline )) int
304  const struct asn1_cursor *private_key,
305  const struct asn1_cursor *public_key ) {
306  return pubkey->match ( private_key, public_key );
307 }
308 
309 static inline __attribute__ (( always_inline )) int
311  const void *base, const void *scalar, void *result ) {
312  return curve->multiply ( base, scalar, result );
313 }
314 
315 extern void digest_null_init ( void *ctx );
316 extern void digest_null_update ( void *ctx, const void *src, size_t len );
317 extern void digest_null_final ( void *ctx, void *out );
318 
319 extern int cipher_null_setkey ( void *ctx, const void *key, size_t keylen );
320 extern void cipher_null_setiv ( void *ctx, const void *iv, size_t ivlen );
321 extern void cipher_null_encrypt ( void *ctx, const void *src, void *dst,
322  size_t len );
323 extern void cipher_null_decrypt ( void *ctx, const void *src, void *dst,
324  size_t len );
325 extern void cipher_null_auth ( void *ctx, void *auth );
326 
327 extern size_t pubkey_null_max_len ( const struct asn1_cursor *key );
328 extern int pubkey_null_encrypt ( const struct asn1_cursor *key,
329  const void *plaintext, size_t plaintext_len,
330  void *ciphertext );
331 extern int pubkey_null_decrypt ( const struct asn1_cursor *key,
332  const void *ciphertext, size_t ciphertext_len,
333  void *plaintext );
334 extern int pubkey_null_sign ( const struct asn1_cursor *key,
335  struct digest_algorithm *digest,
336  const void *value, void *signature );
337 extern int pubkey_null_verify ( const struct asn1_cursor *key,
338  struct digest_algorithm *digest,
339  const void *value, const void *signature ,
340  size_t signature_len );
341 
342 extern struct digest_algorithm digest_null;
343 extern struct cipher_algorithm cipher_null;
344 extern struct pubkey_algorithm pubkey_null;
345 
346 #endif /* _IPXE_CRYPTO_H */
int(* multiply)(const void *base, const void *scalar, void *result)
Multiply scalar by curve point.
Definition: crypto.h:198
#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:266
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:208
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:296
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:310
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:277
__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:214
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:283
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
void(* final)(void *ctx, void *out)
Finalise digest.
Definition: crypto.h:46
__be32 out[4]
Definition: CIB_PRM.h:36
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:303
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:235
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
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:203
size_t keysize
Scalar (and private key) size.
Definition: crypto.h:190
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:225
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:256
size_t ctxsize
Context size.
Definition: crypto.h:54
#define cipher_decrypt(cipher, ctx, src, dst, len)
Definition: crypto.h:245
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:251
static size_t pubkey_max_len(struct pubkey_algorithm *pubkey, const struct asn1_cursor *key)
Definition: crypto.h:271
const char * name
Algorithm name.
Definition: crypto.h:52
size_t pointsize
Point (and public key) size.
Definition: crypto.h:188
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:289
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:219
static int is_block_cipher(struct cipher_algorithm *cipher)
Definition: crypto.h:261
int pubkey_null_decrypt(const struct asn1_cursor *key, const void *ciphertext, size_t ciphertext_len, void *plaintext)