2015-08-18 15 views
6

Per calcolare in modo efficiente un quadrato in Java Card, voglio utilizzare l'algoritmo ALG_RSA_NOPAD con un esponente uguale a 2 e un modulo maggiore del risultato previsto (quindi la riduzione modulare non ha alcun effetto).ALG_RSA_NOPAD in Java Card

Ma non sono in grado di utilizzare l'algoritmo ALG_RSA_NOPAD. Infatti, quando chiamo il metodo doFinal() ottengo un CryptoException che è ILLEGAL_VALUE. In Java Card 2.2.2 spec, si dice che:

CryptoException.ILLEGAL_USE se una delle seguenti condizioni:

• Questo algoritmo di cifratura non pad il messaggio e il messaggio non è isolato allineati.

• Questo algoritmo di crittografia non esegue il rilievo del messaggio e non sono stati forniti dati di input inBuff o tramite il metodo update().

• La lunghezza del messaggio di input non è supportata.

• I dati decrittografati non sono limitati dai byte di riempimento appropriati.

Quindi concludo che il mio messaggio non è allineato a blocchi. Ma cosa significa blocco allineato per questo algoritmo? Il mio messaggio ha la stessa lunghezza del modulo? L'esponente? Ho provato cose diverse, ma non ho trovato ...

Il codice corrispondente:

byte[] res_RSA = new byte[(short) 0x0080]; 
    KeyPair rsa_KeyPair = new KeyPair(KeyPair.ALG_RSA,KeyBuilder.LENGTH_RSA_1024); 
     rsa_KeyPair.genKeyPair(); 
    RSAPublicKey rsa_PubKey; rsa_PubKey = (RSAPublicKey) rsa_KeyPair.getPublic(); 


    rsa_PubKey.setExponent(new byte[]{(byte) 0x02}, (short) 0x00000, (short) 0x0001); 
    rsa_PubKey.setModulus(new byte[] { (byte) 0xFF, (byte) 0xFF, 
     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 
     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 
     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 
     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 
     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 
     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 
     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 
     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 
     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 
     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 
     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 
     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 
     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 
     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 
     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 
     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 
     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 
     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 
     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 
     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 
     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 
     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 
     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 
     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 
     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 
     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 
     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 
     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 
     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 
     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 
     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 
     (byte) 0xFF, (byte) 0xFF, }, (short) 0x0000, (short) 0x0080); 

    cipherRSA = Cipher.getInstance(Cipher.ALG_RSA_NOPAD, false); 

    x = new byte[] { (byte) 0x0C, (byte) 0xE2, (byte) 0x65, (byte) 0x92, 
     (byte) 0x98, (byte) 0x84, (byte) 0x4C, (byte) 0x6C, 
     (byte) 0x39, (byte) 0x31, (byte) 0x78, (byte) 0x22, 
     (byte) 0x99, (byte) 0x39, (byte) 0xAD, (byte) 0xAD, 
     (byte) 0x74, (byte) 0x31, (byte) 0x45, (byte) 0xD2, 
     (byte) 0xB9, (byte) 0x37, (byte) 0xB2, (byte) 0x92, 
     (byte) 0x7D, (byte) 0x32, (byte) 0xE9, (byte) 0x70, 
     (byte) 0x91, (byte) 0x7D, (byte) 0x78, (byte) 0x45, 
     (byte) 0xC9, (byte) 0x5C, (byte) 0xF9, (byte) 0xF2, 
     (byte) 0xFD, (byte) 0xB9, (byte) 0xAE, (byte) 0x6C, 
     (byte) 0xC9, (byte) 0x42, (byte) 0x64, (byte) 0xBA, 
     (byte) 0x2A, (byte) 0xCE, (byte) 0x5A, (byte) 0x71, 
     (byte) 0x60, (byte) 0x58, (byte) 0x56, (byte) 0x17, 
     (byte) 0x2E, (byte) 0x25, (byte) 0xDD, (byte) 0x47, 
     (byte) 0x23, (byte) 0x6B, (byte) 0x15, (byte) 0x76, 
     (byte) 0x8F, (byte) 0x2A, (byte) 0x87, (byte) 0xC7 }; 

    cipherRSA.init(rsa_PubKey, Cipher.MODE_ENCRYPT); 
    cipherRSA.doFinal(x, (short) 0x0000, 
      (short) 0x0040, res_RSA, (short) 0x0000); 

Così il CryptoException viene sollevata l'ultima riga, ma io non capisco perché.

(Si noti che, nel mio codice, ho impostato il modulo per il più grande valore di 128Byte lunghezza per essere sicuri che la piazza non sarà influenzato.)

risposta

3

Bene, sulla mia carta il messaggio deve avere la stessa lunghezza del modulo ... Pensavo di aver provato questo caso ma ho compensato male gli offset.

Quindi anche il mio messaggio non è lungo come il modulo (non ho potuto calcolare un quadrato) Devo riempire l'array di zeri.

2

Spesso RSA è essere verificato solo a lavorare contro certa esponenti pubblici per un'implementazione specifica di Java Card. È molto probabile che questo problema scompaia se si utilizza un esponente pubblico più ampio come 65537 (0x01, 0x00, 0x01). Si prega di consultare il manuale utente del proprio chip/piattaforma.

Si noti che tali calcoli senza riempimento non sono sicuri per RSA. Ciò significa che potrebbero essere difficili da spiegare anche agli organismi di certificazione. Potresti avere più fortuna con le implementazioni di Diffie-Hellman (se ce ne sono).

+0

Proverò ancora con esponente più grande per testare. Per quanto riguarda Diffie-Hellman, l'API di Java Card 2.2.2 menziona solo la versione della curva ellittica, quindi suppongo che tu faccia riferimento a un'API proprietaria? Grazie per le tue spiegazioni – Raoul722

+2

Sì, questo o 3.0.5, ma non penso che troverai JC 3.0.5 in circolazione ... ancora. –

+0

Quindi presterò attenzione alla documentazione, non posso aspettare le cifre AEAD;) – Raoul722