android_keyring/
cipher.rs1use crate::{
2 keystore::Key,
3 methods::{
4 ClassDecl, Constructible, FromValue, JResult, Method, NoParam, SignatureComp, StaticMethod,
5 ToValue,
6 },
7};
8use jni::{
9 JNIEnv,
10 objects::{GlobalRef, JObject, JValueGen},
11};
12use std::marker::PhantomData;
13
14pub struct Cipher {
15 self_: GlobalRef,
16}
17impl FromValue for Cipher {
18 fn signature() -> SignatureComp {
19 Self::class().into()
20 }
21
22 fn from_object(self_: GlobalRef, _env: &mut JNIEnv) -> JResult<Self> {
23 Ok(Self { self_ })
24 }
25}
26impl Cipher {
27 fn class() -> ClassDecl {
28 ClassDecl("Ljavax/crypto/Cipher;")
29 }
30
31 pub fn get_instance(env: &mut JNIEnv, transformation: &str) -> JResult<Self> {
32 struct ThisMethod<'a>(PhantomData<&'a ()>);
33 impl<'a> StaticMethod for ThisMethod<'a> {
34 type Param = &'a str;
35 type Return = Cipher;
36
37 const NAME: &'static str = "getInstance";
38 }
39 ThisMethod::call(Self::class(), env, transformation)
40 }
41
42 pub fn init(&self, env: &mut JNIEnv, mode: i32, key: &Key) -> JResult<()> {
43 struct ThisMethod<'a>(PhantomData<&'a ()>);
44 impl<'a> Method for ThisMethod<'a> {
45 type Param = (i32, &'a Key);
46 type Return = ();
47
48 const NAME: &'static str = "init";
49 }
50
51 ThisMethod::call(&self.self_, env, (mode, key))
52 }
53
54 pub fn init2(
55 &self,
56 env: &mut JNIEnv,
57 mode: i32,
58 key: &Key,
59 spec: AlgorithmParameterSpec,
60 ) -> JResult<()> {
61 struct ThisMethod<'a>(PhantomData<&'a ()>);
62 impl<'a> Method for ThisMethod<'a> {
63 type Param = (i32, &'a Key, AlgorithmParameterSpec);
64 type Return = ();
65
66 const NAME: &'static str = "init";
67 }
68 ThisMethod::call(&self.self_, env, (mode, key, spec))
69 }
70
71 pub fn get_iv(&self, env: &mut JNIEnv) -> JResult<Vec<u8>> {
72 struct ThisMethod;
73 impl Method for ThisMethod {
74 type Param = NoParam;
75 type Return = Vec<u8>;
76
77 const NAME: &str = "getIV";
78 }
79 ThisMethod::call(&self.self_, env, NoParam)
80 }
81
82 pub fn do_final(&self, env: &mut JNIEnv, input: &[u8]) -> JResult<Vec<u8>> {
83 struct ThisMethod<'a>(PhantomData<&'a ()>);
84 impl<'a> Method for ThisMethod<'a> {
85 type Param = &'a [u8];
86 type Return = Vec<u8>;
87
88 const NAME: &'static str = "doFinal";
89 }
90 ThisMethod::call(&self.self_, env, input)
91 }
92}
93
94pub struct AlgorithmParameterSpec {
95 self_: GlobalRef,
96}
97impl ToValue for AlgorithmParameterSpec {
98 fn signature() -> SignatureComp {
99 Self::class().into()
100 }
101
102 fn to_value<'a>(&self, env: &mut JNIEnv<'a>) -> JResult<JValueGen<JObject<'a>>> {
103 Ok(env.new_local_ref(self.self_.as_obj())?.into())
104 }
105}
106impl AlgorithmParameterSpec {
107 fn class() -> ClassDecl {
108 ClassDecl("Ljava/security/spec/AlgorithmParameterSpec;")
109 }
110}
111
112pub struct GCMParameterSpec {
113 self_: GlobalRef,
114}
115impl FromValue for GCMParameterSpec {
116 fn signature() -> SignatureComp {
117 Self::class().into()
118 }
119
120 fn from_object(self_: GlobalRef, _env: &mut JNIEnv) -> JResult<Self> {
121 Ok(Self { self_ })
122 }
123}
124impl ToValue for GCMParameterSpec {
125 fn signature() -> SignatureComp {
126 Self::class().into()
127 }
128
129 fn to_value<'a>(&self, env: &mut JNIEnv<'a>) -> JResult<JValueGen<JObject<'a>>> {
130 Ok(env.new_local_ref(self.self_.as_obj())?.into())
131 }
132}
133impl GCMParameterSpec {
134 fn class() -> ClassDecl {
135 ClassDecl("Ljavax/crypto/spec/GCMParameterSpec;")
136 }
137
138 pub fn new(env: &mut JNIEnv, tag_len: i32, iv: &[u8]) -> JResult<GCMParameterSpec> {
139 struct ThisMethod<'a>(PhantomData<&'a ()>);
140 impl<'a> Constructible for ThisMethod<'a> {
141 type Param = (i32, &'a [u8]);
142 type Return = GCMParameterSpec;
143 }
144 ThisMethod::call_new(Self::class(), env, (tag_len, iv))
145 }
146}
147impl From<GCMParameterSpec> for AlgorithmParameterSpec {
148 fn from(value: GCMParameterSpec) -> Self {
149 Self { self_: value.self_ }
150 }
151}