des.c: Add support for two-key Triple-DES

Add two-key 3DES support, needed by pycrypto.

This commit is based on the one for the bundled libtomcrypt 1.16
code in pycrypto:

https://github.com/dlitz/pycrypto/commit/65085f16
This commit is contained in:
Paul Howarth 2014-04-15 11:25:18 +01:00
parent 449d5e718d
commit 445dfa67a6

View File

@ -1562,17 +1562,27 @@ int des3_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_k
return CRYPT_INVALID_ROUNDS; return CRYPT_INVALID_ROUNDS;
} }
if (keylen != 24) { if (keylen != 24 && keylen != 16) {
return CRYPT_INVALID_KEYSIZE; return CRYPT_INVALID_KEYSIZE;
} }
deskey(key, EN0, skey->des3.ek[0]); deskey(key, EN0, skey->des3.ek[0]);
deskey(key+8, DE1, skey->des3.ek[1]); deskey(key+8, DE1, skey->des3.ek[1]);
deskey(key+16, EN0, skey->des3.ek[2]); if (keylen == 24) {
deskey(key+16, EN0, skey->des3.ek[2]);
} else {
/* two-key 3DES: K3=K1 */
deskey(key, EN0, skey->des3.ek[2]);
}
deskey(key, DE1, skey->des3.dk[2]); deskey(key, DE1, skey->des3.dk[2]);
deskey(key+8, EN0, skey->des3.dk[1]); deskey(key+8, EN0, skey->des3.dk[1]);
deskey(key+16, DE1, skey->des3.dk[0]); if (keylen == 24) {
deskey(key+16, DE1, skey->des3.dk[0]);
} else {
/* two-key 3DES: K3=K1 */
deskey(key, DE1, skey->des3.dk[0]);
}
return CRYPT_OK; return CRYPT_OK;
} }