Skip to content

Commit 4335b81

Browse files
committed
Latest
0 parents  commit 4335b81

File tree

79 files changed

+4369
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+4369
-0
lines changed

.gitignore

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild
14+
.cxx
15+
local.properties

.idea/.gitignore

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/compiler.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

+19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# PQCLibary-AndroidJava
2+
3+
NIST Standartlaştırma sürecindeki kafes-tabanlı SABER, CRYSTAL-KYBER ve NTRU algoritmaları ile oluşturulmuştur. Android-Java ortamında test aşamaları yapılmıştır.
4+
5+
6+
Kütüphanenin Kullanımı:
7+
```java
8+
PQCLibary pqcLibary = new PQCLibary("Algoritma_Etiketi"); // Anahtar Üretimi
9+
```
10+
11+
PQC-Library içerisinde kullanılabilecek etiketler aşağıda listelenmiştir.
12+
| Algoritma | PQC-Library Etiketi |
13+
| --- | --- |
14+
| LightSaber | Saber_Light |
15+
| Saber | Saber |
16+
| FireSaber | Saber_Fire |
17+
| Kyber512 | Kyber_512 |
18+
| Kyber768 | Kyber_768|
19+
| ntruhps2048509 | NTRU_509 |
20+
| ntruhps2048677 | NTRU_677 |
21+
| ntruhps4096821 | NTRU_821 |
22+
| ntruhrss701 | NTRU_701 |
23+
24+
Örnek Kullanım:
25+
```java
26+
PQCLibary pqcLibary = new PQCLibary("Kyber_512"); // Anahtar Üretimi
27+
EncapsulationModel enc = pqcLibary.Encapsulation(pqcLibary.pk); //Paketleme
28+
byte[] sharedSecretKey = pqcLibary.Decapsulation(enc.getCipherText(),pqcLibary.sk); // Paket Çözme
29+
```
30+
Bu çalısma 121R006 proje numarasıyla TUBITAK tarafından desteklenmektedir.

app/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
plugins {
2+
id 'com.android.application'
3+
}
4+
5+
android {
6+
compileSdk 32
7+
8+
defaultConfig {
9+
applicationId "com.afd.pqclibary"
10+
minSdk 21
11+
targetSdk 32
12+
versionCode 1
13+
versionName "1.0"
14+
15+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
16+
}
17+
18+
buildTypes {
19+
release {
20+
minifyEnabled false
21+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
22+
}
23+
}
24+
compileOptions {
25+
sourceCompatibility JavaVersion.VERSION_1_8
26+
targetCompatibility JavaVersion.VERSION_1_8
27+
}
28+
}
29+
30+
dependencies {
31+
implementation 'com.github.aelstad:keccakj:1.1.0'
32+
implementation 'androidx.appcompat:appcompat:1.4.1'
33+
implementation 'com.google.android.material:material:1.5.0'
34+
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
35+
testImplementation 'junit:junit:4.13.2'
36+
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
37+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
38+
}

app/proguard-rules.pro

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.sak.pqclibary;
2+
3+
import android.content.Context;
4+
5+
import androidx.test.platform.app.InstrumentationRegistry;
6+
import androidx.test.ext.junit.runners.AndroidJUnit4;
7+
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
11+
import static org.junit.Assert.*;
12+
13+
/**
14+
* Instrumented test, which will execute on an Android device.
15+
*
16+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
17+
*/
18+
@RunWith(AndroidJUnit4.class)
19+
public class ExampleInstrumentedTest {
20+
@Test
21+
public void useAppContext() {
22+
// Context of the app under test.
23+
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
24+
assertEquals("com.ntru.pqclibary", appContext.getPackageName());
25+
}
26+
}

app/src/main/AndroidManifest.xml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.sak.pqclibary">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:roundIcon="@mipmap/ic_launcher_round"
10+
android:supportsRtl="true"
11+
android:theme="@style/Theme.PQCLibary">
12+
<activity
13+
android:name="com.sak.pqclibary.MainActivity"
14+
android:exported="true">
15+
<intent-filter>
16+
<action android:name="android.intent.action.MAIN" />
17+
18+
<category android:name="android.intent.category.LAUNCHER" />
19+
</intent-filter>
20+
</activity>
21+
</application>
22+
23+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.sak.pqclibary;
2+
3+
import androidx.annotation.RequiresApi;
4+
import androidx.appcompat.app.AppCompatActivity;
5+
6+
import android.os.Build;
7+
import android.os.Bundle;
8+
import android.util.Log;
9+
import android.view.View;
10+
11+
import com.sak.pqclibary.kyber.kyberencryption.provider.Kyber1024KeyPairGenerator;
12+
import com.sak.pqclibary.kyber.kyberencryption.provider.KyberKeyPair;
13+
import com.sak.pqclibary.kyber.kyberencryption.provider.KyberProcess;
14+
import com.sak.pqclibary.kyber.kyberencryption.provider.KyberSecretKey;
15+
import com.sak.pqclibary.kyber.kyberencryption.provider.kyber.KyberParams;
16+
import com.sak.pqclibary.saber.Kem;
17+
import com.sak.pqclibary.saber.models.EncapsulationModel;
18+
import com.sak.pqclibary.saber.models.Keys;
19+
import com.sak.pqclibary.saber.utils.Utils;
20+
21+
public class MainActivity extends AppCompatActivity {
22+
23+
@RequiresApi(api = Build.VERSION_CODES.O)
24+
@Override
25+
protected void onCreate(Bundle savedInstanceState) {
26+
super.onCreate(savedInstanceState);
27+
setContentView(R.layout.activity_main);
28+
29+
PQCLibary pqcLibary = new PQCLibary("Kyber_1024");
30+
EncapsulationModel enc = pqcLibary.Encapsulation(pqcLibary.pk);
31+
byte[] sharedSecretKey = pqcLibary.Decapsulation(enc.getCipherText(),pqcLibary.sk);
32+
Log.d("AFD-AFD", Utils.hex(sharedSecretKey));
33+
34+
35+
36+
}
37+
38+
39+
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package com.sak.pqclibary;
2+
3+
import android.os.Build;
4+
5+
import androidx.annotation.RequiresApi;
6+
7+
import com.sak.pqclibary.kyber.kyberencryption.provider.Kyber1024KeyPairGenerator;
8+
import com.sak.pqclibary.kyber.kyberencryption.provider.Kyber512KeyPairGenerator;
9+
import com.sak.pqclibary.kyber.kyberencryption.provider.Kyber768KeyPairGenerator;
10+
import com.sak.pqclibary.kyber.kyberencryption.provider.KyberKeyPair;
11+
import com.sak.pqclibary.kyber.kyberencryption.provider.KyberProcess;
12+
import com.sak.pqclibary.kyber.kyberencryption.provider.kyber.KyberParams;
13+
import com.sak.pqclibary.ntru.OWCPA;
14+
import com.sak.pqclibary.saber.Kem;
15+
import com.sak.pqclibary.saber.Params;
16+
import com.sak.pqclibary.saber.models.EncapsulationModel;
17+
import com.sak.pqclibary.saber.models.Keys;
18+
19+
public class PQCLibary {
20+
byte[] pk;
21+
byte[] sk;
22+
byte[] cipherText;
23+
byte[] ssk1;
24+
byte[] rnd1 = new byte[KyberParams.paramsSymBytes];
25+
int algorithmType;
26+
@RequiresApi(api = Build.VERSION_CODES.O)
27+
public PQCLibary(String algorithmName) {
28+
String[] a = algorithmName.split("_");
29+
Keys keys = null;
30+
31+
if (a.length==1){
32+
this.algorithmType= 1;
33+
new Params(3,8,4);
34+
keys = Kem.crypto_kem_keypair();
35+
}
36+
else if(a.length == 2){
37+
if(a[0].equalsIgnoreCase("Saber")){
38+
if(a[1].equalsIgnoreCase("Light")){
39+
40+
new Params(2,10,3);
41+
}
42+
else if(a[1].equalsIgnoreCase("Fire")){
43+
new Params(4,6,6);
44+
}
45+
this.algorithmType = 1;
46+
keys = Kem.crypto_kem_keypair();
47+
}
48+
else if(a[0].equalsIgnoreCase("NTRU")){
49+
if(a[1].equalsIgnoreCase("509")){
50+
51+
new com.sak.pqclibary.ntru.Params(509);
52+
53+
}
54+
else if(a[1].equalsIgnoreCase("677")){
55+
new com.sak.pqclibary.ntru.Params(677);
56+
}
57+
else if(a[1].equalsIgnoreCase("821")){
58+
new com.sak.pqclibary.ntru.Params(821);
59+
}
60+
this.algorithmType = 2;
61+
keys = OWCPA.owcpa_keypair();
62+
}
63+
64+
else if(a[0].equalsIgnoreCase("Kyber")){
65+
KyberKeyPair kk =null;
66+
67+
if(a[1].equalsIgnoreCase("512")){
68+
kk = Kyber512KeyPairGenerator.generateKeys512();
69+
this.algorithmType = 3;
70+
}
71+
else if(a[1].equalsIgnoreCase("768")){
72+
kk = Kyber768KeyPairGenerator.generateKeys768();
73+
this.algorithmType = 4;
74+
}
75+
else if(a[1].equalsIgnoreCase("1024")){
76+
kk = Kyber1024KeyPairGenerator.generateKeys1024();
77+
this.algorithmType = 5;
78+
}
79+
keys = new Keys(kk.getKyberPublicKey(),kk.getKyberPrivateKey());
80+
}
81+
}
82+
this.pk = keys.getPk();
83+
this.sk = keys.getSk();
84+
}
85+
86+
public EncapsulationModel Encapsulation(byte[] pk){
87+
EncapsulationModel enc;
88+
switch (algorithmType){
89+
case 1:
90+
enc = Kem.crypto_kem_enc(pk);
91+
break;
92+
case 2:
93+
enc = OWCPA.crypto_kem_enc(pk);
94+
break;
95+
case 3:
96+
enc = new EncapsulationModel(KyberProcess.encrypt512(rnd1,pk).getCipherText(),KyberProcess.encrypt512(rnd1,pk).getSecretKey());
97+
break;
98+
case 4:
99+
enc = new EncapsulationModel(KyberProcess.encrypt768(rnd1,pk).getCipherText(),KyberProcess.encrypt768(rnd1,pk).getSecretKey());
100+
break;
101+
case 5:
102+
enc = new EncapsulationModel(KyberProcess.encrypt1024(rnd1,pk).getCipherText(),KyberProcess.encrypt1024(rnd1,pk).getSecretKey());
103+
break;
104+
default:
105+
throw new IllegalStateException("Unexpected value: " + algorithmType);
106+
}
107+
return new EncapsulationModel(enc.getCipherText(),enc.getSs());
108+
}
109+
110+
111+
112+
public byte[] Decapsulation(byte[] ct , byte[] sk){
113+
byte[] ssk2 ;
114+
switch (algorithmType){
115+
case 1:
116+
ssk2 = Kem.crypto_kem_dec(sk,ct);
117+
break;
118+
case 2 :
119+
ssk2 = OWCPA.crypto_kem_dec(ct,sk);
120+
break;
121+
case 3 :
122+
ssk2 = KyberProcess.decrypt512(ct,sk);
123+
break;
124+
case 4 :
125+
ssk2 = KyberProcess.decrypt768(ct,sk);
126+
break;
127+
case 5 :
128+
ssk2 = KyberProcess.decrypt1024(ct,sk);
129+
break;
130+
default:
131+
throw new IllegalStateException("Unexpected value: " + algorithmType);
132+
}
133+
return ssk2;
134+
}
135+
}

0 commit comments

Comments
 (0)