You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pccrc.h 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. #ifndef _IPXE_PCCRC_H
  2. #define _IPXE_PCCRC_H
  3. /** @file
  4. *
  5. * Peer Content Caching and Retrieval: Content Identification [MS-PCCRC]
  6. *
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  9. #include <stdint.h>
  10. #include <byteswap.h>
  11. #include <ipxe/uaccess.h>
  12. #include <ipxe/crypto.h>
  13. /******************************************************************************
  14. *
  15. * Content Information versioning
  16. *
  17. ******************************************************************************
  18. *
  19. * Note that version 1 data structures are little-endian, but version
  20. * 2 data structures are big-endian.
  21. */
  22. /** Content Information version number */
  23. union peerdist_info_version {
  24. /** Raw version number
  25. *
  26. * Always little-endian, regardless of whether the
  27. * encompassing structure is version 1 (little-endian) or
  28. * version 2 (big-endian).
  29. */
  30. uint16_t raw;
  31. /** Major:minor version number */
  32. struct {
  33. /** Minor version number */
  34. uint8_t minor;
  35. /** Major version number */
  36. uint8_t major;
  37. } __attribute__ (( packed ));
  38. } __attribute__ (( packed ));
  39. /** Content Information version 1 */
  40. #define PEERDIST_INFO_V1 0x0100
  41. /** Content Information version 2 */
  42. #define PEERDIST_INFO_V2 0x0200
  43. /******************************************************************************
  44. *
  45. * Content Information version 1
  46. *
  47. ******************************************************************************
  48. */
  49. /** Content Information version 1 data structure header
  50. *
  51. * All fields are little-endian.
  52. */
  53. struct peerdist_info_v1 {
  54. /** Version number */
  55. union peerdist_info_version version;
  56. /** Hash algorithm
  57. *
  58. * This is a @c PEERDIST_INFO_V1_HASH_XXX constant.
  59. */
  60. uint32_t hash;
  61. /** Length to skip in first segment
  62. *
  63. * Length at the start of the first segment which is not
  64. * included within the content range.
  65. */
  66. uint32_t first;
  67. /** Length to read in last segment, or zero
  68. *
  69. * Length within the last segment which is included within the
  70. * content range. A zero value indicates that the whole of
  71. * the last segment is included within the content range.
  72. */
  73. uint32_t last;
  74. /** Number of segments within the content information */
  75. uint32_t segments;
  76. /* Followed by a variable-length array of segment descriptions
  77. * and a list of variable-length block descriptions:
  78. *
  79. * peerdist_info_v1_segment_t(digestsize) segment[segments];
  80. * peerdist_info_v1_block_t(digestsize, block0.blocks) block0;
  81. * peerdist_info_v1_block_t(digestsize, block1.blocks) block1;
  82. * ...
  83. * peerdist_info_v1_block_t(digestsize, blockN.blocks) blockN;
  84. */
  85. } __attribute__ (( packed ));
  86. /** SHA-256 hash algorithm */
  87. #define PEERDIST_INFO_V1_HASH_SHA256 0x0000800cUL
  88. /** SHA-384 hash algorithm */
  89. #define PEERDIST_INFO_V1_HASH_SHA384 0x0000800dUL
  90. /** SHA-512 hash algorithm */
  91. #define PEERDIST_INFO_V1_HASH_SHA512 0x0000800eUL
  92. /** Content Information version 1 segment description header
  93. *
  94. * All fields are little-endian.
  95. */
  96. struct peerdist_info_v1_segment {
  97. /** Offset of this segment within the content */
  98. uint64_t offset;
  99. /** Length of this segment
  100. *
  101. * Should always be 32MB, except for the last segment within
  102. * the content.
  103. */
  104. uint32_t len;
  105. /** Block size for this segment
  106. *
  107. * Should always be 64kB. Note that the last block within the
  108. * last segment may actually be less than 64kB.
  109. */
  110. uint32_t blksize;
  111. /* Followed by two variable-length hashes:
  112. *
  113. * uint8_t hash[digestsize];
  114. * uint8_t secret[digestsize];
  115. *
  116. * where digestsize is the digest size for the selected hash
  117. * algorithm.
  118. *
  119. * Note that the hash is taken over (the hashes of all blocks
  120. * within) the entire segment, even if the blocks do not
  121. * intersect the content range (and so do not appear within
  122. * the block list). It therefore functions only as a segment
  123. * identifier; it cannot be used to verify the content of the
  124. * segment (since we may not download all blocks within the
  125. * segment).
  126. */
  127. } __attribute__ (( packed ));
  128. /** Content Information version 1 segment description
  129. *
  130. * @v digestsize Digest size
  131. */
  132. #define peerdist_info_v1_segment_t( digestsize ) \
  133. struct { \
  134. struct peerdist_info_v1_segment segment; \
  135. uint8_t hash[digestsize]; \
  136. uint8_t secret[digestsize]; \
  137. } __attribute__ (( packed ))
  138. /** Content Information version 1 block description header
  139. *
  140. * All fields are little-endian.
  141. */
  142. struct peerdist_info_v1_block {
  143. /** Number of blocks within the block description
  144. *
  145. * This is the number of blocks within the segment which
  146. * overlap the content range. It may therefore be less than
  147. * the number of blocks within the segment.
  148. */
  149. uint32_t blocks;
  150. /* Followed by an array of variable-length hashes:
  151. *
  152. * uint8_t hash[blocks][digestsize];
  153. *
  154. * where digestsize is the digest size for the selected hash
  155. * algorithm.
  156. */
  157. } __attribute__ (( packed ));
  158. /** Content Information version 1 block description
  159. *
  160. * @v digestsize Digest size
  161. * @v blocks Number of blocks
  162. */
  163. #define peerdist_info_v1_block_t( digestsize, blocks ) \
  164. struct { \
  165. struct peerdist_info_v1_block block; \
  166. uint8_t hash[blocks][digestsize]; \
  167. } __attribute__ (( packed ))
  168. /******************************************************************************
  169. *
  170. * Content Information version 2
  171. *
  172. ******************************************************************************
  173. */
  174. /** Content Information version 2 data structure header
  175. *
  176. * All fields are big-endian.
  177. */
  178. struct peerdist_info_v2 {
  179. /** Version number */
  180. union peerdist_info_version version;
  181. /** Hash algorithm
  182. *
  183. * This is a @c PEERDIST_INFO_V2_HASH_XXX constant.
  184. */
  185. uint8_t hash;
  186. /** Offset of the first segment within the content */
  187. uint64_t offset;
  188. /** Index of the first segment within the content */
  189. uint64_t index;
  190. /** Length to skip in first segment
  191. *
  192. * Length at the start of the first segment which is not
  193. * included within the content range.
  194. */
  195. uint32_t first;
  196. /** Length of content range, or zero
  197. *
  198. * Length of the content range. A zero indicates that
  199. * everything up to the end of the last segment is included in
  200. * the content range.
  201. */
  202. uint64_t len;
  203. /* Followed by a list of chunk descriptions */
  204. } __attribute__ (( packed ));
  205. /** SHA-512 hash algorithm with output truncated to first 256 bits */
  206. #define PEERDIST_INFO_V2_HASH_SHA512_TRUNC 0x04
  207. /** Content Information version 2 chunk description header
  208. *
  209. * All fields are big-endian.
  210. */
  211. struct peerdist_info_v2_chunk {
  212. /** Chunk type */
  213. uint8_t type;
  214. /** Chunk data length */
  215. uint32_t len;
  216. /* Followed by an array of segment descriptions:
  217. *
  218. * peerdist_info_v2_segment_t(digestsize) segment[segments]
  219. *
  220. * where digestsize is the digest size for the selected hash
  221. * algorithm, and segments is equal to @c len divided by the
  222. * size of each segment array entry.
  223. */
  224. } __attribute__ (( packed ));
  225. /** Content Information version 2 chunk description
  226. *
  227. * @v digestsize Digest size
  228. */
  229. #define peerdist_info_v2_chunk_t( digestsize ) \
  230. struct { \
  231. struct peerdist_info_v2_chunk chunk; \
  232. peerdist_info_v2_segment_t ( digestsize ) segment[0]; \
  233. } __attribute__ (( packed ))
  234. /** Chunk type */
  235. #define PEERDIST_INFO_V2_CHUNK_TYPE 0x00
  236. /** Content Information version 2 segment description header
  237. *
  238. * All fields are big-endian.
  239. */
  240. struct peerdist_info_v2_segment {
  241. /** Segment length */
  242. uint32_t len;
  243. /* Followed by two variable-length hashes:
  244. *
  245. * uint8_t hash[digestsize];
  246. * uint8_t secret[digestsize];
  247. *
  248. * where digestsize is the digest size for the selected hash
  249. * algorithm.
  250. */
  251. } __attribute__ (( packed ));
  252. /** Content Information version 2 segment description
  253. *
  254. * @v digestsize Digest size
  255. */
  256. #define peerdist_info_v2_segment_t( digestsize ) \
  257. struct { \
  258. struct peerdist_info_v2_segment segment; \
  259. uint8_t hash[digestsize]; \
  260. uint8_t secret[digestsize]; \
  261. } __attribute__ (( packed ))
  262. /******************************************************************************
  263. *
  264. * Content Information
  265. *
  266. ******************************************************************************
  267. */
  268. /** Maximum digest size for any supported algorithm
  269. *
  270. * The largest digest size that we support is for SHA-512 at 64 bytes
  271. */
  272. #define PEERDIST_DIGEST_MAX_SIZE 64
  273. /** Raw content information */
  274. struct peerdist_raw {
  275. /** Data buffer */
  276. userptr_t data;
  277. /** Length of data buffer */
  278. size_t len;
  279. };
  280. /** A content range */
  281. struct peerdist_range {
  282. /** Start offset */
  283. size_t start;
  284. /** End offset */
  285. size_t end;
  286. };
  287. /** Content information */
  288. struct peerdist_info {
  289. /** Raw content information */
  290. struct peerdist_raw raw;
  291. /** Content information operations */
  292. struct peerdist_info_operations *op;
  293. /** Digest algorithm */
  294. struct digest_algorithm *digest;
  295. /** Digest size
  296. *
  297. * Note that this may be shorter than the digest size of the
  298. * digest algorithm. The truncation does not always take
  299. * place as soon as a digest is calculated. For example,
  300. * version 2 content information uses SHA-512 with a truncated
  301. * digest size of 32 (256 bits), but the segment identifier
  302. * ("HoHoDk") is calculated by using HMAC with the full
  303. * SHA-512 digest and then truncating the HMAC output, rather
  304. * than by simply using HMAC with the truncated SHA-512
  305. * digest. This is, of course, totally undocumented.
  306. */
  307. size_t digestsize;
  308. /** Content range */
  309. struct peerdist_range range;
  310. /** Trimmed content range */
  311. struct peerdist_range trim;
  312. /** Number of segments within the content information */
  313. unsigned int segments;
  314. };
  315. /** A content information segment */
  316. struct peerdist_info_segment {
  317. /** Content information */
  318. const struct peerdist_info *info;
  319. /** Segment index */
  320. unsigned int index;
  321. /** Content range
  322. *
  323. * Note that this range may exceed the overall content range.
  324. */
  325. struct peerdist_range range;
  326. /** Number of blocks within this segment */
  327. unsigned int blocks;
  328. /** Block size */
  329. size_t blksize;
  330. /** Segment hash of data
  331. *
  332. * This is MS-PCCRC's "HoD".
  333. */
  334. uint8_t hash[PEERDIST_DIGEST_MAX_SIZE];
  335. /** Segment secret
  336. *
  337. * This is MS-PCCRC's "Ke = Kp".
  338. */
  339. uint8_t secret[PEERDIST_DIGEST_MAX_SIZE];
  340. /** Segment identifier
  341. *
  342. * This is MS-PCCRC's "HoHoDk".
  343. */
  344. uint8_t id[PEERDIST_DIGEST_MAX_SIZE];
  345. };
  346. /** Magic string constant used to calculate segment identifier
  347. *
  348. * Note that the MS-PCCRC specification states that this constant is
  349. *
  350. * "the null-terminated ASCII string constant "MS_P2P_CACHING";
  351. * string literals are all ASCII strings with NULL terminators
  352. * unless otherwise noted."
  353. *
  354. * The specification lies. This constant is a UTF-16LE string, not an
  355. * ASCII string. The terminating wNUL *is* included within the
  356. * constant.
  357. */
  358. #define PEERDIST_SEGMENT_ID_MAGIC L"MS_P2P_CACHING"
  359. /** A content information block */
  360. struct peerdist_info_block {
  361. /** Content information segment */
  362. const struct peerdist_info_segment *segment;
  363. /** Block index */
  364. unsigned int index;
  365. /** Content range
  366. *
  367. * Note that this range may exceed the overall content range.
  368. */
  369. struct peerdist_range range;
  370. /** Trimmed content range */
  371. struct peerdist_range trim;
  372. /** Block hash */
  373. uint8_t hash[PEERDIST_DIGEST_MAX_SIZE];
  374. };
  375. /** Content information operations */
  376. struct peerdist_info_operations {
  377. /**
  378. * Populate content information
  379. *
  380. * @v info Content information to fill in
  381. * @ret rc Return status code
  382. */
  383. int ( * info ) ( struct peerdist_info *info );
  384. /**
  385. * Populate content information segment
  386. *
  387. * @v segment Content information segment to fill in
  388. * @ret rc Return status code
  389. */
  390. int ( * segment ) ( struct peerdist_info_segment *segment );
  391. /**
  392. * Populate content information block
  393. *
  394. * @v block Content information block to fill in
  395. * @ret rc Return status code
  396. */
  397. int ( * block ) ( struct peerdist_info_block *block );
  398. };
  399. extern struct digest_algorithm sha512_trunc_algorithm;
  400. extern int peerdist_info ( userptr_t data, size_t len,
  401. struct peerdist_info *info );
  402. extern int peerdist_info_segment ( const struct peerdist_info *info,
  403. struct peerdist_info_segment *segment,
  404. unsigned int index );
  405. extern int peerdist_info_block ( const struct peerdist_info_segment *segment,
  406. struct peerdist_info_block *block,
  407. unsigned int index );
  408. #endif /* _IPXE_PCCRC_H */