1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
9 * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
14 @file der_encode_object_identifier.c
15 ASN.1 DER, Encode Object Identifier, Tom St Denis
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
27 int der_encode_object_identifier(unsigned long *words, unsigned long nwords,
28 unsigned char *out, unsigned long *outlen)
30 unsigned long i, x, y, z, t, mask, wordbuf;
33 LTC_ARGCHK(words != NULL);
34 LTC_ARGCHK(out != NULL);
35 LTC_ARGCHK(outlen != NULL);
38 if ((err = der_length_object_identifier(words, nwords, &x)) != CRYPT_OK) {
43 return CRYPT_BUFFER_OVERFLOW;
46 /* compute length to store OID data */
48 wordbuf = words[0] * 40 + words[1];
49 for (y = 1; y < nwords; y++) {
50 t = der_object_identifier_bits(wordbuf);
51 z += t/7 + ((t%7) ? 1 : 0) + (wordbuf == 0 ? 1 : 0);
53 wordbuf = words[y + 1];
57 /* store header + length */
61 out[x++] = (unsigned char)z;
64 out[x++] = (unsigned char)z;
65 } else if (z < 65536UL) {
67 out[x++] = (unsigned char)((z>>8)&255);
68 out[x++] = (unsigned char)(z&255);
70 return CRYPT_INVALID_ARG;
73 /* store first byte */
74 wordbuf = words[0] * 40 + words[1];
75 for (i = 1; i < nwords; i++) {
76 /* store 7 bit words in little endian */
77 t = wordbuf & 0xFFFFFFFF;
82 out[x++] = (unsigned char)((t & 0x7F) | mask);
84 mask |= 0x80; /* upper bit is set on all but the last byte */
86 /* now swap bytes y...x-1 */
89 t = out[y]; out[y] = out[z]; out[z] = (unsigned char)t;
99 wordbuf = words[i + 1];
109 /* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/object_identifier/der_encode_object_identifier.c,v $ */
110 /* $Revision: 1.6 $ */
111 /* $Date: 2006/12/04 21:34:03 $ */