summaryrefslogtreecommitdiff
path: root/beecrypt/beecrypt.h
blob: 412cc5bf4924cfe59c1063b6366458b448b6b741 (plain)
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
/*
 * beecrypt.h
 *
 * BeeCrypt library hooks & stubs, header
 *
 * Copyright (c) 1999, 2000, 2001 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

#include "types.h"

#include "memchunk.h"
#include "mp32number.h"

/** \name Entropy sources */
/*@{*/

/** \ingroup ES_m
 * Return an array of 32-bit unsigned integers of given size with
 * entropy data.
 *
 * @retval data		entropy data
 * @param size		no. of ints of data
 * @return		0 on success, -1 on failure
 */
typedef int (*entropyNext) (/*@out@*/ uint32* data, int size)
	/*@modifies data @*/;

/** \ingroup ES_m
 * Methods and parameters for entropy sources.
 * Each specific entropy source MUST be written to be multithread-safe.
 */
typedef struct
{
/*@observer@*/
    const char* name;		/*!< entropy source name */
/*@unused@*/
    const entropyNext next;	/*!< return entropy function */
} entropySource;

#ifdef __cplusplus
extern "C" {
#endif

/** \ingroup ES_m
 * Return the number of entropy sources available.
 * @return		number of entropy sources available
 */
BEECRYPTAPI /*@unused@*/
int entropySourceCount(void)
	/*@*/;

/** \ingroup ES_m
 * Retrieve a entropy source by index.
 * @param index		entropy source index
 * @return		entropy source pointer (or NULL)
 */
BEECRYPTAPI /*@observer@*/ /*@null@*/ /*@unused@*/
const entropySource* entropySourceGet(int index)
	/*@*/;

/** \ingroup ES_m
 * Retrieve a entropy source by name.
 * @param name		entropy source name
 * @return		entropy source pointer (or NULL)
 */
/*@-exportlocal@*/
BEECRYPTAPI /*@observer@*/ /*@null@*/
const entropySource* entropySourceFind(const char* name)
	/*@*/;
/*@=exportlocal@*/

/** \ingroup ES_m
 * Retrieve the default entropy source.
 * If the BEECRYPT_ENTROPY environment variable is set, use that
 * entropy source. Otherwise, use the 1st entry in the internal table.
 * @return		entropy source pointer (or NULL)
 */
BEECRYPTAPI /*@observer@*/ /*@null@*/ /*@unused@*/
const entropySource* entropySourceDefault(void)
	/*@*/;

/** \ingroup ES_m
 * Gather entropy from multiple sources (if BEECRYPT_ENTROPY is not set).
 *
 * @retval data		entropy data
 * @param size		no. of ints of data
 * @return		0 on success, -1 on failure
 */
BEECRYPTAPI
int entropyGatherNext(uint32* data, int size)
	/*@*/;

#ifdef __cplusplus
}
#endif

/*@}*/
/** \name Pseudo-random Number Generators */
/*@{*/

/** \ingroup PRNG_m
 */
typedef void randomGeneratorParam;

/** \ingroup PRNG_m
 * Initialize the parameters for use, and seed the generator
 * with entropy from the default entropy source.
 *
 * @param param		generator parameters
 * @return		0 on success, -1 on failure
 */
typedef int (*randomGeneratorSetup) (randomGeneratorParam* param)
	/*@modifies *param @*/;

/** \ingroup PRNG_m
 * Re-seed the random generator with user-provided entropy.
 *
 * @param param		generator parameters
 * @param data		user entropy
 * @param size		no. of ints of entropy
 * @return		0 on success, -1 on failure
 */
typedef int (*randomGeneratorSeed) (randomGeneratorParam* param, const uint32* data, int size)
	/*@modifies *param @*/;

/** \ingroup PRNG_m
 * Return an array of 32-bit unsigned integers of given size with
 * pseudo-random data.
 *
 * @param param		generator parameters
 * @retval data		pseudo-random data
 * @param size		no. of ints of data
 * @return		0 on success, -1 on failure
 */
typedef int (*randomGeneratorNext) (randomGeneratorParam* param, /*@out@*/ uint32* data, int size)
	/*@modifies *param, *data @*/;

/** \ingroup PRNG_m
 * Cleanup after using a generator.
 *
 * @param param		generator parameters
 * @return		0 on success, -1 on failure
 */
typedef int (*randomGeneratorCleanup) (randomGeneratorParam* param)
	/*@modifies *param @*/;

/** \ingroup PRNG_m
 * Methods and parameters for random generators.
 * 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.
 *
 */
typedef struct
{
/*@observer@*/
    const char* name;			/*!< random generator name */
    const unsigned int paramsize;
    const randomGeneratorSetup setup;
    const randomGeneratorSeed seed;
    const randomGeneratorNext next;
    const randomGeneratorCleanup cleanup;
} randomGenerator;

#ifdef __cplusplus
extern "C" {
#endif

/** \ingroup PRNG_m
 * Return the number of generators available.
 * @return		number of generators available
 */
BEECRYPTAPI /*@unused@*/
int randomGeneratorCount(void)
	/*@*/;

/** \ingroup PRNG_m
 * Retrieve a generator by index.
 * @param index		generator index
 * @return		generator pointer (or NULL)
 */
BEECRYPTAPI /*@observer@*/ /*@null@*/ /*@unused@*/
const randomGenerator* randomGeneratorGet(int index)
	/*@*/;

/** \ingroup PRNG_m
 * Retrieve a generator by name.
 * @param name		generator name
 * @return		generator pointer (or NULL)
 */
/*@-exportlocal@*/
BEECRYPTAPI /*@observer@*/ /*@null@*/
const randomGenerator* randomGeneratorFind(const char* name)
	/*@*/;
/*@=exportlocal@*/

/** \ingroup PRNG_m
 * Retrieve the default generator.
 * If the BEECRYPT_RANDOM environment variable is set, use that
 * generator. Otherwise, use "fips186prng".
 * @return		generator pointer
 */
BEECRYPTAPI /*@observer@*/ /*@null@*/ /*@unused@*/
const randomGenerator* randomGeneratorDefault(void)
	/*@*/;

#ifdef __cplusplus
}
#endif

/** \ingroup PRNG_m
 * A randomGenerator instance, global functions and specific parameters.
 */
typedef struct
{
/*@observer@*/ /*@dependent@*/
    const randomGenerator* rng;		/*!< global functions and parameters */
/*@only@*/
    randomGeneratorParam* param;	/*!< specific parameters */
} randomGeneratorContext;

#ifdef __cplusplus
extern "C" {
#endif

/** \ingroup PRNG_m
 * Initialize a randomGenerator instance.
 */
BEECRYPTAPI /*@unused@*/
int randomGeneratorContextInit(randomGeneratorContext* ctxt, /*@observer@*/ /*@dependent@*/ const randomGenerator* rng)
	/*@modifies ctxt->rng, ctxt->param @*/;

/** \ingroup PRNG_m
 * Destroy a randomGenerator instance.
 */
BEECRYPTAPI /*@unused@*/
int randomGeneratorContextFree(/*@special@*/ randomGeneratorContext* ctxt)
	/*@uses ctxt->rng @*/
	/*@releases ctxt->param @*/
	/*@modifies ctxt->rng, ctxt->param @*/;

BEECRYPTAPI /*@unused@*/
int randomGeneratorContextNext(randomGeneratorContext* ctxt, /*@out@*/ uint32* data, int size)
	/*@modifies ctxt->param, *data @*/;

#ifdef __cplusplus
}
#endif

/*@}*/
/** \name Hash Functions */
/*@{*/

/** \ingroup HASH_m
 */
BEECRYPTAPI
typedef void hashFunctionParam;

/** \ingroup HASH_m
 * Re-initialize the parameters of the hash function.
 *
 * @param param		hash parameters
 * @return		0 on success, -1 on failure
 */
typedef int (*hashFunctionReset) (hashFunctionParam* param)
	/*@modifies *param @*/;

/** \ingroup HASH_m
 * Update the hash function with an array of bytes.
 *
 * @param param		hash parameters
 * @param data		array of bytes
 * @param size		no. of bytes
 * @return		0 on success, -1 on failure
 */
typedef int (*hashFunctionUpdate) (hashFunctionParam* param, const byte* data, int size)
	/*@modifies *param @*/;

/** \ingroup HASH_m
 * Compute the digest of all the data passed to the hash function, and return
 * the result in data.
 *
 * @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.
 *
 * @param param		hash parameters
 * @retval data		digest
 * @return		0 on success, -1 on failure
 */
typedef int (*hashFunctionDigest) (hashFunctionParam* param, /*@out@*/ uint32* data)
	/*@modifies *param, *data @*/;

/** \ingroup HASH_m
 * Methods and parameters for hash functions.
 * Specific hash functions MAY be written to be multithread-safe.
 */
typedef struct
{
/*@observer@*/
    const char* name;			/*!< hash function name */
    const unsigned int paramsize;	/*!< in bytes */
    const unsigned int blocksize;	/*!< in bytes */
    const unsigned int digestsize;	/*!< in bytes */
    const hashFunctionReset reset;
    const hashFunctionUpdate update;
    const hashFunctionDigest digest;
} hashFunction;

#ifdef __cplusplus
extern "C" {
#endif

/** \ingroup HASH_m
 * Return the number of hash functions available.
 * @return		number of hash functions available
 */
BEECRYPTAPI /*@unused@*/
int hashFunctionCount(void)
	/*@*/;

/** \ingroup HASH_m
 * Retrieve a hash function by index.
 * @param index		hash function index
 * @return		hash function pointer (or NULL)
 */
BEECRYPTAPI /*@observer@*/ /*@null@*/ /*@unused@*/
const hashFunction* hashFunctionGet(int index)
	/*@*/;

/** \ingroup HASH_m
 * Retrieve a hash function by name.
 * @param name		hash function name
 * @return		hash function pointer (or NULL)
 */
/*@-exportlocal@*/
BEECRYPTAPI /*@observer@*/ /*@null@*/
const hashFunction* hashFunctionFind(const char* name)
	/*@*/;
/*@=exportlocal@*/

/** \ingroup HASH_m
 * Retrieve the default hash function.
 * If the BEECRYPT_HASH environment variable is set, use that
 * hash function. Otherwise, use "sha1".
 * @return		hash function pointer (or NULL)
 */
BEECRYPTAPI /*@observer@*/ /*@null@*/ /*@unused@*/
const hashFunction* hashFunctionDefault(void)
	/*@*/;

#ifdef __cplusplus
}
#endif

/** \ingroup HASH_m
 * A hashFunction instance, global functions and specific parameters.
 */
typedef struct
{
/*@observer@*/ /*@dependent@*/
    const hashFunction* algo;	/*!< global functions and parameters */
/*@only@*/
    hashFunctionParam* param;	/*!< specific parameters */
} hashFunctionContext;

#ifdef __cplusplus
extern "C" {
#endif

/** \ingroup HASH_m
 * Initialize a hashFunction instance.
 */
BEECRYPTAPI
int hashFunctionContextInit(hashFunctionContext* ctxt, /*@observer@*/ /*@dependent@*/ const hashFunction* hash)
	/*@modifies ctxt->algo, ctxt->param */;

/** \ingroup HASH_m
 * Destroy a hashFunction instance.
 */
BEECRYPTAPI
int hashFunctionContextFree(/*@special@*/ hashFunctionContext* ctxt)
	/*@releases ctxt->param @*/
	/*@modifies ctxt->algo, ctxt->param */;

/** \ingroup HASH_m
 */
BEECRYPTAPI
int hashFunctionContextReset(hashFunctionContext* ctxt)
	/*@modifies ctxt */;

/** \ingroup HASH_m
 */
BEECRYPTAPI /*@unused@*/
int hashFunctionContextUpdate(hashFunctionContext* ctxt, const byte* data, int size)
	/*@modifies ctxt */;

/** \ingroup HASH_m
 */
BEECRYPTAPI /*@unused@*/
int hashFunctionContextUpdateMC(hashFunctionContext* ctxt, const memchunk* m)
	/*@modifies ctxt */;

/** \ingroup HASH_m
 */
BEECRYPTAPI
int hashFunctionContextUpdateMP32(hashFunctionContext* ctxt, const mp32number* n)
	/*@modifies ctxt */;

/** \ingroup HASH_m
 */
BEECRYPTAPI
int hashFunctionContextDigest(hashFunctionContext* ctxt, mp32number* dig)
	/*@modifies ctxt, *dig */;

/** \ingroup HASH_m
 */
BEECRYPTAPI /*@unused@*/
int hashFunctionContextDigestMatch(hashFunctionContext* ctxt, const mp32number* match)
	/*@modifies ctxt */;

#ifdef __cplusplus
}
#endif

/*@}*/
/** \name Keyed Hash Functions, a.k.a. Message Authentication Codes */
/*@{*/

/** \ingroup HMAC_m
 */
typedef void keyedHashFunctionParam;

/** \ingroup HMAC_m
 * Setup the keyed hash function parameters with the given secret key.
 * This can also be used to reset the parameters.
 *
 * @note After use, it is recommended to wipe the parameters by calling setup
 * again with another (dummy) key.
 *
 * @param param		keyed hash parameters
 * @param key		secret key
 * @param keybits	no. bits in secret key
 * @return		0 on success, -1 on failure
 */
typedef int (*keyedHashFunctionSetup) (keyedHashFunctionParam* param, const uint32* key, int keybits)
	/*@modifies *param @*/;

/** \ingroup HMAC_m
 * Re-initialize the parameters of a keyed hash function.
 *
 * @param param		keyed hash parameters
 * @return		0 on success, -1 on failure
 */
typedef int (*keyedHashFunctionReset) (keyedHashFunctionParam* param)
	/*@modifies *param @*/;

/** \ingroup HMAC_m
 * Update the keyed hash function with an array of bytes.
 *
 * @param param		keyed hash parameters
 * @param data		array of bytes
 * @param size		no. of bytes
 * @return		0 on success, -1 on failure
 */
typedef int (*keyedHashFunctionUpdate) (keyedHashFunctionParam* param, const byte* data, int size)
	/*@modifies *param @*/;

/** \ingroup HMAC_m
 * Compute the digest (or authentication code) of all the data passed to
 * the keyed hash function, and return the result in data.
 *
 * @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.
 *
 * @param param		keyed hash parameters
 * @retval data		digest (or authentication code)
 * @return		0 on success, -1 on failure
 */
typedef int (*keyedHashFunctionDigest) (keyedHashFunctionParam* param, /*@out@*/ uint32* data)
	/*@modifies *param, *data @*/;

/** \ingroup HMAC_m
 * Methods and parameters for keyed hash functions.
 * Specific keyed hash functions MAY be written to be multithread-safe.
 */
typedef struct
{
/*@observer@*/
    const char* name;			/*!< keyed hash function name */
    const unsigned int paramsize;	/*!< in bytes */
    const unsigned int blocksize;	/*!< in bytes */
    const unsigned int digestsize;	/*!< in bytes */
    const unsigned int keybitsmin;	/*!< min keysize in bits */
    const unsigned int keybitsmax;	/*!< max keysize in bits */
    const unsigned int keybitsinc;	/*!< keysize increment in bits */
    const keyedHashFunctionSetup setup;
    const keyedHashFunctionReset reset;
    const keyedHashFunctionUpdate update;
    const keyedHashFunctionDigest digest;
} keyedHashFunction;

#ifdef __cplusplus
extern "C" {
#endif

/** \ingroup HMAC_m
 * Return the number of keyed hash functions available.
 * @return		number of keyed hash functions available
 */
BEECRYPTAPI /*@unused@*/
int keyedHashFunctionCount(void)
	/*@*/;

/** \ingroup HMAC_m
 * Retrieve a keyed hash function by index.
 * @param index		keyed hash function index
 * @return		keyed hash function pointer (or NULL)
 */
BEECRYPTAPI /*@observer@*/ /*@null@*/ /*@unused@*/
const keyedHashFunction* keyedHashFunctionGet(int index)
	/*@*/;

/** \ingroup HMAC_m
 * Retrieve a keyed hash function by name.
 * @param name		keyed hash function name
 * @return		keyed hash function pointer (or NULL)
 */
/*@-exportlocal@*/
BEECRYPTAPI /*@observer@*/ /*@null@*/
const keyedHashFunction* keyedHashFunctionFind(const char* name)
	/*@*/;
/*@=exportlocal@*/

/** \ingroup HMAC_m
 * Retrieve the default keyed hash function.
 * If the BEECRYPT_KEYEDHASH environment variable is set, use that keyed
 * hash function. Otherwise, use "hmacsha1".
 * @return		keyed hash function pointer
 */
BEECRYPTAPI /*@observer@*/ /*@null@*/ /*@unused@*/
const keyedHashFunction* keyedHashFunctionDefault(void)
	/*@*/;

#ifdef __cplusplus
}
#endif

/** \ingroup HMAC_m
 * A keyedHashFunction instance, global functions and specific parameters.
 */
typedef struct
{
/*@observer@*/ /*@dependent@*/
    const keyedHashFunction* algo;	/*!< global functions and parameters */
/*@only@*/
    keyedHashFunctionParam* param;	/*!< specific parameters */
} keyedHashFunctionContext;

#ifdef __cplusplus
extern "C" {
#endif

/** \ingroup HMAC_m
 * Initialize a keyedHashFunction instance.
 */
BEECRYPTAPI
int keyedHashFunctionContextInit(keyedHashFunctionContext* ctxt, /*@observer@*/ /*@dependent@*/ const keyedHashFunction* mac)
	/*@modifies ctxt->algo, ctxt->param @*/;

/** \ingroup HMAC_m
 * Destroy a keyedHashFunction instance.
 */
BEECRYPTAPI
int keyedHashFunctionContextFree(/*@special@*/ keyedHashFunctionContext* ctxt)
	/*@uses ctxt->algo @*/
	/*@releases ctxt->param @*/
	/*@modifies ctxt->algo, ctxt->param @*/;

/** \ingroup HMAC_m
 */
BEECRYPTAPI
int keyedHashFunctionContextSetup(keyedHashFunctionContext* ctxt, const uint32* key, int keybits)
	/*@modifies ctxt @*/;

/** \ingroup HMAC_m
 */
BEECRYPTAPI /*@unused@*/
int keyedHashFunctionContextReset(keyedHashFunctionContext* ctxt)
	/*@modifies ctxt @*/;

/** \ingroup HMAC_m
 */
BEECRYPTAPI /*@unused@*/
int keyedHashFunctionContextUpdate(keyedHashFunctionContext* ctxt, const byte* data, int size)
	/*@modifies ctxt @*/;

/** \ingroup HMAC_m
 */
BEECRYPTAPI
int keyedHashFunctionContextUpdateMC(keyedHashFunctionContext* ctxt, const memchunk* m)
	/*@modifies ctxt @*/;

/** \ingroup HMAC_m
 */
BEECRYPTAPI /*@unused@*/
int keyedHashFunctionContextUpdateMP32(keyedHashFunctionContext* ctxt, const mp32number* n)
	/*@modifies ctxt @*/;

/** \ingroup HMAC_m
 */
BEECRYPTAPI
int keyedHashFunctionContextDigest(keyedHashFunctionContext* ctxt, mp32number* dig)
	/*@modifies ctxt, *dig @*/;

/** \ingroup HMAC_m
 */
BEECRYPTAPI
int keyedHashFunctionContextDigestMatch(keyedHashFunctionContext* ctxt, const mp32number* match)
	/*@modifies ctxt @*/;

#ifdef __cplusplus
}
#endif

/*@}*/
/** \name Block ciphers */
/*@{*/

/** \ingroup BC_m
 */
typedef void blockCipherParam;

/** \ingroup BC_m
 * Block cipher operations.
 */
typedef enum
{
	ENCRYPT,
	DECRYPT
} cipherOperation;

/** \ingroup BC_m
 * Block cipher modes.
 */
/*@-enummemuse@*/
typedef enum
{
	ECB,
	CBC
} cipherMode;
/*@=enummemuse@*/

/** \ingroup BC_m
 * @param param		blockcipher parameters
 * @param size		no. of ints
 * @retval dst		ciphertext block
 * @param src		plaintext block
 * @return		0 on success, -1 on failure
 */
typedef int (*blockModeEncrypt) (blockCipherParam* param, int count, uint32* dst, const uint32* src)
	/*@modifies *param, *dst @*/;

/** \ingroup BC_m
 * @param param		blockcipher parameters
 * @param size		no. of ints
 * @retval dst		plainttext block
 * @param src		ciphertext block
 * @return		0 on success, -1 on failure
 */
typedef int (*blockModeDecrypt) (blockCipherParam* param, int count, uint32* dst, const uint32* src)
	/*@modifies *param, *dst @*/;

/** \ingroup BC_m
 */
typedef struct
{
	const blockModeEncrypt	encrypt;
	const blockModeDecrypt	decrypt;
} blockMode;

/** \ingroup BC_m
 * Setup the blockcipher parameters with the given secret key for either
 * encryption or decryption.
 *
 * @note After use, it is recommended to wipe the parameters by calling setup
 * again with another (dummy) key.
 *
 * @param param		blockcipher parameters
 * @param key		secret key
 * @param keybits	no. bits in secret key
 * @param cipherOperation
 * @return		0 on success, -1 on failure
 */
typedef int (*blockCipherSetup) (blockCipherParam* param, const uint32* key, int keybits, cipherOperation cipherOperation)
	/*@modifies param @*/;

/** \ingroup BC_m
 * Initialize IV for blockcipher.
 * @param param		blockcipher parameters
 * @param data		iv data
 * @return		0 on success, -1 on failure
 */
typedef int (*blockCipherSetIV) (blockCipherParam* param, const uint32* data)
	/*@modifies param @*/;

/** \ingroup BC_m
 * Encrypt one block of data (with bit size chosen by the blockcipher).
 * @note This is raw encryption, without padding, etc.
 *
 * @param param		blockcipher parameters
 * @retval dst		ciphertext block
 * @param src		plaintext block
 * @return		0 on success, -1 on failure
 */
typedef int (*blockCipherEncrypt) (blockCipherParam* param, uint32* dst, const uint32* src)
	/*@modifies param, dst @*/;

/** \ingroup BC_m
 * Decrypt one block of data (with bit size chosen by the blockcipher).
 * @note This is raw decryption, without padding, etc.
 *
 * @param param		blockcipher parameters
 * @retval dst		plaintext block
 * @param src		ciphertext block
 * @return		0 on success, -1 on failure
 */
typedef int (*blockCipherDecrypt) (blockCipherParam* param, uint32* dst, const uint32* src)
	/*@modifies param, dst @*/;

/** \ingroup BC_m
 * Methods and parameters for block ciphers.
 * Specific block ciphers MAY be written to be multithread-safe.
 */
typedef struct
{
/*@observer@*/
    const char* name;			/*!< block cipher name */
    const unsigned int paramsize;	/*!< in bytes */
    const unsigned int blocksize;	/*!< in bytes */
    const unsigned int keybitsmin;	/*!< min keysize in bits */
    const unsigned int keybitsmax;	/*!< max keysize in bits */
    const unsigned int keybitsinc;	/*!< keysize increment in bits */
    const blockCipherSetup setup;
    const blockCipherSetIV setiv;
    const blockCipherEncrypt encrypt;
    const blockCipherDecrypt decrypt;
/*@dependent@*/
    const blockMode* mode;
} blockCipher;

#ifdef __cplusplus
extern "C" {
#endif

/** \ingroup BC_m
 * Return the number of blockciphers available.
 * @return		number of blockciphers available
 */
BEECRYPTAPI /*@unused@*/
int blockCipherCount(void)
	/*@*/;

/** \ingroup BC_m
 * Retrieve a blockcipher by index.
 * @param index		blockcipher index
 * @return		blockcipher pointer (or NULL)
 */
BEECRYPTAPI /*@observer@*/ /*@null@*/ /*@unused@*/
const blockCipher* blockCipherGet(int index)
	/*@*/;

/** \ingroup BC_m
 * Retrieve a blockcipher by name.
 * @param name		blockcipher name
 * @return		blockcipher pointer (or NULL)
 */
/*@-exportlocal@*/
BEECRYPTAPI /*@observer@*/ /*@null@*/
const blockCipher* blockCipherFind(const char* name)
	/*@*/;
/*@=exportlocal@*/

/** \ingroup BC_m
 * Retrieve the default blockcipher.
 * If the BEECRYPT_CIPHER environment variable is set, use that blockcipher.
 * Otherwise, use "blowfish".
 * @return		blockcipher pointer
 */
BEECRYPTAPI /*@observer@*/ /*@null@*/ /*@unused@*/
const blockCipher* blockCipherDefault(void)
	/*@*/;

#ifdef __cplusplus
}
#endif

/** \ingroup BC_m
 * A blockCipher instance, global functions and specific parameters.
 */
typedef struct
{
/*@observer@*/ /*@dependent@*/
    const blockCipher* algo;	/*!< global functions and parameters */
/*@only@*/
    blockCipherParam* param;	/*!< specific parameters */
} blockCipherContext;

#ifdef __cplusplus
extern "C" {
#endif

/** \ingroup BC_m
 * Initialize a blockCipher instance.
 */
BEECRYPTAPI
int blockCipherContextInit(blockCipherContext* ctxt, /*@observer@*/ /*@dependent@*/ const blockCipher* ciph)
	/*@modifies ctxt->algo, ctxt->param @*/;

/** \ingroup BC_m
 */
BEECRYPTAPI
int blockCipherContextSetup(blockCipherContext* ctxt, const uint32* key, int keybits, cipherOperation op)
	/*@modifies ctxt @*/;

/** \ingroup BC_m
 */
BEECRYPTAPI /*@unused@*/
int blockCipherContextSetIV(blockCipherContext* ctxt, const uint32* iv)
	/*@modifies ctxt @*/;

/** \ingroup BC_m
 * Destroy a blockCipher instance.
 */
BEECRYPTAPI
int blockCipherContextFree(/*@special@*/ blockCipherContext* ctxt)
	/*@releases ctxt->param @*/
	/*@modifies ctxt->algo, ctxt->param @*/;

#ifdef __cplusplus
}
#endif
/*@}*/

#endif