0

How does one write a MySql query to do a search on (for example) a username, when the username has been AES encrypted with a key and an IV?

I can't find examples of how to write the query.

Of course one has to decrypt the entire dataset and then filter it. Doesn't this cause memory problems?

1 Answer 1

1

This is not advisable. Having to decrypt the whole data-set will be slow and memory expensive. You will want to find a reasonable way to encrypt the data without using a separate IV for each username, then encrypt the username you want to find and search for the cipher-text. This will allow you to use indexes and search for only the one username you are interested in.

Alternatively you may consider using hashing in addition to the sorting encrypted username. HMAC the username with a password only known to the server and store in the DB. Then when you want to find a user, hash his username as well and find using the hash. Admittedly this allows some information leakage, for example attacker will be able to see users with the same usernames (you rpobably don't want these anyway). However he should not be able to use any hashtables or even guessing without knowing the HMAC key.

PS: A way you may want to deal with the IV may be: During registration (or username change) choose IV common for the entire server. Encrypt using this IV. If the first block (16 bytes for AES) already are in the database, increment the IV by one and try again. Store the highest increment. During search, try all possible IVs (from the basic one up to the highest increment used).

8
  • Ah.. got you.. thanks. I don't really need hashing here, it's low level plus several fields are affected not just username. In fact username was a terrible example - it's names and emails for data at rest encryption - but needs to be AES256 - that seems to require the IV ? I'd be happy with AES128 but job insists on 256 Commented Apr 25, 2018 at 15:49
  • @anoldermark I believe AES-256 also has 128 bit blocksize. But of course, it is not important. Only compare 32 bytes instead of 16, if you use 256 bit blocks. That is the only importance of that. Of course if the data you search for is not unique, or at least rare, like Country, this will not work well either. Both of these suggestions assume that data will rarely repeat. Commented Apr 25, 2018 at 15:57
  • Right.. hmm I see about Country.. wouldn't using an IV make 'country' secure and still searchable? Whoops, going off topic here.. Commented Apr 25, 2018 at 16:22
  • @anoldermark Well, using it as I described, yes. But it would make the searches very slow again. Possibly to the point where it may be slower than decrypting everything. Commented Apr 25, 2018 at 16:23
  • Yep, the other thing of course is showing lists of users to administrators where you need to decrypt everything to get the alpha order - I know there's "security lossy" workarounds by having keys that relate to initial characters but .. tin opener for can of uncomfortable worms. Commented Apr 25, 2018 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.