<div dir="auto"><div>The DER (Distinguished Encoding Rules of ASN.1, which can be found in ITU-T recommendation X.680 and X.681) requirement is that if a particular number is positive, the highest-order bit can never be set (since the highest-order bit is always the negative sign indicator). The rules further explain that the appropriate way to encode a positive integer with the highest bit set is to add an additional 0x00 byte, making the new most significant bit into a 0.<div dir="auto"><br></div><div dir="auto">This is also why the serial number field in a certificate can be 21 bytes long, even when implementing PKIX's minimum serial number length of 20 bytes.  Numbers included in an ASN.1-encoded structure aren't ever intended to be used directly, they're intended to be decoded before the data is passed back to client code.</div><div dir="auto"><br></div>I realize this is much more information than you asked, but it might help to think of it as "it's done this way for compliance with the standards".</div><div dir="auto"><br></div><div dir="auto">-Kyle H</div><div dir="auto"><br><div class="gmail_quote" dir="auto"><div dir="ltr">On Thu, Nov 29, 2018, 21:26 M K Saravanan <<a href="mailto:mksarav@gmail.com">mksarav@gmail.com</a> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
When I create static DH key pair using openssl, why the public key and<br>
prime contains the prefix 0x00 byte?<br>
<br>
For e.g. in 1024 bit key, 128 bytes is enough.<br>
<br>
private key properly shows 128 bytes.  But public key and prime shows<br>
129 bytes with a 0x00 byte at the beginning.  What is the need for<br>
this 0x00 byte at the beginning?  i.e. why it is using 129 bytes<br>
instead of 128?<br>
<br>
<br>
$ openssl version<br>
OpenSSL 1.1.1  11 Sep 2018<br>
<br>
$ openssl dhparam -out mydhp.pem 1024<br>
[...]<br>
<br>
$ openssl genpkey -paramfile ./mydhp.pem -out mydhkey.pem<br>
<br>
$ openssl pkey -in ./mydhkey.pem -text -noout<br>
DH Private-Key: (1024 bit)<br>
    private-key:<br>
        52:61:87:52:b4:27:5f:c3:cf:ab:2f:20:b4:aa:b7:<br>
        df:c3:87:63:50:d2:06:dd:65:8f:db:55:2e:08:d5:<br>
        62:44:1a:f5:d8:73:66:fe:a7:c4:43:be:f7:f0:d0:<br>
        ba:4c:bf:f0:70:70:c9:25:92:da:ef:69:01:1a:b9:<br>
        d9:d9:1f:b9:22:a6:84:48:d8:58:a8:a4:9e:7f:85:<br>
        6b:9e:45:89:07:0c:fb:00:f1:0a:fb:24:10:e4:bb:<br>
        2b:1c:7d:dc:d1:12:a3:21:5a:9b:8e:bf:9d:33:e8:<br>
        65:fe:c2:5c:ea:47:fa:00:04:80:cf:85:e1:c6:71:<br>
        67:4b:7b:71:92:07:59:48<br>
    public-key:<br>
        00:a0:0d:41:8a:27:55:07:2a:01:dd:a7:e2:86:bb:<br>
        69:71:86:1d:62:0c:f3:b7:61:78:81:37:6c:a1:d3:<br>
        e8:55:9d:8a:1f:e8:5e:7f:18:00:0f:4e:1d:97:70:<br>
        a0:e7:19:2b:82:69:c3:aa:61:ea:b8:9c:10:36:19:<br>
        e9:b9:13:db:9a:ef:34:bf:10:f7:93:84:5d:a3:b4:<br>
        58:3a:40:ec:4b:79:06:52:b8:fe:b8:22:0d:f3:f9:<br>
        33:1e:8e:43:69:bb:77:3d:10:78:c6:65:e8:04:08:<br>
        96:1e:cc:6c:92:e4:55:f4:2c:d0:3d:b7:5f:58:70:<br>
        cf:fe:a7:5f:23:e3:d9:5e:c4<br>
    prime:<br>
        00:a2:f4:9d:1c:3f:75:8f:3e:e3:c9:95:09:79:09:<br>
        16:f2:f0:61:c4:e1:b9:23:22:a3:58:d7:38:7d:06:<br>
        af:57:ad:14:5e:13:bd:71:ed:31:89:cb:65:d6:46:<br>
        3b:29:57:ad:a9:8e:58:e6:df:c0:37:2f:4f:be:45:<br>
        d7:c8:f1:87:ef:af:65:87:34:4a:7d:78:b8:0b:0b:<br>
        33:d8:c1:fb:05:9e:ce:9a:27:7e:4a:2a:aa:18:33:<br>
        35:ea:d0:b0:b7:fa:cb:d1:51:bf:11:98:12:24:be:<br>
        1d:1c:87:c3:37:ed:0f:b9:53:23:fc:a1:be:75:ed:<br>
        81:04:e5:6a:b3:83:40:e0:43<br>
    generator: 2 (0x2)<br>
<br>
<br>
with regards,<br>
Saravanan<br>
-- <br>
openssl-users mailing list<br>
To unsubscribe: <a href="https://mta.openssl.org/mailman/listinfo/openssl-users" rel="noreferrer noreferrer" target="_blank">https://mta.openssl.org/mailman/listinfo/openssl-users</a><br>
</blockquote></div></div></div>