Move digest and padding handling to OperationFactory.

Moving these methods up from RsaOperationFactory makes them available
for EcdsaOperationFactory.

Cherry-picked from internal.

Bug: 21048758
Change-Id: I2987691eb2a39eef9f091457df65e072a433e70b
diff --git a/operation.cpp b/operation.cpp
index dacaf21..c7f0ff8 100644
--- a/operation.cpp
+++ b/operation.cpp
@@ -16,6 +16,10 @@
 
 #include "operation.h"
 
+#include <keymaster/authorization_set.h>
+
+#include "key.h"
+
 namespace keymaster {
 
 bool OperationFactory::supported(keymaster_padding_t padding) const {
@@ -45,4 +49,45 @@
     return false;
 }
 
+bool OperationFactory::GetAndValidatePadding(const AuthorizationSet& begin_params, const Key& key,
+                                             keymaster_padding_t* padding,
+                                             keymaster_error_t* error) const {
+    *error = KM_ERROR_UNSUPPORTED_PADDING_MODE;
+    if (!begin_params.GetTagValue(TAG_PADDING, padding)) {
+        LOG_E("%d padding modes specified in begin params", begin_params.GetTagCount(TAG_PADDING));
+        return false;
+    } else if (!supported(*padding)) {
+        LOG_E("Padding mode %d not supported", *padding);
+        return false;
+    } else if (!key.authorizations().Contains(TAG_PADDING, *padding) &&
+               !key.authorizations().Contains(TAG_PADDING_OLD, *padding)) {
+        LOG_E("Padding mode %d was specified, but not authorized by key", *padding);
+        *error = KM_ERROR_INCOMPATIBLE_PADDING_MODE;
+        return false;
+    }
+
+    *error = KM_ERROR_OK;
+    return true;
+}
+
+bool OperationFactory::GetAndValidateDigest(const AuthorizationSet& begin_params, const Key& key,
+                                            keymaster_digest_t* digest,
+                                            keymaster_error_t* error) const {
+    *error = KM_ERROR_UNSUPPORTED_DIGEST;
+    if (!begin_params.GetTagValue(TAG_DIGEST, digest)) {
+        LOG_E("%d digests specified in begin params", begin_params.GetTagCount(TAG_DIGEST));
+        return false;
+    } else if (!supported(*digest)) {
+        LOG_E("Digest %d not supported", *digest);
+        return false;
+    } else if (!key.authorizations().Contains(TAG_DIGEST, *digest) &&
+               !key.authorizations().Contains(TAG_DIGEST_OLD, *digest)) {
+        LOG_E("Digest %d was specified, but not authorized by key", *digest);
+        *error = KM_ERROR_INCOMPATIBLE_DIGEST;
+        return false;
+    }
+    *error = KM_ERROR_OK;
+    return true;
+}
+
 }  // namespace keymaster
diff --git a/operation.h b/operation.h
index 10e7cf7..7f6669b 100644
--- a/operation.h
+++ b/operation.h
@@ -72,6 +72,11 @@
     bool supported(keymaster_padding_t padding) const;
     bool supported(keymaster_block_mode_t padding) const;
     bool supported(keymaster_digest_t padding) const;
+
+    bool GetAndValidatePadding(const AuthorizationSet& begin_params, const Key& key,
+                               keymaster_padding_t* padding, keymaster_error_t* error) const;
+    bool GetAndValidateDigest(const AuthorizationSet& begin_params, const Key& key,
+                              keymaster_digest_t* digest, keymaster_error_t* error) const;
 };
 
 /**
diff --git a/rsa_operation.cpp b/rsa_operation.cpp
index 903f570..55ff1b3 100644
--- a/rsa_operation.cpp
+++ b/rsa_operation.cpp
@@ -30,47 +30,6 @@
 
 static const int MIN_PSS_SALT_LEN = 8 /* salt len */ + 2 /* overhead */;
 
-bool RsaOperationFactory::GetAndValidatePadding(const AuthorizationSet& begin_params,
-                                                const Key& key, keymaster_padding_t* padding,
-                                                keymaster_error_t* error) const {
-    *error = KM_ERROR_UNSUPPORTED_PADDING_MODE;
-    if (!begin_params.GetTagValue(TAG_PADDING, padding)) {
-        LOG_E("%d padding modes specified in begin params", begin_params.GetTagCount(TAG_PADDING));
-        return false;
-    } else if (!supported(*padding)) {
-        LOG_E("Padding mode %d not supported", *padding);
-        return false;
-    } else if (!key.authorizations().Contains(TAG_PADDING, *padding) &&
-               !key.authorizations().Contains(TAG_PADDING_OLD, *padding)) {
-        LOG_E("Padding mode %d was specified, but not authorized by key", *padding);
-        *error = KM_ERROR_INCOMPATIBLE_PADDING_MODE;
-        return false;
-    }
-
-    *error = KM_ERROR_OK;
-    return true;
-}
-
-bool RsaOperationFactory::GetAndValidateDigest(const AuthorizationSet& begin_params, const Key& key,
-                                               keymaster_digest_t* digest,
-                                               keymaster_error_t* error) const {
-    *error = KM_ERROR_UNSUPPORTED_DIGEST;
-    if (!begin_params.GetTagValue(TAG_DIGEST, digest)) {
-        LOG_E("%d digests specified in begin params", begin_params.GetTagCount(TAG_DIGEST));
-        return false;
-    } else if (!supported(*digest)) {
-        LOG_E("Digest %d not supported", *digest);
-        return false;
-    } else if (!key.authorizations().Contains(TAG_DIGEST, *digest) &&
-               !key.authorizations().Contains(TAG_DIGEST_OLD, *digest)) {
-        LOG_E("Digest %d was specified, but not authorized by key", *digest);
-        *error = KM_ERROR_INCOMPATIBLE_DIGEST;
-        return false;
-    }
-    *error = KM_ERROR_OK;
-    return true;
-}
-
 /* static */
 RSA* RsaOperationFactory::GetRsaKey(const Key& key, keymaster_error_t* error) {
     const RsaKey* rsa_key = static_cast<const RsaKey*>(&key);
diff --git a/rsa_operation.h b/rsa_operation.h
index ee46283..9c0af17 100644
--- a/rsa_operation.h
+++ b/rsa_operation.h
@@ -148,10 +148,6 @@
     virtual keymaster_purpose_t purpose() const = 0;
 
   protected:
-    bool GetAndValidatePadding(const AuthorizationSet& begin_params, const Key& key,
-                               keymaster_padding_t* padding, keymaster_error_t* error) const;
-    bool GetAndValidateDigest(const AuthorizationSet& begin_params, const Key& key,
-                              keymaster_digest_t* digest, keymaster_error_t* error) const;
     static RSA* GetRsaKey(const Key& key, keymaster_error_t* error);
 };