1

I am playing around with ssh keys with ED25519. I have seen a couple of libraries playing with generating the public/private key pair. Another parsing the openssh private key to get the key, but the challenge I'm running into is getting the pk value from the public key.

From OpenSSH https://github.com/openssh/openssh-portable/blob/master/PROTOCOL.certkeys

I find this:

pk is the encoded Ed25519 public key as defined by draft-josefsson-eddsa-ed25519-03.

So I am trying to get the actual public key value (32 bits) from the public key, and then encode them.

My question(s):\

  1. Can someone explain how to extract the actual public key from the the ssh key public key (removing all the extra stuff and leaving just the key)?

  2. Could someone put in human speak (or code) an example of encoding the public key?

(I'm doing this in python, cryptography.hazmat doesn't support this yet. I've waded through the C code for openssh keygen signing, which needs the pk, and I'm not familiar enough with C to dig through the pointers to pointers, etc to figure out well enough what it's doing.)

Happy to do more research if needs be, but just looking for a next place to look.

3

1- Can someone explain how to extract the actual public key from the the ssh key public key (removing all the extra stuff and leaving just the key)?

The format of public key is described as the part of known_hosts section of manual page for sshd:

keytype, base64-encoded key, comment.

So lets create a key to see what is there.

$ ssh-keygen -t ed25519 -f ed25519
$ cat ed25519.pub 
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIE8C+wgMJ89PV5cQZxfCpq+GB/gZ0zO7JQhnVBIh2gOj comment

Using cut we can select only the key blob, which is LV (length value) structure, containing (once again) key name and then the actual PK value:

$ cat ed25519.pub | cut -d' ' -f2 | base64 -d | hexdump -C
00000000  00 00 00 0b 73 73 68 2d  65 64 32 35 35 31 39 00  |....ssh-ed25519.|
00000010  00 00 20 4f 02 fb 08 0c  27 cf 4f 57 97 10 67 17  |.. O....'.OW..g.|
00000020  c2 a6 af 86 07 f8 19 d3  33 bb 25 08 67 54 12 21  |........3.%.gT.!|
00000030  da 03 a3                                          |...|
00000033

We can once again get rid of the key name to get the string representing the PK only. The Length is represented always using 4 bytes: 00 00 00 0b (11) is the length of the ssh-ed25519 string (hex: 73 73 68 2d 65 64 32 35 35 31 39) and then follows the PK length 00 00 00 20 (always 32 bytes) and the key itself (hexadecimal):

4f 02 fb 08 0c 27 cf 4f 57 97 10 67 17 c2 a6 af 86 07 f8 19 d3 33 bb 25 08 67 54 12 21 da 03 a3

2- Could someone put in human speak (or code) an example of encoding the public key?

The above is bash and manual version of extracting the PK value from OpenSSH public key. I believe you will be able to implement that in Python from the above description.

  • 1
    Thanks! This is what I was looking for! Using this info as well I've figured out a similar way to explore the private ssh key. Thanks again for your help! – John Jun 4 '17 at 18:08
  • You are welcome. Happy hacking :) – Jakuje Jun 4 '17 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.