| 1 |
namespace Crypto { |
| 2 |
public class SymmetricCipher { |
| 3 |
private GCrypt.Cipher.Cipher cipher; |
| 4 |
|
| 5 |
public static bool supports(string algo_name) { |
| 6 |
GCrypt.Cipher.Algorithm algo; |
| 7 |
GCrypt.Cipher.Mode mode; |
| 8 |
GCrypt.Cipher.Flag flags; |
| 9 |
return parse(algo_name, out algo, out mode, out flags); |
| 10 |
} |
| 11 |
|
| 12 |
private static unowned string mode_to_string(GCrypt.Cipher.Mode mode) { |
| 13 |
switch (mode) { |
| 14 |
case GCrypt.Cipher.Mode.ECB: return "ECB"; |
| 15 |
case GCrypt.Cipher.Mode.CFB: return "CFB"; |
| 16 |
case GCrypt.Cipher.Mode.CBC: return "CBC"; |
| 17 |
case GCrypt.Cipher.Mode.STREAM: return "STREAM"; |
| 18 |
case GCrypt.Cipher.Mode.OFB: return "OFB"; |
| 19 |
case GCrypt.Cipher.Mode.CTR: return "CTR"; |
| 20 |
case GCrypt.Cipher.Mode.AESWRAP: return "AESWRAP"; |
| 21 |
case GCrypt.Cipher.Mode.GCM: return "GCM"; |
| 22 |
case GCrypt.Cipher.Mode.POLY1305: return "POLY1305"; |
| 23 |
case GCrypt.Cipher.Mode.OCB: return "OCB"; |
| 24 |
case GCrypt.Cipher.Mode.CFB8: return "CFB8"; |
| 25 |
// case GCrypt.Cipher.Mode.XTS: return "XTS"; // Not supported in gcrypt < 1.8 |
| 26 |
} |
| 27 |
return "NONE"; |
| 28 |
} |
| 29 |
|
| 30 |
private static GCrypt.Cipher.Mode mode_from_string(string name) { |
| 31 |
switch (name) { |
| 32 |
case "ECB": return GCrypt.Cipher.Mode.ECB; |
| 33 |
case "CFB": return GCrypt.Cipher.Mode.CFB; |
| 34 |
case "CBC": return GCrypt.Cipher.Mode.CBC; |
| 35 |
case "STREAM": return GCrypt.Cipher.Mode.STREAM; |
| 36 |
case "OFB": return GCrypt.Cipher.Mode.OFB; |
| 37 |
case "CTR": return GCrypt.Cipher.Mode.CTR; |
| 38 |
case "AESWRAP": return GCrypt.Cipher.Mode.AESWRAP; |
| 39 |
case "GCM": return GCrypt.Cipher.Mode.GCM; |
| 40 |
case "POLY1305": return GCrypt.Cipher.Mode.POLY1305; |
| 41 |
case "OCB": return GCrypt.Cipher.Mode.OCB; |
| 42 |
case "CFB8": return GCrypt.Cipher.Mode.CFB8; |
| 43 |
// case "XTS": return GCrypt.Cipher.Mode.XTS; // Not supported in gcrypt < 1.8 |
| 44 |
} |
| 45 |
return GCrypt.Cipher.Mode.NONE; |
| 46 |
} |
| 47 |
|
| 48 |
private static string flags_to_string(GCrypt.Cipher.Flag flags) { |
| 49 |
string? s = null; |
| 50 |
if ((GCrypt.Cipher.Flag.CBC_MAC & flags) != 0) s = (s == null ? "" : @"$s-") + "MAC"; |
| 51 |
if ((GCrypt.Cipher.Flag.CBC_CTS & flags) != 0) s = (s == null ? "" : @"$s-") + "CTS"; |
| 52 |
if ((GCrypt.Cipher.Flag.ENABLE_SYNC & flags) != 0) s = (s == null ? "" : @"$s-") + "SYNC"; |
| 53 |
if ((GCrypt.Cipher.Flag.SECURE & flags) != 0) s = (s == null ? "" : @"$s-") + "SECURE"; |
| 54 |
return s ?? "NONE"; |
| 55 |
} |
| 56 |
|
| 57 |
private static GCrypt.Cipher.Flag flag_from_string(string flag_name) { |
| 58 |
if (flag_name == "SECURE") return GCrypt.Cipher.Flag.SECURE; |
| 59 |
if (flag_name == "SYNC") return GCrypt.Cipher.Flag.ENABLE_SYNC; |
| 60 |
if (flag_name == "CTS") return GCrypt.Cipher.Flag.CBC_CTS; |
| 61 |
if (flag_name == "MAC") return GCrypt.Cipher.Flag.CBC_MAC; |
| 62 |
return 0; |
| 63 |
} |
| 64 |
|
| 65 |
private static GCrypt.Cipher.Flag flags_from_string(string flag_names) { |
| 66 |
GCrypt.Cipher.Flag flags = 0; |
| 67 |
foreach(string flag in flag_names.split("-")) { |
| 68 |
flags |= flag_from_string(flag); |
| 69 |
} |
| 70 |
return flags; |
| 71 |
} |
| 72 |
|
| 73 |
private static bool parse(string algo_name, out GCrypt.Cipher.Algorithm algo, out GCrypt.Cipher.Mode mode, out GCrypt.Cipher.Flag flags) { |
| 74 |
algo = GCrypt.Cipher.Algorithm.NONE; |
| 75 |
mode = GCrypt.Cipher.Mode.NONE; |
| 76 |
flags = 0; |
| 77 |
string[] algo_parts = algo_name.split("-", 3); |
| 78 |
|
| 79 |
algo = GCrypt.Cipher.Algorithm.from_string(algo_parts[0]); |
| 80 |
if (algo_parts.length >= 2) { |
| 81 |
mode = mode_from_string(algo_parts[1]); |
| 82 |
} |
| 83 |
if (algo_parts.length == 3) { |
| 84 |
flags |= flags_from_string(algo_parts[2]); |
| 85 |
} |
| 86 |
return to_algo_name(algo, mode, flags) == algo_name; |
| 87 |
} |
| 88 |
|
| 89 |
private static string to_algo_name(GCrypt.Cipher.Algorithm algo = GCrypt.Cipher.Algorithm.NONE, GCrypt.Cipher.Mode mode = GCrypt.Cipher.Mode.NONE, GCrypt.Cipher.Flag flags = 0) { |
| 90 |
if (flags != 0) { |
| 91 |
return @"$algo-$(mode_to_string(mode))-$(flags_to_string(flags))"; |
| 92 |
} else if (mode != GCrypt.Cipher.Mode.NONE) { |
| 93 |
return @"$algo-$(mode_to_string(mode))"; |
| 94 |
} else { |
| 95 |
return algo.to_string(); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
public SymmetricCipher(string algo_name) throws Error { |
| 100 |
GCrypt.Cipher.Algorithm algo; |
| 101 |
GCrypt.Cipher.Mode mode; |
| 102 |
GCrypt.Cipher.Flag flags; |
| 103 |
if (parse(algo_name, out algo, out mode, out flags)) { |
| 104 |
this.gcrypt(algo, mode, flags); |
| 105 |
} else { |
| 106 |
throw new Error.ILLEGAL_ARGUMENTS(@"The algorithm $algo_name is not supported"); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
private SymmetricCipher.gcrypt(GCrypt.Cipher.Algorithm algo, GCrypt.Cipher.Mode mode, GCrypt.Cipher.Flag flags) throws Error { |
| 111 |
may_throw_gcrypt_error(GCrypt.Cipher.Cipher.open(out this.cipher, algo, mode, flags)); |
| 112 |
} |
| 113 |
|
| 114 |
public void set_key(uint8[] key) throws Error { |
| 115 |
may_throw_gcrypt_error(cipher.set_key(key)); |
| 116 |
} |
| 117 |
|
| 118 |
public void set_iv(uint8[] iv) throws Error { |
| 119 |
may_throw_gcrypt_error(cipher.set_iv(iv)); |
| 120 |
} |
| 121 |
|
| 122 |
public void set_counter_vector(uint8[] ctr) throws Error { |
| 123 |
may_throw_gcrypt_error(cipher.set_counter_vector(ctr)); |
| 124 |
} |
| 125 |
|
| 126 |
public void reset() throws Error { |
| 127 |
may_throw_gcrypt_error(cipher.reset()); |
| 128 |
} |
| 129 |
|
| 130 |
public uint8[] get_tag(size_t taglen) throws Error { |
| 131 |
uint8[] tag = new uint8[taglen]; |
| 132 |
may_throw_gcrypt_error(cipher.get_tag(tag)); |
| 133 |
return tag; |
| 134 |
} |
| 135 |
|
| 136 |
public void check_tag(uint8[] tag) throws Error { |
| 137 |
may_throw_gcrypt_error(cipher.check_tag(tag)); |
| 138 |
} |
| 139 |
|
| 140 |
public void encrypt(uint8[] output, uint8[] input) throws Error { |
| 141 |
may_throw_gcrypt_error(cipher.encrypt(output, input)); |
| 142 |
} |
| 143 |
|
| 144 |
public void decrypt(uint8[] output, uint8[] input) throws Error { |
| 145 |
may_throw_gcrypt_error(cipher.decrypt(output, input)); |
| 146 |
} |
| 147 |
|
| 148 |
public void sync() throws Error { |
| 149 |
may_throw_gcrypt_error(cipher.sync()); |
| 150 |
} |
| 151 |
} |
| 152 |
} |