crypto.h

Go to the documentation of this file.
00001 #ifndef _GPXE_CRYPTO_H
00002 #define _GPXE_CRYPTO_H
00003 
00004 /** @file
00005  *
00006  * Cryptographic API
00007  *
00008  */
00009 
00010 FILE_LICENCE ( GPL2_OR_LATER );
00011 
00012 #include <stdint.h>
00013 #include <stddef.h>
00014 
00015 /** A message digest algorithm */
00016 struct digest_algorithm {
00017         /** Algorithm name */
00018         const char *name;
00019         /** Context size */
00020         size_t ctxsize;
00021         /** Block size */
00022         size_t blocksize;
00023         /** Digest size */
00024         size_t digestsize;
00025         /** Initialise digest
00026          *
00027          * @v ctx               Context
00028          */
00029         void ( * init ) ( void *ctx );
00030         /** Update digest with new data
00031          *
00032          * @v ctx               Context
00033          * @v src               Data to digest
00034          * @v len               Length of data
00035          *
00036          * @v len is not necessarily a multiple of @c blocksize.
00037          */
00038         void ( * update ) ( void *ctx, const void *src, size_t len );
00039         /** Finalise digest
00040          *
00041          * @v ctx               Context
00042          * @v out               Buffer for digest output
00043          */
00044         void ( * final ) ( void *ctx, void *out );
00045 };
00046 
00047 /** A cipher algorithm */
00048 struct cipher_algorithm {
00049         /** Algorithm name */
00050         const char *name;
00051         /** Context size */
00052         size_t ctxsize;
00053         /** Block size */
00054         size_t blocksize;
00055         /** Set key
00056          *
00057          * @v ctx               Context
00058          * @v key               Key
00059          * @v keylen            Key length
00060          * @ret rc              Return status code
00061          */
00062         int ( * setkey ) ( void *ctx, const void *key, size_t keylen );
00063         /** Set initialisation vector
00064          *
00065          * @v ctx               Context
00066          * @v iv                Initialisation vector
00067          */
00068         void ( * setiv ) ( void *ctx, const void *iv );
00069         /** Encrypt data
00070          *
00071          * @v ctx               Context
00072          * @v src               Data to encrypt
00073          * @v dst               Buffer for encrypted data
00074          * @v len               Length of data
00075          *
00076          * @v len is guaranteed to be a multiple of @c blocksize.
00077          */
00078         void ( * encrypt ) ( void *ctx, const void *src, void *dst,
00079                              size_t len );
00080         /** Decrypt data
00081          *
00082          * @v ctx               Context
00083          * @v src               Data to decrypt
00084          * @v dst               Buffer for decrypted data
00085          * @v len               Length of data
00086          *
00087          * @v len is guaranteed to be a multiple of @c blocksize.
00088          */
00089         void ( * decrypt ) ( void *ctx, const void *src, void *dst,
00090                              size_t len );
00091 };
00092 
00093 /** A public key algorithm */
00094 struct pubkey_algorithm {
00095         /** Algorithm name */
00096         const char *name;
00097         /** Context size */
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 /* _GPXE_CRYPTO_H */

Generated on Tue Apr 6 20:00:52 2010 for gPXE by  doxygen 1.5.7.1