fda2f455b0ebb3ef19c37801978cd7f7c143a276
[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 default:
87 alg = ENCR_UNDEFINED;
88 alg_key_size = 0;
89 }
90 if (key_size)
91 {
92 *key_size = alg_key_size;
93 }
94 return alg;
95 }
96
97 /*
98 * Described in header.
99 */
100 int encryption_algorithm_to_oid(encryption_algorithm_t alg, size_t key_size)
101 {
102 int oid;
103
104 switch(alg)
105 {
106 case ENCR_DES:
107 oid = OID_DES_CBC;
108 break;
109 case ENCR_3DES:
110 oid = OID_3DES_EDE_CBC;
111 break;
112 case ENCR_AES_CBC:
113 switch (key_size)
114 {
115 case 128:
116 oid = OID_AES128_CBC;
117 break;
118 case 192:
119 oid = OID_AES192_CBC;
120 break;
121 case 256:
122 oid = OID_AES256_CBC;
123 break;
124 default:
125 oid = OID_UNKNOWN;
126 }
127 break;
128 default:
129 oid = OID_UNKNOWN;
130 }
131 return oid;
132 }
133
134