summaryrefslogtreecommitdiff
path: root/lib/DeWeave/Crypto.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/DeWeave/Crypto.pm')
-rw-r--r--lib/DeWeave/Crypto.pm38
1 files changed, 30 insertions, 8 deletions
diff --git a/lib/DeWeave/Crypto.pm b/lib/DeWeave/Crypto.pm
index 618e095..e21e141 100644
--- a/lib/DeWeave/Crypto.pm
+++ b/lib/DeWeave/Crypto.pm
@@ -8,6 +8,8 @@ use Try::Tiny;
use Digest::SHA ();
use MIME::Base32 'RFC';
use Crypt::CBC;
+use MIME::Base64 ();
+use Data::Dump 'pp';
has storage => (
isa => 'DeWeave::Storage',
@@ -25,8 +27,10 @@ sub _byte_sync_key {
my ($self) = @_;
my $key = $self->sync_key;
- $key =~ y{89}{lo};
- return MIME::Base32::decode($key);
+ $key =~ y{89}{LO};
+ $key =~ s{-}{}g;
+ $key = MIME::Base32::decode($key);
+ return substr($key,0,16);
}
has _hmac_input => (
@@ -46,6 +50,9 @@ sub _build__encryption_key {
my $secret = $self->_hmac_input
. $self->storage->username . "\x01";
+
+warn "enc key: ",pp($secret,$self->_byte_sync_key,length($self->_byte_sync_key));
+
return Digest::SHA::hmac_sha256($secret, $self->_byte_sync_key);
}
@@ -80,8 +87,21 @@ sub _build__keys {
my $j = JSON::Any->new;
- my $keys_payload = $self->storage->get_item('crypto/keys');
- my $struct = $j->decode($keys_payload);
+ my $keys_raw = $self->storage->get_item('storage/crypto/keys');
+
+ my $keys_struct = $j->decode($keys_raw);
+
+ my $payload = $j->decode($keys_struct->{payload});
+
+warn "payload: ", pp $payload;
+warn "key: ",pp $self->_encryption_key, length($self->_encryption_key);
+
+ my $struct = $j->decode($self->decrypt({
+ %$payload,
+ key => $self->_encryption_key,
+ }));
+
+warn "keys: ",pp $struct;
my $keys = {
default => $struct->{default},
@@ -105,16 +125,18 @@ sub keys_for_collection {
sub decrypt {
my ($self,$args) = @_;
- my $iv = $args->{IV};
+ my $iv = MIME::Base64::decode($args->{IV});
my $hmac = $args->{hmac};
- my $ct = $args->{ciphertext};
+ my $ct = MIME::Base64::decode($args->{ciphertext});
+ my $key = $args->{key} || $self->keys_for_collection('default');
+
+warn "Crypto ", pp($iv,$ct, length($ct), $key);
my $cipher = Crypt::CBC->new(
- -key => $self->_encryption_key,
+ -key => $key,
-cipher => 'Crypt::OpenSSL::AES',
-iv => $iv,
-header => 'none',
- -padding => 'null',
-literal_key => 1,
);