xref: /netbsd-src/crypto/external/bsd/netpgp/dist/src/lib/crypto.h (revision 93bf6008f8b7982c1d1a9486e4a4a0e687fe36eb)
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 "packet.h"
30 #include "packet-parse.h"
31 
32 #include <openssl/dsa.h>
33 
34 #define OPS_MIN_HASH_SIZE	16
35 
36 typedef void    __ops_hash_init_t(__ops_hash_t *);
37 typedef void __ops_hash_add_t(__ops_hash_t *, const unsigned char *, unsigned);
38 typedef unsigned __ops_hash_finish_t(__ops_hash_t *, unsigned char *);
39 
40 /** _ops_hash_t */
41 struct _ops_hash_t {
42 	__ops_hash_algorithm_t algorithm;
43 	size_t          size;
44 	const char     *name;
45 	__ops_hash_init_t *init;
46 	__ops_hash_add_t *add;
47 	__ops_hash_finish_t *finish;
48 	void           *data;
49 };
50 
51 typedef void __ops_crypt_set_iv_t(__ops_crypt_t *, const unsigned char *);
52 typedef void __ops_crypt_set_key_t(__ops_crypt_t *, const unsigned char *);
53 typedef void    __ops_crypt_init_t(__ops_crypt_t *);
54 typedef void    __ops_crypt_resync_t(__ops_crypt_t *);
55 typedef void __ops_crypt_block_encrypt_t(__ops_crypt_t *, void *, const void *);
56 typedef void __ops_crypt_block_decrypt_t(__ops_crypt_t *, void *, const void *);
57 typedef void __ops_crypt_cfb_encrypt_t(__ops_crypt_t *, void *, const void *, size_t);
58 typedef void __ops_crypt_cfb_decrypt_t(__ops_crypt_t *, void *, const void *, size_t);
59 typedef void    __ops_crypt_finish_t(__ops_crypt_t *);
60 
61 /** _ops_crypt_t */
62 struct _ops_crypt_t {
63 	__ops_symmetric_algorithm_t algorithm;
64 	size_t          blocksize;
65 	size_t          keysize;
66 	__ops_crypt_set_iv_t *set_iv;	/* Call this before decrypt init! */
67 	__ops_crypt_set_key_t *set_key;	/* Call this before init! */
68 	__ops_crypt_init_t *base_init;
69 	__ops_crypt_resync_t *decrypt_resync;
70 	/* encrypt/decrypt one block  */
71 	__ops_crypt_block_encrypt_t *block_encrypt;
72 	__ops_crypt_block_decrypt_t *block_decrypt;
73 
74 	/* Standard CFB encrypt/decrypt (as used by Sym Enc Int Prot packets) */
75 	__ops_crypt_cfb_encrypt_t *cfb_encrypt;
76 	__ops_crypt_cfb_decrypt_t *cfb_decrypt;
77 
78 	__ops_crypt_finish_t *decrypt_finish;
79 	unsigned char   iv[OPS_MAX_BLOCK_SIZE];
80 	unsigned char   civ[OPS_MAX_BLOCK_SIZE];
81 	unsigned char   siv[OPS_MAX_BLOCK_SIZE];	/* Needed for weird v3
82 							 * resync */
83 	unsigned char   key[OPS_MAX_KEY_SIZE];
84 	int             num;	/* Offset - see openssl _encrypt doco */
85 	void           *encrypt_key;
86 	void           *decrypt_key;
87 };
88 
89 void            __ops_crypto_init(void);
90 void            __ops_crypto_finish(void);
91 void            __ops_hash_md5(__ops_hash_t *);
92 void            __ops_hash_sha1(__ops_hash_t *);
93 void            __ops_hash_sha256(__ops_hash_t *);
94 void            __ops_hash_sha512(__ops_hash_t *);
95 void            __ops_hash_sha384(__ops_hash_t *);
96 void            __ops_hash_sha224(__ops_hash_t *);
97 void            __ops_hash_any(__ops_hash_t *, __ops_hash_algorithm_t);
98 __ops_hash_algorithm_t __ops_hash_algorithm_from_text(const char *);
99 const char     *__ops_text_from_hash(__ops_hash_t *);
100 unsigned        __ops_hash_size(__ops_hash_algorithm_t);
101 unsigned __ops_hash(unsigned char *, __ops_hash_algorithm_t, const void *, size_t);
102 
103 void            __ops_hash_add_int(__ops_hash_t *, unsigned, unsigned);
104 
105 bool __ops_dsa_verify(const unsigned char *, size_t, const __ops_dsa_signature_t *, const __ops_dsa_public_key_t *);
106 
107 int __ops_rsa_public_decrypt(unsigned char *, const unsigned char *, size_t, const __ops_rsa_public_key_t *);
108 int __ops_rsa_public_encrypt(unsigned char *, const unsigned char *, size_t, const __ops_rsa_public_key_t *);
109 
110 int __ops_rsa_private_encrypt(unsigned char *, const unsigned char *, size_t, const __ops_rsa_secret_key_t *, const __ops_rsa_public_key_t *);
111 int __ops_rsa_private_decrypt(unsigned char *, const unsigned char *, size_t, const __ops_rsa_secret_key_t *, const __ops_rsa_public_key_t *);
112 
113 unsigned        __ops_block_size(__ops_symmetric_algorithm_t);
114 unsigned        __ops_key_size(__ops_symmetric_algorithm_t);
115 
116 int __ops_decrypt_data(__ops_content_tag_t, __ops_region_t *, __ops_parse_info_t *);
117 
118 int             __ops_crypt_any(__ops_crypt_t *, __ops_symmetric_algorithm_t);
119 void            __ops_decrypt_init(__ops_crypt_t *);
120 void            __ops_encrypt_init(__ops_crypt_t *);
121 size_t __ops_decrypt_se(__ops_crypt_t *, void *, const void *, size_t);
122 size_t __ops_encrypt_se(__ops_crypt_t *, void *, const void *, size_t);
123 size_t __ops_decrypt_se_ip(__ops_crypt_t *, void *, const void *, size_t);
124 size_t __ops_encrypt_se_ip(__ops_crypt_t *, void *, const void *, size_t);
125 bool   __ops_is_sa_supported(__ops_symmetric_algorithm_t);
126 
127 void __ops_reader_push_decrypt(__ops_parse_info_t *, __ops_crypt_t *, __ops_region_t *);
128 void            __ops_reader_pop_decrypt(__ops_parse_info_t *);
129 
130 /* Hash everything that's read */
131 void            __ops_reader_push_hash(__ops_parse_info_t *, __ops_hash_t *);
132 void            __ops_reader_pop_hash(__ops_parse_info_t *);
133 
134 int __ops_decrypt_and_unencode_mpi(unsigned char *, unsigned, const BIGNUM *, const __ops_secret_key_t *);
135 bool __ops_rsa_encrypt_mpi(const unsigned char *, const size_t, const __ops_public_key_t *, __ops_pk_session_key_parameters_t *);
136 
137 
138 /* Encrypt everything that's written */
139 struct __ops_key_data;
140 void __ops_writer_push_encrypt(__ops_create_info_t *, const struct __ops_key_data *);
141 
142 bool   __ops_encrypt_file(const char *, const char *, const __ops_keydata_t *, const bool, const bool);
143 bool   __ops_decrypt_file(const char *, const char *, __ops_keyring_t *, const bool, const bool, __ops_parse_cb_t *);
144 
145 /* Keys */
146 bool   __ops_rsa_generate_keypair(const int, const unsigned long, __ops_keydata_t *);
147 __ops_keydata_t  *__ops_rsa_create_selfsigned_keypair(const int, const unsigned long, __ops_user_id_t *);
148 
149 int             __ops_dsa_size(const __ops_dsa_public_key_t *);
150 DSA_SIG        *__ops_dsa_sign(unsigned char *, unsigned, const __ops_dsa_secret_key_t *, const __ops_dsa_public_key_t *);
151 
152 #endif
153