Why do general encryption key generators also take the algorithm as input?

To my understanding the key generation logic only needs a source of randomness and the length of the key to be generated as input. However, we still provide the algorithm (names) like AES, DES, Blowfish etc. Is it mainly for validation purpose to ensure that the input key size is correct? So, if e.g. the algorithm is AES, the key length has to be 128, 256 or 192?

Or is there more to it, that I'm not aware of?

Is it mainly for validation purpose to ensure that the input key size is correct. So, for e.g. if algorithm is AES, the key length has to be 128, 256 or 192.

Yes, indicating a name to an algorithm may be used to check for valid generation parameters, including the key size that is generally present.

For Java - the principal language assumed in the initial question - it was commonly possible to assign invalid key sizes - at least before the key is generated. In that case the code may unfortunately only fail at a later stage when the key is used. In other words, it is not a given that the implementation adheres to the fail fast design principle. There have certainly be implementation differences between versions with regards to such tests.

It is important to check if the fact that sizes are validated is specified as such in the API. Many API's will however not be clear on that, which means that you will have to test or trust the implementation; it is probably better to check for valid key sizes yourself if they are made configurable.

Or is there more to it, that I'm not aware of?

You may not want to use a key that was generated for one specific algorithm to be used for another. Nowadays symmetric algorithms will often use fully random keys, but that wasn't always the case. For instance, DES - and by extension triple DES - will have parity bits that should be set. Those parity bits should not be set for AES keys as they would decrease the amount of possible key values and thus the key strength.

Another reason for indicating the algorithm is for keys to be stored in hardware modules or other key stores. For instance, in a HSM the key is not just stored as a byte array. It also includes the type of the key, so it can only be used by the algorithm it was intended for. For such HSM's it is required to indicate the object type during key generation. This is only an issue for API's that allow for hardware engines / providers (this question was initially Java specific) to be configured.

In principle you could also abuse the algorithm name to indicate other configuration parameters, such as the key store. Generally you would use different generation parameters for that though. Of course you would also want to make sure that other possible generation parameters besides the name / key size are correct for the specific algorithm that you selected - which is another use case for specifying a name up front.

• Generating keys for asymmetric encryption (and signature) is even more algorithm-specific, but in Java that's a different API Key**Pair**Generator – dave_thompson_085 Nov 23 '18 at 6:22