crypter: Define a ChaCha20/Poly1305 AEAD algorithm identifier
[strongswan.git] / src / libstrongswan / crypto / crypters / crypter.c
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 #include <asn1/oid.h>
18
19 #include "crypter.h"
20
21 ENUM_BEGIN(encryption_algorithm_names, ENCR_DES_IV64, ENCR_DES_IV32,
22 "DES_IV64",
23 "DES_CBC",
24 "3DES_CBC",
25 "RC5_CBC",
26 "IDEA_CBC",
27 "CAST_CBC",
28 "BLOWFISH_CBC",
29 "3IDEA",
30 "DES_IV32");
31 ENUM_NEXT(encryption_algorithm_names, ENCR_NULL, ENCR_AES_CCM_ICV16, ENCR_DES_IV32,
32 "NULL",
33 "AES_CBC",
34 "AES_CTR",
35 "AES_CCM_8",
36 "AES_CCM_12",
37 "AES_CCM_16");
38 ENUM_NEXT(encryption_algorithm_names, ENCR_AES_GCM_ICV8, ENCR_NULL_AUTH_AES_GMAC, ENCR_AES_CCM_ICV16,
39 "AES_GCM_8",
40 "AES_GCM_12",
41 "AES_GCM_16",
42 "NULL_AES_GMAC");
43 ENUM_NEXT(encryption_algorithm_names, ENCR_CAMELLIA_CBC, ENCR_CHACHA20_POLY1305, ENCR_NULL_AUTH_AES_GMAC,
44 "CAMELLIA_CBC",
45 "CAMELLIA_CTR",
46 "CAMELLIA_CCM_8",
47 "CAMELLIA_CCM_12",
48 "CAMELLIA_CCM_16",
49 "CHACHA20_POLY1305");
50 ENUM_NEXT(encryption_algorithm_names, ENCR_UNDEFINED, ENCR_RC2_CBC, ENCR_CHACHA20_POLY1305,
51 "UNDEFINED",
52 "DES_ECB",
53 "SERPENT_CBC",
54 "TWOFISH_CBC",
55 "RC2_CBC");
56 ENUM_END(encryption_algorithm_names, ENCR_RC2_CBC);
57
58 /*
59 * Described in header.
60 */
61 encryption_algorithm_t encryption_algorithm_from_oid(int oid, size_t *key_size)
62 {
63 encryption_algorithm_t alg;
64 size_t alg_key_size;
65
66 switch (oid)
67 {
68 case OID_DES_CBC:
69 alg = ENCR_DES;
70 alg_key_size = 0;
71 break;
72 case OID_3DES_EDE_CBC:
73 alg = ENCR_3DES;
74 alg_key_size = 0;
75 break;
76 case OID_AES128_CBC:
77 alg = ENCR_AES_CBC;
78 alg_key_size = 128;
79 break;
80 case OID_AES192_CBC:
81 alg = ENCR_AES_CBC;
82 alg_key_size = 192;
83 break;
84 case OID_AES256_CBC:
85 alg = ENCR_AES_CBC;
86 alg_key_size = 256;
87 break;
88 case OID_CAMELLIA128_CBC:
89 alg = ENCR_CAMELLIA_CBC;
90 alg_key_size = 128;
91 break;
92 case OID_CAMELLIA192_CBC:
93 alg = ENCR_CAMELLIA_CBC;
94 alg_key_size = 192;
95 break;
96 case OID_CAMELLIA256_CBC:
97 alg = ENCR_CAMELLIA_CBC;
98 alg_key_size = 256;
99 break;
100 case OID_BLOWFISH_CBC:
101 alg = ENCR_BLOWFISH;
102 alg_key_size = 0;
103 break;
104 default:
105 alg = ENCR_UNDEFINED;
106 alg_key_size = 0;
107 }
108 if (key_size)
109 {
110 *key_size = alg_key_size;
111 }
112 return alg;
113 }
114
115 /*
116 * Described in header.
117 */
118 int encryption_algorithm_to_oid(encryption_algorithm_t alg, size_t key_size)
119 {
120 int oid;
121
122 switch(alg)
123 {
124 case ENCR_DES:
125 oid = OID_DES_CBC;
126 break;
127 case ENCR_3DES:
128 oid = OID_3DES_EDE_CBC;
129 break;
130 case ENCR_AES_CBC:
131 switch (key_size)
132 {
133 case 128:
134 oid = OID_AES128_CBC;
135 break;
136 case 192:
137 oid = OID_AES192_CBC;
138 break;
139 case 256:
140 oid = OID_AES256_CBC;
141 break;
142 default:
143 oid = OID_UNKNOWN;
144 }
145 break;
146 case ENCR_CAMELLIA_CBC:
147 switch (key_size)
148 {
149 case 128:
150 oid = OID_CAMELLIA128_CBC;
151 break;
152 case 192:
153 oid = OID_CAMELLIA192_CBC;
154 break;
155 case 256:
156 oid = OID_CAMELLIA256_CBC;
157 break;
158 default:
159 oid = OID_UNKNOWN;
160 }
161 break;
162 case ENCR_BLOWFISH:
163 oid = OID_BLOWFISH_CBC;
164 break;
165 default:
166 oid = OID_UNKNOWN;
167 }
168 return oid;
169 }
170
171 /*
172 * Described in header.
173 */
174 bool encryption_algorithm_is_aead(encryption_algorithm_t alg)
175 {
176 switch (alg)
177 {
178 case ENCR_AES_CCM_ICV8:
179 case ENCR_AES_CCM_ICV12:
180 case ENCR_AES_CCM_ICV16:
181 case ENCR_AES_GCM_ICV8:
182 case ENCR_AES_GCM_ICV12:
183 case ENCR_AES_GCM_ICV16:
184 case ENCR_NULL_AUTH_AES_GMAC:
185 case ENCR_CAMELLIA_CCM_ICV8:
186 case ENCR_CAMELLIA_CCM_ICV12:
187 case ENCR_CAMELLIA_CCM_ICV16:
188 case ENCR_CHACHA20_POLY1305:
189 return TRUE;
190 default:
191 return FALSE;
192 }
193 }