Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/crypto/ed25519: use array pointers for keys #24376

Closed
FlorianUekermann opened this issue Mar 13, 2018 · 5 comments
Closed

x/crypto/ed25519: use array pointers for keys #24376

FlorianUekermann opened this issue Mar 13, 2018 · 5 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@FlorianUekermann
Copy link
Contributor

FlorianUekermann commented Mar 13, 2018

Most other packages in crypto seem to use array pointers (*[n]byte) for keys instead of slices where the size is known. ed25519 uses []byte for both private and public keys.

Since #395 is still unresolved there no way to retrieve array pointers in a safe way, which makes mixing these conventions especially useful (resolving #24350 will likely use ed25519.GenerateKey, but should probably array pointers to remain consistent with it's friends at nacl/*).

/cc @agl

@gopherbot gopherbot added this to the Unreleased milestone Mar 13, 2018
@andybons andybons added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 13, 2018
@agl
Copy link
Contributor

agl commented Mar 13, 2018

The older crypto packages were written with the assumption that #395 would be resolved. However, that hasn't happened and using array pointers is kind of a pain without out. Ed25519 is newer and so uses a different convention.

If anything, unless #395 is going to be fixed, I'd suggest converting the older packages to taking slices rather than converting ed25519 to take array pointers. (However, I think the benefits of a stable API outweigh that consideration.)

@agl agl closed this as completed Mar 13, 2018
@FlorianUekermann
Copy link
Contributor Author

Could you give me a hint what the problem with array pointers for keys is?

It seems to me that the lack of a fix for #395 is an argument against slices (since conversion to slice via [:] is simple, but the opposite impossible).
Looking at the internals of ed25519 (as an example), the lower level code seems to use arrays anyway and gets cluttered with copy-conversions to arrays and checks like "if l := len(publicKey); l != PublicKeySize"
For the user it has the downside that sizes are much less obvious and not compile time checked.

I understand if you want to keep the API stable for this one, but for future additions I don't see the redeeming features of key slices.

Keys with variable size are a completely different situation of course.

@agl
Copy link
Contributor

agl commented Mar 13, 2018

I think the discussion on #395 covers it pretty well: if you're throwing bytes around in Go, you're probably using []byte. E.g. a message from the network will be a []byte and the signature might be at the end of it. For APIs which take array pointers you then have to have a local [64]byte for the signature, copy the signature from the message to the local variable, and then pass a pointer to the variable to Verify. It's sadly clunky.

I agree about the benefits of array pointers which is why I initially used them, but it was in the expectation of being able to cast from slices to array pointers to avoid that clunkiness.

@agl
Copy link
Contributor

agl commented Mar 13, 2018

(p.s. to be clear: if adding signature support to the nacl package, I think consistency with the existing API in that package is a decent reason to use array pointers there.)

@FlorianUekermann
Copy link
Contributor Author

Thanks I see your point now. I'm not sure sacrificing the more elegant and safe interface is worth the potential convenience here, but that's obviously a matter of opinion.

Maybe the crypto package should just provided a set of conversion functions that use unsafe:

func Arr64(s []byte) *[64]byte
func Arr32(s []byte) *[32]byte
...

That would be about as convenient (and safe) as a proper fix to #395. But I guess that's a separate issue.

@golang golang locked and limited conversation to collaborators Mar 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants