Skip to contentSkip to conversation

Support Argon2 KDF in LUKS2

PATCH Superseded Patrick Steinhardt 4 participants 32 messages Feb 6, 2020 – Feb 21, 2020 Kbuild

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#7 lore raw

PATCH 3/5 disk: luks2: Add missing newline to debug message

In reply to Patrick Steinhardt

The debug message printed when decryption with a keyslot fails is missing its trailing newline. Add it to avoid mangling it with subsequent output.

---
 grub-core/disk/luks2.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
grub-core/disk/luks2.c +1 -1
index 49ee9c862..65c4f0aac 100644
@@ -610,7 +610,7 @@ luks2_recover_key (grub_disk_t disk,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
       if (ret)
 	{
-	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_SIZE" failed", i);
+	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_SIZE" failed\n", i);
 	  continue;
 	}
 
Signature · 1 line
2.25.0
#1 lore raw

PATCH 1/5 efi: Allocate half of available memory by default

In reply to Patrick Steinhardt

By default, GRUB will allocate a quarter of the pages it got available in the EFI subsystem. On many current systems, this will amount to roughly 800MB of RAM assuming an address space of 32 bits. This is plenty for most use cases, but it doesn't suffice when using full disk encryption with a key derival function based on Argon2.

Besides the usual iteration count known from PBKDF2, Argon2 introduces two additional parameters "memory" and "parallelism". While the latter doesn't really matter to us, the memory parameter is quite interesting. If encrypting a partition with LUKS2 using Argon2 as KDF, then cryptsetup will default to a memory parameter of 1GB. Meaning we need to allocate a buffer of 1GB in size in order to be able to derive the key, which definitely won't squeeze into the limit of 800MB.

To prepare for Argon2, let's thus increase the default and make half of memory available, instead of a quarter only. This amounts to about 1600MB on above systems, which is sufficient for Argon2.

---
 grub-core/kern/efi/mm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
grub-core/kern/efi/mm.c +2 -2
index b02fab1b1..d1f9d046b 100644
@@ -599,10 +599,10 @@ grub_efi_mm_init (void)
   filtered_memory_map_end = filter_memory_map (memory_map, filtered_memory_map,
 					       desc_size, memory_map_end);
 
-  /* By default, request a quarter of the available memory.  */
+  /* By default, request half of the available memory.  */
   total_pages = get_total_pages (filtered_memory_map, desc_size,
 				 filtered_memory_map_end);
-  required_pages = (total_pages >> 2);
+  required_pages = (total_pages / 2);
   if (required_pages < BYTES_TO_PAGES (MIN_HEAP_SIZE))
     required_pages = BYTES_TO_PAGES (MIN_HEAP_SIZE);
   else if (required_pages > BYTES_TO_PAGES (MAX_HEAP_SIZE))
Signature · 1 line
2.25.0
#0 lore raw

cover Support Argon2 KDF in LUKS2

Hi,

as promised back when LUKS2 support was merged, here's the code that enables decrypting LUKS2 partitions that use Argon2 as their key derival function. Most of this is simple legwork, but I expect two things to be potentially controversial:

- I've changed how EFI allocates memory. On my test systems, I was only able to allocate roughly 800MB, which isn't enough for the default of 1GB memory parameter that cryptsetup uses with Argon2. Instead of taking a quarter of available memory, we now take half of it, which amounts to ~1.6GB on 32 bit systems.

- The import of Argon2 itself. I've imported code from the cryptsetup project, but I've modified it quite a bit to fit into GRUB's codebase. This included both stripping off unneeded functionality as well as converting the code to use our own coding style. While it makes importing upstream fixes harder, I'd argue the code is still very similar in its structure and thus backporting should be easy enough.

Anyway. With these changes I'm able to successfully decrypt LUKS2 partitions making use of either PBKDF2, Argon2i or Argon2id.

Regards
Patrick

Patrick Steinhardt (5):
efi: Allocate half of available memory by default
argon2: Import Argon2 from cryptsetup
disk: luks2: Add missing newline to debug message
disk: luks2: Discern Argon2i and Argon2id
disk: luks2: Support key derival via Argon2

 Makefile.util.def                             |   4 +-
 grub-core/Makefile.core.def                   |   8 +-
 grub-core/disk/luks2.c                        |  29 +-
 grub-core/kern/efi/mm.c                       |   4 +-
 grub-core/lib/argon2/argon2.c                 | 614 ++++++++++++++++++
 grub-core/lib/argon2/argon2.h                 |  65 ++
 grub-core/lib/argon2/blake2/blake2-impl.h     | 143 ++++
 grub-core/lib/argon2/blake2/blake2.h          |  81 +++
 grub-core/lib/argon2/blake2/blake2b.c         | 384 +++++++++++
 .../lib/argon2/blake2/blamka-round-ref.h      |  56 ++
 10 files changed, 1376 insertions(+), 12 deletions(-)
 create mode 100644 grub-core/lib/argon2/argon2.c
 create mode 100644 grub-core/lib/argon2/argon2.h
 create mode 100644 grub-core/lib/argon2/blake2/blake2-impl.h
 create mode 100644 grub-core/lib/argon2/blake2/blake2.h
 create mode 100644 grub-core/lib/argon2/blake2/blake2b.c
 create mode 100644 grub-core/lib/argon2/blake2/blamka-round-ref.h
Signature · 1 line
2.25.0
#10 lore raw

PATCH 4/5 disk: luks2: Discern Argon2i and Argon2id

While GRUB is already able to parse both Argon2i and Argon2id parameters from the LUKS2 header, it doesn't discern both types. This commit introduces a new KDF type for Argon2id and sets up the parsed KDF's type accordingly.

---
 grub-core/disk/luks2.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
grub-core/disk/luks2.c +8 -5
index 65c4f0aac..767631198 100644
@@ -40,6 +40,7 @@ GRUB_MOD_LICENSE ("GPLv3+");
 enum grub_luks2_kdf_type
 {
   LUKS2_KDF_TYPE_ARGON2I,
+  LUKS2_KDF_TYPE_ARGON2ID,
   LUKS2_KDF_TYPE_PBKDF2
 };
 typedef enum grub_luks2_kdf_type grub_luks2_kdf_type_t;
@@ -90,7 +91,7 @@ struct grub_luks2_keyslot
 	grub_int64_t time;
 	grub_int64_t memory;
 	grub_int64_t cpus;
-      } argon2i;
+      } argon2;
       struct
       {
 	const char   *hash;
@@ -158,10 +159,11 @@ luks2_parse_keyslot (grub_luks2_keyslot_t *out, const grub_json_t *keyslot)
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Missing or invalid KDF");
   else if (!grub_strcmp (type, "argon2i") || !grub_strcmp (type, "argon2id"))
     {
-      out->kdf.type = LUKS2_KDF_TYPE_ARGON2I;
-      if (grub_json_getint64 (&out->kdf.u.argon2i.time, &kdf, "time") ||
-	  grub_json_getint64 (&out->kdf.u.argon2i.memory, &kdf, "memory") ||
-	  grub_json_getint64 (&out->kdf.u.argon2i.cpus, &kdf, "cpus"))
+      out->kdf.type = !grub_strcmp (type, "argon2i")
+		      ? LUKS2_KDF_TYPE_ARGON2I : LUKS2_KDF_TYPE_ARGON2ID;
+      if (grub_json_getint64 (&out->kdf.u.argon2.time, &kdf, "time") ||
+	  grub_json_getint64 (&out->kdf.u.argon2.memory, &kdf, "memory") ||
+	  grub_json_getint64 (&out->kdf.u.argon2.cpus, &kdf, "cpus"))
 	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Missing Argon2i parameters");
     }
   else if (!grub_strcmp (type, "pbkdf2"))
@@ -432,6 +434,7 @@ luks2_decrypt_key (grub_uint8_t *out_key,
   switch (k->kdf.type)
     {
       case LUKS2_KDF_TYPE_ARGON2I:
+      case LUKS2_KDF_TYPE_ARGON2ID:
 	ret = grub_error (GRUB_ERR_BAD_ARGUMENT, "Argon2 not supported");
 	goto err;
       case LUKS2_KDF_TYPE_PBKDF2:
Signature · 1 line
2.25.0
#4 lore raw

PATCH 2/5 argon2: Import Argon2 from cryptsetup

In reply to Patrick Steinhardt

In order to support the Argon2 key derival function for LUKS2, we obviously need to implement Argon2. It doesn't make a lot of sense to hand-code any crypto, which is why this commit instead imports Argon2 from the cryptsetup project. The cryptsetup project was chosen as upstream simply because it is the de-facto home of LUKS2, making us bug-to-bug compatible with their Argon2 implementation.

As the cryptsetup project imported the code themselves from the repository hosted at https://github.com/P-H-C/phc-winner-argon2, it is licensed under a mixture of LGPLv2.1+ and CC0 1.0 Universal/Apache 2.0. Given that both LGPLv2.1+ and Apache 2.0 are compatible with GPLv3, it should be fine to import that code.

The code is imported from https://gitlab.com/cryptsetup/cryptsetup, tag v2.3.0-rc0 (517da0dc6942b8a6038ada3c2fec7e20c2c0335c). It's not a 1:1 copy though, but instead the code was trimmed and adjusted to match GRUB's needs and coding style. Most importantly:

- Unneeded functionality like reading and writing encoded hashes was removed.

- Optimized versions of both Argon2 and Blake2 were removed as they require AVX, which isn't necessarily available on all platforms.

- The code was converted to use our own data types like grub_uint32_t and functions like grub_memset.

- The code was adjusted to match our coding style.

That being said, the structure of functions themselves remains the same and is easily recognized from upstream, so it should be trivial to backport any upstream fixes.

---
 grub-core/Makefile.core.def                   |   6 +
 grub-core/lib/argon2/argon2.c                 | 614 ++++++++++++++++++
 grub-core/lib/argon2/argon2.h                 |  65 ++
 grub-core/lib/argon2/blake2/blake2-impl.h     | 143 ++++
 grub-core/lib/argon2/blake2/blake2.h          |  81 +++
 grub-core/lib/argon2/blake2/blake2b.c         | 384 +++++++++++
 .../lib/argon2/blake2/blamka-round-ref.h      |  56 ++
 7 files changed, 1349 insertions(+)
 create mode 100644 grub-core/lib/argon2/argon2.c
 create mode 100644 grub-core/lib/argon2/argon2.h
 create mode 100644 grub-core/lib/argon2/blake2/blake2-impl.h
 create mode 100644 grub-core/lib/argon2/blake2/blake2.h
 create mode 100644 grub-core/lib/argon2/blake2/blake2b.c
 create mode 100644 grub-core/lib/argon2/blake2/blamka-round-ref.h
grub-core/Makefile.core.def +6 -0
index a0507a1fa..b9e7a4171 100644
@@ -1181,6 +1181,12 @@ module = {
   common = lib/json/json.c;
 };
 
+module = {
+  name = argon2;
+  common = lib/argon2/argon2.c;
+  common = lib/argon2/blake2/blake2b.c;
+};
+
 module = {
   name = afsplitter;
   common = disk/AFSplitter.c;
grub-core/lib/argon2/argon2.c new file +614 -0

614 lines not on this page · raw message

grub-core/lib/argon2/argon2.h new file +65 -0

65 lines not on this page · raw message

grub-core/lib/argon2/blake2/blake2-impl.h new file +143 -0

143 lines not on this page · raw message

grub-core/lib/argon2/blake2/blake2.h new file +81 -0

81 lines not on this page · raw message

grub-core/lib/argon2/blake2/blake2b.c new file +384 -0

384 lines not on this page · raw message

grub-core/lib/argon2/blake2/blamka-round-ref.h new file +56 -0

56 lines not on this page · raw message

Signature · 1 line
2.25.0
#11 lore raw

PATCH 5/5 disk: luks2: Support key derival via Argon2

In reply to Patrick Steinhardt

One addition with LUKS2 was support of the key derival function Argon2 in addition to the previously supported PBKDF2 algortihm. In order to ease getting in initial support for LUKS2, we only reused infrastructure to support LUKS2 with PBKDF2, but left out Argon2.

This commit now introduces support for Argon2 to enable decryption of LUKS2 partitions using this key derival function. As the code for Argon2 has been added in a previous commit in this series, adding support is now trivial.

---
 Makefile.util.def           |  4 +++-
 grub-core/Makefile.core.def |  2 +-
 grub-core/disk/luks2.c      | 14 ++++++++++++--
 3 files changed, 16 insertions(+), 4 deletions(-)
Makefile.util.def +3 -1
index 94336392b..e0c98aa1c 100644
@@ -3,7 +3,7 @@ AutoGen definitions Makefile.tpl;
 library = {
   name = libgrubkern.a;
   cflags = '$(CFLAGS_GNULIB)';
-  cppflags = '$(CPPFLAGS_GNULIB) -I$(srcdir)/grub-core/lib/json';
+  cppflags = '$(CPPFLAGS_GNULIB) -I$(srcdir)/grub-core/lib/json -I$(srcdir)/grub-core/lib/argon2';
 
   common = util/misc.c;
   common = grub-core/kern/command.c;
@@ -36,6 +36,8 @@ library = {
   common = grub-core/kern/misc.c;
   common = grub-core/kern/partition.c;
   common = grub-core/lib/crypto.c;
+  common = grub-core/lib/argon2/argon2.c;
+  common = grub-core/lib/argon2/blake2/blake2b.c;
   common = grub-core/lib/json/json.c;
   common = grub-core/disk/luks.c;
   common = grub-core/disk/luks2.c;
grub-core/Makefile.core.def +1 -1
index b9e7a4171..914325369 100644
@@ -1202,7 +1202,7 @@ module = {
   common = disk/luks2.c;
   common = lib/gnulib/base64.c;
   cflags = '$(CFLAGS_POSIX) $(CFLAGS_GNULIB)';
-  cppflags = '$(CPPFLAGS_POSIX) $(CPPFLAGS_GNULIB) -I$(srcdir)/lib/json';
+  cppflags = '$(CPPFLAGS_POSIX) $(CPPFLAGS_GNULIB) -I$(srcdir)/lib/json -I$(srcdir)/lib/argon2';
 };
 
 module = {
grub-core/disk/luks2.c +12 -2
index 767631198..9c5b416c4 100644
@@ -27,6 +27,7 @@
 #include <grub/partition.h>
 #include <grub/i18n.h>
 
+#include <argon2.h>
 #include <base64.h>
 #include <json.h>
 
@@ -435,8 +436,17 @@ luks2_decrypt_key (grub_uint8_t *out_key,
     {
       case LUKS2_KDF_TYPE_ARGON2I:
       case LUKS2_KDF_TYPE_ARGON2ID:
-	ret = grub_error (GRUB_ERR_BAD_ARGUMENT, "Argon2 not supported");
-	goto err;
+	ret = grub_crypto_argon2 (passphrase, passphraselen, salt, saltlen,
+				  k->kdf.u.argon2.time, k->kdf.u.argon2.memory, k->kdf.u.argon2.cpus,
+				  k->kdf.type == LUKS2_KDF_TYPE_ARGON2I ? GRUB_ARGON2_I : GRUB_ARGON2_ID,
+				  GRUB_ARGON2_VERSION_NUMBER,
+				  area_key, k->area.key_size);
+        if (ret)
+	  {
+	    grub_dprintf ("luks2", "Argon2 failed: %s\n", grub_errmsg);
+	    goto err;
+	  }
+        break;
       case LUKS2_KDF_TYPE_PBKDF2:
 	hash = grub_crypto_lookup_md_by_name (k->kdf.u.pbkdf2.hash);
 	if (!hash)
Signature · 1 line
2.25.0
#5 lore raw

Re: [PATCH 2/5] argon2: Import Argon2 from cryptsetup

In reply to Patrick Steinhardt

Hi,

Quoted 7 lines · Patrick Steinhardt

In order to support the Argon2 key derival function for LUKS2, we obviously need to implement Argon2. It doesn't make a lot of sense to hand-code any crypto, which is why this commit instead imports Argon2 from the cryptsetup project. The cryptsetup project was chosen as upstream simply because it is the de-facto home of LUKS2, making us bug-to-bug compatible with their Argon2 implementation.

Quoted 5 lines

As the cryptsetup project imported the code themselves from the repository hosted at https://github.com/P-H-C/phc-winner-argon2, it is licensed under a mixture of LGPLv2.1+ and CC0 1.0 Universal/Apache 2.0. Given that both LGPLv2.1+ and Apache 2.0 are compatible with GPLv3, it should be fine to import that code.

Well, it was a temporary solution as we (cryptsetup developers) are trying to include Argon2 in OpenSSL (default crypto backend for cryptsetup) - and perhaps in gcrypt later.

So if gcrypt includes Argon2 implementation in future, what is your plan? Switch to it or keep this embedded copy still in place? Just asking :)
...

grub-core/lib/argon2/argon2.c line 5

Quoted 11 lines
grub-core/lib/argon2/argon2.c new file +5 -0
new file mode 100644
index 000000000..1b8b092ae
@@ -0,0 +1,614 @@
+/*
+ * Argon2 PBKDF2 library wrapper
+ *
+ * Copyright (C) 2016-2020 Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2016-2020 Milan Broz

You are missing copyright of the original Argon2 authors here.
(This is apparently not the original wrapper code only but internal argon2 implementation.)

Anyway, this is interesting addition to GRUB2 (and people often asks cryptsetup upstream about this). Thanks!

Milan

#6 lore raw

Re: [PATCH 2/5] argon2: Import Argon2 from cryptsetup

Quoted 14 lines · Milan Broz
Quoted 7 lines · Patrick Steinhardt

In order to support the Argon2 key derival function for LUKS2, we obviously need to implement Argon2. It doesn't make a lot of sense to hand-code any crypto, which is why this commit instead imports Argon2 from the cryptsetup project. The cryptsetup project was chosen as upstream simply because it is the de-facto home of LUKS2, making us bug-to-bug compatible with their Argon2 implementation.

Quoted 5 lines

As the cryptsetup project imported the code themselves from the repository hosted at https://github.com/P-H-C/phc-winner-argon2, it is licensed under a mixture of LGPLv2.1+ and CC0 1.0 Universal/Apache 2.0. Given that both LGPLv2.1+ and Apache 2.0 are compatible with GPLv3, it should be fine to import that code.

Quoted 7 lines

Well, it was a temporary solution as we (cryptsetup developers) are trying to include Argon2 in OpenSSL (default crypto backend for cryptsetup) - and perhaps in gcrypt later.

So if gcrypt includes Argon2 implementation in future, what is your plan? Switch to it or keep this embedded copy still in place? Just asking :)
...

GRUB already uses libgcrypt for some stuff, so switching to an implementation provided by it would most likely be the way to go as soon as it got support for it.

Quoted 6 lines
Quoted 6 lines
grub-core/lib/argon2/argon2.c new file +0 -0
new file mode 100644
index 000000000..1b8b092ae
@@ -0,0 +1,614 @@
Quoted 8 lines

grub-core/lib/argon2/argon2.c line 5

Quoted 5 lines
grub-core/lib/argon2/argon2.c new file +5 -0
+/*
+ * Argon2 PBKDF2 library wrapper
+ *
+ * Copyright (C) 2016-2020 Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2016-2020 Milan Broz

You are missing copyright of the original Argon2 authors here.
(This is apparently not the original wrapper code only but internal argon2 implementation.)

Oops, definitely, forgot to merge them in while collapsing files into one. Thanks for the hint.

Quoted 4 lines

Anyway, this is interesting addition to GRUB2 (and people often asks cryptsetup upstream about this). Thanks!

Milan

Reviewed-by on v1 3/5
#8 lore raw

Re: [PATCH 3/5] disk: luks2: Add missing newline to debug message

In reply to Patrick Steinhardt
Quoted 5 lines · Patrick Steinhardt

The debug message printed when decryption with a keyslot fails is missing its trailing newline. Add it to avoid mangling it with subsequent output.

I will take it as a separate patch.

By the way, please use "luks2:" instead of "disk: luks2:" in the subject.

Daniel

#12 lore raw
In reply to Patrick Steinhardt

Adding Milan, Leif, Alex, Peter, Mathew and Vladimir.

Quoted 7 lines · Patrick Steinhardt

Hi,

as promised back when LUKS2 support was merged, here's the code that enables decrypting LUKS2 partitions that use Argon2 as their key derival function. Most of this is simple legwork, but I expect two things to be potentially controversial:

Quoted 5 lines

- I've changed how EFI allocates memory. On my test systems, I was only able to allocate roughly 800MB, which isn't enough for the default of 1GB memory parameter that cryptsetup uses with Argon2. Instead of taking a quarter of available memory, we now take half of it, which amounts to ~1.6GB on 32 bit systems.

That is huge for the bootloader. What about systems with less than 3 GiB of RAM? Could we reduce amount of RAM required by Argon2?

Quoted 2 lines

- The import of Argon2 itself. I've imported code from the cryptsetup project, but I've modified it quite a bit to fit into

Milan mentioned something about libgcrypt. Milan, when the Argon2 code may land in libgcrypt?

Quoted 2 lines

GRUB's codebase. This included both stripping off unneeded functionality as well as converting the code to use our own coding

Stripping unneeded functionality is OK. However, I think that it does not make sense to convert coding style to the GRUB one. Especially if we do not do that for other modules. So, I would leave coding style in Argon2 module as is and save your precious minutes for something more productive... ;-)

Quoted 6 lines

style. While it makes importing upstream fixes harder, I'd argue the code is still very similar in its structure and thus backporting should be easy enough.

Anyway. With these changes I'm able to successfully decrypt LUKS2 partitions making use of either PBKDF2, Argon2i or Argon2id.

I will take deeper dive into the code if we hammer out things listed above.

Daniel

#13 lore raw
Quoted 10 lines · Daniel Kiper

Adding Milan, Leif, Alex, Peter, Mathew and Vladimir.

Quoted 7 lines · Patrick Steinhardt

Hi,

as promised back when LUKS2 support was merged, here's the code that enables decrypting LUKS2 partitions that use Argon2 as their key derival function. Most of this is simple legwork, but I expect two things to be potentially controversial:

Quoted 8 lines
Quoted 5 lines

- I've changed how EFI allocates memory. On my test systems, I was only able to allocate roughly 800MB, which isn't enough for the default of 1GB memory parameter that cryptsetup uses with Argon2. Instead of taking a quarter of available memory, we now take half of it, which amounts to ~1.6GB on 32 bit systems.

That is huge for the bootloader. What about systems with less than 3 GiB of RAM? Could we reduce amount of RAM required by Argon2?

No, this is the principle of memory-hard function :)
The primary reason is to increase attacker cost for dictionary attacks.

Anyway, there are some limits in cryptsetup - we try to never use more than half of physical memory and maximum is hard-compiled to 4GiB.

(But physical memory limit applies when formatting device, then
is stored in the LUKS2 keyslot header. So if you format it on device with much larger RAM and it is later not available, it fails to open.
It is more complicated though - we have benchmark during format that prioritize unlocking time, so PBKDF memory is usually decreased on low-memory systems anyway.)

Quoted 6 lines
Quoted 3 lines

- The import of Argon2 itself. I've imported code from the cryptsetup project, but I've modified it quite a bit to fit into

Milan mentioned something about libgcrypt. Milan, when the Argon2 code may land in libgcrypt?

Once we have volunteer to implement it / port it to gcrypt :-)

BTW if you have embedded Argon2 code, you should also add some test vectors to your testsuite.
(You can use these we have in cryptsetup - see tests/crypto-vectors.c)

Milan

#15 lore raw
In reply to Daniel Kiper
Quoted 8 lines · Daniel Kiper
Quoted 2 lines

GRUB's codebase. This included both stripping off unneeded functionality as well as converting the code to use our own coding

Stripping unneeded functionality is OK. However, I think that it does not make sense to convert coding style to the GRUB one. Especially if we do not do that for other modules. So, I would leave coding style in Argon2 module as is and save your precious minutes for something more productive... ;-)

Fair enough, I'll send out a v2 with the original coding style. I thought as much when I was ready with v1, but was too lazy to do the work and change back the coding style.

Anyway, to save myself another roundtrip: would you prefer to merge Argon2 functionality into a single file like I've done it right now or to retain the original set of files? The reason why I've opted for the latter is mainly to be able to annotate more functions as static.

Quoted 8 lines
Quoted 6 lines

style. While it makes importing upstream fixes harder, I'd argue the code is still very similar in its structure and thus backporting should be easy enough.

Anyway. With these changes I'm able to successfully decrypt LUKS2 partitions making use of either PBKDF2, Argon2i or Argon2id.

I will take deeper dive into the code if we hammer out things listed above.

Cool, thanks!

Patrick

#9 lore raw

Re: [PATCH 3/5] disk: luks2: Add missing newline to debug message

In reply to Daniel Kiper
Quoted 5 lines · Daniel Kiper
Quoted 4 lines · Patrick Steinhardt

The debug message printed when decryption with a keyslot fails is missing its trailing newline. Add it to avoid mangling it with subsequent output.

Quoted 7 lines
Quoted 1 line

I will take it as a separate patch.

By the way, please use "luks2:" instead of "disk: luks2:" in the subject.

Will do!

Patrick

#16 lore raw
In reply to Patrick Steinhardt
Quoted 11 lines · Patrick Steinhardt
Quoted 8 lines · Daniel Kiper
Quoted 2 lines

GRUB's codebase. This included both stripping off unneeded functionality as well as converting the code to use our own coding

Stripping unneeded functionality is OK. However, I think that it does not make sense to convert coding style to the GRUB one. Especially if we do not do that for other modules. So, I would leave coding style in Argon2 module as is and save your precious minutes for something more productive... ;-)

Quoted 8 lines

Fair enough, I'll send out a v2 with the original coding style. I thought as much when I was ready with v1, but was too lazy to do the work and change back the coding style.

Anyway, to save myself another roundtrip: would you prefer to merge Argon2 functionality into a single file like I've done it right now or to retain the original set of files? The reason why I've opted for the latter is mainly to be able to annotate more functions as static.

I think that you should retain original set of files. And please add a description to the docs/grub-dev.texi how to update Argon2 lib in the future.

Daniel

#2 lore raw

Re: [PATCH 1/5] efi: Allocate half of available memory by default

In reply to Patrick Steinhardt
Quoted 14 lines · Patrick Steinhardt

By default, GRUB will allocate a quarter of the pages it got available in the EFI subsystem. On many current systems, this will amount to roughly 800MB of RAM assuming an address space of 32 bits. This is plenty for most use cases, but it doesn't suffice when using full disk encryption with a key derival function based on Argon2.

Besides the usual iteration count known from PBKDF2, Argon2 introduces two additional parameters "memory" and "parallelism". While the latter doesn't really matter to us, the memory parameter is quite interesting. If encrypting a partition with LUKS2 using Argon2 as KDF, then cryptsetup will default to a memory parameter of 1GB. Meaning we need to allocate a buffer of 1GB in size in order to be able to derive the key, which definitely won't squeeze into the limit of 800MB.

Quoted 3 lines

To prepare for Argon2, let's thus increase the default and make half of memory available, instead of a quarter only. This amounts to about 1600MB on above systems, which is sufficient for Argon2.

I was never a huge fan of the "grab a percentage of RAM" in the first place, and I think "grab twice that" is not the best solution here.

(Real) corner cases that would be affected by this are:
1) chainloading grub from grub
2) OS loaders (loaded by GRUB) requiring large amounts of RAM before ExitBootsevices().

If you have a known minimum requirement, can we work towards that instead?
For a least-invasive approach, that could be something like
- rename required_pages target_heap_pages
- add a required_pages var initialized to ... something real and then

if (target_heap_size < required_pages)
target_heap_pages = required_pages.

The MIN/MAX heap size could move into the "something real" calculation, getting rid of the current (arbitrary) clamping of MAX_HEAP_SIZE to 1.6G..

/
Leif

Quoted 30 lines
---
 grub-core/kern/efi/mm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
grub-core/kern/efi/mm.c +2 -2
index b02fab1b1..d1f9d046b 100644
@@ -599,10 +599,10 @@ grub_efi_mm_init (void)
   filtered_memory_map_end = filter_memory_map (memory_map, filtered_memory_map,
 					       desc_size, memory_map_end);
 
-  /* By default, request a quarter of the available memory.  */
+  /* By default, request half of the available memory.  */
   total_pages = get_total_pages (filtered_memory_map, desc_size,
 				 filtered_memory_map_end);
-  required_pages = (total_pages >> 2);
+  required_pages = (total_pages / 2);
   if (required_pages < BYTES_TO_PAGES (MIN_HEAP_SIZE))
     required_pages = BYTES_TO_PAGES (MIN_HEAP_SIZE);
   else if (required_pages > BYTES_TO_PAGES (MAX_HEAP_SIZE))
Signature · 7 lines
2.25.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#17 lore raw
In reply to Daniel Kiper
Quoted 21 lines · Daniel Kiper
Quoted 11 lines · Patrick Steinhardt
Quoted 8 lines · Daniel Kiper
Quoted 2 lines

GRUB's codebase. This included both stripping off unneeded functionality as well as converting the code to use our own coding

Stripping unneeded functionality is OK. However, I think that it does not make sense to convert coding style to the GRUB one. Especially if we do not do that for other modules. So, I would leave coding style in Argon2 module as is and save your precious minutes for something more productive... ;-)

Quoted 8 lines

Fair enough, I'll send out a v2 with the original coding style. I thought as much when I was ready with v1, but was too lazy to do the work and change back the coding style.

Anyway, to save myself another roundtrip: would you prefer to merge Argon2 functionality into a single file like I've done it right now or to retain the original set of files? The reason why I've opted for the latter is mainly to be able to annotate more functions as static.

Quoted 5 lines

I think that you should retain original set of files. And please add a description to the docs/grub-dev.texi how to update Argon2 lib in the future.

Daniel

In the ideal case, we'd just compile Argon2 with the POSIX compat layer so that we wouldn't need to modify most of the types and functions used by it, like uint32, malloc, etc. As a result, libgrubkern.a would grow a dependency on C{,PP}FLAGS_POSIX, though. I did notice compilation errors in other modules when trying that, so my question is which path to go:
fix resulting incompatibilities when adding POSIX includes or just replace types and function calls in Argon2 code?

Patrick

#20 lore raw

PATCH v2 2/6 types.h: add UINT-related macros needed for Argon2

In reply to Patrick Steinhardt

For the upcoming import of the Argon2 library, we need the macros GRUB_UINT32_MAX, GRUB_UINT32_C and GRUB_UINT64_C. Add them as a preparatory step.

---
 include/grub/types.h | 8 ++++++++
 1 file changed, 8 insertions(+)
include/grub/types.h +8 -0
index 035a4b528..35ba900dd 100644
@@ -137,6 +137,7 @@ typedef grub_int32_t	grub_ssize_t;
 #define GRUB_SHRT_MAX 0x7fff
 #define GRUB_SHRT_MIN (-GRUB_SHRT_MAX - 1)
 #define GRUB_UINT_MAX 4294967295U
+#define GRUB_UINT32_MAX 4294967295U
 #define GRUB_INT_MAX 0x7fffffff
 #define GRUB_INT_MIN (-GRUB_INT_MAX - 1)
 #define GRUB_INT32_MAX 2147483647
@@ -151,6 +152,13 @@ typedef grub_int32_t	grub_ssize_t;
 #endif
 # define GRUB_LONG_MIN (-GRUB_LONG_MAX - 1)
 
+# define GRUB_UINT32_C(x) x ## U
+# if GRUB_ULONG_MAX >> 31 >> 31 >> 1 == 1
+#  define GRUB_UINT64_C(x) x##UL
+# elif 1
+#  define GRUB_UINT64_C(x) x##ULL
+# endif
+
 typedef grub_uint64_t grub_properly_aligned_t;
 
 #define GRUB_PROPERLY_ALIGNED_ARRAY(name, size) grub_properly_aligned_t name[((size) + sizeof (grub_properly_aligned_t) - 1) / sizeof (grub_properly_aligned_t)]
Signature · 1 line
2.25.1
#19 lore raw

PATCH v2 1/6 efi: Allocate half of available memory by default

In reply to Patrick Steinhardt

By default, GRUB will allocate a quarter of the pages it got available in the EFI subsystem. On many current systems, this will amount to roughly 800MB of RAM assuming an address space of 32 bits. This is plenty for most use cases, but it doesn't suffice when using full disk encryption with a key derival function based on Argon2.

Besides the usual iteration count known from PBKDF2, Argon2 introduces two additional parameters "memory" and "parallelism". While the latter doesn't really matter to us, the memory parameter is quite interesting. If encrypting a partition with LUKS2 using Argon2 as KDF, then cryptsetup will default to a memory parameter of 1GB. Meaning we need to allocate a buffer of 1GB in size in order to be able to derive the key, which definitely won't squeeze into the limit of 800MB.

To prepare for Argon2, let's thus increase the default and make half of memory available, instead of a quarter only. This amounts to about 1600MB on above systems, which is sufficient for Argon2.

---
 grub-core/kern/efi/mm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
grub-core/kern/efi/mm.c +2 -2
index b02fab1b1..d1f9d046b 100644
@@ -599,10 +599,10 @@ grub_efi_mm_init (void)
   filtered_memory_map_end = filter_memory_map (memory_map, filtered_memory_map,
 					       desc_size, memory_map_end);
 
-  /* By default, request a quarter of the available memory.  */
+  /* By default, request half of the available memory.  */
   total_pages = get_total_pages (filtered_memory_map, desc_size,
 				 filtered_memory_map_end);
-  required_pages = (total_pages >> 2);
+  required_pages = (total_pages / 2);
   if (required_pages < BYTES_TO_PAGES (MIN_HEAP_SIZE))
     required_pages = BYTES_TO_PAGES (MIN_HEAP_SIZE);
   else if (required_pages > BYTES_TO_PAGES (MAX_HEAP_SIZE))
Signature · 1 line
2.25.1
#18 lore raw

v2 cover Support Argon2 KDF in LUKS2

In reply to Patrick Steinhardt

Hi,

this is the second version of my patchset to add support for Argon2 encryption keys for LUKS2.

The most important change is that I've now verbosely imported the argon2 code from the official reference implementation instead of from the cryptsetup project. The diff between both isn't that big in the end, and including from crypsetup's upstream seems a bit cleaner to me. There were several transformations required to use GRUB's types and functions as well as stripping of unused stuff, which I've now documented the dev manual. This also fixes my previously mistaken license headers.

One thing I'm not sure about here is whether it's fine to declare the argon2 mod's license as GPLv3. The code is licensed under CC0/Apache 2.0, where the latter is compatible with GPLv3. But I don't know whether it's legit to just say "Yeah, this mod is a GPLv3 one".

I didn't address the comment made by Leif yet with regards to grabbing memory. I ain't got much of a clue of GRUB's memory subsystem, so I'd gladly accept help there. Otherwise I'll have to dig a bit deeper.

The range diff compared to the previous version of this patch set is attached to this mail.

Patrick

Patrick Steinhardt (6):
efi: Allocate half of available memory by default types.h: add UINT-related macros needed for Argon2
argon2: Import Argon2 from cryptsetup
luks2: Add missing newline to debug message
luks2: Discern Argon2i and Argon2id
luks2: Support key derival via Argon2

 Makefile.util.def                             |   6 +-
 docs/grub-dev.texi                            |  64 +++
 grub-core/Makefile.core.def                   |  10 +-
 grub-core/disk/luks2.c                        |  28 +-
 grub-core/kern/efi/mm.c                       |   4 +-
 grub-core/lib/argon2/argon2.c                 | 232 ++++++++
 grub-core/lib/argon2/argon2.h                 | 264 +++++++++
 grub-core/lib/argon2/blake2/blake2-impl.h     | 151 +++++
 grub-core/lib/argon2/blake2/blake2.h          |  89 +++
 grub-core/lib/argon2/blake2/blake2b.c         | 388 +++++++++++++
 .../lib/argon2/blake2/blamka-round-ref.h      |  56 ++
 grub-core/lib/argon2/core.c                   | 525 ++++++++++++++++++
 grub-core/lib/argon2/core.h                   | 228 ++++++++
 grub-core/lib/argon2/ref.c                    | 190 +++++++
 include/grub/types.h                          |   8 +
 15 files changed, 2231 insertions(+), 12 deletions(-)
 create mode 100644 grub-core/lib/argon2/argon2.c
 create mode 100644 grub-core/lib/argon2/argon2.h
 create mode 100644 grub-core/lib/argon2/blake2/blake2-impl.h
 create mode 100644 grub-core/lib/argon2/blake2/blake2.h
 create mode 100644 grub-core/lib/argon2/blake2/blake2b.c
 create mode 100644 grub-core/lib/argon2/blake2/blamka-round-ref.h
 create mode 100644 grub-core/lib/argon2/core.c
 create mode 100644 grub-core/lib/argon2/core.h
 create mode 100644 grub-core/lib/argon2/ref.c

Range-diff against v1:

1:  53cdfdc27 = 1:  15bdf830e efi: Allocate half of available memory by default
2:  c55946ca5 < -:  --------- argon2: Import Argon2 from cryptsetup
-:  --------- > 2:  e81db7d95 types.h: add UINT-related macros needed for Argon2
-:  --------- > 3:  50aff9670 argon2: Import Argon2 from cryptsetup
3:  c17cd2197 ! 4:  af3f85665 disk: luks2: Add missing newline to debug message

@@ Metadata
Author: Patrick Steinhardt <[email protected]>

## Commit message ##
- disk: luks2: Add missing newline to debug message
+ luks2: Add missing newline to debug message

The debug message printed when decryption with a keyslot fails is missing its trailing newline. Add it to avoid mangling it with subsequent output.

+ Reviewed-by: Daniel Kiper <[email protected]>

## grub-core/disk/luks2.c ##
@@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t disk,
4: 390728cea ! 5: 89abe827b disk: luks2: Discern Argon2i and Argon2id
@@ Metadata
Author: Patrick Steinhardt <[email protected]>

## Commit message ##
- disk: luks2: Discern Argon2i and Argon2id
+ luks2: Discern Argon2i and Argon2id

While GRUB is already able to parse both Argon2i and Argon2id parameters from the LUKS2 header, it doesn't discern both types. This commit
5: ec4389627 ! 6: 70a354e0b disk: luks2: Support key derival via Argon2

    @@ Metadata
     Author: Patrick Steinhardt <[email protected]>
     
      ## Commit message ##
    -    disk: luks2: Support key derival via Argon2
    +    luks2: Support key derival via Argon2
     
         One addition with LUKS2 was support of the key derival function Argon2
         in addition to the previously supported PBKDF2 algortihm. In order to
    @@ Makefile.util.def: library = {
        common = grub-core/kern/partition.c;
        common = grub-core/lib/crypto.c;
     +  common = grub-core/lib/argon2/argon2.c;
    ++  common = grub-core/lib/argon2/core.c;
    ++  common = grub-core/lib/argon2/ref.c;
     +  common = grub-core/lib/argon2/blake2/blake2b.c;
        common = grub-core/lib/json/json.c;
        common = grub-core/disk/luks.c;
    @@ grub-core/disk/luks2.c: luks2_decrypt_key (grub_uint8_t *out_key,
            case LUKS2_KDF_TYPE_ARGON2ID:
     -	ret = grub_error (GRUB_ERR_BAD_ARGUMENT, "Argon2 not supported");
     -	goto err;
    -+	ret = grub_crypto_argon2 (passphrase, passphraselen, salt, saltlen,
    -+				  k->kdf.u.argon2.time, k->kdf.u.argon2.memory, k->kdf.u.argon2.cpus,
    -+				  k->kdf.type == LUKS2_KDF_TYPE_ARGON2I ? GRUB_ARGON2_I : GRUB_ARGON2_ID,
    -+				  GRUB_ARGON2_VERSION_NUMBER,
    -+				  area_key, k->area.key_size);
    ++	ret = argon2_hash (k->kdf.u.argon2.time, k->kdf.u.argon2.memory, k->kdf.u.argon2.cpus,
    ++			   passphrase, passphraselen, salt, saltlen, area_key, k->area.key_size,
    ++			   k->kdf.type == LUKS2_KDF_TYPE_ARGON2I ? Argon2_i : Argon2_id,
    ++			   ARGON2_VERSION_NUMBER);
     +        if (ret)
     +	  {
    -+	    grub_dprintf ("luks2", "Argon2 failed: %s\n", grub_errmsg);
    ++	    grub_dprintf ("luks2", "Argon2 failed: %s\n", argon2_error_message (ret));
     +	    goto err;
     +	  }
     +        break;
Signature · 1 line
2.25.1
#24 lore raw

PATCH v2 4/6 luks2: Add missing newline to debug message

The debug message printed when decryption with a keyslot fails is missing its trailing newline. Add it to avoid mangling it with subsequent output.

---
 grub-core/disk/luks2.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
grub-core/disk/luks2.c +1 -1
index 49ee9c862..65c4f0aac 100644
@@ -610,7 +610,7 @@ luks2_recover_key (grub_disk_t disk,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
       if (ret)
 	{
-	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_SIZE" failed", i);
+	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_SIZE" failed\n", i);
 	  continue;
 	}
 
Signature · 1 line
2.25.1
#25 lore raw

PATCH v2 5/6 luks2: Discern Argon2i and Argon2id

In reply to Patrick Steinhardt

While GRUB is already able to parse both Argon2i and Argon2id parameters from the LUKS2 header, it doesn't discern both types. This commit introduces a new KDF type for Argon2id and sets up the parsed KDF's type accordingly.

---
 grub-core/disk/luks2.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
grub-core/disk/luks2.c +8 -5
index 65c4f0aac..767631198 100644
@@ -40,6 +40,7 @@ GRUB_MOD_LICENSE ("GPLv3+");
 enum grub_luks2_kdf_type
 {
   LUKS2_KDF_TYPE_ARGON2I,
+  LUKS2_KDF_TYPE_ARGON2ID,
   LUKS2_KDF_TYPE_PBKDF2
 };
 typedef enum grub_luks2_kdf_type grub_luks2_kdf_type_t;
@@ -90,7 +91,7 @@ struct grub_luks2_keyslot
 	grub_int64_t time;
 	grub_int64_t memory;
 	grub_int64_t cpus;
-      } argon2i;
+      } argon2;
       struct
       {
 	const char   *hash;
@@ -158,10 +159,11 @@ luks2_parse_keyslot (grub_luks2_keyslot_t *out, const grub_json_t *keyslot)
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Missing or invalid KDF");
   else if (!grub_strcmp (type, "argon2i") || !grub_strcmp (type, "argon2id"))
     {
-      out->kdf.type = LUKS2_KDF_TYPE_ARGON2I;
-      if (grub_json_getint64 (&out->kdf.u.argon2i.time, &kdf, "time") ||
-	  grub_json_getint64 (&out->kdf.u.argon2i.memory, &kdf, "memory") ||
-	  grub_json_getint64 (&out->kdf.u.argon2i.cpus, &kdf, "cpus"))
+      out->kdf.type = !grub_strcmp (type, "argon2i")
+		      ? LUKS2_KDF_TYPE_ARGON2I : LUKS2_KDF_TYPE_ARGON2ID;
+      if (grub_json_getint64 (&out->kdf.u.argon2.time, &kdf, "time") ||
+	  grub_json_getint64 (&out->kdf.u.argon2.memory, &kdf, "memory") ||
+	  grub_json_getint64 (&out->kdf.u.argon2.cpus, &kdf, "cpus"))
 	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Missing Argon2i parameters");
     }
   else if (!grub_strcmp (type, "pbkdf2"))
@@ -432,6 +434,7 @@ luks2_decrypt_key (grub_uint8_t *out_key,
   switch (k->kdf.type)
     {
       case LUKS2_KDF_TYPE_ARGON2I:
+      case LUKS2_KDF_TYPE_ARGON2ID:
 	ret = grub_error (GRUB_ERR_BAD_ARGUMENT, "Argon2 not supported");
 	goto err;
       case LUKS2_KDF_TYPE_PBKDF2:
Signature · 1 line
2.25.1
#22 lore raw

PATCH v2 3/6 argon2: Import Argon2 from cryptsetup

In reply to Patrick Steinhardt

In order to support the Argon2 key derival function for LUKS2, we obviously need to implement Argon2. It doesn't make a lot of sense to hand-code any crypto, which is why this commit instead imports Argon2 from the cryptsetup project. This commit thus imports the code from the official reference implementation located at [1]. The code is licensed under CC0 1.0 Universal/Apache 2.0. Given that both LGPLv2.1+ and Apache 2.0 are compatible with GPLv3, it should be fine to import that code.

The code is imported from commit 62358ba (Merge pull request #270 from bitmark-property-system/master, 2019-05-20). To make it work for GRUB, several adjustments were required that have beed documented in "grub-dev.texi".

[1]: https://github.com/P-H-C/phc-winner-argon2

---
 docs/grub-dev.texi                            |  64 +++
 grub-core/Makefile.core.def                   |   8 +
 grub-core/lib/argon2/argon2.c                 | 232 ++++++++
 grub-core/lib/argon2/argon2.h                 | 264 +++++++++
 grub-core/lib/argon2/blake2/blake2-impl.h     | 151 +++++
 grub-core/lib/argon2/blake2/blake2.h          |  89 +++
 grub-core/lib/argon2/blake2/blake2b.c         | 388 +++++++++++++
 .../lib/argon2/blake2/blamka-round-ref.h      |  56 ++
 grub-core/lib/argon2/core.c                   | 525 ++++++++++++++++++
 grub-core/lib/argon2/core.h                   | 228 ++++++++
 grub-core/lib/argon2/ref.c                    | 190 +++++++
 11 files changed, 2195 insertions(+)
 create mode 100644 grub-core/lib/argon2/argon2.c
 create mode 100644 grub-core/lib/argon2/argon2.h
 create mode 100644 grub-core/lib/argon2/blake2/blake2-impl.h
 create mode 100644 grub-core/lib/argon2/blake2/blake2.h
 create mode 100644 grub-core/lib/argon2/blake2/blake2b.c
 create mode 100644 grub-core/lib/argon2/blake2/blamka-round-ref.h
 create mode 100644 grub-core/lib/argon2/core.c
 create mode 100644 grub-core/lib/argon2/core.h
 create mode 100644 grub-core/lib/argon2/ref.c
docs/grub-dev.texi +64 -0
index df2350be0..490af8b01 100644
@@ -489,10 +489,74 @@ GRUB includes some code from other projects, and it is sometimes necessary
 to update it.
 
 @menu
+* Argon2::
 * Gnulib::
 * jsmn::
 @end menu
 
+@node Argon2
+@section Argon2
+
+Argon2 is a key derivation function used by LUKS2 in order to derive encryption
+keys from a user-provided password. GRUB imports the official reference
+implementation of Argon2 from @url{https://github.com/P-H-C/phc-winner-argon2}.
+In order to make the library usable for GRUB, we need to perform various
+conversions. This is mainly due to the fact that the imported code makes use of
+types and functions defined in the C standard library, which isn't available.
+Furthermore, using the POSIX wrapper library is not possible as the code needs
+to be part of the kernel.
+
+Updating the code can thus be performed like following:
+
+@example
+$ git clone https://github.com/P-H-C/phc-winner-argon2 argon2
+$ cp argon2/include/argon2.h argon2/src/@{argon2.c,core.c,core.h,ref.c@} \
+    grub-core/lib/argon2/
+$ cp argon2/src/blake2/@{blake2-impl.h,blake2.h,blake2b.c,blamka-round-ref.h@} \
+    grub-core/lib/argon2/blake2/
+$ sed -e 's/UINT32_C/GRUB_UINT32_C/g' \
+      -e 's/UINT64_C/GRUB_UINT64_C/g' \
+      -e 's/UINT32_MAX/GRUB_UINT32_MAX/g' \
+      -e 's/CHAR_BIT/GRUB_CHAR_BIT/g' \
+      -e 's/UINT_MAX/GRUB_UINT_MAX/g' \
+      -e 's/uintptr_t/grub_addr_t/g' \
+      -e 's/size_t/grub_size_t/g' \
+      -e 's/uint32_t/grub_uint32_t/g' \
+      -e 's/uint64_t/grub_uint64_t/g' \
+      -e 's/uint8_t/grub_uint8_t/g' \
+      -e 's/memset/grub_memset/g' \
+      -e 's/memcpy/grub_memcpy/g' \
+      -e 's/malloc/grub_malloc/g' \
+      -e 's/free/grub_free/g' \
+      -e 's/#elif _MSC_VER/#elif defined(_MSC_VER)/' \
+      grub-core/lib/argon2/@{*,blake2/*@}.@{c,h@} -i
+@end example
+
+Afterwards, you need to perform the following manual steps:
+
+@enumerate
+@item Remove all includes of standard library headers, "encoding.h" and
+      "thread.h".
+@item Add includes <grub/mm.h> and <grub/misc.h> to "argon2.h".
+@item Add include <grub/dl.h> and module license declaration to "argon2.c".
+@item Remove the following declarations and functions from "argon2.h" and
+     "argon2.c": argon2_type2string, argon2i_hash_encoded, argon2i_hash_raw,
+     argon2d_hash_encoded, argon2d_hash_raw, argon2id_hash_encoded,
+     argon2id_hash_raw, argon2_compare, argon2_verify, argon2i_verify,
+     argon2d_verify, argon2id_verify, argon2d_ctx, argon2i_ctx, argon2id_ctx,
+     argon2_verify_ctx, argon2d_verify_ctx, argon2i_verify_ctx,
+     argon2id_verify_ctx, argon2_encodedlen.
+@item Move the declaration of `clear_internal_memory()` in "blake2-impl.h" to
+      "blake2b.c".
+@item Remove code guarded by the ARGON2_NO_THREADS macro.
+@item Remove parameters `encoded` and `encodedlen` from `argon2_hash` and remove
+      the encoding block in that function.
+@item Remove parameter verifications in `validate_inputs()` for
+      ARGON2_MIN_PWD_LENGTH, ARGON2_MIN_SECRET, ARGON2_MIN_AD_LENGTH and
+      ARGON2_MAX_MEMORY to fix compiler warnings.
+@item Mark the function argon2_ctx as static.
+@end enumerate
+
 @node Gnulib
 @section Gnulib
 
grub-core/Makefile.core.def +8 -0
index a0507a1fa..7e96cb1ce 100644
@@ -1181,6 +1181,14 @@ module = {
   common = lib/json/json.c;
 };
 
+module = {
+  name = argon2;
+  common = lib/argon2/argon2.c;
+  common = lib/argon2/core.c;
+  common = lib/argon2/ref.c;
+  common = lib/argon2/blake2/blake2b.c;
+};
+
 module = {
   name = afsplitter;
   common = disk/AFSplitter.c;
grub-core/lib/argon2/argon2.c new file +232 -0

232 lines not on this page · raw message

grub-core/lib/argon2/argon2.h new file +264 -0

264 lines not on this page · raw message

grub-core/lib/argon2/blake2/blake2-impl.h new file +151 -0

151 lines not on this page · raw message

grub-core/lib/argon2/blake2/blake2.h new file +89 -0

89 lines not on this page · raw message

grub-core/lib/argon2/blake2/blake2b.c new file +388 -0

388 lines not on this page · raw message

grub-core/lib/argon2/blake2/blamka-round-ref.h new file +56 -0

56 lines not on this page · raw message

grub-core/lib/argon2/core.c new file +525 -0

525 lines not on this page · raw message

grub-core/lib/argon2/core.h new file +228 -0

228 lines not on this page · raw message

grub-core/lib/argon2/ref.c new file +190 -0

190 lines not on this page · raw message

Signature · 1 line
2.25.1
#27 lore raw

PATCH v2 6/6 luks2: Support key derival via Argon2

In reply to Patrick Steinhardt

One addition with LUKS2 was support of the key derival function Argon2 in addition to the previously supported PBKDF2 algortihm. In order to ease getting in initial support for LUKS2, we only reused infrastructure to support LUKS2 with PBKDF2, but left out Argon2.

This commit now introduces support for Argon2 to enable decryption of LUKS2 partitions using this key derival function. As the code for Argon2 has been added in a previous commit in this series, adding support is now trivial.

---
 Makefile.util.def           |  6 +++++-
 grub-core/Makefile.core.def |  2 +-
 grub-core/disk/luks2.c      | 13 +++++++++++--
 3 files changed, 17 insertions(+), 4 deletions(-)
Makefile.util.def +5 -1
index 94336392b..a50effce4 100644
@@ -3,7 +3,7 @@ AutoGen definitions Makefile.tpl;
 library = {
   name = libgrubkern.a;
   cflags = '$(CFLAGS_GNULIB)';
-  cppflags = '$(CPPFLAGS_GNULIB) -I$(srcdir)/grub-core/lib/json';
+  cppflags = '$(CPPFLAGS_GNULIB) -I$(srcdir)/grub-core/lib/json -I$(srcdir)/grub-core/lib/argon2';
 
   common = util/misc.c;
   common = grub-core/kern/command.c;
@@ -36,6 +36,10 @@ library = {
   common = grub-core/kern/misc.c;
   common = grub-core/kern/partition.c;
   common = grub-core/lib/crypto.c;
+  common = grub-core/lib/argon2/argon2.c;
+  common = grub-core/lib/argon2/core.c;
+  common = grub-core/lib/argon2/ref.c;
+  common = grub-core/lib/argon2/blake2/blake2b.c;
   common = grub-core/lib/json/json.c;
   common = grub-core/disk/luks.c;
   common = grub-core/disk/luks2.c;
grub-core/Makefile.core.def +1 -1
index 7e96cb1ce..7ffd26528 100644
@@ -1204,7 +1204,7 @@ module = {
   common = disk/luks2.c;
   common = lib/gnulib/base64.c;
   cflags = '$(CFLAGS_POSIX) $(CFLAGS_GNULIB)';
-  cppflags = '$(CPPFLAGS_POSIX) $(CPPFLAGS_GNULIB) -I$(srcdir)/lib/json';
+  cppflags = '$(CPPFLAGS_POSIX) $(CPPFLAGS_GNULIB) -I$(srcdir)/lib/json -I$(srcdir)/lib/argon2';
 };
 
 module = {
grub-core/disk/luks2.c +11 -2
index 767631198..3c79f14aa 100644
@@ -27,6 +27,7 @@
 #include <grub/partition.h>
 #include <grub/i18n.h>
 
+#include <argon2.h>
 #include <base64.h>
 #include <json.h>
 
@@ -435,8 +436,16 @@ luks2_decrypt_key (grub_uint8_t *out_key,
     {
       case LUKS2_KDF_TYPE_ARGON2I:
       case LUKS2_KDF_TYPE_ARGON2ID:
-	ret = grub_error (GRUB_ERR_BAD_ARGUMENT, "Argon2 not supported");
-	goto err;
+	ret = argon2_hash (k->kdf.u.argon2.time, k->kdf.u.argon2.memory, k->kdf.u.argon2.cpus,
+			   passphrase, passphraselen, salt, saltlen, area_key, k->area.key_size,
+			   k->kdf.type == LUKS2_KDF_TYPE_ARGON2I ? Argon2_i : Argon2_id,
+			   ARGON2_VERSION_NUMBER);
+        if (ret)
+	  {
+	    grub_dprintf ("luks2", "Argon2 failed: %s\n", argon2_error_message (ret));
+	    goto err;
+	  }
+        break;
       case LUKS2_KDF_TYPE_PBKDF2:
 	hash = grub_crypto_lookup_md_by_name (k->kdf.u.pbkdf2.hash);
 	if (!hash)
Signature · 1 line
2.25.1
#29 lore raw
In reply to Patrick Steinhardt

Hi Patrick,

Quoted 11 lines · Patrick Steinhardt

this is the second version of my patchset to add support for Argon2 encryption keys for LUKS2.

The most important change is that I've now verbosely imported the argon2 code from the official reference implementation instead of from the cryptsetup project. The diff between both isn't that big in the end, and including from crypsetup's upstream seems a bit cleaner to me. There were several transformations required to use GRUB's types and functions as well as stripping of unused stuff, which I've now documented the dev manual. This also fixes my previously mistaken license headers.

Quoted 8 lines

One thing I'm not sure about here is whether it's fine to declare the argon2 mod's license as GPLv3. The code is licensed under CC0/Apache 2.0, where the latter is compatible with GPLv3. But I don't know whether it's legit to just say "Yeah, this mod is a GPLv3 one".

I didn't address the comment made by Leif yet with regards to grabbing memory. I ain't got much of a clue of GRUB's memory subsystem, so I'd gladly accept help there. Otherwise I'll have to dig a bit deeper.

That's fair enough. I think we could do something halfway clever to resolve that, or we could do something quick and simple, but either would be better than moving to reserving 50%.

So could you reply to my email on that thread with some info with regards to the specific memory requirements, and whether they are precise or "this much seems to always work"?

Regards,

Leif

Quoted 114 lines

The range diff compared to the previous version of this patch set is attached to this mail.

Patrick

Patrick Steinhardt (6):
efi: Allocate half of available memory by default types.h: add UINT-related macros needed for Argon2
argon2: Import Argon2 from cryptsetup
luks2: Add missing newline to debug message
luks2: Discern Argon2i and Argon2id
luks2: Support key derival via Argon2

 Makefile.util.def                             |   6 +-
 docs/grub-dev.texi                            |  64 +++
 grub-core/Makefile.core.def                   |  10 +-
 grub-core/disk/luks2.c                        |  28 +-
 grub-core/kern/efi/mm.c                       |   4 +-
 grub-core/lib/argon2/argon2.c                 | 232 ++++++++
 grub-core/lib/argon2/argon2.h                 | 264 +++++++++
 grub-core/lib/argon2/blake2/blake2-impl.h     | 151 +++++
 grub-core/lib/argon2/blake2/blake2.h          |  89 +++
 grub-core/lib/argon2/blake2/blake2b.c         | 388 +++++++++++++
 .../lib/argon2/blake2/blamka-round-ref.h      |  56 ++
 grub-core/lib/argon2/core.c                   | 525 ++++++++++++++++++
 grub-core/lib/argon2/core.h                   | 228 ++++++++
 grub-core/lib/argon2/ref.c                    | 190 +++++++
 include/grub/types.h                          |   8 +
 15 files changed, 2231 insertions(+), 12 deletions(-)
 create mode 100644 grub-core/lib/argon2/argon2.c
 create mode 100644 grub-core/lib/argon2/argon2.h
 create mode 100644 grub-core/lib/argon2/blake2/blake2-impl.h
 create mode 100644 grub-core/lib/argon2/blake2/blake2.h
 create mode 100644 grub-core/lib/argon2/blake2/blake2b.c
 create mode 100644 grub-core/lib/argon2/blake2/blamka-round-ref.h
 create mode 100644 grub-core/lib/argon2/core.c
 create mode 100644 grub-core/lib/argon2/core.h
 create mode 100644 grub-core/lib/argon2/ref.c

Range-diff against v1:

1:  53cdfdc27 = 1:  15bdf830e efi: Allocate half of available memory by default
2:  c55946ca5 < -:  --------- argon2: Import Argon2 from cryptsetup
-:  --------- > 2:  e81db7d95 types.h: add UINT-related macros needed for Argon2
-:  --------- > 3:  50aff9670 argon2: Import Argon2 from cryptsetup
3:  c17cd2197 ! 4:  af3f85665 disk: luks2: Add missing newline to debug message

@@ Metadata
Author: Patrick Steinhardt <[email protected]>

## Commit message ##
- disk: luks2: Add missing newline to debug message
+ luks2: Add missing newline to debug message

The debug message printed when decryption with a keyslot fails is missing its trailing newline. Add it to avoid mangling it with subsequent output.

+ Reviewed-by: Daniel Kiper <[email protected]>

## grub-core/disk/luks2.c ##
@@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t disk,
4: 390728cea ! 5: 89abe827b disk: luks2: Discern Argon2i and Argon2id
@@ Metadata
Author: Patrick Steinhardt <[email protected]>

## Commit message ##
- disk: luks2: Discern Argon2i and Argon2id
+ luks2: Discern Argon2i and Argon2id

While GRUB is already able to parse both Argon2i and Argon2id parameters from the LUKS2 header, it doesn't discern both types. This commit
5: ec4389627 ! 6: 70a354e0b disk: luks2: Support key derival via Argon2

    @@ Metadata
     Author: Patrick Steinhardt <[email protected]>
     
      ## Commit message ##
    -    disk: luks2: Support key derival via Argon2
    +    luks2: Support key derival via Argon2
     
         One addition with LUKS2 was support of the key derival function Argon2
         in addition to the previously supported PBKDF2 algortihm. In order to
    @@ Makefile.util.def: library = {
        common = grub-core/kern/partition.c;
        common = grub-core/lib/crypto.c;
     +  common = grub-core/lib/argon2/argon2.c;
    ++  common = grub-core/lib/argon2/core.c;
    ++  common = grub-core/lib/argon2/ref.c;
     +  common = grub-core/lib/argon2/blake2/blake2b.c;
        common = grub-core/lib/json/json.c;
        common = grub-core/disk/luks.c;
    @@ grub-core/disk/luks2.c: luks2_decrypt_key (grub_uint8_t *out_key,
            case LUKS2_KDF_TYPE_ARGON2ID:
     -	ret = grub_error (GRUB_ERR_BAD_ARGUMENT, "Argon2 not supported");
     -	goto err;
    -+	ret = grub_crypto_argon2 (passphrase, passphraselen, salt, saltlen,
    -+				  k->kdf.u.argon2.time, k->kdf.u.argon2.memory, k->kdf.u.argon2.cpus,
    -+				  k->kdf.type == LUKS2_KDF_TYPE_ARGON2I ? GRUB_ARGON2_I : GRUB_ARGON2_ID,
    -+				  GRUB_ARGON2_VERSION_NUMBER,
    -+				  area_key, k->area.key_size);
    ++	ret = argon2_hash (k->kdf.u.argon2.time, k->kdf.u.argon2.memory, k->kdf.u.argon2.cpus,
    ++			   passphrase, passphraselen, salt, saltlen, area_key, k->area.key_size,
    ++			   k->kdf.type == LUKS2_KDF_TYPE_ARGON2I ? Argon2_i : Argon2_id,
    ++			   ARGON2_VERSION_NUMBER);
     +        if (ret)
     +	  {
    -+	    grub_dprintf ("luks2", "Argon2 failed: %s\n", grub_errmsg);
    ++	    grub_dprintf ("luks2", "Argon2 failed: %s\n", argon2_error_message (ret));
     +	    goto err;
     +	  }
     +        break;
Signature · 1 line
2.25.1
#3 lore raw

Re: [PATCH 1/5] efi: Allocate half of available memory by default

In reply to Leif Lindholm
Quoted 22 lines · Leif Lindholm
Quoted 14 lines · Patrick Steinhardt

By default, GRUB will allocate a quarter of the pages it got available in the EFI subsystem. On many current systems, this will amount to roughly 800MB of RAM assuming an address space of 32 bits. This is plenty for most use cases, but it doesn't suffice when using full disk encryption with a key derival function based on Argon2.

Besides the usual iteration count known from PBKDF2, Argon2 introduces two additional parameters "memory" and "parallelism". While the latter doesn't really matter to us, the memory parameter is quite interesting. If encrypting a partition with LUKS2 using Argon2 as KDF, then cryptsetup will default to a memory parameter of 1GB. Meaning we need to allocate a buffer of 1GB in size in order to be able to derive the key, which definitely won't squeeze into the limit of 800MB.

Quoted 3 lines

To prepare for Argon2, let's thus increase the default and make half of memory available, instead of a quarter only. This amounts to about 1600MB on above systems, which is sufficient for Argon2.

I was never a huge fan of the "grab a percentage of RAM" in the first place, and I think "grab twice that" is not the best solution here.

Quoted 7 lines

(Real) corner cases that would be affected by this are:
1) chainloading grub from grub
2) OS loaders (loaded by GRUB) requiring large amounts of RAM before ExitBootsevices().

If you have a known minimum requirement, can we work towards that instead?

Quoting Milan from another mail in this thread:

Quoted 8 lines · Milan Broz

Anyway, there are some limits in cryptsetup - we try to never use more than half of physical memory and maximum is hard-compiled to 4GiB.

(But physical memory limit applies when formatting device, then
is stored in the LUKS2 keyslot header. So if you format it on device with much larger RAM and it is later not available, it fails to open.
It is more complicated though - we have benchmark during format that prioritize unlocking time, so PBKDF memory is usually decreased on low-memory systems anyway.)

As far as I can see, the default memory cost for Argon2 is 1,048,576kB, at least that is what "configure.ac" says for "luks2-memory-kb". That also matches my experience with cryptsetup 2.2.2.

Patrick

#14 lore raw
In reply to Milan Broz
Quoted 9 lines · Milan Broz
Quoted 6 lines · Daniel Kiper
Quoted 2 lines · Patrick Steinhardt

- The import of Argon2 itself. I've imported code from the cryptsetup project, but I've modified it quite a bit to fit into

Milan mentioned something about libgcrypt. Milan, when the Argon2 code may land in libgcrypt?

Once we have volunteer to implement it / port it to gcrypt :-)

I'm not familiar with how the gcrypt project itself operates. In case it's only a simple port similar to what I'm currently doing for GRUB, then I would be happy to invest that time.

Quoted 3 lines

BTW if you have embedded Argon2 code, you should also add some test vectors to your testsuite.
(You can use these we have in cryptsetup - see tests/crypto-vectors.c)

I didn't include test vectors for v2 yet, but will do so as soon as we get closer to something shippable.

Patrick

#30 lore raw
In reply to Patrick Steinhardt

Hi Patrick,

Quoted 13 lines · Patrick Steinhardt

Hi,

this is the second version of my patchset to add support for Argon2 encryption keys for LUKS2.

The most important change is that I've now verbosely imported the argon2 code from the official reference implementation instead of from the cryptsetup project. The diff between both isn't that big in the end, and including from crypsetup's upstream seems a bit cleaner to me. There were several transformations required to use GRUB's types and functions as well as stripping of unused stuff, which I've now documented the dev manual. This also fixes my previously mistaken license headers.

Quoted 4 lines

One thing I'm not sure about here is whether it's fine to declare the argon2 mod's license as GPLv3. The code is licensed under CC0/Apache 2.0, where the latter is compatible with GPLv3. But I don't know whether it's legit to just say "Yeah, this mod is a GPLv3 one".

Could you give me a reference to the doc/spec or what not which says that it is legit to do that? I will consult this with GNU legal folks.

Quoted 3 lines

I didn't address the comment made by Leif yet with regards to grabbing memory. I ain't got much of a clue of GRUB's memory subsystem, so I'd gladly accept help there. Otherwise I'll have to dig a bit deeper.

I will skip this part at this stage of review.

Daniel

#21 lore raw

Re: [PATCH v2 2/6] types.h: add UINT-related macros needed for Argon2

In reply to Patrick Steinhardt
Quoted 22 lines · Patrick Steinhardt

For the upcoming import of the Argon2 library, we need the macros GRUB_UINT32_MAX, GRUB_UINT32_C and GRUB_UINT64_C. Add them as a preparatory step.

---
 include/grub/types.h | 8 ++++++++
 1 file changed, 8 insertions(+)
include/grub/types.h +1 -0
index 035a4b528..35ba900dd 100644
@@ -137,6 +137,7 @@ typedef grub_int32_t	grub_ssize_t;
 #define GRUB_SHRT_MAX 0x7fff
 #define GRUB_SHRT_MIN (-GRUB_SHRT_MAX - 1)
 #define GRUB_UINT_MAX 4294967295U
+#define GRUB_UINT32_MAX 4294967295U
 #define GRUB_INT_MAX 0x7fffffff
 #define GRUB_INT_MIN (-GRUB_INT_MAX - 1)
 #define GRUB_INT32_MAX 2147483647

include/grub/types.h typedef grub_int32_t grub_ssize_t; · line 158

Quoted 7 lines
include/grub/types.h +4 -0
@@ -151,6 +152,13 @@ typedef grub_int32_t	grub_ssize_t;
 #endif
 # define GRUB_LONG_MIN (-GRUB_LONG_MAX - 1)
 
+# define GRUB_UINT32_C(x) x ## U
+# if GRUB_ULONG_MAX >> 31 >> 31 >> 1 == 1
+#  define GRUB_UINT64_C(x) x##UL
+# elif 1

#else?

Daniel

#23 lore raw

Re: [PATCH v2 3/6] argon2: Import Argon2 from cryptsetup

In reply to Patrick Steinhardt
Quoted 8 lines · Patrick Steinhardt

In order to support the Argon2 key derival function for LUKS2, we obviously need to implement Argon2. It doesn't make a lot of sense to hand-code any crypto, which is why this commit instead imports Argon2 from the cryptsetup project. This commit thus imports the code from the official reference implementation located at [1]. The code is licensed under CC0 1.0 Universal/Apache 2.0. Given that both LGPLv2.1+ and Apache 2.0 are compatible with GPLv3, it should be fine to import that code.

Quoted 8 lines

The code is imported from commit 62358ba (Merge pull request #270 from bitmark-property-system/master, 2019-05-20). To make it work for GRUB, several adjustments were required that have beed documented in "grub-dev.texi".

[1]: https://github.com/P-H-C/phc-winner-argon2

LGTM, however, I will give my RB when legal things are resolved...

Daniel

Reviewed-by on v2 5/6
#26 lore raw

Re: [PATCH v2 5/6] luks2: Discern Argon2i and Argon2id

In reply to Patrick Steinhardt
Quoted 6 lines · Patrick Steinhardt

While GRUB is already able to parse both Argon2i and Argon2id parameters from the LUKS2 header, it doesn't discern both types. This commit introduces a new KDF type for Argon2id and sets up the parsed KDF's type accordingly.

Daniel

Reviewed-by on v2 6/6
#28 lore raw

Re: [PATCH v2 6/6] luks2: Support key derival via Argon2

In reply to Patrick Steinhardt
Quoted 11 lines · Patrick Steinhardt

One addition with LUKS2 was support of the key derival function Argon2 in addition to the previously supported PBKDF2 algortihm. In order to ease getting in initial support for LUKS2, we only reused infrastructure to support LUKS2 with PBKDF2, but left out Argon2.

This commit now introduces support for Argon2 to enable decryption of LUKS2 partitions using this key derival function. As the code for Argon2 has been added in a previous commit in this series, adding support is now trivial.

Daniel

#31 lore raw
In reply to Daniel Kiper
Quoted 8 lines · Daniel Kiper
Quoted 4 lines · Patrick Steinhardt

One thing I'm not sure about here is whether it's fine to declare the argon2 mod's license as GPLv3. The code is licensed under CC0/Apache 2.0, where the latter is compatible with GPLv3. But I don't know whether it's legit to just say "Yeah, this mod is a GPLv3 one".

Could you give me a reference to the doc/spec or what not which says that it is legit to do that? I will consult this with GNU legal folks.

With "that" you probably mean combining Apache 2.0 and GPLv3, right? I've taken this from GNU's list of licenses [1], which explicitly mentions Apache 2.0 as compatible with version 3 of the GNU GPL.

Patrick

[1]: https://www.gnu.org/licenses/license-list.html#apache2