Skip to content

PKCS#8 export with PBES2 encryption - #828

Open
karel-m wants to merge 8 commits into
developfrom
pr/pbes2-writer
Open

karel-m wants to merge 8 commits into
developfrom
pr/pbes2-writer

Conversation

@karel-m

@karel-m karel-m commented Nov 17, 2026

Copy link
Copy Markdown
Member

Adds the write side for PKCS#8 keys, which so far could only be read.

  • pbes2_encrypt() - encrypts a PrivateKeyInfo into an EncryptedPrivateKeyInfo (PBES2 with PBKDF2 and AES or 3DES in CBC mode)
  • pka_key_export() - PKCS#8 or SubjectPublicKeyInfo export of any ltc_pka_key
  • pem_encode_pkcs() + pem_encode_pkcs_filehandle() - PEM output according to RFC 7468 (PRIVATE KEY, ENCRYPTED PRIVATE KEY, PUBLIC KEY)

Three commits fix bugs that are already in develop and are not directly related to PBES2. They were found while working on this:

  • fix base64_encode_pem line breaks
  • make ecc_export_openssl compliant with RFC 5915
  • fix ecc_export_openssl - pad curve coefficients a and b to the field size

@karel-m
karel-m requested a review from sjaeckel November 17, 2026 23:41

@sjaeckel sjaeckel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically LGTM, just some ideas which don't necessarily need to be followed up if you disagree, besides the change to use struct password_ctx which should be implemented IMO.

Comment thread doc/crypt.tex
Comment on lines +8168 to +8172
int pbes2_encrypt( unsigned char *out, unsigned long *outlen,
const unsigned char *in, unsigned long inlen,
const unsigned char *pw, unsigned long pwlen,
const pbes2_enc_params *params,
prng_state *prng, int wprng);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this as a public API? I don't think so!?

We can still document the type here, that is good!

Comment thread doc/crypt.tex

int pem_encode_pkcs_filehandle(FILE *f, int which, const ltc_pka_key *key,
const pem_encode_opts *opts,
prng_state *prng, int wprng);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we move the prng into the opts?

Like that all APIs below also become cleaner. (And we could remove wprng as proposed in #515 w/o modifications of the API... we should really give #515 a go IMO ...)


ivlen = cipher_descriptor[cid].block_length;
if (keylen > sizeof(key) || ivlen > sizeof(iv) || inlen > ULONG_MAX - ivlen) return CRYPT_INVALID_ARG;
ctlen = inlen + ivlen - (inlen % ivlen); /* PKCS#7 padding adds 1 to ivlen bytes */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Shouldn't padding_pad() with *padded_length < length do this calculation for you?
  2. What is that comment about?

Comment thread src/misc/pem/pem_encode.c
if ((err = pka_key_export(*der, derlen, which, key)) != CRYPT_OK) {
zeromem(*der, len);
XFREE(*der);
*der = NULL;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add

Suggested change
*der = NULL;
*der = NULL;
*derlen = 0;

Comment on lines +223 to +224
/** PBES2 parameters, NULL for the defaults */
const pbes2_enc_params *enc;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about creating an intermediate union type and using that one here?

typedef union pem_enc_params {
   const pbes2_enc_params *pbes2;
} pem_enc_params;

and add an entry to pem_encode_flags LTC_PEM_PBES2 = 0, since someone will come and wants to add other formats ... Maybe then also change LTC_PEM_CRLF = 8...

Comment on lines +219 to +222
/** Password, NULL writes the key unencrypted */
const unsigned char *pw;
/** Length of the password, an empty password is refused */
unsigned long pwlen;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we want this interactive instead via struct password_ctx?

Comment thread src/misc/pem/pem_encode.c
}
}

b64len = 4 * ((bodylen + 2) / 3);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add the following to tomcrypt_private.h and also use it in s_base64_encode_internal()?

#define ltc_base64_encoded_len(len) (4 * (((len) + 2) / 3))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants