13 /** A message digest algorithm */
14 struct digest_algorithm
{
27 void ( * init
) ( void *ctx
);
28 /** Update digest with new data
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
);
40 * @v out Buffer for digest output
42 void ( * final
) ( void *ctx
, void *out
);
45 /** A cipher algorithm */
46 struct cipher_algorithm
{
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
64 * @v iv Initialisation vector
66 void ( * setiv
) ( void *ctx
, const void *iv
);
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
,
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
,
91 /** A public key algorithm */
92 struct pubkey_algorithm
{
99 static inline void digest_init ( struct digest_algorithm
*digest
,
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
,
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) ); \
134 static inline void cipher_decrypt ( struct cipher_algorithm
*cipher
,
135 void *ctx
, const void *src
, void *dst
,
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) ); \
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 */