Version 3.0.12.
[pwmd.git] / src / cipher.c
blob0a23660fb1093e4c99e1e6b12163f18dbf73bfca
1 /*
2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014
3 Ben Kibbey <bjk@luxsci.net>
5 This file is part of pwmd.
7 Pwmd is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 2 of the License, or
10 (at your option) any later version.
12 Pwmd is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
24 #include <gcrypt.h>
25 #include <string.h>
27 #ifdef HAVE_STRINGS_H
28 #include <strings.h>
29 #endif
31 #include "cipher.h"
33 unsigned
34 gcrypt_to_cipher (int n)
36 if (n == GCRY_CIPHER_AES128)
37 return PWMD_CIPHER_AES128;
38 else if (n == GCRY_CIPHER_AES192)
39 return PWMD_CIPHER_AES192;
40 else if (n == GCRY_CIPHER_AES256)
41 return PWMD_CIPHER_AES256;
42 else if (n == GCRY_CIPHER_SERPENT128)
43 return PWMD_CIPHER_SERPENT128;
44 else if (n == GCRY_CIPHER_SERPENT192)
45 return PWMD_CIPHER_SERPENT192;
46 else if (n == GCRY_CIPHER_SERPENT256)
47 return PWMD_CIPHER_SERPENT256;
48 else if (n == GCRY_CIPHER_CAMELLIA128)
49 return PWMD_CIPHER_CAMELLIA128;
50 else if (n == GCRY_CIPHER_CAMELLIA192)
51 return PWMD_CIPHER_CAMELLIA192;
52 else if (n == GCRY_CIPHER_CAMELLIA256)
53 return PWMD_CIPHER_CAMELLIA256;
54 else if (n == GCRY_CIPHER_BLOWFISH)
55 return PWMD_CIPHER_BLOWFISH;
56 else if (n == GCRY_CIPHER_3DES)
57 return PWMD_CIPHER_3DES;
58 else if (n == GCRY_CIPHER_CAST5)
59 return PWMD_CIPHER_CAST5;
60 else if (n == GCRY_CIPHER_TWOFISH)
61 return PWMD_CIPHER_TWOFISH;
62 else if (n == GCRY_CIPHER_TWOFISH128)
63 return PWMD_CIPHER_TWOFISH128;
65 return PWMD_CIPHER_AES256;
68 int
69 cipher_to_gcrypt (int flags)
71 if (flags < 0)
72 return flags;
74 if (flags & PWMD_CIPHER_AES128)
75 return GCRY_CIPHER_AES128;
76 else if (flags & PWMD_CIPHER_AES192)
77 return GCRY_CIPHER_AES192;
78 else if (flags & PWMD_CIPHER_AES256)
79 return GCRY_CIPHER_AES256;
80 else if (flags & PWMD_CIPHER_SERPENT128)
81 return GCRY_CIPHER_SERPENT128;
82 else if (flags & PWMD_CIPHER_SERPENT192)
83 return GCRY_CIPHER_SERPENT192;
84 else if (flags & PWMD_CIPHER_SERPENT256)
85 return GCRY_CIPHER_SERPENT256;
86 else if (flags & PWMD_CIPHER_CAMELLIA128)
87 return GCRY_CIPHER_CAMELLIA128;
88 else if (flags & PWMD_CIPHER_CAMELLIA192)
89 return GCRY_CIPHER_CAMELLIA192;
90 else if (flags & PWMD_CIPHER_CAMELLIA256)
91 return GCRY_CIPHER_CAMELLIA256;
92 else if (flags & PWMD_CIPHER_BLOWFISH)
93 return GCRY_CIPHER_BLOWFISH;
94 else if (flags & PWMD_CIPHER_3DES)
95 return GCRY_CIPHER_3DES;
96 else if (flags & PWMD_CIPHER_CAST5)
97 return GCRY_CIPHER_CAST5;
98 else if (flags & PWMD_CIPHER_TWOFISH)
99 return GCRY_CIPHER_TWOFISH;
100 else if (flags & PWMD_CIPHER_TWOFISH128)
101 return GCRY_CIPHER_TWOFISH128;
103 return -1;
107 cipher_string_to_cipher (const char *str)
109 int flags = 0;
111 if (!strcasecmp (str, "aes128"))
112 flags = PWMD_CIPHER_AES128;
113 else if (!strcasecmp (str, "aes192"))
114 flags = PWMD_CIPHER_AES192;
115 else if (!strcasecmp (str, "aes256"))
116 flags = PWMD_CIPHER_AES256;
117 else if (!strcasecmp (str, "serpent128"))
118 flags = PWMD_CIPHER_SERPENT128;
119 else if (!strcasecmp (str, "serpent192"))
120 flags = PWMD_CIPHER_SERPENT192;
121 else if (!strcasecmp (str, "serpent256"))
122 flags = PWMD_CIPHER_SERPENT256;
123 else if (!strcasecmp (str, "camellia128"))
124 flags = PWMD_CIPHER_CAMELLIA128;
125 else if (!strcasecmp (str, "camellia192"))
126 flags = PWMD_CIPHER_CAMELLIA192;
127 else if (!strcasecmp (str, "camellia256"))
128 flags = PWMD_CIPHER_CAMELLIA256;
129 else if (!strcasecmp (str, "blowfish"))
130 flags = PWMD_CIPHER_BLOWFISH;
131 else if (!strcasecmp (str, "cast5"))
132 flags = PWMD_CIPHER_CAST5;
133 else if (!strcasecmp (str, "3des"))
134 flags = PWMD_CIPHER_3DES;
135 else if (!strcasecmp (str, "twofish256"))
136 flags = PWMD_CIPHER_TWOFISH;
137 else if (!strcasecmp (str, "twofish128"))
138 flags = PWMD_CIPHER_TWOFISH128;
139 else
140 return -1;
142 return flags;
146 cipher_string_to_gcrypt (const char *str)
148 return cipher_to_gcrypt (cipher_string_to_cipher (str));
151 uint64_t
152 set_cipher_flag (uint64_t flags, int algo)
154 // Clear all previous cipher bits but keep the flags.
155 flags &= ~((uint16_t) ((uint32_t) (flags & 0xFFFFFFFF)));
156 flags |= gcrypt_to_cipher (algo);
157 return flags;