1 /*
2 * crypto_mode.c --- convert between encryption modes and strings
3 *
4 * Copyright (C) 1999 Theodore Ts'o <tytso@mit.edu>
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
9 * %End-Header%
10 */
11
12 #include "config.h"
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <strings.h>
17 #include <ctype.h>
18 #include <errno.h>
19
20 #include "e2p.h"
21
22 struct mode {
23 int num;
24 const char *string;
25 };
26
27 static struct mode mode_list[] = {
28 { EXT4_ENCRYPTION_MODE_INVALID, "Invalid"},
29 { EXT4_ENCRYPTION_MODE_AES_256_XTS, "AES-256-XTS"},
30 { EXT4_ENCRYPTION_MODE_AES_256_GCM, "AES-256-GCM"},
31 { EXT4_ENCRYPTION_MODE_AES_256_CBC, "AES-256-CBC"},
32 { 0, 0 },
33 };
34
e2p_encmode2string(int num)35 const char *e2p_encmode2string(int num)
36 {
37 struct mode *p;
38 static char buf[20];
39
40 for (p = mode_list; p->string; p++) {
41 if (num == p->num)
42 return p->string;
43 }
44 sprintf(buf, "ENC_MODE_%d", num);
45 return buf;
46 }
47
48 /*
49 * Returns the hash algorithm, or -1 on error
50 */
e2p_string2encmode(char * string)51 int e2p_string2encmode(char *string)
52 {
53 struct mode *p;
54 char *eptr;
55 int num;
56
57 for (p = mode_list; p->string; p++) {
58 if (!strcasecmp(string, p->string)) {
59 return p->num;
60 }
61 }
62 if (strncasecmp(string, "ENC_MODE_", 9))
63 return -1;
64
65 if (string[9] == 0)
66 return -1;
67 num = strtol(string+9, &eptr, 10);
68 if (num > 255 || num < 0)
69 return -1;
70 if (*eptr)
71 return -1;
72 return num;
73 }
74
75