diff options
| author | hrxi <hrrrxi@gmail.com> | 2023-11-24 20:53:50 +0100 |
|---|---|---|
| committer | Marvin W <git@larma.de> | 2026-10-23 16:58:30 +0200 |
| commit | 76a91fca2fc7dd5975e8ff0e6695f27c98539dba (patch) | |
| tree | 7829340be71fe60ef847247e578c4589571fecc5 /crypto-vala/src/cipher_converter.vala | |
| parent | b013d28182ce582f229185c23b6011f566f1f8cc (diff) | |
Add support for OpenSSL
Uses work by @hrxi in https://github.com/dino/dino/pull/1515
Co-Authored-By: hrxi <hrrrxi@gmail.com>
Diffstat (limited to 'crypto-vala/src/cipher_converter.vala')
| -rw-r--r-- | crypto-vala/src/cipher_converter.vala | 50 |
1 files changed, 29 insertions, 21 deletions
diff --git a/crypto-vala/src/cipher_converter.vala b/crypto-vala/src/cipher_converter.vala index 5fb1f548..49e7af71 100644 --- a/crypto-vala/src/cipher_converter.vala +++ b/crypto-vala/src/cipher_converter.vala @@ -1,18 +1,14 @@ using GLib; namespace Crypto { -public abstract class SymmetricCipherConverter : Converter, Object { - internal SymmetricCipher cipher; - internal size_t attached_taglen; - public abstract ConverterResult convert(uint8[] inbuf, uint8[] outbuf, ConverterFlags flags, out size_t bytes_read, out size_t bytes_written) throws IOError; +public class SymmetricCipherEncrypter : Converter, Object { + private SymmetricCipherEncryption cipher; + private size_t attached_taglen; - public uint8[] get_tag(size_t taglen) throws Error { - return cipher.get_tag(taglen); - } - - public void check_tag(uint8[] tag) throws Error { - cipher.check_tag(tag); + public SymmetricCipherEncrypter(owned SymmetricCipherEncryption cipher, size_t attached_taglen = 0) { + this.cipher = (owned) cipher; + this.attached_taglen = attached_taglen; } public void reset() { @@ -22,15 +18,12 @@ public abstract class SymmetricCipherConverter : Converter, Object { warning(@"$(e.domain) error while resetting cipher: $(e.message)"); } } -} -public class SymmetricCipherEncrypter : SymmetricCipherConverter { - public SymmetricCipherEncrypter(owned SymmetricCipher cipher, size_t attached_taglen = 0) { - this.cipher = (owned) cipher; - this.attached_taglen = attached_taglen; + public uint8[] get_tag(size_t taglen) throws Error { + return ((SymmetricCipherEncryption)cipher).get_tag(taglen); } - public override ConverterResult convert(uint8[] inbuf, uint8[] outbuf, ConverterFlags flags, out size_t bytes_read, out size_t bytes_written) throws IOError { + 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"); } @@ -39,7 +32,7 @@ public class SymmetricCipherEncrypter : SymmetricCipherConverter { } try { if (inbuf.length > 0) { - cipher.encrypt(outbuf, inbuf); + ((SymmetricCipherEncryption)cipher).encrypt(outbuf, inbuf); } bytes_read = inbuf.length; bytes_written = inbuf.length; @@ -60,13 +53,28 @@ public class SymmetricCipherEncrypter : SymmetricCipherConverter { } } -public class SymmetricCipherDecrypter : SymmetricCipherConverter { - public SymmetricCipherDecrypter(owned SymmetricCipher cipher, size_t attached_taglen = 0) { +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 override ConverterResult convert(uint8[] inbuf, uint8[] outbuf, ConverterFlags flags, out size_t bytes_read, out size_t bytes_written) throws IOError { + 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"); } @@ -78,7 +86,7 @@ public class SymmetricCipherDecrypter : SymmetricCipherConverter { try { inbuf.length -= (int) attached_taglen; if (inbuf.length > 0) { - cipher.decrypt(outbuf, inbuf); + ((SymmetricCipherDecryption)cipher).decrypt(outbuf, inbuf); } bytes_read = inbuf.length; bytes_written = inbuf.length; |
