1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
|
/*
* beecrypt.h
*
* Beecrypt library hooks & stubs, header
*
* Copyright (c) 1999-2000 Virtual Unlimited B.V.
*
* Author: Bob Deblier <bob@virtualunlimited.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef _BEECRYPT_H
#define _BEECRYPT_H
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
typedef struct
{
int size;
byte* data;
} memchunk;
/*
* Entropy Sources
*/
typedef int (*entropyNext)(uint32*, int);
/*
* The struct 'entropySource' holds information and pointers to code specific
* to each entropy source. Each specific entropy source MUST be written to be
* multithread-safe.
*
* The struct contains the following function(s):
*
* int (*next)(uint32* data, int size);
*
* This function will fill an array of 32-bit unsigned integers of given size
* with entropy.
* Return value is 0 on success, or -1 on failure.
*/
typedef struct
{
const char* name;
const entropyNext next;
} entropySource;
/*
* You can use the following functions to find entropy sources implemented by
* the library:
*
* entropySourceCount returns the number of sources available.
*
* entropySourceGet returns the entropy source with a given index (starting
* at zero, up to entropySourceCount() - 1), or NULL if the index was out of
* bounds.
*
* entropySourceFind returns the entropy source with the given name, or NULL
* if no entropy source exists with that name.
*/
#ifdef __cplusplus
extern "C" {
#endif
BEEDLLAPI
int entropySourceCount();
BEEDLLAPI
const entropySource* entropySourceGet(int);
BEEDLLAPI
const entropySource* entropySourceFind(const char*);
BEEDLLAPI
const entropySource* entropySourceDefault();
#ifdef __cplusplus
}
#endif
/*
* Pseudo-random Number Generators
*/
typedef void randomGeneratorParam;
typedef int (*randomGeneratorSetup )(randomGeneratorParam*);
typedef int (*randomGeneratorSeed )(randomGeneratorParam*, const uint32*, int);
typedef int (*randomGeneratorNext )(randomGeneratorParam*, uint32*, int);
typedef int (*randomGeneratorCleanup)(randomGeneratorParam*);
/*
* The struct 'randomGenerator' holds information and pointers to code specific
* to each random generator. Each specific random generator MUST be written to
* be multithread safe.
*
* WARNING: each randomGenerator, when used in cryptographic applications, MUST
* be guaranteed to be of suitable quality and strength (i.e. don't use the
* random() function found in most UN*X-es).
*
* Multiple instances of each randomGenerator can be used (even concurrently),
* provided they each use their own randomGeneratorParam parameters, a chunk
* of memory which must be at least as large as indicated by the paramsize
* field.
*
* The struct contains the following function(s):
*
* int (*setup)(randomGeneratorParam* param);
*
* This function will initialize the parameters for use, and seed the generator
* with entropy from the default entropy source.
* Return value is 0 on success, or -1 on failure.
*
* int (*seed)(randomGeneratorParam* param, const uint32* data, int size);
*
* This function reseeds the random generator with user-provided entropy.
* Return value is 0 on success, or -1 on failure.
*
* int (*next)(randomGeneratorParam* param, uint32* data, int size);
*
* This function will fill an array of 32-bit unsigned integers of given size
* with pseudo-random data.
* Return value is 0 on success, or -1 on failure.
*
* int (*cleanup)(randomGeneratorParam* param);
*
* This function will cleanup after the use of a generator
* Return value is 0 on success, or -1 on failure.
*/
typedef struct
{
const char* name;
const int paramsize;
const randomGeneratorSetup setup;
const randomGeneratorSeed seed;
const randomGeneratorNext next;
const randomGeneratorCleanup cleanup;
} randomGenerator;
/*
* You can use the following functions to find random generators implemented by
* the library:
*
* randomGeneratorCount returns the number of generators available.
*
* randomGeneratorGet returns the random generator with a given index (starting
* at zero, up to randomGeneratorCount() - 1), or NULL if the index was out of
* bounds.
*
* randomGeneratorFind returns the random generator with the given name, or
* NULL if no random generator exists with that name.
*/
#ifdef __cplusplus
extern "C" {
#endif
BEEDLLAPI
int randomGeneratorCount();
BEEDLLAPI
const randomGenerator* randomGeneratorGet(int);
BEEDLLAPI
const randomGenerator* randomGeneratorFind(const char*);
BEEDLLAPI
const randomGenerator* randomGeneratorDefault();
#ifdef __cplusplus
}
#endif
/*
* The struct 'randomGeneratorContext' is used to contain both the functional
* part (the randomGenerator), and its parameters.
*/
typedef struct
{
const randomGenerator* rng;
randomGeneratorParam* param;
} randomGeneratorContext;
/*
* The following functions can be used to initialize and free a
* randomGeneratorContext. Initializing will allocate a buffer of the size
* required by the randomGenerator, freeing will deallocate that buffer.
*/
#ifdef __cplusplus
extern "C" {
#endif
BEEDLLAPI
void randomGeneratorContextInit(randomGeneratorContext*, const randomGenerator*);
BEEDLLAPI
void randomGeneratorContextFree(randomGeneratorContext*);
#ifdef __cplusplus
}
#endif
/*
* Hash Functions
*/
typedef void hashFunctionParam;
typedef int (*hashFunctionReset )(hashFunctionParam*);
typedef int (*hashFunctionUpdate)(hashFunctionParam*, const byte*, int);
typedef int (*hashFunctionDigest)(hashFunctionParam*, uint32*);
/*
* The struct 'hashFunction' holds information and pointers to code specific
* to each hash function. Specific hash functions MAY be written to be
* multithread-safe.
*
* The struct contains the following function(s):
*
* int (*reset)(hashFunctionParam* param);
*
* This function will re-initialize the parameters of this hash function.
* Return value is 0 on success, or -1 on failure.
*
* int (*update)(hashFunctionParam* param, const byte* data, int size);
*
* This function updates the hash function with an array of bytes.
* Return value is 0 on success, or -1 on failure.
*
* int (*digest)(hashFunctionParam* param, uint32* data);
*
* This function computes the digest of all the data passed to the hash
* function, and stores the result in data.
* Return value is 0 on success, or -1 on failure.
* NOTE: data must be at least have a bytesize of 'digestsize' as described
* in the hashFunction struct.
* NOTE: for safety reasons, after calling digest, each specific implementation
* MUST reset itself so that previous values in the parameters are erased.
*/
typedef struct
{
const char* name;
const int paramsize; /* in bytes */
const int digestsize; /* in bytes */
const hashFunctionReset reset;
const hashFunctionUpdate update;
const hashFunctionDigest digest;
} hashFunction;
/*
* You can use the following functions to find hash functions implemented by
* the library:
*
* hashFunctionCount returns the number of hash functions available.
*
* hashFunctionGet returns the hash function with a given index (starting
* at zero, up to hashFunctionCount() - 1), or NULL if the index was out of
* bounds.
*
* hashFunctionFind returns the hash function with the given name, or
* NULL if no hash function exists with that name.
*/
#ifdef __cplusplus
extern "C" {
#endif
BEEDLLAPI
int hashFunctionCount();
BEEDLLAPI
const hashFunction* hashFunctionGet(int);
BEEDLLAPI
const hashFunction* hashFunctionFind(const char*);
BEEDLLAPI
const hashFunction* hashFunctionDefault();
#ifdef __cplusplus
}
#endif
/*
* The struct 'hashFunctionContext' is used to contain both the functional
* part (the hashFunction), and its parameters.
*/
typedef struct
{
const hashFunction* hash;
hashFunctionParam* param;
} hashFunctionContext;
/*
* The following functions can be used to initialize and free a
* hashFunctionContext. Initializing will allocate a buffer of the size
* required by the hashFunction, freeing will deallocate that buffer.
*/
#ifdef __cplusplus
extern "C" {
#endif
BEEDLLAPI
void hashFunctionContextInit(hashFunctionContext*, const hashFunction*);
BEEDLLAPI
void hashFunctionContextFree(hashFunctionContext*);
#ifdef __cplusplus
}
#endif
/*
* Keyed Hash Functions, a.k.a. Message Authentication Codes
*/
typedef void keyedHashFunctionParam;
typedef int (*keyedHashFunctionSetup )(keyedHashFunctionParam*, const uint32*, int);
typedef int (*keyedHashFunctionReset )(keyedHashFunctionParam*);
typedef int (*keyedHashFunctionUpdate )(keyedHashFunctionParam*, const byte*, int);
typedef int (*keyedHashFunctionDigest )(keyedHashFunctionParam*, uint32*);
/*
* The struct 'keyedHashFunction' holds information and pointers to code
* specific to each keyed hash function. Specific keyed hash functions MAY be
* written to be multithread-safe.
*
* The struct field 'keybitsmin' contains the minimum number of bits a key
* must contains, 'keybitsmax' the maximum number of bits a key may contain,
* 'keybitsinc', the increment in bits that may be used between min and max.
*
* The struct contains the following function(s):
*
* int (*setup)(keyedHashFunctionParam *param, const uint32* key, int keybits);
*
* This function will setup the keyed hash function parameters with the given
* secret key; it will also 'reset' the parameters.
* Return value is 0 on success, or -1 on failure.
* NOTE: after use, it is recommended to wipe the parameters by calling setup
* again with another (dummy) key.
*
* int (*reset)(keyedHashFunctionParam* param);
*
* This function will re-initialize the parameters of this keyed hash function.
* Return value is 0 on success, or -1 on failure.
*
* int (*update)(keyedHashFunctionParam* param, const byte* data, int size);
*
* This function updates the keyed hash function with an array of bytes.
* Return value is 0 on success, or -1 on failure.
*
* int (*digest)(keyedHashFunctionParam* param, uint32* data);
*
* This function computes the digest (or authentication code) of all the data
* passed to the keyed hash function, and stores the result in data.
* Return value is 0 on success, or -1 on failure.
* NOTE: data must be at least have a bytesize of 'digestsize' as described
* in the keyedHashFunction struct.
* NOTE: for safety reasons, after calling digest, each specific implementation
* MUST reset itself so that previous values in the parameters are erased.
*/
typedef struct
{
const char* name;
const int paramsize; /* in bytes */
const int digestsize; /* in bytes */
const int keybitsmin; /* in bits */
const int keybitsmax; /* in bits */
const int keybitsinc; /* in bits */
const keyedHashFunctionSetup setup;
const keyedHashFunctionReset reset;
const keyedHashFunctionUpdate update;
const keyedHashFunctionDigest digest;
} keyedHashFunction;
/*
* You can use the following functions to find keyed hash functions implemented
* by the library:
*
* keyedHashFunctionCount returns the number of keyed hash functions available.
*
* keyedHashFunctionGet returns the random generator with a given index
* (starting at zero, up to keyedHashFunctionCount() - 1), or NULL if the index
* was out of bounds.
*
* keyedHashFunctionFind returns the keyed hash function with the given name,
* or NULL if no keyed hash function exists with that name.
*/
#ifdef __cplusplus
extern "C" {
#endif
BEEDLLAPI
int keyedHashFunctionCount();
BEEDLLAPI
const keyedHashFunction* keyedHashFunctionGet(int);
BEEDLLAPI
const keyedHashFunction* keyedHashFunctionFind(const char*);
BEEDLLAPI
const keyedHashFunction* keyedHashFunctionDefault();
#ifdef __cplusplus
}
#endif
/*
* The struct 'keyedHashFunctionContext' is used to contain both the functional
* part (the keyedHashFunction), and its parameters.
*/
typedef struct
{
const keyedHashFunction* hash;
keyedHashFunctionParam* param;
} keyedHashFunctionContext;
/*
* The following functions can be used to initialize and free a
* keyedHashFunctionContext. Initializing will allocate a buffer of the size
* required by the keyedHashFunction, freeing will deallocate that buffer.
*/
#ifdef __cplusplus
extern "C" {
#endif
BEEDLLAPI
void keyedHashFunctionContextInit(keyedHashFunctionContext*, const keyedHashFunction*);
BEEDLLAPI
void keyedHashFunctionContextFree(keyedHashFunctionContext*);
#ifdef __cplusplus
}
#endif
/*
* Block ciphers
*/
typedef enum
{
ENCRYPT,
DECRYPT
} cipherOperation;
typedef enum
{
ECB,
CBC
} cipherMode;
typedef void blockCipherParam;
typedef int (*blockModeEncrypt)(blockCipherParam*, int, uint32*, const uint32*, const uint32*);
typedef int (*blockModeDecrypt)(blockCipherParam*, int, uint32*, const uint32*, const uint32*);
typedef struct
{
const blockModeEncrypt encrypt;
const blockModeDecrypt decrypt;
} blockMode;
typedef int (*blockCipherSetup )(blockCipherParam*, const uint32*, int, cipherOperation);
typedef int (*blockCipherEncrypt)(blockCipherParam*, uint32 *);
typedef int (*blockCipherDecrypt)(blockCipherParam*, uint32 *);
/*
* The struct 'blockCipher' holds information and pointers to code specific
* to each blockcipher. Specific block ciphers MAY be written to be
* multithread-safe.
*
* The struct field 'keybitsmin' contains the minimum number of bits a key
* must contains, 'keybitsmax' the maximum number of bits a key may contain,
* 'keybitsinc', the increment in bits that may be used between min and max.
*
* The struct contains the following function(s):
*
* int (*setup)(blockCipherParam *param, const uint32* key, int keybits, cipherOperation);
*
* This function will setup the blockcipher parameters with the given secret
* key for either encryption or decryption;
* Return value is 0 on success, or -1 on failure.
* NOTE: after use, it is recommended to wipe the parameters by calling setup
* again with another (dummy) key.
*
* int (*encrypt)(blockCipherParam* param, uint32* block);
*
* This function will encrypt one block of data (with bit size equal to
* 'blockbits')
* Return value is 0 on success, or -1 on failure.
* NOTE: this is raw encryption, without padding, etc.
*
* int (*decrypt)(blockCipherParam* param, uint32* block);
*
* This function will decrypt one block of data (with bit size equal to
* 'blockbits')
* Return value is 0 on success, or -1 on failure.
* NOTE: this is raw decryption, without padding, etc.
*/
typedef struct
{
const char* name;
const int paramsize; /* in bytes */
const int keybitsmin; /* in bits */
const int keybitsmax; /* in bits */
const int keybitsinc; /* in bits */
const int blockbits; /* in bits */
const blockCipherSetup setup;
const blockCipherEncrypt encrypt;
const blockCipherDecrypt decrypt;
const blockMode* mode;
} blockCipher;
/*
* You can use the following functions to find blockciphers implemented by
* the library:
*
* blockCipherCount returns the number of blockciphers available.
*
* blockCipherGet returns the blockcipher with a given index (starting
* at zero, up to blockCipherCount() - 1), or NULL if the index was out of
* bounds.
*
* blockCipherFind returns the blockcipher with the given name, or
* NULL if no hash function exists with that name.
*/
#ifdef __cplusplus
extern "C" {
#endif
BEEDLLAPI
int blockCipherCount();
BEEDLLAPI
const blockCipher* blockCipherGet(int);
BEEDLLAPI
const blockCipher* blockCipherFind(const char*);
BEEDLLAPI
const blockCipher* blockCipherDefault();
#ifdef __cplusplus
}
#endif
/*
* The struct 'blockCipherContext' is used to contain both the functional
* part (the blockCipher), and its parameters.
*/
typedef struct
{
const blockCipher* ciph;
blockCipherParam* param;
} blockCipherContext;
/*
* The following functions can be used to initialize and free a
* blockCipherContext. Initializing will allocate a buffer of the size
* required by the blockCipher, freeing will deallocate that buffer.
*/
#ifdef __cplusplus
extern "C" {
#endif
BEEDLLAPI
void blockCipherContextInit(blockCipherContext*, const blockCipher*);
BEEDLLAPI
void blockCipherContextFree(blockCipherContext*);
#ifdef __cplusplus
}
#endif
#endif
|