@@ -6067,6 +6067,149 @@ static void KeyslotContextAndKeyringLink(void)
60676067#endif
60686068}
60696069
6070+ static void TruncatedKeys (void )
6071+ {
6072+ char key [64 ], key2 [64 ], key3 [64 ], key4 [64 ];
6073+ const char * passphrase = PASSPHRASE ;
6074+
6075+ /* test key with trailing zero bytes susceptible to padding conflict in pbkdf2(hmac) */
6076+ const char * vk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a00" ;
6077+ const char * vk_hex2 = "bb21158c733229347bd4e681891e213d00000000000000000000000000000000" ;
6078+
6079+ const char * vk_hex3 = "8ca7689d9d422cd87f86b18cbb6aa834a632e8d74a43ccfd86415bb9c65979c3" \
6080+ "2636b79b74458807fffbda615f6fe5d5deeefef5c890879d66fc6b5ce1105d00" ;
6081+ const char * vk_hex4 = "8ca7689d9d422cd87f86b18cbb6aa834a632e8d74a43ccfd86415bb9c65979c3" \
6082+ "0000000000000000000000000000000000000000000000000000000000000000" ;
6083+ size_t key_size = strlen (vk_hex ) / 2 , key_size2 = strlen (vk_hex3 ) / 2 ;
6084+ const char * cipher = "aes" ;
6085+ const char * cbc_mode = "cbc-essiv:sha256" , * xts_mode = "xts-plain64" ;
6086+ uint64_t r_payload_offset ;
6087+
6088+ OK_ (crypt_decode_key (key , vk_hex , key_size ));
6089+ OK_ (crypt_decode_key (key2 , vk_hex2 , key_size ));
6090+ OK_ (crypt_decode_key (key3 , vk_hex3 , key_size2 ));
6091+ OK_ (crypt_decode_key (key4 , vk_hex4 , key_size2 ));
6092+
6093+ // init test devices
6094+ OK_ (get_luks2_offsets (0 , 0 , 0 , NULL , & r_payload_offset ));
6095+ OK_ (create_dmdevice_over_loop (H_DEVICE , r_payload_offset + 1 ));
6096+
6097+ // cbc mode
6098+ // format with trailing zero byte key
6099+ OK_ (crypt_init (& cd , DMDIR H_DEVICE ));
6100+ OK_ (set_fast_pbkdf (cd ));
6101+ OK_ (crypt_format (cd , CRYPT_LUKS2 , cipher , cbc_mode , NULL , key , key_size , NULL ));
6102+ // the truncated key (cut off zero byte) must not pass verification
6103+ FAIL_ (crypt_volume_key_verify (cd , key , key_size - 1 ), "Key does not match the volume." );
6104+ FAIL_ (crypt_keyslot_add_by_volume_key (cd , CRYPT_ANY_SLOT , key , key_size - 1 , passphrase , strlen (passphrase )), "Key does not match the volume." );
6105+ // add keyslot so LUKS2 can verify key size properly later
6106+ EQ_ (crypt_keyslot_add_by_volume_key (cd , 0 , key , key_size , passphrase , strlen (passphrase )), 0 );
6107+ // no need to test activation since AES will not accept short keys
6108+ CRYPT_FREE (cd );
6109+
6110+ // test again with cached volume key dropped
6111+ OK_ (crypt_init (& cd , DMDIR H_DEVICE ));
6112+ OK_ (crypt_load (cd , CRYPT_LUKS2 , NULL ));
6113+ FAIL_ (crypt_volume_key_verify (cd , key , key_size - 1 ), "Key does not match the volume." );
6114+ FAIL_ (crypt_keyslot_add_by_volume_key (cd , CRYPT_ANY_SLOT , key , key_size - 1 , passphrase , strlen (passphrase )), "Key does not match the volume." );
6115+ CRYPT_FREE (cd );
6116+
6117+ // format device with zeroed second half of the key
6118+ OK_ (crypt_init (& cd , DMDIR H_DEVICE ));
6119+ OK_ (set_fast_pbkdf (cd ));
6120+ OK_ (crypt_format (cd , CRYPT_LUKS2 , cipher , cbc_mode , NULL , key2 , key_size , NULL ));
6121+ FAIL_ (crypt_volume_key_verify (cd , key2 , key_size / 2 ), "Key does not match the volume." );
6122+ FAIL_ (crypt_keyslot_add_by_volume_key (cd , CRYPT_ANY_SLOT , key2 , key_size / 2 , passphrase , strlen (passphrase )), "Key does not match the volume." );
6123+ // activation must fail, we test with 128 bits AES key (the original key was 256 bits)
6124+ FAIL_ (crypt_activate_by_volume_key (cd , CDEVICE_1 , key2 , key_size / 2 , 0 ), "Key does not match the volume." );
6125+ EQ_ (crypt_keyslot_add_by_volume_key (cd , 0 , key2 , key_size , passphrase , strlen (passphrase )), 0 );
6126+ CRYPT_FREE (cd );
6127+
6128+ // test again with cached volume key dropped
6129+ OK_ (crypt_init (& cd , DMDIR H_DEVICE ));
6130+ OK_ (crypt_load (cd , CRYPT_LUKS2 , NULL ));
6131+ FAIL_ (crypt_volume_key_verify (cd , key2 , key_size / 2 ), "Key does not match the volume." );
6132+ FAIL_ (crypt_keyslot_add_by_volume_key (cd , CRYPT_ANY_SLOT , key2 , key_size / 2 , passphrase , strlen (passphrase )), "Key does not match the volume." );
6133+ // activation must fail, we test with 128 bits AES key (the original key was 256 bits)
6134+ FAIL_ (crypt_activate_by_volume_key (cd , CDEVICE_1 , key2 , key_size / 2 , 0 ), "Key does not match the volume." );
6135+ CRYPT_FREE (cd );
6136+
6137+ // xts mode
6138+ // format with trailing zero byte key
6139+ OK_ (crypt_init (& cd , DMDIR H_DEVICE ));
6140+ OK_ (set_fast_pbkdf (cd ));
6141+ OK_ (crypt_format (cd , CRYPT_LUKS2 , cipher , xts_mode , NULL , key3 , key_size2 , NULL ));
6142+ // the truncated key (cut off zero byte) must not pass verification
6143+ FAIL_ (crypt_volume_key_verify (cd , key3 , key_size2 - 1 ), "Key does not match the volume." );
6144+ FAIL_ (crypt_keyslot_add_by_volume_key (cd , CRYPT_ANY_SLOT , key3 , key_size2 - 1 , passphrase , strlen (passphrase )), "Key does not match the volume." );
6145+ // add keyslot so LUKS2 can verify key size properly later
6146+ EQ_ (crypt_keyslot_add_by_volume_key (cd , 0 , key3 , key_size2 , passphrase , strlen (passphrase )), 0 );
6147+ // no need to test activation since AES will not accept short keys
6148+ CRYPT_FREE (cd );
6149+
6150+ // test again with cached volume key dropped
6151+ OK_ (crypt_init (& cd , DMDIR H_DEVICE ));
6152+ OK_ (crypt_load (cd , CRYPT_LUKS2 , NULL ));
6153+ FAIL_ (crypt_volume_key_verify (cd , key3 , key_size2 - 1 ), "Key does not match the volume." );
6154+ FAIL_ (crypt_keyslot_add_by_volume_key (cd , CRYPT_ANY_SLOT , key3 , key_size2 - 1 , passphrase , strlen (passphrase )), "Key does not match the volume." );
6155+ CRYPT_FREE (cd );
6156+
6157+ // format device with zeroed second half of the key
6158+ OK_ (crypt_init (& cd , DMDIR H_DEVICE ));
6159+ OK_ (set_fast_pbkdf (cd ));
6160+ OK_ (crypt_format (cd , CRYPT_LUKS2 , cipher , xts_mode , NULL , key4 , key_size2 , NULL ));
6161+ FAIL_ (crypt_volume_key_verify (cd , key4 , key_size2 / 2 ), "Key does not match the volume." );
6162+ FAIL_ (crypt_keyslot_add_by_volume_key (cd , CRYPT_ANY_SLOT , key4 , key_size2 / 2 , passphrase , strlen (passphrase )), "Key does not match the volume." );
6163+ // activation must fail, we test with (doubled for xts) 128 bits AES key (the original key was 256 bits)
6164+ FAIL_ (crypt_activate_by_volume_key (cd , CDEVICE_1 , key4 , key_size2 / 2 , 0 ), "Key does not match the volume." );
6165+ EQ_ (crypt_keyslot_add_by_volume_key (cd , 0 , key4 , key_size2 , passphrase , strlen (passphrase )), 0 );
6166+ CRYPT_FREE (cd );
6167+
6168+ // test again with cached volume key dropped
6169+ OK_ (crypt_init (& cd , DMDIR H_DEVICE ));
6170+ OK_ (crypt_load (cd , CRYPT_LUKS2 , NULL ));
6171+ FAIL_ (crypt_volume_key_verify (cd , key4 , key_size2 / 2 ), "Key does not match the volume." );
6172+ FAIL_ (crypt_keyslot_add_by_volume_key (cd , CRYPT_ANY_SLOT , key4 , key_size2 / 2 , passphrase , strlen (passphrase )), "Key does not match the volume." );
6173+ // activation must fail, we test with (doubled for xts) 128 bits AES key (the original key was 256 bits)
6174+ FAIL_ (crypt_activate_by_volume_key (cd , CDEVICE_1 , key4 , key_size2 / 2 , 0 ), "Key does not match the volume." );
6175+ CRYPT_FREE (cd );
6176+
6177+ // check unbound key verification
6178+ OK_ (crypt_init (& cd , DMDIR H_DEVICE ));
6179+ OK_ (set_fast_pbkdf (cd ));
6180+ OK_ (crypt_format (cd , CRYPT_LUKS2 , cipher , xts_mode , NULL , key3 , key_size2 , NULL ));
6181+ // keyslot must always be unbound if created with CRYPT_VOLUME_KEY_NO_SEGMENT
6182+ EQ_ (crypt_keyslot_add_by_key (cd , 1 , key3 , key_size2 - 1 , passphrase , strlen (passphrase ), CRYPT_VOLUME_KEY_NO_SEGMENT ), 1 );
6183+ EQ_ (crypt_keyslot_status (cd , 1 ), CRYPT_SLOT_UNBOUND );
6184+ FAIL_ (crypt_activate_by_passphrase (cd , CDEVICE_1 , 1 , passphrase , strlen (passphrase ), 0 ), "Keyslot unusable for device activation." );
6185+ CRYPT_FREE (cd );
6186+
6187+ // check unbound key verification with CRYPT_VOLUME_KEY_DIGEST_REUSE
6188+ OK_ (crypt_init (& cd , DMDIR H_DEVICE ));
6189+ OK_ (set_fast_pbkdf (cd ));
6190+ OK_ (crypt_format (cd , CRYPT_LUKS2 , cipher , xts_mode , NULL , key4 , key_size2 , NULL ));
6191+ // drop cached volume key
6192+ CRYPT_FREE (cd );
6193+ OK_ (crypt_init (& cd , DMDIR H_DEVICE ));
6194+ OK_ (crypt_load (cd , CRYPT_LUKS2 , NULL ));
6195+ EQ_ (crypt_keyslot_add_by_key (cd , 0 , key4 , key_size2 - 1 , passphrase , strlen (passphrase ), CRYPT_VOLUME_KEY_NO_SEGMENT ), 0 );
6196+ EQ_ (crypt_keyslot_add_by_key (cd , 1 , key4 , key_size2 - 2 , passphrase , strlen (passphrase ), CRYPT_VOLUME_KEY_NO_SEGMENT | CRYPT_VOLUME_KEY_DIGEST_REUSE ), 1 );
6197+ /* it must not reuse default segment digest */
6198+ EQ_ (crypt_keyslot_status (cd , 1 ), CRYPT_SLOT_UNBOUND );
6199+ FAIL_ (crypt_activate_by_passphrase (cd , CDEVICE_1 , 1 , passphrase , strlen (passphrase ), 0 ), "Keyslot unusable for device activation." );
6200+ EQ_ (crypt_keyslot_add_by_key (cd , 2 , key4 , key_size2 - 3 , passphrase , strlen (passphrase ), CRYPT_VOLUME_KEY_NO_SEGMENT | CRYPT_VOLUME_KEY_DIGEST_REUSE ), 2 );
6201+ EQ_ (crypt_keyslot_add_by_key (cd , 3 , key4 , key_size2 - 4 , passphrase , strlen (passphrase ), CRYPT_VOLUME_KEY_NO_SEGMENT | CRYPT_VOLUME_KEY_DIGEST_REUSE ), 3 );
6202+ EQ_ (crypt_keyslot_add_by_key (cd , 4 , key4 , key_size2 - 5 , passphrase , strlen (passphrase ), CRYPT_VOLUME_KEY_NO_SEGMENT | CRYPT_VOLUME_KEY_DIGEST_REUSE ), 4 );
6203+ EQ_ (crypt_keyslot_add_by_key (cd , 5 , key4 , key_size2 - 6 , passphrase , strlen (passphrase ), CRYPT_VOLUME_KEY_NO_SEGMENT | CRYPT_VOLUME_KEY_DIGEST_REUSE ), 5 );
6204+ EQ_ (crypt_keyslot_add_by_key (cd , 6 , key4 , key_size2 - 7 , passphrase , strlen (passphrase ), CRYPT_VOLUME_KEY_NO_SEGMENT | CRYPT_VOLUME_KEY_DIGEST_REUSE ), 6 );
6205+ // It must run out of free digests by now
6206+ FAIL_ (crypt_keyslot_add_by_key (cd , 7 , key4 , key_size2 - 8 , passphrase , strlen (passphrase ), CRYPT_VOLUME_KEY_NO_SEGMENT | CRYPT_VOLUME_KEY_DIGEST_REUSE ), "No free digest slot." );
6207+ CRYPT_FREE (cd );
6208+
6209+ _remove_keyfiles ();
6210+ _cleanup_dmdevices ();
6211+ }
6212+
60706213static int _crypt_load_check (struct crypt_device * _cd )
60716214{
60726215#if HAVE_BLKID
@@ -6195,6 +6338,7 @@ int main(int argc, char *argv[])
61956338 RUN_ (LuksKeyslotAdd , "Adding keyslot via new API" );
61966339 RUN_ (VolumeKeyGet , "Getting volume key via keyslot context API" );
61976340 RUN_ (KeyslotContextAndKeyringLink , "Activate via keyslot context API and linking VK to a keyring" );
6341+ RUN_ (TruncatedKeys , "Test truncated candidate keys." );
61986342 RUN_ (Luks2Repair , "LUKS2 repair" ); // test disables metadata locking. Run always last!
61996343
62006344 _cleanup ();
0 commit comments