henrinormak / Heimdall

Heimdall is a wrapper around the Security framework for simple encryption/decryption operations.
MIT License
401 stars 68 forks source link

Possible octet encoding bug? #64

Closed jesperny closed 7 years ago

jesperny commented 7 years ago

Hi I found this library when looking for ways to implement key pair and pem file generation on iOS. Unfortunately my project is still objective-c so I reimplemented the functions I needed. However, there seems to be an issue in the encodedOctets function line 779. If I understand correctly this should calculate the number of bytes needed to store the number. The current implementation does not seem to do that and will be incorrect for values larger than or equal to 512 (512 will require 3 bytes). I implemented it as i = log2(value) / 8 + 1

Hope it is of some use and my apologies if I have completely misunderstood something.

henrinormak commented 7 years ago

I think you are right, the current implementation will end up wasting space, the data will just be longer than needed, but the value itself should still be read out correctly (i.e there should be no difference between whether 1024 is encoded as 10000101 00000000 00000000 00000000 00000100 00000000 or as 10000010 00000100 00000000.

Irregardless, I will push a fix and an updated release to use your suggested method for calculating the length.

henrinormak commented 7 years ago

Should be fixed in 1.1.5