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