From b738cd1e0a739f4ddedc998e6ded63669846ac18 Mon Sep 17 00:00:00 2001 From: Didactic Drunk <1479616+didactic-drunk@users.noreply.github.com> Date: Mon, 14 Jun 2021 16:54:29 -0700 Subject: [PATCH] Fix Secret.bytesize delegates --- spec/not_spec.cr | 5 +++++ src/crypto-secret/not.cr | 26 ++++++++++++++++---------- src/crypto-secret/secret.cr | 2 +- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/spec/not_spec.cr b/spec/not_spec.cr index 9144077..70cc569 100644 --- a/spec/not_spec.cr +++ b/spec/not_spec.cr @@ -20,6 +20,11 @@ describe Crypto::Secret::Not do end end + it "bytesize" do + secret = Crypto::Secret::Not.new 5 + secret.bytesize.should eq 5 + end + it "doesn't leak key material" do secret = Crypto::Secret::Not.new 5 secret.to_s.should match /\(\*\*\*SECRET\*\*\*\)$/ diff --git a/src/crypto-secret/not.cr b/src/crypto-secret/not.cr index 185f278..554e607 100644 --- a/src/crypto-secret/not.cr +++ b/src/crypto-secret/not.cr @@ -5,16 +5,22 @@ require "./stateless" # Not locked in memory # Not access protected # No guard pages -struct Crypto::Secret::Not - include Crypto::Secret::Stateless +# Doesn't wipe +module Crypto::Secret + struct Not + include Stateless - def self.new(size) - new Bytes.new(size) + def self.new(size) + new Bytes.new(size) + end + + def initialize(@bytes : Bytes) + end + + delegate_to_slice @bytes + delegate_to_bytesize @bytes.bytesize + + def wipe + end end - - def initialize(@bytes : Bytes) - end - - delegate_to_slice @bytes - delegate_to_bytesize @bytes end diff --git a/src/crypto-secret/secret.cr b/src/crypto-secret/secret.cr index d2b122b..fc48a23 100644 --- a/src/crypto-secret/secret.cr +++ b/src/crypto-secret/secret.cr @@ -90,7 +90,7 @@ module Crypto::Secret macro delegate_to_bytesize(to object) def bytesize : Int32 - {{object.id}}.bytesize + {{object.id}} end end