14 /** A cryptographic algorithm */
15 struct crypto_algorithm {
22 /** Final output size */
24 /** Initialise algorithm
28 void ( * init ) ( void *ctx );
33 * @v keylen Key length
34 * @ret rc Return status code
36 int ( * setkey ) ( void *ctx, const void *key, size_t keylen );
37 /** Set initialisation vector
40 * @v iv Initialisation vector
42 void ( *setiv ) ( void *ctx, const void *iv );
46 * @v src Data to encode
47 * @v dst Encoded data, or NULL
48 * @v len Length of data
49 * @ret rc Return status code
51 * For a cipher algorithm, the enciphered data should be
52 * placed in @c dst. For a digest algorithm, only the digest
53 * state should be updated, and @c dst will be NULL.
55 * @v len is guaranteed to be a multiple of @c blocksize.
57 void ( * encode ) ( void *ctx, const void *src, void *dst,
62 * @v src Data to decode
64 * @v len Length of data
65 * @ret rc Return status code
67 * @v len is guaranteed to be a multiple of @c blocksize.
69 void ( * decode ) ( void *ctx, const void *src, void *dst,
71 /** Finalise algorithm
74 * @v out Algorithm final output
76 void ( * final ) ( void *ctx, void *out );
79 static inline void digest_init ( struct crypto_algorithm *crypto,
84 static inline void digest_update ( struct crypto_algorithm *crypto,
85 void *ctx, const void *data, size_t len ) {
86 crypto->encode ( ctx, data, NULL, len );
89 static inline void digest_final ( struct crypto_algorithm *crypto,
90 void *ctx, void *out ) {
91 crypto->final ( ctx, out );
94 static inline void cipher_setiv ( struct crypto_algorithm *crypto,
95 void *ctx, const void *iv ) {
96 crypto->setiv ( ctx, iv );
99 static inline int cipher_setkey ( struct crypto_algorithm *crypto,
100 void *ctx, const void *key, size_t keylen ) {
101 return crypto->setkey ( ctx, key, keylen );
104 static inline int cipher_encrypt ( struct crypto_algorithm *crypto,
105 void *ctx, const void *src, void *dst,
107 if ( ( len & ( crypto->blocksize - 1 ) ) ) {
110 crypto->encode ( ctx, src, dst, len );
114 static inline int cipher_decrypt ( struct crypto_algorithm *crypto,
115 void *ctx, const void *src, void *dst,
117 if ( ( len & ( crypto->blocksize - 1 ) ) ) {
120 crypto->decode ( ctx, src, dst, len );
124 static inline int is_stream_cipher ( struct crypto_algorithm *crypto ) {
125 return ( crypto->blocksize == 1 );
128 extern struct crypto_algorithm crypto_null;
130 #endif /* _GPXE_CRYPTO_H */