Debian Codegraph @

crypto-vala/src/cipher_converter.vala

raw
1 using GLib;
2
3 namespace Crypto {
4 public abstract class SymmetricCipherConverter : Converter, Object {
5 internal SymmetricCipher cipher;
6 internal size_t attached_taglen;
7
8 public abstract ConverterResult convert(uint8[] inbuf, uint8[] outbuf, ConverterFlags flags, out size_t bytes_read, out size_t bytes_written) throws IOError;
9
10 public uint8[] get_tag(size_t taglen) throws Error {
11 return cipher.get_tag(taglen);
12 }
13
14 public void check_tag(uint8[] tag) throws Error {
15 cipher.check_tag(tag);
16 }
17
18 public void reset() {
19 try {
20 cipher.reset();
21 } catch (Crypto.Error e) {
22 warning(@"$(e.domain) error while resetting cipher: $(e.message)");
23 }
24 }
25 }
26
27 public class SymmetricCipherEncrypter : SymmetricCipherConverter {
28 public SymmetricCipherEncrypter(owned SymmetricCipher cipher, size_t attached_taglen = 0) {
29 this.cipher = (owned) cipher;
30 this.attached_taglen = attached_taglen;
31 }
32
33 public override ConverterResult convert(uint8[] inbuf, uint8[] outbuf, ConverterFlags flags, out size_t bytes_read, out size_t bytes_written) throws IOError {
34 if (inbuf.length > outbuf.length) {
35 throw new IOError.NO_SPACE("CipherConverter needs at least the size of input as output space");
36 }
37 if ((flags & ConverterFlags.INPUT_AT_END) != 0 && inbuf.length + attached_taglen > outbuf.length) {
38 throw new IOError.NO_SPACE("CipherConverter needs additional output space to attach tag");
39 }
40 try {
41 if (inbuf.length > 0) {
42 cipher.encrypt(outbuf, inbuf);
43 }
44 bytes_read = inbuf.length;
45 bytes_written = inbuf.length;
46 if ((flags & ConverterFlags.INPUT_AT_END) != 0) {
47 if (attached_taglen > 0) {
48 Memory.copy((uint8*)outbuf + inbuf.length, get_tag(attached_taglen), attached_taglen);
49 bytes_written = inbuf.length + attached_taglen;
50 }
51 return ConverterResult.FINISHED;
52 }
53 if ((flags & ConverterFlags.FLUSH) != 0) {
54 return ConverterResult.FLUSHED;
55 }
56 return ConverterResult.CONVERTED;
57 } catch (Crypto.Error e) {
58 throw new IOError.FAILED(@"$(e.domain) error while encrypting: $(e.message)");
59 }
60 }
61 }
62
63 public class SymmetricCipherDecrypter : SymmetricCipherConverter {
64 public SymmetricCipherDecrypter(owned SymmetricCipher cipher, size_t attached_taglen = 0) {
65 this.cipher = (owned) cipher;
66 this.attached_taglen = attached_taglen;
67 }
68
69 public override ConverterResult convert(uint8[] inbuf, uint8[] outbuf, ConverterFlags flags, out size_t bytes_read, out size_t bytes_written) throws IOError {
70 if (inbuf.length > outbuf.length + attached_taglen) {
71 throw new IOError.NO_SPACE("CipherConverter needs at least the size of input as output space");
72 }
73 if ((flags & ConverterFlags.INPUT_AT_END) != 0 && inbuf.length < attached_taglen) {
74 throw new IOError.PARTIAL_INPUT("CipherConverter needs additional input to read tag");
75 } else if ((flags & ConverterFlags.INPUT_AT_END) == 0 && inbuf.length < attached_taglen + 1) {
76 throw new IOError.PARTIAL_INPUT("CipherConverter needs additional input to make sure to not accidentally read tag");
77 }
78 try {
79 inbuf.length -= (int) attached_taglen;
80 if (inbuf.length > 0) {
81 cipher.decrypt(outbuf, inbuf);
82 }
83 bytes_read = inbuf.length;
84 bytes_written = inbuf.length;
85 inbuf.length += (int) attached_taglen;
86 if ((flags & ConverterFlags.INPUT_AT_END) != 0) {
87 if (attached_taglen > 0) {
88 check_tag(inbuf[(inbuf.length - attached_taglen):inbuf.length]);
89 bytes_read = inbuf.length;
90 }
91 return ConverterResult.FINISHED;
92 }
93 if ((flags & ConverterFlags.FLUSH) != 0) {
94 return ConverterResult.FLUSHED;
95 }
96 return ConverterResult.CONVERTED;
97 } catch (Crypto.Error e) {
98 throw new IOError.FAILED(@"$(e.domain) error while decrypting: $(e.message)");
99 }
100 }
101 }
102 }