comparison libtomcrypt/src/pk/asn1/der/object_identifier/der_encode_object_identifier.c @ 285:1b9e69c058d2

propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3) to branch 'au.asn.ucc.matt.dropbear' (head fdf4a7a3b97ae5046139915de7e40399cceb2c01)
author Matt Johnston <matt@ucc.asn.au>
date Wed, 08 Mar 2006 13:23:58 +0000
parents
children 0cbe8f6dbf9e
comparison
equal deleted inserted replaced
281:997e6f7dc01e 285:1b9e69c058d2
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_encode_object_identifier.c
15 ASN.1 DER, Encode Object Identifier, Tom St Denis
16 */
17
18 #ifdef LTC_DER
19 /**
20 Encode an OID
21 @param words The words to encode (upto 32-bits each)
22 @param nwords The number of words in the OID
23 @param out [out] Destination of OID data
24 @param outlen [in/out] The max and resulting size of the OID
25 @return CRYPT_OK if successful
26 */
27 int der_encode_object_identifier(unsigned long *words, unsigned long nwords,
28 unsigned char *out, unsigned long *outlen)
29 {
30 unsigned long i, x, y, z, t, mask;
31 int err;
32
33 LTC_ARGCHK(words != NULL);
34 LTC_ARGCHK(out != NULL);
35 LTC_ARGCHK(outlen != NULL);
36
37 /* check length */
38 if ((err = der_length_object_identifier(words, nwords, &x)) != CRYPT_OK) {
39 return err;
40 }
41 if (x > *outlen) {
42 return CRYPT_BUFFER_OVERFLOW;
43 }
44
45 /* compute length to store OID data */
46 z = 1;
47 for (y = 2; y < nwords; y++) {
48 t = der_object_identifier_bits(words[y]);
49 z += t/7 + ((t%7) ? 1 : 0);
50 }
51
52 /* store header + length */
53 x = 0;
54 out[x++] = 0x06;
55 if (z < 128) {
56 out[x++] = z;
57 } else if (z < 256) {
58 out[x++] = 0x81;
59 out[x++] = z;
60 } else if (z < 65536UL) {
61 out[x++] = 0x82;
62 out[x++] = (z>>8)&255;
63 out[x++] = z&255;
64 } else {
65 return CRYPT_INVALID_ARG;
66 }
67
68 /* store first byte */
69 out[x++] = words[0] * 40 + words[1];
70
71 for (i = 2; i < nwords; i++) {
72 /* store 7 bit words in little endian */
73 t = words[i] & 0xFFFFFFFF;
74 if (t) {
75 y = x;
76 mask = 0;
77 while (t) {
78 out[x++] = (t & 0x7F) | mask;
79 t >>= 7;
80 mask |= 0x80; /* upper bit is set on all but the last byte */
81 }
82 /* now swap bytes y...x-1 */
83 z = x - 1;
84 while (y < z) {
85 t = out[y]; out[y] = out[z]; out[z] = t;
86 ++y;
87 --z;
88 }
89 } else {
90 /* zero word */
91 out[x++] = 0x00;
92 }
93 }
94
95 *outlen = x;
96 return CRYPT_OK;
97 }
98
99 #endif
100
101 /* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/object_identifier/der_encode_object_identifier.c,v $ */
102 /* $Revision: 1.1 $ */
103 /* $Date: 2005/05/16 15:08:11 $ */