comparison src/pk/asn1/der/object_identifier/der_length_object_identifier.c @ 209:39d5d58461d6 libtomcrypt-orig LTC_1.05

Import of libtomcrypt 1.05
author Matt Johnston <matt@ucc.asn.au>
date Wed, 06 Jul 2005 03:53:40 +0000
parents
children d5faf4814ddb
comparison
equal deleted inserted replaced
191:1c15b283127b 209:39d5d58461d6
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 * Tom St Denis, [email protected], http://libtomcrypt.org
10 */
11 #include "tomcrypt.h"
12
13 /**
14 @file der_length_object_identifier.c
15 ASN.1 DER, get length of Object Identifier, Tom St Denis
16 */
17
18 #ifdef LTC_DER
19
20 unsigned long der_object_identifier_bits(unsigned long x)
21 {
22 unsigned long c;
23 x &= 0xFFFFFFFF;
24 c = 0;
25 while (x) {
26 ++c;
27 x >>= 1;
28 }
29 return c;
30 }
31
32
33 /**
34 Gets length of DER encoding of Object Identifier
35 @param nwords The number of OID words
36 @param words The actual OID words to get the size of
37 @param outlen [out] The length of the DER encoding for the given string
38 @return CRYPT_OK if successful
39 */
40 int der_length_object_identifier(unsigned long *words, unsigned long nwords, unsigned long *outlen)
41 {
42 unsigned long y, z, t;
43
44 LTC_ARGCHK(words != NULL);
45 LTC_ARGCHK(outlen != NULL);
46
47
48 /* must be >= 2 words */
49 if (nwords < 2) {
50 return CRYPT_INVALID_ARG;
51 }
52
53 /* word1 = 0,1,2 and word2 0..39 */
54 if (words[0] > 2 || words[1] > 39) {
55 return CRYPT_INVALID_ARG;
56 }
57
58 /* leading byte of first two words */
59 z = 1;
60 for (y = 2; y < nwords; y++) {
61 t = der_object_identifier_bits(words[y]);
62 z += t/7 + ((t%7) ? 1 : 0);
63 }
64
65 /* now depending on the length our length encoding changes */
66 if (z < 128) {
67 z += 2;
68 } else if (z < 256) {
69 z += 3;
70 } else if (z < 65536UL) {
71 z += 4;
72 } else {
73 return CRYPT_INVALID_ARG;
74 }
75
76 *outlen = z;
77 return CRYPT_OK;
78 }
79
80 #endif
81
82 /* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/object_identifier/der_length_object_identifier.c,v $ */
83 /* $Revision: 1.1 $ */
84 /* $Date: 2005/05/16 15:08:11 $ */