author | zautrix <zautrix> | 2004-10-19 20:16:14 (UTC) |
---|---|---|
committer | zautrix <zautrix> | 2004-10-19 20:16:14 (UTC) |
commit | eca49bb06a71980ef61d078904573f25890fc7f2 (patch) (unidiff) | |
tree | c5338e3b12430248979a9ac2c1c7e6646ea9ecdf /pwmanager/libcrypt/crypt | |
parent | 53cc32b6e7b1f672bf91b2baf2df6c1e8baf3e0a (diff) | |
download | kdepimpi-eca49bb06a71980ef61d078904573f25890fc7f2.zip kdepimpi-eca49bb06a71980ef61d078904573f25890fc7f2.tar.gz kdepimpi-eca49bb06a71980ef61d078904573f25890fc7f2.tar.bz2 |
Initial revision
-rw-r--r-- | pwmanager/libcrypt/crypt/Manifest | 58 | ||||
-rw-r--r-- | pwmanager/libcrypt/crypt/ath.c | 287 | ||||
-rw-r--r-- | pwmanager/libcrypt/crypt/ath.h | 116 | ||||
-rw-r--r-- | pwmanager/libcrypt/crypt/cipher.h | 80 | ||||
-rw-r--r-- | pwmanager/libcrypt/crypt/g10lib.h | 250 | ||||
-rw-r--r-- | pwmanager/libcrypt/crypt/gcrypt-module.h | 225 | ||||
-rw-r--r-- | pwmanager/libcrypt/crypt/gcrypt.h | 1497 | ||||
-rw-r--r-- | pwmanager/libcrypt/crypt/global.c | 672 | ||||
-rw-r--r-- | pwmanager/libcrypt/crypt/misc.c | 241 | ||||
-rw-r--r-- | pwmanager/libcrypt/crypt/missing-string.c | 151 | ||||
-rw-r--r-- | pwmanager/libcrypt/crypt/module.c | 200 | ||||
-rw-r--r-- | pwmanager/libcrypt/crypt/mpi.h | 199 | ||||
-rw-r--r-- | pwmanager/libcrypt/crypt/secmem.c | 653 | ||||
-rw-r--r-- | pwmanager/libcrypt/crypt/secmem.h | 38 | ||||
-rw-r--r-- | pwmanager/libcrypt/crypt/sexp.c | 1804 | ||||
-rw-r--r-- | pwmanager/libcrypt/crypt/stdmem.c | 195 | ||||
-rw-r--r-- | pwmanager/libcrypt/crypt/stdmem.h | 32 | ||||
-rw-r--r-- | pwmanager/libcrypt/crypt/types.h | 124 |
18 files changed, 6822 insertions, 0 deletions
diff --git a/pwmanager/libcrypt/crypt/Manifest b/pwmanager/libcrypt/crypt/Manifest new file mode 100644 index 0000000..2d003d8 --- a/dev/null +++ b/pwmanager/libcrypt/crypt/Manifest | |||
@@ -0,0 +1,58 @@ | |||
1 | # Manifest - checksums of the src directory | ||
2 | # Copyright 2004 Free Software Foundation, Inc. | ||
3 | # | ||
4 | # This file is part of Libgcrypt. | ||
5 | # | ||
6 | # Libgcrypt is free software; you can redistribute it and/or modify | ||
7 | # it under the terms of the GNU Lesser general Public License as | ||
8 | # published by the Free Software Foundation; either version 2.1 of | ||
9 | # the License, or (at your option) any later version. | ||
10 | # | ||
11 | # Libgcrypt is distributed in the hope that it will be useful, | ||
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | # GNU Lesser General Public License for more details. | ||
15 | # | ||
16 | # You should have received a copy of the GNU Lesser General Public | ||
17 | # License along with this program; if not, write to the Free Software | ||
18 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | ||
19 | |||
20 | # Checksums for all source files in this directory. Format is | ||
21 | # filename, blanks, base-64 part of an OpenPGP detached signature | ||
22 | # without the header lines. Blank lines and lines beginning with a | ||
23 | # hash mark are ignored. A tool to process this file is available by | ||
24 | # cvs -d :pserver:anoncvs@cvs.gnupg.org:/cvs/wk co misc-scripts/manifest-tool | ||
25 | # | ||
26 | # The special entry "$names$" holds a signature over all sorted | ||
27 | # filenames excluding itself. | ||
28 | |||
29 | gcrypt.h iQCVAwUAQH5RsTEAnp832S/7AQK7xgP+Kc3NY9lipZkaAMrnHDkQVLdHYwTbZWuGOYdTLp8Xy7Auh9wtWV9hrWVUqs+kxDzT/2iF6XkO3WT3rf/PmQ/Q0TIGfOyjE3c/qvB/jVippaxoGda3tnGpODytdI3XPhfPS0Ss8nDzfCStPBGAEq0OVU7imnExrFzhRXt+Gljr0o0==Yagz | ||
30 | gcrypt-module.h iQCVAwUAQH5UXzEAnp832S/7AQJMQgQAzumz9aaZelhw+FxTCeVadphBxt1bbNQvMrnddYYblyJv+AcxZ9ZxGz2oPeusN58Qg54DQcaW3lYhTgnWfXultsi+Ruxlz7400OUrzSXOl3At7KssdODAoscFzZIgh94G9lzQxEBr9lTXI9R3LsPFJP6muNG4frcNBAA42yckK7w==BBp5 | ||
31 | |||
32 | ath.c iQCVAwUAQH5E+DEAnp832S/7AQKFpgP+KSZHtVcnh9FFggIyHKbALUljW2FXauasZvFyN8Sk/mIMgKxyXFOG1THBAUzWLaKWIEWU+WkYU7uThqBtpnEImM5AenWzbQuJjftPC3gVHO8yjjmBWD4zmJj28htoKDoa/xDsoqumrHxae3FYcaCWtYGVjM/Pbl+OMRMOFAhp0ho==lQZ3 | ||
33 | ath.h iQCVAwUAQH5FODEAnp832S/7AQKiuQQAg4K+KOAn1LWBZN32MAhms4FeZKoce0fAuZW7BpyY4cCxIVgxqrtUC90CDykw8XegFfOyyYrgd0NmaMVdY7HZDncNOvIPxpgFQPCZrycsMOoAtoVwjK704RDeNo3zmeyxTKeDH+3M1J7JmLiafaEdSbOC8flX/W0icaV0Ol4dmBc==Ll6w | ||
34 | |||
35 | cipher.h iQCVAwUAQH5FUzEAnp832S/7AQJKLgP9GSSk9f7EINIRqSQH1XKX+dYzt3phDHdqFTUGIfYNh7YzGdy0drvgFhG4k15nqDouKRuFVM/hKY3ZVY7JccmKXKGAH6+ZYShoG6LMFfIGgDX8zne0dNxc72PLfns3fVxNn/RlHmHBkrQ+ppjR9HnSthFmOqzbQaW1BKmc3Z2x5GU==lIeW | ||
36 | g10lib.h iQCVAwUAQH5FejEAnp832S/7AQJ75wP/ZjOybwRix5eoXdfVeXPjoPygejzpYJJdMUGN3Y5UtkfBu9mPREsKfvZ6tH+Evjx+3xfeAb4bU/k2mRMp0tiWnk2koToS08vI9uxnioKQr9oulZH6r28S+NLSgMQuEGN1JNUky6RQ9TTNRndeTjKKSrEjZ7V6bv+rb8A1bYCKChs==P5mk | ||
37 | mpi.h iQCVAwUAQH5FwzEAnp832S/7AQJJ4wP9E3jVkcO9M0YtSBHIbjG3hDWKWXzi86AlUh51qiE8/2XP0FfjA4TosyvmicZs7j48HitAByr9tHOSxnbeo7NBf17ICwAo6Eqty+wKDg+eyLeEGUy7VpVK3RJRQAA4H+kl3S2l3YMTKf3WJlbc7qkWSXZspdy5c9sAxeodCKrAubU==oALf | ||
38 | |||
39 | global.c iQCVAwUAQH5HFzEAnp832S/7AQJc+QQAvi53ZkMCzLnVULHvhI6W+EX537zi9n8cplYguvIJqUhAZrP68yGAIyqyCONbZVDyB7wqeXdUMLzMk7W8fg+xuk5JSDpppAQf2m/bdQyze6XVqJso682eYBM8+b9z/IVEvLaFwhZcOKO1bcXudBlBCcJgVDpupfTtAWgPnewil9Q==Xwy1 | ||
40 | misc.c iQCVAwUAQH5IIjEAnp832S/7AQKNJAQAkEpyY3fCG7tvADJFAW9xA7DEQwLCa8YmiUhHvrEsWOI4YgvS7LUbWWc7VqK+ryORvXLKRAVieznbnHAuy0TKtqdnmA/kUmiurS0ah5SWqR/iuAeJtt0RGsmZaZ6oa2m4PZ2Y2GCHSTZqcclvwsetS9eq5AipxHxYFUltu5wGZNI==twM2 | ||
41 | missing-string.c iQCVAwUAQH5JfjEAnp832S/7AQI3ZQQAg55eEJbGQQHyBEJGxvt/FXpQiXcoDit3ZHzvdaQn/NUgdLjCHiWVzhyCXACGivLWMNModDaSaZk073NXxVkWfPcX9vkF//Wugwzidd5P3Bfu5k35o+Xxz82fsk5KuFGGq1mBUZ07xUYQ8KkKkhADUkr0QiQAuypp079Yq0uUC7Q==zvKn | ||
42 | module.c iQCVAwUAQH5JvjEAnp832S/7AQKlMgQAjZYTXMpWb5kHxCMXzRi069Ku/4/xnWsD+S0dje1LiKzCnRpwTTxARzc/y10Y8OcygkMuR4unEaWedO+9syjjty3fBCcue/j7YlLitq5EC9UE4o23poWvWCuX9Tadm2DK5qf4p7smMJ22O22cLTYTVCyAoYTQ2xC8ajzBsBRkX80==yRRD | ||
43 | secmem.c iQCVAwUAQH5LLDEAnp832S/7AQKtFwQAwY2wBr6WJC1cwqp/1DQoKzHx9C3plONxbZMazwR7VMI83NUbBAbv1mcxpeZWXmb2dRrnsR1VBbNPDSbJLN5T6czLQ2nIb6mnq9u8Ip4SAa+GCWfDV4AUtAJ4hN/yvWo8iEKu+KD5iJ6xJh31NdXjt5yk6vnk46SA6R4FkHdIEXc==UKVr | ||
44 | secmem.h iQCVAwUAQH5LTDEAnp832S/7AQIsJwQAkZUu4hvmh9NXCLNm98+tGZFzWYvZO/NffC2wdPE8Q/OTa/m3g+oBbEhaV1ze3oY4t1F/p7ZHFx5CsIp4zVjyPkxlni8AAVMUOQr/LopyxouHn2OjKO+dVqecWQf01+nPWjklbL2FZ3mQ99k2qeWZlVSkz0nm8u39F3v7z3OTCss==AJqE | ||
45 | sexp.c iQCVAwUAQH5LojEAnp832S/7AQKCTQQArlrj1KGwR2x93fcyN3M0iXuGkBq5R9KNu+1Bq04G4SLlpZ1RRY0OjV3L9To1BHTd01lXlO8MNz7NpRxWlG1Sw5FohbBlhWZQRcW8GdAawJPcfIY2Y8Ek6Yx8quZKbk9uD3bcBmStmg0P+TIA0nr20bmtfB3uX2KQVHQqWZQT5qU==P8FE | ||
46 | stdmem.c iQCVAwUAQH5LzjEAnp832S/7AQLOUAP9FU16itXBBrkfRDGmhUjAOeEEKdd+brQ3XdT8xoLvP/IH/6U1Kq3ampP2/xcL4kwVdz2rw6NRzP7jlL/yM3tW722lSS/JPJkH+2+qUkcb0fYNoql/WYPMYp1/Mzu6ttXnjag1cQGlKIyYAD+G6h3FtpLwQy0hEJopnF9+Ovd8U7A==CkiZ | ||
47 | stdmem.h iQCVAwUAQH5L8jEAnp832S/7AQIH0wP+Lyqh0tj++s2L79Tmf/gqgCK+HLMxTddcewF3XbsYf9T5FmLez1gz6Ggti4Ss9VjozOA3ti3trCiA/YNRmV9AYw4zLUPm+MsjJuveL/AgB9HdoD2v+RfJm0WwgSKiysp+8iyjg3Plopmhba4cGuOP5MJ3CWTqYwPmJVscUKC6g38==02MN | ||
48 | |||
49 | types.h iQCVAwUAQH5MKTEAnp832S/7AQLqTAP6A3mUMD5MMkBkebq4bRY6Bq0KsgdKfZ8TLhc2o87gFay8YD0Uom3YJNG2LF/rAIct2ih4jYJaIb5dRfJ0KJoPi2ETd462J8OFCL4fjq9TaSjB2pXcB+kWoxzPasGNg2Ukk0dQ6lvF1tSYrtt32PVI7q/UaPsjTylgRmzLfX/VxrU==OMu3 | ||
50 | |||
51 | |||
52 | # Configuration | ||
53 | Makefile.am iQCVAwUAQH5WVjEAnp832S/7AQLmsQP/bbI8/UWAC5yITVhGcCOCbN/FaMqXVKjxESzo6GTs02jxK1y3RuuaoNU1ssQZGAxpFiMJW8u933V3yTHFMxWpwHemDnEyv/a8YACxJBQ0tQgpgHS716BjMbHOfcuOis2WlCOOm0ErjhAYNa4NQ1q3jwkOvTDLFpdnqaWI2wWn08U==Yjun | ||
54 | libgcrypt.m4 iQCVAwUAQH5MbTEAnp832S/7AQJ1uAQA1C6xI7qXiKVtUeXawhPytAldosrzcXmqz34xi7JklQqw83d68WtWHFMBEUa7MKfi4WCbuQb7FjGUvMRw5z/T9ez7CoDekHc63+cIIZLQ23weUK8GaA1uQLoD0scmT41J5RkBlJbH7ck1zRd3d04o75rWNEUNit6KBvrQ4Pd8oQ8==uMgB | ||
55 | libgcrypt-config.in iQCVAwUAQH5UbzEAnp832S/7AQJISgP+Nbd2AQnDM/k8sQLbvz8YZjwX3LigZM+AkF1VAwyAm6YOU3nrXnz5t+cXkQD2dkz4L2F0AAsIkFiJsrgmZgCp2h1L6LeFnH+hoId9RhbYw4NkDaHb+MC9JcalpcfFvvxq6vM/W37bSFimM78P+5RLKypXCytVQNAAaIRgZjVfXY8==IGDS | ||
56 | libgcrypt.vers iQCVAwUAQH5MjTEAnp832S/7AQKCdQQAotG6Z3zdcePI0V33YY2sh91uYkLBNhQw+PzyE3BRRAVhMGLOBD1nSWJHJvE3eyCVOqFY0ZmvpVex51Fa0D/TwsJOO4RVxf1L9bbAncu9OuEXaGXKytLZp54TliDTAWGDq0lvtx1TvDDgtM8TbbaXvMbjfQ4wXBxdLvaenFCTlR4==kgHq | ||
57 | |||
58 | $names$ iQCVAwUAQH5UhDEAnp832S/7AQK/jwP9H7A3mI99M1NGuhD+16C+2gJIITB8GJeYeUd3vm8kWQ5n76WyMCdeA62qn0JUddIBjAbagtfvTL5aesnD9MlhEGaNlHauU7SINTIJ8njKf87EAAfDZrhS/tGDziC2nakMPweRxXQCLDWHkBPjYfrspSLLohjdegqBvTNyVM76+KE==3p9Z | ||
diff --git a/pwmanager/libcrypt/crypt/ath.c b/pwmanager/libcrypt/crypt/ath.c new file mode 100644 index 0000000..6788bba --- a/dev/null +++ b/pwmanager/libcrypt/crypt/ath.c | |||
@@ -0,0 +1,287 @@ | |||
1 | /* ath.c - Thread-safeness library. | ||
2 | Copyright (C) 2002, 2003, 2004 g10 Code GmbH | ||
3 | |||
4 | This file is part of Libgcrypt. | ||
5 | |||
6 | Libgcrypt is free software; you can redistribute it and/or modify | ||
7 | it under the terms of the GNU Lesser General Public License as | ||
8 | published by the Free Software Foundation; either version 2.1 of | ||
9 | the License, or (at your option) any later version. | ||
10 | |||
11 | Libgcrypt is distributed in the hope that it will be useful, but | ||
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
14 | General Public License for more details. | ||
15 | |||
16 | You should have received a copy of the GNU Lesser General Public | ||
17 | License along with Libgcrypt; if not, write to the Free Software | ||
18 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | ||
19 | 02111-1307, USA. */ | ||
20 | |||
21 | #ifdef HAVE_CONFIG_H | ||
22 | #include <config.h> | ||
23 | #endif | ||
24 | |||
25 | #include <assert.h> | ||
26 | #include <unistd.h> | ||
27 | #ifdef HAVE_SYS_SELECT_H | ||
28 | # include <sys/select.h> | ||
29 | #else | ||
30 | # include <sys/time.h> | ||
31 | #endif | ||
32 | #include <sys/types.h> | ||
33 | #include <sys/wait.h> | ||
34 | #include <errno.h> | ||
35 | |||
36 | #include "ath.h" | ||
37 | |||
38 | |||
39 | /* The interface table. */ | ||
40 | static struct ath_ops ops; | ||
41 | |||
42 | /* True if we should use the external callbacks. */ | ||
43 | static int ops_set; | ||
44 | |||
45 | |||
46 | /* For the dummy interface. */ | ||
47 | #define MUTEX_UNLOCKED((ath_mutex_t) 0) | ||
48 | #define MUTEX_LOCKED((ath_mutex_t) 1) | ||
49 | #define MUTEX_DESTROYED((ath_mutex_t) 2) | ||
50 | |||
51 | |||
52 | |||
53 | /* The lock we take while checking for lazy lock initialization. */ | ||
54 | static ath_mutex_t check_init_lock = ATH_MUTEX_INITIALIZER; | ||
55 | |||
56 | int | ||
57 | ath_init (void) | ||
58 | { | ||
59 | int err = 0; | ||
60 | |||
61 | if (ops_set) | ||
62 | { | ||
63 | if (ops.init) | ||
64 | err = (*ops.init) (); | ||
65 | if (err) | ||
66 | return err; | ||
67 | err = (*ops.mutex_init) (&check_init_lock); | ||
68 | } | ||
69 | return err; | ||
70 | } | ||
71 | |||
72 | |||
73 | /* Initialize the locking library. Returns 0 if the operation was | ||
74 | successful, EINVAL if the operation table was invalid and EBUSY if | ||
75 | we already were initialized. */ | ||
76 | gpg_err_code_t | ||
77 | ath_install (struct ath_ops *ath_ops, int check_only) | ||
78 | { | ||
79 | if (check_only) | ||
80 | { | ||
81 | enum ath_thread_option option = ATH_THREAD_OPTION_DEFAULT; | ||
82 | |||
83 | /* Check if the requested thread option is compatible to the | ||
84 | thread option we are already committed to. */ | ||
85 | if (ath_ops) | ||
86 | option = ath_ops->option; | ||
87 | |||
88 | if (!ops_set && option) | ||
89 | return GPG_ERR_NOT_SUPPORTED; | ||
90 | |||
91 | if (ops.option == ATH_THREAD_OPTION_USER | ||
92 | || option == ATH_THREAD_OPTION_USER | ||
93 | || ops.option != option) | ||
94 | return GPG_ERR_NOT_SUPPORTED; | ||
95 | |||
96 | return 0; | ||
97 | } | ||
98 | |||
99 | if (ath_ops) | ||
100 | { | ||
101 | /* It is convenient to not require DESTROY. */ | ||
102 | if (!ath_ops->mutex_init || !ath_ops->mutex_lock | ||
103 | || !ath_ops->mutex_unlock) | ||
104 | return GPG_ERR_INV_ARG; | ||
105 | |||
106 | ops = *ath_ops; | ||
107 | ops_set = 1; | ||
108 | } | ||
109 | else | ||
110 | ops_set = 0; | ||
111 | |||
112 | return 0; | ||
113 | } | ||
114 | |||
115 | |||
116 | static int | ||
117 | mutex_init (ath_mutex_t *lock, int just_check) | ||
118 | { | ||
119 | int err = 0; | ||
120 | |||
121 | if (just_check) | ||
122 | (*ops.mutex_lock) (&check_init_lock); | ||
123 | if (*lock == ATH_MUTEX_INITIALIZER || !just_check) | ||
124 | err = (*ops.mutex_init) (lock); | ||
125 | if (just_check) | ||
126 | (*ops.mutex_unlock) (&check_init_lock); | ||
127 | return err; | ||
128 | } | ||
129 | |||
130 | |||
131 | int | ||
132 | ath_mutex_init (ath_mutex_t *lock) | ||
133 | { | ||
134 | if (ops_set) | ||
135 | return mutex_init (lock, 0); | ||
136 | |||
137 | #ifndef NDEBUG | ||
138 | *lock = MUTEX_UNLOCKED; | ||
139 | #endif | ||
140 | return 0; | ||
141 | } | ||
142 | |||
143 | |||
144 | int | ||
145 | ath_mutex_destroy (ath_mutex_t *lock) | ||
146 | { | ||
147 | if (ops_set) | ||
148 | { | ||
149 | int err = mutex_init (lock, 1); | ||
150 | |||
151 | if (err) | ||
152 | return err; | ||
153 | |||
154 | if (ops.mutex_destroy) | ||
155 | return (*ops.mutex_destroy) (lock); | ||
156 | else | ||
157 | return 0; | ||
158 | } | ||
159 | |||
160 | #ifndef NDEBUG | ||
161 | assert (*lock == MUTEX_UNLOCKED); | ||
162 | |||
163 | *lock = MUTEX_DESTROYED; | ||
164 | #endif | ||
165 | return 0; | ||
166 | } | ||
167 | |||
168 | |||
169 | int | ||
170 | ath_mutex_lock (ath_mutex_t *lock) | ||
171 | { | ||
172 | if (ops_set) | ||
173 | { | ||
174 | int ret = mutex_init (lock, 1); | ||
175 | if (ret) | ||
176 | return ret; | ||
177 | return (*ops.mutex_lock) (lock); | ||
178 | } | ||
179 | |||
180 | #ifndef NDEBUG | ||
181 | assert (*lock == MUTEX_UNLOCKED); | ||
182 | |||
183 | *lock = MUTEX_LOCKED; | ||
184 | #endif | ||
185 | return 0; | ||
186 | } | ||
187 | |||
188 | |||
189 | int | ||
190 | ath_mutex_unlock (ath_mutex_t *lock) | ||
191 | { | ||
192 | if (ops_set) | ||
193 | { | ||
194 | int ret = mutex_init (lock, 1); | ||
195 | if (ret) | ||
196 | return ret; | ||
197 | return (*ops.mutex_unlock) (lock); | ||
198 | } | ||
199 | |||
200 | #ifndef NDEBUG | ||
201 | assert (*lock == MUTEX_LOCKED); | ||
202 | |||
203 | *lock = MUTEX_UNLOCKED; | ||
204 | #endif | ||
205 | return 0; | ||
206 | } | ||
207 | |||
208 | |||
209 | ssize_t | ||
210 | ath_read (int fd, void *buf, size_t nbytes) | ||
211 | { | ||
212 | if (ops_set && ops.read) | ||
213 | return (*ops.read) (fd, buf, nbytes); | ||
214 | else | ||
215 | return read (fd, buf, nbytes); | ||
216 | } | ||
217 | |||
218 | |||
219 | ssize_t | ||
220 | ath_write (int fd, const void *buf, size_t nbytes) | ||
221 | { | ||
222 | if (ops_set && ops.write) | ||
223 | return (*ops.write) (fd, buf, nbytes); | ||
224 | else | ||
225 | return write (fd, buf, nbytes); | ||
226 | } | ||
227 | |||
228 | |||
229 | ssize_t | ||
230 | ath_select (int nfd, fd_set *rset, fd_set *wset, fd_set *eset, | ||
231 | struct timeval *timeout) | ||
232 | { | ||
233 | if (ops_set && ops.select) | ||
234 | return (*ops.select) (nfd, rset, wset, eset, timeout); | ||
235 | else | ||
236 | return select (nfd, rset, wset, eset, timeout); | ||
237 | } | ||
238 | |||
239 | |||
240 | ssize_t | ||
241 | ath_waitpid (pid_t pid, int *status, int options) | ||
242 | { | ||
243 | if (ops_set && ops.waitpid) | ||
244 | return (*ops.waitpid) (pid, status, options); | ||
245 | else | ||
246 | return waitpid (pid, status, options); | ||
247 | } | ||
248 | |||
249 | |||
250 | int | ||
251 | ath_accept (int s, struct sockaddr *addr, socklen_t *length_ptr) | ||
252 | { | ||
253 | if (ops_set && ops.accept) | ||
254 | return (*ops.accept) (s, addr, length_ptr); | ||
255 | else | ||
256 | return accept (s, addr, length_ptr); | ||
257 | } | ||
258 | |||
259 | |||
260 | int | ||
261 | ath_connect (int s, struct sockaddr *addr, socklen_t length) | ||
262 | { | ||
263 | if (ops_set && ops.connect) | ||
264 | return (*ops.connect) (s, addr, length); | ||
265 | else | ||
266 | return connect (s, addr, length); | ||
267 | } | ||
268 | |||
269 | |||
270 | int | ||
271 | ath_sendmsg (int s, const struct msghdr *msg, int flags) | ||
272 | { | ||
273 | if (ops_set && ops.sendmsg) | ||
274 | return (*ops.sendmsg) (s, msg, flags); | ||
275 | else | ||
276 | return sendmsg (s, msg, flags); | ||
277 | } | ||
278 | |||
279 | |||
280 | int | ||
281 | ath_recvmsg (int s, struct msghdr *msg, int flags) | ||
282 | { | ||
283 | if (ops_set && ops.recvmsg) | ||
284 | return (*ops.recvmsg) (s, msg, flags); | ||
285 | else | ||
286 | return recvmsg (s, msg, flags); | ||
287 | } | ||
diff --git a/pwmanager/libcrypt/crypt/ath.h b/pwmanager/libcrypt/crypt/ath.h new file mode 100644 index 0000000..d492c2a --- a/dev/null +++ b/pwmanager/libcrypt/crypt/ath.h | |||
@@ -0,0 +1,116 @@ | |||
1 | /* ath.h - Thread-safeness library. | ||
2 | Copyright (C) 2002, 2003, 2004 g10 Code GmbH | ||
3 | |||
4 | This file is part of Libgcrypt. | ||
5 | |||
6 | Libgcrypt is free software; you can redistribute it and/or modify | ||
7 | it under the terms of the GNU Lesser General Public License as | ||
8 | published by the Free Software Foundation; either version 2.1 of | ||
9 | the License, or (at your option) any later version. | ||
10 | |||
11 | Libgcrypt is distributed in the hope that it will be useful, but | ||
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
14 | General Public License for more details. | ||
15 | |||
16 | You should have received a copy of the GNU Lesser General Public | ||
17 | License along with Libgcrypt; if not, write to the Free Software | ||
18 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | ||
19 | 02111-1307, USA. */ | ||
20 | |||
21 | #ifndef ATH_H | ||
22 | #define ATH_H | ||
23 | |||
24 | #ifdef _WIN32 | ||
25 | #warning We need to replace these hacks by cleaner code. | ||
26 | typedef int ssize_t; | ||
27 | typedef int pid_t; | ||
28 | #include <windows.h> | ||
29 | #else | ||
30 | #include <sys/types.h> | ||
31 | #include <sys/socket.h> | ||
32 | #endif | ||
33 | #include <gpg-error.h> | ||
34 | |||
35 | #include <config.h> | ||
36 | |||
37 | |||
38 | /* Define _ATH_EXT_SYM_PREFIX if you want to give all external symbols | ||
39 | a prefix. */ | ||
40 | #define _ATH_EXT_SYM_PREFIX _gcry_ | ||
41 | |||
42 | #ifdef _ATH_EXT_SYM_PREFIX | ||
43 | #define _ATH_PREFIX1(x,y) x ## y | ||
44 | #define _ATH_PREFIX2(x,y) _ATH_PREFIX1(x,y) | ||
45 | #define _ATH_PREFIX(x) _ATH_PREFIX2(_ATH_EXT_SYM_PREFIX,x) | ||
46 | #define ath_install _ATH_PREFIX(ath_install) | ||
47 | #define ath_init _ATH_PREFIX(ath_init) | ||
48 | #define ath_mutex_init _ATH_PREFIX(ath_mutex_init) | ||
49 | #define ath_mutex_destroy _ATH_PREFIX(ath_mutex_destroy) | ||
50 | #define ath_mutex_lock _ATH_PREFIX(ath_mutex_lock) | ||
51 | #define ath_mutex_unlock _ATH_PREFIX(ath_mutex_unlock) | ||
52 | #define ath_read _ATH_PREFIX(ath_read) | ||
53 | #define ath_write _ATH_PREFIX(ath_write) | ||
54 | #define ath_select _ATH_PREFIX(ath_select) | ||
55 | #define ath_waitpid _ATH_PREFIX(ath_waitpid) | ||
56 | #define ath_connect _ATH_PREFIX(ath_connect) | ||
57 | #define ath_accept _ATH_PREFIX(ath_accept) | ||
58 | #define ath_sendmsg _ATH_PREFIX(ath_sendmsg) | ||
59 | #define ath_recvmsg _ATH_PREFIX(ath_recvmsg) | ||
60 | #endif | ||
61 | |||
62 | |||
63 | enum ath_thread_option | ||
64 | { | ||
65 | ATH_THREAD_OPTION_DEFAULT = 0, | ||
66 | ATH_THREAD_OPTION_USER = 1, | ||
67 | ATH_THREAD_OPTION_PTH = 2, | ||
68 | ATH_THREAD_OPTION_PTHREAD = 3 | ||
69 | }; | ||
70 | |||
71 | struct ath_ops | ||
72 | { | ||
73 | enum ath_thread_option option; | ||
74 | int (*init) (void); | ||
75 | int (*mutex_init) (void **priv); | ||
76 | int (*mutex_destroy) (void *priv); | ||
77 | int (*mutex_lock) (void *priv); | ||
78 | int (*mutex_unlock) (void *priv); | ||
79 | ssize_t (*read) (int fd, void *buf, size_t nbytes); | ||
80 | ssize_t (*write) (int fd, const void *buf, size_t nbytes); | ||
81 | ssize_t (*select) (int nfd, fd_set *rset, fd_set *wset, fd_set *eset, | ||
82 | struct timeval *timeout); | ||
83 | ssize_t (*waitpid) (pid_t pid, int *status, int options); | ||
84 | int (*accept) (int s, struct sockaddr *addr, socklen_t *length_ptr); | ||
85 | int (*connect) (int s, struct sockaddr *addr, socklen_t length); | ||
86 | int (*sendmsg) (int s, const struct msghdr *msg, int flags); | ||
87 | int (*recvmsg) (int s, struct msghdr *msg, int flags); | ||
88 | }; | ||
89 | |||
90 | gpg_err_code_t ath_install (struct ath_ops *ath_ops, int check_only); | ||
91 | int ath_init (void); | ||
92 | |||
93 | |||
94 | /* Functions for mutual exclusion. */ | ||
95 | typedef void *ath_mutex_t; | ||
96 | #define ATH_MUTEX_INITIALIZER 0 | ||
97 | |||
98 | int ath_mutex_init (ath_mutex_t *mutex); | ||
99 | int ath_mutex_destroy (ath_mutex_t *mutex); | ||
100 | int ath_mutex_lock (ath_mutex_t *mutex); | ||
101 | int ath_mutex_unlock (ath_mutex_t *mutex); | ||
102 | |||
103 | |||
104 | /* Replacement for the POSIX functions, which can be used to allow | ||
105 | other (user-level) threads to run. */ | ||
106 | ssize_t ath_read (int fd, void *buf, size_t nbytes); | ||
107 | ssize_t ath_write (int fd, const void *buf, size_t nbytes); | ||
108 | ssize_t ath_select (int nfd, fd_set *rset, fd_set *wset, fd_set *eset, | ||
109 | struct timeval *timeout); | ||
110 | ssize_t ath_waitpid (pid_t pid, int *status, int options); | ||
111 | int ath_accept (int s, struct sockaddr *addr, socklen_t *length_ptr); | ||
112 | int ath_connect (int s, struct sockaddr *addr, socklen_t length); | ||
113 | int ath_sendmsg (int s, const struct msghdr *msg, int flags); | ||
114 | int ath_recvmsg (int s, struct msghdr *msg, int flags); | ||
115 | |||
116 | #endif/* ATH_H */ | ||
diff --git a/pwmanager/libcrypt/crypt/cipher.h b/pwmanager/libcrypt/crypt/cipher.h new file mode 100644 index 0000000..a5f194e --- a/dev/null +++ b/pwmanager/libcrypt/crypt/cipher.h | |||
@@ -0,0 +1,80 @@ | |||
1 | /* cipher.h | ||
2 | *Copyright (C) 1998, 2002, 2003 Free Software Foundation, Inc. | ||
3 | * | ||
4 | * This file is part of Libgcrypt. | ||
5 | * | ||
6 | * Libgcrypt is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU Lesser general Public License as | ||
8 | * published by the Free Software Foundation; either version 2.1 of | ||
9 | * the License, or (at your option) any later version. | ||
10 | * | ||
11 | * Libgcrypt is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * GNU Lesser General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU Lesser General Public | ||
17 | * License along with this program; if not, write to the Free Software | ||
18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | ||
19 | */ | ||
20 | #ifndef G10_CIPHER_H | ||
21 | #define G10_CIPHER_H | ||
22 | |||
23 | #include <gcrypt.h> | ||
24 | |||
25 | #define DBG_CIPHER _gcry_get_debug_flag( 1 ) | ||
26 | |||
27 | #include "../cipher/random.h" | ||
28 | |||
29 | #define PUBKEY_FLAG_NO_BLINDING (1 << 0) | ||
30 | |||
31 | /*-- rmd160.c --*/ | ||
32 | void _gcry_rmd160_hash_buffer (char *outbuf, const char *buffer, size_t length); | ||
33 | /*-- sha1.c --*/ | ||
34 | void _gcry_sha1_hash_buffer (char *outbuf, const char *buffer, size_t length); | ||
35 | |||
36 | /*-- dsa.c --*/ | ||
37 | void _gcry_register_pk_dsa_progress (gcry_handler_progress_t cbc, void *cb_data); | ||
38 | /*-- elgamal.c --*/ | ||
39 | void _gcry_register_pk_elg_progress (gcry_handler_progress_t cb, void *cb_data); | ||
40 | /*-- primegen.c --*/ | ||
41 | void _gcry_register_primegen_progress (gcry_handler_progress_t cb, void *cb_data); | ||
42 | |||
43 | /*-- pubkey.c --*/ | ||
44 | const char * _gcry_pk_aliased_algo_name (int algorithm); | ||
45 | |||
46 | /* Declarations for the cipher specifications. */ | ||
47 | extern gcry_cipher_spec_t _gcry_cipher_spec_blowfish; | ||
48 | extern gcry_cipher_spec_t _gcry_cipher_spec_des; | ||
49 | extern gcry_cipher_spec_t _gcry_cipher_spec_tripledes; | ||
50 | extern gcry_cipher_spec_t _gcry_cipher_spec_arcfour; | ||
51 | extern gcry_cipher_spec_t _gcry_cipher_spec_cast5; | ||
52 | extern gcry_cipher_spec_t _gcry_cipher_spec_aes; | ||
53 | extern gcry_cipher_spec_t _gcry_cipher_spec_aes192; | ||
54 | extern gcry_cipher_spec_t _gcry_cipher_spec_aes256; | ||
55 | extern gcry_cipher_spec_t _gcry_cipher_spec_twofish; | ||
56 | extern gcry_cipher_spec_t _gcry_cipher_spec_twofish128; | ||
57 | extern gcry_cipher_spec_t _gcry_cipher_spec_serpent128; | ||
58 | extern gcry_cipher_spec_t _gcry_cipher_spec_serpent192; | ||
59 | extern gcry_cipher_spec_t _gcry_cipher_spec_serpent256; | ||
60 | extern gcry_cipher_spec_t _gcry_cipher_spec_rfc2268_40; | ||
61 | |||
62 | /* Declarations for the digest specifications. */ | ||
63 | extern gcry_md_spec_t _gcry_digest_spec_crc32; | ||
64 | extern gcry_md_spec_t _gcry_digest_spec_crc32_rfc1510; | ||
65 | extern gcry_md_spec_t _gcry_digest_spec_crc24_rfc2440; | ||
66 | extern gcry_md_spec_t _gcry_digest_spec_md4; | ||
67 | extern gcry_md_spec_t _gcry_digest_spec_md5; | ||
68 | extern gcry_md_spec_t _gcry_digest_spec_rmd160; | ||
69 | extern gcry_md_spec_t _gcry_digest_spec_sha1; | ||
70 | extern gcry_md_spec_t _gcry_digest_spec_sha256; | ||
71 | extern gcry_md_spec_t _gcry_digest_spec_sha512; | ||
72 | extern gcry_md_spec_t _gcry_digest_spec_sha384; | ||
73 | extern gcry_md_spec_t _gcry_digest_spec_tiger; | ||
74 | |||
75 | /* Declarations for the pubkey cipher specifications. */ | ||
76 | extern gcry_pk_spec_t _gcry_pubkey_spec_rsa; | ||
77 | extern gcry_pk_spec_t _gcry_pubkey_spec_elg; | ||
78 | extern gcry_pk_spec_t _gcry_pubkey_spec_dsa; | ||
79 | |||
80 | #endif /*G10_CIPHER_H*/ | ||
diff --git a/pwmanager/libcrypt/crypt/g10lib.h b/pwmanager/libcrypt/crypt/g10lib.h new file mode 100644 index 0000000..0aef376 --- a/dev/null +++ b/pwmanager/libcrypt/crypt/g10lib.h | |||
@@ -0,0 +1,250 @@ | |||
1 | /* g10lib.h - internal defintions for libgcrypt | ||
2 | * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc. | ||
3 | * | ||
4 | * This header is to be used inside of libgcrypt in place of gcrypt.h. | ||
5 | * This way we can better distinguish between internal and external | ||
6 | * usage of gcrypt.h | ||
7 | * | ||
8 | * This file is part of Libgcrypt. | ||
9 | * | ||
10 | * Libgcrypt is free software; you can redistribute it and/or modify | ||
11 | * it under the terms of the GNU Lesser general Public License as | ||
12 | * published by the Free Software Foundation; either version 2.1 of | ||
13 | * the License, or (at your option) any later version. | ||
14 | * | ||
15 | * Libgcrypt is distributed in the hope that it will be useful, | ||
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
18 | * GNU Lesser General Public License for more details. | ||
19 | * | ||
20 | * You should have received a copy of the GNU Lesser General Public | ||
21 | * License along with this program; if not, write to the Free Software | ||
22 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | ||
23 | */ | ||
24 | |||
25 | #ifndef G10LIB_H | ||
26 | #define G10LIB_H 1 | ||
27 | |||
28 | #ifdef _GCRYPT_H | ||
29 | #error gcrypt.h already included | ||
30 | #endif | ||
31 | |||
32 | #ifndef _GCRYPT_IN_LIBGCRYPT | ||
33 | #error something is wrong with config.h | ||
34 | #endif | ||
35 | |||
36 | #include <gcrypt.h> | ||
37 | #include "types.h" | ||
38 | |||
39 | #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 ) | ||
40 | #define JNLIB_GCC_M_FUNCTION 1 | ||
41 | #define JNLIB_GCC_A_NR __attribute__ ((noreturn)) | ||
42 | #define JNLIB_GCC_A_PRINTF( f, a ) __attribute__ ((format (printf,f,a))) | ||
43 | #define JNLIB_GCC_A_NR_PRINTF( f, a ) \ | ||
44 | __attribute__ ((noreturn, format (printf,f,a))) | ||
45 | #define GCC_ATTR_NORETURN __attribute__ ((__noreturn__)) | ||
46 | #else | ||
47 | #define JNLIB_GCC_A_NR | ||
48 | #define JNLIB_GCC_A_PRINTF( f, a ) | ||
49 | #define JNLIB_GCC_A_NR_PRINTF( f, a ) | ||
50 | #define GCC_ATTR_NORETURN | ||
51 | #endif | ||
52 | |||
53 | #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96 ) | ||
54 | #define GCC_ATTR_PURE __attribute__ ((__pure__)) | ||
55 | #else | ||
56 | #define GCC_ATTR_PURE | ||
57 | #endif | ||
58 | |||
59 | /* (The malloc attribute might be defined prior to 3.2 - I am just not sure) */ | ||
60 | #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 2 ) | ||
61 | #define GCC_ATTR_MALLOC __attribute__ ((__malloc__)) | ||
62 | #else | ||
63 | #define GCC_ATTR_MALLOC | ||
64 | #endif | ||
65 | |||
66 | #ifdef G10_I18N_H | ||
67 | #error i18n should not be included here | ||
68 | #endif | ||
69 | |||
70 | #define _(a) _gcry_gettext(a) | ||
71 | #define N_(a) (a) | ||
72 | |||
73 | void _gcry_check_heap( const void *a ); | ||
74 | |||
75 | int _gcry_get_debug_flag( unsigned int mask ); | ||
76 | |||
77 | |||
78 | /*-- gcrypt/misc.c --*/ | ||
79 | |||
80 | #ifdef JNLIB_GCC_M_FUNCTION | ||
81 | void _gcry_bug (const char *file, int line, | ||
82 | const char *func) GCC_ATTR_NORETURN; | ||
83 | #else | ||
84 | void _gcry_bug (const char *file, int line); | ||
85 | #endif | ||
86 | |||
87 | const char *_gcry_gettext (const char *key); | ||
88 | void _gcry_fatal_error(int rc, const char *text ) JNLIB_GCC_A_NR; | ||
89 | void _gcry_log( int level, const char *fmt, ... ) JNLIB_GCC_A_PRINTF(2,3); | ||
90 | void _gcry_log_bug( const char *fmt, ... ) JNLIB_GCC_A_NR_PRINTF(1,2); | ||
91 | void _gcry_log_fatal( const char *fmt, ... ) JNLIB_GCC_A_NR_PRINTF(1,2); | ||
92 | void _gcry_log_error( const char *fmt, ... ) JNLIB_GCC_A_PRINTF(1,2); | ||
93 | void _gcry_log_info( const char *fmt, ... ) JNLIB_GCC_A_PRINTF(1,2); | ||
94 | void _gcry_log_debug( const char *fmt, ... ) JNLIB_GCC_A_PRINTF(1,2); | ||
95 | void _gcry_log_printf ( const char *fmt, ... ) JNLIB_GCC_A_PRINTF(1,2); | ||
96 | |||
97 | void _gcry_set_log_verbosity( int level ); | ||
98 | int _gcry_log_verbosity( int level ); | ||
99 | |||
100 | #ifdef JNLIB_GCC_M_FUNCTION | ||
101 | #define BUG() _gcry_bug( __FILE__ , __LINE__, __FUNCTION__ ) | ||
102 | #else | ||
103 | #define BUG() _gcry_bug( __FILE__ , __LINE__ ) | ||
104 | #endif | ||
105 | |||
106 | #define log_hexdump _gcry_log_hexdump | ||
107 | #define log_bug _gcry_log_bug | ||
108 | #define log_fatal _gcry_log_fatal | ||
109 | #define log_error _gcry_log_error | ||
110 | #define log_info _gcry_log_info | ||
111 | #define log_debug _gcry_log_debug | ||
112 | #define log_printf _gcry_log_printf | ||
113 | |||
114 | |||
115 | |||
116 | |||
117 | /*-- cipher/pubkey.c --*/ | ||
118 | |||
119 | #ifndef mpi_powm | ||
120 | #define mpi_powm(w,b,e,m) gcry_mpi_powm( (w), (b), (e), (m) ) | ||
121 | #endif | ||
122 | |||
123 | /*-- primegen.c --*/ | ||
124 | gcry_mpi_t _gcry_generate_secret_prime (unsigned int nbits, | ||
125 | int (*extra_check)(void*, gcry_mpi_t), | ||
126 | void *extra_check_arg); | ||
127 | gcry_mpi_t _gcry_generate_public_prime (unsigned int nbits, | ||
128 | int (*extra_check)(void*, gcry_mpi_t), | ||
129 | void *extra_check_arg); | ||
130 | gcry_mpi_t _gcry_generate_elg_prime( int mode, unsigned pbits, unsigned qbits, | ||
131 | gcry_mpi_t g, gcry_mpi_t **factors ); | ||
132 | |||
133 | |||
134 | /* replacements of missing functions (missing-string.c)*/ | ||
135 | #ifndef HAVE_STPCPY | ||
136 | char *stpcpy (char *a, const char *b); | ||
137 | #endif | ||
138 | #ifndef HAVE_STRCASECMP | ||
139 | int strcasecmp (const char *a, const char *b) GCC_ATTR_PURE; | ||
140 | #endif | ||
141 | |||
142 | /* macros used to rename missing functions */ | ||
143 | #ifndef HAVE_STRTOUL | ||
144 | #define strtoul(a,b,c) ((unsigned long)strtol((a),(b),(c))) | ||
145 | #endif | ||
146 | #ifndef HAVE_MEMMOVE | ||
147 | #define memmove(d, s, n) bcopy((s), (d), (n)) | ||
148 | #endif | ||
149 | #ifndef HAVE_STRICMP | ||
150 | #define stricmp(a,b) strcasecmp( (a), (b) ) | ||
151 | #endif | ||
152 | #ifndef HAVE_ATEXIT | ||
153 | #define atexit(a) (on_exit((a),0)) | ||
154 | #endif | ||
155 | #ifndef HAVE_RAISE | ||
156 | #define raise(a) kill(getpid(), (a)) | ||
157 | #endif | ||
158 | |||
159 | |||
160 | /* some handy macros */ | ||
161 | #ifndef STR | ||
162 | #define STR(v) #v | ||
163 | #endif | ||
164 | #define STR2(v) STR(v) | ||
165 | #define DIM(v) (sizeof(v)/sizeof((v)[0])) | ||
166 | #define DIMof(type,member) DIM(((type *)0)->member) | ||
167 | |||
168 | /* Stack burning. */ | ||
169 | |||
170 | void _gcry_burn_stack (int bytes); | ||
171 | |||
172 | |||
173 | /* To avoid that a compiler optimizes certain memset calls away, these | ||
174 | macros may be used instead. */ | ||
175 | #define wipememory2(_ptr,_set,_len) do { \ | ||
176 | volatile char *_vptr=(volatile char *)(_ptr); \ | ||
177 | size_t _vlen=(_len); \ | ||
178 | while(_vlen) { *_vptr=(_set); _vptr++; _vlen--; } \ | ||
179 | } while(0) | ||
180 | #define wipememory(_ptr,_len) wipememory2(_ptr,0,_len) | ||
181 | |||
182 | |||
183 | |||
184 | /* Digit predicates. */ | ||
185 | |||
186 | #define digitp(p) (*(p) >= '0' && *(p) <= '9') | ||
187 | #define octdigitp(p) (*(p) >= '0' && *(p) <= '7') | ||
188 | #define alphap(a) ( (*(a) >= 'A' && *(a) <= 'Z') \ | ||
189 | || (*(a) >= 'a' && *(a) <= 'z')) | ||
190 | #define hexdigitp(a) (digitp (a) \ | ||
191 | || (*(a) >= 'A' && *(a) <= 'F') \ | ||
192 | || (*(a) >= 'a' && *(a) <= 'f')) | ||
193 | |||
194 | /* Management for ciphers/digests/pubkey-ciphers. */ | ||
195 | |||
196 | /* Structure for each registered `module'. */ | ||
197 | struct gcry_module | ||
198 | { | ||
199 | struct gcry_module *next; /* List pointers. */ | ||
200 | struct gcry_module **prevp; | ||
201 | void *spec; /* The acctual specs. */ | ||
202 | int flags; /* Associated flags. */ | ||
203 | int counter; /* Use counter. */ | ||
204 | unsigned int mod_id; /* ID of this module. */ | ||
205 | }; | ||
206 | |||
207 | /* Flags for the `flags' member of gcry_module_t. */ | ||
208 | #define FLAG_MODULE_DISABLED 1 << 0 | ||
209 | |||
210 | gcry_err_code_t _gcry_module_add (gcry_module_t *entries, | ||
211 | unsigned int id, | ||
212 | void *spec, | ||
213 | gcry_module_t *module); | ||
214 | |||
215 | typedef int (*gcry_module_lookup_t) (void *spec, void *data); | ||
216 | |||
217 | /* Lookup a module specification by it's ID. After a successfull | ||
218 | lookup, the module has it's resource counter incremented. */ | ||
219 | gcry_module_t _gcry_module_lookup_id (gcry_module_t entries, | ||
220 | unsigned int id); | ||
221 | |||
222 | /* Internal function. Lookup a module specification. */ | ||
223 | gcry_module_t _gcry_module_lookup (gcry_module_t entries, void *data, | ||
224 | gcry_module_lookup_t func); | ||
225 | |||
226 | /* Release a module. In case the use-counter reaches zero, destroy | ||
227 | the module. */ | ||
228 | void _gcry_module_release (gcry_module_t entry); | ||
229 | |||
230 | /* Add a reference to a module. */ | ||
231 | void _gcry_module_use (gcry_module_t module); | ||
232 | |||
233 | /* Return a list of module IDs. */ | ||
234 | gcry_err_code_t _gcry_module_list (gcry_module_t modules, | ||
235 | int *list, int *list_length); | ||
236 | |||
237 | gcry_err_code_t _gcry_cipher_init (void); | ||
238 | gcry_err_code_t _gcry_md_init (void); | ||
239 | gcry_err_code_t _gcry_pk_init (void); | ||
240 | |||
241 | gcry_err_code_t _gcry_pk_module_lookup (int id, gcry_module_t *module); | ||
242 | void _gcry_pk_module_release (gcry_module_t module); | ||
243 | |||
244 | /* Memory management. */ | ||
245 | |||
246 | gcry_err_code_t _gcry_malloc (size_t n, unsigned int flags, void **mem); | ||
247 | |||
248 | #define GCRY_ALLOC_FLAG_SECURE (1 << 0) | ||
249 | |||
250 | #endif /* G10LIB_H */ | ||
diff --git a/pwmanager/libcrypt/crypt/gcrypt-module.h b/pwmanager/libcrypt/crypt/gcrypt-module.h new file mode 100644 index 0000000..1099c92 --- a/dev/null +++ b/pwmanager/libcrypt/crypt/gcrypt-module.h | |||
@@ -0,0 +1,225 @@ | |||
1 | /* gcrypt-module.h - GNU cryptographic library interface | ||
2 | * Copyright (C) 2003 Free Software Foundation, Inc. | ||
3 | * | ||
4 | * This file is part of Libgcrypt. | ||
5 | * | ||
6 | * Libgcrypt is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU Lesser General Public License as | ||
8 | * published by the Free Software Foundation; either version 2.1 of | ||
9 | * the License, or (at your option) any later version. | ||
10 | * | ||
11 | * Libgcrypt is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * GNU Lesser General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU Lesser General Public | ||
17 | * License along with this program; if not, write to the Free Software | ||
18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | ||
19 | */ | ||
20 | |||
21 | /* This file contains the necessary declarations/definitions for | ||
22 | working with Libgcrypt modules. */ | ||
23 | |||
24 | #ifndef _GCRYPT_MODULE_H | ||
25 | #define _GCRYPT_MODULE_H | ||
26 | |||
27 | #ifdef __cplusplus | ||
28 | extern "C" { | ||
29 | #if 0 /* keep Emacsens's auto-indent happy */ | ||
30 | } | ||
31 | #endif | ||
32 | #endif | ||
33 | |||
34 | /* This type represents a `module'. */ | ||
35 | typedef struct gcry_module *gcry_module_t; | ||
36 | |||
37 | /* Check that the library fulfills the version requirement. */ | ||
38 | |||
39 | /* Type for the cipher_setkey function. */ | ||
40 | typedef gcry_err_code_t (*gcry_cipher_setkey_t) (void *c, | ||
41 | const unsigned char *key, | ||
42 | unsigned keylen); | ||
43 | |||
44 | /* Type for the cipher_encrypt function. */ | ||
45 | typedef void (*gcry_cipher_encrypt_t) (void *c, | ||
46 | unsigned char *outbuf, | ||
47 | const unsigned char *inbuf); | ||
48 | |||
49 | /* Type for the cipher_decrypt function. */ | ||
50 | typedef void (*gcry_cipher_decrypt_t) (void *c, | ||
51 | unsigned char *outbuf, | ||
52 | const unsigned char *inbuf); | ||
53 | |||
54 | /* Type for the cipher_stencrypt function. */ | ||
55 | typedef void (*gcry_cipher_stencrypt_t) (void *c, | ||
56 | unsigned char *outbuf, | ||
57 | const unsigned char *inbuf, | ||
58 | unsigned int n); | ||
59 | |||
60 | /* Type for the cipher_stdecrypt function. */ | ||
61 | typedef void (*gcry_cipher_stdecrypt_t) (void *c, | ||
62 | unsigned char *outbuf, | ||
63 | const unsigned char *inbuf, | ||
64 | unsigned int n); | ||
65 | |||
66 | typedef struct gcry_cipher_oid_spec | ||
67 | { | ||
68 | const char *oid; | ||
69 | int mode; | ||
70 | } gcry_cipher_oid_spec_t; | ||
71 | |||
72 | /* Module specification structure for ciphers. */ | ||
73 | typedef struct gcry_cipher_spec | ||
74 | { | ||
75 | const char *name; | ||
76 | const char **aliases; | ||
77 | gcry_cipher_oid_spec_t *oids; | ||
78 | size_t blocksize; | ||
79 | size_t keylen; | ||
80 | size_t contextsize; | ||
81 | gcry_cipher_setkey_t setkey; | ||
82 | gcry_cipher_encrypt_t encrypt; | ||
83 | gcry_cipher_decrypt_t decrypt; | ||
84 | gcry_cipher_stencrypt_t stencrypt; | ||
85 | gcry_cipher_stdecrypt_t stdecrypt; | ||
86 | } gcry_cipher_spec_t; | ||
87 | |||
88 | /* Register a new cipher module whose specification can be found in | ||
89 | CIPHER. On success, a new algorithm ID is stored in ALGORITHM_ID | ||
90 | and a pointer representhing this module is stored in MODULE. */ | ||
91 | gcry_error_t gcry_cipher_register (gcry_cipher_spec_t *cipher, | ||
92 | unsigned int *algorithm_id, | ||
93 | gcry_module_t *module); | ||
94 | |||
95 | /* Unregister the cipher identified by MODULE, which must have been | ||
96 | registered with gcry_cipher_register. */ | ||
97 | void gcry_cipher_unregister (gcry_module_t module); | ||
98 | |||
99 | /* ********************** */ | ||
100 | |||
101 | /* Type for the pk_generate function. */ | ||
102 | typedef gcry_err_code_t (*gcry_pk_generate_t) (int algo, | ||
103 | unsigned int nbits, | ||
104 | unsigned long use_e, | ||
105 | gcry_mpi_t *skey, | ||
106 | gcry_mpi_t **retfactors); | ||
107 | |||
108 | /* Type for the pk_check_secret_key function. */ | ||
109 | typedef gcry_err_code_t (*gcry_pk_check_secret_key_t) (int algo, | ||
110 | gcry_mpi_t *skey); | ||
111 | |||
112 | /* Type for the pk_encrypt function. */ | ||
113 | typedef gcry_err_code_t (*gcry_pk_encrypt_t) (int algo, | ||
114 | gcry_mpi_t *resarr, | ||
115 | gcry_mpi_t data, | ||
116 | gcry_mpi_t *pkey, | ||
117 | int flags); | ||
118 | |||
119 | /* Type for the pk_decrypt function. */ | ||
120 | typedef gcry_err_code_t (*gcry_pk_decrypt_t) (int algo, | ||
121 | gcry_mpi_t *result, | ||
122 | gcry_mpi_t *data, | ||
123 | gcry_mpi_t *skey, | ||
124 | int flags); | ||
125 | |||
126 | /* Type for the pk_sign function. */ | ||
127 | typedef gcry_err_code_t (*gcry_pk_sign_t) (int algo, | ||
128 | gcry_mpi_t *resarr, | ||
129 | gcry_mpi_t data, | ||
130 | gcry_mpi_t *skey); | ||
131 | |||
132 | /* Type for the pk_verify function. */ | ||
133 | typedef gcry_err_code_t (*gcry_pk_verify_t) (int algo, | ||
134 | gcry_mpi_t hash, | ||
135 | gcry_mpi_t *data, | ||
136 | gcry_mpi_t *pkey, | ||
137 | int (*cmp) (void *, gcry_mpi_t), | ||
138 | void *opaquev); | ||
139 | |||
140 | /* Type for the pk_get_nbits function. */ | ||
141 | typedef unsigned (*gcry_pk_get_nbits_t) (int algo, gcry_mpi_t *pkey); | ||
142 | |||
143 | /* Module specification structure for message digests. */ | ||
144 | typedef struct gcry_pk_spec | ||
145 | { | ||
146 | const char *name; | ||
147 | char **aliases; | ||
148 | const char *elements_pkey; | ||
149 | const char *elements_skey; | ||
150 | const char *elements_enc; | ||
151 | const char *elements_sig; | ||
152 | const char *elements_grip; | ||
153 | int use; | ||
154 | gcry_pk_generate_t generate; | ||
155 | gcry_pk_check_secret_key_t check_secret_key; | ||
156 | gcry_pk_encrypt_t encrypt; | ||
157 | gcry_pk_decrypt_t decrypt; | ||
158 | gcry_pk_sign_t sign; | ||
159 | gcry_pk_verify_t verify; | ||
160 | gcry_pk_get_nbits_t get_nbits; | ||
161 | } gcry_pk_spec_t; | ||
162 | |||
163 | /* Register a new pubkey module whose specification can be found in | ||
164 | PUBKEY. On success, a new algorithm ID is stored in ALGORITHM_ID | ||
165 | and a pointer representhing this module is stored in MODULE. */ | ||
166 | gcry_error_t gcry_pk_register (gcry_pk_spec_t *pubkey, | ||
167 | unsigned int *algorithm_id, | ||
168 | gcry_module_t *module); | ||
169 | |||
170 | /* Unregister the pubkey identified by ID, which must have been | ||
171 | registered with gcry_pk_register. */ | ||
172 | void gcry_pk_unregister (gcry_module_t module); | ||
173 | |||
174 | /* ********************** */ | ||
175 | |||
176 | /* Type for the md_init function. */ | ||
177 | typedef void (*gcry_md_init_t) (void *c); | ||
178 | |||
179 | /* Type for the md_write function. */ | ||
180 | typedef void (*gcry_md_write_t) (void *c, unsigned char *buf, size_t nbytes); | ||
181 | |||
182 | /* Type for the md_final function. */ | ||
183 | typedef void (*gcry_md_final_t) (void *c); | ||
184 | |||
185 | /* Type for the md_read function. */ | ||
186 | typedef unsigned char *(*gcry_md_read_t) (void *c); | ||
187 | |||
188 | typedef struct gcry_md_oid_spec | ||
189 | { | ||
190 | const char *oidstring; | ||
191 | } gcry_md_oid_spec_t; | ||
192 | |||
193 | /* Module specification structure for message digests. */ | ||
194 | typedef struct gcry_md_spec | ||
195 | { | ||
196 | const char *name; | ||
197 | unsigned char *asnoid; | ||
198 | int asnlen; | ||
199 | gcry_md_oid_spec_t *oids; | ||
200 | int mdlen; | ||
201 | gcry_md_init_t init; | ||
202 | gcry_md_write_t write; | ||
203 | gcry_md_final_t final; | ||
204 | gcry_md_read_t read; | ||
205 | size_t contextsize; /* allocate this amount of context */ | ||
206 | } gcry_md_spec_t; | ||
207 | |||
208 | /* Register a new digest module whose specification can be found in | ||
209 | DIGEST. On success, a new algorithm ID is stored in ALGORITHM_ID | ||
210 | and a pointer representhing this module is stored in MODULE. */ | ||
211 | gcry_error_t gcry_md_register (gcry_md_spec_t *digest, | ||
212 | unsigned int *algorithm_id, | ||
213 | gcry_module_t *module); | ||
214 | |||
215 | /* Unregister the digest identified by ID, which must have been | ||
216 | registered with gcry_digest_register. */ | ||
217 | void gcry_md_unregister (gcry_module_t module); | ||
218 | |||
219 | #if 0 /* keep Emacsens's auto-indent happy */ | ||
220 | { | ||
221 | #endif | ||
222 | #ifdef __cplusplus | ||
223 | } | ||
224 | #endif | ||
225 | #endif | ||
diff --git a/pwmanager/libcrypt/crypt/gcrypt.h b/pwmanager/libcrypt/crypt/gcrypt.h new file mode 100644 index 0000000..49ac53f --- a/dev/null +++ b/pwmanager/libcrypt/crypt/gcrypt.h | |||
@@ -0,0 +1,1497 @@ | |||
1 | /* gcrypt.h - GNU cryptographic library interface | ||
2 | * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004 | ||
3 | * 2004 Free Software Foundation, Inc. | ||
4 | * | ||
5 | * This file is part of Libgcrypt. | ||
6 | * | ||
7 | * Libgcrypt is free software; you can redistribute it and/or modify | ||
8 | * it under the terms of the GNU Lesser General Public License as | ||
9 | * published by the Free Software Foundation; either version 2.1 of | ||
10 | * the License, or (at your option) any later version. | ||
11 | * | ||
12 | * Libgcrypt is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | * GNU Lesser General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU Lesser General Public | ||
18 | * License along with this program; if not, write to the Free Software | ||
19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | ||
20 | */ | ||
21 | |||
22 | #ifndef _GCRYPT_H | ||
23 | #define _GCRYPT_H | ||
24 | |||
25 | #include <stdarg.h> | ||
26 | #include <string.h> | ||
27 | |||
28 | #include <gpg-error.h> | ||
29 | |||
30 | #include <sys/types.h> | ||
31 | #include <sys/socket.h> | ||
32 | |||
33 | /* This is required for error code compatibility. */ | ||
34 | #define _GCRY_ERR_SOURCE_DEFAULT GPG_ERR_SOURCE_GCRYPT | ||
35 | |||
36 | #ifdef __cplusplus | ||
37 | extern "C" { | ||
38 | #if 0 /* keep Emacsens' auto-indent happy */ | ||
39 | } | ||
40 | #endif | ||
41 | #endif | ||
42 | |||
43 | /* The version of this header should match the one of the library. It | ||
44 | should not be used by a program because gcry_check_version() should | ||
45 | return the same version. The purpose of this macro is to let | ||
46 | autoconf (using the AM_PATH_GCRYPT macro) check that this header | ||
47 | matches the installed library. Note: Do not edit the next line as | ||
48 | configure may fix the string here. */ | ||
49 | #define GCRYPT_VERSION "1.2.0" | ||
50 | |||
51 | /* Internal: We can't use the convenience macros for the multi | ||
52 | precision integer functions when building this library. */ | ||
53 | #ifdef _GCRYPT_IN_LIBGCRYPT | ||
54 | #ifndef GCRYPT_NO_MPI_MACROS | ||
55 | #define GCRYPT_NO_MPI_MACROS 1 | ||
56 | #endif | ||
57 | #endif | ||
58 | |||
59 | /* We want to use gcc attributes when possible. Warning: Don't use | ||
60 | these macros in your programs: As indicated by the leading | ||
61 | underscore they are subject to change without notice. */ | ||
62 | #ifdef __GNUC__ | ||
63 | |||
64 | #define _GCRY_GCC_VERSION (__GNUC__ * 10000 \ | ||
65 | + __GNUC_MINOR__ * 100 \ | ||
66 | + __GNUC_PATCHLEVEL__) | ||
67 | |||
68 | #if _GCRY_GCC_VERSION >= 30100 | ||
69 | #define _GCRY_GCC_ATTR_DEPRECATED __attribute__ ((__deprecated__)) | ||
70 | #endif | ||
71 | |||
72 | #if _GCRY_GCC_VERSION >= 29600 | ||
73 | #define _GCRY_GCC_ATTR_PURE __attribute__ ((__pure__)) | ||
74 | #endif | ||
75 | |||
76 | #if _GCRY_GCC_VERSION >= 300200 | ||
77 | #define _GCRY_GCC_ATTR_MALLOC __attribute__ ((__malloc__)) | ||
78 | #endif | ||
79 | |||
80 | #endif /*__GNUC__*/ | ||
81 | |||
82 | #ifndef _GCRY_GCC_ATTR_DEPRECATED | ||
83 | #define _GCRY_GCC_ATTR_DEPRECATED | ||
84 | #endif | ||
85 | #ifndef _GCRY_GCC_ATTR_PURE | ||
86 | #define _GCRY_GCC_ATTR_PURE | ||
87 | #endif | ||
88 | #ifndef _GCRY_GCC_ATTR_MALLOC | ||
89 | #define _GCRY_GCC_ATTR_MALLOC | ||
90 | #endif | ||
91 | |||
92 | /* Wrappers for the libgpg-error library. */ | ||
93 | |||
94 | typedef gpg_error_t gcry_error_t; | ||
95 | typedef gpg_err_code_t gcry_err_code_t; | ||
96 | typedef gpg_err_source_t gcry_err_source_t; | ||
97 | |||
98 | static GPG_ERR_INLINE gcry_error_t | ||
99 | gcry_err_make (gcry_err_source_t source, gcry_err_code_t code) | ||
100 | { | ||
101 | return gpg_err_make (source, code); | ||
102 | } | ||
103 | |||
104 | /* The user can define GPG_ERR_SOURCE_DEFAULT before including this | ||
105 | file to specify a default source for gpg_error. */ | ||
106 | #ifndef GCRY_ERR_SOURCE_DEFAULT | ||
107 | #define GCRY_ERR_SOURCE_DEFAULT GPG_ERR_SOURCE_USER_1 | ||
108 | #endif | ||
109 | |||
110 | static GPG_ERR_INLINE gcry_error_t | ||
111 | gcry_error (gcry_err_code_t code) | ||
112 | { | ||
113 | return gcry_err_make (GCRY_ERR_SOURCE_DEFAULT, code); | ||
114 | } | ||
115 | |||
116 | static GPG_ERR_INLINE gcry_err_code_t | ||
117 | gcry_err_code (gcry_error_t err) | ||
118 | { | ||
119 | return gpg_err_code (err); | ||
120 | } | ||
121 | |||
122 | |||
123 | static GPG_ERR_INLINE gcry_err_source_t | ||
124 | gcry_err_source (gcry_error_t err) | ||
125 | { | ||
126 | return gpg_err_source (err); | ||
127 | } | ||
128 | |||
129 | /* Return a pointer to a string containing a description of the error | ||
130 | code in the error value ERR. */ | ||
131 | const char *gcry_strerror (gcry_error_t err); | ||
132 | |||
133 | /* Return a pointer to a string containing a description of the error | ||
134 | source in the error value ERR. */ | ||
135 | const char *gcry_strsource (gcry_error_t err); | ||
136 | |||
137 | /* Retrieve the error code for the system error ERR. This returns | ||
138 | GPG_ERR_UNKNOWN_ERRNO if the system error is not mapped (report | ||
139 | this). */ | ||
140 | gcry_err_code_t gcry_err_code_from_errno (int err); | ||
141 | |||
142 | /* Retrieve the system error for the error code CODE. This returns 0 | ||
143 | if CODE is not a system error code. */ | ||
144 | int gcry_err_code_to_errno (gcry_err_code_t code); | ||
145 | |||
146 | /* Return an error value with the error source SOURCE and the system | ||
147 | error ERR. */ | ||
148 | gcry_error_t gcry_err_make_from_errno (gcry_err_source_t source, int err); | ||
149 | |||
150 | /* Return an error value with the system error ERR. */ | ||
151 | gcry_err_code_t gcry_error_from_errno (int err); | ||
152 | |||
153 | |||
154 | enum gcry_thread_option | ||
155 | { | ||
156 | GCRY_THREAD_OPTION_DEFAULT = 0, | ||
157 | GCRY_THREAD_OPTION_USER = 1, | ||
158 | GCRY_THREAD_OPTION_PTH = 2, | ||
159 | GCRY_THREAD_OPTION_PTHREAD = 3 | ||
160 | }; | ||
161 | |||
162 | /* Wrapper for struct ath_ops. */ | ||
163 | struct gcry_thread_cbs | ||
164 | { | ||
165 | enum gcry_thread_option option; | ||
166 | int (*init) (void); | ||
167 | int (*mutex_init) (void **priv); | ||
168 | int (*mutex_destroy) (void **priv); | ||
169 | int (*mutex_lock) (void **priv); | ||
170 | int (*mutex_unlock) (void **priv); | ||
171 | ssize_t (*read) (int fd, void *buf, size_t nbytes); | ||
172 | ssize_t (*write) (int fd, const void *buf, size_t nbytes); | ||
173 | ssize_t (*select) (int nfd, fd_set *rset, fd_set *wset, fd_set *eset, | ||
174 | struct timeval *timeout); | ||
175 | ssize_t (*waitpid) (pid_t pid, int *status, int options); | ||
176 | int (*accept) (int s, struct sockaddr *addr, socklen_t *length_ptr); | ||
177 | int (*connect) (int s, struct sockaddr *addr, socklen_t length); | ||
178 | int (*sendmsg) (int s, const struct msghdr *msg, int flags); | ||
179 | int (*recvmsg) (int s, struct msghdr *msg, int flags); | ||
180 | }; | ||
181 | |||
182 | #define GCRY_THREAD_OPTION_PTH_IMPL \ | ||
183 | static int gcry_pth_init (void) \ | ||
184 | { return (pth_init () == FALSE) ? errno : 0; } \ | ||
185 | static int gcry_pth_mutex_init (void **priv) \ | ||
186 | { \ | ||
187 | int err = 0; \ | ||
188 | pth_mutex_t *lock = malloc (sizeof (pth_mutex_t)); \ | ||
189 | \ | ||
190 | if (!lock) \ | ||
191 | err = ENOMEM; \ | ||
192 | if (!err) \ | ||
193 | { \ | ||
194 | err = pth_mutex_init (lock); \ | ||
195 | if (err == FALSE) \ | ||
196 | err = errno; \ | ||
197 | else \ | ||
198 | err = 0; \ | ||
199 | if (err) \ | ||
200 | free (lock); \ | ||
201 | else \ | ||
202 | *priv = lock; \ | ||
203 | } \ | ||
204 | return err; \ | ||
205 | } \ | ||
206 | static int gcry_pth_mutex_destroy (void **lock) \ | ||
207 | { /* GNU Pth has no destructor function. */ free (*lock); return 0; } \ | ||
208 | static int gcry_pth_mutex_lock (void **lock) \ | ||
209 | { return ((pth_mutex_acquire (*lock, 0, NULL)) == FALSE) \ | ||
210 | ? errno : 0; } \ | ||
211 | static int gcry_pth_mutex_unlock (void **lock) \ | ||
212 | { return ((pth_mutex_release (*lock)) == FALSE) \ | ||
213 | ? errno : 0; } \ | ||
214 | static ssize_t gcry_pth_read (int fd, void *buf, size_t nbytes) \ | ||
215 | { return pth_read (fd, buf, nbytes); } \ | ||
216 | static ssize_t gcry_pth_write (int fd, const void *buf, size_t nbytes) \ | ||
217 | { return pth_write (fd, buf, nbytes); } \ | ||
218 | static ssize_t gcry_pth_select (int nfd, fd_set *rset, fd_set *wset, \ | ||
219 | fd_set *eset, struct timeval *timeout) \ | ||
220 | { return pth_select (nfd, rset, wset, eset, timeout); } \ | ||
221 | static ssize_t gcry_pth_waitpid (pid_t pid, int *status, int options) \ | ||
222 | { return pth_waitpid (pid, status, options); } \ | ||
223 | static int gcry_pth_accept (int s, struct sockaddr *addr, \ | ||
224 | socklen_t *length_ptr) \ | ||
225 | { return pth_accept (s, addr, length_ptr); } \ | ||
226 | static int gcry_pth_connect (int s, struct sockaddr *addr, socklen_t length) \ | ||
227 | { return pth_connect (s, addr, length); } \ | ||
228 | \ | ||
229 | /* FIXME: GNU Pth is missing pth_sendmsg and pth_recvmsg. */ \ | ||
230 | static struct gcry_thread_cbs gcry_threads_pth = { GCRY_THREAD_OPTION_PTH, \ | ||
231 | gcry_pth_init, gcry_pth_mutex_init, gcry_pth_mutex_destroy, \ | ||
232 | gcry_pth_mutex_lock, gcry_pth_mutex_unlock, gcry_pth_read, gcry_pth_write, \ | ||
233 | gcry_pth_select, gcry_pth_waitpid, gcry_pth_accept, gcry_pth_connect } | ||
234 | |||
235 | #define GCRY_THREAD_OPTION_PTHREAD_IMPL \ | ||
236 | static int gcry_pthread_mutex_init (void **priv) \ | ||
237 | { \ | ||
238 | int err = 0; \ | ||
239 | pthread_mutex_t *lock = malloc (sizeof (pthread_mutex_t)); \ | ||
240 | \ | ||
241 | if (!lock) \ | ||
242 | err = ENOMEM; \ | ||
243 | if (!err) \ | ||
244 | { \ | ||
245 | err = pthread_mutex_init (lock, NULL); \ | ||
246 | if (err) \ | ||
247 | free (lock); \ | ||
248 | else \ | ||
249 | *priv = lock; \ | ||
250 | } \ | ||
251 | return err; \ | ||
252 | } \ | ||
253 | static int gcry_pthread_mutex_destroy (void **lock) \ | ||
254 | { int err = pthread_mutex_destroy (*lock); free (*lock); return err; } \ | ||
255 | static int gcry_pthread_mutex_lock (void **lock) \ | ||
256 | { return pthread_mutex_lock (*lock); } \ | ||
257 | static int gcry_pthread_mutex_unlock (void **lock) \ | ||
258 | { return pthread_mutex_unlock (*lock); } \ | ||
259 | \ | ||
260 | static struct gcry_thread_cbs gcry_threads_pthread = \ | ||
261 | { GCRY_THREAD_OPTION_PTHREAD, NULL, \ | ||
262 | gcry_pthread_mutex_init, gcry_pthread_mutex_destroy, \ | ||
263 | gcry_pthread_mutex_lock, gcry_pthread_mutex_unlock } | ||
264 | |||
265 | |||
266 | /* The data object used to hold a multi precision integer. */ | ||
267 | struct gcry_mpi; | ||
268 | typedef struct gcry_mpi *gcry_mpi_t; | ||
269 | |||
270 | typedef struct gcry_mpi *GCRY_MPI _GCRY_GCC_ATTR_DEPRECATED; | ||
271 | typedef struct gcry_mpi *GcryMPI _GCRY_GCC_ATTR_DEPRECATED; | ||
272 | |||
273 | |||
274 | |||
275 | /* Check that the library fulfills the version requirement. */ | ||
276 | const char *gcry_check_version (const char *req_version); | ||
277 | |||
278 | /* Codes for function dispatchers. */ | ||
279 | |||
280 | /* Codes used with the gcry_control function. */ | ||
281 | enum gcry_ctl_cmds | ||
282 | { | ||
283 | GCRYCTL_SET_KEY = 1, | ||
284 | GCRYCTL_SET_IV = 2, | ||
285 | GCRYCTL_CFB_SYNC = 3, | ||
286 | GCRYCTL_RESET = 4, /* e.g. for MDs */ | ||
287 | GCRYCTL_FINALIZE = 5, | ||
288 | GCRYCTL_GET_KEYLEN = 6, | ||
289 | GCRYCTL_GET_BLKLEN = 7, | ||
290 | GCRYCTL_TEST_ALGO = 8, | ||
291 | GCRYCTL_IS_SECURE = 9, | ||
292 | GCRYCTL_GET_ASNOID = 10, | ||
293 | GCRYCTL_ENABLE_ALGO = 11, | ||
294 | GCRYCTL_DISABLE_ALGO = 12, | ||
295 | GCRYCTL_DUMP_RANDOM_STATS = 13, | ||
296 | GCRYCTL_DUMP_SECMEM_STATS = 14, | ||
297 | GCRYCTL_GET_ALGO_NPKEY = 15, | ||
298 | GCRYCTL_GET_ALGO_NSKEY = 16, | ||
299 | GCRYCTL_GET_ALGO_NSIGN = 17, | ||
300 | GCRYCTL_GET_ALGO_NENCR = 18, | ||
301 | GCRYCTL_SET_VERBOSITY = 19, | ||
302 | GCRYCTL_SET_DEBUG_FLAGS = 20, | ||
303 | GCRYCTL_CLEAR_DEBUG_FLAGS = 21, | ||
304 | GCRYCTL_USE_SECURE_RNDPOOL= 22, | ||
305 | GCRYCTL_DUMP_MEMORY_STATS = 23, | ||
306 | GCRYCTL_INIT_SECMEM = 24, | ||
307 | GCRYCTL_TERM_SECMEM = 25, | ||
308 | GCRYCTL_DISABLE_SECMEM_WARN = 27, | ||
309 | GCRYCTL_SUSPEND_SECMEM_WARN = 28, | ||
310 | GCRYCTL_RESUME_SECMEM_WARN = 29, | ||
311 | GCRYCTL_DROP_PRIVS = 30, | ||
312 | GCRYCTL_ENABLE_M_GUARD = 31, | ||
313 | GCRYCTL_START_DUMP = 32, | ||
314 | GCRYCTL_STOP_DUMP = 33, | ||
315 | GCRYCTL_GET_ALGO_USAGE = 34, | ||
316 | GCRYCTL_IS_ALGO_ENABLED = 35, | ||
317 | GCRYCTL_DISABLE_INTERNAL_LOCKING = 36, | ||
318 | GCRYCTL_DISABLE_SECMEM = 37, | ||
319 | GCRYCTL_INITIALIZATION_FINISHED = 38, | ||
320 | GCRYCTL_INITIALIZATION_FINISHED_P = 39, | ||
321 | GCRYCTL_ANY_INITIALIZATION_P = 40, | ||
322 | GCRYCTL_SET_CBC_CTS = 41, | ||
323 | GCRYCTL_SET_CBC_MAC = 42, | ||
324 | GCRYCTL_SET_CTR = 43, | ||
325 | GCRYCTL_ENABLE_QUICK_RANDOM = 44, | ||
326 | GCRYCTL_SET_RANDOM_SEED_FILE = 45, | ||
327 | GCRYCTL_UPDATE_RANDOM_SEED_FILE = 46, | ||
328 | GCRYCTL_SET_THREAD_CBS = 47 | ||
329 | }; | ||
330 | |||
331 | /* Perform various operations defined by CMD. */ | ||
332 | gcry_error_t gcry_control (enum gcry_ctl_cmds CMD, ...); | ||
333 | |||
334 | |||
335 | /* S-expression management. */ | ||
336 | |||
337 | /* The object to represent an S-expression as used with the public key | ||
338 | functions. */ | ||
339 | struct gcry_sexp; | ||
340 | typedef struct gcry_sexp *gcry_sexp_t; | ||
341 | |||
342 | typedef struct gcry_sexp *GCRY_SEXP _GCRY_GCC_ATTR_DEPRECATED; | ||
343 | typedef struct gcry_sexp *GcrySexp _GCRY_GCC_ATTR_DEPRECATED; | ||
344 | |||
345 | /* The possible values for the S-expression format. */ | ||
346 | enum gcry_sexp_format | ||
347 | { | ||
348 | GCRYSEXP_FMT_DEFAULT = 0, | ||
349 | GCRYSEXP_FMT_CANON = 1, | ||
350 | GCRYSEXP_FMT_BASE64 = 2, | ||
351 | GCRYSEXP_FMT_ADVANCED = 3 | ||
352 | }; | ||
353 | |||
354 | /* Create an new S-expression object from BUFFER of size LENGTH and | ||
355 | return it in RETSEXP. With AUTODETECT set to 0 the data in BUFFER | ||
356 | is expected to be in canonized format. */ | ||
357 | gcry_error_t gcry_sexp_new (gcry_sexp_t *retsexp, | ||
358 | const void *buffer, size_t length, | ||
359 | int autodetect); | ||
360 | |||
361 | /* Same as gcry_sexp_new but allows to pass a FREEFNC which has the | ||
362 | effect to transfer ownership of BUFFER to the created object. */ | ||
363 | gcry_error_t gcry_sexp_create (gcry_sexp_t *retsexp, | ||
364 | void *buffer, size_t length, | ||
365 | int autodetect, void (*freefnc) (void *)); | ||
366 | |||
367 | /* Scan BUFFER and return a new S-expression object in RETSEXP. This | ||
368 | function expects a printf like string in BUFFER. */ | ||
369 | gcry_error_t gcry_sexp_sscan (gcry_sexp_t *retsexp, size_t *erroff, | ||
370 | const char *buffer, size_t length); | ||
371 | |||
372 | /* Same as gcry_sexp_sscan but expects a string in FORMAT and can thus | ||
373 | only be used for certain encodings. */ | ||
374 | gcry_error_t gcry_sexp_build (gcry_sexp_t *retsexp, size_t *erroff, | ||
375 | const char *format, ...); | ||
376 | |||
377 | /* Like gcry_sexp_build, but uses an array instead of variable | ||
378 | function arguments. */ | ||
379 | gcry_error_t gcry_sexp_build_array (gcry_sexp_t *retsexp, size_t *erroff, | ||
380 | const char *format, void **arg_list); | ||
381 | |||
382 | /* Release the S-expression object SEXP */ | ||
383 | void gcry_sexp_release (gcry_sexp_t sexp); | ||
384 | |||
385 | /* Calculate the length of an canonized S-expresion in BUFFER and | ||
386 | check for a valid encoding. */ | ||
387 | size_t gcry_sexp_canon_len (const unsigned char *buffer, size_t length, | ||
388 | size_t *erroff, gcry_error_t *errcode); | ||
389 | |||
390 | /* Copies the S-expression object SEXP into BUFFER using the format | ||
391 | specified in MODE. */ | ||
392 | size_t gcry_sexp_sprint (gcry_sexp_t sexp, int mode, char *buffer, | ||
393 | size_t maxlength); | ||
394 | |||
395 | /* Dumps the S-expression object A in a aformat suitable for debugging | ||
396 | to Libgcrypt's logging stream. */ | ||
397 | void gcry_sexp_dump (const gcry_sexp_t a); | ||
398 | |||
399 | gcry_sexp_t gcry_sexp_cons (const gcry_sexp_t a, const gcry_sexp_t b); | ||
400 | gcry_sexp_t gcry_sexp_alist (const gcry_sexp_t *array); | ||
401 | gcry_sexp_t gcry_sexp_vlist (const gcry_sexp_t a, ...); | ||
402 | gcry_sexp_t gcry_sexp_append (const gcry_sexp_t a, const gcry_sexp_t n); | ||
403 | gcry_sexp_t gcry_sexp_prepend (const gcry_sexp_t a, const gcry_sexp_t n); | ||
404 | |||
405 | /* Scan the S-expression for a sublist with a type (the car of the | ||
406 | list) matching the string TOKEN. If TOKLEN is not 0, the token is | ||
407 | assumed to be raw memory of this length. The function returns a | ||
408 | newly allocated S-expression consisting of the found sublist or | ||
409 | `NULL' when not found. */ | ||
410 | gcry_sexp_t gcry_sexp_find_token (gcry_sexp_t list, | ||
411 | const char *tok, size_t toklen); | ||
412 | /* Return the length of the LIST. For a valid S-expression this | ||
413 | should be at least 1. */ | ||
414 | int gcry_sexp_length (const gcry_sexp_t list); | ||
415 | |||
416 | /* Create and return a new S-expression from the element with index | ||
417 | NUMBER in LIST. Note that the first element has the index 0. If | ||
418 | there is no such element, `NULL' is returned. */ | ||
419 | gcry_sexp_t gcry_sexp_nth (const gcry_sexp_t list, int number); | ||
420 | |||
421 | /* Create and return a new S-expression from the first element in | ||
422 | LIST; this called the "type" and should always exist and be a | ||
423 | string. `NULL' is returned in case of a problem. */ | ||
424 | gcry_sexp_t gcry_sexp_car (const gcry_sexp_t list); | ||
425 | |||
426 | /* Create and return a new list form all elements except for the first | ||
427 | one. Note, that this function may return an invalid S-expression | ||
428 | because it is not guaranteed, that the type exists and is a string. | ||
429 | However, for parsing a complex S-expression it might be useful for | ||
430 | intermediate lists. Returns `NULL' on error. */ | ||
431 | gcry_sexp_t gcry_sexp_cdr (const gcry_sexp_t list); | ||
432 | |||
433 | gcry_sexp_t gcry_sexp_cadr (const gcry_sexp_t list); | ||
434 | |||
435 | |||
436 | /* This function is used to get data from a LIST. A pointer to the | ||
437 | actual data with index NUMBER is returned and the length of this | ||
438 | data will be stored to DATALEN. If there is no data at the given | ||
439 | index or the index represents another list, `NULL' is returned. | ||
440 | *Note:* The returned pointer is valid as long as LIST is not | ||
441 | modified or released. */ | ||
442 | const char *gcry_sexp_nth_data (const gcry_sexp_t list, int number, | ||
443 | size_t *datalen); | ||
444 | |||
445 | /* This function is used to get and convert data from a LIST. This | ||
446 | data is assumed to be an MPI stored in the format described by | ||
447 | MPIFMT and returned as a standard Libgcrypt MPI. The caller must | ||
448 | release this returned value using `gcry_mpi_release'. If there is | ||
449 | no data at the given index, the index represents a list or the | ||
450 | value can't be converted to an MPI, `NULL' is returned. */ | ||
451 | gcry_mpi_t gcry_sexp_nth_mpi (gcry_sexp_t list, int number, int mpifmt); | ||
452 | |||
453 | |||
454 | |||
455 | /******************************************* | ||
456 | * * | ||
457 | * multi precision integer functions * | ||
458 | * * | ||
459 | *******************************************/ | ||
460 | |||
461 | /* Different formats of external big integer representation. */ | ||
462 | enum gcry_mpi_format | ||
463 | { | ||
464 | GCRYMPI_FMT_NONE= 0, | ||
465 | GCRYMPI_FMT_STD = 1, /* twos complement stored without length */ | ||
466 | GCRYMPI_FMT_PGP = 2, /* As used by OpenPGP (only defined as unsigned)*/ | ||
467 | GCRYMPI_FMT_SSH = 3, /* As used by SSH (same as 1 but with length)*/ | ||
468 | GCRYMPI_FMT_HEX = 4, /* hex format */ | ||
469 | GCRYMPI_FMT_USG = 5 /* like STD but this is an unsigned one */ | ||
470 | }; | ||
471 | |||
472 | /* Flags used for creating big integers. */ | ||
473 | enum gcry_mpi_flag | ||
474 | { | ||
475 | GCRYMPI_FLAG_SECURE = 1, /* Allocate the number in "secure" memory. */ | ||
476 | GCRYMPI_FLAG_OPAQUE = 2 /* The number is not a real one but just a | ||
477 | way to store some bytes. This is | ||
478 | useful for encrypted big integers. */ | ||
479 | }; | ||
480 | |||
481 | |||
482 | /* Allocate a new big integer object, initialize it with 0 and | ||
483 | initially allocate memory for a number of at least NBITS. */ | ||
484 | gcry_mpi_t gcry_mpi_new (unsigned int nbits); | ||
485 | |||
486 | /* Same as gcry_mpi_new() but allocate in "secure" memory. */ | ||
487 | gcry_mpi_t gcry_mpi_snew (unsigned int nbits); | ||
488 | |||
489 | /* Release the number A and free all associated resources. */ | ||
490 | void gcry_mpi_release (gcry_mpi_t a); | ||
491 | |||
492 | /* Create a new number with the same value as A. */ | ||
493 | gcry_mpi_t gcry_mpi_copy (const gcry_mpi_t a); | ||
494 | |||
495 | /* Store the big integer value U in W. */ | ||
496 | gcry_mpi_t gcry_mpi_set (gcry_mpi_t w, const gcry_mpi_t u); | ||
497 | |||
498 | /* Store the unsigned integer value U in W. */ | ||
499 | gcry_mpi_t gcry_mpi_set_ui (gcry_mpi_t w, unsigned long u); | ||
500 | |||
501 | /* Swap the values of A and B. */ | ||
502 | void gcry_mpi_swap (gcry_mpi_t a, gcry_mpi_t b); | ||
503 | |||
504 | /* Compare the big integer number U and V returning 0 for equality, a | ||
505 | positive value for U > V and a negative for U < V. */ | ||
506 | int gcry_mpi_cmp (const gcry_mpi_t u, const gcry_mpi_t v); | ||
507 | |||
508 | /* Compare the big integer number U with the unsigned integer V | ||
509 | returning 0 for equality, a positive value for U > V and a negative | ||
510 | for U < V. */ | ||
511 | int gcry_mpi_cmp_ui (const gcry_mpi_t u, unsigned long v); | ||
512 | |||
513 | /* Convert the external representation of an integer stored in BUFFER | ||
514 | with a length of BUFLEN into a newly create MPI returned in | ||
515 | RET_MPI. If NSCANNED is not NULL, it will receive the number of | ||
516 | bytes actually scanned after a successful operation. */ | ||
517 | gcry_error_t gcry_mpi_scan (gcry_mpi_t *ret_mpi, enum gcry_mpi_format format, | ||
518 | const unsigned char *buffer, size_t buflen, | ||
519 | size_t *nscanned); | ||
520 | |||
521 | /* Convert the big integer A into the external representation | ||
522 | described by FORMAT and store it in the provided BUFFER which has | ||
523 | been allocated by the user with a size of BUFLEN bytes. NWRITTEN | ||
524 | receives the actual length of the external representation unless it | ||
525 | has been passed as NULL. */ | ||
526 | gcry_error_t gcry_mpi_print (enum gcry_mpi_format format, | ||
527 | unsigned char *buffer, size_t buflen, | ||
528 | size_t *nwritten, | ||
529 | const gcry_mpi_t a); | ||
530 | |||
531 | /* Convert the big integer A int the external representation described | ||
532 | by FORMAT and store it in a newly allocated buffer which address | ||
533 | will be put into BUFFER. NWRITTEN receives the actual lengths of the | ||
534 | external representation. */ | ||
535 | gcry_error_t gcry_mpi_aprint (enum gcry_mpi_format format, | ||
536 | unsigned char **buffer, size_t *nwritten, | ||
537 | const gcry_mpi_t a); | ||
538 | |||
539 | /* Dump the value of A in a format suitable for debugging to | ||
540 | Libgcrypt's logging stream. Note that one leading space but no | ||
541 | trailing space or linefeed will be printed. It is okay to pass | ||
542 | NULL for A. */ | ||
543 | void gcry_mpi_dump (const gcry_mpi_t a); | ||
544 | |||
545 | |||
546 | /* W = U + V. */ | ||
547 | void gcry_mpi_add (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v); | ||
548 | |||
549 | /* W = U + V. V is an unsigned integer. */ | ||
550 | void gcry_mpi_add_ui (gcry_mpi_t w, gcry_mpi_t u, unsigned long v); | ||
551 | |||
552 | /* W = U + V mod M. */ | ||
553 | void gcry_mpi_addm (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m); | ||
554 | |||
555 | /* W = U - V. */ | ||
556 | void gcry_mpi_sub (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v); | ||
557 | |||
558 | /* W = U - V. V is an unsigned integer. */ | ||
559 | void gcry_mpi_sub_ui (gcry_mpi_t w, gcry_mpi_t u, unsigned long v ); | ||
560 | |||
561 | /* W = U - V mod M */ | ||
562 | void gcry_mpi_subm (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m); | ||
563 | |||
564 | /* W = U * V. */ | ||
565 | void gcry_mpi_mul (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v); | ||
566 | |||
567 | /* W = U * V. V is an unsigned integer. */ | ||
568 | void gcry_mpi_mul_ui (gcry_mpi_t w, gcry_mpi_t u, unsigned long v ); | ||
569 | |||
570 | /* W = U * V mod M. */ | ||
571 | void gcry_mpi_mulm (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, gcry_mpi_t m); | ||
572 | |||
573 | /* W = U * (2 ^ CNT). */ | ||
574 | void gcry_mpi_mul_2exp (gcry_mpi_t w, gcry_mpi_t u, unsigned long cnt); | ||
575 | |||
576 | /* Q = DIVIDEND / DIVISOR, R = DIVIDEND % DIVISOR, | ||
577 | Q or R may be passed as NULL. ROUND should be negative or 0. */ | ||
578 | void gcry_mpi_div (gcry_mpi_t q, gcry_mpi_t r, | ||
579 | gcry_mpi_t dividend, gcry_mpi_t divisor, int round); | ||
580 | |||
581 | /* R = DIVIDEND % DIVISOR */ | ||
582 | void gcry_mpi_mod (gcry_mpi_t r, gcry_mpi_t dividend, gcry_mpi_t divisor); | ||
583 | |||
584 | /* W = B ^ E mod M. */ | ||
585 | void gcry_mpi_powm (gcry_mpi_t w, | ||
586 | const gcry_mpi_t b, const gcry_mpi_t e, | ||
587 | const gcry_mpi_t m); | ||
588 | |||
589 | /* Set G to the greatest common divisor of A and B. | ||
590 | Return true if the G is 1. */ | ||
591 | int gcry_mpi_gcd (gcry_mpi_t g, gcry_mpi_t a, gcry_mpi_t b); | ||
592 | |||
593 | /* Set X to the multiplicative inverse of A mod M. | ||
594 | Return true if the value exists. */ | ||
595 | int gcry_mpi_invm (gcry_mpi_t x, gcry_mpi_t a, gcry_mpi_t m); | ||
596 | |||
597 | |||
598 | /* Return the number of bits required to represent A. */ | ||
599 | unsigned int gcry_mpi_get_nbits (gcry_mpi_t a); | ||
600 | |||
601 | /* Return true when bit number N (counting from 0) is set in A. */ | ||
602 | int gcry_mpi_test_bit (gcry_mpi_t a, unsigned int n); | ||
603 | |||
604 | /* Set bit number N in A. */ | ||
605 | void gcry_mpi_set_bit (gcry_mpi_t a, unsigned int n); | ||
606 | |||
607 | /* Clear bit number N in A. */ | ||
608 | void gcry_mpi_clear_bit (gcry_mpi_t a, unsigned int n); | ||
609 | |||
610 | /* Set bit number N in A and clear all bits greater than N. */ | ||
611 | void gcry_mpi_set_highbit (gcry_mpi_t a, unsigned int n); | ||
612 | |||
613 | /* Clear bit number N in A and all bits greater than N. */ | ||
614 | void gcry_mpi_clear_highbit (gcry_mpi_t a, unsigned int n); | ||
615 | |||
616 | /* Shift the value of A by N bits to the right and store the result in X. */ | ||
617 | void gcry_mpi_rshift (gcry_mpi_t x, gcry_mpi_t a, unsigned int n); | ||
618 | |||
619 | /* Store NBITS of the value P points to in A and mark A as an opaque | ||
620 | value. WARNING: Never use an opaque MPI for anything thing else then | ||
621 | gcry_mpi_release, gcry_mpi_get_opaque. */ | ||
622 | gcry_mpi_t gcry_mpi_set_opaque (gcry_mpi_t a, void *p, unsigned int nbits); | ||
623 | |||
624 | /* Return a pointer to an opaque value stored in A and return its size | ||
625 | in NBITS. Note that the returned pointer is still owned by A and | ||
626 | that the function should never be used for an non-opaque MPI. */ | ||
627 | void *gcry_mpi_get_opaque (gcry_mpi_t a, unsigned int *nbits); | ||
628 | |||
629 | /* Set the FLAG for the big integer A. Currently only the flag | ||
630 | GCRYMPI_FLAG_SECURE is allowed to convert A into an big intger | ||
631 | stored in "secure" memory. */ | ||
632 | void gcry_mpi_set_flag (gcry_mpi_t a, enum gcry_mpi_flag flag); | ||
633 | |||
634 | /* Clear FLAG for the big integer A. Note that this function is | ||
635 | currently useless as no flags are allowed. */ | ||
636 | void gcry_mpi_clear_flag (gcry_mpi_t a, enum gcry_mpi_flag flag); | ||
637 | |||
638 | /* Return true when the FLAG is set for A. */ | ||
639 | int gcry_mpi_get_flag (gcry_mpi_t a, enum gcry_mpi_flag flag); | ||
640 | |||
641 | /* Unless the GCRYPT_NO_MPI_MACROS is used, provide a couple of | ||
642 | convenience macors for the big integer functions. */ | ||
643 | #ifndef GCRYPT_NO_MPI_MACROS | ||
644 | #define mpi_new(n) gcry_mpi_new( (n) ) | ||
645 | #define mpi_secure_new( n ) gcry_mpi_snew( (n) ) | ||
646 | #define mpi_release(a) \ | ||
647 | do \ | ||
648 | { \ | ||
649 | gcry_mpi_release ((a)); \ | ||
650 | (a) = NULL; \ | ||
651 | } \ | ||
652 | while (0) | ||
653 | |||
654 | #define mpi_copy( a ) gcry_mpi_copy( (a) ) | ||
655 | #define mpi_set( w, u) gcry_mpi_set( (w), (u) ) | ||
656 | #define mpi_set_ui( w, u) gcry_mpi_set_ui( (w), (u) ) | ||
657 | #define mpi_cmp( u, v ) gcry_mpi_cmp( (u), (v) ) | ||
658 | #define mpi_cmp_ui( u, v ) gcry_mpi_cmp_ui( (u), (v) ) | ||
659 | |||
660 | #define mpi_add_ui(w,u,v) gcry_mpi_add_ui((w),(u),(v)) | ||
661 | #define mpi_add(w,u,v) gcry_mpi_add ((w),(u),(v)) | ||
662 | #define mpi_addm(w,u,v,m) gcry_mpi_addm ((w),(u),(v),(m)) | ||
663 | #define mpi_sub_ui(w,u,v) gcry_mpi_sub_ui ((w),(u),(v)) | ||
664 | #define mpi_sub(w,u,v) gcry_mpi_sub ((w),(u),(v)) | ||
665 | #define mpi_subm(w,u,v,m) gcry_mpi_subm ((w),(u),(v),(m)) | ||
666 | #define mpi_mul_ui(w,u,v) gcry_mpi_mul_ui ((w),(u),(v)) | ||
667 | #define mpi_mul_2exp(w,u,v) gcry_mpi_mul_2exp ((w),(u),(v)) | ||
668 | #define mpi_mul(w,u,v) gcry_mpi_mul ((w),(u),(v)) | ||
669 | #define mpi_mulm(w,u,v,m) gcry_mpi_mulm ((w),(u),(v),(m)) | ||
670 | #define mpi_powm(w,b,e,m) gcry_mpi_powm ( (w), (b), (e), (m) ) | ||
671 | #define mpi_tdiv(q,r,a,m) gcry_mpi_div ( (q), (r), (a), (m), 0) | ||
672 | #define mpi_fdiv(q,r,a,m) gcry_mpi_div ( (q), (r), (a), (m), -1) | ||
673 | #define mpi_mod(r,a,m) gcry_mpi_mod ((r), (a), (m)) | ||
674 | #define mpi_gcd(g,a,b) gcry_mpi_gcd ( (g), (a), (b) ) | ||
675 | #define mpi_invm(g,a,b) gcry_mpi_invm ( (g), (a), (b) ) | ||
676 | |||
677 | #define mpi_get_nbits(a) gcry_mpi_get_nbits ((a)) | ||
678 | #define mpi_test_bit(a,b) gcry_mpi_test_bit ((a),(b)) | ||
679 | #define mpi_set_bit(a,b) gcry_mpi_set_bit ((a),(b)) | ||
680 | #define mpi_set_highbit(a,b) gcry_mpi_set_highbit ((a),(b)) | ||
681 | #define mpi_clear_bit(a,b) gcry_mpi_clear_bit ((a),(b)) | ||
682 | #define mpi_clear_highbit(a,b) gcry_mpi_clear_highbit ((a),(b)) | ||
683 | #define mpi_rshift(a,b,c) gcry_mpi_rshift ((a),(b),(c)) | ||
684 | |||
685 | #define mpi_set_opaque(a,b,c) gcry_mpi_set_opaque( (a), (b), (c) ) | ||
686 | #define mpi_get_opaque(a,b) gcry_mpi_get_opaque( (a), (b) ) | ||
687 | #endif /* GCRYPT_NO_MPI_MACROS */ | ||
688 | |||
689 | |||
690 | |||
691 | /************************************ | ||
692 | * * | ||
693 | * symmetric cipher functions * | ||
694 | * * | ||
695 | ************************************/ | ||
696 | |||
697 | /* The data object used to hold a handle to an encryption object. */ | ||
698 | struct gcry_cipher_handle; | ||
699 | typedef struct gcry_cipher_handle *gcry_cipher_hd_t; | ||
700 | |||
701 | typedef struct gcry_cipher_handle *GCRY_CIPHER_HD _GCRY_GCC_ATTR_DEPRECATED; | ||
702 | typedef struct gcry_cipher_handle *GcryCipherHd _GCRY_GCC_ATTR_DEPRECATED; | ||
703 | |||
704 | /* All symmetric encryption algorithms are identified by their IDs. | ||
705 | More IDs may be registered at runtime. */ | ||
706 | enum gcry_cipher_algos | ||
707 | { | ||
708 | GCRY_CIPHER_NONE = 0, | ||
709 | GCRY_CIPHER_IDEA = 1, | ||
710 | GCRY_CIPHER_3DES = 2, | ||
711 | GCRY_CIPHER_CAST5 = 3, | ||
712 | GCRY_CIPHER_BLOWFISH = 4, | ||
713 | GCRY_CIPHER_SAFER_SK128 = 5, | ||
714 | GCRY_CIPHER_DES_SK = 6, | ||
715 | GCRY_CIPHER_AES = 7, | ||
716 | GCRY_CIPHER_AES192 = 8, | ||
717 | GCRY_CIPHER_AES256 = 9, | ||
718 | GCRY_CIPHER_TWOFISH = 10, | ||
719 | |||
720 | /* Other cipher numbers are above 300 for OpenPGP reasons. */ | ||
721 | GCRY_CIPHER_ARCFOUR = 301, /* Fully compatible with RSA's RC4 (tm). */ | ||
722 | GCRY_CIPHER_DES = 302, /* Yes, this is single key 56 bit DES. */ | ||
723 | GCRY_CIPHER_TWOFISH128 = 303, | ||
724 | GCRY_CIPHER_SERPENT128 = 304, | ||
725 | GCRY_CIPHER_SERPENT192 = 305, | ||
726 | GCRY_CIPHER_SERPENT256 = 306, | ||
727 | GCRY_CIPHER_RFC2268_40 = 307, /* Ron's Cipher 2 (40 bit). */ | ||
728 | GCRY_CIPHER_RFC2268_128 = 308, /* Ron's Cipher 2 (128 bit). */ | ||
729 | }; | ||
730 | |||
731 | /* The Rijndael algorithm is basically AES, so provide some macros. */ | ||
732 | #define GCRY_CIPHER_AES128 GCRY_CIPHER_AES | ||
733 | #define GCRY_CIPHER_RIJNDAEL GCRY_CIPHER_AES | ||
734 | #define GCRY_CIPHER_RIJNDAEL128 GCRY_CIPHER_AES128 | ||
735 | #define GCRY_CIPHER_RIJNDAEL192 GCRY_CIPHER_AES192 | ||
736 | #define GCRY_CIPHER_RIJNDAEL256 GCRY_CIPHER_AES256 | ||
737 | |||
738 | /* The supported encryption modes. Note that not all of them are | ||
739 | supported for each algorithm. */ | ||
740 | enum gcry_cipher_modes | ||
741 | { | ||
742 | GCRY_CIPHER_MODE_NONE = 0, /* Not yet specified. */ | ||
743 | GCRY_CIPHER_MODE_ECB = 1, /* Electronic codebook. */ | ||
744 | GCRY_CIPHER_MODE_CFB = 2, /* Cipher feedback. */ | ||
745 | GCRY_CIPHER_MODE_CBC = 3, /* Cipher block chaining. */ | ||
746 | GCRY_CIPHER_MODE_STREAM = 4, /* Used with stream ciphers. */ | ||
747 | GCRY_CIPHER_MODE_OFB = 5, /* Outer feedback. */ | ||
748 | GCRY_CIPHER_MODE_CTR = 6 /* Counter. */ | ||
749 | }; | ||
750 | |||
751 | /* Flags used with the open function. */ | ||
752 | enum gcry_cipher_flags | ||
753 | { | ||
754 | GCRY_CIPHER_SECURE = 1, /* Allocate in secure memory. */ | ||
755 | GCRY_CIPHER_ENABLE_SYNC = 2, /* Enable CFB sync mode. */ | ||
756 | GCRY_CIPHER_CBC_CTS = 4, /* Enable CBC cipher text stealing (CTS). */ | ||
757 | GCRY_CIPHER_CBC_MAC = 8 /* Enable CBC message auth. code (MAC). */ | ||
758 | }; | ||
759 | |||
760 | |||
761 | /* Create a handle for algorithm ALGO to be used in MODE. FLAGS may | ||
762 | be given as an bitwise OR of the gcry_cipher_flags values. */ | ||
763 | gcry_error_t gcry_cipher_open (gcry_cipher_hd_t *handle, | ||
764 | int algo, int mode, unsigned int flags); | ||
765 | |||
766 | /* Close the cioher handle H and release all resource. */ | ||
767 | void gcry_cipher_close (gcry_cipher_hd_t h); | ||
768 | |||
769 | /* Perform various operations on the cipher object H. */ | ||
770 | gcry_error_t gcry_cipher_ctl (gcry_cipher_hd_t h, int cmd, void *buffer, | ||
771 | size_t buflen); | ||
772 | |||
773 | /* Retrieve various information about the cipher object H. */ | ||
774 | gcry_error_t gcry_cipher_info (gcry_cipher_hd_t h, int what, void *buffer, | ||
775 | size_t *nbytes); | ||
776 | |||
777 | /* Retrieve various information about the cipher algorithm ALGO. */ | ||
778 | gcry_error_t gcry_cipher_algo_info (int algo, int what, void *buffer, | ||
779 | size_t *nbytes); | ||
780 | |||
781 | /* Map the cipher algorithm id ALGO to a string representation of that | ||
782 | algorithm name. For unknown algorithms this functions returns an | ||
783 | empty string. */ | ||
784 | const char *gcry_cipher_algo_name (int algo) _GCRY_GCC_ATTR_PURE; | ||
785 | |||
786 | /* Map the algorithm name NAME to an cipher algorithm ID. Return 0 if | ||
787 | the algorithm name is not known. */ | ||
788 | int gcry_cipher_map_name (const char *name) _GCRY_GCC_ATTR_PURE; | ||
789 | |||
790 | /* Given an ASN.1 object identifier in standard IETF dotted decimal | ||
791 | format in STRING, return the encryption mode associated with that | ||
792 | OID or 0 if not known or applicable. */ | ||
793 | int gcry_cipher_mode_from_oid (const char *string) _GCRY_GCC_ATTR_PURE; | ||
794 | |||
795 | /* Encrypt the plaintext of size INLEN in IN using the cipher handle H | ||
796 | into the buffer OUT which has an allocated length of OUTSIZE. For | ||
797 | most algorithms it is possible to pass NULL for in and 0 for INLEN | ||
798 | and do a in-place decryption of the data provided in OUT. */ | ||
799 | gcry_error_t gcry_cipher_encrypt (gcry_cipher_hd_t h, | ||
800 | unsigned char *out, size_t outsize, | ||
801 | const unsigned char *in, size_t inlen); | ||
802 | |||
803 | /* The counterpart to gcry_cipher_encrypt. */ | ||
804 | gcry_error_t gcry_cipher_decrypt (gcry_cipher_hd_t h, | ||
805 | unsigned char *out, size_t outsize, | ||
806 | const unsigned char *in, size_t inlen); | ||
807 | |||
808 | /* Set key K of length L for the cipher handle H. (We have to cast | ||
809 | away a const char* here - this catch-all ctl function was probably | ||
810 | not the best choice) */ | ||
811 | #define gcry_cipher_setkey(h,k,l) gcry_cipher_ctl( (h), GCRYCTL_SET_KEY, \ | ||
812 | (char*)(k), (l) ) | ||
813 | |||
814 | /* Set initialization vector K of length L for the cipher handle H. */ | ||
815 | #define gcry_cipher_setiv(h,k,l) gcry_cipher_ctl( (h), GCRYCTL_SET_IV, \ | ||
816 | (char*)(k), (l) ) | ||
817 | |||
818 | /* Reset the handle to the state after open. */ | ||
819 | #define gcry_cipher_reset(h) gcry_cipher_ctl ((h), GCRYCTL_RESET, NULL, 0) | ||
820 | |||
821 | /* Perform the the OpenPGP sync operation if this is enabled for the | ||
822 | cipher handle H. */ | ||
823 | #define gcry_cipher_sync(h) gcry_cipher_ctl( (h), GCRYCTL_CFB_SYNC, \ | ||
824 | NULL, 0 ) | ||
825 | |||
826 | /* Enable or disable CTS in future calls to gcry_encrypt(). CBC mode only. */ | ||
827 | #define gcry_cipher_cts(h,on) gcry_cipher_ctl( (h), GCRYCTL_SET_CBC_CTS, \ | ||
828 | NULL, on ) | ||
829 | |||
830 | /* Set counter for CTR mode. (K,L) must denote a buffer of block size | ||
831 | length, or (NULL,0) to set the CTR to the all-zero block. */ | ||
832 | #define gcry_cipher_setctr(h,k,l) gcry_cipher_ctl( (h), GCRYCTL_SET_CTR, \ | ||
833 | (char*)(k), (l) ) | ||
834 | |||
835 | /* Retrieved the key length used with algorithm A. */ | ||
836 | size_t gcry_cipher_get_algo_keylen (int algo); | ||
837 | |||
838 | /* Retrieve the block length used with algorithm A. */ | ||
839 | size_t gcry_cipher_get_algo_blklen (int algo); | ||
840 | |||
841 | /* Return 0 if the algorithm A is available for use. */ | ||
842 | #define gcry_cipher_test_algo(a) \ | ||
843 | gcry_cipher_algo_info( (a), GCRYCTL_TEST_ALGO, NULL, NULL ) | ||
844 | |||
845 | /* Get a list consisting of the IDs of the loaded cipher modules. If | ||
846 | LIST is zero, write the number of loaded cipher modules to | ||
847 | LIST_LENGTH and return. If LIST is non-zero, the first | ||
848 | *LIST_LENGTH algorithm IDs are stored in LIST, which must be of | ||
849 | according size. In case there are less cipher modules than | ||
850 | *LIST_LENGTH, *LIST_LENGTH is updated to the correct number. */ | ||
851 | gcry_error_t gcry_cipher_list (int *list, int *list_length); | ||
852 | |||
853 | |||
854 | /************************************ | ||
855 | * * | ||
856 | * asymmetric cipher functions * | ||
857 | * * | ||
858 | ************************************/ | ||
859 | |||
860 | /* The algorithms and their IDs we support. */ | ||
861 | enum gcry_pk_algos | ||
862 | { | ||
863 | GCRY_PK_RSA = 1, | ||
864 | GCRY_PK_RSA_E = 2, /* deprecated */ | ||
865 | GCRY_PK_RSA_S = 3, /* deprecated */ | ||
866 | GCRY_PK_ELG_E = 16, /* use only for OpenPGP */ | ||
867 | GCRY_PK_DSA = 17, | ||
868 | GCRY_PK_ELG = 20 | ||
869 | }; | ||
870 | |||
871 | /* Flags describing usage capabilities of a PK algorithm. */ | ||
872 | #define GCRY_PK_USAGE_SIGN 1 | ||
873 | #define GCRY_PK_USAGE_ENCR 2 | ||
874 | |||
875 | /* Encrypt the DATA using the public key PKEY and store the result as | ||
876 | a newly created S-expression at RESULT. */ | ||
877 | gcry_error_t gcry_pk_encrypt (gcry_sexp_t *result, | ||
878 | gcry_sexp_t data, gcry_sexp_t pkey); | ||
879 | |||
880 | /* Decrypt the DATA using the private key SKEY and store the result as | ||
881 | a newly created S-expression at RESULT. */ | ||
882 | gcry_error_t gcry_pk_decrypt (gcry_sexp_t *result, | ||
883 | gcry_sexp_t data, gcry_sexp_t skey); | ||
884 | |||
885 | /* Sign the DATA using the private key SKEY and store the result as | ||
886 | a newly created S-expression at RESULT. */ | ||
887 | gcry_error_t gcry_pk_sign (gcry_sexp_t *result, | ||
888 | gcry_sexp_t data, gcry_sexp_t skey); | ||
889 | |||
890 | /* Check the signature SIGVAL on DATA using the public key PKEY. */ | ||
891 | gcry_error_t gcry_pk_verify (gcry_sexp_t sigval, | ||
892 | gcry_sexp_t data, gcry_sexp_t pkey); | ||
893 | |||
894 | /* Check that private KEY is sane. */ | ||
895 | gcry_error_t gcry_pk_testkey (gcry_sexp_t key); | ||
896 | |||
897 | /* Generate a new key pair according to the parameters given in | ||
898 | S_PARMS. The new key pair is returned in as an S-expression in | ||
899 | R_KEY. */ | ||
900 | gcry_error_t gcry_pk_genkey (gcry_sexp_t *r_key, gcry_sexp_t s_parms); | ||
901 | |||
902 | /* Catch all function for miscellaneous operations. */ | ||
903 | gcry_error_t gcry_pk_ctl (int cmd, void *buffer, size_t buflen); | ||
904 | |||
905 | /* Retrieve information about the public key algorithm ALGO. */ | ||
906 | gcry_error_t gcry_pk_algo_info (int algo, int what, | ||
907 | void *buffer, size_t *nbytes); | ||
908 | |||
909 | /* Map the public key algorithm id ALGO to a string representation of the | ||
910 | algorithm name. For unknown algorithms this functions returns an | ||
911 | empty string. */ | ||
912 | const char *gcry_pk_algo_name (int algo) _GCRY_GCC_ATTR_PURE; | ||
913 | |||
914 | /* Map the algorithm NAME to a public key algorithm Id. Return 0 if | ||
915 | the algorithm name is not known. */ | ||
916 | int gcry_pk_map_name (const char* name) _GCRY_GCC_ATTR_PURE; | ||
917 | |||
918 | /* Return what is commonly referred as the key length for the given | ||
919 | public or private KEY. */ | ||
920 | unsigned int gcry_pk_get_nbits (gcry_sexp_t key) _GCRY_GCC_ATTR_PURE; | ||
921 | |||
922 | /* Please note that keygrip is still experimental and should not be | ||
923 | used without contacting the author. */ | ||
924 | unsigned char *gcry_pk_get_keygrip (gcry_sexp_t key, unsigned char *array); | ||
925 | |||
926 | /* Return 0 if the public key algorithm A is available for use. */ | ||
927 | #define gcry_pk_test_algo(a) \ | ||
928 | gcry_pk_algo_info( (a), GCRYCTL_TEST_ALGO, NULL, NULL ) | ||
929 | |||
930 | /* Get a list consisting of the IDs of the loaded pubkey modules. If | ||
931 | LIST is zero, write the number of loaded pubkey modules to | ||
932 | LIST_LENGTH and return. If LIST is non-zero, the first | ||
933 | *LIST_LENGTH algorithm IDs are stored in LIST, which must be of | ||
934 | according size. In case there are less pubkey modules than | ||
935 | *LIST_LENGTH, *LIST_LENGTH is updated to the correct number. */ | ||
936 | gcry_error_t gcry_pk_list (int *list, int *list_length); | ||
937 | |||
938 | /* Alternative interface for asymetric cryptography. */ | ||
939 | |||
940 | /* The algorithm IDs. */ | ||
941 | typedef enum gcry_ac_id | ||
942 | { | ||
943 | GCRY_AC_RSA = 1, | ||
944 | GCRY_AC_DSA = 17, | ||
945 | GCRY_AC_ELG = 20, | ||
946 | GCRY_AC_ELG_E = 16, | ||
947 | } | ||
948 | gcry_ac_id_t; | ||
949 | |||
950 | /* Key types. */ | ||
951 | typedef enum gcry_ac_key_type | ||
952 | { | ||
953 | GCRY_AC_KEY_SECRET, | ||
954 | GCRY_AC_KEY_PUBLIC, | ||
955 | } | ||
956 | gcry_ac_key_type_t; | ||
957 | |||
958 | /* AC data. */ | ||
959 | #define GCRY_AC_FLAG_DEALLOC (1 << 0) | ||
960 | #define GCRY_AC_FLAG_COPY (1 << 1) | ||
961 | #define GCRY_AC_FLAG_NO_BLINDING (1 << 2) | ||
962 | |||
963 | /* This type represents a `data set'. */ | ||
964 | typedef struct gcry_ac_data *gcry_ac_data_t; | ||
965 | |||
966 | /* This type represents a single `key', either a secret one or a | ||
967 | public one. */ | ||
968 | typedef struct gcry_ac_key *gcry_ac_key_t; | ||
969 | |||
970 | /* This type represents a `key pair' containing a secret and a public | ||
971 | key. */ | ||
972 | typedef struct gcry_ac_key_pair *gcry_ac_key_pair_t; | ||
973 | |||
974 | /* This type represents a `handle' that is needed by functions | ||
975 | performing cryptographic operations. */ | ||
976 | typedef struct gcry_ac_handle *gcry_ac_handle_t; | ||
977 | |||
978 | /* The caller of gcry_ac_key_pair_generate can provide one of these | ||
979 | structures in order to influence the key generation process in an | ||
980 | algorithm-specific way. */ | ||
981 | typedef struct gcry_ac_key_spec_rsa | ||
982 | { | ||
983 | gcry_mpi_t e; /* E to use. */ | ||
984 | } gcry_ac_key_spec_rsa_t; | ||
985 | |||
986 | /* Returns a new, empty data set in DATA. */ | ||
987 | gcry_error_t gcry_ac_data_new (gcry_ac_data_t *data); | ||
988 | |||
989 | /* Destroy the data set DATA. */ | ||
990 | void gcry_ac_data_destroy (gcry_ac_data_t data); | ||
991 | |||
992 | /* Create a copy of the data set DATA and store it in DATA_CP. */ | ||
993 | gcry_error_t gcry_ac_data_copy (gcry_ac_data_t *data_cp, | ||
994 | gcry_ac_data_t data); | ||
995 | |||
996 | /* Return the number of named MPI values inside of the data set | ||
997 | DATA. */ | ||
998 | unsigned int gcry_ac_data_length (gcry_ac_data_t data); | ||
999 | |||
1000 | /* Destroy any values contained in the data set DATA. */ | ||
1001 | void gcry_ac_data_clear (gcry_ac_data_t data); | ||
1002 | |||
1003 | /* Add the value MPI to DATA with the label NAME. If FLAGS contains | ||
1004 | GCRY_AC_FLAG_DATA_COPY, the data set will contain copies of NAME | ||
1005 | and MPI. If FLAGS contains GCRY_AC_FLAG_DATA_DEALLOC or | ||
1006 | GCRY_AC_FLAG_DATA_COPY, the values contained in the data set will | ||
1007 | be deallocated when they are to be removed from the data set. */ | ||
1008 | gcry_error_t gcry_ac_data_set (gcry_ac_data_t data, unsigned int flags, | ||
1009 | const char *name, gcry_mpi_t mpi); | ||
1010 | |||
1011 | /* Store the value labelled with NAME found in DATA in MPI. If FLAGS | ||
1012 | contains GCRY_AC_FLAG_COPY, store a copy of the MPI value contained | ||
1013 | in the data set. MPI may be NULL. */ | ||
1014 | gcry_error_t gcry_ac_data_get_name (gcry_ac_data_t data, unsigned int flags, | ||
1015 | const char *name, gcry_mpi_t *mpi); | ||
1016 | |||
1017 | /* Stores in NAME and MPI the named MPI value contained in the data | ||
1018 | set DATA with the index IDX. If FLAGS contains GCRY_AC_FLAG_COPY, | ||
1019 | store copies of the values contained in the data set. NAME or MPI | ||
1020 | may be NULL. */ | ||
1021 | gcry_error_t gcry_ac_data_get_index (gcry_ac_data_t data, unsigned int flags, | ||
1022 | unsigned int idx, | ||
1023 | const char **name, gcry_mpi_t *mpi); | ||
1024 | |||
1025 | /* Create a new ac handle. */ | ||
1026 | gcry_error_t gcry_ac_open (gcry_ac_handle_t *handle, | ||
1027 | gcry_ac_id_t algorithm, unsigned int flags); | ||
1028 | |||
1029 | /* Destroy an ac handle. */ | ||
1030 | void gcry_ac_close (gcry_ac_handle_t handle); | ||
1031 | |||
1032 | /* Initialize a key from a given data set. */ | ||
1033 | gcry_error_t gcry_ac_key_init (gcry_ac_key_t *key, gcry_ac_handle_t handle, | ||
1034 | gcry_ac_key_type_t type, gcry_ac_data_t data); | ||
1035 | |||
1036 | /* Generates a new key pair via the handle HANDLE of NBITS bits and | ||
1037 | stores it in KEY_PAIR. In case non-standard settings are wanted, a | ||
1038 | pointer to a structure of type gcry_ac_key_spec_<algorithm>_t, | ||
1039 | matching the selected algorithm, can be given as KEY_SPEC. | ||
1040 | MISC_DATA is not used yet. */ | ||
1041 | gcry_error_t gcry_ac_key_pair_generate (gcry_ac_handle_t handle, | ||
1042 | unsigned int nbits, void *spec, | ||
1043 | gcry_ac_key_pair_t *key_pair, | ||
1044 | gcry_mpi_t **misc_data); | ||
1045 | |||
1046 | /* Returns the key of type WHICH out of the key pair KEY_PAIR. */ | ||
1047 | gcry_ac_key_t gcry_ac_key_pair_extract (gcry_ac_key_pair_t key_pair, | ||
1048 | gcry_ac_key_type_t which); | ||
1049 | |||
1050 | /* Returns the data set contained in the key KEY. */ | ||
1051 | gcry_ac_data_t gcry_ac_key_data_get (gcry_ac_key_t key); | ||
1052 | |||
1053 | /* Verifies that the key KEY is sane via HANDLE. */ | ||
1054 | gcry_error_t gcry_ac_key_test (gcry_ac_handle_t handle, gcry_ac_key_t key); | ||
1055 | |||
1056 | /* Stores the number of bits of the key KEY in NBITS via HANDLE. */ | ||
1057 | gcry_error_t gcry_ac_key_get_nbits (gcry_ac_handle_t handle, | ||
1058 | gcry_ac_key_t key, unsigned int *nbits); | ||
1059 | |||
1060 | /* Writes the 20 byte long key grip of the key KEY to KEY_GRIP via | ||
1061 | HANDLE. */ | ||
1062 | gcry_error_t gcry_ac_key_get_grip (gcry_ac_handle_t handle, gcry_ac_key_t key, | ||
1063 | unsigned char *key_grip); | ||
1064 | |||
1065 | /* Destroy a key. */ | ||
1066 | void gcry_ac_key_destroy (gcry_ac_key_t key); | ||
1067 | |||
1068 | /* Destroy a key pair. */ | ||
1069 | void gcry_ac_key_pair_destroy (gcry_ac_key_pair_t key_pair); | ||
1070 | |||
1071 | /* Encrypt the plain text MPI value DATA_PLAIN with the key KEY under | ||
1072 | the control of the flags FLAGS and store the resulting data set | ||
1073 | into DATA_ENCRYPTED. */ | ||
1074 | gcry_error_t gcry_ac_data_encrypt (gcry_ac_handle_t handle, | ||
1075 | unsigned int flags, | ||
1076 | gcry_ac_key_t key, | ||
1077 | gcry_mpi_t data_plain, | ||
1078 | gcry_ac_data_t *data_encrypted); | ||
1079 | |||
1080 | /* Decrypt the decrypted data contained in the data set DATA_ENCRYPTED | ||
1081 | with the key KEY under the control of the flags FLAGS and store the | ||
1082 | resulting plain text MPI value in DATA_PLAIN. */ | ||
1083 | gcry_error_t gcry_ac_data_decrypt (gcry_ac_handle_t handle, | ||
1084 | unsigned int flags, | ||
1085 | gcry_ac_key_t key, | ||
1086 | gcry_mpi_t *data_plain, | ||
1087 | gcry_ac_data_t data_encrypted); | ||
1088 | |||
1089 | /* Sign the data contained in DATA with the key KEY and store the | ||
1090 | resulting signature in the data set DATA_SIGNATURE. */ | ||
1091 | gcry_error_t gcry_ac_data_sign (gcry_ac_handle_t handle, | ||
1092 | gcry_ac_key_t key, | ||
1093 | gcry_mpi_t data, | ||
1094 | gcry_ac_data_t *data_signature); | ||
1095 | |||
1096 | /* Verify that the signature contained in the data set DATA_SIGNATURE | ||
1097 | is indeed the result of signing the data contained in DATA with the | ||
1098 | secret key belonging to the public key KEY. */ | ||
1099 | gcry_error_t gcry_ac_data_verify (gcry_ac_handle_t handle, | ||
1100 | gcry_ac_key_t key, | ||
1101 | gcry_mpi_t data, | ||
1102 | gcry_ac_data_t data_signature); | ||
1103 | |||
1104 | /* Store the textual representation of the algorithm whose id is given | ||
1105 | in ALGORITHM in NAME. */ | ||
1106 | gcry_error_t gcry_ac_id_to_name (gcry_ac_id_t algorithm, | ||
1107 | const char **name); | ||
1108 | |||
1109 | /* Store the numeric ID of the algorithm whose textual representation | ||
1110 | is contained in NAME in ALGORITHM. */ | ||
1111 | gcry_error_t gcry_ac_name_to_id (const char *name, | ||
1112 | gcry_ac_id_t *algorithm); | ||
1113 | |||
1114 | |||
1115 | |||
1116 | /************************************ | ||
1117 | * * | ||
1118 | * cryptograhic hash functions * | ||
1119 | * * | ||
1120 | ************************************/ | ||
1121 | |||
1122 | /* Algorithm IDs for the hash functions we know about. Not all of them | ||
1123 | are implemnted. */ | ||
1124 | enum gcry_md_algos | ||
1125 | { | ||
1126 | GCRY_MD_NONE = 0, | ||
1127 | GCRY_MD_MD5 = 1, | ||
1128 | GCRY_MD_SHA1 = 2, | ||
1129 | GCRY_MD_RMD160 = 3, | ||
1130 | GCRY_MD_MD2 = 5, | ||
1131 | GCRY_MD_TIGER = 6, /* TIGER/192. */ | ||
1132 | GCRY_MD_HAVAL = 7, /* HAVAL, 5 pass, 160 bit. */ | ||
1133 | GCRY_MD_SHA256 = 8, | ||
1134 | GCRY_MD_SHA384 = 9, | ||
1135 | GCRY_MD_SHA512 = 10, | ||
1136 | GCRY_MD_MD4 = 301, | ||
1137 | GCRY_MD_CRC32 = 302, | ||
1138 | GCRY_MD_CRC32_RFC1510 = 303, | ||
1139 | GCRY_MD_CRC24_RFC2440 = 304 | ||
1140 | }; | ||
1141 | |||
1142 | /* Flags used with the open function. */ | ||
1143 | enum gcry_md_flags | ||
1144 | { | ||
1145 | GCRY_MD_FLAG_SECURE = 1, /* Allocate all buffers in "secure" | ||
1146 | memory. */ | ||
1147 | GCRY_MD_FLAG_HMAC = 2 /* Make an HMAC out of this | ||
1148 | algorithm. */ | ||
1149 | }; | ||
1150 | |||
1151 | /* Forward declaration. */ | ||
1152 | struct gcry_md_context; | ||
1153 | |||
1154 | /* This object is used to hold a handle to a message digest object. | ||
1155 | This structure is private - only to be used by the public gcry_md_* | ||
1156 | macros. */ | ||
1157 | typedef struct gcry_md_handle | ||
1158 | { | ||
1159 | /* Actual context. */ | ||
1160 | struct gcry_md_context *ctx; | ||
1161 | |||
1162 | /* Buffer management. */ | ||
1163 | int bufpos; | ||
1164 | int bufsize; | ||
1165 | unsigned char buf[1]; | ||
1166 | } *gcry_md_hd_t; | ||
1167 | |||
1168 | /* Compatibility types, do not use them. */ | ||
1169 | typedef struct gcry_md_handle *GCRY_MD_HD _GCRY_GCC_ATTR_DEPRECATED; | ||
1170 | typedef struct gcry_md_handle *GcryMDHd _GCRY_GCC_ATTR_DEPRECATED; | ||
1171 | |||
1172 | /* Create a message digest object for algorithm ALGO. FLAGS may be | ||
1173 | given as an bitwise OR of the gcry_md_flags values. ALGO may be | ||
1174 | given as 0 if the algorithms to be used are later set using | ||
1175 | gcry_md_enable. */ | ||
1176 | gcry_error_t gcry_md_open (gcry_md_hd_t *h, int algo, unsigned int flags); | ||
1177 | |||
1178 | /* Release the message digest object HD. */ | ||
1179 | void gcry_md_close (gcry_md_hd_t hd); | ||
1180 | |||
1181 | /* Add the message digest algorithm ALGO to the digest object HD. */ | ||
1182 | gcry_error_t gcry_md_enable (gcry_md_hd_t hd, int algo); | ||
1183 | |||
1184 | /* Create a new digest object as an exact copy of the object HD. */ | ||
1185 | gcry_error_t gcry_md_copy (gcry_md_hd_t *bhd, gcry_md_hd_t ahd); | ||
1186 | |||
1187 | /* Reset the digest object HD to its initial state. */ | ||
1188 | void gcry_md_reset (gcry_md_hd_t hd); | ||
1189 | |||
1190 | /* Perform various operations on the digets object HD. */ | ||
1191 | gcry_error_t gcry_md_ctl (gcry_md_hd_t hd, int cmd, unsigned char *buffer, | ||
1192 | size_t buflen); | ||
1193 | |||
1194 | /* Pass LENGTH bytes of data in BUFFER to the digest object HD so that | ||
1195 | it can update the digest values. This is the actual hash | ||
1196 | function. */ | ||
1197 | void gcry_md_write (gcry_md_hd_t hd, const void *buffer, size_t length); | ||
1198 | |||
1199 | /* Read out the final digest from HD return the digest value for | ||
1200 | algorithm ALGO. */ | ||
1201 | unsigned char *gcry_md_read (gcry_md_hd_t hd, int algo); | ||
1202 | |||
1203 | /* Convenience function to calculate the hash from the data in BUFFER | ||
1204 | of size LENGTH using the algorithm ALGO avoiding the creating of a | ||
1205 | hash object. The hash is returned in the caller provided buffer | ||
1206 | DIGEST which must be large enough to hold the digest of the given | ||
1207 | algorithm. */ | ||
1208 | void gcry_md_hash_buffer (int algo, void *digest, | ||
1209 | const void *buffer, size_t length); | ||
1210 | |||
1211 | /* Retrieve the algorithm used with HD. This does not work reliable | ||
1212 | if more than one algorithm is enabled in HD. */ | ||
1213 | int gcry_md_get_algo (gcry_md_hd_t hd); | ||
1214 | |||
1215 | /* Retrieve the length in bytes of the digest yielded by algorithm | ||
1216 | ALGO. */ | ||
1217 | unsigned int gcry_md_get_algo_dlen (int algo); | ||
1218 | |||
1219 | /* Return true if the the algorithm ALGO is enabled in the digest | ||
1220 | object A. */ | ||
1221 | int gcry_md_is_enabled (gcry_md_hd_t a, int algo); | ||
1222 | |||
1223 | /* Return true if the digest object A is allocated in "secure" memory. */ | ||
1224 | int gcry_md_is_secure (gcry_md_hd_t a); | ||
1225 | |||
1226 | /* Retrieve various information about the object H. */ | ||
1227 | gcry_error_t gcry_md_info (gcry_md_hd_t h, int what, void *buffer, | ||
1228 | size_t *nbytes); | ||
1229 | |||
1230 | /* Retrieve various information about the algorithm ALGO. */ | ||
1231 | gcry_error_t gcry_md_algo_info (int algo, int what, void *buffer, | ||
1232 | size_t *nbytes); | ||
1233 | |||
1234 | /* Map the digest algorithm id ALGO to a string representation of the | ||
1235 | algorithm name. For unknown algorithms this functions returns an | ||
1236 | empty string. */ | ||
1237 | const char *gcry_md_algo_name (int algo) _GCRY_GCC_ATTR_PURE; | ||
1238 | |||
1239 | /* Map the algorithm NAME to a digest algorithm Id. Return 0 if | ||
1240 | the algorithm name is not known. */ | ||
1241 | int gcry_md_map_name (const char* name) _GCRY_GCC_ATTR_PURE; | ||
1242 | |||
1243 | /* For use with the HMAC feature, the set MAC key to the KEY of | ||
1244 | KEYLEN. */ | ||
1245 | gcry_error_t gcry_md_setkey (gcry_md_hd_t hd, const void *key, size_t keylen); | ||
1246 | |||
1247 | /* Update the hash(s) of H with the character C. This is a buffered | ||
1248 | version of the gcry_md_write function. */ | ||
1249 | #define gcry_md_putc(h,c) \ | ||
1250 | do { \ | ||
1251 | gcry_md_hd_t h__ = (h); \ | ||
1252 | if( (h__)->bufpos == (h__)->bufsize ) \ | ||
1253 | gcry_md_write( (h__), NULL, 0 ); \ | ||
1254 | (h__)->buf[(h__)->bufpos++] = (c) & 0xff; \ | ||
1255 | } while(0) | ||
1256 | |||
1257 | /* Finalize the digest calculation. This is not really needed because | ||
1258 | gcry_md_read() does this implicitly. */ | ||
1259 | #define gcry_md_final(a) \ | ||
1260 | gcry_md_ctl ((a), GCRYCTL_FINALIZE, NULL, 0) | ||
1261 | |||
1262 | /* Return 0 if the algorithm A is available for use. */ | ||
1263 | #define gcry_md_test_algo(a) \ | ||
1264 | gcry_md_algo_info( (a), GCRYCTL_TEST_ALGO, NULL, NULL ) | ||
1265 | |||
1266 | /* Return an DER encoded ASN.1 OID for the algorithm A in buffer B. N | ||
1267 | must point to size_t variable with the available size of buffer B. | ||
1268 | After return it will receive the actual size of the returned | ||
1269 | OID. */ | ||
1270 | #define gcry_md_get_asnoid(a,b,n) \ | ||
1271 | gcry_md_algo_info((a), GCRYCTL_GET_ASNOID, (b), (n)) | ||
1272 | |||
1273 | /* Enable debugging for digets object A; i.e. create files named | ||
1274 | dbgmd-<n>.<string> while hashing. B is a string used as the suffix | ||
1275 | for the filename. */ | ||
1276 | #define gcry_md_start_debug(a,b) \ | ||
1277 | gcry_md_ctl( (a), GCRYCTL_START_DUMP, (b), 0 ) | ||
1278 | |||
1279 | /* Disable the debugging of A. */ | ||
1280 | #define gcry_md_stop_debug(a,b) \ | ||
1281 | gcry_md_ctl( (a), GCRYCTL_STOP_DUMP, (b), 0 ) | ||
1282 | |||
1283 | /* Get a list consisting of the IDs of the loaded message digest | ||
1284 | modules. If LIST is zero, write the number of loaded message | ||
1285 | digest modules to LIST_LENGTH and return. If LIST is non-zero, the | ||
1286 | first *LIST_LENGTH algorithm IDs are stored in LIST, which must be | ||
1287 | of according size. In case there are less message digest modules | ||
1288 | than *LIST_LENGTH, *LIST_LENGTH is updated to the correct | ||
1289 | number. */ | ||
1290 | gcry_error_t gcry_md_list (int *list, int *list_length); | ||
1291 | |||
1292 | |||
1293 | /************************************ | ||
1294 | * * | ||
1295 | * random generating functions * | ||
1296 | * * | ||
1297 | ************************************/ | ||
1298 | |||
1299 | /* The possible values for the random quality. The rule of thumb is | ||
1300 | to use STRONG for session keys and VERY_STRONG for key material. | ||
1301 | WEAK is currently an alias for STRONG and should not be used | ||
1302 | anymore - use gcry_create_nonce instead. */ | ||
1303 | typedef enum gcry_random_level | ||
1304 | { | ||
1305 | GCRY_WEAK_RANDOM = 0, | ||
1306 | GCRY_STRONG_RANDOM = 1, | ||
1307 | GCRY_VERY_STRONG_RANDOM = 2 | ||
1308 | } | ||
1309 | gcry_random_level_t; | ||
1310 | |||
1311 | /* Fill BUFFER with LENGTH bytes of random, using random numbers of | ||
1312 | quality LEVEL. */ | ||
1313 | void gcry_randomize (unsigned char *buffer, size_t length, | ||
1314 | enum gcry_random_level level); | ||
1315 | |||
1316 | /* Add the external random from BUFFER with LENGTH bytes into the | ||
1317 | pool. QUALITY should either be -1 for unknown or in the range of 0 | ||
1318 | to 100 */ | ||
1319 | gcry_error_t gcry_random_add_bytes (const void *buffer, size_t length, | ||
1320 | int quality); | ||
1321 | |||
1322 | /* Return NBYTES of allocated random using a random numbers of quality | ||
1323 | LEVEL. */ | ||
1324 | void *gcry_random_bytes (size_t nbytes, enum gcry_random_level level) | ||
1325 | _GCRY_GCC_ATTR_MALLOC; | ||
1326 | |||
1327 | /* Return NBYTES of allocated random using a random numbers of quality | ||
1328 | LEVEL. The random numbers are created returned in "secure" | ||
1329 | memory. */ | ||
1330 | void *gcry_random_bytes_secure (size_t nbytes, enum gcry_random_level level) | ||
1331 | _GCRY_GCC_ATTR_MALLOC; | ||
1332 | |||
1333 | |||
1334 | /* Set the big integer W to a random value of NBITS using a random | ||
1335 | generator with quality LEVEL. */ | ||
1336 | void gcry_mpi_randomize (gcry_mpi_t w, | ||
1337 | unsigned int nbits, enum gcry_random_level level); | ||
1338 | |||
1339 | |||
1340 | /* Create an unpredicable nonce of LENGTH bytes in BUFFER. */ | ||
1341 | void gcry_create_nonce (unsigned char *buffer, size_t length); | ||
1342 | |||
1343 | |||
1344 | |||
1345 | /* Prime interface. */ | ||
1346 | |||
1347 | /* Mode values passed to a gcry_prime_check_func_t. */ | ||
1348 | #define GCRY_PRIME_CHECK_AT_FINISH 0 | ||
1349 | #define GCRY_PRIME_CHECK_AT_GOT_PRIME 1 | ||
1350 | #define GCRY_PRIME_CHECK_AT_MAYBE_PRIME 2 | ||
1351 | |||
1352 | /* The function should return 1 if the operation shall continue, 0 to | ||
1353 | reject the prime candidate. */ | ||
1354 | typedef int (*gcry_prime_check_func_t) (void *arg, int mode, | ||
1355 | gcry_mpi_t candidate); | ||
1356 | |||
1357 | /* Flags for gcry_prime_generate(): */ | ||
1358 | |||
1359 | /* Allocate prime numbers and factors in secure memory. */ | ||
1360 | #define GCRY_PRIME_FLAG_SECRET (1 << 0) | ||
1361 | |||
1362 | /* Make sure that at least one prime factor is of size | ||
1363 | `FACTOR_BITS'. */ | ||
1364 | #define GCRY_PRIME_FLAG_SPECIAL_FACTOR (1 << 1) | ||
1365 | |||
1366 | /* Generate a new prime number of PRIME_BITS bits and store it in | ||
1367 | PRIME. If FACTOR_BITS is non-zero, one of the prime factors of | ||
1368 | (prime - 1) / 2 must be FACTOR_BITS bits long. If FACTORS is | ||
1369 | non-zero, allocate a new, NULL-terminated array holding the prime | ||
1370 | factors and store it in FACTORS. FLAGS might be used to influence | ||
1371 | the prime number generation process. */ | ||
1372 | gcry_error_t gcry_prime_generate (gcry_mpi_t *prime, | ||
1373 | unsigned int prime_bits, | ||
1374 | unsigned int factor_bits, | ||
1375 | gcry_mpi_t **factors, | ||
1376 | gcry_prime_check_func_t cb_func, | ||
1377 | void *cb_arg, | ||
1378 | gcry_random_level_t random_level, | ||
1379 | unsigned int flags); | ||
1380 | |||
1381 | /* Find a generator for PRIME where the factorization of (prime-1) is | ||
1382 | in the NULL terminated array FACTORS. Return the generator as a | ||
1383 | newly allocated MPI in R_G. If START_G is not NULL, use this as | ||
1384 | teh start for the search. */ | ||
1385 | gcry_error_t gcry_prime_group_generator (gcry_mpi_t *r_g, | ||
1386 | gcry_mpi_t prime, gcry_mpi_t *factors, | ||
1387 | gcry_mpi_t start_g); | ||
1388 | |||
1389 | |||
1390 | /* Convenience function to release the FACTORS array. */ | ||
1391 | void gcry_prime_release_factors (gcry_mpi_t *factors); | ||
1392 | |||
1393 | |||
1394 | /* Check wether the number X is prime. */ | ||
1395 | gcry_error_t gcry_prime_check (gcry_mpi_t x, unsigned int flags); | ||
1396 | |||
1397 | |||
1398 | |||
1399 | /************************************ | ||
1400 | * * | ||
1401 | * miscellaneous stuff * | ||
1402 | * * | ||
1403 | ************************************/ | ||
1404 | |||
1405 | /* Log levels used by the internal logging facility. */ | ||
1406 | enum gcry_log_levels | ||
1407 | { | ||
1408 | GCRY_LOG_CONT = 0, /* continue the last log line */ | ||
1409 | GCRY_LOG_INFO = 10, | ||
1410 | GCRY_LOG_WARN = 20, | ||
1411 | GCRY_LOG_ERROR = 30, | ||
1412 | GCRY_LOG_FATAL = 40, | ||
1413 | GCRY_LOG_BUG = 50, | ||
1414 | GCRY_LOG_DEBUG = 100 | ||
1415 | }; | ||
1416 | |||
1417 | /* Type for progress handlers. */ | ||
1418 | typedef void (*gcry_handler_progress_t) (void *, const char *, int, int, int); | ||
1419 | |||
1420 | /* Type for memory allocation handlers. */ | ||
1421 | typedef void *(*gcry_handler_alloc_t) (size_t n); | ||
1422 | |||
1423 | /* Type for secure memory check handlers. */ | ||
1424 | typedef int (*gcry_handler_secure_check_t) (const void *); | ||
1425 | |||
1426 | /* Type for memory reallocation handlers. */ | ||
1427 | typedef void *(*gcry_handler_realloc_t) (void *p, size_t n); | ||
1428 | |||
1429 | /* Type for memory free handlers. */ | ||
1430 | typedef void (*gcry_handler_free_t) (void *); | ||
1431 | |||
1432 | /* Type for out-of-memory handlers. */ | ||
1433 | typedef int (*gcry_handler_no_mem_t) (void *, size_t, unsigned int); | ||
1434 | |||
1435 | /* Type for fatal error handlers. */ | ||
1436 | typedef void (*gcry_handler_error_t) (void *, int, const char *); | ||
1437 | |||
1438 | /* Type for logging handlers. */ | ||
1439 | typedef void (*gcry_handler_log_t) (void *, int, const char *, va_list); | ||
1440 | |||
1441 | /* Certain operations can provide progress information. This function | ||
1442 | is used to register a handler for retrieving these information. */ | ||
1443 | void gcry_set_progress_handler (gcry_handler_progress_t cb, void *cb_data); | ||
1444 | |||
1445 | |||
1446 | /* Register a custom memory allocation functions. */ | ||
1447 | void gcry_set_allocation_handler ( | ||
1448 | gcry_handler_alloc_t func_alloc, | ||
1449 | gcry_handler_alloc_t func_alloc_secure, | ||
1450 | gcry_handler_secure_check_t func_secure_check, | ||
1451 | gcry_handler_realloc_t func_realloc, | ||
1452 | gcry_handler_free_t func_free); | ||
1453 | |||
1454 | /* Register a function used instead of the internal out of memory | ||
1455 | handler. */ | ||
1456 | void gcry_set_outofcore_handler (gcry_handler_no_mem_t h, void *opaque); | ||
1457 | |||
1458 | /* Register a function used instead of the internal fatal error | ||
1459 | handler. */ | ||
1460 | void gcry_set_fatalerror_handler (gcry_handler_error_t fnc, void *opaque); | ||
1461 | |||
1462 | /* Register a function used instead of the internal logging | ||
1463 | facility. */ | ||
1464 | void gcry_set_log_handler (gcry_handler_log_t f, void *opaque); | ||
1465 | |||
1466 | /* Reserved for future use. */ | ||
1467 | void gcry_set_gettext_handler (const char *(*f)(const char*)); | ||
1468 | |||
1469 | /* Libgcrypt uses its own memory allocation. It is important to use | ||
1470 | gcry_free () to release memory allocated by libgcrypt. */ | ||
1471 | void *gcry_malloc (size_t n) _GCRY_GCC_ATTR_MALLOC; | ||
1472 | void *gcry_calloc (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC; | ||
1473 | void *gcry_malloc_secure (size_t n) _GCRY_GCC_ATTR_MALLOC; | ||
1474 | void *gcry_calloc_secure (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC; | ||
1475 | void *gcry_realloc (void *a, size_t n); | ||
1476 | char *gcry_strdup (const char *string) _GCRY_GCC_ATTR_MALLOC; | ||
1477 | void *gcry_xmalloc (size_t n) _GCRY_GCC_ATTR_MALLOC; | ||
1478 | void *gcry_xcalloc (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC; | ||
1479 | void *gcry_xmalloc_secure (size_t n) _GCRY_GCC_ATTR_MALLOC; | ||
1480 | void *gcry_xcalloc_secure (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC; | ||
1481 | void *gcry_xrealloc (void *a, size_t n); | ||
1482 | char *gcry_xstrdup (const char * a) _GCRY_GCC_ATTR_MALLOC; | ||
1483 | void gcry_free (void *a); | ||
1484 | |||
1485 | /* Return true if A is allocated in "secure" memory. */ | ||
1486 | int gcry_is_secure (const void *a) _GCRY_GCC_ATTR_PURE; | ||
1487 | |||
1488 | /* Include support for Libgcrypt modules. */ | ||
1489 | #include <gcrypt-module.h> | ||
1490 | |||
1491 | #if 0 /* keep Emacsens' auto-indent happy */ | ||
1492 | { | ||
1493 | #endif | ||
1494 | #ifdef __cplusplus | ||
1495 | } | ||
1496 | #endif | ||
1497 | #endif /* _GCRYPT_H */ | ||
diff --git a/pwmanager/libcrypt/crypt/global.c b/pwmanager/libcrypt/crypt/global.c new file mode 100644 index 0000000..9e7165b --- a/dev/null +++ b/pwmanager/libcrypt/crypt/global.c | |||
@@ -0,0 +1,672 @@ | |||
1 | /* global.c -global control functions | ||
2 | * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 | ||
3 | * 2004 Free Software Foundation, Inc. | ||
4 | * | ||
5 | * This file is part of Libgcrypt. | ||
6 | * | ||
7 | * Libgcrypt is free software; you can redistribute it and/or modify | ||
8 | * it under the terms of the GNU Lesser general Public License as | ||
9 | * published by the Free Software Foundation; either version 2.1 of | ||
10 | * the License, or (at your option) any later version. | ||
11 | * | ||
12 | * Libgcrypt is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | * GNU Lesser General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU Lesser General Public | ||
18 | * License along with this program; if not, write to the Free Software | ||
19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | ||
20 | */ | ||
21 | |||
22 | #include <config.h> | ||
23 | #include <stdio.h> | ||
24 | #include <stdlib.h> | ||
25 | #include <string.h> | ||
26 | #include <stdarg.h> | ||
27 | #include <ctype.h> | ||
28 | #include <assert.h> | ||
29 | #include <limits.h> | ||
30 | #include <errno.h> | ||
31 | |||
32 | #include "g10lib.h" | ||
33 | #include "cipher.h" | ||
34 | #include "stdmem.h" /* our own memory allocator */ | ||
35 | #include "secmem.h" /* our own secmem allocator */ | ||
36 | #include "ath.h" | ||
37 | |||
38 | /**************** | ||
39 | * flag bits: 0 : general cipher debug | ||
40 | * 1 : general MPI debug | ||
41 | */ | ||
42 | static unsigned int debug_flags; | ||
43 | |||
44 | static gcry_handler_alloc_t alloc_func; | ||
45 | static gcry_handler_alloc_t alloc_secure_func; | ||
46 | static gcry_handler_secure_check_t is_secure_func; | ||
47 | static gcry_handler_realloc_t realloc_func; | ||
48 | static gcry_handler_free_t free_func; | ||
49 | static gcry_handler_no_mem_t outofcore_handler; | ||
50 | |||
51 | static void *outofcore_handler_value = NULL; | ||
52 | static int no_secure_memory = 0; | ||
53 | static int any_init_done; | ||
54 | |||
55 | /* This is our handmade constructor. It gets called by any function | ||
56 | likely to be called at startup. The suggested way for an | ||
57 | application to make sure that this has been called is by using | ||
58 | gcry_check_version. */ | ||
59 | static void | ||
60 | global_init (void) | ||
61 | { | ||
62 | gcry_err_code_t err = GPG_ERR_NO_ERROR; | ||
63 | |||
64 | if (any_init_done) | ||
65 | return; | ||
66 | any_init_done = 1; | ||
67 | |||
68 | err = ath_init (); | ||
69 | if (! err) | ||
70 | _gcry_cipher_init (); | ||
71 | if (! err) | ||
72 | _gcry_md_init (); | ||
73 | if (! err) | ||
74 | _gcry_pk_init (); | ||
75 | |||
76 | if (err) | ||
77 | /* FIXME? */ | ||
78 | BUG (); | ||
79 | } | ||
80 | |||
81 | |||
82 | static const char* | ||
83 | parse_version_number( const char *s, int *number ) | ||
84 | { | ||
85 | int val = 0; | ||
86 | |||
87 | if( *s == '0' && isdigit(s[1]) ) | ||
88 | return NULL; /* leading zeros are not allowed */ | ||
89 | for ( ; isdigit(*s); s++ ) { | ||
90 | val *= 10; | ||
91 | val += *s - '0'; | ||
92 | } | ||
93 | *number = val; | ||
94 | return val < 0? NULL : s; | ||
95 | } | ||
96 | |||
97 | |||
98 | static const char * | ||
99 | parse_version_string( const char *s, int *major, int *minor, int *micro ) | ||
100 | { | ||
101 | s = parse_version_number( s, major ); | ||
102 | if( !s || *s != '.' ) | ||
103 | return NULL; | ||
104 | s++; | ||
105 | s = parse_version_number( s, minor ); | ||
106 | if( !s || *s != '.' ) | ||
107 | return NULL; | ||
108 | s++; | ||
109 | s = parse_version_number( s, micro ); | ||
110 | if( !s ) | ||
111 | return NULL; | ||
112 | return s; /* patchlevel */ | ||
113 | } | ||
114 | |||
115 | /**************** | ||
116 | * Check that the the version of the library is at minimum the requested one | ||
117 | * and return the version string; return NULL if the condition is not | ||
118 | * satisfied. If a NULL is passed to this function, no check is done, | ||
119 | * but the version string is simply returned. | ||
120 | */ | ||
121 | const char * | ||
122 | gcry_check_version( const char *req_version ) | ||
123 | { | ||
124 | const char *ver = VERSION; | ||
125 | int my_major, my_minor, my_micro; | ||
126 | int rq_major, rq_minor, rq_micro; | ||
127 | const char *my_plvl, *rq_plvl; | ||
128 | |||
129 | global_init (); | ||
130 | if ( !req_version ) | ||
131 | return ver; | ||
132 | |||
133 | my_plvl = parse_version_string( ver, &my_major, &my_minor, &my_micro ); | ||
134 | if ( !my_plvl ) | ||
135 | return NULL; /* very strange our own version is bogus */ | ||
136 | rq_plvl = parse_version_string( req_version, &rq_major, &rq_minor, | ||
137 | &rq_micro ); | ||
138 | if ( !rq_plvl ) | ||
139 | return NULL; /* req version string is invalid */ | ||
140 | |||
141 | if ( my_major > rq_major | ||
142 | || (my_major == rq_major && my_minor > rq_minor) | ||
143 | || (my_major == rq_major && my_minor == rq_minor | ||
144 | && my_micro > rq_micro) | ||
145 | || (my_major == rq_major && my_minor == rq_minor | ||
146 | && my_micro == rq_micro | ||
147 | && strcmp( my_plvl, rq_plvl ) >= 0) ) { | ||
148 | return ver; | ||
149 | } | ||
150 | return NULL; | ||
151 | } | ||
152 | |||
153 | gcry_error_t | ||
154 | gcry_control (enum gcry_ctl_cmds cmd, ...) | ||
155 | { | ||
156 | gcry_err_code_t err = GPG_ERR_NO_ERROR; | ||
157 | static int init_finished = 0; | ||
158 | va_list arg_ptr; | ||
159 | |||
160 | va_start (arg_ptr, cmd); | ||
161 | switch (cmd) | ||
162 | { | ||
163 | case GCRYCTL_ENABLE_M_GUARD: | ||
164 | _gcry_private_enable_m_guard (); | ||
165 | break; | ||
166 | |||
167 | case GCRYCTL_ENABLE_QUICK_RANDOM: | ||
168 | _gcry_quick_random_gen (1); | ||
169 | break; | ||
170 | |||
171 | case GCRYCTL_DUMP_RANDOM_STATS: | ||
172 | _gcry_random_dump_stats (); | ||
173 | break; | ||
174 | |||
175 | case GCRYCTL_DUMP_MEMORY_STATS: | ||
176 | /*m_print_stats("[fixme: prefix]");*/ | ||
177 | break; | ||
178 | |||
179 | case GCRYCTL_DUMP_SECMEM_STATS: | ||
180 | _gcry_secmem_dump_stats (); | ||
181 | break; | ||
182 | |||
183 | case GCRYCTL_DROP_PRIVS: | ||
184 | global_init (); | ||
185 | _gcry_secmem_init (0); | ||
186 | break; | ||
187 | |||
188 | case GCRYCTL_DISABLE_SECMEM: | ||
189 | global_init (); | ||
190 | no_secure_memory = 1; | ||
191 | break; | ||
192 | |||
193 | case GCRYCTL_INIT_SECMEM: | ||
194 | global_init (); | ||
195 | _gcry_secmem_init (va_arg (arg_ptr, unsigned int)); | ||
196 | break; | ||
197 | |||
198 | case GCRYCTL_TERM_SECMEM: | ||
199 | global_init (); | ||
200 | _gcry_secmem_term (); | ||
201 | break; | ||
202 | |||
203 | case GCRYCTL_DISABLE_SECMEM_WARN: | ||
204 | _gcry_secmem_set_flags ((_gcry_secmem_get_flags () | ||
205 | | GCRY_SECMEM_FLAG_NO_WARNING)); | ||
206 | break; | ||
207 | |||
208 | case GCRYCTL_SUSPEND_SECMEM_WARN: | ||
209 | _gcry_secmem_set_flags ((_gcry_secmem_get_flags () | ||
210 | | GCRY_SECMEM_FLAG_SUSPEND_WARNING)); | ||
211 | break; | ||
212 | |||
213 | case GCRYCTL_RESUME_SECMEM_WARN: | ||
214 | _gcry_secmem_set_flags ((_gcry_secmem_get_flags () | ||
215 | & ~GCRY_SECMEM_FLAG_SUSPEND_WARNING)); | ||
216 | break; | ||
217 | |||
218 | case GCRYCTL_USE_SECURE_RNDPOOL: | ||
219 | global_init (); | ||
220 | _gcry_secure_random_alloc (); /* put random number into secure memory */ | ||
221 | break; | ||
222 | |||
223 | case GCRYCTL_SET_RANDOM_SEED_FILE: | ||
224 | _gcry_set_random_seed_file (va_arg (arg_ptr, const char *)); | ||
225 | break; | ||
226 | |||
227 | case GCRYCTL_UPDATE_RANDOM_SEED_FILE: | ||
228 | _gcry_update_random_seed_file (); | ||
229 | break; | ||
230 | |||
231 | case GCRYCTL_SET_VERBOSITY: | ||
232 | _gcry_set_log_verbosity (va_arg (arg_ptr, int)); | ||
233 | break; | ||
234 | |||
235 | case GCRYCTL_SET_DEBUG_FLAGS: | ||
236 | debug_flags |= va_arg (arg_ptr, unsigned int); | ||
237 | break; | ||
238 | |||
239 | case GCRYCTL_CLEAR_DEBUG_FLAGS: | ||
240 | debug_flags &= ~va_arg (arg_ptr, unsigned int); | ||
241 | break; | ||
242 | |||
243 | case GCRYCTL_DISABLE_INTERNAL_LOCKING: | ||
244 | global_init (); | ||
245 | break; | ||
246 | |||
247 | case GCRYCTL_ANY_INITIALIZATION_P: | ||
248 | if (any_init_done) | ||
249 | err = GPG_ERR_GENERAL; | ||
250 | break; | ||
251 | |||
252 | case GCRYCTL_INITIALIZATION_FINISHED_P: | ||
253 | if (init_finished) | ||
254 | err = GPG_ERR_GENERAL; | ||
255 | break; | ||
256 | |||
257 | case GCRYCTL_INITIALIZATION_FINISHED: | ||
258 | /* This is a hook which should be used by an application after | ||
259 | all initialization has been done and right before any threads | ||
260 | are started. It is not really needed but the only way to be | ||
261 | really sure that all initialization for thread-safety has | ||
262 | been done. */ | ||
263 | if (! init_finished) | ||
264 | { | ||
265 | global_init (); | ||
266 | _gcry_random_initialize (); | ||
267 | init_finished = 1; | ||
268 | } | ||
269 | break; | ||
270 | |||
271 | case GCRYCTL_SET_THREAD_CBS: | ||
272 | err = ath_install (va_arg (arg_ptr, void *), any_init_done); | ||
273 | break; | ||
274 | |||
275 | default: | ||
276 | err = GPG_ERR_INV_OP; | ||
277 | } | ||
278 | |||
279 | va_end(arg_ptr); | ||
280 | return gcry_error (err); | ||
281 | } | ||
282 | |||
283 | /* Return a pointer to a string containing a description of the error | ||
284 | code in the error value ERR. */ | ||
285 | const char * | ||
286 | gcry_strerror (gcry_error_t err) | ||
287 | { | ||
288 | return gpg_strerror (err); | ||
289 | } | ||
290 | |||
291 | /* Return a pointer to a string containing a description of the error | ||
292 | source in the error value ERR. */ | ||
293 | const char * | ||
294 | gcry_strsource (gcry_error_t err) | ||
295 | { | ||
296 | return gpg_strsource (err); | ||
297 | } | ||
298 | |||
299 | /* Retrieve the error code for the system error ERR. This returns | ||
300 | GPG_ERR_UNKNOWN_ERRNO if the system error is not mapped (report | ||
301 | this). */ | ||
302 | gcry_err_code_t | ||
303 | gcry_err_code_from_errno (int err) | ||
304 | { | ||
305 | return gpg_err_code_from_errno (err); | ||
306 | } | ||
307 | |||
308 | |||
309 | /* Retrieve the system error for the error code CODE. This returns 0 | ||
310 | if CODE is not a system error code. */ | ||
311 | int | ||
312 | gcry_err_code_to_errno (gcry_err_code_t code) | ||
313 | { | ||
314 | return gpg_err_code_from_errno (code); | ||
315 | } | ||
316 | |||
317 | |||
318 | /* Return an error value with the error source SOURCE and the system | ||
319 | error ERR. */ | ||
320 | gcry_error_t | ||
321 | gcry_err_make_from_errno (gpg_err_source_t source, int err) | ||
322 | { | ||
323 | return gpg_err_make_from_errno (source, err); | ||
324 | } | ||
325 | |||
326 | |||
327 | /* Return an error value with the system error ERR. */ | ||
328 | gcry_err_code_t | ||
329 | gcry_error_from_errno (int err) | ||
330 | { | ||
331 | return gcry_error (gpg_err_code_from_errno (err)); | ||
332 | } | ||
333 | |||
334 | /**************** | ||
335 | * NOTE: All 5 functions should be set. */ | ||
336 | void | ||
337 | gcry_set_allocation_handler (gcry_handler_alloc_t new_alloc_func, | ||
338 | gcry_handler_alloc_t new_alloc_secure_func, | ||
339 | gcry_handler_secure_check_t new_is_secure_func, | ||
340 | gcry_handler_realloc_t new_realloc_func, | ||
341 | gcry_handler_free_t new_free_func) | ||
342 | { | ||
343 | global_init (); | ||
344 | |||
345 | alloc_func = new_alloc_func; | ||
346 | alloc_secure_func = new_alloc_secure_func; | ||
347 | is_secure_func = new_is_secure_func; | ||
348 | realloc_func = new_realloc_func; | ||
349 | free_func = new_free_func; | ||
350 | } | ||
351 | |||
352 | |||
353 | |||
354 | /**************** | ||
355 | * Set an optional handler which is called in case the xmalloc functions | ||
356 | * ran out of memory. This handler may do one of these things: | ||
357 | * o free some memory and return true, so that the xmalloc function | ||
358 | * tries again. | ||
359 | * o Do whatever it like and return false, so that the xmalloc functions | ||
360 | * use the default fatal error handler. | ||
361 | * o Terminate the program and don't return. | ||
362 | * | ||
363 | * The handler function is called with 3 arguments: The opaque value set with | ||
364 | * this function, the requested memory size, and a flag with these bits | ||
365 | * currently defined: | ||
366 | *bit 0 set = secure memory has been requested. | ||
367 | */ | ||
368 | void | ||
369 | gcry_set_outofcore_handler( int (*f)( void*, size_t, unsigned int ), | ||
370 | void *value ) | ||
371 | { | ||
372 | global_init (); | ||
373 | |||
374 | outofcore_handler = f; | ||
375 | outofcore_handler_value = value; | ||
376 | } | ||
377 | |||
378 | gcry_err_code_t | ||
379 | _gcry_malloc (size_t n, unsigned int flags, void **mem) | ||
380 | { | ||
381 | gcry_err_code_t err = GPG_ERR_NO_ERROR; | ||
382 | void *m = NULL; | ||
383 | |||
384 | if ((flags & GCRY_ALLOC_FLAG_SECURE) && !no_secure_memory) | ||
385 | { | ||
386 | if (alloc_secure_func) | ||
387 | m = (*alloc_secure_func) (n); | ||
388 | else | ||
389 | m = _gcry_private_malloc_secure (n); | ||
390 | } | ||
391 | else | ||
392 | { | ||
393 | if (alloc_func) | ||
394 | m = (*alloc_func) (n); | ||
395 | else | ||
396 | m = _gcry_private_malloc (n); | ||
397 | } | ||
398 | |||
399 | if (! m) | ||
400 | err = gpg_err_code_from_errno (ENOMEM); | ||
401 | else | ||
402 | *mem = m; | ||
403 | |||
404 | return err; | ||
405 | } | ||
406 | |||
407 | void * | ||
408 | gcry_malloc (size_t n) | ||
409 | { | ||
410 | void *mem = NULL; | ||
411 | |||
412 | _gcry_malloc (n, 0, &mem); | ||
413 | |||
414 | return mem; | ||
415 | } | ||
416 | |||
417 | void * | ||
418 | gcry_malloc_secure (size_t n) | ||
419 | { | ||
420 | void *mem = NULL; | ||
421 | |||
422 | _gcry_malloc (n, GCRY_ALLOC_FLAG_SECURE, &mem); | ||
423 | |||
424 | return mem; | ||
425 | } | ||
426 | |||
427 | int | ||
428 | gcry_is_secure (const void *a) | ||
429 | { | ||
430 | if (no_secure_memory) | ||
431 | return 0; | ||
432 | if (is_secure_func) | ||
433 | return is_secure_func (a) ; | ||
434 | return _gcry_private_is_secure (a); | ||
435 | } | ||
436 | |||
437 | void | ||
438 | _gcry_check_heap( const void *a ) | ||
439 | { | ||
440 | /* FIXME: implement this*/ | ||
441 | #if 0 | ||
442 | if( some_handler ) | ||
443 | some_handler(a) | ||
444 | else | ||
445 | _gcry_private_check_heap(a) | ||
446 | #endif | ||
447 | } | ||
448 | |||
449 | void * | ||
450 | gcry_realloc (void *a, size_t n) | ||
451 | { | ||
452 | if (realloc_func) | ||
453 | return realloc_func (a, n); | ||
454 | return _gcry_private_realloc (a, n); | ||
455 | } | ||
456 | |||
457 | void | ||
458 | gcry_free( void *p ) | ||
459 | { | ||
460 | if( !p ) | ||
461 | return; | ||
462 | |||
463 | if (free_func) | ||
464 | free_func (p); | ||
465 | else | ||
466 | _gcry_private_free (p); | ||
467 | } | ||
468 | |||
469 | void * | ||
470 | gcry_calloc (size_t n, size_t m) | ||
471 | { | ||
472 | size_t bytes; | ||
473 | void *p; | ||
474 | |||
475 | bytes = n * m; /* size_t is unsigned so the behavior on overflow is | ||
476 | defined. */ | ||
477 | if (m && bytes / m != n) | ||
478 | { | ||
479 | errno = ENOMEM; | ||
480 | return NULL; | ||
481 | } | ||
482 | |||
483 | p = gcry_malloc (bytes); | ||
484 | if (p) | ||
485 | memset (p, 0, bytes); | ||
486 | return p; | ||
487 | } | ||
488 | |||
489 | void * | ||
490 | gcry_calloc_secure (size_t n, size_t m) | ||
491 | { | ||
492 | size_t bytes; | ||
493 | void *p; | ||
494 | |||
495 | bytes = n * m; /* size_t is unsigned so the behavior on overflow is | ||
496 | defined. */ | ||
497 | if (m && bytes / m != n) | ||
498 | { | ||
499 | errno = ENOMEM; | ||
500 | return NULL; | ||
501 | } | ||
502 | |||
503 | p = gcry_malloc_secure (bytes); | ||
504 | if (p) | ||
505 | memset (p, 0, bytes); | ||
506 | return p; | ||
507 | } | ||
508 | |||
509 | |||
510 | /* Create and return a copy of the null-terminated string STRING. If | ||
511 | it is contained in secure memory, the copy will be contained in | ||
512 | secure memory as well. In an out-of-memory condition, NULL is | ||
513 | returned. */ | ||
514 | char * | ||
515 | gcry_strdup (const char *string) | ||
516 | { | ||
517 | char *string_cp = NULL; | ||
518 | size_t string_n = 0; | ||
519 | |||
520 | string_n = strlen (string); | ||
521 | |||
522 | if (gcry_is_secure (string)) | ||
523 | string_cp = gcry_malloc_secure (string_n + 1); | ||
524 | else | ||
525 | string_cp = gcry_malloc (string_n + 1); | ||
526 | |||
527 | if (string_cp) | ||
528 | strcpy (string_cp, string); | ||
529 | |||
530 | return string_cp; | ||
531 | } | ||
532 | |||
533 | |||
534 | void * | ||
535 | gcry_xmalloc( size_t n ) | ||
536 | { | ||
537 | void *p; | ||
538 | |||
539 | while ( !(p = gcry_malloc( n )) ) { | ||
540 | if( !outofcore_handler | ||
541 | || !outofcore_handler( outofcore_handler_value, n, 0 ) ) { | ||
542 | _gcry_fatal_error(gpg_err_code_from_errno (errno), NULL ); | ||
543 | } | ||
544 | } | ||
545 | return p; | ||
546 | } | ||
547 | |||
548 | void * | ||
549 | gcry_xrealloc( void *a, size_t n ) | ||
550 | { | ||
551 | void *p; | ||
552 | |||
553 | while ( !(p = gcry_realloc( a, n )) ) { | ||
554 | if( !outofcore_handler | ||
555 | || !outofcore_handler( outofcore_handler_value, n, 2 ) ) { | ||
556 | _gcry_fatal_error(gpg_err_code_from_errno (errno), NULL ); | ||
557 | } | ||
558 | } | ||
559 | return p; | ||
560 | } | ||
561 | |||
562 | void * | ||
563 | gcry_xmalloc_secure( size_t n ) | ||
564 | { | ||
565 | void *p; | ||
566 | |||
567 | while ( !(p = gcry_malloc_secure( n )) ) { | ||
568 | if( !outofcore_handler | ||
569 | || !outofcore_handler( outofcore_handler_value, n, 1 ) ) { | ||
570 | _gcry_fatal_error(gpg_err_code_from_errno (errno), | ||
571 | _("out of core in secure memory")); | ||
572 | } | ||
573 | } | ||
574 | return p; | ||
575 | } | ||
576 | |||
577 | void * | ||
578 | gcry_xcalloc( size_t n, size_t m ) | ||
579 | { | ||
580 | void *p = gcry_xmalloc( n*m ); | ||
581 | memset( p, 0, n*m ); | ||
582 | return p; | ||
583 | } | ||
584 | |||
585 | void * | ||
586 | gcry_xcalloc_secure( size_t n, size_t m ) | ||
587 | { | ||
588 | void *p = gcry_xmalloc_secure( n* m ); | ||
589 | memset( p, 0, n*m ); | ||
590 | return p; | ||
591 | } | ||
592 | |||
593 | char * | ||
594 | gcry_xstrdup (const char *string) | ||
595 | { | ||
596 | char *p; | ||
597 | |||
598 | while ( !(p = gcry_strdup (string)) ) | ||
599 | { | ||
600 | size_t n = strlen (string); | ||
601 | int is_sec = !!gcry_is_secure (string); | ||
602 | |||
603 | if (!outofcore_handler | ||
604 | || !outofcore_handler (outofcore_handler_value, n, is_sec) ) | ||
605 | { | ||
606 | _gcry_fatal_error (gpg_err_code_from_errno (errno), | ||
607 | is_sec? _("out of core in secure memory"):NULL); | ||
608 | } | ||
609 | } | ||
610 | |||
611 | strcpy( p, string ); | ||
612 | return p; | ||
613 | } | ||
614 | |||
615 | |||
616 | int | ||
617 | _gcry_get_debug_flag( unsigned int mask ) | ||
618 | { | ||
619 | return debug_flags & mask; | ||
620 | } | ||
621 | |||
622 | |||
623 | |||
624 | /* It is often useful to get some feedback of long running operations. | ||
625 | This function may be used to register a handler for this. | ||
626 | The callback function CB is used as: | ||
627 | |||
628 | void cb (void *opaque, const char *what, int printchar, | ||
629 | int current, int total); | ||
630 | |||
631 | Where WHAT is a string identifying the the type of the progress | ||
632 | output, PRINTCHAR the character usually printed, CURRENT the amount | ||
633 | of progress currently done and TOTAL the expected amount of | ||
634 | progress. A value of 0 for TOTAL indicates that there is no | ||
635 | estimation available. | ||
636 | |||
637 | Defined values for WHAT: | ||
638 | |||
639 | "need_entropy" X 0 number-of-bytes-required | ||
640 | When running low on entropy | ||
641 | "primegen" '\n' 0 0 | ||
642 | Prime generated | ||
643 | '!' | ||
644 | Need to refresh the prime pool | ||
645 | '<','>' | ||
646 | Number of bits adjusted | ||
647 | '^' | ||
648 | Looking for a generator | ||
649 | '.' | ||
650 | Fermat tests on 10 candidates failed | ||
651 | ':' | ||
652 | Restart with a new random value | ||
653 | '+' | ||
654 | Rabin Miller test passed | ||
655 | "pk_elg" '+','-','.','\n' 0 0 | ||
656 | Only used in debugging mode. | ||
657 | "pk_dsa" | ||
658 | Only used in debugging mode. | ||
659 | */ | ||
660 | void | ||
661 | gcry_set_progress_handler (void (*cb)(void *,const char*,int, int, int), | ||
662 | void *cb_data) | ||
663 | { | ||
664 | #if USE_DSA | ||
665 | _gcry_register_pk_dsa_progress (cb, cb_data); | ||
666 | #endif | ||
667 | #if USE_ELGAMAL | ||
668 | _gcry_register_pk_elg_progress (cb, cb_data); | ||
669 | #endif | ||
670 | _gcry_register_primegen_progress (cb, cb_data); | ||
671 | _gcry_register_random_progress (cb, cb_data); | ||
672 | } | ||
diff --git a/pwmanager/libcrypt/crypt/misc.c b/pwmanager/libcrypt/crypt/misc.c new file mode 100644 index 0000000..6b630e6 --- a/dev/null +++ b/pwmanager/libcrypt/crypt/misc.c | |||
@@ -0,0 +1,241 @@ | |||
1 | /* misc.c | ||
2 | *Copyright (C) 1999, 2001, 2002, 2003 Free Software Foundation, Inc. | ||
3 | * | ||
4 | * This file is part of Libgcrypt. | ||
5 | * | ||
6 | * Libgcrypt is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU Lesser general Public License as | ||
8 | * published by the Free Software Foundation; either version 2.1 of | ||
9 | * the License, or (at your option) any later version. | ||
10 | * | ||
11 | * Libgcrypt is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * GNU Lesser General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU Lesser General Public | ||
17 | * License along with this program; if not, write to the Free Software | ||
18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | ||
19 | */ | ||
20 | |||
21 | #include <config.h> | ||
22 | #include <stdio.h> | ||
23 | #include <stdlib.h> | ||
24 | #include <string.h> | ||
25 | #include <stdarg.h> | ||
26 | #include <assert.h> | ||
27 | #include <unistd.h> | ||
28 | |||
29 | #include "g10lib.h" | ||
30 | |||
31 | static int verbosity_level = 0; | ||
32 | |||
33 | static void (*fatal_error_handler)(void*,int, const char*) = NULL; | ||
34 | static void *fatal_error_handler_value = 0; | ||
35 | static void (*log_handler)(void*,int, const char*, va_list) = NULL; | ||
36 | static void *log_handler_value = 0; | ||
37 | |||
38 | static const char *(*user_gettext_handler)( const char * ) = NULL; | ||
39 | |||
40 | void | ||
41 | gcry_set_gettext_handler( const char *(*f)(const char*) ) | ||
42 | { | ||
43 | user_gettext_handler = f; | ||
44 | } | ||
45 | |||
46 | |||
47 | const char * | ||
48 | _gcry_gettext( const char *key ) | ||
49 | { | ||
50 | if( user_gettext_handler ) | ||
51 | return user_gettext_handler( key ); | ||
52 | /* FIXME: switch the domain to gnupg and restore later */ | ||
53 | return key; | ||
54 | } | ||
55 | |||
56 | void | ||
57 | gcry_set_fatalerror_handler( void (*fnc)(void*,int, const char*), void *value) | ||
58 | { | ||
59 | fatal_error_handler_value = value; | ||
60 | fatal_error_handler = fnc; | ||
61 | } | ||
62 | |||
63 | static void | ||
64 | write2stderr( const char *s ) | ||
65 | { | ||
66 | write( 2, s, strlen(s) ); | ||
67 | } | ||
68 | |||
69 | /* | ||
70 | * This function is called for fatal errors. A caller might want to | ||
71 | * set his own handler because this function simply calls abort(). | ||
72 | */ | ||
73 | void | ||
74 | _gcry_fatal_error (int rc, const char *text) | ||
75 | { | ||
76 | if ( !text ) /* get a default text */ | ||
77 | text = gpg_strerror (rc); | ||
78 | |||
79 | if (fatal_error_handler) | ||
80 | fatal_error_handler (fatal_error_handler_value, rc, text); | ||
81 | |||
82 | write2stderr("\nFatal error: "); | ||
83 | write2stderr(text); | ||
84 | write2stderr("\n"); | ||
85 | abort (); | ||
86 | } | ||
87 | |||
88 | void | ||
89 | gcry_set_log_handler( void (*f)(void*,int, const char*, va_list ), | ||
90 | void *opaque ) | ||
91 | { | ||
92 | log_handler = f; | ||
93 | log_handler_value = opaque; | ||
94 | } | ||
95 | |||
96 | void | ||
97 | _gcry_set_log_verbosity( int level ) | ||
98 | { | ||
99 | verbosity_level = level; | ||
100 | } | ||
101 | |||
102 | int | ||
103 | _gcry_log_verbosity( int level ) | ||
104 | { | ||
105 | return verbosity_level >= level; | ||
106 | } | ||
107 | |||
108 | /**************** | ||
109 | * This is our log function which prints all log messages to stderr or | ||
110 | * using the function defined with gcry_set_log_handler(). | ||
111 | */ | ||
112 | static void | ||
113 | _gcry_logv( int level, const char *fmt, va_list arg_ptr ) | ||
114 | { | ||
115 | if( log_handler ) | ||
116 | log_handler( log_handler_value, level, fmt, arg_ptr ); | ||
117 | else { | ||
118 | switch ( level ) { | ||
119 | case GCRY_LOG_CONT: break; | ||
120 | case GCRY_LOG_INFO: break; | ||
121 | case GCRY_LOG_WARN: break; | ||
122 | case GCRY_LOG_ERROR: break; | ||
123 | case GCRY_LOG_FATAL: fputs("Fatal: ",stderr ); break; | ||
124 | case GCRY_LOG_BUG: fputs("Ohhhh jeeee: ", stderr); break; | ||
125 | case GCRY_LOG_DEBUG: fputs("DBG: ", stderr ); break; | ||
126 | default: fprintf(stderr,"[Unknown log level %d]: ", level ); break; | ||
127 | } | ||
128 | vfprintf(stderr,fmt,arg_ptr) ; | ||
129 | } | ||
130 | |||
131 | if( level == GCRY_LOG_FATAL ) | ||
132 | exit(2); | ||
133 | else if( level == GCRY_LOG_BUG ) | ||
134 | abort(); | ||
135 | } | ||
136 | |||
137 | void | ||
138 | _gcry_log( int level, const char *fmt, ... ) | ||
139 | { | ||
140 | va_list arg_ptr ; | ||
141 | |||
142 | va_start( arg_ptr, fmt ) ; | ||
143 | _gcry_logv( level, fmt, arg_ptr ); | ||
144 | va_end(arg_ptr); | ||
145 | } | ||
146 | |||
147 | |||
148 | #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 ) | ||
149 | void | ||
150 | _gcry_bug( const char *file, int line, const char *func ) | ||
151 | { | ||
152 | _gcry_log( GCRY_LOG_BUG, | ||
153 | ("... this is a bug (%s:%d:%s)\n"), file, line, func ); | ||
154 | abort(); /* never called, but it makes the compiler happy */ | ||
155 | } | ||
156 | #else | ||
157 | void | ||
158 | _gcry_bug( const char *file, int line ) | ||
159 | { | ||
160 | _gcry_log( GCRY_LOG_BUG, | ||
161 | _("you found a bug ... (%s:%d)\n"), file, line); | ||
162 | abort(); /* never called, but it makes the compiler happy */ | ||
163 | } | ||
164 | #endif | ||
165 | |||
166 | void | ||
167 | _gcry_log_info( const char *fmt, ... ) | ||
168 | { | ||
169 | va_list arg_ptr ; | ||
170 | |||
171 | va_start( arg_ptr, fmt ) ; | ||
172 | _gcry_logv( GCRY_LOG_INFO, fmt, arg_ptr ); | ||
173 | va_end(arg_ptr); | ||
174 | } | ||
175 | |||
176 | void | ||
177 | _gcry_log_error( const char *fmt, ... ) | ||
178 | { | ||
179 | va_list arg_ptr ; | ||
180 | |||
181 | va_start( arg_ptr, fmt ) ; | ||
182 | _gcry_logv( GCRY_LOG_ERROR, fmt, arg_ptr ); | ||
183 | va_end(arg_ptr); | ||
184 | } | ||
185 | |||
186 | |||
187 | void | ||
188 | _gcry_log_fatal( const char *fmt, ... ) | ||
189 | { | ||
190 | va_list arg_ptr ; | ||
191 | |||
192 | va_start( arg_ptr, fmt ) ; | ||
193 | _gcry_logv( GCRY_LOG_FATAL, fmt, arg_ptr ); | ||
194 | va_end(arg_ptr); | ||
195 | abort(); /* never called, but it makes the compiler happy */ | ||
196 | } | ||
197 | |||
198 | void | ||
199 | _gcry_log_bug( const char *fmt, ... ) | ||
200 | { | ||
201 | va_list arg_ptr ; | ||
202 | |||
203 | va_start( arg_ptr, fmt ) ; | ||
204 | _gcry_logv( GCRY_LOG_BUG, fmt, arg_ptr ); | ||
205 | va_end(arg_ptr); | ||
206 | abort(); /* never called, but it makes the compiler happy */ | ||
207 | } | ||
208 | |||
209 | void | ||
210 | _gcry_log_debug( const char *fmt, ... ) | ||
211 | { | ||
212 | va_list arg_ptr ; | ||
213 | |||
214 | va_start( arg_ptr, fmt ) ; | ||
215 | _gcry_logv( GCRY_LOG_DEBUG, fmt, arg_ptr ); | ||
216 | va_end(arg_ptr); | ||
217 | } | ||
218 | |||
219 | void | ||
220 | _gcry_log_printf (const char *fmt, ...) | ||
221 | { | ||
222 | va_list arg_ptr; | ||
223 | |||
224 | if (fmt) | ||
225 | { | ||
226 | va_start( arg_ptr, fmt ) ; | ||
227 | _gcry_logv (GCRY_LOG_CONT, fmt, arg_ptr); | ||
228 | va_end(arg_ptr); | ||
229 | } | ||
230 | } | ||
231 | |||
232 | void | ||
233 | _gcry_burn_stack (int bytes) | ||
234 | { | ||
235 | char buf[64]; | ||
236 | |||
237 | wipememory (buf, sizeof buf); | ||
238 | bytes -= sizeof buf; | ||
239 | if (bytes > 0) | ||
240 | _gcry_burn_stack (bytes); | ||
241 | } | ||
diff --git a/pwmanager/libcrypt/crypt/missing-string.c b/pwmanager/libcrypt/crypt/missing-string.c new file mode 100644 index 0000000..80ff052 --- a/dev/null +++ b/pwmanager/libcrypt/crypt/missing-string.c | |||
@@ -0,0 +1,151 @@ | |||
1 | /* missing-string.c - missing string utilities | ||
2 | * Copyright (C) 1994, 1998, 1999, 2000, 2001, | ||
3 | * 2003 Free Software Foundation, Inc. | ||
4 | * | ||
5 | * This file is part of Libgcrypt. | ||
6 | * | ||
7 | * Libgcrypt is free software; you can redistribute it and/or modify | ||
8 | * it under the terms of the GNU Lesser General Public License as | ||
9 | * published by the Free Software Foundation; either version 2.1 of | ||
10 | * the License, or (at your option) any later version. | ||
11 | * | ||
12 | * Libgcrypt is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | * GNU Lesser General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU Lesser General Public | ||
18 | * License along with this program; if not, write to the Free Software | ||
19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | ||
20 | */ | ||
21 | |||
22 | #include <config.h> | ||
23 | #include <stdlib.h> | ||
24 | #include <string.h> | ||
25 | #include <ctype.h> | ||
26 | |||
27 | #include "g10lib.h" | ||
28 | |||
29 | |||
30 | #ifndef HAVE_STPCPY | ||
31 | char * | ||
32 | stpcpy(char *a,const char *b) | ||
33 | { | ||
34 | while( *b ) | ||
35 | *a++ = *b++; | ||
36 | *a = 0; | ||
37 | |||
38 | return (char*)a; | ||
39 | } | ||
40 | #endif | ||
41 | |||
42 | |||
43 | #ifndef HAVE_STRCASECMP | ||
44 | int | ||
45 | strcasecmp( const char *a, const char *b ) | ||
46 | { | ||
47 | for( ; *a && *b; a++, b++ ) { | ||
48 | if( *a != *b && toupper(*a) != toupper(*b) ) | ||
49 | break; | ||
50 | } | ||
51 | return *(const byte*)a - *(const byte*)b; | ||
52 | } | ||
53 | #endif | ||
54 | |||
55 | |||
56 | #ifdef __MINGW32__ | ||
57 | /* | ||
58 | * Like vsprintf but provides a pointer to malloc'd storage, which | ||
59 | * must be freed by the caller (gcry_free). Taken from libiberty as | ||
60 | * found in gcc-2.95.2 and a little bit modernized. | ||
61 | * FIXME: Write a new CRT for W32. | ||
62 | */ | ||
63 | int | ||
64 | vasprintf ( char **result, const char *format, va_list args) | ||
65 | { | ||
66 | const char *p = format; | ||
67 | /* Add one to make sure that it is never zero, which might cause malloc | ||
68 | to return NULL. */ | ||
69 | int total_width = strlen (format) + 1; | ||
70 | va_list ap; | ||
71 | |||
72 | /* this is not really portable but works under Windows */ | ||
73 | memcpy ( &ap, &args, sizeof (va_list)); | ||
74 | |||
75 | while (*p != '\0') | ||
76 | { | ||
77 | if (*p++ == '%') | ||
78 | { | ||
79 | while (strchr ("-+ #0", *p)) | ||
80 | ++p; | ||
81 | if (*p == '*') | ||
82 | { | ||
83 | ++p; | ||
84 | total_width += abs (va_arg (ap, int)); | ||
85 | } | ||
86 | else | ||
87 | { | ||
88 | char *endp; | ||
89 | total_width += strtoul (p, &endp, 10); | ||
90 | p = endp; | ||
91 | } | ||
92 | if (*p == '.') | ||
93 | { | ||
94 | ++p; | ||
95 | if (*p == '*') | ||
96 | { | ||
97 | ++p; | ||
98 | total_width += abs (va_arg (ap, int)); | ||
99 | } | ||
100 | else | ||
101 | { | ||
102 | char *endp; | ||
103 | total_width += strtoul (p, &endp, 10); | ||
104 | p = endp; | ||
105 | } | ||
106 | } | ||
107 | while (strchr ("hlL", *p)) | ||
108 | ++p; | ||
109 | /* Should be big enough for any format specifier except %s | ||
110 | and floats. */ | ||
111 | total_width += 30; | ||
112 | switch (*p) | ||
113 | { | ||
114 | case 'd': | ||
115 | case 'i': | ||
116 | case 'o': | ||
117 | case 'u': | ||
118 | case 'x': | ||
119 | case 'X': | ||
120 | case 'c': | ||
121 | (void) va_arg (ap, int); | ||
122 | break; | ||
123 | case 'f': | ||
124 | case 'e': | ||
125 | case 'E': | ||
126 | case 'g': | ||
127 | case 'G': | ||
128 | (void) va_arg (ap, double); | ||
129 | /* Since an ieee double can have an exponent of 307, we'll | ||
130 | make the buffer wide enough to cover the gross case. */ | ||
131 | total_width += 307; | ||
132 | |||
133 | case 's': | ||
134 | total_width += strlen (va_arg (ap, char *)); | ||
135 | break; | ||
136 | case 'p': | ||
137 | case 'n': | ||
138 | (void) va_arg (ap, char *); | ||
139 | break; | ||
140 | } | ||
141 | } | ||
142 | } | ||
143 | *result = gcry_malloc (total_width); | ||
144 | if (*result != NULL) | ||
145 | return vsprintf (*result, format, args); | ||
146 | else | ||
147 | return 0; | ||
148 | } | ||
149 | |||
150 | #endif /*__MINGW32__*/ | ||
151 | |||
diff --git a/pwmanager/libcrypt/crypt/module.c b/pwmanager/libcrypt/crypt/module.c new file mode 100644 index 0000000..e98c36e --- a/dev/null +++ b/pwmanager/libcrypt/crypt/module.c | |||
@@ -0,0 +1,200 @@ | |||
1 | /* module.c - Module management for libgcrypt. | ||
2 | * Copyright (C) 2003 Free Software Foundation, Inc. | ||
3 | * | ||
4 | * This file is part of Libgcrypt. | ||
5 | * | ||
6 | * Libgcrypt is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU Lesser general Public License as | ||
8 | * published by the Free Software Foundation; either version 2.1 of | ||
9 | * the License, or (at your option) any later version. | ||
10 | * | ||
11 | * Libgcrypt is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * GNU Lesser General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU Lesser General Public | ||
17 | * License along with this program; if not, write to the Free Software | ||
18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | ||
19 | */ | ||
20 | |||
21 | #include <assert.h> | ||
22 | #include <config.h> | ||
23 | #include <errno.h> | ||
24 | #include "g10lib.h" | ||
25 | |||
26 | #define MODULE_ID_MIN 600 | ||
27 | |||
28 | /* Internal function. Generate a new, unique module ID for a module | ||
29 | that should be inserted into the module chain starting at | ||
30 | MODULES. */ | ||
31 | static gcry_err_code_t | ||
32 | _gcry_module_id_new (gcry_module_t modules, unsigned int *id_new) | ||
33 | { | ||
34 | /* FIXME, what should be the ID of the first module registered by | ||
35 | the user? */ | ||
36 | unsigned int id_min = MODULE_ID_MIN, id_max = (unsigned int) -1, mod_id; | ||
37 | gcry_err_code_t err = GPG_ERR_NO_ERROR; | ||
38 | gcry_module_t module; | ||
39 | |||
40 | /* Search for unused ID. */ | ||
41 | for (mod_id = id_min; mod_id < id_max; mod_id++) | ||
42 | { | ||
43 | /* Search for a module with the current ID. */ | ||
44 | for (module = modules; module; module = module->next) | ||
45 | if (mod_id == module->mod_id) | ||
46 | break; | ||
47 | |||
48 | if (! module) | ||
49 | /* None found -> the ID is available for use. */ | ||
50 | break; | ||
51 | } | ||
52 | |||
53 | if (mod_id < id_max) | ||
54 | /* Done. */ | ||
55 | *id_new = mod_id; | ||
56 | else | ||
57 | /* No free ID found. */ | ||
58 | err = GPG_ERR_INTERNAL; | ||
59 | |||
60 | return err; | ||
61 | } | ||
62 | |||
63 | /* Add a module specification to the list ENTRIES. The new module has | ||
64 | it's use-counter set to one. */ | ||
65 | gcry_err_code_t | ||
66 | _gcry_module_add (gcry_module_t *entries, unsigned int mod_id, | ||
67 | void *spec, gcry_module_t *module) | ||
68 | { | ||
69 | gcry_err_code_t err = 0; | ||
70 | gcry_module_t entry; | ||
71 | |||
72 | if (! mod_id) | ||
73 | err = _gcry_module_id_new (*entries, &mod_id); | ||
74 | |||
75 | if (! err) | ||
76 | { | ||
77 | entry = gcry_malloc (sizeof (struct gcry_module)); | ||
78 | if (! entry) | ||
79 | err = gpg_err_code_from_errno (errno); | ||
80 | } | ||
81 | |||
82 | if (! err) | ||
83 | { | ||
84 | /* Fill new module entry. */ | ||
85 | entry->flags = 0; | ||
86 | entry->counter = 1; | ||
87 | entry->spec = spec; | ||
88 | entry->mod_id = mod_id; | ||
89 | |||
90 | /* Link it into the list. */ | ||
91 | entry->next = *entries; | ||
92 | entry->prevp = entries; | ||
93 | if (*entries) | ||
94 | (*entries)->prevp = &entry->next; | ||
95 | *entries = entry; | ||
96 | |||
97 | /* And give it to the caller. */ | ||
98 | if (module) | ||
99 | *module = entry; | ||
100 | } | ||
101 | return err; | ||
102 | } | ||
103 | |||
104 | /* Internal function. Unlink CIPHER_ENTRY from the list of registered | ||
105 | ciphers and destroy it. */ | ||
106 | static void | ||
107 | _gcry_module_drop (gcry_module_t entry) | ||
108 | { | ||
109 | *entry->prevp = entry->next; | ||
110 | if (entry->next) | ||
111 | entry->next->prevp = entry->prevp; | ||
112 | |||
113 | gcry_free (entry); | ||
114 | } | ||
115 | |||
116 | /* Lookup a module specification by it's ID. After a successfull | ||
117 | lookup, the module has it's resource counter incremented. */ | ||
118 | gcry_module_t | ||
119 | _gcry_module_lookup_id (gcry_module_t entries, unsigned int mod_id) | ||
120 | { | ||
121 | gcry_module_t entry; | ||
122 | |||
123 | for (entry = entries; entry; entry = entry->next) | ||
124 | if (entry->mod_id == mod_id) | ||
125 | { | ||
126 | entry->counter++; | ||
127 | break; | ||
128 | } | ||
129 | |||
130 | return entry; | ||
131 | } | ||
132 | |||
133 | /* Lookup a module specification. After a successfull lookup, the | ||
134 | module has it's resource counter incremented. FUNC is a function | ||
135 | provided by the caller, which is responsible for identifying the | ||
136 | wanted module. */ | ||
137 | gcry_module_t | ||
138 | _gcry_module_lookup (gcry_module_t entries, void *data, | ||
139 | gcry_module_lookup_t func) | ||
140 | { | ||
141 | gcry_module_t entry; | ||
142 | |||
143 | for (entry = entries; entry; entry = entry->next) | ||
144 | if ((*func) (entry->spec, data)) | ||
145 | { | ||
146 | entry->counter++; | ||
147 | break; | ||
148 | } | ||
149 | |||
150 | return entry; | ||
151 | } | ||
152 | |||
153 | /* Release a module. In case the use-counter reaches zero, destroy | ||
154 | the module. Passing MODULE as NULL is a dummy operation (similar | ||
155 | to free()). */ | ||
156 | void | ||
157 | _gcry_module_release (gcry_module_t module) | ||
158 | { | ||
159 | if (module && ! --module->counter) | ||
160 | _gcry_module_drop (module); | ||
161 | } | ||
162 | |||
163 | /* Add a reference to a module. */ | ||
164 | void | ||
165 | _gcry_module_use (gcry_module_t module) | ||
166 | { | ||
167 | ++module->counter; | ||
168 | } | ||
169 | |||
170 | /* If LIST is zero, write the number of modules identified by MODULES | ||
171 | to LIST_LENGTH and return. If LIST is non-zero, the first | ||
172 | *LIST_LENGTH algorithm IDs are stored in LIST, which must be of | ||
173 | according size. In case there are less cipher modules than | ||
174 | *LIST_LENGTH, *LIST_LENGTH is updated to the correct number. */ | ||
175 | gcry_err_code_t | ||
176 | _gcry_module_list (gcry_module_t modules, | ||
177 | int *list, int *list_length) | ||
178 | { | ||
179 | gcry_err_code_t err = GPG_ERR_NO_ERROR; | ||
180 | gcry_module_t module; | ||
181 | int length, i; | ||
182 | |||
183 | for (module = modules, length = 0; module; module = module->next, length++); | ||
184 | |||
185 | if (list) | ||
186 | { | ||
187 | if (length > *list_length) | ||
188 | length = *list_length; | ||
189 | |||
190 | for (module = modules, i = 0; i < length; module = module->next, i++) | ||
191 | list[i] = module->mod_id; | ||
192 | |||
193 | if (length < *list_length) | ||
194 | *list_length = length; | ||
195 | } | ||
196 | else | ||
197 | *list_length = length; | ||
198 | |||
199 | return err; | ||
200 | } | ||
diff --git a/pwmanager/libcrypt/crypt/mpi.h b/pwmanager/libcrypt/crypt/mpi.h new file mode 100644 index 0000000..9e0037e --- a/dev/null +++ b/pwmanager/libcrypt/crypt/mpi.h | |||
@@ -0,0 +1,199 @@ | |||
1 | /* mpi.h - Multi Precision Integers | ||
2 | * Copyright (C) 1994, 1996, 1998, 2001, 2002, 2003 Free Software Foundation, Inc. | ||
3 | * | ||
4 | * This file is part of Libgcrypt. | ||
5 | * | ||
6 | * Libgcrypt is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU Lesser general Public License as | ||
8 | * published by the Free Software Foundation; either version 2.1 of | ||
9 | * the License, or (at your option) any later version. | ||
10 | * | ||
11 | * Libgcrypt is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * GNU Lesser General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU Lesser General Public | ||
17 | * License along with this program; if not, write to the Free Software | ||
18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | ||
19 | * | ||
20 | * Note: This code is heavily based on the GNU MP Library. | ||
21 | * Actually it's the same code with only minor changes in the | ||
22 | * way the data is stored; this is to support the abstraction | ||
23 | * of an optional secure memory allocation which may be used | ||
24 | * to avoid revealing of sensitive data due to paging etc. | ||
25 | */ | ||
26 | |||
27 | #ifndef G10_MPI_H | ||
28 | #define G10_MPI_H | ||
29 | |||
30 | #include <config.h> | ||
31 | #include <stdio.h> | ||
32 | #include "types.h" | ||
33 | #include "memory.h" | ||
34 | #include "../mpi/mpi-asm-defs.h" | ||
35 | |||
36 | #include "g10lib.h" | ||
37 | |||
38 | #ifndef _GCRYPT_IN_LIBGCRYPT | ||
39 | #error this file should only be used inside libgcrypt | ||
40 | #endif | ||
41 | |||
42 | #ifndef BITS_PER_MPI_LIMB | ||
43 | #if BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_INT | ||
44 | typedef unsigned int mpi_limb_t; | ||
45 | typedef signed int mpi_limb_signed_t; | ||
46 | #elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_LONG | ||
47 | typedef unsigned long int mpi_limb_t; | ||
48 | typedef signed long int mpi_limb_signed_t; | ||
49 | #elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_LONG_LONG | ||
50 | typedef unsigned long long int mpi_limb_t; | ||
51 | typedef signed long long int mpi_limb_signed_t; | ||
52 | #elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_SHORT | ||
53 | typedef unsigned short int mpi_limb_t; | ||
54 | typedef signed short int mpi_limb_signed_t; | ||
55 | #else | ||
56 | #error BYTES_PER_MPI_LIMB does not match any C type | ||
57 | #endif | ||
58 | #define BITS_PER_MPI_LIMB (8*BYTES_PER_MPI_LIMB) | ||
59 | #endif /*BITS_PER_MPI_LIMB*/ | ||
60 | |||
61 | #define DBG_MPI _gcry_get_debug_flag( 2 ); | ||
62 | |||
63 | struct gcry_mpi { | ||
64 | int alloced; /* array size (# of allocated limbs) */ | ||
65 | int nlimbs; /* number of valid limbs */ | ||
66 | int sign; /* indicates a negative number and is used for opaque | ||
67 | * MPIs to store the length */ | ||
68 | unsigned flags; /* bit 0: array must be allocated in secure memory space */ | ||
69 | /* bit 2: the limb is a pointer to some m_alloced data */ | ||
70 | mpi_limb_t *d; /* array with the limbs */ | ||
71 | }; | ||
72 | |||
73 | #define MPI_NULL NULL | ||
74 | |||
75 | #define mpi_get_nlimbs(a) ((a)->nlimbs) | ||
76 | #define mpi_is_neg(a) ((a)->sign) | ||
77 | |||
78 | /*-- mpiutil.c --*/ | ||
79 | |||
80 | #ifdef M_DEBUG | ||
81 | #define mpi_alloc(n)_gcry_mpi_debug_alloc((n), M_DBGINFO( __LINE__ ) ) | ||
82 | #define mpi_alloc_secure(n) _gcry_mpi_debug_alloc_secure((n), M_DBGINFO( __LINE__ ) ) | ||
83 | #define mpi_free(a)_gcry_mpi_debug_free((a), M_DBGINFO(__LINE__) ) | ||
84 | #define mpi_resize(a,b) _gcry_mpi_debug_resize((a),(b), M_DBGINFO(__LINE__) ) | ||
85 | #define mpi_copy(a) _gcry_mpi_debug_copy((a), M_DBGINFO(__LINE__) ) | ||
86 | gcry_mpi_t _gcry_mpi_debug_alloc( unsigned nlimbs, const char *info ); | ||
87 | gcry_mpi_t _gcry_mpi_debug_alloc_secure( unsigned nlimbs, const char *info ); | ||
88 | void _gcry_mpi_debug_free( gcry_mpi_t a, const char *info ); | ||
89 | void _gcry_mpi_debug_resize( gcry_mpi_t a, unsigned nlimbs, const char *info ); | ||
90 | gcry_mpi_t _gcry_mpi_debug_copy( gcry_mpi_t a, const char *info); | ||
91 | #else | ||
92 | #define mpi_alloc(n) _gcry_mpi_alloc((n) ) | ||
93 | #define mpi_alloc_secure(n) _gcry_mpi_alloc_secure((n) ) | ||
94 | #define mpi_free(a) _gcry_mpi_free((a) ) | ||
95 | #define mpi_resize(a,b) _gcry_mpi_resize((a),(b)) | ||
96 | #define mpi_copy(a) _gcry_mpi_copy((a)) | ||
97 | gcry_mpi_t _gcry_mpi_alloc( unsigned nlimbs ); | ||
98 | gcry_mpi_t _gcry_mpi_alloc_secure( unsigned nlimbs ); | ||
99 | void _gcry_mpi_free( gcry_mpi_t a ); | ||
100 | void _gcry_mpi_resize( gcry_mpi_t a, unsigned nlimbs ); | ||
101 | gcry_mpi_t _gcry_mpi_copy( gcry_mpi_t a ); | ||
102 | #endif | ||
103 | #define mpi_is_opaque(a) ((a) && ((a)->flags&4)) | ||
104 | #define mpi_is_secure(a) ((a) && ((a)->flags&1)) | ||
105 | #define mpi_clear(a) _gcry_mpi_clear ((a)) | ||
106 | #define mpi_alloc_like(a) _gcry_mpi_alloc_like((a)) | ||
107 | #define mpi_set(a,b) _gcry_mpi_set ((a),(b)) | ||
108 | #define mpi_set_ui(a,b) _gcry_mpi_set_ui ((a),(b)) | ||
109 | #define mpi_alloc_set_ui(a) _gcry_mpi_alloc_set_ui ((a)) | ||
110 | #define mpi_m_check(a) _gcry_mpi_m_check ((a)) | ||
111 | #define mpi_swap(a,b) _gcry_mpi_swap ((a),(b)) | ||
112 | |||
113 | void _gcry_mpi_clear( gcry_mpi_t a ); | ||
114 | gcry_mpi_t _gcry_mpi_alloc_like( gcry_mpi_t a ); | ||
115 | void _gcry_mpi_set( gcry_mpi_t w, gcry_mpi_t u); | ||
116 | void _gcry_mpi_set_ui( gcry_mpi_t w, ulong u); | ||
117 | gcry_mpi_t _gcry_mpi_alloc_set_ui( unsigned long u); | ||
118 | void _gcry_mpi_m_check( gcry_mpi_t a ); | ||
119 | void _gcry_mpi_swap( gcry_mpi_t a, gcry_mpi_t b); | ||
120 | |||
121 | /*-- mpicoder.c --*/ | ||
122 | void _gcry_log_mpidump( const char *text, gcry_mpi_t a ); | ||
123 | u32 _gcry_mpi_get_keyid( gcry_mpi_t a, u32 *keyid ); | ||
124 | byte *_gcry_mpi_get_buffer( gcry_mpi_t a, unsigned *nbytes, int *sign ); | ||
125 | byte *_gcry_mpi_get_secure_buffer( gcry_mpi_t a, unsigned *nbytes, int *sign ); | ||
126 | void _gcry_mpi_set_buffer( gcry_mpi_t a, const byte *buffer, unsigned nbytes, int sign ); | ||
127 | |||
128 | #define log_mpidump _gcry_log_mpidump | ||
129 | |||
130 | /*-- mpi-add.c --*/ | ||
131 | #define mpi_add_ui(w,u,v) gcry_mpi_add_ui((w),(u),(v)) | ||
132 | #define mpi_add(w,u,v) gcry_mpi_add ((w),(u),(v)) | ||
133 | #define mpi_addm(w,u,v,m) gcry_mpi_addm ((w),(u),(v),(m)) | ||
134 | #define mpi_sub_ui(w,u,v) gcry_mpi_sub_ui ((w),(u),(v)) | ||
135 | #define mpi_sub(w,u,v) gcry_mpi_sub ((w),(u),(v)) | ||
136 | #define mpi_subm(w,u,v,m) gcry_mpi_subm ((w),(u),(v),(m)) | ||
137 | |||
138 | |||
139 | /*-- mpi-mul.c --*/ | ||
140 | #define mpi_mul_ui(w,u,v) gcry_mpi_mul_ui ((w),(u),(v)) | ||
141 | #define mpi_mul_2exp(w,u,v) gcry_mpi_mul_2exp ((w),(u),(v)) | ||
142 | #define mpi_mul(w,u,v) gcry_mpi_mul ((w),(u),(v)) | ||
143 | #define mpi_mulm(w,u,v,m) gcry_mpi_mulm ((w),(u),(v),(m)) | ||
144 | |||
145 | |||
146 | /*-- mpi-div.c --*/ | ||
147 | #define mpi_fdiv_r_ui(a,b,c) _gcry_mpi_fdiv_r_ui((a),(b),(c)) | ||
148 | #define mpi_fdiv_r(a,b,c) _gcry_mpi_fdiv_r((a),(b),(c)) | ||
149 | #define mpi_fdiv_q(a,b,c) _gcry_mpi_fdiv_q((a),(b),(c)) | ||
150 | #define mpi_fdiv_qr(a,b,c,d) _gcry_mpi_fdiv_qr((a),(b),(c),(d)) | ||
151 | #define mpi_tdiv_r(a,b,c) _gcry_mpi_tdiv_r((a),(b),(c)) | ||
152 | #define mpi_tdiv_qr(a,b,c,d) _gcry_mpi_tdiv_qr((a),(b),(c),(d)) | ||
153 | #define mpi_tdiv_q_2exp(a,b,c) _gcry_mpi_tdiv_q_2exp((a),(b),(c)) | ||
154 | #define mpi_divisible_ui(a,b) _gcry_mpi_divisible_ui((a),(b)) | ||
155 | ulong _gcry_mpi_fdiv_r_ui( gcry_mpi_t rem, gcry_mpi_t dividend, ulong divisor ); | ||
156 | void _gcry_mpi_fdiv_r( gcry_mpi_t rem, gcry_mpi_t dividend, gcry_mpi_t divisor ); | ||
157 | void _gcry_mpi_fdiv_q( gcry_mpi_t quot, gcry_mpi_t dividend, gcry_mpi_t divisor ); | ||
158 | void _gcry_mpi_fdiv_qr( gcry_mpi_t quot, gcry_mpi_t rem, gcry_mpi_t dividend, gcry_mpi_t divisor ); | ||
159 | void _gcry_mpi_tdiv_r( gcry_mpi_t rem, gcry_mpi_t num, gcry_mpi_t den); | ||
160 | void _gcry_mpi_tdiv_qr( gcry_mpi_t quot, gcry_mpi_t rem, gcry_mpi_t num, gcry_mpi_t den); | ||
161 | void _gcry_mpi_tdiv_q_2exp( gcry_mpi_t w, gcry_mpi_t u, unsigned count ); | ||
162 | int _gcry_mpi_divisible_ui(gcry_mpi_t dividend, ulong divisor ); | ||
163 | |||
164 | /*-- mpi-gcd.c --*/ | ||
165 | |||
166 | /*-- mpi-mpow.c --*/ | ||
167 | #define mpi_mulpowm(a,b,c,d) _gcry_mpi_mulpowm ((a),(b),(c),(d)) | ||
168 | void _gcry_mpi_mulpowm( gcry_mpi_t res, gcry_mpi_t *basearray, gcry_mpi_t *exparray, gcry_mpi_t mod); | ||
169 | |||
170 | /*-- mpi-cmp.c --*/ | ||
171 | #define mpi_cmp_ui(a,b) gcry_mpi_cmp_ui ((a),(b)) | ||
172 | #define mpi_cmp(a,b) gcry_mpi_cmp ((a),(b)) | ||
173 | int gcry_mpi_cmp_ui( gcry_mpi_t u, ulong v ); | ||
174 | int gcry_mpi_cmp( gcry_mpi_t u, gcry_mpi_t v ); | ||
175 | |||
176 | /*-- mpi-scan.c --*/ | ||
177 | #define mpi_trailing_zeros(a) _gcry_mpi_trailing_zeros ((a)) | ||
178 | int _gcry_mpi_getbyte( gcry_mpi_t a, unsigned idx ); | ||
179 | void _gcry_mpi_putbyte( gcry_mpi_t a, unsigned idx, int value ); | ||
180 | unsigned _gcry_mpi_trailing_zeros( gcry_mpi_t a ); | ||
181 | |||
182 | /*-- mpi-bit.c --*/ | ||
183 | #define mpi_normalize(a) _gcry_mpi_normalize ((a)) | ||
184 | #define mpi_get_nbits(a) gcry_mpi_get_nbits ((a)) | ||
185 | #define mpi_test_bit(a,b) gcry_mpi_test_bit ((a),(b)) | ||
186 | #define mpi_set_bit(a,b) gcry_mpi_set_bit ((a),(b)) | ||
187 | #define mpi_set_highbit(a,b) gcry_mpi_set_highbit ((a),(b)) | ||
188 | #define mpi_clear_bit(a,b) gcry_mpi_clear_bit ((a),(b)) | ||
189 | #define mpi_clear_highbit(a,b) gcry_mpi_clear_highbit ((a),(b)) | ||
190 | #define mpi_rshift(a,b,c) gcry_mpi_rshift ((a),(b),(c)) | ||
191 | |||
192 | void _gcry_mpi_normalize( gcry_mpi_t a ); | ||
193 | |||
194 | /*-- mpi-inv.c --*/ | ||
195 | #define mpi_invm(a,b,c) _gcry_mpi_invm ((a),(b),(c)) | ||
196 | void _gcry_mpi_invm( gcry_mpi_t x, gcry_mpi_t u, gcry_mpi_t v ); | ||
197 | |||
198 | |||
199 | #endif /*G10_MPI_H*/ | ||
diff --git a/pwmanager/libcrypt/crypt/secmem.c b/pwmanager/libcrypt/crypt/secmem.c new file mode 100644 index 0000000..163bf20 --- a/dev/null +++ b/pwmanager/libcrypt/crypt/secmem.c | |||
@@ -0,0 +1,653 @@ | |||
1 | /* secmem.c -memory allocation from a secure heap | ||
2 | * Copyright (C) 1998, 1999, 2000, 2001, 2002, | ||
3 | * 2003 Free Software Foundation, Inc. | ||
4 | * | ||
5 | * This file is part of Libgcrypt. | ||
6 | * | ||
7 | * Libgcrypt is free software; you can redistribute it and/or modify | ||
8 | * it under the terms of the GNU Lesser general Public License as | ||
9 | * published by the Free Software Foundation; either version 2.1 of | ||
10 | * the License, or (at your option) any later version. | ||
11 | * | ||
12 | * Libgcrypt is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | * GNU Lesser General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU Lesser General Public | ||
18 | * License along with this program; if not, write to the Free Software | ||
19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | ||
20 | */ | ||
21 | |||
22 | #include <config.h> | ||
23 | #include <stdio.h> | ||
24 | #include <stdlib.h> | ||
25 | #include <string.h> | ||
26 | #include <errno.h> | ||
27 | #include <stdarg.h> | ||
28 | #include <unistd.h> | ||
29 | #include <stddef.h> | ||
30 | |||
31 | #if defined(HAVE_MLOCK) || defined(HAVE_MMAP) | ||
32 | #include <sys/mman.h> | ||
33 | #include <sys/types.h> | ||
34 | #include <fcntl.h> | ||
35 | #ifdef USE_CAPABILITIES | ||
36 | #include <sys/capability.h> | ||
37 | #endif | ||
38 | #endif | ||
39 | |||
40 | #include "ath.h" | ||
41 | #include "g10lib.h" | ||
42 | #include "secmem.h" | ||
43 | |||
44 | #if defined (MAP_ANON) && ! defined (MAP_ANONYMOUS) | ||
45 | #define MAP_ANONYMOUS MAP_ANON | ||
46 | #endif | ||
47 | |||
48 | #define DEFAULT_POOL_SIZE 16384 | ||
49 | #define DEFAULT_PAGE_SIZE 4096 | ||
50 | |||
51 | typedef struct memblock | ||
52 | { | ||
53 | unsigned size; /* Size of the memory available to the | ||
54 | user. */ | ||
55 | int flags; /* See below. */ | ||
56 | PROPERLY_ALIGNED_TYPE aligned; | ||
57 | } memblock_t; | ||
58 | |||
59 | /* This flag specifies that the memory block is in use. */ | ||
60 | #define MB_FLAG_ACTIVE 1 << 0 | ||
61 | |||
62 | /* The pool of secure memory. */ | ||
63 | static void *pool; | ||
64 | |||
65 | /* Size of POOL in bytes. */ | ||
66 | static size_t pool_size; | ||
67 | |||
68 | /* True, if the memory pool is ready for use. May be checked in an | ||
69 | atexit function. */ | ||
70 | static volatile int pool_okay; | ||
71 | |||
72 | /* True, if the memory pool is mmapped. */ | ||
73 | static volatile int pool_is_mmapped; | ||
74 | |||
75 | /* FIXME? */ | ||
76 | static int disable_secmem; | ||
77 | static int show_warning; | ||
78 | static int no_warning; | ||
79 | static int suspend_warning; | ||
80 | |||
81 | /* Stats. */ | ||
82 | static unsigned int cur_alloced, cur_blocks; | ||
83 | |||
84 | /* Lock protecting accesses to the memory pool. */ | ||
85 | static ath_mutex_t secmem_lock; | ||
86 | |||
87 | /* Convenient macros. */ | ||
88 | #define SECMEM_LOCK ath_mutex_lock (&secmem_lock) | ||
89 | #define SECMEM_UNLOCK ath_mutex_unlock (&secmem_lock) | ||
90 | |||
91 | /* The size of the memblock structure; this does not include the | ||
92 | memory that is available to the user. */ | ||
93 | #define BLOCK_HEAD_SIZE \ | ||
94 | offsetof (memblock_t, aligned) | ||
95 | |||
96 | /* Convert an address into the according memory block structure. */ | ||
97 | #define ADDR_TO_BLOCK(addr) \ | ||
98 | (memblock_t *) ((char *) addr - BLOCK_HEAD_SIZE) | ||
99 | |||
100 | /* Check wether MB is a valid block. */ | ||
101 | #define BLOCK_VALID(mb) \ | ||
102 | (((char *) mb - (char *) pool) < pool_size) | ||
103 | |||
104 | /* Update the stats. */ | ||
105 | static void | ||
106 | stats_update (size_t add, size_t sub) | ||
107 | { | ||
108 | if (add) | ||
109 | { | ||
110 | cur_alloced += add; | ||
111 | cur_blocks++; | ||
112 | } | ||
113 | if (sub) | ||
114 | { | ||
115 | cur_alloced -= sub; | ||
116 | cur_blocks--; | ||
117 | } | ||
118 | } | ||
119 | |||
120 | /* Return the block following MB or NULL, if MB is the last block. */ | ||
121 | static memblock_t * | ||
122 | mb_get_next (memblock_t *mb) | ||
123 | { | ||
124 | memblock_t *mb_next; | ||
125 | |||
126 | mb_next = (memblock_t *) ((char *) mb + BLOCK_HEAD_SIZE + mb->size); | ||
127 | |||
128 | if (! BLOCK_VALID (mb_next)) | ||
129 | mb_next = NULL; | ||
130 | |||
131 | return mb_next; | ||
132 | } | ||
133 | |||
134 | /* Return the block preceeding MB or NULL, if MB is the first | ||
135 | block. */ | ||
136 | static memblock_t * | ||
137 | mb_get_prev (memblock_t *mb) | ||
138 | { | ||
139 | memblock_t *mb_prev, *mb_next; | ||
140 | |||
141 | if (mb == pool) | ||
142 | mb_prev = NULL; | ||
143 | else | ||
144 | { | ||
145 | mb_prev = (memblock_t *) pool; | ||
146 | while (1) | ||
147 | { | ||
148 | mb_next = mb_get_next (mb_prev); | ||
149 | if (mb_next == mb) | ||
150 | break; | ||
151 | else | ||
152 | mb_prev = mb_next; | ||
153 | } | ||
154 | } | ||
155 | |||
156 | return mb_prev; | ||
157 | } | ||
158 | |||
159 | /* If the preceeding block of MB and/or the following block of MB | ||
160 | exist and are not active, merge them to form a bigger block. */ | ||
161 | static void | ||
162 | mb_merge (memblock_t *mb) | ||
163 | { | ||
164 | memblock_t *mb_prev, *mb_next; | ||
165 | |||
166 | mb_prev = mb_get_prev (mb); | ||
167 | mb_next = mb_get_next (mb); | ||
168 | |||
169 | if (mb_prev && (! (mb_prev->flags & MB_FLAG_ACTIVE))) | ||
170 | { | ||
171 | mb_prev->size += BLOCK_HEAD_SIZE + mb->size; | ||
172 | mb = mb_prev; | ||
173 | } | ||
174 | if (mb_next && (! (mb_next->flags & MB_FLAG_ACTIVE))) | ||
175 | mb->size += BLOCK_HEAD_SIZE + mb_next->size; | ||
176 | } | ||
177 | |||
178 | /* Return a new block, which can hold SIZE bytes. */ | ||
179 | static memblock_t * | ||
180 | mb_get_new (memblock_t *block, size_t size) | ||
181 | { | ||
182 | memblock_t *mb, *mb_split; | ||
183 | |||
184 | for (mb = block; BLOCK_VALID (mb); mb = mb_get_next (mb)) | ||
185 | if (! (mb->flags & MB_FLAG_ACTIVE) && mb->size >= size) | ||
186 | { | ||
187 | /* Found a free block. */ | ||
188 | mb->flags |= MB_FLAG_ACTIVE; | ||
189 | |||
190 | if (mb->size - size > BLOCK_HEAD_SIZE) | ||
191 | { | ||
192 | /* Split block. */ | ||
193 | |||
194 | mb_split = (memblock_t *) (((char *) mb) + BLOCK_HEAD_SIZE + size); | ||
195 | mb_split->size = mb->size - size - BLOCK_HEAD_SIZE; | ||
196 | mb_split->flags = 0; | ||
197 | |||
198 | mb->size = size; | ||
199 | |||
200 | mb_merge (mb_split); | ||
201 | |||
202 | } | ||
203 | |||
204 | break; | ||
205 | } | ||
206 | |||
207 | if (! BLOCK_VALID (mb)) | ||
208 | mb = NULL; | ||
209 | |||
210 | return mb; | ||
211 | } | ||
212 | |||
213 | /* Print a warning message. */ | ||
214 | static void | ||
215 | print_warn (void) | ||
216 | { | ||
217 | if (!no_warning) | ||
218 | log_info (_("Warning: using insecure memory!\n")); | ||
219 | } | ||
220 | |||
221 | /* Lock the memory pages into core and drop privileges. */ | ||
222 | static void | ||
223 | lock_pool (void *p, size_t n) | ||
224 | { | ||
225 | #if defined(USE_CAPABILITIES) && defined(HAVE_MLOCK) | ||
226 | int err; | ||
227 | |||
228 | cap_set_proc (cap_from_text ("cap_ipc_lock+ep")); | ||
229 | err = mlock (p, n); | ||
230 | if (err && errno) | ||
231 | err = errno; | ||
232 | cap_set_proc (cap_from_text ("cap_ipc_lock+p")); | ||
233 | |||
234 | if (err) | ||
235 | { | ||
236 | if (errno != EPERM | ||
237 | #ifdef EAGAIN/* OpenBSD returns this */ | ||
238 | && errno != EAGAIN | ||
239 | #endif | ||
240 | #ifdef ENOSYS/* Some SCOs return this (function not implemented) */ | ||
241 | && errno != ENOSYS | ||
242 | #endif | ||
243 | #ifdef ENOMEM /* Linux might return this. */ | ||
244 | && errno != ENOMEM | ||
245 | #endif | ||
246 | ) | ||
247 | log_error ("can't lock memory: %s\n", strerror (err)); | ||
248 | show_warning = 1; | ||
249 | } | ||
250 | |||
251 | #elif defined(HAVE_MLOCK) | ||
252 | uid_t uid; | ||
253 | int err; | ||
254 | |||
255 | uid = getuid (); | ||
256 | |||
257 | #ifdef HAVE_BROKEN_MLOCK | ||
258 | /* Under HP/UX mlock segfaults if called by non-root. Note, we have | ||
259 | noch checked whether mlock does really work under AIX where we | ||
260 | also detected a broken nlock. Note further, that using plock () | ||
261 | is not a good idea under AIX. */ | ||
262 | if (uid) | ||
263 | { | ||
264 | errno = EPERM; | ||
265 | err = errno; | ||
266 | } | ||
267 | else | ||
268 | { | ||
269 | err = mlock (p, n); | ||
270 | if (err && errno) | ||
271 | err = errno; | ||
272 | } | ||
273 | #else /* !HAVE_BROKEN_MLOCK */ | ||
274 | err = mlock (p, n); | ||
275 | if (err && errno) | ||
276 | err = errno; | ||
277 | #endif /* !HAVE_BROKEN_MLOCK */ | ||
278 | |||
279 | if (uid && ! geteuid ()) | ||
280 | { | ||
281 | /* check that we really dropped the privs. | ||
282 | * Note: setuid(0) should always fail */ | ||
283 | if (setuid (uid) || getuid () != geteuid () || !setuid (0)) | ||
284 | log_fatal ("failed to reset uid: %s\n", strerror (errno)); | ||
285 | } | ||
286 | |||
287 | if (err) | ||
288 | { | ||
289 | if (errno != EPERM | ||
290 | #ifdef EAGAIN/* OpenBSD returns this. */ | ||
291 | && errno != EAGAIN | ||
292 | #endif | ||
293 | #ifdef ENOSYS/* Some SCOs return this (function not implemented). */ | ||
294 | && errno != ENOSYS | ||
295 | #endif | ||
296 | #ifdef ENOMEM /* Linux might return this. */ | ||
297 | && errno != ENOMEM | ||
298 | #endif | ||
299 | ) | ||
300 | log_error ("can't lock memory: %s\n", strerror (err)); | ||
301 | show_warning = 1; | ||
302 | } | ||
303 | |||
304 | #elif defined ( __QNX__ ) | ||
305 | /* QNX does not page at all, so the whole secure memory stuff does | ||
306 | * not make much sense. However it is still of use because it | ||
307 | * wipes out the memory on a free(). | ||
308 | * Therefore it is sufficient to suppress the warning | ||
309 | */ | ||
310 | #elif defined (HAVE_DOSISH_SYSTEM) || defined (__CYGWIN__) | ||
311 | /* It does not make sense to print such a warning, given the fact that | ||
312 | * this whole Windows !@#$% and their user base are inherently insecure | ||
313 | */ | ||
314 | #elif defined (__riscos__) | ||
315 | /* no virtual memory on RISC OS, so no pages are swapped to disc, | ||
316 | * besides we don't have mmap, so we don't use it! ;-) | ||
317 | * But don't complain, as explained above. | ||
318 | */ | ||
319 | #else | ||
320 | log_info ("Please note that you don't have secure memory on this system\n"); | ||
321 | #endif | ||
322 | } | ||
323 | |||
324 | /* Initialize POOL. */ | ||
325 | static void | ||
326 | init_pool (size_t n) | ||
327 | { | ||
328 | size_t pgsize; | ||
329 | memblock_t *mb; | ||
330 | |||
331 | pool_size = n; | ||
332 | |||
333 | if (disable_secmem) | ||
334 | log_bug ("secure memory is disabled"); | ||
335 | |||
336 | #ifdef HAVE_GETPAGESIZE | ||
337 | pgsize = getpagesize (); | ||
338 | #else | ||
339 | pgsize = DEFAULT_PAGE_SIZE; | ||
340 | #endif | ||
341 | |||
342 | #if HAVE_MMAP | ||
343 | pool_size = (pool_size + pgsize - 1) & ~(pgsize - 1); | ||
344 | #ifdef MAP_ANONYMOUS | ||
345 | pool = mmap (0, pool_size, PROT_READ | PROT_WRITE, | ||
346 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | ||
347 | #else /* map /dev/zero instead */ | ||
348 | { | ||
349 | int fd; | ||
350 | |||
351 | fd = open ("/dev/zero", O_RDWR); | ||
352 | if (fd == -1) | ||
353 | { | ||
354 | log_error ("can't open /dev/zero: %s\n", strerror (errno)); | ||
355 | pool = (void *) -1; | ||
356 | } | ||
357 | else | ||
358 | { | ||
359 | pool = mmap (0, pool_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); | ||
360 | } | ||
361 | } | ||
362 | #endif | ||
363 | if (pool == (void *) -1) | ||
364 | log_info ("can't mmap pool of %u bytes: %s - using malloc\n", | ||
365 | (unsigned) pool_size, strerror (errno)); | ||
366 | else | ||
367 | { | ||
368 | pool_is_mmapped = 1; | ||
369 | pool_okay = 1; | ||
370 | } | ||
371 | |||
372 | #endif | ||
373 | if (!pool_okay) | ||
374 | { | ||
375 | pool = malloc (pool_size); | ||
376 | if (!pool) | ||
377 | log_fatal ("can't allocate memory pool of %u bytes\n", | ||
378 | (unsigned) pool_size); | ||
379 | else | ||
380 | pool_okay = 1; | ||
381 | } | ||
382 | |||
383 | /* Initialize first memory block. */ | ||
384 | mb = (memblock_t *) pool; | ||
385 | mb->size = pool_size; | ||
386 | mb->flags = 0; | ||
387 | } | ||
388 | |||
389 | void | ||
390 | _gcry_secmem_set_flags (unsigned flags) | ||
391 | { | ||
392 | int was_susp; | ||
393 | |||
394 | SECMEM_LOCK; | ||
395 | |||
396 | was_susp = suspend_warning; | ||
397 | no_warning = flags & GCRY_SECMEM_FLAG_NO_WARNING; | ||
398 | suspend_warning = flags & GCRY_SECMEM_FLAG_SUSPEND_WARNING; | ||
399 | |||
400 | /* and now issue the warning if it is not longer suspended */ | ||
401 | if (was_susp && !suspend_warning && show_warning) | ||
402 | { | ||
403 | show_warning = 0; | ||
404 | print_warn (); | ||
405 | } | ||
406 | |||
407 | SECMEM_UNLOCK; | ||
408 | } | ||
409 | |||
410 | unsigned | ||
411 | _gcry_secmem_get_flags (void) | ||
412 | { | ||
413 | unsigned flags; | ||
414 | |||
415 | SECMEM_LOCK; | ||
416 | |||
417 | flags = no_warning ? GCRY_SECMEM_FLAG_NO_WARNING : 0; | ||
418 | flags |= suspend_warning ? GCRY_SECMEM_FLAG_SUSPEND_WARNING : 0; | ||
419 | |||
420 | SECMEM_UNLOCK; | ||
421 | |||
422 | return flags; | ||
423 | } | ||
424 | |||
425 | /* Initialize the secure memory system. If running with the necessary | ||
426 | privileges, the secure memory pool will be locked into the core in | ||
427 | order to prevent page-outs of the data. Furthermore allocated | ||
428 | secure memory will be wiped out when released. */ | ||
429 | void | ||
430 | _gcry_secmem_init (size_t n) | ||
431 | { | ||
432 | SECMEM_LOCK; | ||
433 | |||
434 | if (!n) | ||
435 | { | ||
436 | #ifdef USE_CAPABILITIES | ||
437 | /* drop all capabilities */ | ||
438 | cap_set_proc (cap_from_text ("all-eip")); | ||
439 | |||
440 | #elif !defined(HAVE_DOSISH_SYSTEM) | ||
441 | uid_t uid; | ||
442 | |||
443 | disable_secmem = 1; | ||
444 | uid = getuid (); | ||
445 | if (uid != geteuid ()) | ||
446 | { | ||
447 | if (setuid (uid) || getuid () != geteuid () || !setuid (0)) | ||
448 | log_fatal ("failed to drop setuid\n"); | ||
449 | } | ||
450 | #endif | ||
451 | } | ||
452 | else | ||
453 | { | ||
454 | if (n < DEFAULT_POOL_SIZE) | ||
455 | n = DEFAULT_POOL_SIZE; | ||
456 | if (! pool_okay) | ||
457 | { | ||
458 | init_pool (n); | ||
459 | if (! geteuid ()) | ||
460 | lock_pool (pool, n); | ||
461 | else if (!no_warning) | ||
462 | log_info ("Secure memory is not locked into core\n"); | ||
463 | } | ||
464 | else | ||
465 | log_error ("Oops, secure memory pool already initialized\n"); | ||
466 | } | ||
467 | |||
468 | SECMEM_UNLOCK; | ||
469 | } | ||
470 | |||
471 | |||
472 | static void * | ||
473 | _gcry_secmem_malloc_internal (size_t size) | ||
474 | { | ||
475 | memblock_t *mb; | ||
476 | |||
477 | if (!pool_okay) | ||
478 | { | ||
479 | log_info (_ | ||
480 | ("operation is not possible without initialized secure memory\n")); | ||
481 | exit (2); | ||
482 | } | ||
483 | if (show_warning && !suspend_warning) | ||
484 | { | ||
485 | show_warning = 0; | ||
486 | print_warn (); | ||
487 | } | ||
488 | |||
489 | /* Blocks are always a multiple of 32. */ | ||
490 | size = ((size + 31) / 32) * 32; | ||
491 | |||
492 | mb = mb_get_new ((memblock_t *) pool, size); | ||
493 | if (mb) | ||
494 | stats_update (size, 0); | ||
495 | |||
496 | return mb ? &mb->aligned.c : NULL; | ||
497 | } | ||
498 | |||
499 | void * | ||
500 | _gcry_secmem_malloc (size_t size) | ||
501 | { | ||
502 | void *p; | ||
503 | |||
504 | SECMEM_LOCK; | ||
505 | p = _gcry_secmem_malloc_internal (size); | ||
506 | SECMEM_UNLOCK; | ||
507 | |||
508 | return p; | ||
509 | } | ||
510 | |||
511 | static void | ||
512 | _gcry_secmem_free_internal (void *a) | ||
513 | { | ||
514 | memblock_t *mb; | ||
515 | int size; | ||
516 | |||
517 | if (!a) | ||
518 | return; | ||
519 | |||
520 | mb = ADDR_TO_BLOCK (a); | ||
521 | size = mb->size; | ||
522 | |||
523 | /* This does not make much sense: probably this memory is held in the | ||
524 | * cache. We do it anyway: */ | ||
525 | #define MB_WIPE_OUT(byte) \ | ||
526 | memset ((memblock_t *) ((char *) mb + BLOCK_HEAD_SIZE), (byte), size); | ||
527 | |||
528 | MB_WIPE_OUT (0xff); | ||
529 | MB_WIPE_OUT (0xaa); | ||
530 | MB_WIPE_OUT (0x55); | ||
531 | MB_WIPE_OUT (0x00); | ||
532 | |||
533 | stats_update (0, size); | ||
534 | |||
535 | mb->flags &= ~MB_FLAG_ACTIVE; | ||
536 | |||
537 | /* Update stats. */ | ||
538 | |||
539 | mb_merge (mb); | ||
540 | } | ||
541 | |||
542 | /* Wipe out and release memory. */ | ||
543 | void | ||
544 | _gcry_secmem_free (void *a) | ||
545 | { | ||
546 | SECMEM_LOCK; | ||
547 | _gcry_secmem_free_internal (a); | ||
548 | SECMEM_UNLOCK; | ||
549 | } | ||
550 | |||
551 | /* Realloc memory. */ | ||
552 | void * | ||
553 | _gcry_secmem_realloc (void *p, size_t newsize) | ||
554 | { | ||
555 | memblock_t *mb; | ||
556 | size_t size; | ||
557 | void *a; | ||
558 | |||
559 | SECMEM_LOCK; | ||
560 | |||
561 | mb = (memblock_t *) ((char *) p - ((size_t) &((memblock_t *) 0)->aligned.c)); | ||
562 | size = mb->size; | ||
563 | if (newsize < size) | ||
564 | { | ||
565 | /* It is easier to not shrink the memory. */ | ||
566 | a = p; | ||
567 | } | ||
568 | else | ||
569 | { | ||
570 | a = _gcry_secmem_malloc_internal (newsize); | ||
571 | if (a) | ||
572 | { | ||
573 | memcpy (a, p, size); | ||
574 | memset ((char *) a + size, 0, newsize - size); | ||
575 | _gcry_secmem_free_internal (p); | ||
576 | } | ||
577 | } | ||
578 | |||
579 | SECMEM_UNLOCK; | ||
580 | |||
581 | return a; | ||
582 | } | ||
583 | |||
584 | int | ||
585 | _gcry_private_is_secure (const void *p) | ||
586 | { | ||
587 | int ret = 0; | ||
588 | |||
589 | SECMEM_LOCK; | ||
590 | |||
591 | if (pool_okay && BLOCK_VALID (ADDR_TO_BLOCK (p))) | ||
592 | ret = 1; | ||
593 | |||
594 | SECMEM_UNLOCK; | ||
595 | |||
596 | return ret; | ||
597 | } | ||
598 | |||
599 | |||
600 | /**************** | ||
601 | * Warning: This code might be called by an interrupt handler | ||
602 | * and frankly, there should really be such a handler, | ||
603 | * to make sure that the memory is wiped out. | ||
604 | * We hope that the OS wipes out mlocked memory after | ||
605 | * receiving a SIGKILL - it really should do so, otherwise | ||
606 | * there is no chance to get the secure memory cleaned. | ||
607 | */ | ||
608 | void | ||
609 | _gcry_secmem_term () | ||
610 | { | ||
611 | if (!pool_okay) | ||
612 | return; | ||
613 | |||
614 | wipememory2 (pool, 0xff, pool_size); | ||
615 | wipememory2 (pool, 0xaa, pool_size); | ||
616 | wipememory2 (pool, 0x55, pool_size); | ||
617 | wipememory2 (pool, 0x00, pool_size); | ||
618 | #if HAVE_MMAP | ||
619 | if (pool_is_mmapped) | ||
620 | munmap (pool, pool_size); | ||
621 | #endif | ||
622 | pool = NULL; | ||
623 | pool_okay = 0; | ||
624 | pool_size = 0; | ||
625 | } | ||
626 | |||
627 | |||
628 | void | ||
629 | _gcry_secmem_dump_stats () | ||
630 | { | ||
631 | #if 1 | ||
632 | SECMEM_LOCK; | ||
633 | |||
634 | if (pool_okay) | ||
635 | log_info ("secmem usage: %u/%lu bytes in %u blocks\n", | ||
636 | cur_alloced, (unsigned long)pool_size, cur_blocks); | ||
637 | SECMEM_UNLOCK; | ||
638 | #else | ||
639 | memblock_t *mb; | ||
640 | int i; | ||
641 | |||
642 | SECMEM_LOCK; | ||
643 | |||
644 | for (i = 0, mb = (memblock_t *) pool; | ||
645 | BLOCK_VALID (mb); | ||
646 | mb = mb_get_next (mb), i++) | ||
647 | log_info ("SECMEM: [%s] block: %i; size: %i\n", | ||
648 | (mb->flags & MB_FLAG_ACTIVE) ? "used" : "free", | ||
649 | i, | ||
650 | mb->size); | ||
651 | SECMEM_UNLOCK; | ||
652 | #endif | ||
653 | } | ||
diff --git a/pwmanager/libcrypt/crypt/secmem.h b/pwmanager/libcrypt/crypt/secmem.h new file mode 100644 index 0000000..54e16ba --- a/dev/null +++ b/pwmanager/libcrypt/crypt/secmem.h | |||
@@ -0,0 +1,38 @@ | |||
1 | /* secmem.h - internal definitions for secmem | ||
2 | *Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc. | ||
3 | * | ||
4 | * This file is part of Libgcrypt. | ||
5 | * | ||
6 | * Libgcrypt is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU Lesser general Public License as | ||
8 | * published by the Free Software Foundation; either version 2.1 of | ||
9 | * the License, or (at your option) any later version. | ||
10 | * | ||
11 | * Libgcrypt is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * GNU Lesser General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU Lesser General Public | ||
17 | * License along with this program; if not, write to the Free Software | ||
18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | ||
19 | */ | ||
20 | |||
21 | #ifndef G10_SECMEM_H | ||
22 | #define G10_SECMEM_H 1 | ||
23 | |||
24 | void _gcry_secmem_init (size_t npool); | ||
25 | void _gcry_secmem_term (void); | ||
26 | void *_gcry_secmem_malloc (size_t size) GCC_ATTR_MALLOC; | ||
27 | void *_gcry_secmem_realloc (void *a, size_t newsize); | ||
28 | void _gcry_secmem_free (void *a); | ||
29 | void _gcry_secmem_dump_stats (void); | ||
30 | void _gcry_secmem_set_flags (unsigned flags); | ||
31 | unsigned _gcry_secmem_get_flags(void) GCC_ATTR_PURE; | ||
32 | int _gcry_private_is_secure (const void *p) GCC_ATTR_PURE; | ||
33 | |||
34 | /* Flags for _gcry_secmem_{set,get}_flags. */ | ||
35 | #define GCRY_SECMEM_FLAG_NO_WARNING 1 << 0 | ||
36 | #define GCRY_SECMEM_FLAG_SUSPEND_WARNING 1 << 1 | ||
37 | |||
38 | #endif /* G10_SECMEM_H */ | ||
diff --git a/pwmanager/libcrypt/crypt/sexp.c b/pwmanager/libcrypt/crypt/sexp.c new file mode 100644 index 0000000..c19dc33 --- a/dev/null +++ b/pwmanager/libcrypt/crypt/sexp.c | |||
@@ -0,0 +1,1804 @@ | |||
1 | /* sexp.c - S-Expression handling | ||
2 | * Copyright (C) 1999, 2000, 2001, 2002, 2003, | ||
3 | * 2004 Free Software Foundation, Inc. | ||
4 | * | ||
5 | * This file is part of Libgcrypt. | ||
6 | * | ||
7 | * Libgcrypt is free software; you can redistribute it and/or modify | ||
8 | * it under the terms of the GNU Lesser general Public License as | ||
9 | * published by the Free Software Foundation; either version 2.1 of | ||
10 | * the License, or (at your option) any later version. | ||
11 | * | ||
12 | * Libgcrypt is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | * GNU Lesser General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU Lesser General Public | ||
18 | * License along with this program; if not, write to the Free Software | ||
19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | ||
20 | */ | ||
21 | |||
22 | |||
23 | #include <config.h> | ||
24 | #include <stdio.h> | ||
25 | #include <stdlib.h> | ||
26 | #include <string.h> | ||
27 | #include <stdarg.h> | ||
28 | #include <ctype.h> | ||
29 | #include <assert.h> | ||
30 | |||
31 | #define GCRYPT_NO_MPI_MACROS 1 | ||
32 | #include "g10lib.h" | ||
33 | #include "memory.h" | ||
34 | |||
35 | typedef struct gcry_sexp *NODE; | ||
36 | typedef unsigned short DATALEN; | ||
37 | |||
38 | struct gcry_sexp | ||
39 | { | ||
40 | byte d[1]; | ||
41 | }; | ||
42 | |||
43 | #define ST_STOP 0 | ||
44 | #define ST_DATA 1 /* datalen follows */ | ||
45 | #define ST_HINT 2 /* datalen follows */ | ||
46 | #define ST_OPEN 3 | ||
47 | #define ST_CLOSE 4 | ||
48 | |||
49 | /* the atoi macros assume that the buffer has only valid digits */ | ||
50 | #define atoi_1(p) (*(p) - '0' ) | ||
51 | #define xtoi_1(p) (*(p) <= '9'? (*(p)- '0'): \ | ||
52 | *(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10)) | ||
53 | #define xtoi_2(p) ((xtoi_1(p) * 16) + xtoi_1((p)+1)) | ||
54 | |||
55 | #define TOKEN_SPECIALS "-./_:*+=" | ||
56 | |||
57 | static gcry_error_t | ||
58 | sexp_sscan (gcry_sexp_t *retsexp, size_t *erroff, | ||
59 | const char *buffer, size_t length, int argflag, | ||
60 | va_list arg_ptr, void **arg_list); | ||
61 | |||
62 | #if 0 | ||
63 | static void | ||
64 | dump_mpi( gcry_mpi_t a ) | ||
65 | { | ||
66 | char buffer[1000]; | ||
67 | size_t n = 1000; | ||
68 | |||
69 | if( !a ) | ||
70 | fputs("[no MPI]", stderr ); | ||
71 | else if( gcry_mpi_print( GCRYMPI_FMT_HEX, buffer, &n, a ) ) | ||
72 | fputs("[MPI too large to print]", stderr ); | ||
73 | else | ||
74 | fputs( buffer, stderr ); | ||
75 | } | ||
76 | #endif | ||
77 | |||
78 | static void | ||
79 | dump_string (const byte *p, size_t n, int delim ) | ||
80 | { | ||
81 | for (; n; n--, p++ ) | ||
82 | { | ||
83 | if ((*p & 0x80) || iscntrl( *p ) || *p == delim ) | ||
84 | { | ||
85 | if( *p == '\n' ) | ||
86 | log_printf ("\\n"); | ||
87 | else if( *p == '\r' ) | ||
88 | log_printf ("\\r"); | ||
89 | else if( *p == '\f' ) | ||
90 | log_printf ("\\f"); | ||
91 | else if( *p == '\v' ) | ||
92 | log_printf ("\\v"); | ||
93 | else if( *p == '\b' ) | ||
94 | log_printf ("\\b"); | ||
95 | else if( !*p ) | ||
96 | log_printf ("\\0"); | ||
97 | else | ||
98 | log_printf ("\\x%02x", *p ); | ||
99 | } | ||
100 | else | ||
101 | log_printf ("%c", *p); | ||
102 | } | ||
103 | } | ||
104 | |||
105 | |||
106 | void | ||
107 | gcry_sexp_dump (const gcry_sexp_t a) | ||
108 | { | ||
109 | const byte *p; | ||
110 | int indent = 0; | ||
111 | int type; | ||
112 | |||
113 | if (!a) | ||
114 | { | ||
115 | log_printf ( "[nil]\n"); | ||
116 | return; | ||
117 | } | ||
118 | |||
119 | p = a->d; | ||
120 | while ( (type = *p) != ST_STOP ) | ||
121 | { | ||
122 | p++; | ||
123 | switch ( type ) | ||
124 | { | ||
125 | case ST_OPEN: | ||
126 | log_printf ("%*s[open]\n", 2*indent, ""); | ||
127 | indent++; | ||
128 | break; | ||
129 | case ST_CLOSE: | ||
130 | if( indent ) | ||
131 | indent--; | ||
132 | log_printf ("%*s[close]\n", 2*indent, ""); | ||
133 | break; | ||
134 | case ST_DATA: { | ||
135 | DATALEN n; | ||
136 | memcpy ( &n, p, sizeof n ); | ||
137 | p += sizeof n; | ||
138 | log_printf ("%*s[data=\"", 2*indent, "" ); | ||
139 | dump_string (p, n, '\"' ); | ||
140 | log_printf ("\"]\n"); | ||
141 | p += n; | ||
142 | } | ||
143 | break; | ||
144 | default: | ||
145 | log_printf ("%*s[unknown tag %d]\n", 2*indent, "", type); | ||
146 | break; | ||
147 | } | ||
148 | } | ||
149 | } | ||
150 | |||
151 | /**************** | ||
152 | * Pass list through except when it is an empty list - in that case | ||
153 | * return NULL and release the passed list. | ||
154 | */ | ||
155 | static gcry_sexp_t | ||
156 | normalize ( gcry_sexp_t list ) | ||
157 | { | ||
158 | char *p; | ||
159 | if ( !list ) | ||
160 | return NULL; | ||
161 | p = list->d; | ||
162 | if ( *p == ST_STOP ) { | ||
163 | /* this is "" */ | ||
164 | gcry_sexp_release ( list ); | ||
165 | return NULL; | ||
166 | } | ||
167 | if( *p == ST_OPEN && p[1] == ST_CLOSE ) { | ||
168 | /* this is "()" */ | ||
169 | gcry_sexp_release ( list ); | ||
170 | return NULL; | ||
171 | } | ||
172 | |||
173 | return list; | ||
174 | } | ||
175 | |||
176 | /* Create a new S-expression object by reading LENGTH bytes from | ||
177 | BUFFER, assuming it is canonilized encoded or autodetected encoding | ||
178 | when AUTODETECT is set to 1. With FREEFNC not NULL, ownership of | ||
179 | the buffer is transferred to tyhe newle created object. FREEFNC | ||
180 | should be the freefnc used to release BUFFER; there is no guarantee | ||
181 | at which point this function is called; most likey you want to use | ||
182 | free() or gcry_free(). | ||
183 | |||
184 | Passing LENGTH and AUTODETECT as 0 is allowed to indicate that | ||
185 | BUFFER points to a valid canonical encoded S-expression. A LENGTH | ||
186 | of 0 and AUTODETECT 1 indicates that buffer points to a | ||
187 | null-terminated string. | ||
188 | |||
189 | This function returns 0 and and the pointer to the new object in | ||
190 | RETSEXP or an error code in which case RETSEXP is set to NULL. */ | ||
191 | gcry_error_t | ||
192 | gcry_sexp_create (gcry_sexp_t *retsexp, void *buffer, size_t length, | ||
193 | int autodetect, void (*freefnc)(void*) ) | ||
194 | { | ||
195 | gcry_error_t errcode; | ||
196 | gcry_sexp_t se; | ||
197 | volatile va_list dummy_arg_ptr; | ||
198 | |||
199 | if (!retsexp) | ||
200 | return gcry_error (GPG_ERR_INV_ARG); | ||
201 | *retsexp = NULL; | ||
202 | if (autodetect < 0 || autodetect > 1 || !buffer) | ||
203 | return gcry_error (GPG_ERR_INV_ARG); | ||
204 | |||
205 | if (!length && !autodetect) | ||
206 | { /* What a brave caller to assume that there is really a canonical | ||
207 | encoded S-expression in buffer */ | ||
208 | length = gcry_sexp_canon_len (buffer, 0, NULL, &errcode); | ||
209 | if (!length) | ||
210 | return errcode; | ||
211 | } | ||
212 | else if (!length && autodetect) | ||
213 | { /* buffer is a string */ | ||
214 | length = strlen ((char *)buffer); | ||
215 | } | ||
216 | |||
217 | errcode = sexp_sscan (&se, NULL, buffer, length, 0, dummy_arg_ptr, NULL); | ||
218 | if (errcode) | ||
219 | return errcode; | ||
220 | |||
221 | *retsexp = se; | ||
222 | if (freefnc) | ||
223 | { | ||
224 | /* For now we release the buffer immediately. As soon as we | ||
225 | have changed the internal represenation of S-expression to | ||
226 | the canoncial format - which has the advantage of faster | ||
227 | parsing - we will use this function as a closure in our | ||
228 | GCRYSEXP object and use the BUFFER directly */ | ||
229 | freefnc (buffer); | ||
230 | } | ||
231 | return gcry_error (GPG_ERR_NO_ERROR); | ||
232 | } | ||
233 | |||
234 | /* Same as gcry_sexp_create but don't transfer ownership */ | ||
235 | gcry_error_t | ||
236 | gcry_sexp_new (gcry_sexp_t *retsexp, const void *buffer, size_t length, | ||
237 | int autodetect) | ||
238 | { | ||
239 | return gcry_sexp_create (retsexp, (void *)buffer, length, autodetect, NULL); | ||
240 | } | ||
241 | |||
242 | |||
243 | /**************** | ||
244 | * Release resource of the given SEXP object. | ||
245 | */ | ||
246 | void | ||
247 | gcry_sexp_release( gcry_sexp_t sexp ) | ||
248 | { | ||
249 | if (sexp) | ||
250 | { | ||
251 | if (gcry_is_secure (sexp)) | ||
252 | { | ||
253 | /* Extra paranoid wiping. */ | ||
254 | const byte *p = sexp->d; | ||
255 | int type; | ||
256 | |||
257 | while ( (type = *p) != ST_STOP ) | ||
258 | { | ||
259 | p++; | ||
260 | switch ( type ) | ||
261 | { | ||
262 | case ST_OPEN: | ||
263 | break; | ||
264 | case ST_CLOSE: | ||
265 | break; | ||
266 | case ST_DATA: | ||
267 | { | ||
268 | DATALEN n; | ||
269 | memcpy ( &n, p, sizeof n ); | ||
270 | p += sizeof n; | ||
271 | p += n; | ||
272 | } | ||
273 | break; | ||
274 | default: | ||
275 | break; | ||
276 | } | ||
277 | } | ||
278 | wipememory (sexp->d, p - sexp->d); | ||
279 | } | ||
280 | gcry_free ( sexp ); | ||
281 | } | ||
282 | } | ||
283 | |||
284 | |||
285 | /**************** | ||
286 | * Make a pair from lists a and b, don't use a or b later on. | ||
287 | * Special behaviour: If one is a single element list we put the | ||
288 | * element straight into the new pair. | ||
289 | */ | ||
290 | gcry_sexp_t | ||
291 | gcry_sexp_cons( const gcry_sexp_t a, const gcry_sexp_t b ) | ||
292 | { | ||
293 | /* NYI: Implementation should be quite easy with our new data | ||
294 | representation */ | ||
295 | BUG (); | ||
296 | return NULL; | ||
297 | } | ||
298 | |||
299 | |||
300 | /**************** | ||
301 | * Make a list from all items in the array the end of the array is marked | ||
302 | * with a NULL. | ||
303 | */ | ||
304 | gcry_sexp_t | ||
305 | gcry_sexp_alist( const gcry_sexp_t *array ) | ||
306 | { | ||
307 | /* NYI: Implementation should be quite easy with our new data | ||
308 | representation. */ | ||
309 | BUG (); | ||
310 | return NULL; | ||
311 | } | ||
312 | |||
313 | /**************** | ||
314 | * Make a list from all items, the end of list is indicated by a NULL | ||
315 | */ | ||
316 | gcry_sexp_t | ||
317 | gcry_sexp_vlist( const gcry_sexp_t a, ... ) | ||
318 | { | ||
319 | /* NYI: Implementation should be quite easy with our new data | ||
320 | representation. */ | ||
321 | BUG (); | ||
322 | return NULL; | ||
323 | } | ||
324 | |||
325 | |||
326 | /**************** | ||
327 | * Append n to the list a | ||
328 | * Returns: a new ist (which maybe a) | ||
329 | */ | ||
330 | gcry_sexp_t | ||
331 | gcry_sexp_append( const gcry_sexp_t a, const gcry_sexp_t n ) | ||
332 | { | ||
333 | /* NYI: Implementation should be quite easy with our new data | ||
334 | representation. */ | ||
335 | BUG (); | ||
336 | return NULL; | ||
337 | } | ||
338 | |||
339 | gcry_sexp_t | ||
340 | gcry_sexp_prepend( const gcry_sexp_t a, const gcry_sexp_t n ) | ||
341 | { | ||
342 | /* NYI: Implementation should be quite easy with our new data | ||
343 | representation. */ | ||
344 | BUG (); | ||
345 | return NULL; | ||
346 | } | ||
347 | |||
348 | |||
349 | |||
350 | /**************** | ||
351 | * Locate token in a list. The token must be the car of a sublist. | ||
352 | * Returns: A new list with this sublist or NULL if not found. | ||
353 | */ | ||
354 | gcry_sexp_t | ||
355 | gcry_sexp_find_token( const gcry_sexp_t list, const char *tok, size_t toklen ) | ||
356 | { | ||
357 | const byte *p; | ||
358 | DATALEN n; | ||
359 | |||
360 | if ( !list ) | ||
361 | return NULL; | ||
362 | |||
363 | if( !toklen ) | ||
364 | toklen = strlen(tok); | ||
365 | p = list->d; | ||
366 | while ( *p != ST_STOP ) { | ||
367 | if ( *p == ST_OPEN && p[1] == ST_DATA ) { | ||
368 | const byte *head = p; | ||
369 | |||
370 | p += 2; | ||
371 | memcpy ( &n, p, sizeof n ); p += sizeof n; | ||
372 | if ( n == toklen && !memcmp( p, tok, toklen ) ) { /* found it */ | ||
373 | gcry_sexp_t newlist; | ||
374 | byte *d; | ||
375 | int level = 1; | ||
376 | |||
377 | /* look for the end of the list */ | ||
378 | for ( p += n; level; p++ ) { | ||
379 | if ( *p == ST_DATA ) { | ||
380 | memcpy ( &n, ++p, sizeof n ); | ||
381 | p += sizeof n + n; | ||
382 | p--; /* compensate for later increment */ | ||
383 | } | ||
384 | else if ( *p == ST_OPEN ) { | ||
385 | level++; | ||
386 | } | ||
387 | else if ( *p == ST_CLOSE ) { | ||
388 | level--; | ||
389 | } | ||
390 | else if ( *p == ST_STOP ) { | ||
391 | BUG (); | ||
392 | } | ||
393 | } while ( level ); | ||
394 | n = p - head; | ||
395 | |||
396 | newlist = gcry_xmalloc ( sizeof *newlist + n ); | ||
397 | d = newlist->d; | ||
398 | memcpy ( d, head, n ); d += n; | ||
399 | *d++ = ST_STOP; | ||
400 | return normalize ( newlist ); | ||
401 | } | ||
402 | p += n; | ||
403 | } | ||
404 | else if ( *p == ST_DATA ) { | ||
405 | memcpy ( &n, ++p, sizeof n ); p += sizeof n; | ||
406 | p += n; | ||
407 | } | ||
408 | else | ||
409 | p++; | ||
410 | } | ||
411 | return NULL; | ||
412 | } | ||
413 | |||
414 | /**************** | ||
415 | * Return the length of the given list | ||
416 | */ | ||
417 | int | ||
418 | gcry_sexp_length( const gcry_sexp_t list ) | ||
419 | { | ||
420 | const byte *p; | ||
421 | DATALEN n; | ||
422 | int type; | ||
423 | int length = 0; | ||
424 | int level = 0; | ||
425 | |||
426 | if ( !list ) | ||
427 | return 0; | ||
428 | |||
429 | p = list->d; | ||
430 | while ( (type=*p) != ST_STOP ) { | ||
431 | p++; | ||
432 | if ( type == ST_DATA ) { | ||
433 | memcpy ( &n, p, sizeof n ); | ||
434 | p += sizeof n + n; | ||
435 | if ( level == 1 ) | ||
436 | length++; | ||
437 | } | ||
438 | else if ( type == ST_OPEN ) { | ||
439 | if ( level == 1 ) | ||
440 | length++; | ||
441 | level++; | ||
442 | } | ||
443 | else if ( type == ST_CLOSE ) { | ||
444 | level--; | ||
445 | } | ||
446 | } | ||
447 | return length; | ||
448 | } | ||
449 | |||
450 | |||
451 | |||
452 | /**************** | ||
453 | * Extract the CAR of the given list | ||
454 | */ | ||
455 | gcry_sexp_t | ||
456 | gcry_sexp_nth( const gcry_sexp_t list, int number ) | ||
457 | { | ||
458 | const byte *p; | ||
459 | DATALEN n; | ||
460 | gcry_sexp_t newlist; | ||
461 | byte *d; | ||
462 | int level = 0; | ||
463 | |||
464 | if ( !list || list->d[0] != ST_OPEN ) | ||
465 | return NULL; | ||
466 | p = list->d; | ||
467 | |||
468 | while ( number > 0 ) { | ||
469 | p++; | ||
470 | if ( *p == ST_DATA ) { | ||
471 | memcpy ( &n, ++p, sizeof n ); | ||
472 | p += sizeof n + n; | ||
473 | p--; | ||
474 | if ( !level ) | ||
475 | number--; | ||
476 | } | ||
477 | else if ( *p == ST_OPEN ) { | ||
478 | level++; | ||
479 | } | ||
480 | else if ( *p == ST_CLOSE ) { | ||
481 | level--; | ||
482 | if ( !level ) | ||
483 | number--; | ||
484 | } | ||
485 | else if ( *p == ST_STOP ) { | ||
486 | return NULL; | ||
487 | } | ||
488 | } | ||
489 | p++; | ||
490 | |||
491 | if ( *p == ST_DATA ) { | ||
492 | memcpy ( &n, p, sizeof n ); p += sizeof n; | ||
493 | newlist = gcry_xmalloc ( sizeof *newlist + n + 1 ); | ||
494 | d = newlist->d; | ||
495 | memcpy ( d, p, n ); d += n; | ||
496 | *d++ = ST_STOP; | ||
497 | } | ||
498 | else if ( *p == ST_OPEN ) { | ||
499 | const byte *head = p; | ||
500 | |||
501 | level = 1; | ||
502 | do { | ||
503 | p++; | ||
504 | if ( *p == ST_DATA ) { | ||
505 | memcpy ( &n, ++p, sizeof n ); | ||
506 | p += sizeof n + n; | ||
507 | p--; | ||
508 | } | ||
509 | else if ( *p == ST_OPEN ) { | ||
510 | level++; | ||
511 | } | ||
512 | else if ( *p == ST_CLOSE ) { | ||
513 | level--; | ||
514 | } | ||
515 | else if ( *p == ST_STOP ) { | ||
516 | BUG (); | ||
517 | } | ||
518 | } while ( level ); | ||
519 | n = p + 1 - head; | ||
520 | |||
521 | newlist = gcry_xmalloc ( sizeof *newlist + n ); | ||
522 | d = newlist->d; | ||
523 | memcpy ( d, head, n ); d += n; | ||
524 | *d++ = ST_STOP; | ||
525 | } | ||
526 | else | ||
527 | newlist = NULL; | ||
528 | |||
529 | return normalize (newlist); | ||
530 | } | ||
531 | |||
532 | gcry_sexp_t | ||
533 | gcry_sexp_car( const gcry_sexp_t list ) | ||
534 | { | ||
535 | return gcry_sexp_nth ( list, 0 ); | ||
536 | } | ||
537 | |||
538 | /**************** | ||
539 | * Get data from the car. The returned value is valid as long as the list | ||
540 | * is not modified. | ||
541 | */ | ||
542 | const char * | ||
543 | gcry_sexp_nth_data( const gcry_sexp_t list, int number, size_t *datalen ) | ||
544 | { | ||
545 | const byte *p; | ||
546 | DATALEN n; | ||
547 | int level = 0; | ||
548 | |||
549 | *datalen = 0; | ||
550 | if ( !list ) { | ||
551 | return NULL; | ||
552 | } | ||
553 | p = list->d; | ||
554 | if ( *p == ST_OPEN ) | ||
555 | p++; /* yep, a list */ | ||
556 | else if (number ) | ||
557 | return NULL; /* not a list but an n > 0 element requested */ | ||
558 | |||
559 | /* skip n elements */ | ||
560 | while ( number > 0 ) { | ||
561 | if ( *p == ST_DATA ) { | ||
562 | memcpy ( &n, ++p, sizeof n ); | ||
563 | p += sizeof n + n; | ||
564 | p--; | ||
565 | if ( !level ) | ||
566 | number--; | ||
567 | } | ||
568 | else if ( *p == ST_OPEN ) { | ||
569 | level++; | ||
570 | } | ||
571 | else if ( *p == ST_CLOSE ) { | ||
572 | level--; | ||
573 | if ( !level ) | ||
574 | number--; | ||
575 | } | ||
576 | else if ( *p == ST_STOP ) { | ||
577 | return NULL; | ||
578 | } | ||
579 | p++; | ||
580 | } | ||
581 | |||
582 | |||
583 | if ( *p == ST_DATA ) { | ||
584 | memcpy ( &n, ++p, sizeof n ); | ||
585 | *datalen = n; | ||
586 | return p + sizeof n; | ||
587 | } | ||
588 | |||
589 | return NULL; | ||
590 | } | ||
591 | |||
592 | /**************** | ||
593 | * Get a MPI from the car | ||
594 | */ | ||
595 | gcry_mpi_t | ||
596 | gcry_sexp_nth_mpi( gcry_sexp_t list, int number, int mpifmt ) | ||
597 | { | ||
598 | const byte *p; | ||
599 | DATALEN n; | ||
600 | int level = 0; | ||
601 | |||
602 | if ( !list ) | ||
603 | return NULL; | ||
604 | if ( !mpifmt ) | ||
605 | mpifmt = GCRYMPI_FMT_STD; | ||
606 | |||
607 | p = list->d; | ||
608 | if ( *p == ST_OPEN ) | ||
609 | p++; /* yep, a list */ | ||
610 | else if (number ) | ||
611 | return NULL; /* not a list but an n > 0 element requested */ | ||
612 | |||
613 | /* skip n elements */ | ||
614 | while ( number > 0 ) { | ||
615 | if ( *p == ST_DATA ) { | ||
616 | memcpy ( &n, ++p, sizeof n ); | ||
617 | p += sizeof n + n; | ||
618 | p--; | ||
619 | if ( !level ) | ||
620 | number--; | ||
621 | } | ||
622 | else if ( *p == ST_OPEN ) { | ||
623 | level++; | ||
624 | } | ||
625 | else if ( *p == ST_CLOSE ) { | ||
626 | level--; | ||
627 | if ( !level ) | ||
628 | number--; | ||
629 | } | ||
630 | else if ( *p == ST_STOP ) { | ||
631 | return NULL; | ||
632 | } | ||
633 | p++; | ||
634 | } | ||
635 | |||
636 | if ( *p == ST_DATA ) { | ||
637 | gcry_mpi_t a; | ||
638 | size_t nbytes; | ||
639 | |||
640 | memcpy ( &n, ++p, sizeof n ); | ||
641 | p += sizeof n; | ||
642 | nbytes = n; | ||
643 | if( !gcry_mpi_scan( &a, mpifmt, p, n, &nbytes ) ) | ||
644 | return a; | ||
645 | } | ||
646 | |||
647 | return NULL; | ||
648 | } | ||
649 | |||
650 | |||
651 | /**************** | ||
652 | * Get the CDR | ||
653 | */ | ||
654 | gcry_sexp_t | ||
655 | gcry_sexp_cdr( const gcry_sexp_t list ) | ||
656 | { | ||
657 | const byte *p; | ||
658 | const byte *head; | ||
659 | DATALEN n; | ||
660 | gcry_sexp_t newlist; | ||
661 | byte *d; | ||
662 | int level = 0; | ||
663 | int skip = 1; | ||
664 | |||
665 | if ( !list || list->d[0] != ST_OPEN ) | ||
666 | return NULL; | ||
667 | p = list->d; | ||
668 | |||
669 | while ( skip > 0 ) { | ||
670 | p++; | ||
671 | if ( *p == ST_DATA ) { | ||
672 | memcpy ( &n, ++p, sizeof n ); | ||
673 | p += sizeof n + n; | ||
674 | p--; | ||
675 | if ( !level ) | ||
676 | skip--; | ||
677 | } | ||
678 | else if ( *p == ST_OPEN ) { | ||
679 | level++; | ||
680 | } | ||
681 | else if ( *p == ST_CLOSE ) { | ||
682 | level--; | ||
683 | if ( !level ) | ||
684 | skip--; | ||
685 | } | ||
686 | else if ( *p == ST_STOP ) { | ||
687 | return NULL; | ||
688 | } | ||
689 | } | ||
690 | p++; | ||
691 | |||
692 | head = p; | ||
693 | level = 0; | ||
694 | do { | ||
695 | if ( *p == ST_DATA ) { | ||
696 | memcpy ( &n, ++p, sizeof n ); | ||
697 | p += sizeof n + n; | ||
698 | p--; | ||
699 | } | ||
700 | else if ( *p == ST_OPEN ) { | ||
701 | level++; | ||
702 | } | ||
703 | else if ( *p == ST_CLOSE ) { | ||
704 | level--; | ||
705 | } | ||
706 | else if ( *p == ST_STOP ) { | ||
707 | return NULL; | ||
708 | } | ||
709 | p++; | ||
710 | } while ( level ); | ||
711 | n = p - head; | ||
712 | |||
713 | newlist = gcry_xmalloc ( sizeof *newlist + n + 2 ); | ||
714 | d = newlist->d; | ||
715 | *d++ = ST_OPEN; | ||
716 | memcpy ( d, head, n ); d += n; | ||
717 | *d++ = ST_CLOSE; | ||
718 | *d++ = ST_STOP; | ||
719 | |||
720 | return normalize (newlist); | ||
721 | } | ||
722 | |||
723 | gcry_sexp_t | ||
724 | gcry_sexp_cadr ( const gcry_sexp_t list ) | ||
725 | { | ||
726 | gcry_sexp_t a, b; | ||
727 | |||
728 | a = gcry_sexp_cdr ( list ); | ||
729 | b = gcry_sexp_car ( a ); | ||
730 | gcry_sexp_release ( a ); | ||
731 | return b; | ||
732 | } | ||
733 | |||
734 | |||
735 | |||
736 | static int | ||
737 | hextobyte( const byte *s ) | ||
738 | { | ||
739 | int c=0; | ||
740 | |||
741 | if( *s >= '0' && *s <= '9' ) | ||
742 | c = 16 * (*s - '0'); | ||
743 | else if( *s >= 'A' && *s <= 'F' ) | ||
744 | c = 16 * (10 + *s - 'A'); | ||
745 | else if( *s >= 'a' && *s <= 'f' ) { | ||
746 | c = 16 * (10 + *s - 'a'); | ||
747 | } | ||
748 | s++; | ||
749 | if( *s >= '0' && *s <= '9' ) | ||
750 | c += *s - '0'; | ||
751 | else if( *s >= 'A' && *s <= 'F' ) | ||
752 | c += 10 + *s - 'A'; | ||
753 | else if( *s >= 'a' && *s <= 'f' ) { | ||
754 | c += 10 + *s - 'a'; | ||
755 | } | ||
756 | return c; | ||
757 | } | ||
758 | |||
759 | struct make_space_ctx { | ||
760 | gcry_sexp_t sexp; | ||
761 | size_t allocated; | ||
762 | byte *pos; | ||
763 | }; | ||
764 | |||
765 | static void | ||
766 | make_space ( struct make_space_ctx *c, size_t n ) | ||
767 | { | ||
768 | size_t used = c->pos - c->sexp->d; | ||
769 | |||
770 | if ( used + n + sizeof(DATALEN) + 1 >= c->allocated ) { | ||
771 | gcry_sexp_t newsexp; | ||
772 | byte *newhead; | ||
773 | |||
774 | c->allocated += 2*(n+sizeof(DATALEN)+1); | ||
775 | newsexp = gcry_xrealloc ( c->sexp, sizeof *newsexp + c->allocated - 1 ); | ||
776 | newhead = newsexp->d; | ||
777 | c->pos = newhead + used; | ||
778 | c->sexp = newsexp; | ||
779 | } | ||
780 | } | ||
781 | |||
782 | |||
783 | /* Unquote STRING of LENGTH and store it into BUF. The surrounding | ||
784 | quotes are must already be removed from STRING. We assume that the | ||
785 | quoted string is syntacillay correct. */ | ||
786 | static size_t | ||
787 | unquote_string (const unsigned char *string, size_t length, unsigned char *buf) | ||
788 | { | ||
789 | int esc = 0; | ||
790 | const unsigned char *s = string; | ||
791 | unsigned char *d = buf; | ||
792 | size_t n = length; | ||
793 | |||
794 | for (; n; n--, s++) | ||
795 | { | ||
796 | if (esc) | ||
797 | { | ||
798 | switch (*s) | ||
799 | { | ||
800 | case 'b': *d++ = '\b'; break; | ||
801 | case 't': *d++ = '\t'; break; | ||
802 | case 'v': *d++ = '\v'; break; | ||
803 | case 'n': *d++ = '\n'; break; | ||
804 | case 'f': *d++ = '\f'; break; | ||
805 | case 'r': *d++ = '\r'; break; | ||
806 | case '"': *d++ = '\"'; break; | ||
807 | case '\'': *d++ = '\''; break; | ||
808 | case '\\': *d++ = '\\'; break; | ||
809 | |||
810 | case '\r': /* ignore CR[,LF] */ | ||
811 | if (n>1 && s[1] == '\n') | ||
812 | { | ||
813 | s++; n--; | ||
814 | } | ||
815 | esc = 0; | ||
816 | break; | ||
817 | |||
818 | case '\n': /* ignore LF[,CR] */ | ||
819 | if (n>1 && s[1] == '\r') | ||
820 | { | ||
821 | s++; n--; | ||
822 | } | ||
823 | break; | ||
824 | |||
825 | case 'x': /* hex value */ | ||
826 | if (n>2 && hexdigitp (s+1) && hexdigitp (s+2)) | ||
827 | { | ||
828 | s++; n--; | ||
829 | *d++ = xtoi_2 (s); | ||
830 | s++; n--; | ||
831 | } | ||
832 | break; | ||
833 | |||
834 | default: | ||
835 | if (n>2 && octdigitp (s) && octdigitp (s+1) && octdigitp (s+2)) | ||
836 | { | ||
837 | *d++ = (atoi_1 (s)*64) + (atoi_1 (s+1)*8) + atoi_1 (s+2); | ||
838 | s += 2; | ||
839 | n -= 2; | ||
840 | } | ||
841 | break; | ||
842 | } | ||
843 | esc = 0; | ||
844 | } | ||
845 | else if( *s == '\\' ) | ||
846 | esc = 1; | ||
847 | else | ||
848 | *d++ = *s; | ||
849 | } | ||
850 | |||
851 | return d - buf; | ||
852 | } | ||
853 | |||
854 | /**************** | ||
855 | * Scan the provided buffer and return the S expression in our internal | ||
856 | * format. Returns a newly allocated expression. If erroff is not NULL and | ||
857 | * a parsing error has occured, the offset into buffer will be returned. | ||
858 | * If ARGFLAG is true, the function supports some printf like | ||
859 | * expressions. | ||
860 | * These are: | ||
861 | *%m - MPI | ||
862 | *%s - string (no autoswitch to secure allocation) | ||
863 | *%d - integer stored as string (no autoswitch to secure allocation) | ||
864 | * %b - memory buffer; this takes _two_ arguments: an integer with the | ||
865 | * length of the buffer and a pointer to the buffer. | ||
866 | * all other format elements are currently not defined and return an error. | ||
867 | * this includes the "%%" sequence becauce the percent sign is not an | ||
868 | * allowed character. | ||
869 | * FIXME: We should find a way to store the secure-MPIs not in the string | ||
870 | * but as reference to somewhere - this can help us to save huge amounts | ||
871 | * of secure memory. The problem is, that if only one element is secure, all | ||
872 | * other elements are automagicaly copied to secure memory too, so the most | ||
873 | * common operation gcry_sexp_cdr_mpi() will always return a secure MPI | ||
874 | * regardless whether it is needed or not. | ||
875 | */ | ||
876 | static gcry_error_t | ||
877 | sexp_sscan (gcry_sexp_t *retsexp, size_t *erroff, | ||
878 | const char *buffer, size_t length, int argflag, | ||
879 | va_list arg_ptr, void **arg_list) | ||
880 | { | ||
881 | gcry_err_code_t err = GPG_ERR_NO_ERROR; | ||
882 | static const char tokenchars[] = | ||
883 | "abcdefghijklmnopqrstuvwxyz" | ||
884 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | ||
885 | "0123456789-./_:*+="; | ||
886 | const char *p; | ||
887 | size_t n; | ||
888 | const char *digptr = NULL; | ||
889 | const char *quoted = NULL; | ||
890 | const char *tokenp = NULL; | ||
891 | const char *hexfmt = NULL; | ||
892 | const char *base64 = NULL; | ||
893 | const char *disphint = NULL; | ||
894 | const char *percent = NULL; | ||
895 | int hexcount = 0; | ||
896 | int quoted_esc = 0; | ||
897 | int datalen = 0; | ||
898 | size_t dummy_erroff; | ||
899 | struct make_space_ctx c; | ||
900 | int arg_counter = 0; | ||
901 | int level = 0; | ||
902 | |||
903 | /* FIXME: invent better error codes (?). */ | ||
904 | |||
905 | if (! erroff) | ||
906 | erroff = &dummy_erroff; | ||
907 | |||
908 | /* Depending on wether ARG_LIST is non-zero or not, this macro gives | ||
909 | us the next argument, either from the variable argument list as | ||
910 | specified by ARG_PTR or from the argument array ARG_LIST. */ | ||
911 | #define ARG_NEXT(storage, type) \ | ||
912 | do \ | ||
913 | { \ | ||
914 | if (! arg_list) \ | ||
915 | storage = va_arg (arg_ptr, type); \ | ||
916 | else \ | ||
917 | storage = *((type *) (arg_list[arg_counter++])); \ | ||
918 | } \ | ||
919 | while (0) | ||
920 | |||
921 | #define MAKE_SPACE(n) do { make_space ( &c, (n) ); } while (0) | ||
922 | #define STORE_LEN(p,n) do { \ | ||
923 | DATALEN ashort = (n); \ | ||
924 | memcpy ( (p), &ashort, sizeof(ashort) ); \ | ||
925 | (p) += sizeof (ashort); \ | ||
926 | } while (0) | ||
927 | |||
928 | /* We assume that the internal representation takes less memory | ||
929 | * than the provided one. However, we add space for one extra datalen | ||
930 | * so that the code which does the ST_CLOSE can use MAKE_SPACE */ | ||
931 | c.allocated = length + sizeof(DATALEN); | ||
932 | if (buffer && length && gcry_is_secure (buffer)) | ||
933 | c.sexp = gcry_xmalloc_secure (sizeof *c.sexp + c.allocated - 1); | ||
934 | else | ||
935 | c.sexp = gcry_xmalloc (sizeof *c.sexp + c.allocated - 1); | ||
936 | c.pos = c.sexp->d; | ||
937 | |||
938 | for (p = buffer, n = length; n; p++, n--) | ||
939 | { | ||
940 | if (tokenp && (! hexfmt)) | ||
941 | { | ||
942 | if (strchr (tokenchars, *p)) | ||
943 | continue; | ||
944 | else | ||
945 | { | ||
946 | datalen = p - tokenp; | ||
947 | MAKE_SPACE (datalen); | ||
948 | *c.pos++ = ST_DATA; | ||
949 | STORE_LEN (c.pos, datalen); | ||
950 | memcpy (c.pos, tokenp, datalen); | ||
951 | c.pos += datalen; | ||
952 | tokenp = NULL; | ||
953 | } | ||
954 | } | ||
955 | |||
956 | if (quoted) | ||
957 | { | ||
958 | if (quoted_esc) | ||
959 | { | ||
960 | switch (*p) | ||
961 | { | ||
962 | case 'b': case 't': case 'v': case 'n': case 'f': | ||
963 | case 'r': case '"': case '\'': case '\\': | ||
964 | quoted_esc = 0; | ||
965 | break; | ||
966 | |||
967 | case '0': case '1': case '2': case '3': case '4': | ||
968 | case '5': case '6': case '7': | ||
969 | if (! ((n > 2) | ||
970 | && (p[1] >= '0') && (p[1] <= '7') | ||
971 | && (p[2] >= '0') && (p[2] <= '7'))) | ||
972 | { | ||
973 | *erroff = p - buffer; | ||
974 | /* Invalid octal value. */ | ||
975 | err = GPG_ERR_SEXP_BAD_QUOTATION; | ||
976 | //return gcry_error (GPG_ERR_SEXP_BAD_QUOTATION); | ||
977 | } | ||
978 | p += 2; | ||
979 | n -= 2; | ||
980 | quoted_esc = 0; | ||
981 | break; | ||
982 | |||
983 | case 'x': | ||
984 | if (! ((n > 2) && isxdigit(p[1]) && isxdigit(p[2]))) | ||
985 | { | ||
986 | *erroff = p - buffer; | ||
987 | /* Invalid hex value. */ | ||
988 | err = GPG_ERR_SEXP_BAD_QUOTATION; | ||
989 | //return gcry_error (GPG_ERR_SEXP_BAD_QUOTATION); | ||
990 | } | ||
991 | p += 2; | ||
992 | n -= 2; | ||
993 | quoted_esc = 0; | ||
994 | break; | ||
995 | |||
996 | case '\r': | ||
997 | /* ignore CR[,LF] */ | ||
998 | if (n && (p[1] == '\n')) | ||
999 | { | ||
1000 | p++; | ||
1001 | n--; | ||
1002 | } | ||
1003 | quoted_esc = 0; | ||
1004 | break; | ||
1005 | |||
1006 | case '\n': | ||
1007 | /* ignore LF[,CR] */ | ||
1008 | if (n && (p[1] == '\r')) | ||
1009 | { | ||
1010 | p++; | ||
1011 | n--; | ||
1012 | } | ||
1013 | quoted_esc = 0; | ||
1014 | break; | ||
1015 | |||
1016 | default: | ||
1017 | *erroff = p - buffer; | ||
1018 | /* Invalid quoted string escape. */ | ||
1019 | err = GPG_ERR_SEXP_BAD_QUOTATION; | ||
1020 | } | ||
1021 | } | ||
1022 | else if (*p == '\\') | ||
1023 | quoted_esc = 1; | ||
1024 | else if (*p == '\"') | ||
1025 | { | ||
1026 | /* Keep it easy - we know that the unquoted string will | ||
1027 | never be larger. */ | ||
1028 | char *save; | ||
1029 | size_t len; | ||
1030 | |||
1031 | quoted++; /* Skip leading quote. */ | ||
1032 | MAKE_SPACE (p - quoted); | ||
1033 | *c.pos++ = ST_DATA; | ||
1034 | save = c.pos; | ||
1035 | STORE_LEN (c.pos, 0); /* Will be fixed up later. */ | ||
1036 | len = unquote_string (quoted, p - quoted, c.pos); | ||
1037 | c.pos += len; | ||
1038 | STORE_LEN (save, len); | ||
1039 | quoted = NULL; | ||
1040 | } | ||
1041 | } | ||
1042 | else if (hexfmt) | ||
1043 | { | ||
1044 | if (isxdigit (*p)) | ||
1045 | hexcount++; | ||
1046 | else if (*p == '#') | ||
1047 | { | ||
1048 | if ((hexcount & 1)) | ||
1049 | { | ||
1050 | *erroff = p - buffer; | ||
1051 | err = GPG_ERR_SEXP_ODD_HEX_NUMBERS; | ||
1052 | } | ||
1053 | |||
1054 | datalen = hexcount / 2; | ||
1055 | MAKE_SPACE (datalen); | ||
1056 | *c.pos++ = ST_DATA; | ||
1057 | STORE_LEN (c.pos, datalen); | ||
1058 | for (hexfmt++; hexfmt < p; hexfmt++) | ||
1059 | { | ||
1060 | if (isspace (*hexfmt)) | ||
1061 | continue; | ||
1062 | *c.pos++ = hextobyte (hexfmt); | ||
1063 | hexfmt++; | ||
1064 | } | ||
1065 | hexfmt = NULL; | ||
1066 | } | ||
1067 | else if (! isspace (*p)) | ||
1068 | { | ||
1069 | *erroff = p - buffer; | ||
1070 | err = GPG_ERR_SEXP_BAD_HEX_CHAR; | ||
1071 | } | ||
1072 | } | ||
1073 | else if (base64) | ||
1074 | { | ||
1075 | if (*p == '|') | ||
1076 | base64 = NULL; | ||
1077 | } | ||
1078 | else if (digptr) | ||
1079 | { | ||
1080 | if (isdigit (*p)) | ||
1081 | ; | ||
1082 | else if (*p == ':') | ||
1083 | { | ||
1084 | datalen = atoi (digptr); /* FIXME: check for overflow. */ | ||
1085 | digptr = NULL; | ||
1086 | if (datalen > n - 1) | ||
1087 | { | ||
1088 | *erroff = p - buffer; | ||
1089 | /* Buffer too short. */ | ||
1090 | err = GPG_ERR_SEXP_STRING_TOO_LONG; | ||
1091 | } | ||
1092 | /* Make a new list entry. */ | ||
1093 | MAKE_SPACE (datalen); | ||
1094 | *c.pos++ = ST_DATA; | ||
1095 | STORE_LEN (c.pos, datalen); | ||
1096 | memcpy (c.pos, p + 1, datalen); | ||
1097 | c.pos += datalen; | ||
1098 | n -= datalen; | ||
1099 | p += datalen; | ||
1100 | } | ||
1101 | else if (*p == '\"') | ||
1102 | { | ||
1103 | digptr = NULL; /* We ignore the optional length. */ | ||
1104 | quoted = p; | ||
1105 | quoted_esc = 0; | ||
1106 | } | ||
1107 | else if (*p == '#') | ||
1108 | { | ||
1109 | digptr = NULL; /* We ignore the optional length. */ | ||
1110 | hexfmt = p; | ||
1111 | hexcount = 0; | ||
1112 | } | ||
1113 | else if (*p == '|') | ||
1114 | { | ||
1115 | digptr = NULL; /* We ignore the optional length. */ | ||
1116 | base64 = p; | ||
1117 | } | ||
1118 | else | ||
1119 | { | ||
1120 | *erroff = p - buffer; | ||
1121 | err = GPG_ERR_SEXP_INV_LEN_SPEC; | ||
1122 | } | ||
1123 | } | ||
1124 | else if (percent) | ||
1125 | { | ||
1126 | if (*p == 'm') | ||
1127 | { | ||
1128 | /* Insert an MPI. */ | ||
1129 | gcry_mpi_t m; | ||
1130 | size_t nm = 0; | ||
1131 | |||
1132 | ARG_NEXT (m, gcry_mpi_t); | ||
1133 | |||
1134 | if (gcry_mpi_print (GCRYMPI_FMT_STD, NULL, 0, &nm, m)) | ||
1135 | BUG (); | ||
1136 | |||
1137 | MAKE_SPACE (nm); | ||
1138 | if ((! gcry_is_secure (c.sexp->d)) | ||
1139 | && gcry_mpi_get_flag ( m, GCRYMPI_FLAG_SECURE)) | ||
1140 | { | ||
1141 | /* We have to switch to secure allocation. */ | ||
1142 | gcry_sexp_t newsexp; | ||
1143 | byte *newhead; | ||
1144 | |||
1145 | newsexp = gcry_xmalloc_secure (sizeof *newsexp | ||
1146 | + c.allocated - 1); | ||
1147 | newhead = newsexp->d; | ||
1148 | memcpy (newhead, c.sexp->d, (c.pos - c.sexp->d)); | ||
1149 | c.pos = newhead + (c.pos - c.sexp->d); | ||
1150 | gcry_free (c.sexp); | ||
1151 | c.sexp = newsexp; | ||
1152 | } | ||
1153 | |||
1154 | *c.pos++ = ST_DATA; | ||
1155 | STORE_LEN (c.pos, nm); | ||
1156 | if (gcry_mpi_print (GCRYMPI_FMT_STD, c.pos, nm, &nm, m)) | ||
1157 | BUG (); | ||
1158 | c.pos += nm; | ||
1159 | } | ||
1160 | else if (*p == 's') | ||
1161 | { | ||
1162 | /* Insert an string. */ | ||
1163 | const char *astr; | ||
1164 | size_t alen; | ||
1165 | |||
1166 | ARG_NEXT (astr, const char *); | ||
1167 | alen = strlen (astr); | ||
1168 | |||
1169 | MAKE_SPACE (alen); | ||
1170 | *c.pos++ = ST_DATA; | ||
1171 | STORE_LEN (c.pos, alen); | ||
1172 | memcpy (c.pos, astr, alen); | ||
1173 | c.pos += alen; | ||
1174 | } | ||
1175 | else if (*p == 'b') | ||
1176 | { | ||
1177 | /* Insert a memory buffer. */ | ||
1178 | const char *astr; | ||
1179 | int alen; | ||
1180 | |||
1181 | ARG_NEXT (alen, int); | ||
1182 | ARG_NEXT (astr, const char *); | ||
1183 | |||
1184 | MAKE_SPACE (alen); | ||
1185 | if (alen | ||
1186 | && !gcry_is_secure (c.sexp->d) | ||
1187 | && gcry_is_secure (astr)) | ||
1188 | { | ||
1189 | /* We have to switch to secure allocation. */ | ||
1190 | gcry_sexp_t newsexp; | ||
1191 | byte *newhead; | ||
1192 | |||
1193 | newsexp = gcry_xmalloc_secure (sizeof *newsexp | ||
1194 | + c.allocated - 1); | ||
1195 | newhead = newsexp->d; | ||
1196 | memcpy (newhead, c.sexp->d, (c.pos - c.sexp->d)); | ||
1197 | c.pos = newhead + (c.pos - c.sexp->d); | ||
1198 | gcry_free (c.sexp); | ||
1199 | c.sexp = newsexp; | ||
1200 | } | ||
1201 | |||
1202 | *c.pos++ = ST_DATA; | ||
1203 | STORE_LEN (c.pos, alen); | ||
1204 | memcpy (c.pos, astr, alen); | ||
1205 | c.pos += alen; | ||
1206 | } | ||
1207 | else if (*p == 'd') | ||
1208 | { | ||
1209 | /* Insert an integer as string. */ | ||
1210 | int aint; | ||
1211 | size_t alen; | ||
1212 | char buf[20]; | ||
1213 | |||
1214 | ARG_NEXT (aint, int); | ||
1215 | sprintf (buf, "%d", aint); | ||
1216 | alen = strlen (buf); | ||
1217 | MAKE_SPACE (alen); | ||
1218 | *c.pos++ = ST_DATA; | ||
1219 | STORE_LEN (c.pos, alen); | ||
1220 | memcpy (c.pos, buf, alen); | ||
1221 | c.pos += alen; | ||
1222 | } | ||
1223 | else | ||
1224 | { | ||
1225 | *erroff = p - buffer; | ||
1226 | /* Invalid format specifier. */ | ||
1227 | err = GPG_ERR_SEXP_INV_LEN_SPEC; | ||
1228 | } | ||
1229 | percent = NULL; | ||
1230 | } | ||
1231 | else if (*p == '(') | ||
1232 | { | ||
1233 | if (disphint) | ||
1234 | { | ||
1235 | *erroff = p - buffer; | ||
1236 | /* Open display hint. */ | ||
1237 | err = GPG_ERR_SEXP_UNMATCHED_DH; | ||
1238 | } | ||
1239 | MAKE_SPACE (0); | ||
1240 | *c.pos++ = ST_OPEN; | ||
1241 | level++; | ||
1242 | } | ||
1243 | else if (*p == ')') | ||
1244 | { | ||
1245 | /* Walk up. */ | ||
1246 | if (disphint) | ||
1247 | { | ||
1248 | *erroff = p - buffer; | ||
1249 | /* Open display hint. */ | ||
1250 | err = GPG_ERR_SEXP_UNMATCHED_DH; | ||
1251 | } | ||
1252 | MAKE_SPACE (0); | ||
1253 | *c.pos++ = ST_CLOSE; | ||
1254 | level--; | ||
1255 | } | ||
1256 | else if (*p == '\"') | ||
1257 | { | ||
1258 | quoted = p; | ||
1259 | quoted_esc = 0; | ||
1260 | } | ||
1261 | else if (*p == '#') | ||
1262 | { | ||
1263 | hexfmt = p; | ||
1264 | hexcount = 0; | ||
1265 | } | ||
1266 | else if (*p == '|') | ||
1267 | base64 = p; | ||
1268 | else if (*p == '[') | ||
1269 | { | ||
1270 | if (disphint) | ||
1271 | { | ||
1272 | *erroff = p - buffer; | ||
1273 | /* Open display hint. */ | ||
1274 | err = GPG_ERR_SEXP_NESTED_DH; | ||
1275 | } | ||
1276 | disphint = p; | ||
1277 | } | ||
1278 | else if (*p == ']') | ||
1279 | { | ||
1280 | if (! disphint) | ||
1281 | { | ||
1282 | *erroff = p - buffer; | ||
1283 | /* Open display hint. */ | ||
1284 | err = GPG_ERR_SEXP_UNMATCHED_DH; | ||
1285 | } | ||
1286 | disphint = NULL; | ||
1287 | } | ||
1288 | else if (isdigit (*p)) | ||
1289 | { | ||
1290 | if (*p == '0') | ||
1291 | { | ||
1292 | /* A length may not begin with zero. */ | ||
1293 | *erroff = p - buffer; | ||
1294 | err = GPG_ERR_SEXP_ZERO_PREFIX; | ||
1295 | } | ||
1296 | digptr = p; | ||
1297 | } | ||
1298 | else if (strchr (tokenchars, *p)) | ||
1299 | tokenp = p; | ||
1300 | else if (isspace (*p)) | ||
1301 | ; | ||
1302 | else if (*p == '{') | ||
1303 | { | ||
1304 | /* fixme: handle rescanning: we can do this by saving our | ||
1305 | current state and start over at p+1 -- Hmmm. At this | ||
1306 | point here we are in a well defined state, so we don't | ||
1307 | need to save it. Great. */ | ||
1308 | *erroff = p - buffer; | ||
1309 | err = GPG_ERR_SEXP_UNEXPECTED_PUNC; | ||
1310 | } | ||
1311 | else if (strchr ("&\\", *p)) | ||
1312 | { | ||
1313 | /* Reserved punctuation. */ | ||
1314 | *erroff = p - buffer; | ||
1315 | err = GPG_ERR_SEXP_UNEXPECTED_PUNC; | ||
1316 | } | ||
1317 | else if (argflag && (*p == '%')) | ||
1318 | percent = p; | ||
1319 | else | ||
1320 | { | ||
1321 | /* Bad or unavailable. */ | ||
1322 | *erroff = p - buffer; | ||
1323 | err = GPG_ERR_SEXP_BAD_CHARACTER; | ||
1324 | } | ||
1325 | } | ||
1326 | MAKE_SPACE (0); | ||
1327 | *c.pos++ = ST_STOP; | ||
1328 | |||
1329 | if (level) | ||
1330 | err = GPG_ERR_SEXP_UNMATCHED_PAREN; | ||
1331 | |||
1332 | if (err) | ||
1333 | { | ||
1334 | /* Error -> deallocate. */ | ||
1335 | if (c.sexp) | ||
1336 | { | ||
1337 | /* Extra paranoid wipe on error. */ | ||
1338 | if (gcry_is_secure (c.sexp)) | ||
1339 | wipememory (c.sexp, sizeof (struct gcry_sexp) + c.allocated - 1); | ||
1340 | gcry_free (c.sexp); | ||
1341 | } | ||
1342 | /* This might be expected by existing code... */ | ||
1343 | *retsexp = NULL; | ||
1344 | } | ||
1345 | else | ||
1346 | *retsexp = normalize (c.sexp); | ||
1347 | |||
1348 | return gcry_error (err); | ||
1349 | #undef MAKE_SPACE | ||
1350 | #undef STORE_LEN | ||
1351 | } | ||
1352 | |||
1353 | gcry_error_t | ||
1354 | gcry_sexp_build (gcry_sexp_t *retsexp, size_t *erroff, const char *format, ...) | ||
1355 | { | ||
1356 | gcry_error_t rc; | ||
1357 | va_list arg_ptr; | ||
1358 | |||
1359 | va_start (arg_ptr, format); | ||
1360 | rc = sexp_sscan (retsexp, erroff, format, strlen(format), 1, | ||
1361 | arg_ptr, NULL); | ||
1362 | va_end (arg_ptr); | ||
1363 | |||
1364 | return rc; | ||
1365 | } | ||
1366 | |||
1367 | /* Like gcry_sexp_build, but uses an array instead of variable | ||
1368 | function arguments. */ | ||
1369 | gcry_error_t | ||
1370 | gcry_sexp_build_array (gcry_sexp_t *retsexp, size_t *erroff, | ||
1371 | const char *format, void **arg_list) | ||
1372 | { | ||
1373 | /* We don't need the va_list because it is controlled by the | ||
1374 | following flag, however we have to pass it but can't initialize | ||
1375 | it as there is no portable way to do so. volatile is needed to | ||
1376 | suppress the compiler warning */ | ||
1377 | volatile va_list dummy_arg_ptr; | ||
1378 | |||
1379 | gcry_error_t rc; | ||
1380 | |||
1381 | rc = sexp_sscan (retsexp, erroff, format, strlen(format), 1, | ||
1382 | dummy_arg_ptr, arg_list); | ||
1383 | |||
1384 | return rc; | ||
1385 | } | ||
1386 | |||
1387 | gcry_error_t | ||
1388 | gcry_sexp_sscan (gcry_sexp_t *retsexp, size_t *erroff, | ||
1389 | const char *buffer, size_t length) | ||
1390 | { | ||
1391 | /* We don't need the va_list because it is controlled by the | ||
1392 | following flag, however we have to pass it but can't initialize | ||
1393 | it as there is no portable way to do so. volatile is needed to | ||
1394 | suppress the compiler warning */ | ||
1395 | volatile va_list dummy_arg_ptr; | ||
1396 | |||
1397 | return sexp_sscan (retsexp, erroff, buffer, length, 0, | ||
1398 | dummy_arg_ptr, NULL); | ||
1399 | } | ||
1400 | |||
1401 | |||
1402 | /* Figure out a suitable encoding for BUFFER of LENGTH. | ||
1403 | Returns: 0 = Binary | ||
1404 | 1 = String possible | ||
1405 | 2 = Token possible | ||
1406 | */ | ||
1407 | static int | ||
1408 | suitable_encoding (const unsigned char *buffer, size_t length) | ||
1409 | { | ||
1410 | const unsigned char *s; | ||
1411 | int maybe_token = 1; | ||
1412 | |||
1413 | if (!length) | ||
1414 | return 1; | ||
1415 | |||
1416 | for (s=buffer; length; s++, length--) | ||
1417 | { | ||
1418 | if ( (*s < 0x20 || (*s >= 0x7f && *s <= 0xa0)) | ||
1419 | && !strchr ("\b\t\v\n\f\r\"\'\\", *s)) | ||
1420 | return 0; /*binary*/ | ||
1421 | if ( maybe_token | ||
1422 | && !alphap (s) && !digitp (s) && !strchr (TOKEN_SPECIALS, *s)) | ||
1423 | maybe_token = 0; | ||
1424 | } | ||
1425 | s = buffer; | ||
1426 | if ( maybe_token && !digitp (s) ) | ||
1427 | return 2; | ||
1428 | return 1; | ||
1429 | } | ||
1430 | |||
1431 | |||
1432 | static int | ||
1433 | convert_to_hex (const unsigned char *src, size_t len, unsigned char *dest) | ||
1434 | { | ||
1435 | int i; | ||
1436 | |||
1437 | if (dest) | ||
1438 | { | ||
1439 | *dest++ = '#'; | ||
1440 | for (i=0; i < len; i++, dest += 2 ) | ||
1441 | sprintf (dest, "%02X", src[i]); | ||
1442 | *dest++ = '#'; | ||
1443 | } | ||
1444 | return len*2+2; | ||
1445 | } | ||
1446 | |||
1447 | static int | ||
1448 | convert_to_string (const unsigned char *s, size_t len, unsigned char *dest) | ||
1449 | { | ||
1450 | if (dest) | ||
1451 | { | ||
1452 | unsigned char *p = dest; | ||
1453 | *p++ = '\"'; | ||
1454 | for (; len; len--, s++ ) | ||
1455 | { | ||
1456 | switch (*s) | ||
1457 | { | ||
1458 | case '\b': *p++ = '\\'; *p++ = 'b'; break; | ||
1459 | case '\t': *p++ = '\\'; *p++ = 't'; break; | ||
1460 | case '\v': *p++ = '\\'; *p++ = 'v'; break; | ||
1461 | case '\n': *p++ = '\\'; *p++ = 'n'; break; | ||
1462 | case '\f': *p++ = '\\'; *p++ = 'f'; break; | ||
1463 | case '\r': *p++ = '\\'; *p++ = 'r'; break; | ||
1464 | case '\"': *p++ = '\\'; *p++ = '\"'; break; | ||
1465 | case '\'': *p++ = '\\'; *p++ = '\''; break; | ||
1466 | case '\\': *p++ = '\\'; *p++ = '\\'; break; | ||
1467 | default: | ||
1468 | if ( (*s < 0x20 || (*s >= 0x7f && *s <= 0xa0))) | ||
1469 | { | ||
1470 | sprintf (p, "\\x%02x", *s); | ||
1471 | p += 4; | ||
1472 | } | ||
1473 | else | ||
1474 | *p++ = *s; | ||
1475 | } | ||
1476 | } | ||
1477 | *p++ = '\"'; | ||
1478 | return p - dest; | ||
1479 | } | ||
1480 | else | ||
1481 | { | ||
1482 | int count = 2; | ||
1483 | for (; len; len--, s++ ) | ||
1484 | { | ||
1485 | switch (*s) | ||
1486 | { | ||
1487 | case '\b': | ||
1488 | case '\t': | ||
1489 | case '\v': | ||
1490 | case '\n': | ||
1491 | case '\f': | ||
1492 | case '\r': | ||
1493 | case '\"': | ||
1494 | case '\'': | ||
1495 | case '\\': count += 2; break; | ||
1496 | default: | ||
1497 | if ( (*s < 0x20 || (*s >= 0x7f && *s <= 0xa0))) | ||
1498 | count += 4; | ||
1499 | else | ||
1500 | count++; | ||
1501 | } | ||
1502 | } | ||
1503 | return count; | ||
1504 | } | ||
1505 | } | ||
1506 | |||
1507 | |||
1508 | |||
1509 | static int | ||
1510 | convert_to_token (const unsigned char *src, size_t len, unsigned char *dest) | ||
1511 | { | ||
1512 | if (dest) | ||
1513 | memcpy (dest, src, len); | ||
1514 | return len; | ||
1515 | } | ||
1516 | |||
1517 | |||
1518 | /**************** | ||
1519 | * Print SEXP to buffer using the MODE. Returns the length of the | ||
1520 | * SEXP in buffer or 0 if the buffer is too short (We have at least an | ||
1521 | * empty list consisting of 2 bytes). If a buffer of NULL is provided, | ||
1522 | * the required length is returned. | ||
1523 | */ | ||
1524 | size_t | ||
1525 | gcry_sexp_sprint( const gcry_sexp_t list, int mode, | ||
1526 | char *buffer, size_t maxlength ) | ||
1527 | { | ||
1528 | static byte empty[3] = { ST_OPEN, ST_CLOSE, ST_STOP }; | ||
1529 | const byte *s; | ||
1530 | char *d; | ||
1531 | DATALEN n; | ||
1532 | char numbuf[20]; | ||
1533 | size_t len = 0; | ||
1534 | int i, indent = 0; | ||
1535 | |||
1536 | s = list? list->d : empty; | ||
1537 | d = buffer; | ||
1538 | while ( *s != ST_STOP ) | ||
1539 | { | ||
1540 | switch ( *s ) | ||
1541 | { | ||
1542 | case ST_OPEN: | ||
1543 | s++; | ||
1544 | if ( mode != GCRYSEXP_FMT_CANON ) | ||
1545 | { | ||
1546 | if (indent) | ||
1547 | len++; | ||
1548 | len += indent; | ||
1549 | } | ||
1550 | len++; | ||
1551 | if ( buffer ) | ||
1552 | { | ||
1553 | if ( len >= maxlength ) | ||
1554 | return 0; | ||
1555 | if ( mode != GCRYSEXP_FMT_CANON ) | ||
1556 | { | ||
1557 | if (indent) | ||
1558 | *d++ = '\n'; | ||
1559 | for (i=0; i < indent; i++) | ||
1560 | *d++ = ' '; | ||
1561 | } | ||
1562 | *d++ = '('; | ||
1563 | } | ||
1564 | indent++; | ||
1565 | break; | ||
1566 | case ST_CLOSE: | ||
1567 | s++; | ||
1568 | len++; | ||
1569 | if ( buffer ) | ||
1570 | { | ||
1571 | if ( len >= maxlength ) | ||
1572 | return 0; | ||
1573 | *d++ = ')'; | ||
1574 | } | ||
1575 | indent--; | ||
1576 | if (*s != ST_OPEN && *s != ST_STOP && mode != GCRYSEXP_FMT_CANON) | ||
1577 | { | ||
1578 | len++; | ||
1579 | len += indent; | ||
1580 | if (buffer) | ||
1581 | { | ||
1582 | if (len >= maxlength) | ||
1583 | return 0; | ||
1584 | *d++ = '\n'; | ||
1585 | for (i=0; i < indent; i++) | ||
1586 | *d++ = ' '; | ||
1587 | } | ||
1588 | } | ||
1589 | break; | ||
1590 | case ST_DATA: | ||
1591 | s++; | ||
1592 | memcpy ( &n, s, sizeof n ); s += sizeof n; | ||
1593 | if (mode == GCRYSEXP_FMT_ADVANCED) | ||
1594 | { | ||
1595 | int type; | ||
1596 | size_t nn; | ||
1597 | |||
1598 | switch ( (type=suitable_encoding (s, n))) | ||
1599 | { | ||
1600 | case 1: nn = convert_to_string (s, n, NULL); break; | ||
1601 | case 2: nn = convert_to_token (s, n, NULL); break; | ||
1602 | default: nn = convert_to_hex (s, n, NULL); break; | ||
1603 | } | ||
1604 | len += nn; | ||
1605 | if (buffer) | ||
1606 | { | ||
1607 | if (len >= maxlength) | ||
1608 | return 0; | ||
1609 | switch (type) | ||
1610 | { | ||
1611 | case 1: convert_to_string (s, n, d); break; | ||
1612 | case 2: convert_to_token (s, n, d); break; | ||
1613 | default: convert_to_hex (s, n, d); break; | ||
1614 | } | ||
1615 | d += nn; | ||
1616 | } | ||
1617 | if (s[n] != ST_CLOSE) | ||
1618 | { | ||
1619 | len++; | ||
1620 | if (buffer) | ||
1621 | { | ||
1622 | if (len >= maxlength) | ||
1623 | return 0; | ||
1624 | *d++ = ' '; | ||
1625 | } | ||
1626 | } | ||
1627 | } | ||
1628 | else | ||
1629 | { | ||
1630 | sprintf (numbuf, "%u:", (unsigned int)n ); | ||
1631 | len += strlen (numbuf) + n; | ||
1632 | if ( buffer ) | ||
1633 | { | ||
1634 | if ( len >= maxlength ) | ||
1635 | return 0; | ||
1636 | d = stpcpy ( d, numbuf ); | ||
1637 | memcpy ( d, s, n ); d += n; | ||
1638 | } | ||
1639 | } | ||
1640 | s += n; | ||
1641 | break; | ||
1642 | default: | ||
1643 | BUG (); | ||
1644 | } | ||
1645 | } | ||
1646 | if ( mode != GCRYSEXP_FMT_CANON ) | ||
1647 | { | ||
1648 | len++; | ||
1649 | if (buffer) | ||
1650 | { | ||
1651 | if ( len >= maxlength ) | ||
1652 | return 0; | ||
1653 | *d++ = '\n'; | ||
1654 | } | ||
1655 | } | ||
1656 | if (buffer) | ||
1657 | { | ||
1658 | if ( len >= maxlength ) | ||
1659 | return 0; | ||
1660 | *d++ = 0; /* for convenience we make a C string */ | ||
1661 | } | ||
1662 | else | ||
1663 | len++; /* we need one byte more for this */ | ||
1664 | |||
1665 | return len; | ||
1666 | } | ||
1667 | |||
1668 | |||
1669 | /* Scan a cannocial encoded buffer with implicit length values and | ||
1670 | return the actual length this S-expression uses. For a valid S-Exp | ||
1671 | it should never return 0. If LENGTH is not zero, the maximum | ||
1672 | length to scan is given - this can be used for syntax checks of | ||
1673 | data passed from outside. errorcode and erroff may both be passed as | ||
1674 | NULL. */ | ||
1675 | size_t | ||
1676 | gcry_sexp_canon_len (const unsigned char *buffer, size_t length, | ||
1677 | size_t *erroff, gcry_error_t *errcode) | ||
1678 | { | ||
1679 | const unsigned char *p; | ||
1680 | const char *disphint=NULL; | ||
1681 | unsigned int datalen = 0; | ||
1682 | size_t dummy_erroff; | ||
1683 | gcry_error_t dummy_errcode; | ||
1684 | size_t count = 0; | ||
1685 | int level = 0; | ||
1686 | |||
1687 | if (!erroff) | ||
1688 | erroff = &dummy_erroff; | ||
1689 | if (!errcode) | ||
1690 | errcode = &dummy_errcode; | ||
1691 | |||
1692 | *errcode = gcry_error (GPG_ERR_NO_ERROR); | ||
1693 | *erroff = 0; | ||
1694 | if (!buffer) | ||
1695 | return 0; | ||
1696 | if (*buffer != '(') | ||
1697 | { | ||
1698 | *errcode = gcry_error (GPG_ERR_SEXP_NOT_CANONICAL); | ||
1699 | return 0; | ||
1700 | } | ||
1701 | |||
1702 | for (p=buffer; ; p++, count++ ) | ||
1703 | { | ||
1704 | if (length && count >= length) | ||
1705 | { | ||
1706 | *erroff = count; | ||
1707 | *errcode = gcry_error (GPG_ERR_SEXP_STRING_TOO_LONG); | ||
1708 | return 0; | ||
1709 | } | ||
1710 | |||
1711 | if (datalen) | ||
1712 | { | ||
1713 | if (*p == ':') | ||
1714 | { | ||
1715 | if (length && (count+datalen) >= length) | ||
1716 | { | ||
1717 | *erroff = count; | ||
1718 | *errcode = gcry_error (GPG_ERR_SEXP_STRING_TOO_LONG); | ||
1719 | return 0; | ||
1720 | } | ||
1721 | count += datalen; | ||
1722 | p += datalen; | ||
1723 | datalen = 0; | ||
1724 | } | ||
1725 | else if (digitp(p)) | ||
1726 | datalen = datalen*10 + atoi_1(p); | ||
1727 | else | ||
1728 | { | ||
1729 | *erroff = count; | ||
1730 | *errcode = gcry_error (GPG_ERR_SEXP_INV_LEN_SPEC); | ||
1731 | return 0; | ||
1732 | } | ||
1733 | } | ||
1734 | else if (*p == '(') | ||
1735 | { | ||
1736 | if (disphint) | ||
1737 | { | ||
1738 | *erroff = count; | ||
1739 | *errcode = gcry_error (GPG_ERR_SEXP_UNMATCHED_DH); | ||
1740 | return 0; | ||
1741 | } | ||
1742 | level++; | ||
1743 | } | ||
1744 | else if (*p == ')') | ||
1745 | { /* walk up */ | ||
1746 | if (!level) | ||
1747 | { | ||
1748 | *erroff = count; | ||
1749 | *errcode = gcry_error (GPG_ERR_SEXP_UNMATCHED_PAREN); | ||
1750 | return 0; | ||
1751 | } | ||
1752 | if (disphint) | ||
1753 | { | ||
1754 | *erroff = count; | ||
1755 | *errcode = gcry_error (GPG_ERR_SEXP_UNMATCHED_DH); | ||
1756 | return 0; | ||
1757 | } | ||
1758 | if (!--level) | ||
1759 | return ++count; /* ready */ | ||
1760 | } | ||
1761 | else if (*p == '[') | ||
1762 | { | ||
1763 | if (disphint) | ||
1764 | { | ||
1765 | *erroff = count; | ||
1766 | *errcode = gcry_error (GPG_ERR_SEXP_NESTED_DH); | ||
1767 | return 0; | ||
1768 | } | ||
1769 | disphint = p; | ||
1770 | } | ||
1771 | else if (*p == ']') | ||
1772 | { | ||
1773 | if( !disphint ) | ||
1774 | { | ||
1775 | *erroff = count; | ||
1776 | *errcode = gcry_error (GPG_ERR_SEXP_UNMATCHED_DH); | ||
1777 | return 0; | ||
1778 | } | ||
1779 | disphint = NULL; | ||
1780 | } | ||
1781 | else if (digitp (p) ) | ||
1782 | { | ||
1783 | if (*p == '0') | ||
1784 | { | ||
1785 | *erroff = count; | ||
1786 | *errcode = gcry_error (GPG_ERR_SEXP_ZERO_PREFIX); | ||
1787 | return 0; | ||
1788 | } | ||
1789 | datalen = atoi_1 (p); | ||
1790 | } | ||
1791 | else if (*p == '&' || *p == '\\') | ||
1792 | { | ||
1793 | *erroff = count; | ||
1794 | *errcode = gcry_error (GPG_ERR_SEXP_UNEXPECTED_PUNC); | ||
1795 | return 0; | ||
1796 | } | ||
1797 | else | ||
1798 | { | ||
1799 | *erroff = count; | ||
1800 | *errcode = gcry_error (GPG_ERR_SEXP_BAD_CHARACTER); | ||
1801 | return 0; | ||
1802 | } | ||
1803 | } | ||
1804 | } | ||
diff --git a/pwmanager/libcrypt/crypt/stdmem.c b/pwmanager/libcrypt/crypt/stdmem.c new file mode 100644 index 0000000..659bc4e --- a/dev/null +++ b/pwmanager/libcrypt/crypt/stdmem.c | |||
@@ -0,0 +1,195 @@ | |||
1 | /* stdmem.c -private memory allocator | ||
2 | *Copyright (C) 1998, 2000, 2002 Free Software Foundation, Inc. | ||
3 | * | ||
4 | * This file is part of Libgcrypt. | ||
5 | * | ||
6 | * Libgcrypt is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU Lesser general Public License as | ||
8 | * published by the Free Software Foundation; either version 2.1 of | ||
9 | * the License, or (at your option) any later version. | ||
10 | * | ||
11 | * Libgcrypt is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * GNU Lesser General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU Lesser General Public | ||
17 | * License along with this program; if not, write to the Free Software | ||
18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | ||
19 | */ | ||
20 | |||
21 | #include <config.h> | ||
22 | #include <stdio.h> | ||
23 | #include <stdlib.h> | ||
24 | #include <string.h> | ||
25 | #include <stdarg.h> | ||
26 | |||
27 | #include "g10lib.h" | ||
28 | #include "stdmem.h" | ||
29 | #include "secmem.h" | ||
30 | |||
31 | |||
32 | #define MAGIC_NOR_BYTE 0x55 | ||
33 | #define MAGIC_SEC_BYTE 0xcc | ||
34 | #define MAGIC_END_BYTE 0xaa | ||
35 | |||
36 | #if SIZEOF_UNSIGNED_LONG == 8 | ||
37 | #define EXTRA_ALIGN 4 | ||
38 | #else | ||
39 | #define EXTRA_ALIGN 0 | ||
40 | #endif | ||
41 | |||
42 | |||
43 | static int use_m_guard = 0; | ||
44 | |||
45 | /**************** | ||
46 | * Warning: Never use this function after any of the functions | ||
47 | * here have been used. | ||
48 | */ | ||
49 | void | ||
50 | _gcry_private_enable_m_guard(void) | ||
51 | { | ||
52 | use_m_guard = 1; | ||
53 | } | ||
54 | |||
55 | /**************** | ||
56 | * Allocate memory of size n. | ||
57 | * Return NULL if we are out of memory. | ||
58 | */ | ||
59 | void * | ||
60 | _gcry_private_malloc( size_t n) | ||
61 | { | ||
62 | if(!n) | ||
63 | return NULL; /* allocating 0 bytes is undefined - better return | ||
64 | an error */ | ||
65 | if( use_m_guard ) { | ||
66 | char *p; | ||
67 | |||
68 | if( !(p = malloc( n + EXTRA_ALIGN+5 )) ) | ||
69 | return NULL; | ||
70 | ((byte*)p)[EXTRA_ALIGN+0] = n; | ||
71 | ((byte*)p)[EXTRA_ALIGN+1] = n >> 8 ; | ||
72 | ((byte*)p)[EXTRA_ALIGN+2] = n >> 16 ; | ||
73 | ((byte*)p)[EXTRA_ALIGN+3] = MAGIC_NOR_BYTE; | ||
74 | p[4+EXTRA_ALIGN+n] = MAGIC_END_BYTE; | ||
75 | return p+EXTRA_ALIGN+4; | ||
76 | } | ||
77 | else { | ||
78 | return malloc( n ); | ||
79 | } | ||
80 | } | ||
81 | |||
82 | /**************** | ||
83 | * Allocate memory of size n from the secure memory pool. | ||
84 | * Return NULL if we are out of memory. | ||
85 | */ | ||
86 | void * | ||
87 | _gcry_private_malloc_secure( size_t n) | ||
88 | { | ||
89 | if(!n) | ||
90 | return NULL; /* allocating 0 bytes is undefined - better return | ||
91 | an error */ | ||
92 | if( use_m_guard ) { | ||
93 | char *p; | ||
94 | |||
95 | if( !(p = _gcry_secmem_malloc( n +EXTRA_ALIGN+ 5 )) ) | ||
96 | return NULL; | ||
97 | ((byte*)p)[EXTRA_ALIGN+0] = n; | ||
98 | ((byte*)p)[EXTRA_ALIGN+1] = n >> 8 ; | ||
99 | ((byte*)p)[EXTRA_ALIGN+2] = n >> 16 ; | ||
100 | ((byte*)p)[EXTRA_ALIGN+3] = MAGIC_SEC_BYTE; | ||
101 | p[4+EXTRA_ALIGN+n] = MAGIC_END_BYTE; | ||
102 | return p+EXTRA_ALIGN+4; | ||
103 | } | ||
104 | else { | ||
105 | return _gcry_secmem_malloc( n ); | ||
106 | } | ||
107 | } | ||
108 | |||
109 | |||
110 | /**************** | ||
111 | * realloc and clear the old space | ||
112 | * Return NULL if there is not enoug memory. | ||
113 | */ | ||
114 | void * | ||
115 | _gcry_private_realloc( void *a, size_t n ) | ||
116 | { | ||
117 | if( use_m_guard ) { | ||
118 | unsigned char *p = a; | ||
119 | char *b; | ||
120 | size_t len; | ||
121 | |||
122 | if (!a) | ||
123 | return _gcry_private_malloc(n); | ||
124 | |||
125 | _gcry_private_check_heap(p); | ||
126 | len = p[-4]; | ||
127 | len |= p[-3] << 8; | ||
128 | len |= p[-2] << 16; | ||
129 | if( len >= n ) /* we don't shrink for now */ | ||
130 | return a; | ||
131 | if( p[-1] == MAGIC_SEC_BYTE ) | ||
132 | b = _gcry_private_malloc_secure(n); | ||
133 | else | ||
134 | b = _gcry_private_malloc(n); | ||
135 | if( !b ) | ||
136 | return NULL; | ||
137 | memcpy(b, a, len ); | ||
138 | memset(b+len, 0, n-len ); | ||
139 | _gcry_private_free( p ); | ||
140 | return b; | ||
141 | } | ||
142 | else if( _gcry_private_is_secure(a) ) { | ||
143 | return _gcry_secmem_realloc( a, n ); | ||
144 | } | ||
145 | else { | ||
146 | return realloc( a, n ); | ||
147 | } | ||
148 | } | ||
149 | |||
150 | |||
151 | void | ||
152 | _gcry_private_check_heap( const void *a ) | ||
153 | { | ||
154 | if( use_m_guard ) { | ||
155 | const byte *p = a; | ||
156 | size_t len; | ||
157 | |||
158 | if( !p ) | ||
159 | return; | ||
160 | |||
161 | if( !(p[-1] == MAGIC_NOR_BYTE || p[-1] == MAGIC_SEC_BYTE) ) | ||
162 | _gcry_log_fatal("memory at %p corrupted (underflow=%02x)\n", p, p[-1] ); | ||
163 | len = p[-4]; | ||
164 | len |= p[-3] << 8; | ||
165 | len |= p[-2] << 16; | ||
166 | if( p[len] != MAGIC_END_BYTE ) | ||
167 | _gcry_log_fatal("memory at %p corrupted (overflow=%02x)\n", p, p[-1] ); | ||
168 | } | ||
169 | } | ||
170 | |||
171 | /**************** | ||
172 | * Free a memory block allocated by this opr the secmem module | ||
173 | */ | ||
174 | void | ||
175 | _gcry_private_free( void *a ) | ||
176 | { | ||
177 | byte *p = a; | ||
178 | |||
179 | if( !p ) | ||
180 | return; | ||
181 | if( use_m_guard ) { | ||
182 | _gcry_private_check_heap(p); | ||
183 | if( _gcry_private_is_secure(a) ) | ||
184 | _gcry_secmem_free(p-EXTRA_ALIGN-4); | ||
185 | else { | ||
186 | free(p-EXTRA_ALIGN-4); | ||
187 | } | ||
188 | } | ||
189 | else if( _gcry_private_is_secure(a) ) | ||
190 | _gcry_secmem_free(p); | ||
191 | else | ||
192 | free(p); | ||
193 | } | ||
194 | |||
195 | |||
diff --git a/pwmanager/libcrypt/crypt/stdmem.h b/pwmanager/libcrypt/crypt/stdmem.h new file mode 100644 index 0000000..cf83f68 --- a/dev/null +++ b/pwmanager/libcrypt/crypt/stdmem.h | |||
@@ -0,0 +1,32 @@ | |||
1 | /* stdmem.h - internal definitions for stdmem | ||
2 | *Copyright (C) 2000, 2002 Free Software Foundation, Inc. | ||
3 | * | ||
4 | * This file is part of Libgcrypt. | ||
5 | * | ||
6 | * Libgcrypt is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU Lesser general Public License as | ||
8 | * published by the Free Software Foundation; either version 2.1 of | ||
9 | * the License, or (at your option) any later version. | ||
10 | * | ||
11 | * Libgcrypt is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * GNU Lesser General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU Lesser General Public | ||
17 | * License along with this program; if not, write to the Free Software | ||
18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | ||
19 | */ | ||
20 | |||
21 | #ifndef G10_STDMEM_H | ||
22 | #define G10_STDMEM_H 1 | ||
23 | |||
24 | void _gcry_private_enable_m_guard(void); | ||
25 | |||
26 | void *_gcry_private_malloc (size_t n) GCC_ATTR_MALLOC; | ||
27 | void *_gcry_private_malloc_secure (size_t n) GCC_ATTR_MALLOC; | ||
28 | void *_gcry_private_realloc (void *a, size_t n); | ||
29 | void _gcry_private_check_heap (const void *a); | ||
30 | void _gcry_private_free (void *a); | ||
31 | |||
32 | #endif /* G10_STDMEM_H */ | ||
diff --git a/pwmanager/libcrypt/crypt/types.h b/pwmanager/libcrypt/crypt/types.h new file mode 100644 index 0000000..c3a9d93 --- a/dev/null +++ b/pwmanager/libcrypt/crypt/types.h | |||
@@ -0,0 +1,124 @@ | |||
1 | /* types.h - some common typedefs | ||
2 | *Copyright (C) 1998, 2000, 2002, 2003 Free Software Foundation, Inc. | ||
3 | * | ||
4 | * This file is part of Libgcrypt. | ||
5 | * | ||
6 | * Libgcrypt is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU Lesser general Public License as | ||
8 | * published by the Free Software Foundation; either version 2.1 of | ||
9 | * the License, or (at your option) any later version. | ||
10 | * | ||
11 | * Libgcrypt is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * GNU Lesser General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU Lesser General Public | ||
17 | * License along with this program; if not, write to the Free Software | ||
18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | ||
19 | */ | ||
20 | |||
21 | #ifndef GCRYPT_TYPES_H | ||
22 | #define GCRYPT_TYPES_H | ||
23 | |||
24 | |||
25 | /* The AC_CHECK_SIZEOF() in configure fails for some machines. | ||
26 | * we provide some fallback values here */ | ||
27 | #if !SIZEOF_UNSIGNED_SHORT | ||
28 | #undef SIZEOF_UNSIGNED_SHORT | ||
29 | #define SIZEOF_UNSIGNED_SHORT 2 | ||
30 | #endif | ||
31 | #if !SIZEOF_UNSIGNED_INT | ||
32 | #undef SIZEOF_UNSIGNED_INT | ||
33 | #define SIZEOF_UNSIGNED_INT 4 | ||
34 | #endif | ||
35 | #if !SIZEOF_UNSIGNED_LONG | ||
36 | #undef SIZEOF_UNSIGNED_LONG | ||
37 | #define SIZEOF_UNSIGNED_LONG 4 | ||
38 | #endif | ||
39 | |||
40 | |||
41 | #include <sys/types.h> | ||
42 | |||
43 | |||
44 | #ifndef HAVE_BYTE_TYPEDEF | ||
45 | #undef byte /* maybe there is a macro with this name */ | ||
46 | typedef unsigned char byte; | ||
47 | #define HAVE_BYTE_TYPEDEF | ||
48 | #endif | ||
49 | |||
50 | #ifndef HAVE_USHORT_TYPEDEF | ||
51 | #undef ushort /* maybe there is a macro with this name */ | ||
52 | typedef unsigned short ushort; | ||
53 | #define HAVE_USHORT_TYPEDEF | ||
54 | #endif | ||
55 | |||
56 | #ifndef HAVE_ULONG_TYPEDEF | ||
57 | #undef ulong /* maybe there is a macro with this name */ | ||
58 | typedef unsigned long ulong; | ||
59 | #define HAVE_ULONG_TYPEDEF | ||
60 | #endif | ||
61 | |||
62 | #ifndef HAVE_U16_TYPEDEF | ||
63 | #undef u16 /* maybe there is a macro with this name */ | ||
64 | #if SIZEOF_UNSIGNED_INT == 2 | ||
65 | typedef unsigned int u16; | ||
66 | #elif SIZEOF_UNSIGNED_SHORT == 2 | ||
67 | typedef unsigned short u16; | ||
68 | #else | ||
69 | #error no typedef for u16 | ||
70 | #endif | ||
71 | #define HAVE_U16_TYPEDEF | ||
72 | #endif | ||
73 | |||
74 | #ifndef HAVE_U32_TYPEDEF | ||
75 | #undef u32 /* maybe there is a macro with this name */ | ||
76 | #if SIZEOF_UNSIGNED_INT == 4 | ||
77 | typedef unsigned int u32; | ||
78 | #elif SIZEOF_UNSIGNED_LONG == 4 | ||
79 | typedef unsigned long u32; | ||
80 | #else | ||
81 | #error no typedef for u32 | ||
82 | #endif | ||
83 | #define HAVE_U32_TYPEDEF | ||
84 | #endif | ||
85 | |||
86 | /**************** | ||
87 | * Warning: Some systems segfault when this u64 typedef and | ||
88 | * the dummy code in cipher/md.c is not available. Examples are | ||
89 | * Solaris and IRIX. | ||
90 | */ | ||
91 | #ifndef HAVE_U64_TYPEDEF | ||
92 | #undef u64 /* maybe there is a macro with this name */ | ||
93 | #if SIZEOF_UNSIGNED_INT == 8 | ||
94 | typedef unsigned int u64; | ||
95 | #define U64_C(c) (c ## U) | ||
96 | #define HAVE_U64_TYPEDEF | ||
97 | #elif SIZEOF_UNSIGNED_LONG == 8 | ||
98 | typedef unsigned long u64; | ||
99 | #define U64_C(c) (c ## UL) | ||
100 | #define HAVE_U64_TYPEDEF | ||
101 | #elif SIZEOF_UNSIGNED_LONG_LONG == 8 | ||
102 | typedef unsigned long long u64; | ||
103 | #define U64_C(c) (c ## ULL) | ||
104 | #define HAVE_U64_TYPEDEF | ||
105 | #elif SIZEOF_UINT64_T == 8 | ||
106 | typedef uint64_t u64; | ||
107 | #define U64_C(c) (UINT64_C(c)) | ||
108 | #define HAVE_U64_TYPEDEF | ||
109 | #endif | ||
110 | #endif | ||
111 | |||
112 | typedef union { | ||
113 | int a; | ||
114 | short b; | ||
115 | char c[1]; | ||
116 | long d; | ||
117 | #ifdef HAVE_U64_TYPEDEF | ||
118 | u64 e; | ||
119 | #endif | ||
120 | float f; | ||
121 | double g; | ||
122 | } PROPERLY_ALIGNED_TYPE; | ||
123 | |||
124 | #endif /*GCRYPT_TYPES_H*/ | ||