Skip to content

Commit 2386ebf

Browse files
committed
split-up into multiple C files
... and slightly optimize multiple things, e.g. `DEK-Info` decoding Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
1 parent cfc4f62 commit 2386ebf

File tree

4 files changed

+314
-253
lines changed

4 files changed

+314
-253
lines changed

src/headers/tomcrypt_private.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ int base64_encode_pem(const unsigned char *in, unsigned long inlen,
218218

219219
/* PEM related */
220220

221+
#ifdef LTC_PEM
221222
struct password {
222223
/* usually a `char*` but could also contain binary data
223224
* so use a `void*` + length to be on the safe side.
@@ -241,7 +242,12 @@ struct str {
241242
#define SET_STR(n, s) n.p = s, n.len = XSTRLEN(s)
242243
#define SET_CSTR(n, s) n.p = (char*)s, n.len = XSTRLEN(s)
243244
#define COPY_STR(n, s, l) do { XMEMCPY(n.p, s, l); n.len = l; } while(0)
244-
#define FREE_STR(n) do { n.p = NULL; n.len = 0; } while(0)
245+
#define RESET_STR(n) do { n.p = NULL; n.len = 0; } while(0)
246+
247+
struct dek_info_from_str {
248+
const struct str id;
249+
struct dek_info info;
250+
};
245251

246252
enum more_headers {
247253
no,
@@ -265,14 +271,21 @@ struct pem_headers {
265271
struct password *pw;
266272
};
267273

274+
extern const struct pem_header_id pem_std_headers[];
275+
extern const unsigned long pem_std_headers_num;
276+
extern const struct str pem_proc_type_encrypted;
277+
extern const struct str pem_dek_info_start;
278+
extern const struct dek_info_from_str pem_dek_infos[];
279+
extern const unsigned long pem_dek_infos_num;
280+
268281
struct bufp {
269282
/* `end` points to one byte after the last
270283
* element of the allocated buffer
271284
*/
272-
char *p, *r, *end;
285+
char *start, *work, *end;
273286
};
274287

275-
#define SET_BUFP(n, d, l) n.p = (char*)d, n.r = (char*)d, n.end = (char*)d + l + 1
288+
#define SET_BUFP(n, d, l) n.start = (char*)d, n.work = (char*)d, n.end = (char*)d + l + 1
276289

277290
struct get_char {
278291
int (*get)(struct get_char*);
@@ -283,6 +296,7 @@ struct get_char {
283296
struct str unget_buf;
284297
char unget_buf_[LTC_PEM_DECODE_BUFSZ];
285298
};
299+
#endif
286300

287301
/* others */
288302

src/misc/pem/pem.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2+
/* SPDX-License-Identifier: Unlicense */
3+
#include "tomcrypt_private.h"
4+
5+
/**
6+
@file pem.c
7+
Const declarations for PEM, Steffen Jaeckel
8+
*/
9+
10+
#ifdef LTC_PEM
11+
12+
const struct pem_header_id pem_std_headers[] = {
13+
{
14+
/* PKCS#8 encrypted */
15+
SET_CSTR(.start, "-----BEGIN ENCRYPTED PRIVATE KEY-----"),
16+
SET_CSTR(.end, "-----END ENCRYPTED PRIVATE KEY-----"),
17+
.has_more_headers = no,
18+
.encrypted = 1,
19+
.pkcs8 = 1,
20+
},
21+
{
22+
/* PKCS#8 plain */
23+
SET_CSTR(.start, "-----BEGIN PRIVATE KEY-----"),
24+
SET_CSTR(.end, "-----END PRIVATE KEY-----"),
25+
.has_more_headers = no,
26+
.pkcs8 = 1,
27+
},
28+
/* Regular plain or encrypted private keys */
29+
{
30+
SET_CSTR(.start, "-----BEGIN RSA PRIVATE KEY-----"),
31+
SET_CSTR(.end, "-----END RSA PRIVATE KEY-----"),
32+
.has_more_headers = maybe,
33+
.pka = LTC_PKA_RSA,
34+
},
35+
{
36+
SET_CSTR(.start, "-----BEGIN EC PRIVATE KEY-----"),
37+
SET_CSTR(.end, "-----END EC PRIVATE KEY-----"),
38+
.has_more_headers = maybe,
39+
.pka = LTC_PKA_EC,
40+
},
41+
{
42+
SET_CSTR(.start, "-----BEGIN DSA PRIVATE KEY-----"),
43+
SET_CSTR(.end, "-----END DSA PRIVATE KEY-----"),
44+
.has_more_headers = maybe,
45+
.pka = LTC_PKA_DSA,
46+
},
47+
};
48+
const unsigned long pem_std_headers_num = sizeof(pem_std_headers)/sizeof(pem_std_headers[0]);
49+
50+
51+
/* Encrypted PEM files */
52+
const struct str pem_proc_type_encrypted = { SET_CSTR(, "Proc-Type: 4,ENCRYPTED") };
53+
const struct str pem_dek_info_start = { SET_CSTR(, "DEK-Info: ") };
54+
const struct dek_info_from_str pem_dek_infos[] =
55+
{
56+
{ SET_CSTR(.id, "AES-128-CBC,"), .info.alg = "aes", .info.keylen = 128 / 8, },
57+
{ SET_CSTR(.id, "AES-192-CBC,"), .info.alg = "aes", .info.keylen = 192 / 8, },
58+
{ SET_CSTR(.id, "AES-256-CBC,"), .info.alg = "aes", .info.keylen = 256 / 8, },
59+
{ SET_CSTR(.id, "CAMELLIA-128-CBC,"), .info.alg = "camellia", .info.keylen = 128 / 8, },
60+
{ SET_CSTR(.id, "CAMELLIA-192-CBC,"), .info.alg = "camellia", .info.keylen = 192 / 8, },
61+
{ SET_CSTR(.id, "CAMELLIA-256-CBC,"), .info.alg = "camellia", .info.keylen = 256 / 8, },
62+
{ SET_CSTR(.id, "DES-EDE3-CBC,"), .info.alg = "3des", .info.keylen = 192 / 8, },
63+
{ SET_CSTR(.id, "DES-CBC,"), .info.alg = "des", .info.keylen = 64 / 8, },
64+
};
65+
const unsigned long pem_dek_infos_num = sizeof(pem_dek_infos)/sizeof(pem_dek_infos[0]);
66+
67+
#endif /* LTC_PEM */

0 commit comments

Comments
 (0)