aboutsummaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/standalone/libs/jni/cpp_utils.cpp
blob: 20d9a3cfa00a51d63581790a78c95c6c5dd54a12 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "cpp_utils.hpp"

jclass ec_parameter_spec_class;
jclass ecgen_parameter_spec_class;
jclass secret_key_spec_class;
jclass pubkey_class;
jclass privkey_class;
jclass keypair_class;
jclass elliptic_curve_class;
jclass fp_field_class;
jclass f2m_field_class;
jclass point_class;
jclass biginteger_class;
jclass illegal_state_exception_class;

void init_classes(JNIEnv *env, std::string lib_name) {
    jclass local_ec_parameter_spec_class = env->FindClass("java/security/spec/ECParameterSpec");
    ec_parameter_spec_class = (jclass) env->NewGlobalRef(local_ec_parameter_spec_class);

    jclass local_ecgen_parameter_spec_class = env->FindClass("java/security/spec/ECGenParameterSpec");
    ecgen_parameter_spec_class = (jclass) env->NewGlobalRef(local_ecgen_parameter_spec_class);

    jclass local_secret_key_spec_class = env->FindClass("javax/crypto/spec/SecretKeySpec");
    secret_key_spec_class = (jclass) env->NewGlobalRef(local_secret_key_spec_class);

    std::string pubkey_class_name("cz/crcs/ectester/standalone/libs/jni/NativeECPublicKey$");
    pubkey_class_name += lib_name;

    jclass local_pubkey_class = env->FindClass(pubkey_class_name.c_str());
    pubkey_class = (jclass) env->NewGlobalRef(local_pubkey_class);

    std::string privkey_class_name("cz/crcs/ectester/standalone/libs/jni/NativeECPrivateKey$");
    privkey_class_name += lib_name;

    jclass local_privkey_class = env->FindClass(privkey_class_name.c_str());
    privkey_class = (jclass) env->NewGlobalRef(local_privkey_class);

    jclass local_keypair_class = env->FindClass("java/security/KeyPair");
    keypair_class = (jclass) env->NewGlobalRef(local_keypair_class);

    jclass local_elliptic_curve_class = env->FindClass("java/security/spec/EllipticCurve");
    elliptic_curve_class = (jclass) env->NewGlobalRef(local_elliptic_curve_class);

    jclass local_fp_field_class = env->FindClass("java/security/spec/ECFieldFp");
    fp_field_class = (jclass) env->NewGlobalRef(local_fp_field_class);

    jclass local_f2m_field_class = env->FindClass("java/security/spec/ECFieldF2m");
    f2m_field_class = (jclass) env->NewGlobalRef(local_f2m_field_class);

    jclass local_biginteger_class = env->FindClass("java/math/BigInteger");
    biginteger_class = (jclass) env->NewGlobalRef(local_biginteger_class);

    jclass local_point_class = env->FindClass("java/security/spec/ECPoint");
    point_class = (jclass) env->NewGlobalRef(local_point_class);

    jclass local_illegal_state_exception_class = env->FindClass("java/lang/IllegalStateException");
    illegal_state_exception_class = (jclass) env->NewGlobalRef(local_illegal_state_exception_class);
}

void throw_new(JNIEnv *env, const std::string& klass, const std::string& message) {
    jclass clazz = env->FindClass(klass.c_str());
    env->ThrowNew(clazz, message.c_str());
}

jint get_kdf_bits(JNIEnv *env, jstring algorithm) {
    if (algorithm == NULL) {
        return 0;
    }

    const char *algo_data = env->GetStringUTFChars(algorithm, NULL);
    std::string algo(algo_data);

    jint result = 0;
    if (algo == "DES") {
        result = 64;
    } else if (algo == "BLOWFISH") {
        result = 128;
    } else if (algo == "DESEDE") {
        result = 192;
    } else if (algo == "AES" || algo == "CAMELLIA") {
        result = 256;
    } else {
        char *end;
        long bits = strtol(algo_data, &end, 10);
        if (*end == 0) {
            result = (jint) bits;
        }
    }
    env->ReleaseStringUTFChars(algorithm, algo_data);
    return result;
}

static void add_provider_property(JNIEnv *env, const std::string &type, const std::string &klass, jobject provider, jmethodID put_method) {
    jstring type_str = env->NewStringUTF(type.c_str());
    jstring class_str = env->NewStringUTF(klass.c_str());
    env->CallObjectMethod(provider, put_method, type_str, class_str);
}

void add_kpg(JNIEnv *env, const std::string &type, const std::string &klass, jobject provider, jmethodID put_method) {
    const std::string full_type = "KeyPairGenerator." + type;
    const std::string full_class = "cz.crcs.ectester.standalone.libs.jni.NativeKeyPairGeneratorSpi$" + klass;
    add_provider_property(env, full_type, full_class, provider, put_method);
}

void add_ka(JNIEnv *env, const std::string &type, const std::string &klass, jobject provider, jmethodID put_method) {
    const std::string full_type = "KeyAgreement." + type;
    const std::string full_class = "cz.crcs.ectester.standalone.libs.jni.NativeKeyAgreementSpi$" + klass;
    add_provider_property(env, full_type, full_class, provider, put_method);
}

void add_sig(JNIEnv *env, const std::string &type, const std::string &klass, jobject provider, jmethodID put_method) {
    const std::string full_type = "Signature." + type;
    const std::string full_class = "cz.crcs.ectester.standalone.libs.jni.NativeSignatureSpi$" + klass;
    add_provider_property(env, full_type, full_class, provider, put_method);
}