Skip to content

siglen parameter in crypto_sign_signature is prone to API misuse #789

@davidchisnall

Description

@davidchisnall

The crypto_sign_signature function takes a pointer that it will set to the length of the output buffer. This is less than ideal for good ergonomics:

  • The parameter makes it appear as if the length of the signature is dynamic, but it is not, it is always MLDSA_CRYPTO_BYTES.
  • The shape of the parameter pair (buffer followed by length) is sufficiently close to other uses where it is an input parameter that callers may expect it to be set on input.
  • It isn't checked internally, so if a caller does set it and it has the wrong value, then this is ignored.
  • A check of the size after the call is almost never useful. There are two cases:
    • The caller provided a buffer that is too large. The check may enable a call to realloc or some other approach to trim the buffer (though given that the size is invariant, this is almost always an error).
    • The caller provided a buffer that is too small. In this case, the check of the returned size happens after the library has written past the end of a buffer and therefore the state of the value is undefined.

All of this adds up to a dangerous API pattern, where people write code that appears defensive but will actually corrupt memory.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions