f2fs_crypto.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. char ci_master_key[F2FS_KEY_DESCRIPTOR_SIZE];
  73. };
  74. #define F2FS_CTX_REQUIRES_FREE_ENCRYPT_FL 0x00000001
  75. #define F2FS_WRITE_PATH_FL 0x00000002
  76. struct f2fs_crypto_ctx {
  77. union {
  78. struct {
  79. struct page *bounce_page; /* Ciphertext page */
  80. struct page *control_page; /* Original page */
  81. } w;
  82. struct {
  83. struct bio *bio;
  84. struct work_struct work;
  85. } r;
  86. struct list_head free_list; /* Free list */
  87. };
  88. char flags; /* Flags */
  89. };
  90. struct f2fs_completion_result {
  91. struct completion completion;
  92. int res;
  93. };
  94. #define DECLARE_F2FS_COMPLETION_RESULT(ecr) \
  95. struct f2fs_completion_result ecr = { \
  96. COMPLETION_INITIALIZER((ecr).completion), 0 }
  97. static inline int f2fs_encryption_key_size(int mode)
  98. {
  99. switch (mode) {
  100. case F2FS_ENCRYPTION_MODE_AES_256_XTS:
  101. return F2FS_AES_256_XTS_KEY_SIZE;
  102. case F2FS_ENCRYPTION_MODE_AES_256_GCM:
  103. return F2FS_AES_256_GCM_KEY_SIZE;
  104. case F2FS_ENCRYPTION_MODE_AES_256_CBC:
  105. return F2FS_AES_256_CBC_KEY_SIZE;
  106. case F2FS_ENCRYPTION_MODE_AES_256_CTS:
  107. return F2FS_AES_256_CTS_KEY_SIZE;
  108. default:
  109. BUG();
  110. }
  111. return 0;
  112. }
  113. #define F2FS_FNAME_NUM_SCATTER_ENTRIES 4
  114. #define F2FS_CRYPTO_BLOCK_SIZE 16
  115. #define F2FS_FNAME_CRYPTO_DIGEST_SIZE 32
  116. /**
  117. * For encrypted symlinks, the ciphertext length is stored at the beginning
  118. * of the string in little-endian format.
  119. */
  120. struct f2fs_encrypted_symlink_data {
  121. __le16 len;
  122. char encrypted_path[1];
  123. } __attribute__((__packed__));
  124. /**
  125. * This function is used to calculate the disk space required to
  126. * store a filename of length l in encrypted symlink format.
  127. */
  128. static inline u32 encrypted_symlink_data_len(u32 l)
  129. {
  130. return (l + sizeof(struct f2fs_encrypted_symlink_data) - 1);
  131. }
  132. #endif /* _F2FS_CRYPTO_H */