root/openpgpsdk/trunk/include/openpgpsdk/crypto.h

Revision 701 (checked in by ben, 4 years ago)

Code contributed by Alasdair Mackintosh <alasdair@google.com> to do
partial length encodings and signatures much more cleanly.

Line 
1 /*
2  * Copyright (c) 2005-2008 Nominet UK (www.nic.uk)
3  * All rights reserved.
4  * Contributors: Ben Laurie, Rachel Willmer. The Contributors have asserted
5  * their moral rights under the UK Copyright Design and Patents Act 1988 to
6  * be recorded as the authors of this copyright work.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9  * use this file except in compliance with the License.
10  *
11  * You may obtain a copy of the License at
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21
22 /** \file
23  */
24
25 #ifndef OPS_CRYPTO_H
26 #define OPS_CRYPTO_H
27
28 #include "keyring.h"
29 #include "util.h"
30 #include "packet.h"
31 #include "packet-parse.h"
32 #include <openssl/dsa.h>
33 #include <openssl/opensslv.h>
34 #include <openssl/opensslconf.h>
35
36 #if OPENSSL_VERSION_NUMBER < 0x00908030L
37 # define OPENSSL_NO_CAMELLIA
38 #endif
39
40 #define OPS_MIN_HASH_SIZE       16
41
42 typedef void ops_hash_init_t(ops_hash_t *hash);
43 typedef void ops_hash_add_t(ops_hash_t *hash,const unsigned char *data,
44                         unsigned length);
45 typedef unsigned ops_hash_finish_t(ops_hash_t *hash,unsigned char *out);
46
47 /** _ops_hash_t */
48 struct _ops_hash_t
49     {
50     ops_hash_algorithm_t algorithm;
51     size_t size;
52     const char *name;
53     ops_hash_init_t *init;
54     ops_hash_add_t *add;
55     ops_hash_finish_t *finish;
56     void *data;
57     };
58
59 typedef void ops_crypt_set_iv_t(ops_crypt_t *crypt,
60                                 const unsigned char *iv);
61 typedef void ops_crypt_set_key_t(ops_crypt_t *crypt,
62                                  const unsigned char *key);
63 typedef void ops_crypt_init_t(ops_crypt_t *crypt);
64 typedef void ops_crypt_resync_t(ops_crypt_t *crypt);
65 typedef void ops_crypt_block_encrypt_t(ops_crypt_t *crypt,void *out,
66                                        const void *in);
67 typedef void ops_crypt_block_decrypt_t(ops_crypt_t *crypt,void *out,
68                                        const void *in);
69 typedef void ops_crypt_cfb_encrypt_t(ops_crypt_t *crypt,void *out,
70                                      const void *in, size_t count);
71 typedef void ops_crypt_cfb_decrypt_t(ops_crypt_t *crypt,void *out,
72                                      const void *in, size_t count);
73 typedef void ops_crypt_finish_t(ops_crypt_t *crypt);
74
75 /** _ops_crypt_t */
76 struct _ops_crypt_t
77     {
78     ops_symmetric_algorithm_t algorithm;
79     size_t blocksize;
80     size_t keysize;
81     ops_crypt_set_iv_t *set_iv; /* Call this before decrypt init! */
82     ops_crypt_set_key_t *set_key; /* Call this before init! */
83     ops_crypt_init_t *base_init;
84     ops_crypt_resync_t *decrypt_resync;
85     // encrypt/decrypt one block
86     ops_crypt_block_encrypt_t *block_encrypt;
87     ops_crypt_block_decrypt_t *block_decrypt;
88
89     // Standard CFB encrypt/decrypt (as used by Sym Enc Int Prot packets)
90     ops_crypt_cfb_encrypt_t *cfb_encrypt;
91     ops_crypt_cfb_decrypt_t *cfb_decrypt;
92
93     ops_crypt_finish_t *decrypt_finish;
94     unsigned char iv[OPS_MAX_BLOCK_SIZE];
95     unsigned char civ[OPS_MAX_BLOCK_SIZE];
96     unsigned char siv[OPS_MAX_BLOCK_SIZE]; /* Needed for weird v3 resync */
97     unsigned char key[OPS_MAX_KEY_SIZE];
98     size_t num; /* Offset - see openssl _encrypt doco */
99     void *encrypt_key;
100     void *decrypt_key;
101     };
102
103 void ops_crypto_init(void);
104 void ops_crypto_finish(void);
105 void ops_hash_md5(ops_hash_t *hash);
106 void ops_hash_sha1(ops_hash_t *hash);
107 void ops_hash_sha256(ops_hash_t *hash);
108 void ops_hash_sha512(ops_hash_t *hash);
109 void ops_hash_sha384(ops_hash_t *hash);
110 void ops_hash_sha224(ops_hash_t *hash);
111 void ops_hash_any(ops_hash_t *hash,ops_hash_algorithm_t alg);
112 ops_hash_algorithm_t ops_hash_algorithm_from_text(const char *hash);
113 const char *ops_text_from_hash(ops_hash_t *hash);
114 unsigned ops_hash_size(ops_hash_algorithm_t alg);
115 unsigned ops_hash(unsigned char *out,ops_hash_algorithm_t alg,const void *in,
116                   size_t length);
117
118 void ops_hash_add_int(ops_hash_t *hash,unsigned n,unsigned length);
119
120 ops_boolean_t ops_dsa_verify(const unsigned char *hash,size_t hash_length,
121                              const ops_dsa_signature_t *sig,
122                              const ops_dsa_public_key_t *dsa);
123 int ops_rsa_public_decrypt(unsigned char *out,const unsigned char *in,
124                            size_t length,const ops_rsa_public_key_t *rsa);
125 int ops_rsa_public_encrypt(unsigned char *out,const unsigned char *in,
126                            size_t length,const ops_rsa_public_key_t *rsa);
127 int ops_rsa_private_encrypt(unsigned char *out,const unsigned char *in,
128                             size_t length,const ops_rsa_secret_key_t *srsa,
129                             const ops_rsa_public_key_t *rsa);
130 int ops_rsa_private_decrypt(unsigned char *out,const unsigned char *in,
131                             size_t length,const ops_rsa_secret_key_t *srsa,
132                             const ops_rsa_public_key_t *rsa);
133
134 unsigned ops_block_size(ops_symmetric_algorithm_t alg);
135 unsigned ops_key_size(ops_symmetric_algorithm_t alg);
136
137 int ops_decrypt_data(ops_content_tag_t tag,ops_region_t *region,
138                      ops_parse_info_t *parse_info);
139
140 int ops_crypt_any(ops_crypt_t *decrypt,ops_symmetric_algorithm_t alg);
141 void ops_decrypt_init(ops_crypt_t *decrypt);
142 void ops_encrypt_init(ops_crypt_t *encrypt);
143 size_t ops_decrypt_se(ops_crypt_t *decrypt,void *out,const void *in,
144                    size_t count);
145 size_t ops_encrypt_se(ops_crypt_t *encrypt,void *out,const void *in,
146                    size_t count);
147 size_t ops_decrypt_se_ip(ops_crypt_t *decrypt,void *out,const void *in,
148                    size_t count);
149 size_t ops_encrypt_se_ip(ops_crypt_t *encrypt,void *out,const void *in,
150                    size_t count);
151 ops_boolean_t ops_is_sa_supported(ops_symmetric_algorithm_t alg);
152
153 void ops_reader_push_decrypt(ops_parse_info_t *pinfo,ops_crypt_t *decrypt,
154                              ops_region_t *region);
155 void ops_reader_pop_decrypt(ops_parse_info_t *pinfo);
156
157 // Hash everything that's read
158 void ops_reader_push_hash(ops_parse_info_t *pinfo,ops_hash_t *hash);
159 void ops_reader_pop_hash(ops_parse_info_t *pinfo);
160
161 int ops_decrypt_and_unencode_mpi(unsigned char *buf,unsigned buflen,const BIGNUM *encmpi,
162                     const ops_secret_key_t *skey);
163 ops_boolean_t ops_rsa_encrypt_mpi(const unsigned char *buf, const size_t buflen,
164                               const ops_public_key_t *pkey,
165                               ops_pk_session_key_parameters_t *spk);
166
167
168 // Encrypt everything that's written
169 struct ops_key_data;
170 void ops_writer_push_encrypt(ops_create_info_t *info,
171                              const struct ops_key_data *key);
172
173 ops_boolean_t ops_encrypt_file(const char* input_filename, const char* output_filename, const ops_keydata_t *pub_key, const ops_boolean_t use_armour, const ops_boolean_t allow_overwrite);
174 ops_boolean_t ops_decrypt_file(const char* input_filename, const char* output_filename, ops_keyring_t *keyring, const ops_boolean_t use_armour, const ops_boolean_t allow_overwrite,ops_parse_cb_t* cb_get_passphrase);
175 extern void ops_encrypt_stream(ops_create_info_t* cinfo, const ops_keydata_t* public_key, const ops_secret_key_t* secret_key, const ops_boolean_t compress, const ops_boolean_t use_armour);
176
177 // Keys
178 ops_boolean_t ops_rsa_generate_keypair(const int numbits, const unsigned long e, ops_keydata_t* keydata);
179 ops_keydata_t* ops_rsa_create_selfsigned_keypair(const int numbits, const unsigned long e, ops_user_id_t * userid);
180
181 int ops_dsa_size(const ops_dsa_public_key_t *dsa);
182 DSA_SIG* ops_dsa_sign(unsigned char* hashbuf, unsigned hashsize, const ops_dsa_secret_key_t *sdsa, const ops_dsa_public_key_t *dsa);
183 #endif
184
Note: See TracBrowser for help on using the browser.