Implement TrustyKeymaster key generation, plus tests.

Change-Id: I085be101c735d136e7d5b2915a9510102722e695
diff --git a/key_blob.cpp b/key_blob.cpp
index 3f354e3..0e8a640 100644
--- a/key_blob.cpp
+++ b/key_blob.cpp
@@ -40,6 +40,12 @@
                  const keymaster_key_blob_t& master_key, const uint8_t nonce[NONCE_LENGTH])
     : error_(KM_ERROR_OK), nonce_(new uint8_t[NONCE_LENGTH]), tag_(new uint8_t[TAG_LENGTH]),
       enforced_(enforced), unenforced_(unenforced), hidden_(hidden) {
+    if (!nonce_.get() || !tag_.get()) {
+        error_ = KM_ERROR_MEMORY_ALLOCATION_FAILED;
+        return;
+    }
+    error_ = KM_ERROR_OK;
+
     if (enforced_.is_valid() == AuthorizationSet::ALLOCATION_FAILURE ||
         unenforced_.is_valid() == AuthorizationSet::ALLOCATION_FAILURE ||
         hidden_.is_valid() == AuthorizationSet::ALLOCATION_FAILURE) {
@@ -74,12 +80,30 @@
 KeyBlob::KeyBlob(const keymaster_key_blob_t& key, const AuthorizationSet& hidden,
                  const keymaster_key_blob_t& master_key)
     : nonce_(new uint8_t[NONCE_LENGTH]), tag_(new uint8_t[TAG_LENGTH]), hidden_(hidden) {
+    if (!nonce_.get() || !tag_.get()) {
+        error_ = KM_ERROR_MEMORY_ALLOCATION_FAILED;
+        return;
+    }
+    error_ = KM_ERROR_OK;
+
     const uint8_t* p = key.key_material;
     if (!Deserialize(&p, key.key_material + key.key_material_size))
         return;
     DecryptKey(master_key);
 }
 
+KeyBlob::KeyBlob(const uint8_t* key_blob, size_t blob_size)
+    : nonce_(new uint8_t[NONCE_LENGTH]), tag_(new uint8_t[TAG_LENGTH]) {
+    if (!nonce_.get() || !tag_.get()) {
+        error_ = KM_ERROR_MEMORY_ALLOCATION_FAILED;
+        return;
+    }
+    error_ = KM_ERROR_OK;
+
+    if (!Deserialize(&key_blob, key_blob + blob_size))
+        return;
+}
+
 size_t KeyBlob::SerializedSize() const {
     return NONCE_LENGTH + sizeof(uint32_t) + key_material_length() + TAG_LENGTH +
            enforced_.SerializedSize() + unenforced_.SerializedSize();