22

There are many published reports that on older versions of Android, we need to provide our own SecureRandom-based initialization vector (IV), as the default ones are not random:

Conversely, as of API Level 23, if you try to provide your own IV, you also have to call setRandomizedEncryptionRequired(false) on the KeyGenParameterSpec.Builder, as otherwise you get a "Caller-provided IV not permitted when encrypting" exception.

Presumably, somewhere along the line, Android went from "awful" to "good enough" in terms of IV generation.

What is the cutoff, below which we should generate our own IV versus use Android's generated IV?

THN
  • 3,351
  • 3
  • 26
  • 40
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • **Always** of course. What *exactly* would be the point of **not** doing it ? – Jon Goodwin Feb 10 '18 at 04:20
  • @JonGoodwin: Well, as I wrote, you need to specifically tell `KeyGenParameterSpec.Builder` that you are supplying the random IV. If you don't, you crash, because Google wants `KeyGenParameterSpec.Builder` to generate the random IV. So, *Google* thinks that Google is doing a fine enough job here, at least as of API Level 23. You apparently disagree with Google, and that's perfectly fine. *I* disagree with Google a lot. I'm just seeing if there is something more to go on than that. – CommonsWare Feb 10 '18 at 11:43

1 Answers1

2

From a security point of view, you should always provide your own IV, because you would have total control of its randomization quality, and eliminate one potential security weak point.

Regarding the exception, in your perspective, the IV is random and good. But in Android's perspective, your provided IV is fixed and thus not good, the API does not know if it is properly randomly generated or not. So the exception "Caller-provided IV not permitted when encrypting" is just a general warning trying to warn developers against using bad IV and encourage them to use the built-in IV.

However, note that the built-in IV is just one method to build the IV. As you can see, no one assures its quality as of API Level 23, so the best practice is still to assure your own IV quality yourself.

THN
  • 3,351
  • 3
  • 26
  • 40