c++asn.1der

ASN.1 REAL binary base 2 encoding mantissa normalization


I am trying to DER encode REAL data in binary 2 base form using C++. I calculate the mantissa and exponent with the following algorithm.

sample data = 32.3125

  1. using C++ std::frexp function, extract double mantissa and int exponent. mantissa 0.5048828125 exponent 6
  2. Convert the mantissa to integer by multiplying by 2 and decreasing exponent by 1 till integer mantissa is got. mantissa 517 exponent -4
  3. Finally the data is encoded as 09 04 80(binary 2 base) FC(exp) 02 05(mantissa)

Is this encoding correct? In the standard(X.690-0207), it talks about mantissa representation in a different form.

8.5.6 When binary encoding is used (bit 8 = 1), then if the mantissa M is non-zero, it shall be represented by a sign S, a positive integer value N and a binary scaling factor F, such that: M = S × N × 2F

In the Canonical Encoding Rules and the Distinguished Encoding Rules normalization is specified and the mantissa (unless it is 0) needs to be repeatedly shifted until the least significant bit is a 1.

Is this necessary to convert the mantissa in this format and encode N and F, or is it ok to keep the F as '0' as in my example?


Solution

  • Your encoding is correct. Note that the requirements for DER are covered in X.690 11.3, and you have met them (you are using base 2, your mantissa is odd, and F = 0). F != 0 is only ever needed when encoding using base 8 or 16.