Re: [f2fs-dev] [PATCH 01/10] fs crypto: add basic definitions for per-file encryption

2016-03-11 Thread Jaegeuk Kim
Hi Dan,

On Thu, Mar 10, 2016 at 09:00:25PM -0800, Dan Williams wrote:
> On Mon, Feb 29, 2016 at 5:35 PM, Jaegeuk Kim  wrote:
> > On Sun, Feb 28, 2016 at 09:41:22PM -0800, Randy Dunlap wrote:
> >> On 02/25/16 11:25, Jaegeuk Kim wrote:
> >> > This patch adds definitions for per-file encryption used by ext4 and 
> >> > f2fs.
> >> >
> >> > Signed-off-by: Jaegeuk Kim 
> >> > ---
> >> >  include/linux/fs.h   |   8 ++
> >> >  include/linux/fscrypto.h | 239 
> >> > +++
> >> >  include/uapi/linux/fs.h  |  18 
> >> >  3 files changed, 265 insertions(+)
> >> >  create mode 100644 include/linux/fscrypto.h
> >> >
> >> > diff --git a/include/linux/fs.h b/include/linux/fs.h
> >> > index ae68100..d8f57cf 100644
> >> > --- a/include/linux/fs.h
> >> > +++ b/include/linux/fs.h
> >> > @@ -53,6 +53,8 @@ struct swap_info_struct;
> >> >  struct seq_file;
> >> >  struct workqueue_struct;
> >> >  struct iov_iter;
> >> > +struct fscrypt_info;
> >> > +struct fscrypt_operations;
> >> >
> >> >  extern void __init inode_init(void);
> >> >  extern void __init inode_init_early(void);
> >> > @@ -678,6 +680,10 @@ struct inode {
> >> > struct hlist_head   i_fsnotify_marks;
> >> >  #endif
> >> >
> >> > +#ifdef CONFIG_FS_ENCRYPTION
> >> > +   struct fscrypt_info *i_crypt_info;
> >> > +#endif
> >> > +
> >> > void*i_private; /* fs or device private pointer 
> >> > */
> >> >  };
> >> >
> >> > @@ -1323,6 +1329,8 @@ struct super_block {
> >> >  #endif
> >> > const struct xattr_handler **s_xattr;
> >> >
> >> > +   const struct fscrypt_operations *s_cop;
> >> > +
> >> > struct hlist_bl_heads_anon; /* anonymous dentries for 
> >> > (nfs) exporting */
> >> > struct list_heads_mounts;   /* list of mounts; _not_ for 
> >> > fs use */
> >> > struct block_device *s_bdev;
> >> > diff --git a/include/linux/fscrypto.h b/include/linux/fscrypto.h
> >> > new file mode 100644
> >> > index 000..b0aed92
> >> > --- /dev/null
> >> > +++ b/include/linux/fscrypto.h
> >> > @@ -0,0 +1,239 @@
> >> > +/*
> >> > + * General per-file encryption definition
> >> > + *
> >> > + * Copyright (C) 2015, Google, Inc.
> >> > + *
> >> > + * Written by Michael Halcrow, 2015.
> >> > + * Modified by Jaegeuk Kim, 2015.
> >> > + */
> >> > +
> >> > +#ifndef _LINUX_FSCRYPTO_H
> >> > +#define _LINUX_FSCRYPTO_H
> >> > +
> >> > +#include 
> >> > +#include 
> >> > +#include 
> >> > +#include 
> >> > +#include 
> >> > +#include 
> >> > +
> >> > +#define FS_KEY_DERIVATION_NONCE_SIZE   16
> >> > +#define FS_ENCRYPTION_CONTEXT_FORMAT_V11
> >> > +
> >> > +#define FS_POLICY_FLAGS_PAD_4  0x00
> >> > +#define FS_POLICY_FLAGS_PAD_8  0x01
> >> > +#define FS_POLICY_FLAGS_PAD_16 0x02
> >> > +#define FS_POLICY_FLAGS_PAD_32 0x03
> >> > +#define FS_POLICY_FLAGS_PAD_MASK   0x03
> >> > +#define FS_POLICY_FLAGS_VALID  0x03
> >> > +
> >> > +/* Encryption algorithms */
> >> > +#define FS_ENCRYPTION_MODE_INVALID 0
> >> > +#define FS_ENCRYPTION_MODE_AES_256_XTS 1
> >> > +#define FS_ENCRYPTION_MODE_AES_256_GCM 2
> >> > +#define FS_ENCRYPTION_MODE_AES_256_CBC 3
> >> > +#define FS_ENCRYPTION_MODE_AES_256_CTS 4
> >> > +
> >> > +/**
> >> > + * Encryption context for inode
> >> > + *
> >> > + * Protector format:
> >> > + *  1 byte: Protector format (1 = this version)
> >> > + *  1 byte: File contents encryption mode
> >> > + *  1 byte: File names encryption mode
> >> > + *  1 byte: Flags
> >> > + *  8 bytes: Master Key descriptor
> >> > + *  16 bytes: Encryption Key derivation nonce
> >> > + */
> >> > +struct fscrypt_context {
> >> > +   char format;
> >> > +   char contents_encryption_mode;
> >> > +   char filenames_encryption_mode;
> >> > +   char flags;
> >> > +   char master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE];
> >> > +   char nonce[FS_KEY_DERIVATION_NONCE_SIZE];
> >>
> >> how about u8 instead of char?
> >
> > It seems that it needs to user u8 instead of char for other variables as 
> > well.
> > I'll take a look at all the usages.
> 
> I think it needs to be __u8 otherwise I get this in a userspace program:
> 
> In file included from test/blk_namespaces.c:17:0:
> /usr/include/linux/fs.h:256:2: error: unknown type name ‘u8’
>   u8 version;
>   ^
> /usr/include/linux/fs.h:257:2: error: unknown type name ‘u8’
>   u8 contents_encryption_mode;
>   ^
> /usr/include/linux/fs.h:258:2: error: unknown type name ‘u8’
>   u8 filenames_encryption_mode;
>   ^
> /usr/include/linux/fs.h:259:2: error: unknown type name ‘u8’
>   u8 flags;
>   ^
> /usr/include/linux/fs.h:260:2: error: unknown type name ‘u8’
>   u8 master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE];
>   ^

I realized that it needs to use __u8 as an exportable data type which can be
seen by user-space programs.

So, IMO, only fscrypt_policy should be 

Re: [f2fs-dev] [PATCH 01/10] fs crypto: add basic definitions for per-file encryption

2016-03-10 Thread Dan Williams
On Mon, Feb 29, 2016 at 5:35 PM, Jaegeuk Kim  wrote:
> On Sun, Feb 28, 2016 at 09:41:22PM -0800, Randy Dunlap wrote:
>> On 02/25/16 11:25, Jaegeuk Kim wrote:
>> > This patch adds definitions for per-file encryption used by ext4 and f2fs.
>> >
>> > Signed-off-by: Jaegeuk Kim 
>> > ---
>> >  include/linux/fs.h   |   8 ++
>> >  include/linux/fscrypto.h | 239 
>> > +++
>> >  include/uapi/linux/fs.h  |  18 
>> >  3 files changed, 265 insertions(+)
>> >  create mode 100644 include/linux/fscrypto.h
>> >
>> > diff --git a/include/linux/fs.h b/include/linux/fs.h
>> > index ae68100..d8f57cf 100644
>> > --- a/include/linux/fs.h
>> > +++ b/include/linux/fs.h
>> > @@ -53,6 +53,8 @@ struct swap_info_struct;
>> >  struct seq_file;
>> >  struct workqueue_struct;
>> >  struct iov_iter;
>> > +struct fscrypt_info;
>> > +struct fscrypt_operations;
>> >
>> >  extern void __init inode_init(void);
>> >  extern void __init inode_init_early(void);
>> > @@ -678,6 +680,10 @@ struct inode {
>> > struct hlist_head   i_fsnotify_marks;
>> >  #endif
>> >
>> > +#ifdef CONFIG_FS_ENCRYPTION
>> > +   struct fscrypt_info *i_crypt_info;
>> > +#endif
>> > +
>> > void*i_private; /* fs or device private pointer */
>> >  };
>> >
>> > @@ -1323,6 +1329,8 @@ struct super_block {
>> >  #endif
>> > const struct xattr_handler **s_xattr;
>> >
>> > +   const struct fscrypt_operations *s_cop;
>> > +
>> > struct hlist_bl_heads_anon; /* anonymous dentries for 
>> > (nfs) exporting */
>> > struct list_heads_mounts;   /* list of mounts; _not_ for 
>> > fs use */
>> > struct block_device *s_bdev;
>> > diff --git a/include/linux/fscrypto.h b/include/linux/fscrypto.h
>> > new file mode 100644
>> > index 000..b0aed92
>> > --- /dev/null
>> > +++ b/include/linux/fscrypto.h
>> > @@ -0,0 +1,239 @@
>> > +/*
>> > + * General per-file encryption definition
>> > + *
>> > + * Copyright (C) 2015, Google, Inc.
>> > + *
>> > + * Written by Michael Halcrow, 2015.
>> > + * Modified by Jaegeuk Kim, 2015.
>> > + */
>> > +
>> > +#ifndef _LINUX_FSCRYPTO_H
>> > +#define _LINUX_FSCRYPTO_H
>> > +
>> > +#include 
>> > +#include 
>> > +#include 
>> > +#include 
>> > +#include 
>> > +#include 
>> > +
>> > +#define FS_KEY_DERIVATION_NONCE_SIZE   16
>> > +#define FS_ENCRYPTION_CONTEXT_FORMAT_V11
>> > +
>> > +#define FS_POLICY_FLAGS_PAD_4  0x00
>> > +#define FS_POLICY_FLAGS_PAD_8  0x01
>> > +#define FS_POLICY_FLAGS_PAD_16 0x02
>> > +#define FS_POLICY_FLAGS_PAD_32 0x03
>> > +#define FS_POLICY_FLAGS_PAD_MASK   0x03
>> > +#define FS_POLICY_FLAGS_VALID  0x03
>> > +
>> > +/* Encryption algorithms */
>> > +#define FS_ENCRYPTION_MODE_INVALID 0
>> > +#define FS_ENCRYPTION_MODE_AES_256_XTS 1
>> > +#define FS_ENCRYPTION_MODE_AES_256_GCM 2
>> > +#define FS_ENCRYPTION_MODE_AES_256_CBC 3
>> > +#define FS_ENCRYPTION_MODE_AES_256_CTS 4
>> > +
>> > +/**
>> > + * Encryption context for inode
>> > + *
>> > + * Protector format:
>> > + *  1 byte: Protector format (1 = this version)
>> > + *  1 byte: File contents encryption mode
>> > + *  1 byte: File names encryption mode
>> > + *  1 byte: Flags
>> > + *  8 bytes: Master Key descriptor
>> > + *  16 bytes: Encryption Key derivation nonce
>> > + */
>> > +struct fscrypt_context {
>> > +   char format;
>> > +   char contents_encryption_mode;
>> > +   char filenames_encryption_mode;
>> > +   char flags;
>> > +   char master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE];
>> > +   char nonce[FS_KEY_DERIVATION_NONCE_SIZE];
>>
>> how about u8 instead of char?
>
> It seems that it needs to user u8 instead of char for other variables as well.
> I'll take a look at all the usages.

I think it needs to be __u8 otherwise I get this in a userspace program:

In file included from test/blk_namespaces.c:17:0:
/usr/include/linux/fs.h:256:2: error: unknown type name ‘u8’
  u8 version;
  ^
/usr/include/linux/fs.h:257:2: error: unknown type name ‘u8’
  u8 contents_encryption_mode;
  ^
/usr/include/linux/fs.h:258:2: error: unknown type name ‘u8’
  u8 filenames_encryption_mode;
  ^
/usr/include/linux/fs.h:259:2: error: unknown type name ‘u8’
  u8 flags;
  ^
/usr/include/linux/fs.h:260:2: error: unknown type name ‘u8’
  u8 master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE];
  ^

--
Transform Data into Opportunity.
Accelerate data analysis in your applications with
Intel Data Analytics Acceleration Library.
Click to learn more.
http://pubads.g.doubleclick.net/gampad/clk?id=278785111=/4140
___
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel


[f2fs-dev] [PATCH 01/10] fs crypto: add basic definitions for per-file encryption

2016-03-02 Thread Jaegeuk Kim
This patch adds definitions for per-file encryption used by ext4 and f2fs.

Signed-off-by: Jaegeuk Kim 
---
 include/linux/fs.h   |   8 ++
 include/linux/fscrypto.h | 239 +++
 include/uapi/linux/fs.h  |  18 
 3 files changed, 265 insertions(+)
 create mode 100644 include/linux/fscrypto.h

diff --git a/include/linux/fs.h b/include/linux/fs.h
index ae68100..28fc121 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -53,6 +53,8 @@ struct swap_info_struct;
 struct seq_file;
 struct workqueue_struct;
 struct iov_iter;
+struct fscrypt_info;
+struct fscrypt_operations;
 
 extern void __init inode_init(void);
 extern void __init inode_init_early(void);
@@ -678,6 +680,10 @@ struct inode {
struct hlist_head   i_fsnotify_marks;
 #endif
 
+#if IS_ENABLED(CONFIG_FS_ENCRYPTION)
+   struct fscrypt_info *i_crypt_info;
+#endif
+
void*i_private; /* fs or device private pointer */
 };
 
@@ -1323,6 +1329,8 @@ struct super_block {
 #endif
const struct xattr_handler **s_xattr;
 
+   const struct fscrypt_operations *s_cop;
+
struct hlist_bl_heads_anon; /* anonymous dentries for (nfs) 
exporting */
struct list_heads_mounts;   /* list of mounts; _not_ for fs 
use */
struct block_device *s_bdev;
diff --git a/include/linux/fscrypto.h b/include/linux/fscrypto.h
new file mode 100644
index 000..4d83f08
--- /dev/null
+++ b/include/linux/fscrypto.h
@@ -0,0 +1,239 @@
+/*
+ * General per-file encryption definition
+ *
+ * Copyright (C) 2015, Google, Inc.
+ *
+ * Written by Michael Halcrow, 2015.
+ * Modified by Jaegeuk Kim, 2015.
+ */
+
+#ifndef _LINUX_FSCRYPTO_H
+#define _LINUX_FSCRYPTO_H
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define FS_KEY_DERIVATION_NONCE_SIZE   16
+#define FS_ENCRYPTION_CONTEXT_FORMAT_V11
+
+#define FS_POLICY_FLAGS_PAD_4  0x00
+#define FS_POLICY_FLAGS_PAD_8  0x01
+#define FS_POLICY_FLAGS_PAD_16 0x02
+#define FS_POLICY_FLAGS_PAD_32 0x03
+#define FS_POLICY_FLAGS_PAD_MASK   0x03
+#define FS_POLICY_FLAGS_VALID  0x03
+
+/* Encryption algorithms */
+#define FS_ENCRYPTION_MODE_INVALID 0
+#define FS_ENCRYPTION_MODE_AES_256_XTS 1
+#define FS_ENCRYPTION_MODE_AES_256_GCM 2
+#define FS_ENCRYPTION_MODE_AES_256_CBC 3
+#define FS_ENCRYPTION_MODE_AES_256_CTS 4
+
+/**
+ * Encryption context for inode
+ *
+ * Protector format:
+ *  1 byte: Protector format (1 = this version)
+ *  1 byte: File contents encryption mode
+ *  1 byte: File names encryption mode
+ *  1 byte: Flags
+ *  8 bytes: Master Key descriptor
+ *  16 bytes: Encryption Key derivation nonce
+ */
+struct fscrypt_context {
+   u8 format;
+   u8 contents_encryption_mode;
+   u8 filenames_encryption_mode;
+   u8 flags;
+   u8 master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE];
+   u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
+} __packed;
+
+/* Encryption parameters */
+#define FS_XTS_TWEAK_SIZE  16
+#define FS_AES_128_ECB_KEY_SIZE16
+#define FS_AES_256_GCM_KEY_SIZE32
+#define FS_AES_256_CBC_KEY_SIZE32
+#define FS_AES_256_CTS_KEY_SIZE32
+#define FS_AES_256_XTS_KEY_SIZE64
+#define FS_MAX_KEY_SIZE64
+
+#define FS_KEY_DESC_PREFIX "fscrypt:"
+#define FS_KEY_DESC_PREFIX_SIZE8
+
+/* This is passed in from userspace into the kernel keyring */
+struct fscrypt_key {
+   u32 mode;
+   u8 raw[FS_MAX_KEY_SIZE];
+   u32 size;
+} __packed;
+
+struct fscrypt_info {
+   u8 ci_data_mode;
+   u8 ci_filename_mode;
+   u8 ci_flags;
+   struct crypto_ablkcipher *ci_ctfm;
+   struct key *ci_keyring_key;
+   u8 ci_master_key[FS_KEY_DESCRIPTOR_SIZE];
+};
+
+#define FS_CTX_REQUIRES_FREE_ENCRYPT_FL0x0001
+#define FS_WRITE_PATH_FL   0x0002
+
+struct fscrypt_ctx {
+   union {
+   struct {
+   struct page *bounce_page;   /* Ciphertext page */
+   struct page *control_page;  /* Original page  */
+   } w;
+   struct {
+   struct bio *bio;
+   struct work_struct work;
+   } r;
+   struct list_head free_list; /* Free list */
+   };
+   u8 flags;   /* Flags */
+   u8 mode;/* Encryption mode for tfm */
+};
+
+struct fscrypt_completion_result {
+   struct completion completion;
+   int res;
+};
+
+#define DECLARE_FS_COMPLETION_RESULT(ecr) \
+   struct fscrypt_completion_result ecr = { \
+   COMPLETION_INITIALIZER((ecr).completion), 0 }
+
+static inline int fscrypt_key_size(int mode)
+{
+   

Re: [f2fs-dev] [PATCH 01/10] fs crypto: add basic definitions for per-file encryption

2016-02-29 Thread Jaegeuk Kim
On Sun, Feb 28, 2016 at 09:41:22PM -0800, Randy Dunlap wrote:
> On 02/25/16 11:25, Jaegeuk Kim wrote:
> > This patch adds definitions for per-file encryption used by ext4 and f2fs.
> > 
> > Signed-off-by: Jaegeuk Kim 
> > ---
> >  include/linux/fs.h   |   8 ++
> >  include/linux/fscrypto.h | 239 
> > +++
> >  include/uapi/linux/fs.h  |  18 
> >  3 files changed, 265 insertions(+)
> >  create mode 100644 include/linux/fscrypto.h
> > 
> > diff --git a/include/linux/fs.h b/include/linux/fs.h
> > index ae68100..d8f57cf 100644
> > --- a/include/linux/fs.h
> > +++ b/include/linux/fs.h
> > @@ -53,6 +53,8 @@ struct swap_info_struct;
> >  struct seq_file;
> >  struct workqueue_struct;
> >  struct iov_iter;
> > +struct fscrypt_info;
> > +struct fscrypt_operations;
> >  
> >  extern void __init inode_init(void);
> >  extern void __init inode_init_early(void);
> > @@ -678,6 +680,10 @@ struct inode {
> > struct hlist_head   i_fsnotify_marks;
> >  #endif
> >  
> > +#ifdef CONFIG_FS_ENCRYPTION
> > +   struct fscrypt_info *i_crypt_info;
> > +#endif
> > +
> > void*i_private; /* fs or device private pointer */
> >  };
> >  
> > @@ -1323,6 +1329,8 @@ struct super_block {
> >  #endif
> > const struct xattr_handler **s_xattr;
> >  
> > +   const struct fscrypt_operations *s_cop;
> > +
> > struct hlist_bl_heads_anon; /* anonymous dentries for (nfs) 
> > exporting */
> > struct list_heads_mounts;   /* list of mounts; _not_ for fs 
> > use */
> > struct block_device *s_bdev;
> > diff --git a/include/linux/fscrypto.h b/include/linux/fscrypto.h
> > new file mode 100644
> > index 000..b0aed92
> > --- /dev/null
> > +++ b/include/linux/fscrypto.h
> > @@ -0,0 +1,239 @@
> > +/*
> > + * General per-file encryption definition
> > + *
> > + * Copyright (C) 2015, Google, Inc.
> > + *
> > + * Written by Michael Halcrow, 2015.
> > + * Modified by Jaegeuk Kim, 2015.
> > + */
> > +
> > +#ifndef _LINUX_FSCRYPTO_H
> > +#define _LINUX_FSCRYPTO_H
> > +
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +
> > +#define FS_KEY_DERIVATION_NONCE_SIZE   16
> > +#define FS_ENCRYPTION_CONTEXT_FORMAT_V11
> > +
> > +#define FS_POLICY_FLAGS_PAD_4  0x00
> > +#define FS_POLICY_FLAGS_PAD_8  0x01
> > +#define FS_POLICY_FLAGS_PAD_16 0x02
> > +#define FS_POLICY_FLAGS_PAD_32 0x03
> > +#define FS_POLICY_FLAGS_PAD_MASK   0x03
> > +#define FS_POLICY_FLAGS_VALID  0x03
> > +
> > +/* Encryption algorithms */
> > +#define FS_ENCRYPTION_MODE_INVALID 0
> > +#define FS_ENCRYPTION_MODE_AES_256_XTS 1
> > +#define FS_ENCRYPTION_MODE_AES_256_GCM 2
> > +#define FS_ENCRYPTION_MODE_AES_256_CBC 3
> > +#define FS_ENCRYPTION_MODE_AES_256_CTS 4
> > +
> > +/**
> > + * Encryption context for inode
> > + *
> > + * Protector format:
> > + *  1 byte: Protector format (1 = this version)
> > + *  1 byte: File contents encryption mode
> > + *  1 byte: File names encryption mode
> > + *  1 byte: Flags
> > + *  8 bytes: Master Key descriptor
> > + *  16 bytes: Encryption Key derivation nonce
> > + */
> > +struct fscrypt_context {
> > +   char format;
> > +   char contents_encryption_mode;
> > +   char filenames_encryption_mode;
> > +   char flags;
> > +   char master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE];
> > +   char nonce[FS_KEY_DERIVATION_NONCE_SIZE];
> 
> how about u8 instead of char?

It seems that it needs to user u8 instead of char for other variables as well.
I'll take a look at all the usages.

Thanks,

> 
> > +} __packed;
> > +
> > +/* Encryption parameters */
> > +#define FS_XTS_TWEAK_SIZE  16
> > +#define FS_AES_128_ECB_KEY_SIZE16
> > +#define FS_AES_256_GCM_KEY_SIZE32
> > +#define FS_AES_256_CBC_KEY_SIZE32
> > +#define FS_AES_256_CTS_KEY_SIZE32
> > +#define FS_AES_256_XTS_KEY_SIZE64
> > +#define FS_MAX_KEY_SIZE64
> > +
> > +#define FS_KEY_DESC_PREFIX "fscrypt:"
> > +#define FS_KEY_DESC_PREFIX_SIZE8
> > +
> > +/* This is passed in from userspace into the kernel keyring */
> > +struct fscrypt_key {
> > +   __u32 mode;
> > +   char raw[FS_MAX_KEY_SIZE];
> > +   __u32 size;
> > +} __packed;
> > +
> > +struct fscrypt_info {
> > +   char ci_data_mode;
> > +   char ci_filename_mode;
> > +   char ci_flags;
> 
> ditto
> 
> > +   struct crypto_ablkcipher *ci_ctfm;
> > +   struct key *ci_keyring_key;
> > +   char ci_master_key[FS_KEY_DESCRIPTOR_SIZE];
> > +};
> > +
> > +#define FS_CTX_REQUIRES_FREE_ENCRYPT_FL0x0001
> > +#define FS_WRITE_PATH_FL   0x0002
> > +
> > +struct fscrypt_ctx {
> > +   union {
> > +   struct {
> > +   struct page *bounce_page;   /* Ciphertext page */
> > + 

Re: [f2fs-dev] [PATCH 01/10] fs crypto: add basic definitions for per-file encryption

2016-02-28 Thread Randy Dunlap
On 02/25/16 11:25, Jaegeuk Kim wrote:
> This patch adds definitions for per-file encryption used by ext4 and f2fs.
> 
> Signed-off-by: Jaegeuk Kim 
> ---
>  include/linux/fs.h   |   8 ++
>  include/linux/fscrypto.h | 239 
> +++
>  include/uapi/linux/fs.h  |  18 
>  3 files changed, 265 insertions(+)
>  create mode 100644 include/linux/fscrypto.h
> 
> diff --git a/include/linux/fs.h b/include/linux/fs.h
> index ae68100..d8f57cf 100644
> --- a/include/linux/fs.h
> +++ b/include/linux/fs.h
> @@ -53,6 +53,8 @@ struct swap_info_struct;
>  struct seq_file;
>  struct workqueue_struct;
>  struct iov_iter;
> +struct fscrypt_info;
> +struct fscrypt_operations;
>  
>  extern void __init inode_init(void);
>  extern void __init inode_init_early(void);
> @@ -678,6 +680,10 @@ struct inode {
>   struct hlist_head   i_fsnotify_marks;
>  #endif
>  
> +#ifdef CONFIG_FS_ENCRYPTION
> + struct fscrypt_info *i_crypt_info;
> +#endif
> +
>   void*i_private; /* fs or device private pointer */
>  };
>  
> @@ -1323,6 +1329,8 @@ struct super_block {
>  #endif
>   const struct xattr_handler **s_xattr;
>  
> + const struct fscrypt_operations *s_cop;
> +
>   struct hlist_bl_heads_anon; /* anonymous dentries for (nfs) 
> exporting */
>   struct list_heads_mounts;   /* list of mounts; _not_ for fs 
> use */
>   struct block_device *s_bdev;
> diff --git a/include/linux/fscrypto.h b/include/linux/fscrypto.h
> new file mode 100644
> index 000..b0aed92
> --- /dev/null
> +++ b/include/linux/fscrypto.h
> @@ -0,0 +1,239 @@
> +/*
> + * General per-file encryption definition
> + *
> + * Copyright (C) 2015, Google, Inc.
> + *
> + * Written by Michael Halcrow, 2015.
> + * Modified by Jaegeuk Kim, 2015.
> + */
> +
> +#ifndef _LINUX_FSCRYPTO_H
> +#define _LINUX_FSCRYPTO_H
> +
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +
> +#define FS_KEY_DERIVATION_NONCE_SIZE 16
> +#define FS_ENCRYPTION_CONTEXT_FORMAT_V1  1
> +
> +#define FS_POLICY_FLAGS_PAD_40x00
> +#define FS_POLICY_FLAGS_PAD_80x01
> +#define FS_POLICY_FLAGS_PAD_16   0x02
> +#define FS_POLICY_FLAGS_PAD_32   0x03
> +#define FS_POLICY_FLAGS_PAD_MASK 0x03
> +#define FS_POLICY_FLAGS_VALID0x03
> +
> +/* Encryption algorithms */
> +#define FS_ENCRYPTION_MODE_INVALID   0
> +#define FS_ENCRYPTION_MODE_AES_256_XTS   1
> +#define FS_ENCRYPTION_MODE_AES_256_GCM   2
> +#define FS_ENCRYPTION_MODE_AES_256_CBC   3
> +#define FS_ENCRYPTION_MODE_AES_256_CTS   4
> +
> +/**
> + * Encryption context for inode
> + *
> + * Protector format:
> + *  1 byte: Protector format (1 = this version)
> + *  1 byte: File contents encryption mode
> + *  1 byte: File names encryption mode
> + *  1 byte: Flags
> + *  8 bytes: Master Key descriptor
> + *  16 bytes: Encryption Key derivation nonce
> + */
> +struct fscrypt_context {
> + char format;
> + char contents_encryption_mode;
> + char filenames_encryption_mode;
> + char flags;
> + char master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE];
> + char nonce[FS_KEY_DERIVATION_NONCE_SIZE];

how about u8 instead of char?

> +} __packed;
> +
> +/* Encryption parameters */
> +#define FS_XTS_TWEAK_SIZE16
> +#define FS_AES_128_ECB_KEY_SIZE  16
> +#define FS_AES_256_GCM_KEY_SIZE  32
> +#define FS_AES_256_CBC_KEY_SIZE  32
> +#define FS_AES_256_CTS_KEY_SIZE  32
> +#define FS_AES_256_XTS_KEY_SIZE  64
> +#define FS_MAX_KEY_SIZE  64
> +
> +#define FS_KEY_DESC_PREFIX   "fscrypt:"
> +#define FS_KEY_DESC_PREFIX_SIZE  8
> +
> +/* This is passed in from userspace into the kernel keyring */
> +struct fscrypt_key {
> + __u32 mode;
> + char raw[FS_MAX_KEY_SIZE];
> + __u32 size;
> +} __packed;
> +
> +struct fscrypt_info {
> + char ci_data_mode;
> + char ci_filename_mode;
> + char ci_flags;

ditto

> + struct crypto_ablkcipher *ci_ctfm;
> + struct key *ci_keyring_key;
> + char ci_master_key[FS_KEY_DESCRIPTOR_SIZE];
> +};
> +
> +#define FS_CTX_REQUIRES_FREE_ENCRYPT_FL  0x0001
> +#define FS_WRITE_PATH_FL 0x0002
> +
> +struct fscrypt_ctx {
> + union {
> + struct {
> + struct page *bounce_page;   /* Ciphertext page */
> + struct page *control_page;  /* Original page  */
> + } w;
> + struct {
> + struct bio *bio;
> + struct work_struct work;
> + } r;
> + struct list_head free_list; /* Free list */
> + };
> + char flags; /* Flags */
> + char mode;   

[f2fs-dev] [PATCH 01/10] fs crypto: add basic definitions for per-file encryption

2016-02-25 Thread Jaegeuk Kim
This patch adds definitions for per-file encryption used by ext4 and f2fs.

Signed-off-by: Jaegeuk Kim 
---
 include/linux/fs.h   |   8 ++
 include/linux/fscrypto.h | 239 +++
 include/uapi/linux/fs.h  |  18 
 3 files changed, 265 insertions(+)
 create mode 100644 include/linux/fscrypto.h

diff --git a/include/linux/fs.h b/include/linux/fs.h
index ae68100..d8f57cf 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -53,6 +53,8 @@ struct swap_info_struct;
 struct seq_file;
 struct workqueue_struct;
 struct iov_iter;
+struct fscrypt_info;
+struct fscrypt_operations;
 
 extern void __init inode_init(void);
 extern void __init inode_init_early(void);
@@ -678,6 +680,10 @@ struct inode {
struct hlist_head   i_fsnotify_marks;
 #endif
 
+#ifdef CONFIG_FS_ENCRYPTION
+   struct fscrypt_info *i_crypt_info;
+#endif
+
void*i_private; /* fs or device private pointer */
 };
 
@@ -1323,6 +1329,8 @@ struct super_block {
 #endif
const struct xattr_handler **s_xattr;
 
+   const struct fscrypt_operations *s_cop;
+
struct hlist_bl_heads_anon; /* anonymous dentries for (nfs) 
exporting */
struct list_heads_mounts;   /* list of mounts; _not_ for fs 
use */
struct block_device *s_bdev;
diff --git a/include/linux/fscrypto.h b/include/linux/fscrypto.h
new file mode 100644
index 000..b0aed92
--- /dev/null
+++ b/include/linux/fscrypto.h
@@ -0,0 +1,239 @@
+/*
+ * General per-file encryption definition
+ *
+ * Copyright (C) 2015, Google, Inc.
+ *
+ * Written by Michael Halcrow, 2015.
+ * Modified by Jaegeuk Kim, 2015.
+ */
+
+#ifndef _LINUX_FSCRYPTO_H
+#define _LINUX_FSCRYPTO_H
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define FS_KEY_DERIVATION_NONCE_SIZE   16
+#define FS_ENCRYPTION_CONTEXT_FORMAT_V11
+
+#define FS_POLICY_FLAGS_PAD_4  0x00
+#define FS_POLICY_FLAGS_PAD_8  0x01
+#define FS_POLICY_FLAGS_PAD_16 0x02
+#define FS_POLICY_FLAGS_PAD_32 0x03
+#define FS_POLICY_FLAGS_PAD_MASK   0x03
+#define FS_POLICY_FLAGS_VALID  0x03
+
+/* Encryption algorithms */
+#define FS_ENCRYPTION_MODE_INVALID 0
+#define FS_ENCRYPTION_MODE_AES_256_XTS 1
+#define FS_ENCRYPTION_MODE_AES_256_GCM 2
+#define FS_ENCRYPTION_MODE_AES_256_CBC 3
+#define FS_ENCRYPTION_MODE_AES_256_CTS 4
+
+/**
+ * Encryption context for inode
+ *
+ * Protector format:
+ *  1 byte: Protector format (1 = this version)
+ *  1 byte: File contents encryption mode
+ *  1 byte: File names encryption mode
+ *  1 byte: Flags
+ *  8 bytes: Master Key descriptor
+ *  16 bytes: Encryption Key derivation nonce
+ */
+struct fscrypt_context {
+   char format;
+   char contents_encryption_mode;
+   char filenames_encryption_mode;
+   char flags;
+   char master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE];
+   char nonce[FS_KEY_DERIVATION_NONCE_SIZE];
+} __packed;
+
+/* Encryption parameters */
+#define FS_XTS_TWEAK_SIZE  16
+#define FS_AES_128_ECB_KEY_SIZE16
+#define FS_AES_256_GCM_KEY_SIZE32
+#define FS_AES_256_CBC_KEY_SIZE32
+#define FS_AES_256_CTS_KEY_SIZE32
+#define FS_AES_256_XTS_KEY_SIZE64
+#define FS_MAX_KEY_SIZE64
+
+#define FS_KEY_DESC_PREFIX "fscrypt:"
+#define FS_KEY_DESC_PREFIX_SIZE8
+
+/* This is passed in from userspace into the kernel keyring */
+struct fscrypt_key {
+   __u32 mode;
+   char raw[FS_MAX_KEY_SIZE];
+   __u32 size;
+} __packed;
+
+struct fscrypt_info {
+   char ci_data_mode;
+   char ci_filename_mode;
+   char ci_flags;
+   struct crypto_ablkcipher *ci_ctfm;
+   struct key *ci_keyring_key;
+   char ci_master_key[FS_KEY_DESCRIPTOR_SIZE];
+};
+
+#define FS_CTX_REQUIRES_FREE_ENCRYPT_FL0x0001
+#define FS_WRITE_PATH_FL   0x0002
+
+struct fscrypt_ctx {
+   union {
+   struct {
+   struct page *bounce_page;   /* Ciphertext page */
+   struct page *control_page;  /* Original page  */
+   } w;
+   struct {
+   struct bio *bio;
+   struct work_struct work;
+   } r;
+   struct list_head free_list; /* Free list */
+   };
+   char flags; /* Flags */
+   char mode;  /* Encryption mode for tfm */
+};
+
+struct fscrypt_completion_result {
+   struct completion completion;
+   int res;
+};
+
+#define DECLARE_FS_COMPLETION_RESULT(ecr) \
+   struct fscrypt_completion_result ecr = { \
+   COMPLETION_INITIALIZER((ecr).completion), 0 }
+
+static inline int fscrypt_key_size(int