Skip to contentSkip to conversation

Questions about keyctl_dh_compute_kdf

DISCUSSION Ben Boeckel 3 participants 4 messages Sep 24, 2018 Keys/keyrings

lore
Share & tools
lore
#0 lore raw

Hi,

I've been updating rust-keyutils[1] to have support for the KDF support (as a PR since it hasn't landed yet) and I've noticed a few things:

long keyctl_dh_compute_kdf(
key_serial_t private, key_serial_t prime, key_serial_t base, char *hashname, char *otherinfo, size_t otherinfolen, char *buffer, size_t buflen);

- Should hashname be a const char*? Does the kernel have any reason to write here?
- Should otherinfo be a const unsigned char*? Maybe const void*? If the caller specifies the format, why is it non-const?

The manpage mentions:

The KDF returns the requested number of bytes specified with the genlen or the buflen parameter depending on the invoked function.

but genlen isn't mentioned anywhere else in the manpage. I assume the length is calculated by passing a NULL buffer, but that's just based on other APIs in keyutils.

The keyctl_dh_compute_kdf function is also missing from the RETURN VALUE section of the manpage.

In addition:

The hashname specifies the Linux kernel crypto API name for a hash that shall be used for the key derivation function, such as sha256. The hashname must be a NULL terminated string

A reference to the manpage or header (hash_info.h?) listing these this would be great.

Thanks!

--Ben

[1]https://github.com/mathstuf/rust-keyutils

#1 lore raw

Am Montag, 24. September 2018, 13:28:38 CEST schrieb Ben Boeckel:

Hi Ben,

Quoted 10 lines

Hi,

I've been updating rust-keyutils[1] to have support for the KDF support (as a PR since it hasn't landed yet) and I've noticed a few things:

long keyctl_dh_compute_kdf(
key_serial_t private, key_serial_t prime, key_serial_t base, char *hashname, char *otherinfo, size_t otherinfolen, char *buffer, size_t buflen);

Quoted 4 lines

- Should hashname be a const char*? Does the kernel have any reason to write here?
- Should otherinfo be a const unsigned char*? Maybe const void*? If the caller specifies the format, why is it non-const?

You are correct, both should be const. The data type of char is appropriate and should not be turned into a void. If you have a specific data type for the otherinfo data, I think it would be appropriate for the developer to simply type-cast it. In general, I would like to avoid a void unless there is no other way.

Quoted 9 lines

The manpage mentions:

The KDF returns the requested number of bytes specified with the genlen or the buflen parameter depending on the invoked function.

but genlen isn't mentioned anywhere else in the manpage. I assume the length is calculated by passing a NULL buffer, but that's just based on other APIs in keyutils.

Actually this is not the case here: you have a KDF which generates exactly the requested amount of data unlike the other functions which return a fixed amount of data.

Quoted 3 lines

The keyctl_dh_compute_kdf function is also missing from the RETURN VALUE section of the manpage.

The return value is identical as described for the other functions. But you are right, it needs to be added to the man page.

Would you care for sending a patch?

Quoted 9 lines

In addition:

The hashname specifies the Linux kernel crypto API name for a hash that shall be used for the key derivation function, such as sha256. The hashname must be a NULL terminated string

A reference to the manpage or header (hash_info.h?) listing these this would be great.

There is no header file where this is listed. Instead, /proc/crypto is your friend if you have already registered hashes. But you can request new hashes of they have not been loaded (or the kernel modules are not yet insmod'ed) where the entry will pop up in /proc/crypto at the loading time. Further, it is permissible to use the "generic name" as well as the "driver name" (see / proc/crypto for examples).

Quoted 6 lines

Thanks!

--Ben

[1]https://github.com/mathstuf/rust-keyutils

Thanks.

Ciao
Stephan

#2 lore raw
In reply to Ben Boeckel
Quoted 14 lines · Stephan Mueller

Am Montag, 24. September 2018, 13:28:38 CEST schrieb Ben Boeckel:

Quoted 8 lines

I've been updating rust-keyutils[1] to have support for the KDF support (as a PR since it hasn't landed yet) and I've noticed a few things:

long keyctl_dh_compute_kdf(
key_serial_t private, key_serial_t prime, key_serial_t base, char *hashname, char *otherinfo, size_t otherinfolen, char *buffer, size_t buflen);

Quoted 4 lines

- Should hashname be a const char*? Does the kernel have any reason to write here?
- Should otherinfo be a const unsigned char*? Maybe const void*? If the caller specifies the format, why is it non-const?

Quoted 5 lines

You are correct, both should be const. The data type of char is appropriate and should not be turned into a void. If you have a specific data type for the otherinfo data, I think it would be appropriate for the developer to simply type-cast it. In general, I would like to avoid a void unless there is no other way.

Avoiding void* is sensible. But const char* makes me think of C-strings. Maybe const uint8_t* to indicate that it is arbitrary data? Though looking at other APIs, there's mostly void* AFAICS (not an excuse, just an observation).

I also see that otherinfolen is uint32_t on its way into the kernel, but size_t on the API. This can cause silent truncation of the buffer size. Should it be uint32_t in the API as well?

Quoted 5 lines
Quoted 5 lines

The manpage mentions:

The KDF returns the requested number of bytes specified with the genlen or the buflen parameter depending on the invoked function.

Quoted 7 lines
Quoted 3 lines

but genlen isn't mentioned anywhere else in the manpage. I assume the length is calculated by passing a NULL buffer, but that's just based on other APIs in keyutils.

Actually this is not the case here: you have a KDF which generates exactly the requested amount of data unlike the other functions which return a fixed amount of data.

OK, I'll look at rewording to make it clearer then. That's different than other APIs.

Quoted 7 lines
Quoted 2 lines

The keyctl_dh_compute_kdf function is also missing from the RETURN VALUE section of the manpage.

The return value is identical as described for the other functions. But you are right, it needs to be added to the man page.

Would you care for sending a patch?

Incoming.

Quoted 9 lines
Quoted 8 lines

In addition:

The hashname specifies the Linux kernel crypto API name for a hash that shall be used for the key derivation function, such as sha256. The hashname must be a NULL terminated string

A reference to the manpage or header (hash_info.h?) listing these this would be great.

Quoted 6 lines

There is no header file where this is listed. Instead, /proc/crypto is your friend if you have already registered hashes. But you can request new hashes of they have not been loaded (or the kernel modules are not yet insmod'ed) where the entry will pop up in /proc/crypto at the loading time. Further, it is permissible to use the "generic name" as well as the "driver name" (see / proc/crypto for examples).

OK, I'll add a reference to inspect that file then.

--Ben

maintainer
#3 lore raw
In reply to Ben Boeckel
Quoted 5 lines · Ben Boeckel
Quoted 5 lines
Quoted 4 lines

- Should hashname be a const char*? Does the kernel have any reason to write here?
- Should otherinfo be a const unsigned char*? Maybe const void*? If the caller specifies the format, why is it non-const?

Quoted 7 lines
Quoted 5 lines

You are correct, both should be const. The data type of char is appropriate and should not be turned into a void. If you have a specific data type for the otherinfo data, I think it would be appropriate for the developer to simply type-cast it. In general, I would like to avoid a void unless there is no other way.

Avoiding void* is sensible.

IMO, "(const) void *" should be used for arbitrary or opaque data rather than "(const) char *" or "(const) unsigned char *" so that you don't need to typecast the pointer.

Quoted 2 lines

But const char* makes me think of C-strings. Maybe const uint8_t* to indicate that it is arbitrary data?

Don't switch for "(const) char *" to "const uint8_t *" please. If someone's already using "char *" their compiler may start complaining as "char" may be implicitly a signed number and you would be switching to explicitly unsigned.

Quoted 2 lines

Though looking at other APIs, there's mostly void* AFAICS (not an excuse, just an observation).

Yep.

David