Revert "Revert "Large refactor to move context out of AndroidKeymaster.""

This reverts commit 13fbe3e93247943c26e7ca2ed27b6d650282b8bf.

Bug: 20912868, 19799085
Change-Id: Iadd6ce5cbe94956c2a2fe277f1bf5b108e4bcf57
diff --git a/key.h b/key.h
index f7d2a1c..0eb1539 100644
--- a/key.h
+++ b/key.h
@@ -22,18 +22,19 @@
 #include <keymaster/logger.h>
 
 #include "abstract_factory_registry.h"
-#include "unencrypted_key_blob.h"
 
 namespace keymaster {
 
 class Key;
+class KeymasterContext;
 
 /**
- * KeyFactory is a pure interface whose subclasses know how to construct a specific subclass of Key.
+ * KeyFactory is a abstraction whose subclasses know how to construct a specific subclass of Key.
  * There is a one to one correspondence between Key subclasses and KeyFactory subclasses.
  */
 class KeyFactory {
   public:
+    KeyFactory(const KeymasterContext* context) : context_(context) {}
     virtual ~KeyFactory() {}
 
     // Required for registry
@@ -41,15 +42,27 @@
     virtual keymaster_algorithm_t registry_key() const = 0;
 
     // Factory methods.
-    virtual Key* GenerateKey(const AuthorizationSet& key_description, keymaster_error_t* error) = 0;
-    virtual Key* ImportKey(const AuthorizationSet& key_description,
-                           keymaster_key_format_t key_format, const uint8_t* key_data,
-                           size_t key_data_length, keymaster_error_t* error) = 0;
-    virtual Key* LoadKey(const UnencryptedKeyBlob& blob, keymaster_error_t* error) = 0;
+    virtual keymaster_error_t GenerateKey(const AuthorizationSet& key_description,
+                                          KeymasterKeyBlob* key_blob, AuthorizationSet* hw_enforced,
+                                          AuthorizationSet* sw_enforced) = 0;
+
+    virtual keymaster_error_t ImportKey(const AuthorizationSet& key_description,
+                                        keymaster_key_format_t input_key_material_format,
+                                        const KeymasterKeyBlob& input_key_material,
+                                        KeymasterKeyBlob* output_key_blob,
+                                        AuthorizationSet* hw_enforced,
+                                        AuthorizationSet* sw_enforced) = 0;
+
+    virtual keymaster_error_t LoadKey(const KeymasterKeyBlob& key_material,
+                                      const AuthorizationSet& hw_enforced,
+                                      const AuthorizationSet& sw_enforced, UniquePtr<Key>* key) = 0;
 
     // Informational methods.
     virtual const keymaster_key_format_t* SupportedImportFormats(size_t* format_count) = 0;
     virtual const keymaster_key_format_t* SupportedExportFormats(size_t* format_count) = 0;
+
+  protected:
+    const KeymasterContext* context_;
 };
 
 typedef AbstractFactoryRegistry<KeyFactory> KeyFactoryRegistry;
@@ -77,8 +90,8 @@
     const AuthorizationSet& authorizations() const { return authorizations_; }
 
   protected:
-    Key(const KeyBlob& blob);
-    Key(const AuthorizationSet& authorizations) : authorizations_(authorizations) {}
+    Key(const AuthorizationSet& hw_enforced, const AuthorizationSet& sw_enforced,
+        keymaster_error_t* error);
 
   private:
     AuthorizationSet authorizations_;