summaryrefslogtreecommitdiff
path: root/crypto-pgp
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2021-07-11 22:52:26 +0530
committerGitHub <noreply@github.com>2021-07-11 17:22:26 +0000
commit6e4ffe290265ef8b3cd82f5ab6a7eb8c0157bf6a (patch)
tree13739c1079e5cd4ce609cdbb4501450b2eabecb4 /crypto-pgp
parent9c388e49748084bdac3fb277ea507b80b9c7c33a (diff)
Add initial implementation of Gopenpgp-backed PGP (#1441)
Diffstat (limited to 'crypto-pgp')
-rw-r--r--crypto-pgp/api/crypto-pgp.api7
-rw-r--r--crypto-pgp/build.gradle.kts15
-rw-r--r--crypto-pgp/src/main/AndroidManifest.xml6
-rw-r--r--crypto-pgp/src/main/kotlin/dev/msfjarvis/aps/data/crypto/GopenpgpCryptoHandler.kt49
4 files changed, 77 insertions, 0 deletions
diff --git a/crypto-pgp/api/crypto-pgp.api b/crypto-pgp/api/crypto-pgp.api
new file mode 100644
index 00000000..2164360c
--- /dev/null
+++ b/crypto-pgp/api/crypto-pgp.api
@@ -0,0 +1,7 @@
+public final class dev/msfjarvis/aps/data/crypto/GopenpgpCryptoHandler : dev/msfjarvis/aps/data/crypto/CryptoHandler {
+ public fun <init> ()V
+ public fun canHandle (Ljava/lang/String;)Z
+ public fun decrypt (Ljava/lang/String;[B[B)[B
+ public fun encrypt (Ljava/lang/String;[B)[B
+}
+
diff --git a/crypto-pgp/build.gradle.kts b/crypto-pgp/build.gradle.kts
new file mode 100644
index 00000000..493062b6
--- /dev/null
+++ b/crypto-pgp/build.gradle.kts
@@ -0,0 +1,15 @@
+/*
+ * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+plugins {
+ id("com.android.library")
+ kotlin("android")
+ `aps-plugin`
+}
+
+dependencies {
+ api(projects.cryptoCommon)
+ implementation(libs.aps.gopenpgp)
+ implementation(libs.dagger.hilt.core)
+}
diff --git a/crypto-pgp/src/main/AndroidManifest.xml b/crypto-pgp/src/main/AndroidManifest.xml
new file mode 100644
index 00000000..f72b702d
--- /dev/null
+++ b/crypto-pgp/src/main/AndroidManifest.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8"?><!--
+ ~ Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
+ ~ SPDX-License-Identifier: LGPL-3.0-only WITH LGPL-3.0-linking-exception
+ -->
+
+<manifest package="dev.msfjarvis.aps.cryptopgp"></manifest>
diff --git a/crypto-pgp/src/main/kotlin/dev/msfjarvis/aps/data/crypto/GopenpgpCryptoHandler.kt b/crypto-pgp/src/main/kotlin/dev/msfjarvis/aps/data/crypto/GopenpgpCryptoHandler.kt
new file mode 100644
index 00000000..5d14b160
--- /dev/null
+++ b/crypto-pgp/src/main/kotlin/dev/msfjarvis/aps/data/crypto/GopenpgpCryptoHandler.kt
@@ -0,0 +1,49 @@
+/*
+ * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+package dev.msfjarvis.aps.data.crypto
+
+import com.proton.Gopenpgp.crypto.Crypto
+import com.proton.Gopenpgp.helper.Helper
+import javax.inject.Inject
+
+/** Gopenpgp backed implementation of [CryptoHandler]. */
+public class GopenpgpCryptoHandler @Inject constructor() : CryptoHandler {
+
+ /**
+ * Decrypt the given [ciphertext] using the given PGP [privateKey] and corresponding [passphrase].
+ */
+ override fun decrypt(
+ privateKey: String,
+ passphrase: ByteArray,
+ ciphertext: ByteArray,
+ ): ByteArray {
+ // Decode the incoming cipher into a string and try to guess if it's armored.
+ val cipherString = ciphertext.decodeToString()
+ val isArmor = cipherString.startsWith("-----BEGIN PGP MESSAGE-----")
+ val message =
+ if (isArmor) {
+ Crypto.newPGPMessageFromArmored(cipherString)
+ } else {
+ Crypto.newPGPMessage(ciphertext)
+ }
+ return Helper.decryptBinaryMessageArmored(
+ privateKey,
+ passphrase,
+ message.armored,
+ )
+ }
+
+ override fun encrypt(publicKey: String, plaintext: ByteArray): ByteArray {
+ return Helper.encryptBinaryMessage(
+ publicKey,
+ plaintext,
+ )
+ }
+
+ override fun canHandle(fileName: String): Boolean {
+ return fileName.split('.').last() == "gpg"
+ }
+}