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
12 @file der_encode_object_identifier.c
13 ASN.1 DER, Encode Object Identifier, Tom St Denis
19 @param words The words to encode (upto 32-bits each)
20 @param nwords The number of words in the OID
21 @param out [out] Destination of OID data
22 @param outlen [in/out] The max and resulting size of the OID
23 @return CRYPT_OK if successful
25 int der_encode_object_identifier(unsigned long *words, unsigned long nwords,
26 unsigned char *out, unsigned long *outlen)
28 unsigned long i, x, y, z, t, mask, wordbuf;
31 LTC_ARGCHK(words != NULL);
32 LTC_ARGCHK(out != NULL);
33 LTC_ARGCHK(outlen != NULL);
36 if ((err = der_length_object_identifier(words, nwords, &x)) != CRYPT_OK) {
41 return CRYPT_BUFFER_OVERFLOW;
44 /* compute length to store OID data */
46 wordbuf = words[0] * 40 + words[1];
47 for (y = 1; y < nwords; y++) {
48 t = der_object_identifier_bits(wordbuf);
49 z += t/7 + ((t%7) ? 1 : 0) + (wordbuf == 0 ? 1 : 0);
51 wordbuf = words[y + 1];
55 /* store header + length */
59 out[x++] = (unsigned char)z;
62 out[x++] = (unsigned char)z;
63 } else if (z < 65536UL) {
65 out[x++] = (unsigned char)((z>>8)&255);
66 out[x++] = (unsigned char)(z&255);
68 return CRYPT_INVALID_ARG;
71 /* store first byte */
72 wordbuf = words[0] * 40 + words[1];
73 for (i = 1; i < nwords; i++) {
74 /* store 7 bit words in little endian */
75 t = wordbuf & 0xFFFFFFFF;
80 out[x++] = (unsigned char)((t & 0x7F) | mask);
82 mask |= 0x80; /* upper bit is set on all but the last byte */
84 /* now swap bytes y...x-1 */
87 t = out[y]; out[y] = out[z]; out[z] = (unsigned char)t;
97 wordbuf = words[i + 1];
107 /* ref: $Format:%D$ */
108 /* git commit: $Format:%H$ */
109 /* commit time: $Format:%ai$ */