Data security in mobile Java applications

Learn how to select and use third-party security toolkits

1 2 3 4 Page 2
Page 2 of 4

Phaos PBES implementations used in my examples perform worse than their Bouncy Castle counterparts. For RSA and DSA algorithms, Phaos implementations are better optimized than Bouncy Castle 1.16. But public key tasks still take seconds on even high-end mobile phones. In fact, no matter how much optimization you do, those classic PKI algorithms might just prove too heavy for the smallest devices. Novel algorithms and approaches are needed. NTRU and a start-up company called B3 Security provide such solutions.

NTRU Neo for Java toolkit

NTRU PKI algorithms include an encryption algorithm NTRUEncrypt and a signature algorithm NTRUSign, invented and developed by four math professors at Brown University. In my sample programs, NTRU algorithms perform 5 to 30 times faster compared with other public key algorithms with similar cryptographic strength. NTRU algorithms are published and on their way to becoming IEEE (Institute of Electrical and Electronics Engineers) and IETF (Internet Engineering Task Force) standards. NTRU patented the algorithms to protect their business interests. NTRU algorithm patents have been licensed by a variety of mobile software, smart card, and DSP (Digital Signal Processor) chip vendors, including Sony and Texas Instruments.

Cryptographic algorithms are scrutinized and improved repeatedly before considered mature and ready for general public adoption. Although NTRU algorithms have been inspected many times by both academic and business worlds, they are still relatively new. Security weaknesses were identified in NTRUEncrypt as late as May 2001. Those weaknesses do not undermine NTRU algorithm fundamentals and have since been fixed. As you should with any critical project, research NTRU security before licensing it.

NTRU provides an implementation of its algorithms in a Java package called NTRU Neo for Java. You must work out an agreement with NTRU before you can evaluate the package. In addition to NTRU public key algorithms, Neo for Java also includes an implementation of the AES Rijndael symmetric key algorithm. The Neo for Java package runs on CLDC, CDC, and J2SE platforms. It has a memory footprint of 37 KB without signature key generation classes, which have a footprint of 35 KB.

The Neo for Java API is simple and easy to use. In fact, it might be too simplistic. For example, the block encryption method requires users to divide plaintext data into blocks.

Using Neo for Java, NTRUEncrypt key pairs can be generated quickly from pass phrases. The same pass phrase always produces the same key pair. For that reason, Neo for Java does not provide a password-based key store facility. NTRUSign keys, however, are slow to generate and require floating-point support. The Neo for Java package provides a floating-point emulation class, which can support NTRUSign key generation on a CLDC device. But on-device NTRUSign key pair generation takes a long time to complete. Generating and distributing NTRUSign keys from a server computer is a better approach. Fortunately, a signature key can sign thousands of messages before it needs replacement.

B3 Security

B3 Security is a San Jose, Calif. startup that specializes in developing new lightweight security infrastructures that minimize the current overhead associated with PKI. Its flagship products are B3 Tamper Detection and Digital Signature (B3Sig) SDK and B3 End-to-End (B3E2E) Security SDK. Both are available for J2ME. The B3E2E SDK (still in beta) provides features equivalent to SSL in the PKI world, but with a shorter handshake, faster session key establishment, and less management overhead, especially for pushed messages.

I tested the B3Sig SDK, which runs on the J2ME/CLDC platform. The B3 digital signature scheme is based on keyed hash (HMAC, or Keyed-Hashing for Message Authentication) technology, which has been around for many years and has proven security. B3 uniquely uses HMAC properties instead of more computationally intensive public key algorithms, such as large integer factoring, to form a B3 tamper-proof block of bytes and digital signature. In a mobile enterprise application setting, it works like so:

  • In its preferred operation mode, the B3Sig SDK uses two pairs of shared and nonshared secrets. Analogous to the PKI world, a shared secret acts like a public key with targeted distribution scope, and a nonshared secret acts like a private key.

    Each user knows her own password in an existing enterprise identity management system. The system only stores a hash (for example, MD5, or Message Digest 5) of the password. No clear text password is stored anywhere. The B3 SDK uses that hash as the first shared secret. The first nonshared secret comes from a different hash (for example, SHA-1, or Secure Hash Algorithm-1) of the same password.

    B3 software on a device generates a private root key and the corresponding shared secret. They form the second pair of secrets, which ensures stronger authentication. The second shared secret can be used for third-party verification. A B3 protocol can also use the second pair to efficiently reset a forgotten password.

  • Nonshared secrets are used together with the message (or transaction) itself and user ID to generate a unique signing key for every message.
  • A B3 algorithm then generates a digital signature containing three interrelated parts.
  • A B3 algorithm can verify message and user ID integrity with shared secrets. The receiving party can query the password system to verify the sender's authenticity.

B3 scheme has the following advantages:

  • Speed: Cryptographic hash and HMAC algorithms can run 1,000 times faster than public key algorithms.
  • Seamless integration with existing enterprise authentication infrastructure: Various password-based identity management systems are already widely deployed in enterprises (the simplest example is a password file). Utilizing existing password-based identity management systems avoids the expensive overhead of digital certificate management associated with PKI digital signature.
  • Strong two-factor authentication: Only the person who has access to the specific device and knows her application password can generate the correct shared and nonshared secrets to sign messages. That also helps to prevent password guessing and dictionary attacks.
  • Tamper detection: B3Sig SDK has a conservative design: It assumes that no algorithm is permanently secure, including its own. In case of a successful crypto attack on B3 signature and verification algorithms, the sender can still prove that he did not send the forged message. Part of the B3 signature is linked to the nonshared secrets through well-established non-B3 one-way algorithms (HMACs).

B3 solutions do not dictate complete replacement of the current PKI infrastructure. Rather, B3 solutions can coexist and interoperate with the current system. For example, we can use HTTPS as well as B3E2E SDK to pass shared secrets during setup. The application can add delegated PKI signatures on top of the B3 signatures if desired. To use B3 signatures without an existing identity/password management system, we also need to set up a B3 shared secrets store. To learn more technical details about B3 solutions, please contact B3 directly for whitepapers and evaluation SDKs.

Leading security experts in the financial services industry, such as Larry Suto, FTCS cochair at Wells Fargo Bank, and Jim Anderson, a VP of information security at Visa, have agreed to be references for the B3 solutions. If B3 does deliver on its promises, it could become one of the most important security solutions for mobile enterprise applications. However, B3 is still a young company, and its approach has not been tested in large-scale real-world environments. I recommend you investigate the feasibility of B3 solutions yourself.

Device-specific APIs

MIDP device vendors (for example, Motorola iDEN phones) also provide device-specific cryptography API extensions. Those packages utilize device native cryptography libraries and special hardware features. Thus, they likely have good on-device performance.

But applications using vendor-specific APIs are no longer portable to other devices. That causes J2ME platform fragmentation and defeats one of Java's greatest advantages. I do not discuss those APIs in detail here. Interested readers can refer to their device manufacturers' developer manuals.

One way to avoid such fragmentation yet still take advantage of native performance is to develop standard J2ME cryptography API specifications and allow device vendors to plug in their own implementations. The CDC/FP/PP device vendors can implement native JCE providers. However, at the time of this writing, no MIDP compatible lightweight crypto API JSR (Java Specification Request) is being developed in the Java Community Process.

Overview of examples

The sample applications accompanying this article illustrate how to incorporate cryptographic functionalities into your application. They demonstrate how to use symmetric key encryption, public key encryption, digital signature, and password-based encryption. However, due to space limitations, I only discuss the source code for digital signature.

The zip archive, which you can download from Resources, contains three sample applications—each demonstrates API usage of a crypto package (Bouncy Castle, Phaos, and Neo for Java). Inside each sample application, the most important class is CryptoEngine, which stores keys and provides thin wrappers over API methods. CryptoEngine sports a monolithic single class design not optimized for code reuse. Do not consider it a best-practice example. Instead, the examples are designed to get you started with working code quickly. Each CryptoEngine method demonstrates a complete application task (for example, to encrypt a message using RSA).

The CryptoEngine's constructor constructs pregenerated keys from files in the JAR's keys/ directory (work/res/keys directory in the build system). CryptoEngine also has methods to support key generation on mobile devices.

All the toolkits used in the sample applications run on both CLDC and CDC platforms. But to evaluate their performance in the most restricted environment, I provided user interface (UI) drivers for MIDP. Those MIDlets are in the MIDlets package. They measure time spent on each task. Classes in directory work/keygensrc pregenerate keys and serialize them to files in directory work/res/keys before MIDP suite packaging.

Building and running the sample applications is easy. You simply change the parameters in the build.xml file to reflect your system settings and run Ant tasks package and run. I bundled Bouncy Castle 1.16 in the BC sample. For other toolkit samples, you must contact vendors to obtain their software and evaluation licenses. You need to un-JAR the toolkit files into the work/classes directory.

Note: Phaos secure number generator
The Phaos MF for CLDC package is not shipped with random generator classes. You must supply your own generator, which implements the com.phaos.micro.crypto.RandomBitsSource interface using secure random facilities provided by the device vendor. For testing purposes, I supplied SecureRandom and SecureRBS classes in my example package com.enterprisej2me.PhaosMobile.security. Class SecureRandom is based on the Bouncy Castle implementation of java.security.SecureRandom, and SecureRBS implements the com.phaos.micro.crypto.RandomBitsSource interface.

Digital signature examples

Digital signature can guarantee message integrity and authenticity in an open network. To generate a signature, you first calculate a hash (also called digest) of your message. Then you encrypt that hash with your private key to generate a signature. The party at the receiving end first decrypts your signature into a hash using your public key. Then she calculates the hash from your message. If the two hashes match, the message is indeed from you and unaltered.

If someone altered the message during its transmission and generated a new hash based on the modified message, public key algorithms guarantee that he cannot produce a matching signature without knowing your private key. The two parties must share four pieces of information: the message itself, its digital signature, hash and signing algorithms, and the public key.

Figure 4 shows the example application's digital signature MIDlet user interface.

Figure 4. The sample application digital signature UI (for NTRU)

Bouncy Castle

Bouncy Castle supports DSA, RSA, and ECC (Elliptic Curve Cryptography) digital signature algorithms. My code example only shows how to use the RSA signature.

To generate an RSA key pair, BC requires the developer to pick a public exponent. A common pick is 65537 (Hex 10001) for strong security and fast performance:

Listing 1. Bouncy Castle RSA key generation in CryptoEngine

public void generateRSAKeyPair () throws Exception {
  SecureRandom sr = new SecureRandom();
  BigInteger pubExp = new BigInteger("10001", 16);
  RSAKeyGenerationParameters RSAKeyGenPara =
       new RSAKeyGenerationParameters(pubExp, sr, 1024, 80);
  RSAKeyPairGenerator RSAKeyPairGen = new RSAKeyPairGenerator();
  RSAKeyPairGen.init(RSAKeyGenPara);
  AsymmetricCipherKeyPair keyPair = RSAKeyPairGen.generateKeyPair();
  RSAprivKey = (RSAPrivateCrtKeyParameters) keyPair.getPrivate();
  RSApubKey = (RSAKeyParameters) keyPair.getPublic();
}

Bouncy Castle's lightweight package lacks a good key serialization API. To serialize keys for transportation or future use, we must export key parameters one by one:

Listing 2. Bouncy Castle RSA key serialization in keygensrc/GenerateAllKeys.java

1 2 3 4 Page 2
Page 2 of 4