Main Page | Modules | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

beecrypt.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1999, 2000, 2001, 2002 Virtual Unlimited B.V.
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or (at your option) any later version.
00008  * 
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  * 
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with this library; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  *
00018  */
00019 
00030 #ifndef _BEECRYPT_H
00031 #define _BEECRYPT_H
00032 
00033 #include "beecrypt/api.h"
00034 
00035 #include "beecrypt/memchunk.h"
00036 #include "beecrypt/mpnumber.h"
00037 
00038 /*
00039  * Entropy Sources
00040  */
00041 
00046 typedef int (*entropyNext)(byte*, size_t);
00047 
00052 #ifdef __cplusplus
00053 struct BEECRYPTAPI entropySource
00054 #else
00055 struct _entropySource
00056 #endif
00057 {
00061     const char*         name;
00065     const entropyNext   next;
00066 };
00067 
00068 #ifndef __cplusplus
00069 typedef struct _entropySource entropySource;
00070 #endif
00071 
00072 #ifdef __cplusplus
00073 extern "C" {
00074 #endif
00075 
00081 BEECRYPTAPI
00082 int                     entropySourceCount(void);
00083 
00092 BEECRYPTAPI
00093 const entropySource*    entropySourceGet(int n);
00094 
00100 BEECRYPTAPI
00101 const entropySource*    entropySourceFind(const char* name);
00102 
00108 BEECRYPTAPI
00109 const entropySource*    entropySourceDefault(void);
00110 
00122 BEECRYPTAPI
00123 int                     entropyGatherNext(byte*, size_t);
00124 
00125 #ifdef __cplusplus
00126 }
00127 #endif
00128 
00129 /*
00130  * Pseudo-random Number Generators
00131  */
00132 
00133 typedef void randomGeneratorParam;
00134 
00135 typedef int (*randomGeneratorSetup  )(randomGeneratorParam*);
00136 typedef int (*randomGeneratorSeed   )(randomGeneratorParam*, const byte*, size_t);
00137 typedef int (*randomGeneratorNext   )(randomGeneratorParam*, byte*, size_t);
00138 typedef int (*randomGeneratorCleanup)(randomGeneratorParam*);
00139 
00140 /*
00141  * The struct 'randomGenerator' holds information and pointers to code specific
00142  * to each random generator. Each specific random generator MUST be written to
00143  * be multithread safe.
00144  *
00145  * WARNING: each randomGenerator, when used in cryptographic applications, MUST
00146  * be guaranteed to be of suitable quality and strength (i.e. don't use the
00147  * random() function found in most UN*X-es).
00148  *
00149  * Multiple instances of each randomGenerator can be used (even concurrently),
00150  * provided they each use their own randomGeneratorParam parameters, a chunk
00151  * of memory which must be at least as large as indicated by the paramsize
00152  * field.
00153  *
00154  */
00155 
00160 #ifdef __cplusplus
00161 struct BEECRYPTAPI randomGenerator
00162 #else
00163 struct _randomGenerator
00164 #endif
00165 {
00169     const char*                     name;
00175     const size_t                    paramsize;
00179     const randomGeneratorSetup      setup;
00183     const randomGeneratorSeed       seed;
00187     const randomGeneratorNext       next;
00191     const randomGeneratorCleanup    cleanup;
00192 };
00193 
00194 #ifndef __cplusplus
00195 typedef struct _randomGenerator randomGenerator;
00196 #endif
00197 
00198 /*
00199  * You can use the following functions to find random generators implemented by
00200  * the library:
00201  *
00202  * randomGeneratorCount returns the number of generators available.
00203  *
00204  * randomGeneratorGet returns the random generator with a given index (starting
00205  * at zero, up to randomGeneratorCount() - 1), or NULL if the index was out of
00206  * bounds.
00207  *
00208  * randomGeneratorFind returns the random generator with the given name, or
00209  * NULL if no random generator exists with that name.
00210  */
00211 
00212 #ifdef __cplusplus
00213 extern "C" {
00214 #endif
00215 
00216 BEECRYPTAPI
00217 int                     randomGeneratorCount(void);
00218 BEECRYPTAPI
00219 const randomGenerator*  randomGeneratorGet(int);
00220 BEECRYPTAPI
00221 const randomGenerator*  randomGeneratorFind(const char*);
00222 BEECRYPTAPI
00223 const randomGenerator*  randomGeneratorDefault(void);
00224 
00225 #ifdef __cplusplus
00226 }
00227 #endif
00228 
00229 /*
00230  * The struct 'randomGeneratorContext' is used to contain both the functional
00231  * part (the randomGenerator), and its parameters.
00232  */
00233 
00234 #ifdef __cplusplus
00235 struct BEECRYPTAPI randomGeneratorContext
00236 #else
00237 struct _randomGeneratorContext
00238 #endif
00239 {
00240     const randomGenerator* rng;
00241     randomGeneratorParam* param;
00242 
00243     #ifdef __cplusplus
00244     randomGeneratorContext();
00245     randomGeneratorContext(const randomGenerator*);
00246     ~randomGeneratorContext();
00247     #endif
00248 };
00249 
00250 #ifndef __cplusplus
00251 typedef struct _randomGeneratorContext randomGeneratorContext;
00252 #endif
00253 
00254 /*
00255  * The following functions can be used to initialize and free a
00256  * randomGeneratorContext. Initializing will allocate a buffer of the size
00257  * required by the randomGenerator, freeing will deallocate that buffer.
00258  */
00259 
00260 #ifdef __cplusplus
00261 extern "C" {
00262 #endif
00263 
00264 BEECRYPTAPI
00265 int randomGeneratorContextInit(randomGeneratorContext*, const randomGenerator*);
00266 BEECRYPTAPI
00267 int randomGeneratorContextFree(randomGeneratorContext*);
00268 BEECRYPTAPI
00269 int randomGeneratorContextNext(randomGeneratorContext*, byte*, size_t);
00270 BEECRYPTAPI
00271 int randomGeneratorContextSeed(randomGeneratorContext*, const byte*, size_t);
00272 
00273 #ifdef __cplusplus
00274 }
00275 #endif
00276 
00277 /*
00278  * Hash Functions
00279  */
00280 
00284 typedef void hashFunctionParam;
00285 
00286 typedef int (*hashFunctionReset )(hashFunctionParam*);
00287 typedef int (*hashFunctionUpdate)(hashFunctionParam*, const byte*, size_t);
00288 typedef int (*hashFunctionDigest)(hashFunctionParam*, byte*);
00289 
00290 /*
00291  * The struct 'hashFunction' holds information and pointers to code specific
00292  * to each hash function. Specific hash functions MAY be written to be
00293  * multithread-safe.
00294  *
00295  * NOTE: data MUST have a size (in bytes) of at least 'digestsize' as described
00296  * in the hashFunction struct.
00297  * NOTE: for safety reasons, after calling digest, each specific implementation
00298  * MUST reset itself so that previous values in the parameters are erased.
00299  */
00300 #ifdef __cplusplus
00301 struct BEECRYPTAPI hashFunction
00302 #else
00303 struct _hashFunction
00304 #endif
00305 {
00306     const char*                 name;
00307     const size_t                paramsize;  /* in bytes */
00308     const size_t                blocksize;  /* in bytes */
00309     const size_t                digestsize; /* in bytes */
00310     const hashFunctionReset     reset;
00311     const hashFunctionUpdate    update;
00312     const hashFunctionDigest    digest;
00313 };
00314 
00315 #ifndef __cplusplus
00316 typedef struct _hashFunction hashFunction;
00317 #endif
00318 
00319 /*
00320  * You can use the following functions to find hash functions implemented by
00321  * the library:
00322  *
00323  * hashFunctionCount returns the number of hash functions available.
00324  *
00325  * hashFunctionGet returns the hash function with a given index (starting
00326  * at zero, up to hashFunctionCount() - 1), or NULL if the index was out of
00327  * bounds.
00328  *
00329  * hashFunctionFind returns the hash function with the given name, or
00330  * NULL if no hash function exists with that name.
00331  */
00332 
00333 #ifdef __cplusplus
00334 extern "C" {
00335 #endif
00336 
00337 BEECRYPTAPI
00338 int                 hashFunctionCount(void);
00339 BEECRYPTAPI
00340 const hashFunction* hashFunctionGet(int);
00341 BEECRYPTAPI
00342 const hashFunction* hashFunctionFind(const char*);
00343 BEECRYPTAPI
00344 const hashFunction* hashFunctionDefault(void);
00345 
00346 #ifdef __cplusplus
00347 }
00348 #endif
00349 
00350 /*
00351  * The struct 'hashFunctionContext' is used to contain both the functional
00352  * part (the hashFunction), and its parameters.
00353  */
00354 #ifdef __cplusplus
00355 struct BEECRYPTAPI hashFunctionContext
00356 #else
00357 struct _hashFunctionContext
00358 #endif
00359 {
00360     const hashFunction* algo;
00361     hashFunctionParam* param;
00362 
00363     #ifdef __cplusplus
00364     hashFunctionContext();
00365     hashFunctionContext(const hashFunction*);
00366     ~hashFunctionContext();
00367     #endif
00368 };
00369 
00370 #ifndef __cplusplus
00371 typedef struct _hashFunctionContext hashFunctionContext;
00372 #endif
00373 
00374 /*
00375  * The following functions can be used to initialize and free a
00376  * hashFunctionContext. Initializing will allocate a buffer of the size
00377  * required by the hashFunction, freeing will deallocate that buffer.
00378  */
00379 
00380 #ifdef __cplusplus
00381 extern "C" {
00382 #endif
00383 
00384 BEECRYPTAPI
00385 int hashFunctionContextInit(hashFunctionContext*, const hashFunction*);
00386 BEECRYPTAPI
00387 int hashFunctionContextFree(hashFunctionContext*);
00388 BEECRYPTAPI
00389 int hashFunctionContextReset(hashFunctionContext*);
00390 BEECRYPTAPI
00391 int hashFunctionContextUpdate(hashFunctionContext*, const byte*, size_t);
00392 BEECRYPTAPI
00393 int hashFunctionContextUpdateMC(hashFunctionContext*, const memchunk*);
00394 BEECRYPTAPI
00395 int hashFunctionContextUpdateMP(hashFunctionContext*, const mpnumber*);
00396 BEECRYPTAPI
00397 int hashFunctionContextDigest(hashFunctionContext*, byte*);
00398 BEECRYPTAPI
00399 int hashFunctionContextDigestMP(hashFunctionContext*, mpnumber*);
00400 BEECRYPTAPI
00401 int hashFunctionContextDigestMatch(hashFunctionContext*, const mpnumber*);
00402 
00403 #ifdef __cplusplus
00404 }
00405 #endif
00406 
00407 /*
00408  * Keyed Hash Functions, a.k.a. Message Authentication Codes
00409  */
00410 
00414 typedef void keyedHashFunctionParam;
00415 
00416 typedef int (*keyedHashFunctionSetup  )(keyedHashFunctionParam*, const byte*, size_t);
00417 typedef int (*keyedHashFunctionReset  )(keyedHashFunctionParam*);
00418 typedef int (*keyedHashFunctionUpdate )(keyedHashFunctionParam*, const byte*, size_t);
00419 typedef int (*keyedHashFunctionDigest )(keyedHashFunctionParam*, byte*);
00420 
00421 /*
00422  * The struct 'keyedHashFunction' holds information and pointers to code
00423  * specific to each keyed hash function. Specific keyed hash functions MAY be
00424  * written to be multithread-safe.
00425  *
00426  * The struct field 'keybitsmin' contains the minimum number of bits a key
00427  * must contains, 'keybitsmax' the maximum number of bits a key may contain,
00428  * 'keybitsinc', the increment in bits that may be used between min and max.
00429  * 
00430  * NOTE: data must be at least have a bytesize of 'digestsize' as described
00431  * in the keyedHashFunction struct.
00432  * NOTE: for safety reasons, after calling digest, each specific implementation
00433  * MUST reset itself so that previous values in the parameters are erased.
00434  */
00435 #ifdef __cplusplus
00436 struct BEECRYPTAPI keyedHashFunction
00437 #else
00438 struct _keyedHashFunction
00439 #endif
00440 {
00441     const char*                     name;
00442     const size_t                    paramsize;  /* in bytes */
00443     const size_t                    blocksize;  /* in bytes */
00444     const size_t                    digestsize; /* in bytes */
00445     const size_t                    keybitsmin; /* in bits */
00446     const size_t                    keybitsmax; /* in bits */
00447     const size_t                    keybitsinc; /* in bits */
00448     const keyedHashFunctionSetup    setup;
00449     const keyedHashFunctionReset    reset;
00450     const keyedHashFunctionUpdate   update;
00451     const keyedHashFunctionDigest   digest;
00452 };
00453 
00454 #ifndef __cplusplus
00455 typedef struct _keyedHashFunction keyedHashFunction;
00456 #endif
00457 
00458 /*
00459  * You can use the following functions to find keyed hash functions implemented
00460  * by the library:
00461  *
00462  * keyedHashFunctionCount returns the number of keyed hash functions available.
00463  *
00464  * keyedHashFunctionGet returns the keyed hash function with a given index
00465  * (starting at zero, up to keyedHashFunctionCount() - 1), or NULL if the index
00466  * was out of bounds.
00467  *
00468  * keyedHashFunctionFind returns the keyed hash function with the given name,
00469  * or NULL if no keyed hash function exists with that name.
00470  */
00471 
00472 #ifdef __cplusplus
00473 extern "C" {
00474 #endif
00475 
00476 BEECRYPTAPI
00477 int                         keyedHashFunctionCount(void);
00478 BEECRYPTAPI
00479 const keyedHashFunction*    keyedHashFunctionGet(int);
00480 BEECRYPTAPI
00481 const keyedHashFunction*    keyedHashFunctionFind(const char*);
00482 BEECRYPTAPI
00483 const keyedHashFunction*    keyedHashFunctionDefault(void);
00484 
00485 #ifdef __cplusplus
00486 }
00487 #endif
00488 
00489 /*
00490  * The struct 'keyedHashFunctionContext' is used to contain both the functional
00491  * part (the keyedHashFunction), and its parameters.
00492  */
00493 #ifdef __cplusplus
00494 struct BEECRYPTAPI keyedHashFunctionContext
00495 #else
00496 struct _keyedHashFunctionContext
00497 #endif
00498 {
00499     const keyedHashFunction*    algo;
00500     keyedHashFunctionParam*     param;
00501 
00502     #ifdef __cplusplus
00503     keyedHashFunctionContext();
00504     keyedHashFunctionContext(const keyedHashFunction*);
00505     ~keyedHashFunctionContext();
00506     #endif
00507 };
00508 
00509 #ifndef __cplusplus
00510 typedef struct _keyedHashFunctionContext keyedHashFunctionContext;
00511 #endif
00512 
00513 /*
00514  * The following functions can be used to initialize and free a
00515  * keyedHashFunctionContext. Initializing will allocate a buffer of the size
00516  * required by the keyedHashFunction, freeing will deallocate that buffer.
00517  */
00518 
00519 #ifdef __cplusplus
00520 extern "C" {
00521 #endif
00522 
00523 BEECRYPTAPI
00524 int keyedHashFunctionContextInit(keyedHashFunctionContext*, const keyedHashFunction*);
00525 BEECRYPTAPI
00526 int keyedHashFunctionContextFree(keyedHashFunctionContext*);
00527 BEECRYPTAPI
00528 int keyedHashFunctionContextSetup(keyedHashFunctionContext*, const byte*, size_t);
00529 BEECRYPTAPI
00530 int keyedHashFunctionContextReset(keyedHashFunctionContext*);
00531 BEECRYPTAPI
00532 int keyedHashFunctionContextUpdate(keyedHashFunctionContext*, const byte*, size_t);
00533 BEECRYPTAPI
00534 int keyedHashFunctionContextUpdateMC(keyedHashFunctionContext*, const memchunk*);
00535 BEECRYPTAPI
00536 int keyedHashFunctionContextUpdateMP(keyedHashFunctionContext*, const mpnumber*);
00537 BEECRYPTAPI
00538 int keyedHashFunctionContextDigest(keyedHashFunctionContext*, byte*);
00539 BEECRYPTAPI
00540 int keyedHashFunctionContextDigestMP(keyedHashFunctionContext*, mpnumber*);
00541 BEECRYPTAPI
00542 int keyedHashFunctionContextDigestMatch(keyedHashFunctionContext*, const mpnumber*);
00543 
00544 #ifdef __cplusplus
00545 }
00546 #endif
00547 
00548 /*
00549  * Block ciphers
00550  */
00551 
00556 typedef enum
00557 {
00558     NOCRYPT,
00559     ENCRYPT,
00560     DECRYPT
00561 } cipherOperation;
00562 
00568 typedef void blockCipherParam;
00569 
00573 typedef int (*blockCipherSetup  )(blockCipherParam*, const byte*, size_t, cipherOperation);
00574 
00584 typedef int (*blockCipherSetIV  )(blockCipherParam*, const byte*);
00585 
00595 typedef int (*blockCipherRawcrypt)(blockCipherParam*, uint32_t*, const uint32_t*);
00596 
00608 typedef int (*blockCipherModcrypt)(blockCipherParam*, uint32_t*, const uint32_t*, unsigned int);
00609 
00610 typedef uint32_t* (*blockCipherFeedback)(blockCipherParam*);
00611 
00612 typedef struct
00613 {
00614     const blockCipherRawcrypt encrypt;
00615     const blockCipherRawcrypt decrypt;
00616 } blockCipherRaw;
00617 
00618 typedef struct
00619 {
00620     const blockCipherModcrypt encrypt;
00621     const blockCipherModcrypt decrypt;
00622 } blockCipherMode;
00623 
00630 #ifdef __cplusplus
00631 struct BEECRYPTAPI blockCipher
00632 #else
00633 struct _blockCipher
00634 #endif
00635 {
00639     const char*                 name;
00643     const size_t                paramsize;
00647     const size_t                blocksize;
00651     const size_t                keybitsmin;
00655     const size_t                keybitsmax;
00660     const size_t                keybitsinc;
00664     const blockCipherSetup      setup;
00668     const blockCipherSetIV      setiv;
00672     const blockCipherRaw        raw;
00676     const blockCipherMode       ecb;
00677     const blockCipherMode       cbc;
00681     const blockCipherFeedback   getfb;
00682 };
00683 
00684 #ifndef __cplusplus
00685 typedef struct _blockCipher blockCipher;
00686 #endif
00687 
00688 #ifdef __cplusplus
00689 extern "C" {
00690 #endif
00691 
00697 BEECRYPTAPI
00698 int                     blockCipherCount(void);
00699 
00708 BEECRYPTAPI
00709 const blockCipher*      blockCipherGet(int);
00710 
00716 BEECRYPTAPI
00717 const blockCipher*      blockCipherFind(const char*);
00718 
00724 BEECRYPTAPI
00725 const blockCipher*      blockCipherDefault(void);
00726 
00727 #ifdef __cplusplus
00728 }
00729 #endif
00730 
00735 #ifdef __cplusplus
00736 struct BEECRYPTAPI blockCipherContext
00737 #else
00738 struct _blockCipherContext
00739 #endif
00740 {
00744     const blockCipher*  algo;
00748     blockCipherParam*   param;
00751     cipherOperation     op;
00752 
00753     #ifdef __cplusplus
00754     blockCipherContext();
00755     blockCipherContext(const blockCipher*);
00756     ~blockCipherContext();
00757     #endif
00758 };
00759 
00760 #ifndef __cplusplus
00761 typedef struct _blockCipherContext blockCipherContext;
00762 #endif
00763 
00764 /*
00765  * The following functions can be used to initialize and free a
00766  * blockCipherContext. Initializing will allocate a buffer of the size
00767  * required by the blockCipher, freeing will deallocate that buffer.
00768  */
00769 
00770 #ifdef __cplusplus
00771 extern "C" {
00772 #endif
00773 
00774 BEECRYPTAPI
00775 int blockCipherContextInit(blockCipherContext*, const blockCipher*);
00776 
00777 BEECRYPTAPI
00778 int blockCipherContextSetup(blockCipherContext*, const byte*, size_t, cipherOperation);
00779 
00780 BEECRYPTAPI
00781 int blockCipherContextSetIV(blockCipherContext*, const byte*);
00782 
00783 BEECRYPTAPI
00784 int blockCipherContextFree(blockCipherContext*);
00785 
00786 BEECRYPTAPI
00787 int blockCipherContextECB(blockCipherContext*, uint32_t*, const uint32_t*, int);
00788 
00789 BEECRYPTAPI
00790 int blockCipherContextCBC(blockCipherContext*, uint32_t*, const uint32_t*, int);
00791 
00792 BEECRYPTAPI
00793 int blockCipherContextValidKeylen(blockCipherContext*, size_t);
00794 
00795 #ifdef __cplusplus
00796 }
00797 #endif
00798 
00799 #endif

Generated on Mon Jun 20 10:09:20 2005 for BeeCrypt by  doxygen 1.4.0