diff --git a/sys/crypto/twofish/twofish.c b/sys/crypto/twofish/twofish.c index 0000000..1111111 100644 --- a/sys/crypto/twofish/twofish.c +++ b/sys/crypto/twofish/twofish.c @@ -430,7 +430,21 @@ l_key = ctx->l_key; s_key = ctx->s_key; - ctx->k_len = key_len_bits / 64; /* 2, 3 or 4 */ + /* Defense-in-depth: only 128/192/256-bit keys are valid for Twofish. + * The kernel cryptoapi wrappers (twofish_cbc_setkey, twofish_xts_setkey) + * already validate this, but clamp here as well so a future caller + * cannot trigger the OOB stack/struct writes that the k_len-driven + * loops below would otherwise perform. */ + switch (key_len_bits) { + case 128: + case 192: + case 256: + ctx->k_len = key_len_bits / 64; /* 2, 3 or 4 */ + break; + default: + ctx->k_len = 0; + return; + } for(i = 0; i < ctx->k_len; ++i) {