1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3  *
4  * LibTomCrypt is a library that provides various cryptographic
5  * algorithms in a highly modular and flexible manner.
6  *
7  * The library is free for all purposes without any express
8  * guarantee it works.
9  */
10 #include "tomcrypt_private.h"
11 
12 /**
13   @file der_encode_short_integer.c
14   ASN.1 DER, encode an integer, Tom St Denis
15 */
16 
17 
18 #ifdef LTC_DER
19 
20 /**
21   Store a short integer in the range (0,2^32-1)
22   @param num      The integer to encode
23   @param out      [out] The destination for the DER encoded integers
24   @param outlen   [in/out] The max size and resulting size of the DER encoded integers
25   @return CRYPT_OK if successful
26 */
der_encode_short_integer(unsigned long num,unsigned char * out,unsigned long * outlen)27 int der_encode_short_integer(unsigned long num, unsigned char *out, unsigned long *outlen)
28 {
29    unsigned long len, x, y, z;
30    int           err;
31 
32    LTC_ARGCHK(out    != NULL);
33    LTC_ARGCHK(outlen != NULL);
34 
35    /* force to 32 bits */
36    num &= 0xFFFFFFFFUL;
37 
38    /* find out how big this will be */
39    if ((err = der_length_short_integer(num, &len)) != CRYPT_OK) {
40       return err;
41    }
42 
43    if (*outlen < len) {
44       *outlen = len;
45       return CRYPT_BUFFER_OVERFLOW;
46    }
47 
48    /* get len of output */
49    z = 0;
50    y = num;
51    while (y) {
52      ++z;
53      y >>= 8;
54    }
55 
56    /* handle zero */
57    if (z == 0) {
58       z = 1;
59    }
60 
61    /* see if msb is set */
62    z += (num&(1UL<<((z<<3) - 1))) ? 1 : 0;
63 
64    /* adjust the number so the msB is non-zero */
65    for (x = 0; (z <= 4) && (x < (4 - z)); x++) {
66       num <<= 8;
67    }
68 
69    /* store header */
70    x = 0;
71    out[x++] = 0x02;
72    out[x++] = (unsigned char)z;
73 
74    /* if 31st bit is set output a leading zero and decrement count */
75    if (z == 5) {
76       out[x++] = 0;
77       --z;
78    }
79 
80    /* store values */
81    for (y = 0; y < z; y++) {
82       out[x++] = (unsigned char)((num >> 24) & 0xFF);
83       num    <<= 8;
84    }
85 
86    /* we good */
87    *outlen = x;
88 
89    return CRYPT_OK;
90 }
91 
92 #endif
93 
94 /* ref:         $Format:%D$ */
95 /* git commit:  $Format:%H$ */
96 /* commit time: $Format:%ai$ */
97