]> pd.if.org Git - zpackage/blob - libtomcrypt/src/pk/asn1/der/bit/der_length_bit_string.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / pk / asn1 / der / bit / der_length_bit_string.c
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2  *
3  * LibTomCrypt is a library that provides various cryptographic
4  * algorithms in a highly modular and flexible manner.
5  *
6  * The library is free for all purposes without any express
7  * guarantee it works.
8  */
9 #include "tomcrypt.h"
10
11 /**
12   @file der_length_bit_string.c
13   ASN.1 DER, get length of BIT STRING, Tom St Denis
14 */
15
16 #ifdef LTC_DER
17 /**
18   Gets length of DER encoding of BIT STRING
19   @param nbits  The number of bits in the string to encode
20   @param outlen [out] The length of the DER encoding for the given string
21   @return CRYPT_OK if successful
22 */
23 int der_length_bit_string(unsigned long nbits, unsigned long *outlen)
24 {
25    unsigned long nbytes;
26    LTC_ARGCHK(outlen != NULL);
27
28    /* get the number of the bytes */
29    nbytes = (nbits >> 3) + ((nbits & 7) ? 1 : 0) + 1;
30
31    if (nbytes < 128) {
32       /* 03 LL PP DD DD DD ... */
33       *outlen = 2 + nbytes;
34    } else if (nbytes < 256) {
35       /* 03 81 LL PP DD DD DD ... */
36       *outlen = 3 + nbytes;
37    } else if (nbytes < 65536) {
38       /* 03 82 LL LL PP DD DD DD ... */
39       *outlen = 4 + nbytes;
40    } else {
41       return CRYPT_INVALID_ARG;
42    }
43
44    return CRYPT_OK;
45 }
46
47 #endif
48
49
50 /* ref:         $Format:%D$ */
51 /* git commit:  $Format:%H$ */
52 /* commit time: $Format:%ai$ */