Refactor Update operation to return amount of input consumed.
Per the keymaster "update" API documentation, implementations need not
consume all provided input, and must return information about how much
they did consume, so the caller knows to resend the unprocessed portion.
It's convenient for the AES OCB mode encryption to sometimes process
less than is provided, but the Update operation interfaces didn't
account for not consuming all data.
This change was already reviewed, merged and reverted, so I'm skipping
the review step this time.
Change-Id: Ida401453a6af6c751ea7093e283a101bd8527709
diff --git a/google_keymaster_test.cpp b/google_keymaster_test.cpp
index bdd9e24..b061fb0 100644
--- a/google_keymaster_test.cpp
+++ b/google_keymaster_test.cpp
@@ -471,6 +471,7 @@
device.UpdateOperation(update_request, &update_response);
ASSERT_EQ(KM_ERROR_OK, update_response.error);
EXPECT_EQ(0U, update_response.output.available_read());
+ EXPECT_EQ(size, update_response.input_consumed);
FinishOperationRequest finish_request;
finish_request.op_handle = begin_response.op_handle;
@@ -612,6 +613,7 @@
device.UpdateOperation(update_request, &update_response);
ASSERT_EQ(KM_ERROR_OK, update_response.error);
EXPECT_EQ(0U, update_response.output.available_read());
+ EXPECT_EQ(31U, update_response.input_consumed);
FinishOperationRequest finish_request;
finish_request.op_handle = begin_response.op_handle;
@@ -651,6 +653,7 @@
device.UpdateOperation(update_request, &update_response);
ASSERT_EQ(KM_ERROR_OK, update_response.error);
EXPECT_EQ(0U, update_response.output.available_read());
+ EXPECT_EQ(message_len, update_response.input_consumed);
FinishOperationRequest finish_request;
finish_request.op_handle = begin_response.op_handle;
@@ -979,7 +982,7 @@
}
keymaster_error_t UpdateOperation(uint64_t op_handle, const void* message, size_t size,
- string* output) {
+ string* output, size_t* input_consumed) {
UpdateOperationRequest update_request;
update_request.op_handle = op_handle;
update_request.input.Reinitialize(message, size);
@@ -989,6 +992,7 @@
if (update_response.error == KM_ERROR_OK)
output->append(reinterpret_cast<const char*>(update_response.output.peek_read()),
update_response.output.available_read());
+ *input_consumed = update_response.input_consumed;
return update_response.error;
}
@@ -1009,8 +1013,11 @@
EXPECT_EQ(KM_ERROR_OK, BeginOperation(purpose, key_blob, &op_handle));
string result;
- EXPECT_EQ(KM_ERROR_OK, UpdateOperation(op_handle, message, size, &result));
+ size_t input_consumed;
+ EXPECT_EQ(KM_ERROR_OK, UpdateOperation(op_handle, message, size, &result, &input_consumed));
+ EXPECT_EQ(size, input_consumed);
EXPECT_EQ(KM_ERROR_OK, FinishOperation(op_handle, &result));
+
return result;
}
@@ -1064,10 +1071,12 @@
const char message[] = "12345678901234567890123";
uint64_t op_handle;
string result;
+ size_t input_consumed;
EXPECT_EQ(KM_ERROR_OK,
BeginOperation(KM_PURPOSE_ENCRYPT, generate_response_.key_blob, &op_handle));
- EXPECT_EQ(KM_ERROR_OK, UpdateOperation(op_handle, message, array_size(message), &result));
+ EXPECT_EQ(KM_ERROR_OK,
+ UpdateOperation(op_handle, message, array_size(message), &result, &input_consumed));
EXPECT_EQ(KM_ERROR_INVALID_INPUT_LENGTH, FinishOperation(op_handle, &result));
EXPECT_EQ(0, result.size());
}
@@ -1083,10 +1092,11 @@
uint64_t op_handle;
string result;
+ size_t input_consumed;
EXPECT_EQ(KM_ERROR_OK,
BeginOperation(KM_PURPOSE_DECRYPT, generate_response_.key_blob, &op_handle));
- EXPECT_EQ(KM_ERROR_OK,
- UpdateOperation(op_handle, ciphertext.data(), ciphertext.size(), &result));
+ EXPECT_EQ(KM_ERROR_OK, UpdateOperation(op_handle, ciphertext.data(), ciphertext.size(), &result,
+ &input_consumed));
EXPECT_EQ(KM_ERROR_UNKNOWN_ERROR, FinishOperation(op_handle, &result));
EXPECT_EQ(0, result.size());
}
@@ -1119,10 +1129,12 @@
const char message[] = "1234567890123456789012345678901234567890123456789012";
uint64_t op_handle;
string result;
+ size_t input_consumed;
EXPECT_EQ(KM_ERROR_OK,
BeginOperation(KM_PURPOSE_ENCRYPT, generate_response_.key_blob, &op_handle));
- EXPECT_EQ(KM_ERROR_OK, UpdateOperation(op_handle, message, array_size(message), &result));
+ EXPECT_EQ(KM_ERROR_OK,
+ UpdateOperation(op_handle, message, array_size(message), &result, &input_consumed));
EXPECT_EQ(KM_ERROR_INVALID_INPUT_LENGTH, FinishOperation(op_handle, &result));
EXPECT_EQ(0, result.size());
}
@@ -1138,10 +1150,11 @@
uint64_t op_handle;
string result;
+ size_t input_consumed;
EXPECT_EQ(KM_ERROR_OK,
BeginOperation(KM_PURPOSE_DECRYPT, generate_response_.key_blob, &op_handle));
- EXPECT_EQ(KM_ERROR_OK,
- UpdateOperation(op_handle, ciphertext.data(), ciphertext.size(), &result));
+ EXPECT_EQ(KM_ERROR_OK, UpdateOperation(op_handle, ciphertext.data(), ciphertext.size(), &result,
+ &input_consumed));
EXPECT_EQ(KM_ERROR_UNKNOWN_ERROR, FinishOperation(op_handle, &result));
EXPECT_EQ(0, result.size());
}