Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android registering security provider

I am trying to understand how java security providers work in android. I would like to force all calls to Cipher.getInstance() to return a cipher with spongy castle as the provider. I am having no luck.

The following code returns a cipher with provider being "AndroidKeyStoreBCWorkaround version 1.0", but I want the provider to be SpongyCastle.

The reason I want to do this, is that I have a library that calls into javax.crypto.Cipher.getInstance() multiple times. I want all those calls to go to spongy castle, without having to re-write the library to explicitly specify "SC" as the provider.

public class MainActivity extends Activity
{
    static
    {
        Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(), 1);
        Security.removeProvider("BC");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        try
        {
          //this returns provider = "AndroidKeyStoreBCWorkaround version 1.0"
          javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance("AES/CTR/NoPadding");
          //this works
          // cipher = javax.crypto.Cipher.getInstance("AES/CTR/NoPadding", "SC");
        }
        catch(Exception e)
        {
        }
    }
}
like image 557
marcwho Avatar asked Dec 10 '15 21:12

marcwho


1 Answers

You just need to write another class which calls javax.crypto.Cipher.getInstance and passes it the "SC" variable. You should be able to do so without making any changes to the original library, and can do so in the same file as the code you have provided. Though this answer seems overly simple so perhaps there are other aspects to your question? That's the answer, anyway.

like image 88
Peter David Carter Avatar answered Nov 04 '22 11:11

Peter David Carter