auth_encrypted_key_blob.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright 2015 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <keymaster/key_blob_utils/auth_encrypted_key_blob.h>
  17. #include <keymaster/android_keymaster_utils.h>
  18. #include <keymaster/authorization_set.h>
  19. #include <keymaster/key_blob_utils/ocb_utils.h>
  20. #include <keymaster/logger.h>
  21. namespace keymaster {
  22. const uint32_t CURRENT_BLOB_VERSION = 0;
  23. keymaster_error_t SerializeAuthEncryptedBlob(const KeymasterKeyBlob& encrypted_key_material,
  24. const AuthorizationSet& hw_enforced,
  25. const AuthorizationSet& sw_enforced,
  26. const Buffer& nonce, const Buffer& tag,
  27. KeymasterKeyBlob* key_blob) {
  28. size_t size = 1 /* version byte */ + nonce.SerializedSize() +
  29. encrypted_key_material.SerializedSize() + tag.SerializedSize() +
  30. hw_enforced.SerializedSize() + sw_enforced.SerializedSize();
  31. if (!key_blob->Reset(size))
  32. return KM_ERROR_MEMORY_ALLOCATION_FAILED;
  33. uint8_t* buf = key_blob->writable_data();
  34. const uint8_t* end = key_blob->key_material + key_blob->key_material_size;
  35. *buf++ = CURRENT_BLOB_VERSION;
  36. buf = nonce.Serialize(buf, end);
  37. buf = encrypted_key_material.Serialize(buf, end);
  38. buf = tag.Serialize(buf, end);
  39. buf = hw_enforced.Serialize(buf, end);
  40. buf = sw_enforced.Serialize(buf, end);
  41. if (buf != key_blob->key_material + key_blob->key_material_size)
  42. return KM_ERROR_UNKNOWN_ERROR;
  43. return KM_ERROR_OK;
  44. }
  45. static keymaster_error_t DeserializeUnversionedBlob(const KeymasterKeyBlob& key_blob,
  46. KeymasterKeyBlob* encrypted_key_material,
  47. AuthorizationSet* hw_enforced,
  48. AuthorizationSet* sw_enforced, Buffer* nonce,
  49. Buffer* tag) {
  50. const uint8_t* tmp = key_blob.key_material;
  51. const uint8_t** buf_ptr = &tmp;
  52. const uint8_t* end = tmp + key_blob.key_material_size;
  53. if (!nonce->reserve(OCB_NONCE_LENGTH) || !tag->reserve(OCB_TAG_LENGTH))
  54. return KM_ERROR_MEMORY_ALLOCATION_FAILED;
  55. if (!copy_from_buf(buf_ptr, end, nonce->peek_write(), OCB_NONCE_LENGTH) ||
  56. !encrypted_key_material->Deserialize(buf_ptr, end) ||
  57. !copy_from_buf(buf_ptr, end, tag->peek_write(), OCB_TAG_LENGTH) ||
  58. !hw_enforced->Deserialize(buf_ptr, end) || //
  59. !sw_enforced->Deserialize(buf_ptr, end)) {
  60. LOG_D("Failed to deserialize unversioned blob (may be a HW-backed key)", 0);
  61. return KM_ERROR_INVALID_KEY_BLOB;
  62. }
  63. if (!nonce->advance_write(OCB_NONCE_LENGTH) || !tag->advance_write(OCB_TAG_LENGTH))
  64. return KM_ERROR_UNKNOWN_ERROR;
  65. return KM_ERROR_OK;
  66. }
  67. keymaster_error_t DeserializeAuthEncryptedBlob(const KeymasterKeyBlob& key_blob,
  68. KeymasterKeyBlob* encrypted_key_material,
  69. AuthorizationSet* hw_enforced,
  70. AuthorizationSet* sw_enforced, Buffer* nonce,
  71. Buffer* tag) {
  72. if (!key_blob.key_material || key_blob.key_material_size == 0)
  73. return KM_ERROR_INVALID_KEY_BLOB;
  74. const uint8_t* tmp = key_blob.key_material;
  75. const uint8_t** buf_ptr = &tmp;
  76. const uint8_t* end = tmp + key_blob.key_material_size;
  77. if (end <= *buf_ptr)
  78. return KM_ERROR_INVALID_KEY_BLOB;
  79. uint8_t version = *(*buf_ptr)++;
  80. if (version != CURRENT_BLOB_VERSION || //
  81. !nonce->Deserialize(buf_ptr, end) || nonce->available_read() != OCB_NONCE_LENGTH ||
  82. !encrypted_key_material->Deserialize(buf_ptr, end) || //
  83. !tag->Deserialize(buf_ptr, end) || tag->available_read() != OCB_TAG_LENGTH ||
  84. !hw_enforced->Deserialize(buf_ptr, end) || //
  85. !sw_enforced->Deserialize(buf_ptr, end)) {
  86. // This blob failed to parse. Either it's corrupted or it's a blob generated by an earlier
  87. // version of keymaster using a previous blob format which did not include the version byte
  88. // or the nonce or tag length fields. So we try to parse it as that previous version.
  89. //
  90. // Note that it's not really a problem if we erronously parse a corrupted blob, because
  91. // decryption will fail the authentication check.
  92. //
  93. // A bigger potential problem is: What if a valid unversioned blob appears to parse
  94. // correctly as a versioned blob? It would then be rejected during decryption, causing a
  95. // valid key to become unusable. If this is a disk encryption key, upgrading to a keymaster
  96. // version with the new format would destroy the user's data.
  97. //
  98. // What is the probability that an unversioned key could be successfully parsed as a version
  99. // 0 key? The first 12 bytes of an unversioned key are the nonce, which, in the only
  100. // keymaster version released with unversioned keys, is chosen randomly. In order for an
  101. // unversioned key to parse as a version 0 key, the following must be true about the first
  102. // five of those random bytes:
  103. //
  104. // 1. The first byte must be zero. This will happen with probability 1/2^8.
  105. //
  106. // 2. The second through fifth bytes must contain an unsigned integer value equal to
  107. // NONCE_LENGTH. This will happen with probability 1/2^32.
  108. //
  109. // Based on those two checks alone, the probability of interpreting an unversioned blob as a
  110. // version 0 blob is 1/2^40. That's small enough to be negligible, but there are additional
  111. // checks which lower it further.
  112. LOG_D("Failed to deserialize versioned key blob. Assuming unversioned.", 0);
  113. return DeserializeUnversionedBlob(key_blob, encrypted_key_material, hw_enforced,
  114. sw_enforced, nonce, tag);
  115. }
  116. return KM_ERROR_OK;
  117. }
  118. } // namespace keymaster