@@ -385,28 +385,9 @@ private void initialize23(@androidx.annotation.NonNull final KeyPairGenerator ke
385385 final boolean trySetAttestationChallenge ,
386386 final int purposes ,
387387 final boolean unnecessaryCryptoPurposesDisabled ) throws InvalidAlgorithmParameterException {
388- KeyGenParameterSpec .Builder builder ;
389-
390- builder = new KeyGenParameterSpec .Builder (
391- mKeyManager .getKeyAlias (), purposes
392- )
393- .setKeySize (keySize )
394- .setSignaturePaddings (
395- KeyProperties .SIGNATURE_PADDING_RSA_PKCS1
396- )
397- .setDigests (
398- KeyProperties .DIGEST_NONE ,
399- KeyProperties .DIGEST_SHA1 ,
400- KeyProperties .DIGEST_SHA256
401- );
402-
403- if (!unnecessaryCryptoPurposesDisabled ) {
404- builder .setEncryptionPaddings (
405- KeyProperties .ENCRYPTION_PADDING_RSA_OAEP ,
406- KeyProperties .ENCRYPTION_PADDING_RSA_PKCS1
407- );
408- }
388+ KeyGenParameterSpec .Builder builder = buildCommonKeyGenSpec (keySize , purposes , unnecessaryCryptoPurposesDisabled );
409389
390+ // API 23-specific conditions: attestation requires API 24+, StrongBox requires API 28+
410391 if (trySetAttestationChallenge && Build .VERSION .SDK_INT >= Build .VERSION_CODES .N ) {
411392 builder = setAttestationChallenge (builder );
412393 }
@@ -464,24 +445,7 @@ private void initialize28(@androidx.annotation.NonNull final KeyPairGenerator ke
464445 final boolean trySetAttestationChallenge ,
465446 final int purposes ,
466447 final boolean unnecessaryCryptoPurposesDisabled ) throws InvalidAlgorithmParameterException {
467- KeyGenParameterSpec .Builder builder = new KeyGenParameterSpec .Builder (
468- mKeyManager .getKeyAlias (), purposes )
469- .setKeySize (keySize )
470- .setSignaturePaddings (
471- KeyProperties .SIGNATURE_PADDING_RSA_PKCS1
472- )
473- .setDigests (
474- KeyProperties .DIGEST_NONE ,
475- KeyProperties .DIGEST_SHA1 ,
476- KeyProperties .DIGEST_SHA256
477- );
478-
479- if (!unnecessaryCryptoPurposesDisabled ) {
480- builder .setEncryptionPaddings (
481- KeyProperties .ENCRYPTION_PADDING_RSA_OAEP ,
482- KeyProperties .ENCRYPTION_PADDING_RSA_PKCS1
483- );
484- }
448+ KeyGenParameterSpec .Builder builder = buildCommonKeyGenSpec (keySize , purposes , unnecessaryCryptoPurposesDisabled );
485449
486450 if (trySetAttestationChallenge ) {
487451 builder = setAttestationChallenge (builder );
@@ -499,6 +463,55 @@ private void initialize28(@androidx.annotation.NonNull final KeyPairGenerator ke
499463 keyPairGenerator .initialize (spec );
500464 }
501465
466+ /**
467+ * Sets encryption paddings on the KeyGenParameterSpec.Builder if encryption purposes are enabled.
468+ *
469+ * @param builder The KeyGenParameterSpec.Builder to configure
470+ * @param unnecessaryCryptoPurposesDisabled When true, encryption paddings are not set to address
471+ * SDL security requirements by limiting key usage to signing operations only
472+ */
473+ private void setEncryptionPaddingsIfNeeded (@ NonNull final KeyGenParameterSpec .Builder builder ,
474+ final boolean unnecessaryCryptoPurposesDisabled ) {
475+ final String methodTag = TAG + ":setEncryptionPaddingsIfNeeded" ;
476+ if (!unnecessaryCryptoPurposesDisabled ) {
477+ Logger .verbose (methodTag , "Adding encryption paddings (RSA_OAEP, RSA_PKCS1) to key specification" );
478+ builder .setEncryptionPaddings (
479+ KeyProperties .ENCRYPTION_PADDING_RSA_OAEP ,
480+ KeyProperties .ENCRYPTION_PADDING_RSA_PKCS1
481+ );
482+ } else {
483+ Logger .verbose (methodTag , "Skipping encryption paddings due to SDL security restrictions" );
484+ }
485+ }
486+
487+ /**
488+ * Builds the common KeyGenParameterSpec configuration shared between API 23+ and 28+ initialization methods.
489+ * Setting core configuration : Key alias, purposes, size, signature paddings, digests, and encryption paddings.
490+ *
491+ * @param keySize The RSA key size
492+ * @param purposes The key purposes (signing, encryption, etc.)
493+ * @param unnecessaryCryptoPurposesDisabled Whether to skip encryption paddings for SDL compliance
494+ * @return Configured KeyGenParameterSpec.Builder ready for API-specific configuration
495+ */
496+ private KeyGenParameterSpec .Builder buildCommonKeyGenSpec (final int keySize ,
497+ final int purposes ,
498+ final boolean unnecessaryCryptoPurposesDisabled ) {
499+ KeyGenParameterSpec .Builder builder = new KeyGenParameterSpec .Builder (
500+ mKeyManager .getKeyAlias (), purposes )
501+ .setKeySize (keySize )
502+ .setSignaturePaddings (
503+ KeyProperties .SIGNATURE_PADDING_RSA_PKCS1
504+ )
505+ .setDigests (
506+ KeyProperties .DIGEST_NONE ,
507+ KeyProperties .DIGEST_SHA1 ,
508+ KeyProperties .DIGEST_SHA256
509+ );
510+
511+ setEncryptionPaddingsIfNeeded (builder , unnecessaryCryptoPurposesDisabled );
512+
513+ return builder ;
514+ }
502515
503516 @ SuppressLint (NewApi )
504517 @ SuppressWarnings ("deprecation" )
0 commit comments