cleaned up pluto's crypto framework
[strongswan.git] / src / libstrongswan / crypto / crypters / crypter.h
1 /*
2 * Copyright (C) 2005-2006 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 /**
18 * @defgroup crypter crypter
19 * @{ @ingroup crypto
20 */
21
22 #ifndef CRYPTER_H_
23 #define CRYPTER_H_
24
25 typedef enum encryption_algorithm_t encryption_algorithm_t;
26 typedef struct crypter_t crypter_t;
27
28 #include <library.h>
29
30 /**
31 * Encryption algorithm, as in IKEv2 RFC 3.3.2.
32 */
33 enum encryption_algorithm_t {
34 ENCR_DES_IV64 = 1,
35 ENCR_DES = 2,
36 ENCR_3DES = 3,
37 ENCR_RC5 = 4,
38 ENCR_IDEA = 5,
39 ENCR_CAST = 6,
40 ENCR_BLOWFISH = 7,
41 ENCR_3IDEA = 8,
42 ENCR_DES_IV32 = 9,
43 ENCR_NULL = 11,
44 ENCR_AES_CBC = 12,
45 ENCR_AES_CTR = 13,
46 ENCR_AES_CCM_ICV8 = 14,
47 ENCR_AES_CCM_ICV12 = 15,
48 ENCR_AES_CCM_ICV16 = 16,
49 ENCR_AES_GCM_ICV8 = 18,
50 ENCR_AES_GCM_ICV12 = 19,
51 ENCR_AES_GCM_ICV16 = 20,
52 ENCR_NULL_AUTH_AES_GMAC = 21,
53 ENCR_CAMELLIA_CBC = 23,
54 ENCR_CAMELLIA_CTR = 24,
55 ENCR_CAMELLIA_CCM_ICV8 = 25,
56 ENCR_CAMELLIA_CCM_ICV12 = 26,
57 ENCR_CAMELLIA_CCM_ICV16 = 27,
58 ENCR_UNDEFINED = 1024,
59 ENCR_DES_ECB = 1025,
60 ENCR_SERPENT_CBC = 1026,
61 ENCR_TWOFISH_CBC = 1027
62 };
63
64 #define DES_BLOCK_SIZE 8
65 #define BLOWFISH_BLOCK_SIZE 8
66 #define AES_BLOCK_SIZE 16
67 #define SERPENT_BLOCK_SIZE 16
68 #define TWOFISH_BLOCK_SIZE 16
69
70 /**
71 * enum name for encryption_algorithm_t.
72 */
73 extern enum_name_t *encryption_algorithm_names;
74
75 /**
76 * Generic interface for symmetric encryption algorithms.
77 */
78 struct crypter_t {
79
80 /**
81 * Encrypt a chunk of data and allocate space for the encrypted value.
82 *
83 * The length of the iv must equal to get_block_size(), while the length
84 * of data must be a multiple it.
85 * If encrypted is NULL, the encryption is done in-place (overwriting data).
86 *
87 * @param data data to encrypt
88 * @param iv initializing vector
89 * @param encrypted chunk to allocate encrypted data, or NULL
90 */
91 void (*encrypt) (crypter_t *this, chunk_t data, chunk_t iv,
92 chunk_t *encrypted);
93
94 /**
95 * Decrypt a chunk of data and allocate space for the decrypted value.
96 *
97 * The length of the iv must equal to get_block_size(), while the length
98 * of data must be a multiple it.
99 * If decrpyted is NULL, the encryption is done in-place (overwriting data).
100 *
101 * @param data data to decrypt
102 * @param iv initializing vector
103 * @param encrypted chunk to allocate decrypted data, or NULL
104 */
105 void (*decrypt) (crypter_t *this, chunk_t data, chunk_t iv,
106 chunk_t *decrypted);
107
108 /**
109 * Get the block size of the crypto algorithm.
110 *
111 * @return block size in bytes
112 */
113 size_t (*get_block_size) (crypter_t *this);
114
115 /**
116 * Get the key size of the crypto algorithm.
117 *
118 * @return key size in bytes
119 */
120 size_t (*get_key_size) (crypter_t *this);
121
122 /**
123 * Set the key.
124 *
125 * The length of the key must match get_key_size().
126 *
127 * @param key key to set
128 */
129 void (*set_key) (crypter_t *this, chunk_t key);
130
131 /**
132 * Destroys a crypter_t object.
133 */
134 void (*destroy) (crypter_t *this);
135 };
136
137 /**
138 * Conversion of ASN.1 OID to encryption algorithm.
139 *
140 * @param oid ASN.1 OID
141 * @param key_size returns size of encryption key in bits
142 * @return encryption algorithm, ENCR_UNDEFINED if OID unsupported
143 */
144 encryption_algorithm_t encryption_algorithm_from_oid(int oid, size_t *key_size);
145
146 /**
147 * Conversion of encryption algorithm to ASN.1 OID.
148 *
149 * @param alg encryption algorithm
150 * @param key_size size of encryption key in bits
151 * @return ASN.1 OID, OID_UNKNOWN if OID is unknown
152 */
153 int encryption_algorithm_to_oid(encryption_algorithm_t alg, size_t key_size);
154
155 #endif /** CRYPTER_H_ @}*/