10 FILE_LICENCE ( GPL2_OR_LATER );
15 /** A message digest algorithm */
16 struct digest_algorithm {
29 void ( * init ) ( void *ctx );
30 /** Update digest with new data
33 * @v src Data to digest
34 * @v len Length of data
36 * @v len is not necessarily a multiple of @c blocksize.
38 void ( * update ) ( void *ctx, const void *src, size_t len );
42 * @v out Buffer for digest output
44 void ( * final ) ( void *ctx, void *out );
47 /** A cipher algorithm */
48 struct cipher_algorithm {
59 * @v keylen Key length
60 * @ret rc Return status code
62 int ( * setkey ) ( void *ctx, const void *key, size_t keylen );
63 /** Set initialisation vector
66 * @v iv Initialisation vector
68 void ( * setiv ) ( void *ctx, const void *iv );
72 * @v src Data to encrypt
73 * @v dst Buffer for encrypted data
74 * @v len Length of data
76 * @v len is guaranteed to be a multiple of @c blocksize.
78 void ( * encrypt ) ( void *ctx, const void *src, void *dst,
83 * @v src Data to decrypt
84 * @v dst Buffer for decrypted data
85 * @v len Length of data
87 * @v len is guaranteed to be a multiple of @c blocksize.
89 void ( * decrypt ) ( void *ctx, const void *src, void *dst,
93 /** A public key algorithm */
94 struct pubkey_algorithm {
99 /** Initialise algorithm
103 * @v key_len Length of key
104 * @ret rc Return status code
106 int ( * init ) ( void *ctx, const void *key, size_t key_len );
107 /** Calculate maximum output length
110 * @ret max_len Maximum output length
112 size_t ( * max_len ) ( void *ctx );
116 * @v plaintext Plaintext
117 * @v plaintext_len Length of plaintext
118 * @v ciphertext Ciphertext
119 * @ret ciphertext_len Length of ciphertext, or negative error
121 int ( * encrypt ) ( void *ctx, const void *data, size_t len,
126 * @v ciphertext Ciphertext
127 * @v ciphertext_len Ciphertext length
128 * @v plaintext Plaintext
129 * @ret plaintext_len Plaintext length, or negative error
131 int ( * decrypt ) ( void *ctx, const void *data, size_t len,
133 /** Sign digest value
136 * @v digest Digest algorithm
137 * @v value Digest value
138 * @v signature Signature
139 * @ret signature_len Signature length, or negative error
141 int ( * sign ) ( void *ctx, struct digest_algorithm *digest,
142 const void *value, void *signature );
143 /** Verify signed digest value
146 * @v digest Digest algorithm
147 * @v value Digest value
148 * @v signature Signature
149 * @v signature_len Signature length
150 * @ret rc Return status code
152 int ( * verify ) ( void *ctx, struct digest_algorithm *digest,
153 const void *value, const void *signature,
154 size_t signature_len );
155 /** Finalise algorithm
159 void ( * final ) ( void *ctx );
160 /** Check that public key matches private key
162 * @v private_key Private key
163 * @v private_key_len Private key length
164 * @v public_key Public key
165 * @v public_key_len Public key length
166 * @ret rc Return status code
168 int ( * match ) ( const void *private_key, size_t private_key_len,
169 const void *public_key, size_t public_key_len );
172 static inline void digest_init ( struct digest_algorithm *digest,
174 digest->init ( ctx );
177 static inline void digest_update ( struct digest_algorithm *digest,
178 void *ctx, const void *data, size_t len ) {
179 digest->update ( ctx, data, len );
182 static inline void digest_final ( struct digest_algorithm *digest,
183 void *ctx, void *out ) {
184 digest->final ( ctx, out );
187 static inline int cipher_setkey ( struct cipher_algorithm *cipher,
188 void *ctx, const void *key, size_t keylen ) {
189 return cipher->setkey ( ctx, key, keylen );
192 static inline void cipher_setiv ( struct cipher_algorithm *cipher,
193 void *ctx, const void *iv ) {
194 cipher->setiv ( ctx, iv );
197 static inline void cipher_encrypt ( struct cipher_algorithm *cipher,
198 void *ctx, const void *src, void *dst,
200 cipher->encrypt ( ctx, src, dst, len );
202 #define cipher_encrypt( cipher, ctx, src, dst, len ) do { \
203 assert ( ( (len) & ( (cipher)->blocksize - 1 ) ) == 0 ); \
204 cipher_encrypt ( (cipher), (ctx), (src), (dst), (len) ); \
207 static inline void cipher_decrypt ( struct cipher_algorithm *cipher,
208 void *ctx, const void *src, void *dst,
210 cipher->decrypt ( ctx, src, dst, len );
212 #define cipher_decrypt( cipher, ctx, src, dst, len ) do { \
213 assert ( ( (len) & ( (cipher)->blocksize - 1 ) ) == 0 ); \
214 cipher_decrypt ( (cipher), (ctx), (src), (dst), (len) ); \
217 static inline int is_stream_cipher ( struct cipher_algorithm *cipher ) {
218 return ( cipher->blocksize == 1 );
221 static inline int pubkey_init ( struct pubkey_algorithm *pubkey, void *ctx,
222 const void *key, size_t key_len ) {
223 return pubkey->init ( ctx, key, key_len );
226 static inline size_t pubkey_max_len ( struct pubkey_algorithm *pubkey,
228 return pubkey->max_len ( ctx );
231 static inline int pubkey_encrypt ( struct pubkey_algorithm *pubkey, void *ctx,
232 const void *data, size_t len, void *out ) {
233 return pubkey->encrypt ( ctx, data, len, out );
236 static inline int pubkey_decrypt ( struct pubkey_algorithm *pubkey, void *ctx,
237 const void *data, size_t len, void *out ) {
238 return pubkey->decrypt ( ctx, data, len, out );
241 static inline int pubkey_sign ( struct pubkey_algorithm *pubkey, void *ctx,
242 struct digest_algorithm *digest,
243 const void *value, void *signature ) {
244 return pubkey->sign ( ctx, digest, value, signature );
247 static inline int pubkey_verify ( struct pubkey_algorithm *pubkey, void *ctx,
248 struct digest_algorithm *digest,
249 const void *value, const void *signature,
250 size_t signature_len ) {
251 return pubkey->verify ( ctx, digest, value, signature, signature_len );
254 static inline void pubkey_final ( struct pubkey_algorithm *pubkey, void *ctx ) {
255 pubkey->final ( ctx );
258 static inline int pubkey_match ( struct pubkey_algorithm *pubkey,
259 const void *private_key,
260 size_t private_key_len, const void *public_key,
261 size_t public_key_len ) {
262 return pubkey->match ( private_key, private_key_len, public_key,
266 extern struct digest_algorithm digest_null;
267 extern struct cipher_algorithm cipher_null;
268 extern struct pubkey_algorithm pubkey_null;
270 #endif /* _IPXE_CRYPTO_H */