• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, tomstdenis@gmail.com, http://libtomcrypt.com
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 */
der_encode_object_identifier(unsigned long * words,unsigned long nwords,unsigned char * out,unsigned long * outlen)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, wordbuf;
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       *outlen = x;
43       return CRYPT_BUFFER_OVERFLOW;
44    }
45 
46    /* compute length to store OID data */
47    z = 0;
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);
52        if (y < nwords - 1) {
53           wordbuf = words[y + 1];
54        }
55    }
56 
57    /* store header + length */
58    x = 0;
59    out[x++] = 0x06;
60    if (z < 128) {
61       out[x++] = (unsigned char)z;
62    } else if (z < 256) {
63       out[x++] = 0x81;
64       out[x++] = (unsigned char)z;
65    } else if (z < 65536UL) {
66       out[x++] = 0x82;
67       out[x++] = (unsigned char)((z>>8)&255);
68       out[x++] = (unsigned char)(z&255);
69    } else {
70       return CRYPT_INVALID_ARG;
71    }
72 
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;
78         if (t) {
79            y    = x;
80            mask = 0;
81            while (t) {
82                out[x++] = (unsigned char)((t & 0x7F) | mask);
83                t    >>= 7;
84                mask  |= 0x80;  /* upper bit is set on all but the last byte */
85            }
86            /* now swap bytes y...x-1 */
87            z = x - 1;
88            while (y < z) {
89                t = out[y]; out[y] = out[z]; out[z] = (unsigned char)t;
90                ++y;
91                --z;
92            }
93        } else {
94           /* zero word */
95           out[x++] = 0x00;
96        }
97 
98        if (i < nwords - 1) {
99           wordbuf = words[i + 1];
100        }
101    }
102 
103    *outlen = x;
104    return CRYPT_OK;
105 }
106 
107 #endif
108 
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 $ */
112