123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- #ifndef _GPXE_CRYPTO_H
- #define _GPXE_CRYPTO_H
-
-
-
- #include <stdint.h>
- #include <stddef.h>
- #include <errno.h>
-
-
- struct crypto_algorithm {
-
- const char *name;
-
- size_t ctxsize;
-
- size_t blocksize;
-
- size_t digestsize;
-
-
- void ( * init ) ( void *ctx );
-
-
- int ( * setkey ) ( void *ctx, const void *key, size_t keylen );
-
-
- void ( *setiv ) ( void *ctx, const void *iv );
-
-
- void ( * encode ) ( void *ctx, const void *src, void *dst,
- size_t len );
-
-
- void ( * decode ) ( void *ctx, const void *src, void *dst,
- size_t len );
-
-
- void ( * final ) ( void *ctx, void *out );
- };
-
- static inline void digest_init ( struct crypto_algorithm *crypto,
- void *ctx ) {
- crypto->init ( ctx );
- }
-
- static inline void digest_update ( struct crypto_algorithm *crypto,
- void *ctx, const void *data, size_t len ) {
- crypto->encode ( ctx, data, NULL, len );
- }
-
- static inline void digest_final ( struct crypto_algorithm *crypto,
- void *ctx, void *out ) {
- crypto->final ( ctx, out );
- }
-
- static inline void cipher_setiv ( struct crypto_algorithm *crypto,
- void *ctx, const void *iv ) {
- crypto->setiv ( ctx, iv );
- }
-
- static inline int cipher_setkey ( struct crypto_algorithm *crypto,
- void *ctx, const void *key, size_t keylen ) {
- return crypto->setkey ( ctx, key, keylen );
- }
-
- static inline int cipher_encrypt ( struct crypto_algorithm *crypto,
- void *ctx, const void *src, void *dst,
- size_t len ) {
- if ( ( len & ( crypto->blocksize - 1 ) ) ) {
- return -EINVAL;
- }
- crypto->encode ( ctx, src, dst, len );
- return 0;
- }
-
- static inline int cipher_decrypt ( struct crypto_algorithm *crypto,
- void *ctx, const void *src, void *dst,
- size_t len ) {
- if ( ( len & ( crypto->blocksize - 1 ) ) ) {
- return -EINVAL;
- }
- crypto->decode ( ctx, src, dst, len );
- return 0;
- }
-
- static inline int is_stream_cipher ( struct crypto_algorithm *crypto ) {
- return ( crypto->blocksize == 1 );
- }
-
- extern struct crypto_algorithm crypto_null;
-
- #endif
|