]> pd.if.org Git - zpackage/blob - libtomcrypt/src/pk/asn1/der/printable_string/der_length_printable_string.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / pk / asn1 / der / printable_string / der_length_printable_string.c
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 #include "tomcrypt.h"
10
11 /**
12   @file der_length_printable_string.c
13   ASN.1 DER, get length of Printable STRING, Tom St Denis
14 */
15
16 #ifdef LTC_DER
17
18 static const struct {
19    int code, value;
20 } printable_table[] = {
21 { ' ', 32 },
22 { '\'', 39 },
23 { '(', 40 },
24 { ')', 41 },
25 { '+', 43 },
26 { ',', 44 },
27 { '-', 45 },
28 { '.', 46 },
29 { '/', 47 },
30 { '0', 48 },
31 { '1', 49 },
32 { '2', 50 },
33 { '3', 51 },
34 { '4', 52 },
35 { '5', 53 },
36 { '6', 54 },
37 { '7', 55 },
38 { '8', 56 },
39 { '9', 57 },
40 { ':', 58 },
41 { '=', 61 },
42 { '?', 63 },
43 { 'A', 65 },
44 { 'B', 66 },
45 { 'C', 67 },
46 { 'D', 68 },
47 { 'E', 69 },
48 { 'F', 70 },
49 { 'G', 71 },
50 { 'H', 72 },
51 { 'I', 73 },
52 { 'J', 74 },
53 { 'K', 75 },
54 { 'L', 76 },
55 { 'M', 77 },
56 { 'N', 78 },
57 { 'O', 79 },
58 { 'P', 80 },
59 { 'Q', 81 },
60 { 'R', 82 },
61 { 'S', 83 },
62 { 'T', 84 },
63 { 'U', 85 },
64 { 'V', 86 },
65 { 'W', 87 },
66 { 'X', 88 },
67 { 'Y', 89 },
68 { 'Z', 90 },
69 { 'a', 97 },
70 { 'b', 98 },
71 { 'c', 99 },
72 { 'd', 100 },
73 { 'e', 101 },
74 { 'f', 102 },
75 { 'g', 103 },
76 { 'h', 104 },
77 { 'i', 105 },
78 { 'j', 106 },
79 { 'k', 107 },
80 { 'l', 108 },
81 { 'm', 109 },
82 { 'n', 110 },
83 { 'o', 111 },
84 { 'p', 112 },
85 { 'q', 113 },
86 { 'r', 114 },
87 { 's', 115 },
88 { 't', 116 },
89 { 'u', 117 },
90 { 'v', 118 },
91 { 'w', 119 },
92 { 'x', 120 },
93 { 'y', 121 },
94 { 'z', 122 },
95 };
96
97 int der_printable_char_encode(int c)
98 {
99    int x;
100    for (x = 0; x < (int)(sizeof(printable_table)/sizeof(printable_table[0])); x++) {
101        if (printable_table[x].code == c) {
102           return printable_table[x].value;
103        }
104    }
105    return -1;
106 }
107
108 int der_printable_value_decode(int v)
109 {
110    int x;
111    for (x = 0; x < (int)(sizeof(printable_table)/sizeof(printable_table[0])); x++) {
112        if (printable_table[x].value == v) {
113           return printable_table[x].code;
114        }
115    }
116    return -1;
117 }
118
119 /**
120   Gets length of DER encoding of Printable STRING
121   @param octets   The values you want to encode
122   @param noctets  The number of octets in the string to encode
123   @param outlen   [out] The length of the DER encoding for the given string
124   @return CRYPT_OK if successful
125 */
126 int der_length_printable_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen)
127 {
128    unsigned long x;
129
130    LTC_ARGCHK(outlen != NULL);
131    LTC_ARGCHK(octets != NULL);
132
133    /* scan string for validity */
134    for (x = 0; x < noctets; x++) {
135        if (der_printable_char_encode(octets[x]) == -1) {
136           return CRYPT_INVALID_ARG;
137        }
138    }
139
140    if (noctets < 128) {
141       /* 16 LL DD DD DD ... */
142       *outlen = 2 + noctets;
143    } else if (noctets < 256) {
144       /* 16 81 LL DD DD DD ... */
145       *outlen = 3 + noctets;
146    } else if (noctets < 65536UL) {
147       /* 16 82 LL LL DD DD DD ... */
148       *outlen = 4 + noctets;
149    } else if (noctets < 16777216UL) {
150       /* 16 83 LL LL LL DD DD DD ... */
151       *outlen = 5 + noctets;
152    } else {
153       return CRYPT_INVALID_ARG;
154    }
155
156    return CRYPT_OK;
157 }
158
159 #endif
160
161
162 /* ref:         $Format:%D$ */
163 /* git commit:  $Format:%H$ */
164 /* commit time: $Format:%ai$ */