You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[webinterface] We need to change how we approach encryption #109
AES only works with very specific key-lengths (16 bytes for AES-128, 24 bytes for AES-192 and 32 bytes for AES-256). Right now our handle is 48 bytes long - 8 for the first eight chars of the filename (uft-8), 32 for the primordial hash, and 8 more for the salt (utf-8).
According to this SO answer CryptoJS should just give an empty string when decrypting as the key is larger than 32 bytes, but we seem to be passing the entire handle and it still seems to work fine for some reason so I'm not too sure what's going on there.
Another possible issue is the fact that we're not tracking or even consciously using nonces/iv. AES encryption (for the majority of chaining modes) is weakened immensely when you encrypt two different payloads with the same key and nonce. I'm not sure how CryptoJS generates those nonces or how it tracks them, but in any case, we shouldn't leave it up to a single library to do so. If we want other languages to be able to interact with files uploaded using the webinterface we need to go lower and start interacting with the base AES algo, not just use the shortcuts CryptoJS provides.
I have two possible solutions to the key issue and one for the nonce:
Key:
We directly generate a 32 bytes long key. Eg: 16 bytes long primordial hash + 16 bytes long salt.
We hash the entire handle using SHA256 (or perhaps SHA3-256) and use the 32 bytes long digest as the encryption key.
Nonce:
We need to set a manual, random nonce for every chunk. For AES-256 GCM the recommended length of this nonce is 16 bytes. After we set this nonce, we convert it to trytes using raw bytes -> trytes conversion to get a 32 trytes long string that we append to the end of the message in the iota tx.
I've successfully tested this using my python lib and they seem to work well. Encryption overhead is not nearly as bad as I thought it was going to be, and if we do manage to get bytes -> trytes conversion without encoding, we could easily fit 1kB+nonce in a single message.
A possible option to implement these changes could be dropping CryptoJS in favor of node-forge, which gives way better control over the parameters of encryption.
Considering the wider use and more recent updates, it might also add some extra stability over CryptoJS, which seems hardly maintained.
I think switching to node-forge is a good idea. I have a PR that changes encryption/decryption for the webnode, perhaps webinterface can re-use those same methods.
Also, with the brokernode's new way of encrypting the treasure chunks, I am now (on the webinterface) encountering the same issue I encountered on the webnode the other day. For some reason it is able to decrypt things it shouldn't be able to decrypt, including the treasure chunk. This explains a lot. Very recently, downloads started coming back scrambled sometimes, but only when there was a treasure chunk in the original upload. Without the treasure chunk being in the file, the downloads are fine.
We were planning to make some changes to the encryption on the webinterface anyway. I propose that when we make these changes, we also add a chunk prefix like the treasure prefix we added for the treasure chunks. Something simple like "Chunk: " so the webinterface has something it can check for. For chunks that don't decrypt with that string at the beginning, we don't include it in the re-assembled file.
When this issue was created I started playing around with a from-scratch port to node-forge, and ended up with an implementation that was compatible with @automyr's python lib.
Specifically:
We hash the entire handle using SHA256 (or perhaps SHA3-256) and use the 32 bytes long digest as the encryption key.
We need to set a manual, random nonce for every chunk. For AES-256 GCM the recommended length of this nonce is 16 bytes. After we set this nonce, we convert it to trytes using raw bytes -> trytes conversion to get a 32 trytes long string that we append to the end of the message in the iota tx.
Additionally proposed:
Add a trailing stopper-tryte (I vote for J) to prevent the parseMessage function from trimming too much off the payload, should it end in more than one 9-tryte.
Remove encoding conversions in datamap/hashchain. Currently every cycle treats the input as utf8, and outputs a hexstring (there's a branch in the oyster-streamable repo linked below, fixing this)
Possibly switch to SHA3-256 (would require js-sha3)
My node-forge implementation of most methods can be found here and here
Add a trailing stopper-tryte (I vote for J) to prevent the parseMessage function from trimming too much off the payload, should it end in more than one 9-tryte.
This may not be anything to worry about. parseMessage takes all the 9s off the end, then if the result string is an odd number of chars, it will add back an additional 9 so that it can successfully get through the asciiToTrytes method.
If we need to add a trailing stopper-tryte, we certainly can. But what if tryte string actually ends with J, or whatever other trailing stopper we use?
The difference would be that it cuts off all trailing nines and the trailing J tryte, removing ambiguity.
If we, for example, had a 6-tryte payload "SMFA99" that gets padded to 10 trytes, we get "SMFA999999". If we run parseMessage on it, we get back a payload of length 4, as two extra nines are chopped off.
With a stopper tryte, we'd get for example "SMFA99J999", then parseMessage would cut off the padding and the stopper tryte, leaving us with the payload we started with. If the payload was "SMFAJJ", it would get padded to "SMFAJJJ999" and trimmed back down to 6 trytes as well.
A little bonus is that with 1 byte always resulting in 2 trytes, all payloads should be even. Messages can be up to 2187 trytes long, so even if we use the whole length of it, there's an odd tryte left for the stopper.
I couldn't test it yet, but I am assuming this is the error that's causing #118
I couldn't test it yet, but I am assuming this is the error that's causing #118
I believe that this error is caused by the treasure chunk. When I put the broker in TEST_MODE_NO_TREASURE that error no longer happens. Basically our current decryption on the webinterface assumes that if the client did not encrypt the chunk (i.e. it's a treasure chunk), that attempting to decrypt it with the handle will result in "" or NULL. But what actually happens is that we get back a bunch of gibberish. This is the same reason why I had to add the treasure prefix for the treasure chunk, because the webnode was somehow able to decrypt everything. So we had to give it something to check for. I'll probably wind up adding some kind of prefix to the file chunks as well, which should fix #118.
I may be wrong, but I believe 9s in trytes are basically just nothing/padding. Not even whitespace.
If you go to the bottom of that page and type in a bunch of 9s, the ascii values that get produced are those little empty boxes. What I'm getting at is that I don't think there's ever a real payload that ends with two 9s. One 9 sometimes, but not two. If 9s represented anything important on their own, then every iota transaction would potentially be wrong, because signatureMessageFragments always get padded with a bunch of 9s. The iota API doesn't add any kind of stopper tryte, so how would people be able to distinguish between where the "real" 9s end and the padding 9s begin?
But, I could be wrong about that, and adding a trailing stopper tryte is a good idea anyway, so I'll add that in.
That sounds plausible as well. I haven't spent enough time on treasures to add more to that.
The boxes are NUL bytes according to the ASCII table. Since 9's are trytes with a value of [0, 0, 0] two of them are translated to 0x00.
In a normal message made up of readable ASCII characters these would never happen and could indeed be chopped off. Since we work with bytes that are in the full range of 0-255, we don't have that luxury. In the case of the proposed encryption, the last trytes are made up of the IV, which are 16 random bytes. So it's only a matter of time to get an IV that ends with 0x00, leading to two nines.
I see. Our current decryption method accepts ascii strings and we aren't currently using IVs, so I guess that's why our current implementation with parseMessage works. But you're right, with the new way, it wouldn't work anymore.
I think a pleasant side effect of these changes is we'll end up being able to fit the amount of data into each chunk that we want to, now that we'll be getting to a lower level with the encryption and using bytes instead of strings or word arrays.
I've thought about this again, and if there's a chance we will ever do the trimming in trits instead of trytes, we should probably use A as stopper. A being [1, 0, 0] in trits we could just trim off all 0's and the first 1 from the right. With [1, 0, 1] this would be less straightforward.
In trytes it doesn't matter, hence the previous suggestion, but I guess it doesn't hurt to be prepared.
Activity
nullpilot commentedon Apr 30, 2018
A possible option to implement these changes could be dropping CryptoJS in favor of node-forge, which gives way better control over the parameters of encryption.
Considering the wider use and more recent updates, it might also add some extra stability over CryptoJS, which seems hardly maintained.
rfornea commentedon May 13, 2018
I think switching to node-forge is a good idea. I have a PR that changes encryption/decryption for the webnode, perhaps webinterface can re-use those same methods.
Also, with the brokernode's new way of encrypting the treasure chunks, I am now (on the webinterface) encountering the same issue I encountered on the webnode the other day. For some reason it is able to decrypt things it shouldn't be able to decrypt, including the treasure chunk. This explains a lot. Very recently, downloads started coming back scrambled sometimes, but only when there was a treasure chunk in the original upload. Without the treasure chunk being in the file, the downloads are fine.
We were planning to make some changes to the encryption on the webinterface anyway. I propose that when we make these changes, we also add a chunk prefix like the treasure prefix we added for the treasure chunks. Something simple like "Chunk: " so the webinterface has something it can check for. For chunks that don't decrypt with that string at the beginning, we don't include it in the re-assembled file.
nullpilot commentedon May 15, 2018
When this issue was created I started playing around with a from-scratch port to node-forge, and ended up with an implementation that was compatible with @automyr's python lib.
Specifically:
Additionally proposed:
parseMessagefunction from trimming too much off the payload, should it end in more than one 9-tryte.My node-forge implementation of most methods can be found here and here
rfornea commentedon May 15, 2018
This may not be anything to worry about. parseMessage takes all the 9s off the end, then if the result string is an odd number of chars, it will add back an additional 9 so that it can successfully get through the asciiToTrytes method.
If we need to add a trailing stopper-tryte, we certainly can. But what if tryte string actually ends with J, or whatever other trailing stopper we use?
nullpilot commentedon May 15, 2018
The difference would be that it cuts off all trailing nines and the trailing J tryte, removing ambiguity.
If we, for example, had a 6-tryte payload
"SMFA99"that gets padded to 10 trytes, we get"SMFA999999". If we runparseMessageon it, we get back a payload of length 4, as two extra nines are chopped off.With a stopper tryte, we'd get for example
"SMFA99J999", thenparseMessagewould cut off the padding and the stopper tryte, leaving us with the payload we started with. If the payload was"SMFAJJ", it would get padded to"SMFAJJJ999"and trimmed back down to 6 trytes as well.A little bonus is that with 1 byte always resulting in 2 trytes, all payloads should be even. Messages can be up to 2187 trytes long, so even if we use the whole length of it, there's an odd tryte left for the stopper.
I couldn't test it yet, but I am assuming this is the error that's causing #118
rfornea commentedon May 15, 2018
Okay, I see what you're saying.
rfornea commentedon May 15, 2018
I believe that this error is caused by the treasure chunk. When I put the broker in TEST_MODE_NO_TREASURE that error no longer happens. Basically our current decryption on the webinterface assumes that if the client did not encrypt the chunk (i.e. it's a treasure chunk), that attempting to decrypt it with the handle will result in "" or NULL. But what actually happens is that we get back a bunch of gibberish. This is the same reason why I had to add the treasure prefix for the treasure chunk, because the webnode was somehow able to decrypt everything. So we had to give it something to check for. I'll probably wind up adding some kind of prefix to the file chunks as well, which should fix #118.
I may be wrong, but I believe 9s in trytes are basically just nothing/padding. Not even whitespace.
https://laurencetennant.com/iota-tools/
If you go to the bottom of that page and type in a bunch of 9s, the ascii values that get produced are those little empty boxes. What I'm getting at is that I don't think there's ever a real payload that ends with two 9s. One 9 sometimes, but not two. If 9s represented anything important on their own, then every iota transaction would potentially be wrong, because signatureMessageFragments always get padded with a bunch of 9s. The iota API doesn't add any kind of stopper tryte, so how would people be able to distinguish between where the "real" 9s end and the padding 9s begin?
But, I could be wrong about that, and adding a trailing stopper tryte is a good idea anyway, so I'll add that in.
nullpilot commentedon May 15, 2018
That sounds plausible as well. I haven't spent enough time on treasures to add more to that.
The boxes are NUL bytes according to the ASCII table. Since 9's are trytes with a value of [0, 0, 0] two of them are translated to 0x00.
In a normal message made up of readable ASCII characters these would never happen and could indeed be chopped off. Since we work with bytes that are in the full range of 0-255, we don't have that luxury. In the case of the proposed encryption, the last trytes are made up of the IV, which are 16 random bytes. So it's only a matter of time to get an IV that ends with 0x00, leading to two nines.
rfornea commentedon May 15, 2018
I see. Our current decryption method accepts ascii strings and we aren't currently using IVs, so I guess that's why our current implementation with parseMessage works. But you're right, with the new way, it wouldn't work anymore.
I think a pleasant side effect of these changes is we'll end up being able to fit the amount of data into each chunk that we want to, now that we'll be getting to a lower level with the encryption and using bytes instead of strings or word arrays.
nullpilot commentedon May 16, 2018
I've thought about this again, and if there's a chance we will ever do the trimming in trits instead of trytes, we should probably use A as stopper. A being
[1, 0, 0]in trits we could just trim off all 0's and the first 1 from the right. With[1, 0, 1]this would be less straightforward.In trytes it doesn't matter, hence the previous suggestion, but I guess it doesn't hurt to be prepared.