xref: /optee_os/core/lib/libtomcrypt/src/pk/asn1/der/bit/der_length_bit_string.c (revision 8411e6ad673d20c4742ed30c785e3f5cdea54dfa)
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 #include "tomcrypt_private.h"
4 
5 /**
6   @file der_length_bit_string.c
7   ASN.1 DER, get length of BIT STRING, Tom St Denis
8 */
9 
10 #ifdef LTC_DER
11 /**
12   Gets length of DER encoding of BIT STRING
13   @param nbits  The number of bits in the string to encode
14   @param outlen [out] The length of the DER encoding for the given string
15   @return CRYPT_OK if successful
16 */
der_length_bit_string(unsigned long nbits,unsigned long * outlen)17 int der_length_bit_string(unsigned long nbits, unsigned long *outlen)
18 {
19    unsigned long nbytes, x;
20    int err;
21 
22    LTC_ARGCHK(outlen != NULL);
23 
24    /* get the number of the bytes */
25    nbytes = (nbits >> 3) + ((nbits & 7) ? 1 : 0) + 1;
26 
27    if ((err = der_length_asn1_length(nbytes, &x)) != CRYPT_OK) {
28       return err;
29    }
30    *outlen = 1 + x + nbytes;
31 
32    return CRYPT_OK;
33 }
34 
35 #endif
36 
37