BeeCrypt
4.2.1
|
00001 /* 00002 * Copyright (c) 1999, 2000, 2001, 2002 X-Way Rights BV 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 00596 typedef int (*blockCipherSetCTR )(blockCipherParam*, const byte*, size_t); 00597 00598 00608 typedef int (*blockCipherRawcrypt)(blockCipherParam*, uint32_t*, const uint32_t*); 00609 00621 typedef int (*blockCipherModcrypt)(blockCipherParam*, uint32_t*, const uint32_t*, unsigned int); 00622 00623 typedef uint32_t* (*blockCipherFeedback)(blockCipherParam*); 00624 00625 typedef struct 00626 { 00627 const blockCipherRawcrypt encrypt; 00628 const blockCipherRawcrypt decrypt; 00629 } blockCipherRaw; 00630 00631 typedef struct 00632 { 00633 const blockCipherModcrypt encrypt; 00634 const blockCipherModcrypt decrypt; 00635 } blockCipherMode; 00636 00643 #ifdef __cplusplus 00644 struct BEECRYPTAPI blockCipher 00645 #else 00646 struct _blockCipher 00647 #endif 00648 { 00652 const char* name; 00656 const size_t paramsize; 00660 const size_t blocksize; 00664 const size_t keybitsmin; 00668 const size_t keybitsmax; 00673 const size_t keybitsinc; 00677 const blockCipherSetup setup; 00681 const blockCipherSetIV setiv; 00685 const blockCipherSetCTR setctr; 00689 const blockCipherFeedback getfb; 00693 const blockCipherRaw raw; 00697 const blockCipherMode ecb; 00701 const blockCipherMode cbc; 00705 const blockCipherMode ctr; 00706 }; 00707 00708 #ifndef __cplusplus 00709 typedef struct _blockCipher blockCipher; 00710 #endif 00711 00712 #ifdef __cplusplus 00713 extern "C" { 00714 #endif 00715 00721 BEECRYPTAPI 00722 int blockCipherCount(void); 00723 00732 BEECRYPTAPI 00733 const blockCipher* blockCipherGet(int); 00734 00740 BEECRYPTAPI 00741 const blockCipher* blockCipherFind(const char*); 00742 00748 BEECRYPTAPI 00749 const blockCipher* blockCipherDefault(void); 00750 00751 #ifdef __cplusplus 00752 } 00753 #endif 00754 00759 #ifdef __cplusplus 00760 struct BEECRYPTAPI blockCipherContext 00761 #else 00762 struct _blockCipherContext 00763 #endif 00764 { 00768 const blockCipher* algo; 00772 blockCipherParam* param; 00775 cipherOperation op; 00776 00777 #ifdef __cplusplus 00778 blockCipherContext(); 00779 blockCipherContext(const blockCipher*); 00780 ~blockCipherContext(); 00781 #endif 00782 }; 00783 00784 #ifndef __cplusplus 00785 typedef struct _blockCipherContext blockCipherContext; 00786 #endif 00787 00788 /* 00789 * The following functions can be used to initialize and free a 00790 * blockCipherContext. Initializing will allocate a buffer of the size 00791 * required by the blockCipher, freeing will deallocate that buffer. 00792 */ 00793 00794 #ifdef __cplusplus 00795 extern "C" { 00796 #endif 00797 00798 BEECRYPTAPI 00799 int blockCipherContextInit(blockCipherContext*, const blockCipher*); 00800 00801 BEECRYPTAPI 00802 int blockCipherContextSetup(blockCipherContext*, const byte*, size_t, cipherOperation); 00803 00804 BEECRYPTAPI 00805 int blockCipherContextSetIV(blockCipherContext*, const byte*); 00806 00807 BEECRYPTAPI 00808 int blockCipherContextSetCTR(blockCipherContext*, const byte*, size_t); 00809 00810 BEECRYPTAPI 00811 int blockCipherContextFree(blockCipherContext*); 00812 00813 BEECRYPTAPI 00814 int blockCipherContextECB(blockCipherContext*, uint32_t*, const uint32_t*, int); 00815 00816 BEECRYPTAPI 00817 int blockCipherContextCBC(blockCipherContext*, uint32_t*, const uint32_t*, int); 00818 00819 BEECRYPTAPI 00820 int blockCipherContextCTR(blockCipherContext*, uint32_t*, const uint32_t*, int); 00821 00822 BEECRYPTAPI 00823 int blockCipherContextValidKeylen(blockCipherContext*, size_t); 00824 00825 #ifdef __cplusplus 00826 } 00827 #endif 00828 00829 #endif