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