1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 /* SPDX-License-Identifier: Unlicense */ 3 #include "tomcrypt_private.h" 4 5 /** 6 @file der_length_short_integer.c 7 ASN.1 DER, get length of encoding, Tom St Denis 8 */ 9 10 11 #ifdef LTC_DER 12 /** 13 Gets length of DER encoding of num 14 @param num The integer to get the size of 15 @param outlen [out] The length of the DER encoding for the given integer 16 @return CRYPT_OK if successful 17 */ der_length_short_integer(unsigned long num,unsigned long * outlen)18int der_length_short_integer(unsigned long num, unsigned long *outlen) 19 { 20 unsigned long z, y; 21 int err; 22 23 LTC_ARGCHK(outlen != NULL); 24 25 /* force to 32 bits */ 26 num &= 0xFFFFFFFFUL; 27 28 /* get the number of bytes */ 29 z = 0; 30 y = num; 31 while (y) { 32 ++z; 33 y >>= 8; 34 } 35 36 /* handle zero */ 37 if (z == 0) { 38 z = 1; 39 } else if ((num&(1UL<<((z<<3) - 1))) != 0) { 40 /* in case msb is set */ 41 ++z; 42 } 43 44 if ((err = der_length_asn1_length(z, &y)) != CRYPT_OK) { 45 return err; 46 } 47 *outlen = 1 + y + z; 48 49 return CRYPT_OK; 50 } 51 52 #endif 53