libfortuna  1
FortunalibraryfunctionsextractedfromPostgreSQLsource
 All Data Structures Files Functions Variables Typedefs Macros
internal.h
Go to the documentation of this file.
1 /*
2  * internal.c
3  * Wrapper for builtin functions
4  *
5  * Copyright (c) 2001 Marko Kreen
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * contrib/pgcrypto/internal.c
30  */
31 
32 #ifndef __INTERNAL_H
33 #define __INTERNAL_H
34 
35 #include <stdlib.h>
36 #include <malloc_np.h>
37 #include <time.h>
38 
39 #include "px.h"
40 #include "md5.h"
41 #include "sha1.h"
42 #include "blf.h"
43 #include "rijndael.h"
44 #include "fortuna.h"
45 
46 /*
47  * System reseeds should be separated at least this much.
48  */
49 #define SYSTEM_RESEED_MIN (20*60) /* 20 min */
50 /*
51  * How often to roll dice.
52  */
53 #define SYSTEM_RESEED_CHECK_TIME (10*60) /* 10 min */
54 /*
55  * The chance is x/256 that the reseed happens.
56  */
57 #define SYSTEM_RESEED_CHANCE (4) /* 256/4 * 10min ~ 10h */
58 
59 /*
60  * If this much time has passed, force reseed.
61  */
62 #define SYSTEM_RESEED_MAX (12*60*60) /* 12h */
63 
64 
65 #ifndef MD5_DIGEST_LENGTH
66 #define MD5_DIGEST_LENGTH 16
67 #endif
68 
69 #ifndef SHA1_DIGEST_LENGTH
70 #ifdef SHA1_RESULTLEN
71 #define SHA1_DIGEST_LENGTH SHA1_RESULTLEN
72 #else
73 #define SHA1_DIGEST_LENGTH 20
74 #endif
75 #endif
76 
77 #define SHA1_BLOCK_SIZE 64
78 #define MD5_BLOCK_SIZE 64
79 
80 void init_md5(PX_MD *h);
81 void init_sha1(PX_MD *h);
82 
83 void init_sha224(PX_MD *h);
84 void init_sha256(PX_MD *h);
85 void init_sha384(PX_MD *h);
86 void init_sha512(PX_MD *h);
87 
88 
89 /* MD5 */
90 
91 unsigned int_md5_len(PX_MD *h);
92 unsigned int_md5_block_len(PX_MD *h);
93 void int_md5_update(PX_MD *h, const uint8 *data, unsigned dlen);
94 void int_md5_reset(PX_MD *h);
95 void int_md5_finish(PX_MD *h, uint8 *dst);
96 void int_md5_free(PX_MD *h);
97 
98 unsigned int_sha1_len(PX_MD *h);
99 unsigned int_sha1_block_len(PX_MD *h);
100 void int_sha1_update(PX_MD *h, const uint8 *data, unsigned dlen);
101 void int_sha1_reset(PX_MD *h);
102 void int_sha1_finish(PX_MD *h, uint8 *dst);
103 void int_sha1_free(PX_MD *h);
104 void init_md5(PX_MD *md);
105 void init_sha1(PX_MD *md);
106 
107 #define INT_MAX_KEY (512/8)
108 #define INT_MAX_IV (128/8)
109 
110 struct int_ctx
111 {
114  union
115  {
118  } ctx;
119  unsigned keylen;
120  int is_init;
121  int mode;
122 };
123 
124 void intctx_free(PX_Cipher *c);
125 
126 #define MODE_ECB 0
127 #define MODE_CBC 1
128 
129 unsigned rj_block_size(PX_Cipher *c);
130 unsigned rj_key_size(PX_Cipher *c);
131 unsigned rj_iv_size(PX_Cipher *c);
132 int rj_init(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv);
133 int rj_real_init(struct int_ctx * cx, int dir);
134 int rj_encrypt(PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res);
135 int rj_decrypt(PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res);
136 PX_Cipher *rj_load(int mode);
137 unsigned bf_block_size(PX_Cipher *c);
138 unsigned bf_key_size(PX_Cipher *c);
139 unsigned bf_iv_size(PX_Cipher *c);
140 int bf_init(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv);
141 int bf_encrypt(PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res);
142 int bf_decrypt(PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res);
143 PX_Cipher *bf_load(int mode);
144 PX_Cipher *rj_128_ecb(void);
145 PX_Cipher *rj_128_cbc(void);
146 PX_Cipher *bf_ecb_load(void);
147 PX_Cipher *bf_cbc_load(void);
148 
149 int px_find_digest(const char *name, PX_MD **res);
150 
151 int px_find_cipher(const char *name, PX_Cipher **res);
152 
153 int px_get_pseudo_random_bytes(uint8 *dst, unsigned count);
154 
155 void system_reseed(void);
156 
157 int px_get_random_bytes(uint8 *dst, unsigned count);
158 
159 int px_add_entropy(const uint8 *data, unsigned count);
160 
161 unsigned int_sha224_len(PX_MD *h);
162 unsigned int_sha224_block_len(PX_MD *h);
163 void int_sha224_update(PX_MD *h, const uint8 *data, unsigned dlen);
164 void int_sha224_reset(PX_MD *h);
165 void int_sha224_finish(PX_MD *h, uint8 *dst);
166 void int_sha224_free(PX_MD *h);
167 unsigned int_sha256_len(PX_MD *h);
168 unsigned int_sha256_block_len(PX_MD *h);
169 void int_sha256_update(PX_MD *h, const uint8 *data, unsigned dlen);
170 void int_sha256_reset(PX_MD *h);
171 void int_sha256_finish(PX_MD *h, uint8 *dst);
172 void int_sha256_free(PX_MD *h);
173 unsigned int_sha384_len(PX_MD *h);
174 unsigned int_sha384_block_len(PX_MD *h);
175 void int_sha384_update(PX_MD *h, const uint8 *data, unsigned dlen);
176 void int_sha384_reset(PX_MD *h);
177 void int_sha384_finish(PX_MD *h, uint8 *dst);
178 void int_sha384_free(PX_MD *h);
179 unsigned int_sha512_len(PX_MD *h);
180 unsigned int_sha512_block_len(PX_MD *h);
181 void int_sha512_update(PX_MD *h, const uint8 *data, unsigned dlen);
182 void int_sha512_reset(PX_MD *h);
183 void int_sha512_finish(PX_MD *h, uint8 *dst);
184 void int_sha512_free(PX_MD *h);
185 void init_sha224(PX_MD *md);
186 void init_sha256(PX_MD *md);
187 void init_sha384(PX_MD *md);
188 void init_sha512(PX_MD *md);
189 
190 #endif