conversion from CAMELLIA OIDs to encryption_algorithm
[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_CAMELLIA_CCM_ICV16, ENCR_NULL_AUTH_AES_GMAC,
44 "CAMELLIA_CBC",
45 "CAMELLIA_CTR",
46 "CAMELLIA_CCM_8",
47 "CAMELLIA_CCM_12",
48 "CAMELLIA_CCM_16");
49 ENUM_NEXT(encryption_algorithm_names, ENCR_UNDEFINED, ENCR_TWOFISH_CBC, ENCR_CAMELLIA_CCM_ICV16,
50 "UNDEFINED",
51 "DES_ECB",
52 "SERPENT_CBC",
53 "TWOFISH_CBC");
54 ENUM_END(encryption_algorithm_names, ENCR_TWOFISH_CBC);
55
56 /*
57 * Described in header.
58 */
59 encryption_algorithm_t encryption_algorithm_from_oid(int oid, size_t *key_size)
60 {
61 encryption_algorithm_t alg;
62 size_t alg_key_size;
63
64 switch (oid)
65 {
66 case OID_DES_CBC:
67 alg = ENCR_DES;
68 alg_key_size = 0;
69 break;
70 case OID_3DES_EDE_CBC:
71 alg = ENCR_3DES;
72 alg_key_size = 0;
73 break;
74 case OID_AES128_CBC:
75 alg = ENCR_AES_CBC;
76 alg_key_size = 128;
77 break;
78 case OID_AES192_CBC:
79 alg = ENCR_AES_CBC;
80 alg_key_size = 192;
81 break;
82 case OID_AES256_CBC:
83 alg = ENCR_AES_CBC;
84 alg_key_size = 256;
85 break;
86 case OID_CAMELLIA128_CBC:
87 alg = ENCR_CAMELLIA_CBC;
88 alg_key_size = 128;
89 break;
90 case OID_CAMELLIA192_CBC:
91 alg = ENCR_CAMELLIA_CBC;
92 alg_key_size = 192;
93 break;
94 case OID_CAMELLIA256_CBC:
95 alg = ENCR_CAMELLIA_CBC;
96 alg_key_size = 256;
97 break;
98 default:
99 alg = ENCR_UNDEFINED;
100 alg_key_size = 0;
101 }
102 if (key_size)
103 {
104 *key_size = alg_key_size;
105 }
106 return alg;
107 }
108
109 /*
110 * Described in header.
111 */
112 int encryption_algorithm_to_oid(encryption_algorithm_t alg, size_t key_size)
113 {
114 int oid;
115
116 switch(alg)
117 {
118 case ENCR_DES:
119 oid = OID_DES_CBC;
120 break;
121 case ENCR_3DES:
122 oid = OID_3DES_EDE_CBC;
123 break;
124 case ENCR_AES_CBC:
125 switch (key_size)
126 {
127 case 128:
128 oid = OID_AES128_CBC;
129 break;
130 case 192:
131 oid = OID_AES192_CBC;
132 break;
133 case 256:
134 oid = OID_AES256_CBC;
135 break;
136 default:
137 oid = OID_UNKNOWN;
138 }
139 break;
140 case ENCR_CAMELLIA_CBC:
141 switch (key_size)
142 {
143 case 128:
144 oid = OID_CAMELLIA128_CBC;
145 break;
146 case 192:
147 oid = OID_CAMELLIA192_CBC;
148 break;
149 case 256:
150 oid = OID_CAMELLIA256_CBC;
151 break;
152 default:
153 oid = OID_UNKNOWN;
154 }
155 break;
156 default:
157 oid = OID_UNKNOWN;
158 }
159 return oid;
160 }
161
162