perf(android): the key is derived once per session, not once per save
Adding an entry took five to seven seconds. The log now says where they went: key derived in 4395 ms. Every save re-derived the master password - 600000 iterations - to arrive at exactly the key that had just been used. The remedy was already half written: EncryptContainerWithKey exists for this reason, but only within a single save. The key now lives for the session, and is reused only when the container carries the same salt and the same iteration count - the desktop may have re-encrypted its own with a fresh salt, and a key that does not fit fails on GCM's tag while the message blames the password, which would be right. Nothing new is exposed: the plaintext vault is already in memory, and the cache already holds the password that produces this key. It goes with them, at lock - salt included, or the next open would think it holds a usable key and decrypt against nothing. The envelope is read in one place now rather than copied per entry point: at three copies, a drift between them would surface as a wrong-password error. Measured against the real repository: HTTP 200, decrypted, 8 entries - so the envelope refactor holds. The reuse line itself is not yet measured on the device.
This commit is contained in:
+51
-6
@@ -116,6 +116,20 @@ type
|
||||
FUrl, FToken, FPwd: string;
|
||||
/// Le compte de cet appareil. Vide tant qu'aucun coffre local n'existe.
|
||||
FUser: string;
|
||||
/// LA CLE DEJA DERIVEE, et de quoi savoir a quel conteneur elle convient.
|
||||
///
|
||||
/// Chaque enregistrement rederivait le mot de passe : 600 000 iterations,
|
||||
/// quatre a six secondes, pour retrouver exactement la cle qui venait de
|
||||
/// servir. C'est ce qui faisait durer un ajout d'entree cinq a sept
|
||||
/// secondes. Elle ne se reutilise QUE si le conteneur porte le meme sel et
|
||||
/// le meme nombre d'iterations - le poste peut avoir rechiffre le sien
|
||||
/// entre-temps, avec un sel neuf, et une cle qui ne convient pas echouerait
|
||||
/// sur la marque de GCM en accusant le mot de passe, qui serait bon.
|
||||
///
|
||||
/// Rien de nouveau n'est expose : le coffre EN CLAIR est deja en memoire.
|
||||
/// Elle part au verrouillage, avec lui.
|
||||
FKey, FKeySalt: TBytes;
|
||||
FKeyIters: Integer;
|
||||
/// Code d'ouverture du cache local, choisi dans l'application. JAMAIS
|
||||
/// ENREGISTRE : il est vide a chaque lancement et doit etre retape. C'est
|
||||
/// ce qui le distingue du jeton, dont la persistance est un confort sans
|
||||
@@ -204,6 +218,9 @@ type
|
||||
procedure PatchSelfCheck;
|
||||
{$ENDIF}
|
||||
procedure OpenEntry(AVaultIndex: Integer);
|
||||
/// Ouvre un conteneur en evitant la derivation quand la cle en main
|
||||
/// convient. Retient au passage celle qui a servi.
|
||||
function OpenContainer(const AContainer, APwd: string): string;
|
||||
procedure RefuseChange(const AMessage: string);
|
||||
procedure CommitChange(const ALabel, AComment, AReopenUuid: string;
|
||||
const APatch: TFunc<string, string>);
|
||||
@@ -612,6 +629,26 @@ end;
|
||||
/// desactive son bouton en envoyant, donc un refus qui se contente de parler
|
||||
/// laisse un formulaire mort avec la saisie derriere. La regle s'est deja fait
|
||||
/// oublier trois fois - a l'editeur, puis au formulaire de creation.
|
||||
function TForm8.OpenContainer(const AContainer, APwd: string): string;
|
||||
var
|
||||
LSalt: TBytes;
|
||||
LIters: Integer;
|
||||
LSw: TStopwatch;
|
||||
begin
|
||||
LSw := TStopwatch.StartNew;
|
||||
if (FKey <> nil) and ContainerKdfParams(AContainer, LSalt, LIters)
|
||||
and (LIters = FKeyIters) and (Length(LSalt) = Length(FKeySalt))
|
||||
and CompareMem(Pointer(LSalt), Pointer(FKeySalt), Length(LSalt)) then
|
||||
begin
|
||||
Result := DecryptContainerWithKey(AContainer, FKey);
|
||||
Log(Format(' opened with the key already in hand, in %d ms',
|
||||
[LSw.ElapsedMilliseconds]));
|
||||
Exit;
|
||||
end;
|
||||
Result := DecryptContainerEx(AContainer, APwd, FKey, FKeySalt, FKeyIters);
|
||||
Log(Format(' key derived in %d ms', [LSw.ElapsedMilliseconds]));
|
||||
end;
|
||||
|
||||
procedure TForm8.RefuseChange(const AMessage: string);
|
||||
begin
|
||||
WebCall('PM.toast(' + JsArg(AMessage) + ')');
|
||||
@@ -671,8 +708,8 @@ begin
|
||||
// cache ensuite : l'inverse laisserait un cache en avance sur
|
||||
// l'autorite si l'ecriture echouait entre les deux.
|
||||
LSw := TStopwatch.StartNew;
|
||||
LPlain := DecryptContainerEx(LoadLocalContainer, FSyncPwd,
|
||||
LKey, LSalt, LIter);
|
||||
LPlain := OpenContainer(LoadLocalContainer, FSyncPwd);
|
||||
LKey := FKey; LSalt := FKeySalt; LIter := FKeyIters;
|
||||
LNew := APatch(LPlain);
|
||||
Log(Format('save: decrypt+patch in %d ms', [LSw.ElapsedMilliseconds]));
|
||||
LSw := TStopwatch.StartNew;
|
||||
@@ -707,8 +744,8 @@ begin
|
||||
// reutilise. Deriver deux fois le meme mot de passe coutait une
|
||||
// seconde fois quatre a six secondes, pour une cle equivalente.
|
||||
LSw := TStopwatch.StartNew;
|
||||
LPlain := DecryptContainerEx(LGet.Container, FSyncPwd,
|
||||
LKey, LSalt, LIter);
|
||||
LPlain := OpenContainer(LGet.Container, FSyncPwd);
|
||||
LKey := FKey; LSalt := FKeySalt; LIter := FKeyIters;
|
||||
LNew := APatch(LPlain);
|
||||
Log(Format('save: decrypt+patch in %d ms', [LSw.ElapsedMilliseconds]));
|
||||
except
|
||||
@@ -1034,6 +1071,14 @@ begin
|
||||
// ecrase ce que l'utilisateur vient de taper - l'auto-verrouillage le faisait
|
||||
// donc sans le moindre geste de sa part, et le champ jeton semblait ne rien
|
||||
// retenir. Verrouiller efface le clair ; la configuration n'a rien a y voir.
|
||||
//
|
||||
// LA CLE DERIVEE PART AUSSI. Elle ouvre le conteneur a elle seule, sans le
|
||||
// mot de passe : la garder apres un verrouillage reviendrait a ne pas
|
||||
// verrouiller. Le sel la suit, sinon la prochaine ouverture croirait tenir
|
||||
// une cle valable et dechiffrerait avec du vide.
|
||||
FKey := nil;
|
||||
FKeySalt := nil;
|
||||
FKeyIters := 0;
|
||||
ShowUnlock;
|
||||
end;
|
||||
|
||||
@@ -1156,7 +1201,7 @@ begin
|
||||
LDoc: TJSONObject;
|
||||
begin
|
||||
try
|
||||
LPlain := DecryptContainerEx(LoadLocalContainer, APwd, LKey, LSalt, LIters);
|
||||
LPlain := OpenContainer(LoadLocalContainer, APwd);
|
||||
LOwner := '';
|
||||
LDoc := TJSONObject.ParseJSONValue(LPlain) as TJSONObject;
|
||||
if LDoc <> nil then
|
||||
@@ -1595,7 +1640,7 @@ begin
|
||||
LSw := TStopwatch.StartNew;
|
||||
// Volontairement AVANT de retenir l'ETag : un echec ici doit laisser
|
||||
// FETag vide pour que le prochain essai retelecharge.
|
||||
LPlain := DecryptContainer(LRes.Container, APwd);
|
||||
LPlain := OpenContainer(LRes.Container, APwd);
|
||||
LSw.Stop;
|
||||
FETag := LRes.ETag;
|
||||
Log(' decrypted in ' + LSw.ElapsedMilliseconds.ToString + ' ms');
|
||||
|
||||
Reference in New Issue
Block a user