ext4_crypto.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * linux/fs/ext4/ext4_crypto.h
  3. *
  4. * Copyright (C) 2015, Google, Inc.
  5. *
  6. * This contains encryption header content for ext4
  7. *
  8. * Written by Michael Halcrow, 2015.
  9. */
  10. #ifndef _EXT4_CRYPTO_H
  11. #define _EXT4_CRYPTO_H
  12. #include <linux/fs.h>
  13. #define EXT4_KEY_DESCRIPTOR_SIZE 8
  14. /* Policy provided via an ioctl on the topmost directory */
  15. struct ext4_encryption_policy {
  16. char version;
  17. char contents_encryption_mode;
  18. char filenames_encryption_mode;
  19. char flags;
  20. char master_key_descriptor[EXT4_KEY_DESCRIPTOR_SIZE];
  21. } __attribute__((__packed__));
  22. #define EXT4_ENCRYPTION_CONTEXT_FORMAT_V1 1
  23. #define EXT4_KEY_DERIVATION_NONCE_SIZE 16
  24. #define EXT4_POLICY_FLAGS_PAD_4 0x00
  25. #define EXT4_POLICY_FLAGS_PAD_8 0x01
  26. #define EXT4_POLICY_FLAGS_PAD_16 0x02
  27. #define EXT4_POLICY_FLAGS_PAD_32 0x03
  28. #define EXT4_POLICY_FLAGS_PAD_MASK 0x03
  29. #define EXT4_POLICY_FLAGS_VALID 0x03
  30. /**
  31. * Encryption context for inode
  32. *
  33. * Protector format:
  34. * 1 byte: Protector format (1 = this version)
  35. * 1 byte: File contents encryption mode
  36. * 1 byte: File names encryption mode
  37. * 1 byte: Reserved
  38. * 8 bytes: Master Key descriptor
  39. * 16 bytes: Encryption Key derivation nonce
  40. */
  41. struct ext4_encryption_context {
  42. char format;
  43. char contents_encryption_mode;
  44. char filenames_encryption_mode;
  45. char flags;
  46. char master_key_descriptor[EXT4_KEY_DESCRIPTOR_SIZE];
  47. char nonce[EXT4_KEY_DERIVATION_NONCE_SIZE];
  48. } __attribute__((__packed__));
  49. /* Encryption parameters */
  50. #define EXT4_XTS_TWEAK_SIZE 16
  51. #define EXT4_AES_128_ECB_KEY_SIZE 16
  52. #define EXT4_AES_256_GCM_KEY_SIZE 32
  53. #define EXT4_AES_256_CBC_KEY_SIZE 32
  54. #define EXT4_AES_256_CTS_KEY_SIZE 32
  55. #define EXT4_AES_256_XTS_KEY_SIZE 64
  56. #define EXT4_MAX_KEY_SIZE 64
  57. #define EXT4_KEY_DESC_PREFIX "ext4:"
  58. #define EXT4_KEY_DESC_PREFIX_SIZE 5
  59. struct ext4_encryption_key {
  60. uint32_t mode;
  61. char raw[EXT4_MAX_KEY_SIZE];
  62. uint32_t size;
  63. };
  64. #define EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL 0x00000001
  65. #define EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL 0x00000002
  66. struct ext4_crypto_ctx {
  67. struct crypto_tfm *tfm; /* Crypto API context */
  68. struct page *bounce_page; /* Ciphertext page on write path */
  69. struct page *control_page; /* Original page on write path */
  70. struct bio *bio; /* The bio for this context */
  71. struct work_struct work; /* Work queue for read complete path */
  72. struct list_head free_list; /* Free list */
  73. int flags; /* Flags */
  74. int mode; /* Encryption mode for tfm */
  75. };
  76. struct ext4_completion_result {
  77. struct completion completion;
  78. int res;
  79. };
  80. #define DECLARE_EXT4_COMPLETION_RESULT(ecr) \
  81. struct ext4_completion_result ecr = { \
  82. COMPLETION_INITIALIZER((ecr).completion), 0 }
  83. static inline int ext4_encryption_key_size(int mode)
  84. {
  85. switch (mode) {
  86. case EXT4_ENCRYPTION_MODE_AES_256_XTS:
  87. return EXT4_AES_256_XTS_KEY_SIZE;
  88. case EXT4_ENCRYPTION_MODE_AES_256_GCM:
  89. return EXT4_AES_256_GCM_KEY_SIZE;
  90. case EXT4_ENCRYPTION_MODE_AES_256_CBC:
  91. return EXT4_AES_256_CBC_KEY_SIZE;
  92. case EXT4_ENCRYPTION_MODE_AES_256_CTS:
  93. return EXT4_AES_256_CTS_KEY_SIZE;
  94. default:
  95. BUG();
  96. }
  97. return 0;
  98. }
  99. #define EXT4_FNAME_NUM_SCATTER_ENTRIES 4
  100. #define EXT4_CRYPTO_BLOCK_SIZE 16
  101. #define EXT4_FNAME_CRYPTO_DIGEST_SIZE 32
  102. struct ext4_str {
  103. unsigned char *name;
  104. u32 len;
  105. };
  106. struct ext4_fname_crypto_ctx {
  107. u32 lim;
  108. char tmp_buf[EXT4_CRYPTO_BLOCK_SIZE];
  109. struct crypto_ablkcipher *ctfm;
  110. struct crypto_hash *htfm;
  111. struct page *workpage;
  112. struct ext4_encryption_key key;
  113. unsigned flags : 8;
  114. unsigned has_valid_key : 1;
  115. unsigned ctfm_key_is_ready : 1;
  116. };
  117. /**
  118. * For encrypted symlinks, the ciphertext length is stored at the beginning
  119. * of the string in little-endian format.
  120. */
  121. struct ext4_encrypted_symlink_data {
  122. __le16 len;
  123. char encrypted_path[1];
  124. } __attribute__((__packed__));
  125. /**
  126. * This function is used to calculate the disk space required to
  127. * store a filename of length l in encrypted symlink format.
  128. */
  129. static inline u32 encrypted_symlink_data_len(u32 l)
  130. {
  131. if (l < EXT4_CRYPTO_BLOCK_SIZE)
  132. l = EXT4_CRYPTO_BLOCK_SIZE;
  133. return (l + sizeof(struct ext4_encrypted_symlink_data) - 1);
  134. }
  135. #endif /* _EXT4_CRYPTO_H */