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

Revision 485 (checked in by rachel, 6 years ago)

RSA encryption produces packets which can be decrypted by GPG.
MDC error still to fix.
Some refactoring to re-use common code in tests.

Line 
1 /** \file
2  */
3
4 #ifndef OPS_CRYPTO_H
5 #define OPS_CRYPTO_H
6
7 #include "util.h"
8 #include "packet.h"
9 #include "packet-parse.h"
10
11 #define OPS_MIN_HASH_SIZE       16
12
13 typedef void ops_hash_init_t(ops_hash_t *hash);
14 typedef void ops_hash_add_t(ops_hash_t *hash,const unsigned char *data,
15                         unsigned length);
16 typedef unsigned ops_hash_finish_t(ops_hash_t *hash,unsigned char *out);
17
18 /** _ops_hash_t */
19 struct _ops_hash_t
20     {
21     ops_hash_algorithm_t algorithm;
22     size_t size;
23     const char *name;
24     ops_hash_init_t *init;
25     ops_hash_add_t *add;
26     ops_hash_finish_t *finish;
27     void *data;
28     };
29
30 typedef void ops_crypt_set_iv_t(ops_crypt_t *crypt,
31                                 const unsigned char *iv);
32 typedef void ops_crypt_set_key_t(ops_crypt_t *crypt,
33                                  const unsigned char *key);
34 typedef void ops_crypt_init_t(ops_crypt_t *crypt);
35 typedef void ops_crypt_resync_t(ops_crypt_t *crypt);
36 typedef void ops_crypt_block_encrypt_t(ops_crypt_t *crypt,void *out,
37                                        const void *in);
38 typedef void ops_crypt_block_decrypt_t(ops_crypt_t *crypt,void *out,
39                                        const void *in);
40 typedef void ops_crypt_finish_t(ops_crypt_t *crypt);
41
42 /** _ops_crypt_t */
43 struct _ops_crypt_t
44     {
45     ops_symmetric_algorithm_t algorithm;
46     size_t blocksize;
47     size_t keysize;
48     ops_crypt_set_iv_t *set_iv; /* Call this before decrypt init! */
49     ops_crypt_set_key_t *set_key; /* Call this before init! */
50     ops_crypt_init_t *base_init;
51     ops_crypt_resync_t *decrypt_resync;
52     ops_crypt_block_encrypt_t *block_encrypt;
53     ops_crypt_block_decrypt_t *block_decrypt;
54     ops_crypt_finish_t *decrypt_finish;
55     unsigned char iv[OPS_MAX_BLOCK_SIZE];
56     unsigned char civ[OPS_MAX_BLOCK_SIZE];
57     unsigned char siv[OPS_MAX_BLOCK_SIZE]; /* Needed for weird v3 resync */
58     unsigned char key[OPS_MAX_KEY_SIZE];
59     size_t num; /* Offset - see openssl _encrypt doco */
60     void *data;
61     };
62
63 void ops_crypto_init(void);
64 void ops_crypto_finish(void);
65 void ops_hash_md5(ops_hash_t *hash);
66 void ops_hash_sha1(ops_hash_t *hash);
67 void ops_hash_any(ops_hash_t *hash,ops_hash_algorithm_t alg);
68 ops_hash_algorithm_t ops_hash_algorithm_from_text(const char *hash);
69 const char *ops_text_from_hash(ops_hash_t *hash);
70 unsigned ops_hash_size(ops_hash_algorithm_t alg);
71 unsigned ops_hash(unsigned char *out,ops_hash_algorithm_t alg,const void *in,
72                   size_t length);
73
74 void ops_hash_add_int(ops_hash_t *hash,unsigned n,unsigned length);
75
76 ops_boolean_t ops_dsa_verify(const unsigned char *hash,size_t hash_length,
77                              const ops_dsa_signature_t *sig,
78                              const ops_dsa_public_key_t *dsa);
79 int ops_rsa_public_decrypt(unsigned char *out,const unsigned char *in,
80                            size_t length,const ops_rsa_public_key_t *rsa);
81 int ops_rsa_public_encrypt(unsigned char *out,const unsigned char *in,
82                            size_t length,const ops_rsa_public_key_t *rsa);
83 int ops_rsa_private_encrypt(unsigned char *out,const unsigned char *in,
84                             size_t length,const ops_rsa_secret_key_t *srsa,
85                             const ops_rsa_public_key_t *rsa);
86 int ops_rsa_private_decrypt(unsigned char *out,const unsigned char *in,
87                             size_t length,const ops_rsa_secret_key_t *srsa,
88                             const ops_rsa_public_key_t *rsa);
89
90 unsigned ops_block_size(ops_symmetric_algorithm_t alg);
91 unsigned ops_key_size(ops_symmetric_algorithm_t alg);
92
93 int ops_decrypt_data(ops_content_tag_t tag,ops_region_t *region,
94                      ops_parse_info_t *parse_info);
95
96 void ops_crypt_any(ops_crypt_t *decrypt,ops_symmetric_algorithm_t alg);
97 void ops_decrypt_init(ops_crypt_t *decrypt);
98 void ops_encrypt_init(ops_crypt_t *encrypt);
99 size_t ops_decrypt_se(ops_crypt_t *decrypt,void *out,const void *in,
100                    size_t count);
101 size_t ops_encrypt_se(ops_crypt_t *encrypt,void *out,const void *in,
102                    size_t count);
103 size_t ops_decrypt_se_ip(ops_crypt_t *decrypt,void *out,const void *in,
104                    size_t count);
105 size_t ops_encrypt_se_ip(ops_crypt_t *encrypt,void *out,const void *in,
106                    size_t count);
107
108 void ops_reader_push_decrypt(ops_parse_info_t *pinfo,ops_crypt_t *decrypt,
109                              ops_region_t *region);
110 void ops_reader_pop_decrypt(ops_parse_info_t *pinfo);
111
112 // Hash everything that's read
113 void ops_reader_push_hash(ops_parse_info_t *pinfo,ops_hash_t *hash);
114 void ops_reader_pop_hash(ops_parse_info_t *pinfo);
115
116 int ops_decrypt_and_unencode_mpi(unsigned char *buf,unsigned buflen,const BIGNUM *encmpi,
117                     const ops_secret_key_t *skey);
118 ops_boolean_t ops_encrypt_mpi(const unsigned char *buf, size_t buflen,
119                               const ops_public_key_t *pkey,
120                               ops_pk_session_key_parameters_t *spk);
121
122
123 // Encrypt everything that's written
124 struct ops_key_data;
125 void ops_writer_push_encrypt(ops_create_info_t *info,
126                              const struct ops_key_data *key);
127
128 #endif
129
Note: See TracBrowser for help on using the browser.