Skip to content

Commit 246d520

Browse files
committed
add v6 SKESK
1 parent 51d177b commit 246d520

20 files changed

+360
-23
lines changed

include/repgp/repgp_def.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,13 @@ typedef enum {
499499
PGP_C_UNKNOWN = 255
500500
} pgp_compression_type_t;
501501

502-
enum { PGP_SKSK_V4 = 4, PGP_SKSK_V5 = 5 };
502+
typedef enum {
503+
PGP_SKSK_V4 = 4,
504+
PGP_SKSK_V5 = 5,
505+
#if defined(ENABLE_CRYPTO_REFRESH)
506+
PGP_SKSK_V6 = 6
507+
#endif
508+
} pgp_skesk_version_t;
503509
typedef enum {
504510
PGP_PKSK_V3 = 3,
505511
#if defined(ENABLE_CRYPTO_REFRESH)

include/rnp/rnp.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3619,6 +3619,17 @@ RNP_API rnp_result_t rnp_op_encrypt_add_recipient(rnp_op_encrypt_t op, rnp_key_h
36193619
* @return RNP_SUCCESS or errorcode if failed.
36203620
*/
36213621
RNP_API rnp_result_t rnp_op_encrypt_enable_pkesk_v6(rnp_op_encrypt_t op);
3622+
3623+
/**
3624+
* @brief Enables the creation of SKESK v6 (instead of v4) which results in the use of SEIPDv2.
3625+
* The actually created version depends on whether an AEAD algorithm has been chosen.
3626+
* NOTE: This is an experimental feature and this function can be replaced (or removed)
3627+
* at any time.
3628+
*
3629+
* @param op opaque encrypting context. Must be allocated and initialized.
3630+
* @return RNP_SUCCESS or errorcode if failed.
3631+
*/
3632+
RNP_API rnp_result_t rnp_op_encrypt_enable_skesk_v6(rnp_op_encrypt_t op);
36223633
#endif
36233634

36243635
#if defined(RNP_EXPERIMENTAL_PQC)

src/lib/rnp.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2624,6 +2624,18 @@ try {
26242624
return RNP_SUCCESS;
26252625
}
26262626
FFI_GUARD
2627+
2628+
rnp_result_t
2629+
rnp_op_encrypt_enable_skesk_v6(rnp_op_encrypt_t op)
2630+
try {
2631+
if (!op) {
2632+
return RNP_ERROR_NULL_POINTER;
2633+
}
2634+
2635+
op->rnpctx.enable_skesk_v6 = true;
2636+
return RNP_SUCCESS;
2637+
}
2638+
FFI_GUARD
26272639
#endif
26282640

26292641
#if defined(RNP_EXPERIMENTAL_PQC)
@@ -2768,7 +2780,7 @@ try {
27682780
return RNP_ERROR_BAD_PARAMETERS;
27692781
}
27702782
#ifdef ENABLE_CRYPTO_REFRESH
2771-
if (op->rnpctx.aalg == PGP_AEAD_NONE && op->rnpctx.enable_pkesk_v6) {
2783+
if (op->rnpctx.aalg == PGP_AEAD_NONE && (op->rnpctx.enable_pkesk_v6)) {
27722784
FFI_LOG(op->ffi,
27732785
"Setting AEAD algorithm to PGP_AEAD_NONE (%s) would contradict the previously "
27742786
"enabled PKESKv6 setting",

src/librepgp/stream-ctx.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ typedef struct rnp_symmetric_pass_info_t {
7272
* - recipients : list of key ids used to encrypt data to
7373
* - enable_pkesk_v6 (Only if defined: ENABLE_CRYPTO_REFRESH): if true and each recipient in
7474
* the list of recipients has the capability, allows PKESKv6/SEIPDv2
75+
* - enable_skesk_v6 : if true and AEAD cipher is chosen, creates SKESKv6/SEIPDv2
7576
* - pref_pqc_enc_subkey (Only if defined: ENABLE_PQC): if true, prefers PQC subkey over
7677
* non-PQC subkey for encryption.
7778
* - passwords : list of passwords used for password-based encryption
@@ -110,6 +111,7 @@ typedef struct rnp_ctx_t {
110111
bool no_wrap{}; /* do not wrap source in literal data packet */
111112
#if defined(ENABLE_CRYPTO_REFRESH)
112113
bool enable_pkesk_v6{}; /* allows pkesk v6 if list of recipients is suitable */
114+
bool enable_skesk_v6{}; /* allows skesk v6 if chosen cipher is suitable */
113115
#endif
114116
#if defined(ENABLE_PQC)
115117
bool pref_pqc_enc_subkey{}; /* prefer to encrypt to PQC subkey */

src/librepgp/stream-packet.cpp

Lines changed: 122 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -937,11 +937,43 @@ pgp_sk_sesskey_t::write(pgp_dest_t &dst) const
937937
pgp_packet_body_t pktbody(PGP_PKT_SK_SESSION_KEY);
938938
/* version and algorithm fields */
939939
pktbody.add_byte(version);
940+
#if defined(ENABLE_CRYPTO_REFRESH)
941+
uint8_t s2k_len;
942+
/* A one-octet scalar octet count for the 5 fields following this octet. */
943+
/* TODO: unify with pgp_key_pkt_t::s2k_specifier_len() */
944+
if (version == PGP_SKSK_V6) {
945+
switch (s2k.specifier) {
946+
case PGP_S2KS_SIMPLE:
947+
s2k_len = 2;
948+
break;
949+
case PGP_S2KS_SALTED:
950+
s2k_len = 10;
951+
break;
952+
case PGP_S2KS_ITERATED_AND_SALTED:
953+
s2k_len = 11;
954+
break;
955+
default:
956+
RNP_LOG("invalid specifier");
957+
throw rnp::rnp_exception(RNP_ERROR_BAD_PARAMETERS);
958+
}
959+
pktbody.add_byte(3 + s2k_len + ivlen);
960+
}
961+
#endif
940962
pktbody.add_byte(alg);
941-
if (version == PGP_SKSK_V5) {
963+
if (version == PGP_SKSK_V5
964+
#if defined(ENABLE_CRYPTO_REFRESH)
965+
|| version == PGP_SKSK_V6
966+
#endif
967+
) {
942968
pktbody.add_byte(aalg);
943969
}
944-
/* S2K specifier */
970+
/* S2K specifier */
971+
#if defined(ENABLE_CRYPTO_REFRESH)
972+
/* A one-octet scalar octet count of the following field. */
973+
if (version == PGP_SKSK_V6) {
974+
pktbody.add_byte(s2k_len);
975+
}
976+
#endif
945977
pktbody.add_byte(s2k.specifier);
946978
pktbody.add_byte(s2k.hash_alg);
947979

@@ -960,7 +992,11 @@ pgp_sk_sesskey_t::write(pgp_dest_t &dst) const
960992
throw rnp::rnp_exception(RNP_ERROR_BAD_PARAMETERS);
961993
}
962994
/* v5 : iv */
963-
if (version == PGP_SKSK_V5) {
995+
if (version == PGP_SKSK_V5
996+
#if defined(ENABLE_CRYPTO_REFRESH)
997+
|| version == PGP_SKSK_V6
998+
#endif
999+
) {
9641000
pktbody.add(iv, ivlen);
9651001
}
9661002
/* encrypted key and auth tag for v5 */
@@ -971,6 +1007,82 @@ pgp_sk_sesskey_t::write(pgp_dest_t &dst) const
9711007
pktbody.write(dst);
9721008
}
9731009

1010+
#if defined(ENABLE_CRYPTO_REFRESH)
1011+
rnp_result_t
1012+
pgp_sk_sesskey_t::parse_v6(pgp_packet_body_t &pkt)
1013+
{
1014+
uint8_t bt;
1015+
uint8_t octet_count;
1016+
uint8_t s2k_len;
1017+
1018+
/* A one-octet scalar octet count for the 5 fields following this octet. */
1019+
/* TODO: do we need to check octet_count? */
1020+
if (!pkt.get(octet_count)) {
1021+
RNP_LOG("failed to get octet count of next 5 fields");
1022+
return RNP_ERROR_BAD_FORMAT;
1023+
}
1024+
1025+
/* symmetric algorithm */
1026+
if (!pkt.get(bt)) {
1027+
RNP_LOG("failed to get symm alg");
1028+
return RNP_ERROR_BAD_FORMAT;
1029+
}
1030+
alg = (pgp_symm_alg_t) bt;
1031+
1032+
/* aead algorithm */
1033+
if (!pkt.get(bt)) {
1034+
RNP_LOG("failed to get aead alg");
1035+
return RNP_ERROR_BAD_FORMAT;
1036+
}
1037+
aalg = (pgp_aead_alg_t) bt;
1038+
if ((aalg != PGP_AEAD_EAX) && (aalg != PGP_AEAD_OCB)) {
1039+
RNP_LOG("unsupported AEAD algorithm : %d", (int) aalg);
1040+
return RNP_ERROR_BAD_PARAMETERS;
1041+
}
1042+
1043+
/* A one-octet scalar octet count of the following field. */
1044+
/* TODO: do we need to check s2k_len? */
1045+
if (!pkt.get(s2k_len)) {
1046+
RNP_LOG("failed to get octet count of next 5 fields");
1047+
return RNP_ERROR_BAD_FORMAT;
1048+
}
1049+
1050+
/* s2k */
1051+
if (!pkt.get(s2k)) {
1052+
RNP_LOG("failed to parse s2k");
1053+
return RNP_ERROR_BAD_FORMAT;
1054+
}
1055+
1056+
size_t noncelen = pgp_cipher_aead_nonce_len(aalg);
1057+
size_t taglen = pgp_cipher_aead_tag_len(aalg);
1058+
size_t keylen = 0;
1059+
1060+
if (pkt.left() > noncelen + taglen + PGP_MAX_KEY_SIZE) {
1061+
RNP_LOG("too long esk");
1062+
return RNP_ERROR_BAD_FORMAT;
1063+
}
1064+
if (pkt.left() < noncelen + taglen + 8) {
1065+
RNP_LOG("too short esk");
1066+
return RNP_ERROR_BAD_FORMAT;
1067+
}
1068+
/* iv */
1069+
if (!pkt.get(iv, noncelen)) {
1070+
RNP_LOG("failed to get iv");
1071+
return RNP_ERROR_BAD_FORMAT;
1072+
}
1073+
ivlen = noncelen;
1074+
1075+
/* key */
1076+
keylen = pkt.left();
1077+
if (!pkt.get(enckey, keylen)) {
1078+
RNP_LOG("failed to get key");
1079+
return RNP_ERROR_BAD_FORMAT;
1080+
}
1081+
enckeylen = keylen;
1082+
return RNP_SUCCESS;
1083+
}
1084+
#endif
1085+
9741086
rnp_result_t
9751087
pgp_sk_sesskey_t::parse(pgp_source_t &src)
9761088
{
@@ -983,6 +1095,12 @@ pgp_sk_sesskey_t::parse(pgp_source_t &src)
9831095
/* version */
9841096
uint8_t bt;
9851097
if (!pkt.get(bt) || ((bt != PGP_SKSK_V4) && (bt != PGP_SKSK_V5))) {
1098+
#if defined(ENABLE_CRYPTO_REFRESH)
1099+
if (bt == PGP_SKSK_V6) {
1100+
version = bt;
1101+
return parse_v6(pkt);
1102+
}
1103+
#endif
9861104
RNP_LOG("wrong packet version");
9871105
return RNP_ERROR_BAD_FORMAT;
9881106
}
@@ -1150,7 +1268,7 @@ pgp_pk_sesskey_t::parse(pgp_source_t &src)
11501268
return RNP_ERROR_BAD_FORMAT;
11511269
}
11521270
fp.length = fp_len;
1153-
if (fp.length && (fp.length != (unsigned)(fp_and_key_ver_len - 1))) {
1271+
if (fp.length && (fp.length != (unsigned) (fp_and_key_ver_len - 1))) {
11541272
RNP_LOG("size mismatch (fingerprint size and fp+key version length field)");
11551273
return RNP_ERROR_BAD_FORMAT;
11561274
}

src/librepgp/stream-packet.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@ typedef struct pgp_sk_sesskey_t {
211211

212212
void write(pgp_dest_t &dst) const;
213213
rnp_result_t parse(pgp_source_t &src);
214+
215+
#if defined(ENABLE_CRYPTO_REFRESH)
216+
private:
217+
rnp_result_t parse_v6(pgp_packet_body_t &pkt);
218+
#endif
214219
} pgp_sk_sesskey_t;
215220

216221
/** pgp_one_pass_sig_t */

src/librepgp/stream-parse.cpp

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,7 +1781,11 @@ encrypted_try_password(pgp_source_encrypted_param_t *param, const char *password
17811781
continue;
17821782
}
17831783
keyavail = true;
1784-
} else if (skey.version == PGP_SKSK_V5) {
1784+
} else if (skey.version == PGP_SKSK_V5
1785+
#if defined(ENABLE_CRYPTO_REFRESH)
1786+
|| skey.version == PGP_SKSK_V6
1787+
#endif
1788+
) {
17851789
#if !defined(ENABLE_AEAD)
17861790
continue;
17871791
#else
@@ -1795,6 +1799,31 @@ encrypted_try_password(pgp_source_encrypted_param_t *param, const char *password
17951799
alg = skey.alg;
17961800

17971801
/* initialize cipher */
1802+
#if defined(ENABLE_CRYPTO_REFRESH)
1803+
if (skey.version == PGP_SKSK_V6) {
1804+
/* For v6 SKESK, we use the S2K derived key as input to the KDF */
1805+
auto kdf = rnp::Hkdf::create(PGP_HASH_SHA256);
1806+
1807+
std::vector<uint8_t> kdf_info;
1808+
kdf_info.push_back(PGP_PKT_SK_SESSION_KEY | PGP_PTAG_ALWAYS_SET |
1809+
PGP_PTAG_NEW_FORMAT);
1810+
kdf_info.push_back(skey.version);
1811+
kdf_info.push_back(skey.alg);
1812+
kdf_info.push_back(skey.aalg);
1813+
1814+
std::vector<uint8_t> kdf_input(keybuf.data(),
1815+
keybuf.data() + pgp_key_size(skey.alg));
1816+
1817+
kdf->extract_expand(NULL,
1818+
0, // no salt
1819+
kdf_input.data(),
1820+
kdf_input.size(),
1821+
kdf_info.data(),
1822+
kdf_info.size(),
1823+
keybuf.data(),
1824+
keybuf.size());
1825+
}
1826+
#endif
17981827
if (!pgp_cipher_aead_init(&crypt, skey.alg, skey.aalg, keybuf.data(), true)) {
17991828
continue;
18001829
}
@@ -2242,14 +2271,12 @@ encrypted_read_packet_data(pgp_source_encrypted_param_t *param)
22422271
}
22432272
#ifdef ENABLE_CRYPTO_REFRESH
22442273
else if (SEIPD_version == PGP_SE_IP_DATA_V2) {
2245-
/* SKESK v6 is not yet implemented, thus we must not attempt to decrypt
2246-
SEIPDv2 here
2247-
TODO: Once SKESK v6 is implemented, replace this check with a check for
2248-
consistency between SEIPD and SKESK version
2249-
*/
2250-
if (param->symencs.size() > 0) {
2251-
RNP_LOG("SEIPDv2 not usable with SKESK version");
2252-
return RNP_ERROR_BAD_FORMAT;
2274+
for (auto symenc : param->symencs) {
2275+
// consistency check if SEIPDv2 is only coupled with SKESKv6
2276+
if (symenc.version != PGP_SKSK_V6) {
2277+
RNP_LOG("SEIPDv2 not usable with SKESK version");
2278+
return RNP_ERROR_BAD_FORMAT;
2279+
}
22532280
}
22542281

22552282
param->auth_type = rnp::AuthType::AEADv2;

src/librepgp/stream-write.cpp

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#include <time.h>
5858
#include <algorithm>
5959
#ifdef ENABLE_CRYPTO_REFRESH
60+
#include "crypto/hkdf.hpp"
6061
#include "v2_seipd.h"
6162
#endif
6263

@@ -721,7 +722,11 @@ encrypted_add_password(rnp_symmetric_pass_info_t * pass,
721722

722723
skey.s2k = pass->s2k;
723724

724-
if (param->auth_type != rnp::AuthType::AEADv1) {
725+
if (param->auth_type != rnp::AuthType::AEADv1
726+
#if defined(ENABLE_CRYPTO_REFRESH)
727+
&& param->auth_type != rnp::AuthType::AEADv2
728+
#endif
729+
) {
725730
skey.version = PGP_SKSK_V4;
726731
if (singlepass) {
727732
/* if there are no public keys then we do not encrypt session key in the packet */
@@ -758,6 +763,33 @@ encrypted_add_password(rnp_symmetric_pass_info_t * pass,
758763
skey.ivlen = pgp_cipher_aead_nonce_len(skey.aalg);
759764
skey.enckeylen = keylen + pgp_cipher_aead_tag_len(skey.aalg);
760765

766+
#if defined(ENABLE_CRYPTO_REFRESH)
767+
if (param->auth_type == rnp::AuthType::AEADv2) {
768+
skey.version = PGP_SKSK_V6;
769+
770+
auto kdf = rnp::Hkdf::create(PGP_HASH_SHA256);
771+
772+
std::vector<uint8_t> kdf_info;
773+
kdf_info.push_back(PGP_PKT_SK_SESSION_KEY | PGP_PTAG_ALWAYS_SET |
774+
PGP_PTAG_NEW_FORMAT);
775+
kdf_info.push_back(skey.version);
776+
kdf_info.push_back(skey.alg);
777+
kdf_info.push_back(skey.aalg);
778+
779+
std::vector<uint8_t> kdf_input(pass->key.data(),
780+
pass->key.data() + pgp_key_size(skey.alg));
781+
782+
kdf->extract_expand(NULL,
783+
0, // no salt
784+
kdf_input.data(),
785+
kdf_input.size(),
786+
kdf_info.data(),
787+
kdf_info.size(),
788+
pass->key.data(),
789+
pgp_key_size(skey.alg));
790+
}
791+
#endif
792+
761793
try {
762794
param->ctx->ctx->rng.get(skey.iv, skey.ivlen);
763795
} catch (const std::exception &e) {
@@ -1012,6 +1044,12 @@ init_encrypted_dst(pgp_write_handler_t *handler, pgp_dest_t *dst, pgp_dest_t *wr
10121044
if (handler->ctx->enable_pkesk_v6 && handler->ctx->pkeskv6_capable() && pkeycount > 0) {
10131045
param->auth_type = rnp::AuthType::AEADv2;
10141046
}
1047+
1048+
/* Use SEIPDv2 for SKESK if enabled and preconditions are met */
1049+
if (handler->ctx->enable_skesk_v6 && handler->ctx->aalg != PGP_AEAD_NONE &&
1050+
skeycount > 0) {
1051+
param->auth_type = rnp::AuthType::AEADv2;
1052+
}
10151053
#endif
10161054
param->aalg = handler->ctx->aalg;
10171055
param->ctx = handler->ctx;

0 commit comments

Comments
 (0)