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