13 /** A cryptographic algorithm */
14 struct crypto_algorithm {
21 /** Final output size */
23 /** Initialise algorithm
27 void ( * init ) ( void *ctx );
32 * @v keylen Key length
33 * @ret rc Return status code
35 int ( * setkey ) ( void *ctx, const void *key, size_t keylen );
36 /** Set initialisation vector
39 * @v iv Initialisation vector
41 void ( *setiv ) ( void *ctx, const void *iv );
45 * @v src Data to encode
46 * @v dst Encoded data, or NULL
47 * @v len Length of data
48 * @ret rc Return status code
50 * For a cipher algorithm, the enciphered data should be
51 * placed in @c dst. For a digest algorithm, only the digest
52 * state should be updated, and @c dst will be NULL.
54 * @v len is guaranteed to be a multiple of @c blocksize.
56 void ( * encode ) ( void *ctx, const void *src, void *dst,
61 * @v src Data to decode
63 * @v len Length of data
64 * @ret rc Return status code
66 * @v len is guaranteed to be a multiple of @c blocksize.
68 void ( * decode ) ( void *ctx, const void *src, void *dst,
70 /** Finalise algorithm
73 * @v out Algorithm final output
75 void ( * final ) ( void *ctx, void *out );
78 static inline void digest_init ( struct crypto_algorithm *crypto,
83 static inline void digest_update ( struct crypto_algorithm *crypto,
84 void *ctx, const void *data, size_t len ) {
85 crypto->encode ( ctx, data, NULL, len );
88 static inline void digest_final ( struct crypto_algorithm *crypto,
89 void *ctx, void *out ) {
90 crypto->final ( ctx, out );
93 static inline void cipher_setiv ( struct crypto_algorithm *crypto,
94 void *ctx, const void *iv ) {
95 crypto->setiv ( ctx, iv );
98 static inline int cipher_setkey ( struct crypto_algorithm *crypto,
99 void *ctx, const void *key, size_t keylen ) {
100 return crypto->setkey ( ctx, key, keylen );
103 static inline int is_stream_cipher ( struct crypto_algorithm *crypto ) {
104 return ( crypto->blocksize == 1 );
107 extern struct crypto_algorithm crypto_null;
109 extern int cipher_encrypt ( struct crypto_algorithm *crypto,
110 void *ctx, const void *src, void *dst,
112 extern int cipher_decrypt ( struct crypto_algorithm *crypto,
113 void *ctx, const void *src, void *dst,
116 #endif /* _GPXE_CRYPTO_H */