{"action":"created","comment":{"url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/pulls/comments/32151225","id":32151225,"diff_hunk":"@@ -181,72 +180,121 @@ impl <T:Axolotl> ReceiveChain<T> {\n }\n impl <T:Axolotl> AxolotlState<T> {\n \n-    pub fn encrypt(&mut self, axolotl_impl : &T, plaintext : &T::PlainText) -> (T::Message, T::Mac) {\n+    pub fn encrypt(&mut self, axolotl_impl : &T, plaintext : T::PlainText) -> (T::Message, T::Mac) {\n         let (new_chain_key,result) = self.encrypt_and_get_next_chain_key(axolotl_impl, plaintext);\n         self.chain_key_send = new_chain_key;\n         self.message_number_send += 1;\n         result\n     }\n \n-    fn encrypt_and_get_next_chain_key(&self, axolotl_impl : &T, plaintext : &T::PlainText) -> (T::ChainKey, (T::Message, T::Mac)) {\n+    fn encrypt_and_get_next_chain_key(&self, axolotl_impl : &T, plaintext : T::PlainText) -> (T::ChainKey, (T::Message, T::Mac)) {\n         let (new_chain_key, message_key) = axolotl_impl.derive_next_chain_and_message_key(&self.chain_key_send);\n         let ciphertext = axolotl_impl.encrypt_message(&message_key, plaintext);\n \n         let message = axolotl_impl.encode_header_and_ciphertext(\n-            self.message_number_send,\n-            self.ratchet_key_send.public.clone(),\n+            Header{\n+                message_number : self.message_number_send,\n+                message_number_prev : self.message_number_prev,\n+                ratchet_key : self.ratchet_key_send.public.clone(),\n+            },\n             ciphertext\n         );\n-        let mac = axolotl_impl.authenticate_message(&message, &message_key, &self.identity_key_remote, &self.identity_key_local);\n+        let mac = axolotl_impl.authenticate_message(&message, &message_key, &self.session_identity);\n \n         (new_chain_key,(message,mac))\n     }\n \n-    pub fn decrypt<'a>(&mut self, axolotl_impl : &T, message : &'a T::Message, mac : T::Mac\n-    ) -> Result<T::PlainText,ReceiveError<T>> where &'a T::Message : AxolotlMessageRef<T> {\n-        let (message_number, message_ratchet_key) = try!(axolotl_impl.decode_header(message).map_err(|e|{ReceiveError::DecodeError(e)}));\n-        let receive_chain_position =  self.receive_chains.iter().position(\n-            | &ReceiveChain{ref ratchet_key, ..} | axolotl_impl.ratchet_keys_are_equal(ratchet_key, message_ratchet_key.borrow())\n+    pub fn decrypt(&mut self, axolotl_impl : &T, message : T::Message, ref mac : T::Mac\n+    ) -> Result<T::PlainText,ReceiveError<T>> {\n+        let Header{ message_number, message_number_prev, ratchet_key : message_ratchet_key_ref } = try!(\n+            axolotl_impl\n+                .decode_header(&message)\n+                .map_err(|e|{ReceiveError::DecodeError(e)})\n         );\n+        let message_ratchet_key = message_ratchet_key_ref.borrow();\n+\n+        self.try_decrypt_with_skipped_chain(axolotl_impl, message, mac, message_number, message_ratchet_key)\n+            .or_else(|message| {\n+                self.try_decrypt_with_current_chain(axolotl_impl, message, mac, message_number, message_ratchet_key)\n+            })\n+            .unwrap_or_else(|message| {\n+                self.try_decrypt_with_new_chain(axolotl_impl, message_number, message_number_prev, message_ratchet_key, message, mac)\n+            })\n+    }\n \n-        match receive_chain_position {\n+    fn try_decrypt_with_skipped_chain(\n+        &mut self, \n+        axolotl_impl : &T, \n+        message : T::Message, \n+        mac : &T::Mac, \n+        message_number : usize, \n+        message_ratchet_key : &T::PublicKey\n+    ) ->  Result<Result<T::PlainText,ReceiveError<T>>, T::Message> {\n+        let position = self.skipped_receive_chains.iter().position(\n+            | &ReceiveChain{ref ratchet_key, ..} | axolotl_impl.ratchet_keys_are_equal(ratchet_key, message_ratchet_key)\n+        );\n+        match position {\n             Some(pos) => {\n-                let receive_chain = &mut self.receive_chains[pos];\n-                receive_chain.try_decrypt(axolotl_impl, message_number, message, mac, &self.identity_key_local, &self.identity_key_remote)\n-            }\n-            None => {\n-                self.try_decrypt_with_new_chain(axolotl_impl, message_number, message_ratchet_key.borrow(), message, mac)\n-            }\n-                \n+                let receive_chain = &mut self.skipped_receive_chains[pos];\n+                Ok(receive_chain.try_decrypt(axolotl_impl, message_number, message, mac, &self.session_identity))\n+            },\n+            None => Err(message),\n         }\n     }\n \n-    fn try_decrypt_with_new_chain<'a>(\n+    fn try_decrypt_with_current_chain(\n+        &mut self, \n+        axolotl_impl : &T, \n+        message : T::Message, \n+        mac : &T::Mac, \n+        message_number : usize, \n+        message_ratchet_key : &T::PublicKey\n+    ) ->  Result<Result<T::PlainText,ReceiveError<T>>, T::Message> {\n+        match self.current_receive_chain {\n+            Some((ref mut current_chain, ref mut chain_key)) => {\n+                if !axolotl_impl.ratchet_keys_are_equal(&current_chain.ratchet_key, message_ratchet_key) {\n+                    return Err(message);\n+                }\n+                let ref session_identity = self.session_identity;","path":"src/axolotl/state.rs","position":349,"original_position":349,"commit_id":"4308c8f25b361725d9b59e8e3438997ef8be7c46","original_commit_id":"4308c8f25b361725d9b59e8e3438997ef8be7c46","user":{"login":"jazzz","id":473256,"avatar_url":"https://avatars.githubusercontent.com/u/473256?v=3","gravatar_id":"","url":"https://api.github.com/users/jazzz","html_url":"https://github.com/jazzz","followers_url":"https://api.github.com/users/jazzz/followers","following_url":"https://api.github.com/users/jazzz/following{/other_user}","gists_url":"https://api.github.com/users/jazzz/gists{/gist_id}","starred_url":"https://api.github.com/users/jazzz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jazzz/subscriptions","organizations_url":"https://api.github.com/users/jazzz/orgs","repos_url":"https://api.github.com/users/jazzz/repos","events_url":"https://api.github.com/users/jazzz/events{/privacy}","received_events_url":"https://api.github.com/users/jazzz/received_events","type":"User","site_admin":false},"body":"is this line nessesary?","created_at":"2015-06-10T18:39:18Z","updated_at":"2015-06-10T18:39:18Z","html_url":"https://github.com/mjewkes/Axolotl-Rust/pull/38#discussion_r32151225","pull_request_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/pulls/38","_links":{"self":{"href":"https://api.github.com/repos/mjewkes/Axolotl-Rust/pulls/comments/32151225"},"html":{"href":"https://github.com/mjewkes/Axolotl-Rust/pull/38#discussion_r32151225"},"pull_request":{"href":"https://api.github.com/repos/mjewkes/Axolotl-Rust/pulls/38"}}},"pull_request":{"url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/pulls/38","id":37290154,"html_url":"https://github.com/mjewkes/Axolotl-Rust/pull/38","diff_url":"https://github.com/mjewkes/Axolotl-Rust/pull/38.diff","patch_url":"https://github.com/mjewkes/Axolotl-Rust/pull/38.patch","issue_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/issues/38","number":38,"state":"open","locked":false,"title":"use previous message number, and allow in-place encryption, separate 3dh","user":{"login":"hclarke","id":108887,"avatar_url":"https://avatars.githubusercontent.com/u/108887?v=3","gravatar_id":"","url":"https://api.github.com/users/hclarke","html_url":"https://github.com/hclarke","followers_url":"https://api.github.com/users/hclarke/followers","following_url":"https://api.github.com/users/hclarke/following{/other_user}","gists_url":"https://api.github.com/users/hclarke/gists{/gist_id}","starred_url":"https://api.github.com/users/hclarke/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hclarke/subscriptions","organizations_url":"https://api.github.com/users/hclarke/orgs","repos_url":"https://api.github.com/users/hclarke/repos","events_url":"https://api.github.com/users/hclarke/events{/privacy}","received_events_url":"https://api.github.com/users/hclarke/received_events","type":"User","site_admin":false},"body":"previous message number is now used to figure out how many message keys to store for a chain. header related functions use a struct rather than a tuple, since there are now two usize values, and they could use names.\r\n\r\nencrypt/decrypt and encode/decode now take parameters by value. this allows an implementor to modify the plaintext in-place and return it as ciphertext, if they wanted to.","created_at":"2015-06-09T19:31:20Z","updated_at":"2015-06-10T18:39:18Z","closed_at":null,"merged_at":null,"merge_commit_sha":"b132184525ee720860b89cf1db0cd4562270f4bf","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/pulls/38/commits","review_comments_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/pulls/38/comments","review_comment_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/pulls/comments{/number}","comments_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/issues/38/comments","statuses_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/statuses/4308c8f25b361725d9b59e8e3438997ef8be7c46","head":{"label":"mjewkes:feature/no_allocation","ref":"feature/no_allocation","sha":"4308c8f25b361725d9b59e8e3438997ef8be7c46","user":{"login":"mjewkes","id":108649,"avatar_url":"https://avatars.githubusercontent.com/u/108649?v=3","gravatar_id":"","url":"https://api.github.com/users/mjewkes","html_url":"https://github.com/mjewkes","followers_url":"https://api.github.com/users/mjewkes/followers","following_url":"https://api.github.com/users/mjewkes/following{/other_user}","gists_url":"https://api.github.com/users/mjewkes/gists{/gist_id}","starred_url":"https://api.github.com/users/mjewkes/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mjewkes/subscriptions","organizations_url":"https://api.github.com/users/mjewkes/orgs","repos_url":"https://api.github.com/users/mjewkes/repos","events_url":"https://api.github.com/users/mjewkes/events{/privacy}","received_events_url":"https://api.github.com/users/mjewkes/received_events","type":"User","site_admin":false},"repo":{"id":34677158,"name":"Axolotl-Rust","full_name":"mjewkes/Axolotl-Rust","owner":{"login":"mjewkes","id":108649,"avatar_url":"https://avatars.githubusercontent.com/u/108649?v=3","gravatar_id":"","url":"https://api.github.com/users/mjewkes","html_url":"https://github.com/mjewkes","followers_url":"https://api.github.com/users/mjewkes/followers","following_url":"https://api.github.com/users/mjewkes/following{/other_user}","gists_url":"https://api.github.com/users/mjewkes/gists{/gist_id}","starred_url":"https://api.github.com/users/mjewkes/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mjewkes/subscriptions","organizations_url":"https://api.github.com/users/mjewkes/orgs","repos_url":"https://api.github.com/users/mjewkes/repos","events_url":"https://api.github.com/users/mjewkes/events{/privacy}","received_events_url":"https://api.github.com/users/mjewkes/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/mjewkes/Axolotl-Rust","description":"Axolotl protocol written in Rust","fork":false,"url":"https://api.github.com/repos/mjewkes/Axolotl-Rust","forks_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/forks","keys_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/teams","hooks_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/hooks","issue_events_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/issues/events{/number}","events_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/events","assignees_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/assignees{/user}","branches_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/branches{/branch}","tags_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/tags","blobs_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/git/refs{/sha}","trees_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/statuses/{sha}","languages_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/languages","stargazers_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/stargazers","contributors_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/contributors","subscribers_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/subscribers","subscription_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/subscription","commits_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/commits{/sha}","git_commits_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/git/commits{/sha}","comments_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/comments{/number}","issue_comment_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/issues/comments{/number}","contents_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/contents/{+path}","compare_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/merges","archive_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/downloads","issues_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/issues{/number}","pulls_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/pulls{/number}","milestones_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/milestones{/number}","notifications_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/labels{/name}","releases_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/releases{/id}","created_at":"2015-04-27T16:13:34Z","updated_at":"2015-05-16T13:07:14Z","pushed_at":"2015-06-10T16:47:22Z","git_url":"git://github.com/mjewkes/Axolotl-Rust.git","ssh_url":"git@github.com:mjewkes/Axolotl-Rust.git","clone_url":"https://github.com/mjewkes/Axolotl-Rust.git","svn_url":"https://github.com/mjewkes/Axolotl-Rust","homepage":null,"size":621,"stargazers_count":1,"watchers_count":1,"language":"Rust","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":17,"forks":1,"open_issues":17,"watchers":1,"default_branch":"develop"}},"base":{"label":"mjewkes:develop","ref":"develop","sha":"3f2fd9190968d051868bccdfddf6e8798e99575c","user":{"login":"mjewkes","id":108649,"avatar_url":"https://avatars.githubusercontent.com/u/108649?v=3","gravatar_id":"","url":"https://api.github.com/users/mjewkes","html_url":"https://github.com/mjewkes","followers_url":"https://api.github.com/users/mjewkes/followers","following_url":"https://api.github.com/users/mjewkes/following{/other_user}","gists_url":"https://api.github.com/users/mjewkes/gists{/gist_id}","starred_url":"https://api.github.com/users/mjewkes/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mjewkes/subscriptions","organizations_url":"https://api.github.com/users/mjewkes/orgs","repos_url":"https://api.github.com/users/mjewkes/repos","events_url":"https://api.github.com/users/mjewkes/events{/privacy}","received_events_url":"https://api.github.com/users/mjewkes/received_events","type":"User","site_admin":false},"repo":{"id":34677158,"name":"Axolotl-Rust","full_name":"mjewkes/Axolotl-Rust","owner":{"login":"mjewkes","id":108649,"avatar_url":"https://avatars.githubusercontent.com/u/108649?v=3","gravatar_id":"","url":"https://api.github.com/users/mjewkes","html_url":"https://github.com/mjewkes","followers_url":"https://api.github.com/users/mjewkes/followers","following_url":"https://api.github.com/users/mjewkes/following{/other_user}","gists_url":"https://api.github.com/users/mjewkes/gists{/gist_id}","starred_url":"https://api.github.com/users/mjewkes/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mjewkes/subscriptions","organizations_url":"https://api.github.com/users/mjewkes/orgs","repos_url":"https://api.github.com/users/mjewkes/repos","events_url":"https://api.github.com/users/mjewkes/events{/privacy}","received_events_url":"https://api.github.com/users/mjewkes/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/mjewkes/Axolotl-Rust","description":"Axolotl protocol written in Rust","fork":false,"url":"https://api.github.com/repos/mjewkes/Axolotl-Rust","forks_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/forks","keys_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/teams","hooks_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/hooks","issue_events_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/issues/events{/number}","events_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/events","assignees_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/assignees{/user}","branches_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/branches{/branch}","tags_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/tags","blobs_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/git/refs{/sha}","trees_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/statuses/{sha}","languages_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/languages","stargazers_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/stargazers","contributors_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/contributors","subscribers_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/subscribers","subscription_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/subscription","commits_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/commits{/sha}","git_commits_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/git/commits{/sha}","comments_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/comments{/number}","issue_comment_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/issues/comments{/number}","contents_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/contents/{+path}","compare_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/merges","archive_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/downloads","issues_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/issues{/number}","pulls_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/pulls{/number}","milestones_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/milestones{/number}","notifications_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/labels{/name}","releases_url":"https://api.github.com/repos/mjewkes/Axolotl-Rust/releases{/id}","created_at":"2015-04-27T16:13:34Z","updated_at":"2015-05-16T13:07:14Z","pushed_at":"2015-06-10T16:47:22Z","git_url":"git://github.com/mjewkes/Axolotl-Rust.git","ssh_url":"git@github.com:mjewkes/Axolotl-Rust.git","clone_url":"https://github.com/mjewkes/Axolotl-Rust.git","svn_url":"https://github.com/mjewkes/Axolotl-Rust","homepage":null,"size":621,"stargazers_count":1,"watchers_count":1,"language":"Rust","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":17,"forks":1,"open_issues":17,"watchers":1,"default_branch":"develop"}},"_links":{"self":{"href":"https://api.github.com/repos/mjewkes/Axolotl-Rust/pulls/38"},"html":{"href":"https://github.com/mjewkes/Axolotl-Rust/pull/38"},"issue":{"href":"https://api.github.com/repos/mjewkes/Axolotl-Rust/issues/38"},"comments":{"href":"https://api.github.com/repos/mjewkes/Axolotl-Rust/issues/38/comments"},"review_comments":{"href":"https://api.github.com/repos/mjewkes/Axolotl-Rust/pulls/38/comments"},"review_comment":{"href":"https://api.github.com/repos/mjewkes/Axolotl-Rust/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/mjewkes/Axolotl-Rust/pulls/38/commits"},"statuses":{"href":"https://api.github.com/repos/mjewkes/Axolotl-Rust/statuses/4308c8f25b361725d9b59e8e3438997ef8be7c46"}}}}