tf_fuzz: add new crypto key generation model

* Add a new model of what it means for a crypto policy to be valid. This
  fixes the simulation of psa_generate_key() calls, which TF-Fuzz
  currently cannot accurately predict.

  The PSA Crypto properties modelled are whether an algorithm and key
  type are compatible, whether a key type/ algorithm are disabled, and
  what key sizes are allowed.

  Although the PSA Crypto specification has additional, more complicated
  requirements for policies and keys, this commit only implements what
  is necessary for predicting the outcome of psa_generate_key(), and not
  requirements that make a key useless but still valid or involve
  features not yet in TF-M or MbedTLS.

* Improve the way in which policies are filled in, and allow policies to
  be updated at simulation time using the value of a named policy asset.
  Information about other assets and calls is not accessible during
  parse time when the key policy is usually filled in. However, this is
  required for the improved simulation of psa_generate_key calls. This
  is because policy information for generate key calls come from a named
  policy created earlier in the test file.

* Add valid flag to set-policy calls, allowing the creation of a random
  valid policy. For example, see demo/36.test.

* Add demo/36.test. This test generates a policy with a (roughly) even
  chance of being valid or invalid and then tries to generate a key
  using it.

  Running this test a large number of times (~300) succeeds with the
  changes in this commit, showing that TF-Fuzz can now accurately
  predict the outcome of psa_generate_key calls.

Change-Id: Ia40ff893db50b8d2c579d975aa23341b7aab004d
Signed-off-by: Nik Dewally <Nik.Dewally@arm.com>
diff --git a/tf_fuzz/tfz-cpp/calls/crypto_call.cpp b/tf_fuzz/tfz-cpp/calls/crypto_call.cpp
index b800baa..c9d000d 100644
--- a/tf_fuzz/tfz-cpp/calls/crypto_call.cpp
+++ b/tf_fuzz/tfz-cpp/calls/crypto_call.cpp
@@ -20,7 +20,9 @@
 #include "crypto_asset.hpp"
 #include "psa_call.hpp"
 #include "crypto_call.hpp"
+#include "crypto_model.hpp"
 
+using namespace crypto_model;
 
 /**********************************************************************************
    Methods of class policy_call follow:
@@ -958,7 +960,50 @@
 
 bool generate_key_call::copy_call_to_asset (void)
 {
-    return copy_call_to_asset_t<key_asset*> (this, yes_create_asset);
+    // we create the asset conditionally in simulate
+    return copy_call_to_asset_t<key_asset*> (this, dont_create_asset);
+}
+
+bool generate_key_call::simulate() {
+    bool is_policy_valid;
+
+    // NOTE: although algorithm and key-type depend on eachother for validity,
+    // this is checked during key operations not generation
+
+
+    if (policy.key_type == "PSA_KEY_TYPE_RSA_PUBLIC_KEY") {
+        is_policy_valid = false;
+    }
+    else if (policy.key_type == "PSA_KEY_TYPE_PEPPER") {
+        is_policy_valid = false;
+    } else {
+        key_type& kt = get_key_type(policy.key_type);
+        algorithm& alg = get_algorithm(policy.key_algorithm);
+        is_policy_valid = kt.is_valid_key_size(policy.n_bits);
+    }
+
+    psa_asset_usage asset_usage;
+
+    if (!is_policy_valid) {
+        // do not create a key when policy is invalid
+        exp_data.expect_failure();
+        return true;
+    }
+
+    // create an asset, and copy the information we want in it to it.
+    copy_call_to_asset_t<key_asset*> (this, yes_create_asset);
+    switch (asset_info.how_asset_found) {
+    case asset_search::created_new:
+        exp_data.expect_pass();
+
+        break;
+
+    default: // asset already exists!
+        exp_data.expect_failure();
+        break;
+    }
+
+    return true;
 }
 
 void generate_key_call::fill_in_prep_code (void)
@@ -1011,9 +1056,10 @@
     return;  // just to have something to pin a breakpoint onto
 }
 
+
 bool create_key_call::copy_call_to_asset (void)
 {
-    return copy_call_to_asset_t<key_asset*> (this, yes_create_asset);
+    return copy_call_to_asset_t<key_asset*> (this, dont_create_asset);
 }
 
 void create_key_call::fill_in_prep_code (void)
@@ -1324,6 +1370,30 @@
     return;  // just to have something to pin a breakpoint onto
 }
 
+bool remove_key_call::simulate () {
+    if (!exp_data.simulation_needed()) {
+        return false;
+    }
+    switch (asset_info.how_asset_found) {
+        case asset_search::found_active:
+        case asset_search::created_new:
+            exp_data.expect_pass();
+            return true;
+
+        // if the asset never existed, its handle will be null.
+        // deleting the null handle succeeds
+        case asset_search::not_found:
+            exp_data.expect_pass();
+            return true;
+
+        case asset_search::found_deleted:
+            exp_data.expect_failure();
+            return true;
+        default:
+            exp_data.expect_failure();
+            return true;
+    }
+}
 bool remove_key_call::copy_call_to_asset (void)
 {
     return copy_call_to_asset_t<key_asset*> (this, dont_create_asset);