Fix SAVE --keygrip and --sign-keygrip.
[pwmd.git] / src / cipher.c
blob9afaa28d10503e27821f27569057efa3cbf0f9b3
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 #include <gcrypt.h>
21 #include <string.h>
22 #include "cipher.h"
24 unsigned
25 gcrypt_to_cipher (int n)
27 if (n == GCRY_CIPHER_AES128)
28 return PWMD_CIPHER_AES128;
29 else if (n == GCRY_CIPHER_AES192)
30 return PWMD_CIPHER_AES192;
31 else if (n == GCRY_CIPHER_AES256)
32 return PWMD_CIPHER_AES256;
33 else if (n == GCRY_CIPHER_SERPENT128)
34 return PWMD_CIPHER_SERPENT128;
35 else if (n == GCRY_CIPHER_SERPENT192)
36 return PWMD_CIPHER_SERPENT192;
37 else if (n == GCRY_CIPHER_SERPENT256)
38 return PWMD_CIPHER_SERPENT256;
39 else if (n == GCRY_CIPHER_CAMELLIA128)
40 return PWMD_CIPHER_CAMELLIA128;
41 else if (n == GCRY_CIPHER_CAMELLIA192)
42 return PWMD_CIPHER_CAMELLIA192;
43 else if (n == GCRY_CIPHER_CAMELLIA256)
44 return PWMD_CIPHER_CAMELLIA256;
45 else if (n == GCRY_CIPHER_BLOWFISH)
46 return PWMD_CIPHER_BLOWFISH;
47 else if (n == GCRY_CIPHER_3DES)
48 return PWMD_CIPHER_3DES;
49 else if (n == GCRY_CIPHER_CAST5)
50 return PWMD_CIPHER_CAST5;
51 else if (n == GCRY_CIPHER_TWOFISH)
52 return PWMD_CIPHER_TWOFISH;
53 else if (n == GCRY_CIPHER_TWOFISH128)
54 return PWMD_CIPHER_TWOFISH128;
56 return PWMD_CIPHER_AES256;
59 int
60 cipher_to_gcrypt (int flags)
62 if (flags < 0)
63 return flags;
65 if (flags & PWMD_CIPHER_AES128)
66 return GCRY_CIPHER_AES128;
67 else if (flags & PWMD_CIPHER_AES192)
68 return GCRY_CIPHER_AES192;
69 else if (flags & PWMD_CIPHER_AES256)
70 return GCRY_CIPHER_AES256;
71 else if (flags & PWMD_CIPHER_SERPENT128)
72 return GCRY_CIPHER_SERPENT128;
73 else if (flags & PWMD_CIPHER_SERPENT192)
74 return GCRY_CIPHER_SERPENT192;
75 else if (flags & PWMD_CIPHER_SERPENT256)
76 return GCRY_CIPHER_SERPENT256;
77 else if (flags & PWMD_CIPHER_CAMELLIA128)
78 return GCRY_CIPHER_CAMELLIA128;
79 else if (flags & PWMD_CIPHER_CAMELLIA192)
80 return GCRY_CIPHER_CAMELLIA192;
81 else if (flags & PWMD_CIPHER_CAMELLIA256)
82 return GCRY_CIPHER_CAMELLIA256;
83 else if (flags & PWMD_CIPHER_BLOWFISH)
84 return GCRY_CIPHER_BLOWFISH;
85 else if (flags & PWMD_CIPHER_3DES)
86 return GCRY_CIPHER_3DES;
87 else if (flags & PWMD_CIPHER_CAST5)
88 return GCRY_CIPHER_CAST5;
89 else if (flags & PWMD_CIPHER_TWOFISH)
90 return GCRY_CIPHER_TWOFISH;
91 else if (flags & PWMD_CIPHER_TWOFISH128)
92 return GCRY_CIPHER_TWOFISH128;
94 return -1;
97 int
98 cipher_string_to_cipher (const char *str)
100 int flags = 0;
102 if (!strcasecmp (str, "aes128"))
103 flags = PWMD_CIPHER_AES128;
104 else if (!strcasecmp (str, "aes192"))
105 flags = PWMD_CIPHER_AES192;
106 else if (!strcasecmp (str, "aes256"))
107 flags = PWMD_CIPHER_AES256;
108 else if (!strcasecmp (str, "serpent128"))
109 flags = PWMD_CIPHER_SERPENT128;
110 else if (!strcasecmp (str, "serpent192"))
111 flags = PWMD_CIPHER_SERPENT192;
112 else if (!strcasecmp (str, "serpent256"))
113 flags = PWMD_CIPHER_SERPENT256;
114 else if (!strcasecmp (str, "camellia128"))
115 flags = PWMD_CIPHER_CAMELLIA128;
116 else if (!strcasecmp (str, "camellia192"))
117 flags = PWMD_CIPHER_CAMELLIA192;
118 else if (!strcasecmp (str, "camellia256"))
119 flags = PWMD_CIPHER_CAMELLIA256;
120 else if (!strcasecmp (str, "blowfish"))
121 flags = PWMD_CIPHER_BLOWFISH;
122 else if (!strcasecmp (str, "cast5"))
123 flags = PWMD_CIPHER_CAST5;
124 else if (!strcasecmp (str, "3des"))
125 flags = PWMD_CIPHER_3DES;
126 else if (!strcasecmp (str, "twofish256"))
127 flags = PWMD_CIPHER_TWOFISH;
128 else if (!strcasecmp (str, "twofish128"))
129 flags = PWMD_CIPHER_TWOFISH128;
130 else
131 return -1;
133 return flags;
137 cipher_string_to_gcrypt (const char *str)
139 return cipher_to_gcrypt (cipher_string_to_cipher (str));
142 uint64_t
143 set_cipher_flag (uint64_t flags, int algo)
145 // Clear all previous cipher bits but keep the flags.
146 flags &= ~((uint16_t) ((uint32_t) (flags & 0xFFFFFFFF)));
147 flags |= gcrypt_to_cipher (algo);
148 return flags;