Runtime: Question: how to compare nonce if it's normalized?

Created on 8 May 2020  路  4Comments  路  Source: dotnet/runtime

From doc , Rfc3161TimestampRequest.CreateFromHash has a parameter nonce, may be normalized:

nonce
Nullable<ReadOnlyMemory<Byte>>
An optional nonce (number used once) to uniquely identify this request to pair it with the response. The value is interpreted as an unsigned big-endian integer and may be normalized to the encoding format.

So I have an issue about comparing nonce from the request and the nonce from the response (Rfc3161TimestampTokenInfo.GetNonce()), as the latter one may be normalized.
And they're different as follows:
nonce passed in request:
00-66-58-91-33-16-53-22-5C-25-55-82-AD-44-F6-59-DD-7C-6D-48-D1-64-8F-55-CC-1A-07-42-BA-AC-09-51
nonce received:
66-58-91-33-16-53-22-5C-25-55-82-AD-44-F6-59-DD-7C-6D-48-D1-64-8F-55-CC-1A-07-42-BA-AC-09-51
The normalization seems removed the leading zeros.
Do you have any suggestion about comparing the nonce, so that we can get the right results? Thanks!

area-System.Security question untriaged

Most helpful comment

The nonce in RFC3161 is actually a signed INTEGER. The leading zeros won't always be removed, just insignificant ones. If the nonce is {0, 0, 0xFF, 1, 2, 3} for example, it will be normalized to {0, 0xFF, 1, 2, 3}, since {0xFF, 1, 2, 3} would mean a negative number.

Rfc3161TimestampRequest normalizes the nonce on construction, so if are using a random nonce and need the normalized form if it, use GetNonce() on the request.

byte[] nonce = new byte[] { 0, 1, 2, 3 };
Rfc3161TimestampRequest req = Rfc3161TimestampRequest.CreateFromData(default, HashAlgorithmName.SHA256, nonce: nonce);
ReadOnlyMemory<byte>? normalizedNonce = req.GetNonce(); // will be {1, 2, 3}

All 4 comments

Tagging subscribers to this area: @bartonjs, @vcsjones, @krwq
Notify danmosemsft if you want to be subscribed.

The nonce in RFC3161 is actually a signed INTEGER. The leading zeros won't always be removed, just insignificant ones. If the nonce is {0, 0, 0xFF, 1, 2, 3} for example, it will be normalized to {0, 0xFF, 1, 2, 3}, since {0xFF, 1, 2, 3} would mean a negative number.

Rfc3161TimestampRequest normalizes the nonce on construction, so if are using a random nonce and need the normalized form if it, use GetNonce() on the request.

byte[] nonce = new byte[] { 0, 1, 2, 3 };
Rfc3161TimestampRequest req = Rfc3161TimestampRequest.CreateFromData(default, HashAlgorithmName.SHA256, nonce: nonce);
ReadOnlyMemory<byte>? normalizedNonce = req.GetNonce(); // will be {1, 2, 3}

Thanks for your help @vcsjones !

@heng-liu closing issue since the question seems to be answered. Please re-open if you feel like there are more questions or some follow-up work

Was this page helpful?
0 / 5 - 0 ratings