f2fs_crypto.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * linux/fs/f2fs/f2fs_crypto.h
  3. *
  4. * Copied from linux/fs/ext4/ext4_crypto.h
  5. *
  6. * Copyright (C) 2015, Google, Inc.
  7. *
  8. * This contains encryption header content for f2fs
  9. *
  10. * Written by Michael Halcrow, 2015.
  11. * Modified by Jaegeuk Kim, 2015.
  12. */
  13. #ifndef _F2FS_CRYPTO_H
  14. #define _F2FS_CRYPTO_H
  15. #include <linux/fs.h>
  16. #define F2FS_KEY_DESCRIPTOR_SIZE 8
  17. /* Policy provided via an ioctl on the topmost directory */
  18. struct f2fs_encryption_policy {
  19. char version;
  20. char contents_encryption_mode;
  21. char filenames_encryption_mode;
  22. char flags;
  23. char master_key_descriptor[F2FS_KEY_DESCRIPTOR_SIZE];
  24. } __attribute__((__packed__));
  25. #define F2FS_ENCRYPTION_CONTEXT_FORMAT_V1 1
  26. #define F2FS_KEY_DERIVATION_NONCE_SIZE 16
  27. #define F2FS_POLICY_FLAGS_PAD_4 0x00
  28. #define F2FS_POLICY_FLAGS_PAD_8 0x01
  29. #define F2FS_POLICY_FLAGS_PAD_16 0x02
  30. #define F2FS_POLICY_FLAGS_PAD_32 0x03
  31. #define F2FS_POLICY_FLAGS_PAD_MASK 0x03
  32. #define F2FS_POLICY_FLAGS_VALID 0x03
  33. /**
  34. * Encryption context for inode
  35. *
  36. * Protector format:
  37. * 1 byte: Protector format (1 = this version)
  38. * 1 byte: File contents encryption mode
  39. * 1 byte: File names encryption mode
  40. * 1 byte: Flags
  41. * 8 bytes: Master Key descriptor
  42. * 16 bytes: Encryption Key derivation nonce
  43. */
  44. struct f2fs_encryption_context {
  45. char format;
  46. char contents_encryption_mode;
  47. char filenames_encryption_mode;
  48. char flags;
  49. char master_key_descriptor[F2FS_KEY_DESCRIPTOR_SIZE];
  50. char nonce[F2FS_KEY_DERIVATION_NONCE_SIZE];
  51. } __attribute__((__packed__));
  52. /* Encryption parameters */
  53. #define F2FS_XTS_TWEAK_SIZE 16
  54. #define F2FS_AES_128_ECB_KEY_SIZE 16
  55. #define F2FS_AES_256_GCM_KEY_SIZE 32
  56. #define F2FS_AES_256_CBC_KEY_SIZE 32
  57. #define F2FS_AES_256_CTS_KEY_SIZE 32
  58. #define F2FS_AES_256_XTS_KEY_SIZE 64
  59. #define F2FS_MAX_KEY_SIZE 64
  60. #define F2FS_KEY_DESC_PREFIX "f2fs:"
  61. #define F2FS_KEY_DESC_PREFIX_SIZE 5
  62. struct f2fs_encryption_key {
  63. __u32 mode;
  64. char raw[F2FS_MAX_KEY_SIZE];
  65. __u32 size;
  66. } __attribute__((__packed__));
  67. struct f2fs_crypt_info {
  68. char ci_data_mode;
  69. char ci_filename_mode;
  70. char ci_flags;
  71. struct crypto_ablkcipher *ci_ctfm;
  72. struct key *ci_keyring_key;
  73. char ci_master_key[F2FS_KEY_DESCRIPTOR_SIZE];
  74. };
  75. #define F2FS_CTX_REQUIRES_FREE_ENCRYPT_FL 0x00000001
  76. #define F2FS_WRITE_PATH_FL 0x00000002
  77. struct f2fs_crypto_ctx {
  78. union {
  79. struct {
  80. struct page *bounce_page; /* Ciphertext page */
  81. struct page *control_page; /* Original page */
  82. } w;
  83. struct {
  84. struct bio *bio;
  85. struct work_struct work;
  86. } r;
  87. struct list_head free_list; /* Free list */
  88. };
  89. char flags; /* Flags */
  90. };
  91. struct f2fs_completion_result {
  92. struct completion completion;
  93. int res;
  94. };
  95. #define DECLARE_F2FS_COMPLETION_RESULT(ecr) \
  96. struct f2fs_completion_result ecr = { \
  97. COMPLETION_INITIALIZER((ecr).completion), 0 }
  98. static inline int f2fs_encryption_key_size(int mode)
  99. {
  100. switch (mode) {
  101. case F2FS_ENCRYPTION_MODE_AES_256_XTS:
  102. return F2FS_AES_256_XTS_KEY_SIZE;
  103. case F2FS_ENCRYPTION_MODE_AES_256_GCM:
  104. return F2FS_AES_256_GCM_KEY_SIZE;
  105. case F2FS_ENCRYPTION_MODE_AES_256_CBC:
  106. return F2FS_AES_256_CBC_KEY_SIZE;
  107. case F2FS_ENCRYPTION_MODE_AES_256_CTS:
  108. return F2FS_AES_256_CTS_KEY_SIZE;
  109. default:
  110. BUG();
  111. }
  112. return 0;
  113. }
  114. #define F2FS_FNAME_NUM_SCATTER_ENTRIES 4
  115. #define F2FS_CRYPTO_BLOCK_SIZE 16
  116. #define F2FS_FNAME_CRYPTO_DIGEST_SIZE 32
  117. /**
  118. * For encrypted symlinks, the ciphertext length is stored at the beginning
  119. * of the string in little-endian format.
  120. */
  121. struct f2fs_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. return (l + sizeof(struct f2fs_encrypted_symlink_data) - 1);
  132. }
  133. #endif /* _F2FS_CRYPTO_H */