Skip to content

Commit

Permalink
add specs for empty message
Browse files Browse the repository at this point in the history
  • Loading branch information
Kingsley Hendrickse committed Apr 27, 2019
1 parent 85d6c52 commit aabd388
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ You can create a keypair which returns the public and private keys. The private

Using the keypair you can sign a message and then verify it

## Developement

You must generate the encryption.o before the library is usable and before the specs will run.

`cd encryption && make`

## Contributing

1. Fork it (<https://github.com/sushichain/crystal-ecdsa/fork>)
Expand Down
26 changes: 13 additions & 13 deletions spec/crystal-ecdsa_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ describe ECCrypto do
end
end

# it "should raise an error on empty message" do
# expect_raises(Exception, "Message must not be empty") do
# receiver_public_key = ECCrypto.create_key_pair[:hex_public_key]
# ECCrypto.encrypt(receiver_public_key, "")
# end
# end
it "should raise an error on empty message" do
expect_raises(Exception, "Message must not be empty") do
receiver_public_key = ECCrypto.create_key_pair[:hex_public_key]
ECCrypto.encrypt(receiver_public_key, "")
end
end
end

describe "Decrypt errors" do
Expand All @@ -115,13 +115,13 @@ describe ECCrypto do
end
end

# it "should raise an error on empty decrypt message" do
# receiver_private_key = ECCrypto.create_key_pair[:hex_private_key]
#
# expect_raises(Exception, "Message must not be empty") do
# ECCrypto.decrypt(receiver_private_key, "")
# end
# end
it "should raise an error on empty decrypt message" do
receiver_private_key = ECCrypto.create_key_pair[:hex_private_key]

expect_raises(Exception, "Encrypted message must not be empty") do
ECCrypto.decrypt(receiver_private_key, "")
end
end

it "should raise an error when encrypted message was not encrypted by this library" do
receiver_key_pair = ECCrypto.create_key_pair
Expand Down
6 changes: 4 additions & 2 deletions src/crystal-ecdsa.cr
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ module ECCrypto
# set the group type from NID
eccgrp_id = LibECCrypto.OBJ_txt2nid("secp256k1")
raise "Error could not set EC group" unless eccgrp_id != 0
raise "Message must not be empty" unless message.size > 0

# use the crypto library to encrypt the message using the receiver public key and get the ephemeral public key, the init vector and the tag
# (we add one to message size to encrypt the null at the end of the string)
Expand All @@ -229,8 +230,9 @@ module ECCrypto

def self.decrypt(hex_receiver_private_key : String, encrypted_message : String) : String
dummy : UInt8 = 0
decrypted_message : UInt8* = pointerof(dummy)
decrypted_message : UInt8* = pointerof(dummy)

raise "Encrypted message must not be empty" unless encrypted_message.size > 0
# pull the componente of the encrypted message apart
chunks = encrypted_message.split("fx")
raise "Message not encrypted by ECCrypto.encrypt" if chunks.size != 4
Expand All @@ -251,7 +253,7 @@ module ECCrypto
status = LibECCrypto.decrypt_message(eccgrp_id, hex_receiver_private_key,
epubk, epubk_len, iv, iv_len, tag, tag_len, ciphertext, ciphertext_len,
pointerof(decrypted_message))

raise String.new(decrypted_message) unless status == 0

return String.new(decrypted_message)
Expand Down

0 comments on commit aabd388

Please sign in to comment.