Home

Awesome

Virgil Pythia Java SDK

Build Status GitHub license

Introduction | SDK Features | Install and configure SDK | Usage Examples | Docs | Support

Introduction

<a href="https://developer.virgilsecurity.com/docs"><img width="230px" src="https://cdn.virgilsecurity.com/assets/images/github/logos/virgil-logo-red.png" align="left" hspace="10" vspace="6"></a>Virgil Security provides an SDK which allows you to communicate with Virgil Pythia Service and implement Pythia protocol for the following use cases:

In both cases you get the mechanism which assures you that neither Virgil nor attackers know anything about user's password.

SDK Features

Install and configure SDK

The Virgil Java SDK is provided as a package named com.virgilsecurity.sdk. The package is distributed via Maven repository.

The package is available for:

Prerequisites:

You can easily add SDK dependency to your project, just follow the examples below.

Install SDK Package

Maven

Apache Maven is a software project management and comprehension tool.

To integrate Virgil SDK into your Java project using Maven, set up dependencies in your pom.xml:

<dependencies>
    <dependency>
        <groupId>com.virgilsecurity</groupId>
        <artifactId>pythia</artifactId>
        <version>0.2.1</version>
    </dependency>
</dependencies>

Gradle

Gradle is an open-source build automation system that builds upon the concepts of Apache Ant and Apache Maven and introduces a Groovy-based domain-specific language (DSL) instead of the XML form used by Apache Maven for declaring the project configuration.

Server

To integrate Virgil SDK into your Java project using Gradle, set up dependencies in your build.gradle:

dependencies {
    compile 'com.virgilsecurity:pythia:0.2.1'
}
Android

To integrate Virgil SDK into your Android project using Gradle, add jcenter() repository if missing:

repositories {
    jcenter()
}

Set up dependencies in your build.gradle:

dependencies {
    implementation 'com.virgilsecurity.sdk:crypto-android:5.0.8@aar'
    implementation ('com.virgilsecurity:pythia:0.2.1') {
        exclude group: 'com.virgilsecurity.sdk', module: 'crypto'
    }
}

Configure SDK

When you create a Pythia Application on the Virgil Dashboard you will receive Application credentials including: Proof Key and App ID. Specify your Pythia Application and Virgil account credentials in a Pythia SDK class instance. These credentials are used for the following purposes:

Here is an example of how to specify your credentials SDK class instance:

// here set your Virgil Account and Pythia Application credentials
PythiaContext context = new PythiaContext.Builder()
    .setAppId("APP_ID")
    .setApiPublicKeyIdentifier("API_KEY_ID")
    .setApiKey("API_KEY")
    .setProofKeys(Arrays.asList("PK.1.PROOF_KEY"))
    .setPythiaCrypto(new VirgilPythiaCrypto())
    .build();

Pythia pythia = new Pythia(context);

Usage Examples

Breach-proof password

Virgil Pythia SDK lets you easily perform all the necessary operations to create, verify and update user's breach-proof password without requiring any additional actions and use Virgil Crypto library.

First of all, you need to set up your database to store users' breach-proof passwords. Create additional columns in your database for storing the following parameters:

<table class="params"> <thead> <tr> <th>Parameters</th> <th>Type</th> <th>Size (bytes)</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td>salt</td> <td>blob</td> <td>32</td> <td> Unique random string that is generated by Pythia SDK for each user</td> </tr> <tr> <td>deblindedPassword</td> <td>blob </td> <td>384 </td> <td>user's breach-proof password</td> </tr> <tr> <td>version</td> <td>int </td> <td>4 </td> <td>Version of your Pythia Application credentials. This parameter has the same value for all users unless you generate new Pythia credentials on Virgil Dashboard</td> </tr> </tbody> </table>

Now we can start creating breach-proof passwords for users. Depending on the situation, you will use one of the following Pythia SDK functions:

Create Breach-Proof Password

Use this flow to create a new breach-proof password for a user.

Remember, if you already have a database with user passwords, you don't have to wait until a user logs in into your system to implement Pythia. You can go through your database and create breach-proof user passwords at any time.

So, in order to create a user's breach-proof password for a new database or available one, go through the following operations:

// create a new Breach-proof password using user's password or its hash
BreachProofPassword pwd = pythia.createBreachProofPassword("USER_PASSWORD");

// save Breach-proof password parameters into your users DB

After performing createBreachProofPassword function you get previously mentioned parameters (salt, deblindedPassword, version), save these parameters into corresponding columns in your database.

Check that you updated all database records and delete the now unnecessary column where user passwords were previously stored.

Verify Breach-Proof Password

Use this flow when a user already has his or her own breach-proof password in your database. You will have to pass his or her password into an verifyBreachProofPassword function:

// get user's Breach-proof password parameters from your users DB

// ...

// calculate user's Breach-proof password parameters
// compare these parameters with parameters from your DB
boolean isValid = pythia.verifyBreachProofPassword("USER_PASSWORD", pwd, true);

if (!isValid) {
    throw new Exception("Authentication failed");
}

The difference between the verifyBreachProofPassword and createBreachProofPassword functions is that the verification of Pythia Service is optional in verifyBreachProofPassword function, which allows you to achieve maximum performance when processing data. You can turn on a proof step in verifyBreachProofPassword function if you have any suspicions that a user or Pythia Service were compromised.

Update breach-proof passwords

This step will allow you to use an updateToken in order to update users' breach-proof passwords in your database.

Use this flow only if your database was COMPROMISED.

How it works:

Here is an example of using the updateBreachProofPassword function:

// get previous user's verifyBreachProofPassword parameters from a compromised DB

// ...

// set up an updateToken that you got on the Virgil Dashboard
// update previous user's Breach-proof password, and save new one into
// your DB

BreachProofPassword updatedPwd =
    pythia.updateBreachProofPassword("UT.1.2.UPDATE_TOKEN", pwd);

BrainKey

PYTHIA Service can be used directly as a means to generate strong cryptographic keys based on user's password or other secret data. We call these keys the BrainKeys. Thus, when you need to restore a Private Key you use only user's Password and Pythia Service.

In order to create a user's BrainKey, go through the following operations:

Generate BrainKey based on user's password

// 1. Specify your JWT provider

// Get generated token from server-side
final String authenticatedQueryToServerSide = "eyJraWQiOiI3MGI0NDdlMzIxZjNhMGZkIiwidHlwIjoiSldUIiwiYWxnIjoiVkVEUzUxMiIsImN0eSI6InZpcmdpbC1qd3Q7dj0xIn0.eyJleHAiOjE1MTg2OTg5MTcsImlzcyI6InZpcmdpbC1iZTAwZTEwZTRlMWY0YmY1OGY5YjRkYzg1ZDc5Yzc3YSIsInN1YiI6ImlkZW50aXR5LUFsaWNlIiwiaWF0IjoxNTE4NjEyNTE3fQ.MFEwDQYJYIZIAWUDBAIDBQAEQP4Yo3yjmt8WWJ5mqs3Yrqc_VzG6nBtrW2KIjP-kxiIJL_7Wv0pqty7PDbDoGhkX8CJa6UOdyn3rBWRvMK7p7Ak";

// Setup AccessTokenProvider
AccessTokenProvider accessTokenProvider = new CachingJwtProvider(new RenewJwtCallback() {

    @Override
    public Jwt renewJwt(TokenContext tokenContext) {
        return new Jwt(authenticatedQueryToServerSide);
    }
});

// 2. Setup BrainKey

BrainKeyContext brainKeyContext = new BrainKeyContext.Builder()
        .setAccessTokenProvider(accessTokenProvider)
        .setPythiaCrypto(new VirgilPythiaCrypto())
        .setPythiaClient(new VirgilPythiaClient())
        .build();
BrainKey brainKey = new BrainKey(brainKeyContext);

// Generate default public/private keypair which is Curve ED25519
// If you need to generate several BrainKeys for the same password,
// use different IDs (optional). Default brainKeyId value is null.
VirgilKeyPair keyPair = brainKey.generateKeyPair("Your password", "Optional BrainKey id");

Generate BrainKey based on unique URL

The typical BrainKey implementation uses a password or concatenated answers to security questions to regenerate the user’s private key. But a unique session link generated by the system admin can also do the trick.

This typically makes the most sense for situations where it’s burdensome to require a password each time a user wants to send or receive messages, like single-session chats in a browser application.

Here’s the general flow of how BrainKey can be used to regenerate a private key based on a unique URL:

Important notes for implementation:

...
    VirgilKeyPair keyPair = brainKey.generateKeyPair("abcdef13803488", "Optional User SSN");
...

Note! if you don't need to use additional parameters, like "Optional User SSN", you can just omit it: VirgilKeyPair keyPair = brainKey.generateKeyPair("abcdef13803488");

Docs

Virgil Security has a powerful set of APIs, and the documentation below can get you started today.

License

This library is released under the 3-clause BSD License.

Support

Our developer support team is here to help you. Find out more information on our Help Center.

You can find us on Twitter or send us email support@VirgilSecurity.com.

Also, get extra help from our support team on Slack.