Slide 1

Slide 1 text

Secure your app’s data with JetSec Monika Kumar Jethani

Slide 2

Slide 2 text

About Me Monika is an Android developer with 6 years of experience in mobile application development. She is a regular Android/Kotlin speaker at mobile development conferences/meetups. Last, but not the least, she shares her birthday with Java :) Twitter handle: @monika_jethani

Slide 3

Slide 3 text

Agenda • Need for Encryption • Challenges with Encryption • Intro to JetSec • Double layer security model • Android Keystore and Master Key • Encrypting Files • Encrypting Shared Preferences • Demo • Overview of Tink • What’s new in JetSec

Slide 4

Slide 4 text

Need for encryption • To protect data in shared storage. • To protect sensitive data like personally identifiable information (PII), personal health information(PHI), financial details, enterprise data, etc. • To protect data on a rooted device, even with full-disk encryption. • To prevent the users from accessing tokens and keys used in your app.

Slide 5

Slide 5 text

Challenges with Encryption

Slide 6

Slide 6 text

I don’t know the different encryption algorithms and the differences between them. How can I do encryption of data in my app?

Slide 7

Slide 7 text

JetSec The Jetpack Security library provides an implementation of the security best practices related to reading and writing data at rest, as well as key creation and verification. Source: developer.android.com

Slide 8

Slide 8 text

JetSec Features • Key creation and storage • Encryption Operations • Supported from API levels 23+ • Allows you to locally protect files that may contain sensitive data, API keys, OAuth tokens, and other types of secrets. • Apt for consumer apps such as banking and chat apps as well as enterprise apps. • Provides great encryption as well as good performance. • Apt for apps that require a hardware-backed keystore and user presence for providing key access.

Slide 9

Slide 9 text

Getting started with JetSec implementation "androidx.security:security-crypto:1.0.0-rc03"

Slide 10

Slide 10 text

Double layer Security Model • Keyset - It contains one or more keys to encrypt a file or shared preferences. It is stored in SharedPreferences. • A Master(Primary) key - It encrypts all keysets that are used for each cryptographic operation. This key is stored in Android keystore, making it difficult to extract.

Slide 11

Slide 11 text

Android Keystore • Hardware-backed • Stores cryptographic keys, making them hard to extract. • Stores keys in a trusted execution environment or StrongBox. • To allow JetSec to store keys in StrongBox, we need to enable a flag.

Slide 12

Slide 12 text

Master Key val masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC)

Slide 13

Slide 13 text

Custom Key val keySpecifications = KeyGenParameterSpec.Builder( "keystore_alias", KeyProperties.PURPOSE_ENCRYPT ).apply { setKeySize(256) setBlockModes(KeyProperties.BLOCK_MODE_GCM) setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE) }.build() val masterKey = MasterKeys.getOrCreate(keySpecifications)

Slide 14

Slide 14 text

Add-ons for Key Creation val advancedSpec = KeyGenParameterSpec.Builder( "master_keystore_alias", KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT ).apply { setBlockModes(KeyProperties.BLOCK_MODE_GCM) setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE) setKeySize(256) setUserAuthenticationRequired(true) setUserAuthenticationValidityDurationSeconds(15) // must be larger than 0 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { setUnlockedDeviceRequired(true) setIsStrongBoxBacked(true) } }.build() val masterKeyAlias = MasterKeys.getOrCreate(advancedSpec)

Slide 15

Slide 15 text

Encrypting Files • Uses EncryptedFile class. • Provides custom implementations of FileInputStream and FileOutputStream. • Uses the Streaming Authenticated Encryption with Associated Data (AEAD) primitive of Tink. • Facilitates secure reading and writing operations.

Slide 16

Slide 16 text

Creating an Encrypted File val encryptedFile = EncryptedFile.Builder( secretFile, context, masterKeyAlias, EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB) .build()

Slide 17

Slide 17 text

Reading and Writing to EncryptedFile encryptedFile.openFileOutput().bufferedWriter().use { bufferedWriter -> // Writing some data.. } encryptedFile.openFileInput().bufferedReader().useLines { lines -> // Reading data.. }

Slide 18

Slide 18 text

Encrypting Shared Preferences • Uses EncryptedSharedPreferences class. • Wrapper around SharedPreferences class. • Encrypts keys and values using two-stream method, 1. Keys are encrypted deterministically. 2. Values are encrypted non-deterministically using AES-256 GCM.

Slide 19

Slide 19 text

Creating an Encrypted SharedPreference val sharedPreferences = EncryptedSharedPreferences.create( "encrypted_shared_prefs", //filename masterKeyAlias, context, EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM)

Slide 20

Slide 20 text

Writing to EncryptedSharedPreferences sharedPreferences.edit() .putString(SHARED_PREFERENCES_KEY, data.text.toString()) .apply()

Slide 21

Slide 21 text

Demo of Encrypting and Decrypting Shared Preferences data

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

Contents of Shared Preferences file after encryption

Slide 24

Slide 24 text

What does JetSec use under the hood? • JetSec uses Tink under the hood. • Tink is an open-source, cross-platform security project from Google. • https://github.com/google/tink • Provides cryptographic APIs that are secure, easy to use, harder to misuse.

Slide 25

Slide 25 text

Features of Tink • Key Rotation • Message Signing • Hybrid Encryption • MAC • Google Cloud Key support • Create you own primitives

Slide 26

Slide 26 text

What’s new in JetSec? • Lollipop(API 21+) is now supported • MasterKeys class has become deprecated and a new MasterKey class has come into picture to support new features and new versions of Android that don’t have KeyGenParameterSpec.

Slide 27

Slide 27 text

https://issuetracker.google.com/issues/new?component=618647&template=1257270

Slide 28

Slide 28 text

Thank you For queries, write to me at twitter @monika_jethani