aboutsummaryrefslogtreecommitdiff
path: root/crypto-vala/src/cipher_converter.vala
blob: 49e7af7172b1238ebd8c5bb061351873763692ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using GLib;

namespace Crypto {

public class SymmetricCipherEncrypter : Converter, Object {
    private SymmetricCipherEncryption cipher;
    private size_t attached_taglen;

    public SymmetricCipherEncrypter(owned SymmetricCipherEncryption cipher, size_t attached_taglen = 0) {
        this.cipher = (owned) cipher;
        this.attached_taglen = attached_taglen;
    }

    public void reset() {
        try {
            cipher.reset();
        } catch (Crypto.Error e) {
            warning(@"$(e.domain) error while resetting cipher: $(e.message)");
        }
    }

    public uint8[] get_tag(size_t taglen) throws Error {
        return ((SymmetricCipherEncryption)cipher).get_tag(taglen);
    }

    public ConverterResult convert(uint8[] inbuf, uint8[] outbuf, ConverterFlags flags, out size_t bytes_read, out size_t bytes_written) throws IOError {
        if (inbuf.length > outbuf.length) {
            throw new IOError.NO_SPACE("CipherConverter needs at least the size of input as output space");
        }
        if ((flags & ConverterFlags.INPUT_AT_END) != 0 && inbuf.length + attached_taglen > outbuf.length) {
            throw new IOError.NO_SPACE("CipherConverter needs additional output space to attach tag");
        }
        try {
            if (inbuf.length > 0) {
                ((SymmetricCipherEncryption)cipher).encrypt(outbuf, inbuf);
            }
            bytes_read = inbuf.length;
            bytes_written = inbuf.length;
            if ((flags & ConverterFlags.INPUT_AT_END) != 0) {
                if (attached_taglen > 0) {
                    Memory.copy((uint8*)outbuf + inbuf.length, get_tag(attached_taglen), attached_taglen);
                    bytes_written = inbuf.length + attached_taglen;
                }
                return ConverterResult.FINISHED;
            }
            if ((flags & ConverterFlags.FLUSH) != 0) {
                return ConverterResult.FLUSHED;
            }
            return ConverterResult.CONVERTED;
        } catch (Crypto.Error e) {
            throw new IOError.FAILED(@"$(e.domain) error while encrypting: $(e.message)");
        }
    }
}

public class SymmetricCipherDecrypter : Converter, Object {
    private SymmetricCipherDecryption cipher;
    private size_t attached_taglen;

    public SymmetricCipherDecrypter(owned SymmetricCipherDecryption cipher, size_t attached_taglen = 0) {
        this.cipher = (owned) cipher;
        this.attached_taglen = attached_taglen;
    }

    public void reset() {
        try {
            cipher.reset();
        } catch (Crypto.Error e) {
            warning(@"$(e.domain) error while resetting cipher: $(e.message)");
        }
    }

    public void check_tag(uint8[] tag) throws Error {
        ((SymmetricCipherDecryption)cipher).check_tag(tag);
    }

    public ConverterResult convert(uint8[] inbuf, uint8[] outbuf, ConverterFlags flags, out size_t bytes_read, out size_t bytes_written) throws IOError {
        if (inbuf.length > outbuf.length + attached_taglen) {
            throw new IOError.NO_SPACE("CipherConverter needs at least the size of input as output space");
        }
        if ((flags & ConverterFlags.INPUT_AT_END) != 0 && inbuf.length < attached_taglen) {
            throw new IOError.PARTIAL_INPUT("CipherConverter needs additional input to read tag");
        } else if ((flags & ConverterFlags.INPUT_AT_END) == 0 && inbuf.length < attached_taglen + 1) {
            throw new IOError.PARTIAL_INPUT("CipherConverter needs additional input to make sure to not accidentally read tag");
        }
        try {
            inbuf.length -= (int) attached_taglen;
            if (inbuf.length > 0) {
                ((SymmetricCipherDecryption)cipher).decrypt(outbuf, inbuf);
            }
            bytes_read = inbuf.length;
            bytes_written = inbuf.length;
            inbuf.length += (int) attached_taglen;
            if ((flags & ConverterFlags.INPUT_AT_END) != 0) {
                if (attached_taglen > 0) {
                    check_tag(inbuf[(inbuf.length - attached_taglen):inbuf.length]);
                    bytes_read = inbuf.length;
                }
                return ConverterResult.FINISHED;
            }
            if ((flags & ConverterFlags.FLUSH) != 0) {
                return ConverterResult.FLUSHED;
            }
            return ConverterResult.CONVERTED;
        } catch (Crypto.Error e) {
            throw new IOError.FAILED(@"$(e.domain) error while decrypting: $(e.message)");
        }
    }
}
}