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  /** Encrypt
125  *
126  * @v key Key
127  * @v plaintext Plaintext
128  * @v ciphertext Ciphertext
129  * @ret rc Return status code
130  */
131  int ( * encrypt ) ( const struct asn1_cursor *key,
132  const struct asn1_cursor *plaintext,
133  struct asn1_builder *ciphertext );
134  /** Decrypt
135  *
136  * @v key Key
137  * @v ciphertext Ciphertext
138  * @v plaintext Plaintext
139  * @ret rc Return status code
140  */
141  int ( * decrypt ) ( const struct asn1_cursor *key,
142  const struct asn1_cursor *ciphertext,
143  struct asn1_builder *plaintext );
144  /** Sign digest value
145  *
146  * @v key Key
147  * @v digest Digest algorithm
148  * @v value Digest value
149  * @v signature Signature
150  * @ret rc Return status code
151  */
152  int ( * sign ) ( const struct asn1_cursor *key,
153  struct digest_algorithm *digest, const void *value,
154  struct asn1_builder *builder );
155  /** Verify signed digest value
156  *
157  * @v key Key
158  * @v digest Digest algorithm
159  * @v value Digest value
160  * @v signature Signature
161  * @ret rc Return status code
162  */
163  int ( * verify ) ( const struct asn1_cursor *key,
164  struct digest_algorithm *digest, const void *value,
165  const struct asn1_cursor *signature );
166  /** Check that public key matches private key
167  *
168  * @v private_key Private key
169  * @v public_key Public key
170  * @ret rc Return status code
171  */
172  int ( * match ) ( const struct asn1_cursor *private_key,
173  const struct asn1_cursor *public_key );
174 };
175 
176 /** An elliptic curve */
178  /** Curve name */
179  const char *name;
180  /** Point (and public key) size */
181  size_t pointsize;
182  /** Scalar (and private key) size */
183  size_t keysize;
184  /** Multiply scalar by curve point
185  *
186  * @v base Base point (or NULL to use generator)
187  * @v scalar Scalar multiple
188  * @v result Result point to fill in
189  * @ret rc Return status code
190  */
191  int ( * multiply ) ( const void *base, const void *scalar,
192  void *result );
193 };
194 
195 static inline __attribute__ (( always_inline )) void
196 digest_init ( struct digest_algorithm *digest, void *ctx ) {
197  digest->init ( ctx );
198 }
199 
200 static inline __attribute__ (( always_inline )) void
201 digest_update ( struct digest_algorithm *digest, void *ctx,
202  const void *data, size_t len ) {
203  digest->update ( ctx, data, len );
204 }
205 
206 static inline __attribute__ (( always_inline )) void
207 digest_final ( struct digest_algorithm *digest, void *ctx, void *out ) {
208  digest->final ( ctx, out );
209 }
210 
211 static inline __attribute__ (( always_inline )) int
212 cipher_setkey ( struct cipher_algorithm *cipher, void *ctx,
213  const void *key, size_t keylen ) {
214  return cipher->setkey ( ctx, key, keylen );
215 }
216 
217 static inline __attribute__ (( always_inline )) void
218 cipher_setiv ( struct cipher_algorithm *cipher, void *ctx,
219  const void *iv, size_t ivlen ) {
220  cipher->setiv ( ctx, iv, ivlen );
221 }
222 
223 static inline __attribute__ (( always_inline )) void
224 cipher_encrypt ( struct cipher_algorithm *cipher, void *ctx,
225  const void *src, void *dst, size_t len ) {
226  cipher->encrypt ( ctx, src, dst, len );
227 }
228 #define cipher_encrypt( cipher, ctx, src, dst, len ) do { \
229  assert ( ( (len) & ( (cipher)->blocksize - 1 ) ) == 0 ); \
230  cipher_encrypt ( (cipher), (ctx), (src), (dst), (len) ); \
231  } while ( 0 )
232 
233 static inline __attribute__ (( always_inline )) void
234 cipher_decrypt ( struct cipher_algorithm *cipher, void *ctx,
235  const void *src, void *dst, size_t len ) {
236  cipher->decrypt ( ctx, src, dst, len );
237 }
238 #define cipher_decrypt( cipher, ctx, src, dst, len ) do { \
239  assert ( ( (len) & ( (cipher)->blocksize - 1 ) ) == 0 ); \
240  cipher_decrypt ( (cipher), (ctx), (src), (dst), (len) ); \
241  } while ( 0 )
242 
243 static inline __attribute__ (( always_inline )) void
244 cipher_auth ( struct cipher_algorithm *cipher, void *ctx, void *auth ) {
245  cipher->auth ( ctx, auth );
246 }
247 
248 static inline __attribute__ (( always_inline )) int
249 is_stream_cipher ( struct cipher_algorithm *cipher ) {
250  return ( cipher->blocksize == 1 );
251 }
252 
253 static inline __attribute__ (( always_inline )) int
254 is_block_cipher ( struct cipher_algorithm *cipher ) {
255  return ( cipher->blocksize > 1 );
256 }
257 
258 static inline __attribute__ (( always_inline )) int
259 is_auth_cipher ( struct cipher_algorithm *cipher ) {
260  return cipher->authsize;
261 }
262 
263 static inline __attribute__ (( always_inline )) int
264 pubkey_encrypt ( struct pubkey_algorithm *pubkey, const struct asn1_cursor *key,
265  const struct asn1_cursor *plaintext,
266  struct asn1_builder *ciphertext ) {
267  return pubkey->encrypt ( key, plaintext, ciphertext );
268 }
269 
270 static inline __attribute__ (( always_inline )) int
271 pubkey_decrypt ( struct pubkey_algorithm *pubkey, const struct asn1_cursor *key,
272  const struct asn1_cursor *ciphertext,
273  struct asn1_builder *plaintext ) {
274  return pubkey->decrypt ( key, ciphertext, plaintext );
275 }
276 
277 static inline __attribute__ (( always_inline )) int
278 pubkey_sign ( struct pubkey_algorithm *pubkey, const struct asn1_cursor *key,
279  struct digest_algorithm *digest, const void *value,
280  struct asn1_builder *signature ) {
281  return pubkey->sign ( key, digest, value, signature );
282 }
283 
284 static inline __attribute__ (( always_inline )) int
285 pubkey_verify ( struct pubkey_algorithm *pubkey, const struct asn1_cursor *key,
286  struct digest_algorithm *digest, const void *value,
287  const struct asn1_cursor *signature ) {
288  return pubkey->verify ( key, digest, value, signature );
289 }
290 
291 static inline __attribute__ (( always_inline )) int
293  const struct asn1_cursor *private_key,
294  const struct asn1_cursor *public_key ) {
295  return pubkey->match ( private_key, public_key );
296 }
297 
298 static inline __attribute__ (( always_inline )) int
300  const void *base, const void *scalar, void *result ) {
301  return curve->multiply ( base, scalar, result );
302 }
303 
304 extern void digest_null_init ( void *ctx );
305 extern void digest_null_update ( void *ctx, const void *src, size_t len );
306 extern void digest_null_final ( void *ctx, void *out );
307 
308 extern int cipher_null_setkey ( void *ctx, const void *key, size_t keylen );
309 extern void cipher_null_setiv ( void *ctx, const void *iv, size_t ivlen );
310 extern void cipher_null_encrypt ( void *ctx, const void *src, void *dst,
311  size_t len );
312 extern void cipher_null_decrypt ( void *ctx, const void *src, void *dst,
313  size_t len );
314 extern void cipher_null_auth ( void *ctx, void *auth );
315 
316 extern int pubkey_null_encrypt ( const struct asn1_cursor *key,
317  const struct asn1_cursor *plaintext,
318  struct asn1_builder *ciphertext );
319 extern int pubkey_null_decrypt ( const struct asn1_cursor *key,
320  const struct asn1_cursor *ciphertext,
321  struct asn1_builder *plaintext );
322 extern int pubkey_null_sign ( const struct asn1_cursor *key,
323  struct digest_algorithm *digest,
324  const void *value,
325  struct asn1_builder *signature );
326 extern int pubkey_null_verify ( const struct asn1_cursor *key,
327  struct digest_algorithm *digest,
328  const void *value,
329  const struct asn1_cursor *signature );
330 
331 extern struct digest_algorithm digest_null;
332 extern struct cipher_algorithm cipher_null;
333 extern struct pubkey_algorithm pubkey_null;
334 
335 #endif /* _IPXE_CRYPTO_H */
int(* multiply)(const void *base, const void *scalar, void *result)
Multiply scalar by curve point.
Definition: crypto.h:191
int pubkey_null_verify(const struct asn1_cursor *key, struct digest_algorithm *digest, const void *value, const struct asn1_cursor *signature)
#define __attribute__(x)
Definition: compiler.h:10
uint32_t base
Base.
Definition: librm.h:138
static int is_auth_cipher(struct cipher_algorithm *cipher)
Definition: crypto.h:259
size_t blocksize
Block size.
Definition: crypto.h:60
static void digest_update(struct digest_algorithm *digest, void *ctx, const void *data, size_t len)
Definition: crypto.h:201
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:299
struct cipher_algorithm cipher_null
Definition: crypto_null.c:83
static void digest_final(struct digest_algorithm *digest, void *ctx, void *out)
Definition: crypto.h:207
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)
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
int pubkey_null_sign(const struct asn1_cursor *key, struct digest_algorithm *digest, const void *value, struct asn1_builder *signature)
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:292
struct digest_algorithm digest_null
Definition: crypto_null.c:48
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:228
size_t authsize
Authentication tag size.
Definition: crypto.h:74
size_t blocksize
Block size.
Definition: crypto.h:24
static int pubkey_sign(struct pubkey_algorithm *pubkey, const struct asn1_cursor *key, struct digest_algorithm *digest, const void *value, struct asn1_builder *signature)
Definition: crypto.h:278
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)
static const void * src
Definition: string.h:47
ASN.1 encoding.
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
int(* verify)(const struct asn1_cursor *key, struct digest_algorithm *digest, const void *value, const struct asn1_cursor *signature)
Verify signed digest value.
Definition: crypto.h:163
ring len
Length.
Definition: dwmac.h:231
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:122
const char * name
Curve name.
Definition: crypto.h:179
int(* match)(const struct asn1_cursor *private_key, const struct asn1_cursor *public_key)
Check that public key matches private key.
Definition: crypto.h:172
static void digest_init(struct digest_algorithm *digest, void *ctx)
Definition: crypto.h:196
size_t keysize
Scalar (and private key) size.
Definition: crypto.h:183
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:218
An ASN.1 object builder.
Definition: asn1.h:28
void(* auth)(void *ctx, void *auth)
Generate authentication tag.
Definition: crypto.h:117
int(* decrypt)(const struct asn1_cursor *key, const struct asn1_cursor *ciphertext, struct asn1_builder *plaintext)
Decrypt.
Definition: crypto.h:141
static int is_stream_cipher(struct cipher_algorithm *cipher)
Definition: crypto.h:249
size_t ctxsize
Context size.
Definition: crypto.h:54
#define cipher_decrypt(cipher, ctx, src, dst, len)
Definition: crypto.h:238
int pubkey_null_decrypt(const struct asn1_cursor *key, const struct asn1_cursor *ciphertext, struct asn1_builder *plaintext)
uint16_t result
Definition: hyperv.h:33
void digest_null_final(void *ctx, void *out)
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
An elliptic curve.
Definition: crypto.h:177
static int pubkey_verify(struct pubkey_algorithm *pubkey, const struct asn1_cursor *key, struct digest_algorithm *digest, const void *value, const struct asn1_cursor *signature)
Definition: crypto.h:285
size_t ctxsize
Context size.
Definition: crypto.h:22
static int pubkey_encrypt(struct pubkey_algorithm *pubkey, const struct asn1_cursor *key, const struct asn1_cursor *plaintext, struct asn1_builder *ciphertext)
Definition: crypto.h:264
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)
int(* encrypt)(const struct asn1_cursor *key, const struct asn1_cursor *plaintext, struct asn1_builder *ciphertext)
Encrypt.
Definition: crypto.h:131
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:244
int pubkey_null_encrypt(const struct asn1_cursor *key, const struct asn1_cursor *plaintext, struct asn1_builder *ciphertext)
const char * name
Algorithm name.
Definition: crypto.h:52
size_t pointsize
Point (and public key) size.
Definition: crypto.h:181
u8 signature
CPU signature.
Definition: CIB_PRM.h:35
struct private_key private_key
Private key.
Definition: privkey.c:67
An ASN.1 object cursor.
Definition: asn1.h:20
int(* sign)(const struct asn1_cursor *key, struct digest_algorithm *digest, const void *value, struct asn1_builder *builder)
Sign digest value.
Definition: crypto.h:152
A public key algorithm.
Definition: crypto.h:121
union @391 key
Sense key.
Definition: scsi.h:17
static int pubkey_decrypt(struct pubkey_algorithm *pubkey, const struct asn1_cursor *key, const struct asn1_cursor *ciphertext, struct asn1_builder *plaintext)
Definition: crypto.h:271
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:212
static int is_block_cipher(struct cipher_algorithm *cipher)
Definition: crypto.h:254