00001 #ifndef _GPXE_CRYPTO_H
00002 #define _GPXE_CRYPTO_H
00003
00004
00005
00006
00007
00008
00009
00010 FILE_LICENCE ( GPL2_OR_LATER );
00011
00012 #include <stdint.h>
00013 #include <stddef.h>
00014
00015
00016 struct digest_algorithm {
00017
00018 const char *name;
00019
00020 size_t ctxsize;
00021
00022 size_t blocksize;
00023
00024 size_t digestsize;
00025
00026
00027
00028
00029 void ( * init ) ( void *ctx );
00030
00031
00032
00033
00034
00035
00036
00037
00038 void ( * update ) ( void *ctx, const void *src, size_t len );
00039
00040
00041
00042
00043
00044 void ( * final ) ( void *ctx, void *out );
00045 };
00046
00047
00048 struct cipher_algorithm {
00049
00050 const char *name;
00051
00052 size_t ctxsize;
00053
00054 size_t blocksize;
00055
00056
00057
00058
00059
00060
00061
00062 int ( * setkey ) ( void *ctx, const void *key, size_t keylen );
00063
00064
00065
00066
00067
00068 void ( * setiv ) ( void *ctx, const void *iv );
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 void ( * encrypt ) ( void *ctx, const void *src, void *dst,
00079 size_t len );
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 void ( * decrypt ) ( void *ctx, const void *src, void *dst,
00090 size_t len );
00091 };
00092
00093
00094 struct pubkey_algorithm {
00095
00096 const char *name;
00097
00098 size_t ctxsize;
00099 };
00100
00101 static inline void digest_init ( struct digest_algorithm *digest,
00102 void *ctx ) {
00103 digest->init ( ctx );
00104 }
00105
00106 static inline void digest_update ( struct digest_algorithm *digest,
00107 void *ctx, const void *data, size_t len ) {
00108 digest->update ( ctx, data, len );
00109 }
00110
00111 static inline void digest_final ( struct digest_algorithm *digest,
00112 void *ctx, void *out ) {
00113 digest->final ( ctx, out );
00114 }
00115
00116 static inline int cipher_setkey ( struct cipher_algorithm *cipher,
00117 void *ctx, const void *key, size_t keylen ) {
00118 return cipher->setkey ( ctx, key, keylen );
00119 }
00120
00121 static inline void cipher_setiv ( struct cipher_algorithm *cipher,
00122 void *ctx, const void *iv ) {
00123 cipher->setiv ( ctx, iv );
00124 }
00125
00126 static inline void cipher_encrypt ( struct cipher_algorithm *cipher,
00127 void *ctx, const void *src, void *dst,
00128 size_t len ) {
00129 cipher->encrypt ( ctx, src, dst, len );
00130 }
00131 #define cipher_encrypt( cipher, ctx, src, dst, len ) do { \
00132 assert ( ( (len) & ( (cipher)->blocksize - 1 ) ) == 0 ); \
00133 cipher_encrypt ( (cipher), (ctx), (src), (dst), (len) ); \
00134 } while ( 0 )
00135
00136 static inline void cipher_decrypt ( struct cipher_algorithm *cipher,
00137 void *ctx, const void *src, void *dst,
00138 size_t len ) {
00139 cipher->decrypt ( ctx, src, dst, len );
00140 }
00141 #define cipher_decrypt( cipher, ctx, src, dst, len ) do { \
00142 assert ( ( (len) & ( (cipher)->blocksize - 1 ) ) == 0 ); \
00143 cipher_decrypt ( (cipher), (ctx), (src), (dst), (len) ); \
00144 } while ( 0 )
00145
00146 static inline int is_stream_cipher ( struct cipher_algorithm *cipher ) {
00147 return ( cipher->blocksize == 1 );
00148 }
00149
00150 extern struct digest_algorithm digest_null;
00151 extern struct cipher_algorithm cipher_null;
00152 extern struct pubkey_algorithm pubkey_null;
00153
00154 void get_random_bytes ( void *buf, size_t len );
00155
00156 #endif