ext4_crypto.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. /* This is passed in from userspace into the kernel keyring */
  60. struct ext4_encryption_key {
  61. __u32 mode;
  62. char raw[EXT4_MAX_KEY_SIZE];
  63. __u32 size;
  64. } __attribute__((__packed__));
  65. struct ext4_crypt_info {
  66. char ci_data_mode;
  67. char ci_filename_mode;
  68. char ci_flags;
  69. struct crypto_ablkcipher *ci_ctfm;
  70. char ci_master_key[EXT4_KEY_DESCRIPTOR_SIZE];
  71. };
  72. #define EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL 0x00000001
  73. #define EXT4_WRITE_PATH_FL 0x00000002
  74. struct ext4_crypto_ctx {
  75. union {
  76. struct {
  77. struct page *bounce_page; /* Ciphertext page */
  78. struct page *control_page; /* Original page */
  79. } w;
  80. struct {
  81. struct bio *bio;
  82. struct work_struct work;
  83. } r;
  84. struct list_head free_list; /* Free list */
  85. };
  86. char flags; /* Flags */
  87. char mode; /* Encryption mode for tfm */
  88. };
  89. struct ext4_completion_result {
  90. struct completion completion;
  91. int res;
  92. };
  93. #define DECLARE_EXT4_COMPLETION_RESULT(ecr) \
  94. struct ext4_completion_result ecr = { \
  95. COMPLETION_INITIALIZER((ecr).completion), 0 }
  96. static inline int ext4_encryption_key_size(int mode)
  97. {
  98. switch (mode) {
  99. case EXT4_ENCRYPTION_MODE_AES_256_XTS:
  100. return EXT4_AES_256_XTS_KEY_SIZE;
  101. case EXT4_ENCRYPTION_MODE_AES_256_GCM:
  102. return EXT4_AES_256_GCM_KEY_SIZE;
  103. case EXT4_ENCRYPTION_MODE_AES_256_CBC:
  104. return EXT4_AES_256_CBC_KEY_SIZE;
  105. case EXT4_ENCRYPTION_MODE_AES_256_CTS:
  106. return EXT4_AES_256_CTS_KEY_SIZE;
  107. default:
  108. BUG();
  109. }
  110. return 0;
  111. }
  112. #define EXT4_FNAME_NUM_SCATTER_ENTRIES 4
  113. #define EXT4_CRYPTO_BLOCK_SIZE 16
  114. #define EXT4_FNAME_CRYPTO_DIGEST_SIZE 32
  115. struct ext4_str {
  116. unsigned char *name;
  117. u32 len;
  118. };
  119. /**
  120. * For encrypted symlinks, the ciphertext length is stored at the beginning
  121. * of the string in little-endian format.
  122. */
  123. struct ext4_encrypted_symlink_data {
  124. __le16 len;
  125. char encrypted_path[1];
  126. } __attribute__((__packed__));
  127. /**
  128. * This function is used to calculate the disk space required to
  129. * store a filename of length l in encrypted symlink format.
  130. */
  131. static inline u32 encrypted_symlink_data_len(u32 l)
  132. {
  133. if (l < EXT4_CRYPTO_BLOCK_SIZE)
  134. l = EXT4_CRYPTO_BLOCK_SIZE;
  135. return (l + sizeof(struct ext4_encrypted_symlink_data) - 1);
  136. }
  137. #endif /* _EXT4_CRYPTO_H */