]> pd.if.org Git - zpackage/blob - libtomcrypt/src/pk/asn1/der/octet/der_length_octet_string.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / pk / asn1 / der / octet / der_length_octet_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_octet_string.c
13   ASN.1 DER, get length of OCTET STRING, Tom St Denis
14 */
15
16 #ifdef LTC_DER
17 /**
18   Gets length of DER encoding of OCTET STRING
19   @param noctets  The number of octets 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_octet_string(unsigned long noctets, unsigned long *outlen)
24 {
25    LTC_ARGCHK(outlen != NULL);
26
27    if (noctets < 128) {
28       /* 04 LL DD DD DD ... */
29       *outlen = 2 + noctets;
30    } else if (noctets < 256) {
31       /* 04 81 LL DD DD DD ... */
32       *outlen = 3 + noctets;
33    } else if (noctets < 65536UL) {
34       /* 04 82 LL LL DD DD DD ... */
35       *outlen = 4 + noctets;
36    } else if (noctets < 16777216UL) {
37       /* 04 83 LL LL LL DD DD DD ... */
38       *outlen = 5 + noctets;
39    } else {
40       return CRYPT_INVALID_ARG;
41    }
42
43    return CRYPT_OK;
44 }
45
46 #endif
47
48
49 /* ref:         $Format:%D$ */
50 /* git commit:  $Format:%H$ */
51 /* commit time: $Format:%ai$ */