[PATCH GnuPG 2/2] scd:piv: Support 4096 bit RSA keys

David Seifert soap at gentoo.org
Tue Nov 10 13:40:18 CET 2026


* scd/app-piv.c (PIV_ALGORITHM_RSA_4096): New.
(get_key_algorithm_by_dobj): Map RSA keys larger than 3072 bit to
PIV_ALGORITHM_RSA_4096.
(do_sign, do_decipher): Support PIV_ALGORITHM_RSA_4096.
(get_keygrip_by_tag, do_readkey): Handle 3072 and 4096 bit RSA keys
stored without a certificate.
(writekey_rsa): Use PIV_ALGORITHM_RSA_4096 for keys larger than 3072
bit.
(do_genkey): Support "rsa4096".

--

YubiKeys with firmware 5.7 and later support 4096 bit RSA keys in
PIV using the algorithm identifier 0x16. scdaemon took any RSA key
larger than 3000 bit for a 3072 bit key, so signing and decryption
with such a key failed with "input of 512 octets too large for
mechanism 5", and generating or importing such a key was not
possible.

After generating or importing a key, scdaemon stores the public key
in the certificate object until a certificate is written. Reading
such a key only worked for 2048 bit RSA keys; for 3072 bit keys it
failed with "Invalid public key algorithm". This is fixed as well.

Tested on a YubiKey 5C NFC (firmware 5.7.1) and a TOKEN2 PIN+ R3.3
card (which also implements 0x16) with on-card generated 4096 bit
keys in 9C and 9D:

  - gpgsm signing and decryption, checked with OpenSSL.
  - gpg with both keys added as OpenPGP subkeys ([S] and [E]) to a
    software primary key: the backsig made by 9C, signing and
    verifying, and encrypting and decrypting.
  - RSA-PSS (SHA-256, 32 byte salt): PKSIGN --hash=none with a
    512 byte PSS encoded block, checked with OpenSSL.

Without this change all of these fail.

Key generation and import through scdaemon on the YubiKey 5C NFC:

  - Generating 2048, 3072 and 4096 bit RSA keys and a nistp256 key,
    reading the RSA keys back and signing with them, checked with
    OpenSSL. Decryption with a 4096 bit key in 9D.
  - Importing a 4096 bit RSA key into 9D; the keygrip matches the
    software key, signing and decryption work.

Key generation through scdaemon on the TOKEN2 card:

  - Generating 4096 bit RSA keys in 9A, 9C and 9D, reading them back
    and signing with them (PKCS#1 v1.5 and PSS), checked with OpenSSL.
    Decryption with the key in 9D.

Signed-off-by: David Seifert <soap at gentoo.org>
---
 scd/app-piv.c | 52 ++++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 41 insertions(+), 11 deletions(-)

diff --git a/scd/app-piv.c b/scd/app-piv.c
index a316fd41a..74fa14844 100644
--- a/scd/app-piv.c
+++ b/scd/app-piv.c
@@ -81,6 +81,7 @@
 #define PIV_ALGORITHM_AES256_CBC 0x0D
 #define PIV_ALGORITHM_ECC_P256   0x11
 #define PIV_ALGORITHM_ECC_P384   0x14
+#define PIV_ALGORITHM_RSA_4096   0x16  /* Yubico extension (fw 5.7).  */
 
 
 /* The AID for PIV.  */
@@ -1332,7 +1333,9 @@ get_keygrip_by_tag (app_t app, unsigned int tag,
     goto leave;
   if (mechanism) /* Compute keygrip from public key.  */
     {
-      if (mechanism == PIV_ALGORITHM_RSA_2048)
+      if (mechanism == PIV_ALGORITHM_RSA_2048
+          || mechanism == PIV_ALGORITHM_RSA_3072
+          || mechanism == PIV_ALGORITHM_RSA_4096)
         err = genkey_parse_rsa (certbuf, certbuflen, &s_pkey);
       else if (mechanism == PIV_ALGORITHM_ECC_P256
                || mechanism == PIV_ALGORITHM_ECC_P384)
@@ -1547,7 +1550,9 @@ do_readkey (app_t app, ctrl_t ctrl, const char *keyrefstr, unsigned int flags,
   else
     {
       /* Convert the public key into the expected s-expression.  */
-      if (mechanism == PIV_ALGORITHM_RSA_2048)
+      if (mechanism == PIV_ALGORITHM_RSA_2048
+          || mechanism == PIV_ALGORITHM_RSA_3072
+          || mechanism == PIV_ALGORITHM_RSA_4096)
         err = genkey_parse_rsa (cert, certlen, &s_pkey);
       else if (mechanism == PIV_ALGORITHM_ECC_P256
                || mechanism == PIV_ALGORITHM_ECC_P384)
@@ -1636,6 +1641,7 @@ get_key_algorithm_by_dobj (app_t app, data_object_t dobj, int *r_mechanism)
         {
         case PIV_ALGORITHM_RSA_2048:
         case PIV_ALGORITHM_RSA_3072:
+        case PIV_ALGORITHM_RSA_4096:
         case PIV_ALGORITHM_ECC_P256:
         case PIV_ALGORITHM_ECC_P384:
           *r_mechanism = mechanism;
@@ -1698,7 +1704,9 @@ get_key_algorithm_by_dobj (app_t app, data_object_t dobj, int *r_mechanism)
   switch (algo)
     {
     case GCRY_PK_RSA:
-      if (gcry_pk_get_nbits (s_pkey) > 3000)
+      if (gcry_pk_get_nbits (s_pkey) > 3072)
+        algo = PIV_ALGORITHM_RSA_4096;
+      else if (gcry_pk_get_nbits (s_pkey) > 3000)
         algo = PIV_ALGORITHM_RSA_3072;
       else
         algo = PIV_ALGORITHM_RSA_2048;
@@ -2330,18 +2338,30 @@ do_sign (app_t app, ctrl_t ctrl, const char *keyidstr, int hashalgo,
        * this is PSS formatted data and we use it verbatim; PIV cards
        * accept PSS as well as PKCS#1.  */
     }
+  else if (mechanism == PIV_ALGORITHM_RSA_4096
+           && indatalen == 4096/8 && indata[indatalen-1] == 0xBC)
+    {
+      /* If the provided data length matches the supported RSA
+       * framelen and the last octet of the data is 0xBC, we assume
+       * this is PSS formatted data and we use it verbatim; PIV cards
+       * accept PSS as well as PKCS#1.  */
+    }
   else if (mechanism == PIV_ALGORITHM_RSA_2048
-           || mechanism == PIV_ALGORITHM_RSA_3072)
+           || mechanism == PIV_ALGORITHM_RSA_3072
+           || mechanism == PIV_ALGORITHM_RSA_4096)
     {
-      /* PIV requires 2048 bit or 3072 bit RSA.  */
+      /* PIV requires 2048 bit or 3072 bit RSA; Yubikeys with
+       * firmware 5.7 also support 4096 bit RSA.  */
       unsigned int framelen;
       unsigned char *frame;
       int i;
 
       if (mechanism == PIV_ALGORITHM_RSA_2048)
         framelen = 2048 / 8;
-      else
+      else if (mechanism == PIV_ALGORITHM_RSA_3072)
         framelen = 3072 / 8;
+      else
+        framelen = 4096 / 8;
 
       oidbuflen = sizeof oidbuf;
       if (!hashalgo)
@@ -2460,7 +2480,8 @@ do_sign (app_t app, ctrl_t ctrl, const char *keyidstr, int hashalgo,
           memmove (outdata, outdata + (s - outdata), n);
           outdatalen = n;
         }
-      else if (mechanism == PIV_ALGORITHM_RSA_3072)
+      else if (mechanism == PIV_ALGORITHM_RSA_3072
+               || mechanism == PIV_ALGORITHM_RSA_4096)
         {
           memmove (outdata, outdata + (s - outdata), n);
           outdatalen = n;
@@ -2608,6 +2629,9 @@ do_decipher (app_t app, ctrl_t ctrl, const char *keyidstr,
     case PIV_ALGORITHM_RSA_3072:
       framelen = 3072 / 8;
       break;
+    case PIV_ALGORITHM_RSA_4096:
+      framelen = 4096 / 8;
+      break;
     default:
       err = gpg_error (GPG_ERR_INTERNAL);
       log_debug ("piv: unknown PIV mechanism %d while decrypting\n", mechanism);
@@ -2625,7 +2649,8 @@ do_decipher (app_t app, ctrl_t ctrl, const char *keyidstr,
   if (indatalen > framelen)
     {
       if ((mechanism == PIV_ALGORITHM_RSA_2048
-          || mechanism == PIV_ALGORITHM_RSA_3072)
+          || mechanism == PIV_ALGORITHM_RSA_3072
+          || mechanism == PIV_ALGORITHM_RSA_4096)
           && indatalen == framelen + 1 && !*indata)
         {
           indata_buffer = xtrycalloc (1, framelen);
@@ -2669,7 +2694,8 @@ do_decipher (app_t app, ctrl_t ctrl, const char *keyidstr,
                          (int)0x7c, (size_t)0, NULL, /* Constructed. */
                          (int)0x82, (size_t)0, "",
                          (mechanism == PIV_ALGORITHM_RSA_2048
-                          || mechanism == PIV_ALGORITHM_RSA_3072)?
+                          || mechanism == PIV_ALGORITHM_RSA_3072
+                          || mechanism == PIV_ALGORITHM_RSA_4096)?
                          (int)0x81 : (int)0x85, (size_t)indatalen, indata,
                          (int)0, (size_t)0, NULL);
   if (err)
@@ -2878,7 +2904,9 @@ writekey_rsa (app_t app, data_object_t dobj, int keyref,
   if (err)
     goto leave;
 
-  if (rsa_n_len > 3000/8)
+  if (rsa_n_len > 3072/8)
+    mechanism = PIV_ALGORITHM_RSA_4096;
+  else if (rsa_n_len > 3000/8)
     mechanism = PIV_ALGORITHM_RSA_3072;
   else
     mechanism = PIV_ALGORITHM_RSA_2048;
@@ -3290,7 +3318,7 @@ genkey_parse_ecc (const unsigned char *data, size_t datalen, int mechanism,
 
 /* Create a new keypair for KEYREF.  If KEYTYPE is NULL a default
  * keytype is selected, else it may be one of the strings:
- *  "rsa2048", "rsa3072", "nistp256, or "nistp384".
+ *  "rsa2048", "rsa3072", "rsa4096", "nistp256, or "nistp384".
  *
  * Supported FLAGS are:
  *   APP_GENKEY_FLAG_FORCE   Overwrite existing key.
@@ -3330,6 +3358,8 @@ do_genkey (app_t app, ctrl_t ctrl, const char *keyrefstr, const char *keytype,
     mechanism = PIV_ALGORITHM_RSA_2048;
   else if (!strcmp (keytype, "rsa3072"))
     mechanism = PIV_ALGORITHM_RSA_3072;
+  else if (!strcmp (keytype, "rsa4096"))
+    mechanism = PIV_ALGORITHM_RSA_4096;
   else if (!strcmp (keytype, "nistp256"))
     mechanism = PIV_ALGORITHM_ECC_P256;
   else if (!strcmp (keytype, "nistp384"))
-- 
2.56.0



More information about the Gnupg-devel mailing list