[sundance] Add reset completion check
[gpxe.git] / src / include / gpxe / crypto.h
blob10882d37e117e7faa7c9eab6f58b0f0af8d2d31e
1 #ifndef _GPXE_CRYPTO_H
2 #define _GPXE_CRYPTO_H
4 /** @file
6 * Cryptographic API
8 */
10 #include <stdint.h>
11 #include <stddef.h>
13 /** A message digest algorithm */
14 struct digest_algorithm {
15 /** Algorithm name */
16 const char *name;
17 /** Context size */
18 size_t ctxsize;
19 /** Block size */
20 size_t blocksize;
21 /** Digest size */
22 size_t digestsize;
23 /** Initialise digest
25 * @v ctx Context
27 void ( * init ) ( void *ctx );
28 /** Update digest with new data
30 * @v ctx Context
31 * @v src Data to digest
32 * @v len Length of data
34 * @v len is not necessarily a multiple of @c blocksize.
36 void ( * update ) ( void *ctx, const void *src, size_t len );
37 /** Finalise digest
39 * @v ctx Context
40 * @v out Buffer for digest output
42 void ( * final ) ( void *ctx, void *out );
45 /** A cipher algorithm */
46 struct cipher_algorithm {
47 /** Algorithm name */
48 const char *name;
49 /** Context size */
50 size_t ctxsize;
51 /** Block size */
52 size_t blocksize;
53 /** Set key
55 * @v ctx Context
56 * @v key Key
57 * @v keylen Key length
58 * @ret rc Return status code
60 int ( * setkey ) ( void *ctx, const void *key, size_t keylen );
61 /** Set initialisation vector
63 * @v ctx Context
64 * @v iv Initialisation vector
66 void ( * setiv ) ( void *ctx, const void *iv );
67 /** Encrypt data
69 * @v ctx Context
70 * @v src Data to encrypt
71 * @v dst Buffer for encrypted data
72 * @v len Length of data
74 * @v len is guaranteed to be a multiple of @c blocksize.
76 void ( * encrypt ) ( void *ctx, const void *src, void *dst,
77 size_t len );
78 /** Decrypt data
80 * @v ctx Context
81 * @v src Data to decrypt
82 * @v dst Buffer for decrypted data
83 * @v len Length of data
85 * @v len is guaranteed to be a multiple of @c blocksize.
87 void ( * decrypt ) ( void *ctx, const void *src, void *dst,
88 size_t len );
91 /** A public key algorithm */
92 struct pubkey_algorithm {
93 /** Algorithm name */
94 const char *name;
95 /** Context size */
96 size_t ctxsize;
99 static inline void digest_init ( struct digest_algorithm *digest,
100 void *ctx ) {
101 digest->init ( ctx );
104 static inline void digest_update ( struct digest_algorithm *digest,
105 void *ctx, const void *data, size_t len ) {
106 digest->update ( ctx, data, len );
109 static inline void digest_final ( struct digest_algorithm *digest,
110 void *ctx, void *out ) {
111 digest->final ( ctx, out );
114 static inline int cipher_setkey ( struct cipher_algorithm *cipher,
115 void *ctx, const void *key, size_t keylen ) {
116 return cipher->setkey ( ctx, key, keylen );
119 static inline void cipher_setiv ( struct cipher_algorithm *cipher,
120 void *ctx, const void *iv ) {
121 cipher->setiv ( ctx, iv );
124 static inline void cipher_encrypt ( struct cipher_algorithm *cipher,
125 void *ctx, const void *src, void *dst,
126 size_t len ) {
127 cipher->encrypt ( ctx, src, dst, len );
129 #define cipher_encrypt( cipher, ctx, src, dst, len ) do { \
130 assert ( ( len & ( (cipher)->blocksize - 1 ) ) == 0 ); \
131 cipher_encrypt ( (cipher), (ctx), (src), (dst), (len) ); \
132 } while ( 0 )
134 static inline void cipher_decrypt ( struct cipher_algorithm *cipher,
135 void *ctx, const void *src, void *dst,
136 size_t len ) {
137 cipher->decrypt ( ctx, src, dst, len );
139 #define cipher_decrypt( cipher, ctx, src, dst, len ) do { \
140 assert ( ( len & ( (cipher)->blocksize - 1 ) ) == 0 ); \
141 cipher_decrypt ( (cipher), (ctx), (src), (dst), (len) ); \
142 } while ( 0 )
144 static inline int is_stream_cipher ( struct cipher_algorithm *cipher ) {
145 return ( cipher->blocksize == 1 );
148 extern struct digest_algorithm digest_null;
149 extern struct cipher_algorithm cipher_null;
150 extern struct pubkey_algorithm pubkey_null;
152 #endif /* _GPXE_CRYPTO_H */