integrity.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2019 Google LLC
  4. */
  5. #include <crypto/sha.h>
  6. #include <crypto/hash.h>
  7. #include <linux/err.h>
  8. #include <linux/version.h>
  9. #include "integrity.h"
  10. struct incfs_hash_alg *incfs_get_hash_alg(enum incfs_hash_tree_algorithm id)
  11. {
  12. static struct incfs_hash_alg sha256 = {
  13. .name = "sha256",
  14. .digest_size = SHA256_DIGEST_SIZE,
  15. .id = INCFS_HASH_TREE_SHA256
  16. };
  17. struct incfs_hash_alg *result = NULL;
  18. struct crypto_shash *shash;
  19. if (id == INCFS_HASH_TREE_SHA256) {
  20. BUILD_BUG_ON(INCFS_MAX_HASH_SIZE < SHA256_DIGEST_SIZE);
  21. result = &sha256;
  22. }
  23. if (result == NULL)
  24. return ERR_PTR(-ENOENT);
  25. /* pairs with cmpxchg_release() below */
  26. shash = smp_load_acquire(&result->shash);
  27. if (shash)
  28. return result;
  29. shash = crypto_alloc_shash(result->name, 0, 0);
  30. if (IS_ERR(shash)) {
  31. int err = PTR_ERR(shash);
  32. pr_err("Can't allocate hash alg %s, error code:%d",
  33. result->name, err);
  34. return ERR_PTR(err);
  35. }
  36. /* pairs with smp_load_acquire() above */
  37. if (cmpxchg_release(&result->shash, NULL, shash) != NULL)
  38. crypto_free_shash(shash);
  39. return result;
  40. }
  41. struct signature_info {
  42. u32 version;
  43. enum incfs_hash_tree_algorithm hash_algorithm;
  44. u8 log2_blocksize;
  45. struct mem_range salt;
  46. struct mem_range root_hash;
  47. };
  48. static bool read_u32(u8 **p, u8 *top, u32 *result)
  49. {
  50. if (*p + sizeof(u32) > top)
  51. return false;
  52. *result = le32_to_cpu(*(__le32 *)*p);
  53. *p += sizeof(u32);
  54. return true;
  55. }
  56. static bool read_u8(u8 **p, u8 *top, u8 *result)
  57. {
  58. if (*p + sizeof(u8) > top)
  59. return false;
  60. *result = *(u8 *)*p;
  61. *p += sizeof(u8);
  62. return true;
  63. }
  64. static bool read_mem_range(u8 **p, u8 *top, struct mem_range *range)
  65. {
  66. u32 len;
  67. if (!read_u32(p, top, &len) || *p + len > top)
  68. return false;
  69. range->len = len;
  70. range->data = *p;
  71. *p += len;
  72. return true;
  73. }
  74. static int incfs_parse_signature(struct mem_range signature,
  75. struct signature_info *si)
  76. {
  77. u8 *p = signature.data;
  78. u8 *top = signature.data + signature.len;
  79. u32 hash_section_size;
  80. if (signature.len > INCFS_MAX_SIGNATURE_SIZE)
  81. return -EINVAL;
  82. if (!read_u32(&p, top, &si->version) ||
  83. si->version != INCFS_SIGNATURE_VERSION)
  84. return -EINVAL;
  85. if (!read_u32(&p, top, &hash_section_size) ||
  86. p + hash_section_size > top)
  87. return -EINVAL;
  88. top = p + hash_section_size;
  89. if (!read_u32(&p, top, &si->hash_algorithm) ||
  90. si->hash_algorithm != INCFS_HASH_TREE_SHA256)
  91. return -EINVAL;
  92. if (!read_u8(&p, top, &si->log2_blocksize) || si->log2_blocksize != 12)
  93. return -EINVAL;
  94. if (!read_mem_range(&p, top, &si->salt))
  95. return -EINVAL;
  96. if (!read_mem_range(&p, top, &si->root_hash))
  97. return -EINVAL;
  98. if (p != top)
  99. return -EINVAL;
  100. return 0;
  101. }
  102. struct mtree *incfs_alloc_mtree(struct mem_range signature,
  103. int data_block_count)
  104. {
  105. int error;
  106. struct signature_info si;
  107. struct mtree *result = NULL;
  108. struct incfs_hash_alg *hash_alg = NULL;
  109. int hash_per_block;
  110. int lvl;
  111. int total_blocks = 0;
  112. int blocks_in_level[INCFS_MAX_MTREE_LEVELS];
  113. int blocks = data_block_count;
  114. if (data_block_count <= 0)
  115. return ERR_PTR(-EINVAL);
  116. error = incfs_parse_signature(signature, &si);
  117. if (error)
  118. return ERR_PTR(error);
  119. hash_alg = incfs_get_hash_alg(si.hash_algorithm);
  120. if (IS_ERR(hash_alg))
  121. return ERR_PTR(PTR_ERR(hash_alg));
  122. if (si.root_hash.len < hash_alg->digest_size)
  123. return ERR_PTR(-EINVAL);
  124. result = kzalloc(sizeof(*result), GFP_NOFS);
  125. if (!result)
  126. return ERR_PTR(-ENOMEM);
  127. result->alg = hash_alg;
  128. hash_per_block = INCFS_DATA_FILE_BLOCK_SIZE / result->alg->digest_size;
  129. /* Calculating tree geometry. */
  130. /* First pass: calculate how many blocks in each tree level. */
  131. for (lvl = 0; blocks > 1; lvl++) {
  132. if (lvl >= INCFS_MAX_MTREE_LEVELS) {
  133. pr_err("incfs: too much data in mtree");
  134. goto err;
  135. }
  136. blocks = (blocks + hash_per_block - 1) / hash_per_block;
  137. blocks_in_level[lvl] = blocks;
  138. total_blocks += blocks;
  139. }
  140. result->depth = lvl;
  141. result->hash_tree_area_size = total_blocks * INCFS_DATA_FILE_BLOCK_SIZE;
  142. if (result->hash_tree_area_size > INCFS_MAX_HASH_AREA_SIZE)
  143. goto err;
  144. blocks = 0;
  145. /* Second pass: calculate offset of each level. 0th level goes last. */
  146. for (lvl = 0; lvl < result->depth; lvl++) {
  147. u32 suboffset;
  148. blocks += blocks_in_level[lvl];
  149. suboffset = (total_blocks - blocks)
  150. * INCFS_DATA_FILE_BLOCK_SIZE;
  151. result->hash_level_suboffset[lvl] = suboffset;
  152. }
  153. /* Root hash is stored separately from the rest of the tree. */
  154. memcpy(result->root_hash, si.root_hash.data, hash_alg->digest_size);
  155. return result;
  156. err:
  157. kfree(result);
  158. return ERR_PTR(-E2BIG);
  159. }
  160. void incfs_free_mtree(struct mtree *tree)
  161. {
  162. kfree(tree);
  163. }
  164. int incfs_calc_digest(struct incfs_hash_alg *alg, struct mem_range data,
  165. struct mem_range digest)
  166. {
  167. SHASH_DESC_ON_STACK(desc, alg->shash);
  168. if (!alg || !alg->shash || !data.data || !digest.data)
  169. return -EFAULT;
  170. if (alg->digest_size > digest.len)
  171. return -EINVAL;
  172. desc->tfm = alg->shash;
  173. if (data.len < INCFS_DATA_FILE_BLOCK_SIZE) {
  174. int err;
  175. void *buf = kzalloc(INCFS_DATA_FILE_BLOCK_SIZE, GFP_NOFS);
  176. if (!buf)
  177. return -ENOMEM;
  178. memcpy(buf, data.data, data.len);
  179. err = crypto_shash_digest(desc, buf, INCFS_DATA_FILE_BLOCK_SIZE,
  180. digest.data);
  181. kfree(buf);
  182. return err;
  183. }
  184. return crypto_shash_digest(desc, data.data, data.len, digest.data);
  185. }