-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Expand file tree
/
Copy pathpal_cipher.c
More file actions
423 lines (342 loc) · 13.3 KB
/
Copy pathpal_cipher.c
File metadata and controls
423 lines (342 loc) · 13.3 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#include "pal_cipher.h"
#include "pal_utilities.h"
enum
{
CIPHER_NONE = 0,
CIPHER_HAS_VARIABLE_TAG = 1,
CIPHER_REQUIRES_IV = 2,
};
typedef uint32_t CipherFlags;
typedef struct CipherInfo
{
CipherFlags flags;
int32_t width;
const char* name;
} CipherInfo;
#define DEFINE_CIPHER(cipherId, width, javaName, flags) \
CipherInfo* AndroidCryptoNative_ ## cipherId(void) \
{ \
static CipherInfo info = { flags, width, javaName }; \
return &info; \
}
DEFINE_CIPHER(Aes128Ecb, 128, "AES/ECB/NoPadding", CIPHER_NONE)
DEFINE_CIPHER(Aes128Cbc, 128, "AES/CBC/NoPadding", CIPHER_REQUIRES_IV)
DEFINE_CIPHER(Aes128Cfb8, 128, "AES/CFB8/NoPadding", CIPHER_REQUIRES_IV)
DEFINE_CIPHER(Aes128Cfb128, 128, "AES/CFB128/NoPadding", CIPHER_REQUIRES_IV)
DEFINE_CIPHER(Aes128Gcm, 128, "AES/GCM/NoPadding", CIPHER_HAS_VARIABLE_TAG | CIPHER_REQUIRES_IV)
DEFINE_CIPHER(Aes128Ccm, 128, "AES/CCM/NoPadding", CIPHER_HAS_VARIABLE_TAG | CIPHER_REQUIRES_IV)
DEFINE_CIPHER(Aes192Ecb, 192, "AES/ECB/NoPadding", CIPHER_NONE)
DEFINE_CIPHER(Aes192Cbc, 192, "AES/CBC/NoPadding", CIPHER_REQUIRES_IV)
DEFINE_CIPHER(Aes192Cfb8, 192, "AES/CFB8/NoPadding", CIPHER_REQUIRES_IV)
DEFINE_CIPHER(Aes192Cfb128, 192, "AES/CFB128/NoPadding", CIPHER_REQUIRES_IV)
DEFINE_CIPHER(Aes192Gcm, 192, "AES/GCM/NoPadding", CIPHER_HAS_VARIABLE_TAG | CIPHER_REQUIRES_IV)
DEFINE_CIPHER(Aes192Ccm, 192, "AES/CCM/NoPadding", CIPHER_HAS_VARIABLE_TAG | CIPHER_REQUIRES_IV)
DEFINE_CIPHER(Aes256Ecb, 256, "AES/ECB/NoPadding", CIPHER_NONE)
DEFINE_CIPHER(Aes256Cbc, 256, "AES/CBC/NoPadding", CIPHER_REQUIRES_IV)
DEFINE_CIPHER(Aes256Cfb8, 256, "AES/CFB8/NoPadding", CIPHER_REQUIRES_IV)
DEFINE_CIPHER(Aes256Cfb128, 256, "AES/CFB128/NoPadding", CIPHER_REQUIRES_IV)
DEFINE_CIPHER(Aes256Gcm, 256, "AES/GCM/NoPadding", CIPHER_HAS_VARIABLE_TAG | CIPHER_REQUIRES_IV)
DEFINE_CIPHER(Aes256Ccm, 256, "AES/CCM/NoPadding", CIPHER_HAS_VARIABLE_TAG | CIPHER_REQUIRES_IV)
DEFINE_CIPHER(DesEcb, 64, "DES/ECB/NoPadding", CIPHER_NONE)
DEFINE_CIPHER(DesCbc, 64, "DES/CBC/NoPadding", CIPHER_REQUIRES_IV)
DEFINE_CIPHER(DesCfb8, 64, "DES/CFB8/NoPadding", CIPHER_REQUIRES_IV)
DEFINE_CIPHER(Des3Ecb, 128, "DESede/ECB/NoPadding", CIPHER_NONE)
DEFINE_CIPHER(Des3Cbc, 128, "DESede/CBC/NoPadding", CIPHER_REQUIRES_IV)
DEFINE_CIPHER(Des3Cfb8, 128, "DESede/CFB8/NoPadding", CIPHER_REQUIRES_IV)
DEFINE_CIPHER(Des3Cfb64, 128, "DESede/CFB/NoPadding", CIPHER_REQUIRES_IV)
DEFINE_CIPHER(ChaCha20Poly1305, 256, "ChaCha20/Poly1305/NoPadding", CIPHER_REQUIRES_IV)
//
// We don't have to check whether `CipherInfo` arguments are valid pointers, as these functions will be called after the
// context is created and the type stored in `CipherInfo` is asserted to be not NULL on creation time. Managed code
// cannot modify the context so it's fairly safe to assume that we're passed a valid pointer here.
//
// The entry functions (those that can be called by external code) take care to validate that the context passed to them
// is a valid pointer and so we can assume the assertion from the preceding paragraph.
//
ARGS_NON_NULL_ALL static bool HasVariableTag(CipherInfo* type)
{
return (type->flags & CIPHER_HAS_VARIABLE_TAG) == CIPHER_HAS_VARIABLE_TAG;
}
ARGS_NON_NULL_ALL static bool RequiresIV(CipherInfo* type)
{
return (type->flags & CIPHER_REQUIRES_IV) == CIPHER_REQUIRES_IV;
}
ARGS_NON_NULL_ALL static jobject GetAlgorithmName(JNIEnv* env, CipherInfo* type)
{
return make_java_string(env, type->name);
}
int32_t AndroidCryptoNative_CipherIsSupported(CipherInfo* type)
{
abort_if_invalid_pointer_argument (type);
JNIEnv* env = GetJNIEnv();
jobject algName = GetAlgorithmName(env, type);
if (!algName)
return FAIL;
jobject cipher = (*env)->CallStaticObjectMethod(env, g_cipherClass, g_cipherGetInstanceMethod, algName);
(*env)->DeleteLocalRef(env, algName);
(*env)->DeleteLocalRef(env, cipher);
// If we were able to call Cipher.getInstance without an exception, like NoSuchAlgorithmException,
// then the algorithm is supported.
return TryClearJNIExceptions(env) ? FAIL : SUCCESS;
}
CipherCtx* AndroidCryptoNative_CipherCreatePartial(CipherInfo* type)
{
abort_if_invalid_pointer_argument (type);
JNIEnv* env = GetJNIEnv();
jobject algName = GetAlgorithmName(env, type);
if (!algName)
return FAIL;
jobject cipher = ToGRef(env, (*env)->CallStaticObjectMethod(env, g_cipherClass, g_cipherGetInstanceMethod, algName));
(*env)->DeleteLocalRef(env, algName);
if (CheckJNIExceptions(env))
{
return FAIL;
}
CipherCtx* ctx = xmalloc(sizeof(CipherCtx));
ctx->cipher = cipher;
ctx->type = type;
ctx->tagLength = TAG_MAX_LENGTH;
ctx->keySizeInBits = type->width;
ctx->ivLength = 0;
ctx->encMode = 0;
ctx->key = NULL;
ctx->iv = NULL;
return ctx;
}
int32_t AndroidCryptoNative_CipherSetTagLength(CipherCtx* ctx, int32_t tagLength)
{
if (!ctx)
return FAIL;
if(tagLength > TAG_MAX_LENGTH)
return FAIL;
ctx->tagLength = tagLength;
return SUCCESS;
}
ARGS_NON_NULL_ALL static int32_t ReinitializeCipher(CipherCtx* ctx)
{
JNIEnv* env = GetJNIEnv();
int32_t ret = FAIL;
INIT_LOCALS(loc, algName, keyBytes, sksObj, ivBytes, ivPsObj);
// SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "AES");
// IvParameterSpec ivSpec = new IvParameterSpec(IV); or GCMParameterSpec for GCM/CCM
// cipher.init(encMode, keySpec, ivSpec);
loc[algName] = GetAlgorithmName(env, ctx->type);
if (!loc[algName])
goto cleanup;
int32_t keyLength = ctx->keySizeInBits / 8;
loc[keyBytes] = make_java_byte_array(env, keyLength);
(*env)->SetByteArrayRegion(env, loc[keyBytes], 0, keyLength, (jbyte*)ctx->key);
loc[sksObj] = (*env)->NewObject(env, g_sksClass, g_sksCtor, loc[keyBytes], loc[algName]);
ON_EXCEPTION_PRINT_AND_GOTO(cleanup);
if (RequiresIV(ctx->type))
{
loc[ivBytes] = make_java_byte_array(env, ctx->ivLength);
(*env)->SetByteArrayRegion(env, loc[ivBytes], 0, ctx->ivLength, (jbyte*)ctx->iv);
if (HasVariableTag(ctx->type))
{
loc[ivPsObj] = (*env)->NewObject(env, g_GCMParameterSpecClass, g_GCMParameterSpecCtor, ctx->tagLength * 8, loc[ivBytes]);
}
else
{
loc[ivPsObj] = (*env)->NewObject(env, g_ivPsClass, g_ivPsCtor, loc[ivBytes]);
}
ON_EXCEPTION_PRINT_AND_GOTO(cleanup);
}
(*env)->CallVoidMethod(env, ctx->cipher, g_cipherInitMethod, ctx->encMode, loc[sksObj], loc[ivPsObj]);
ON_EXCEPTION_PRINT_AND_GOTO(cleanup);
ret = SUCCESS;
cleanup:
RELEASE_LOCALS(loc, env);
return ret;
}
int32_t AndroidCryptoNative_CipherSetKeyAndIV(CipherCtx* ctx, uint8_t* key, uint8_t* iv, int32_t enc)
{
if (!ctx)
return FAIL;
// input: 0 for Decrypt, 1 for Encrypt, -1 leave untouched
// Cipher: 2 for Decrypt, 1 for Encrypt, N/A
if (enc != -1)
{
abort_unless(enc == 0 || enc == 1, "The 'enc' parameter must be either 1 or 0");
ctx->encMode = enc == 0 ? CIPHER_DECRYPT_MODE : CIPHER_ENCRYPT_MODE;
}
// CryptoNative_CipherSetKeyAndIV can be called separately for key and iv
// so we need to wait for both and do Init after.
if (key)
SaveTo(key, &ctx->key, (size_t)ctx->keySizeInBits / 8, /* overwrite */ true);
if (iv)
{
// Make sure length is set
if (!ctx->ivLength)
{
// ivLength = cipher.getBlockSize();
JNIEnv *env = GetJNIEnv();
ctx->ivLength = (*env)->CallIntMethod(env, ctx->cipher, g_getBlockSizeMethod);
}
SaveTo(iv, &ctx->iv, (size_t)ctx->ivLength, /* overwrite */ true);
}
if (!ctx->key || (!ctx->iv && RequiresIV(ctx->type)))
return SUCCESS;
return ReinitializeCipher(ctx);
}
CipherCtx* AndroidCryptoNative_CipherCreate(CipherInfo* type, uint8_t* key, int32_t keySizeInBits, uint8_t* iv, int32_t enc)
{
CipherCtx* ctx = AndroidCryptoNative_CipherCreatePartial(type);
// Update the key size if provided
if (keySizeInBits > 0)
ctx->keySizeInBits = keySizeInBits;
if (AndroidCryptoNative_CipherSetKeyAndIV(ctx, key, iv, enc) != SUCCESS)
return FAIL;
return ctx;
}
int32_t AndroidCryptoNative_CipherUpdateAAD(CipherCtx* ctx, uint8_t* in, int32_t inl)
{
if (!ctx)
return FAIL;
abort_if_invalid_pointer_argument(in);
JNIEnv* env = GetJNIEnv();
jbyteArray inDataBytes = make_java_byte_array(env, inl);
(*env)->SetByteArrayRegion(env, inDataBytes, 0, inl, (jbyte*)in);
(*env)->CallVoidMethod(env, ctx->cipher, g_cipherUpdateAADMethod, inDataBytes);
(*env)->DeleteLocalRef(env, inDataBytes);
return CheckJNIExceptions(env) ? FAIL : SUCCESS;
}
int32_t AndroidCryptoNative_CipherUpdate(CipherCtx* ctx, uint8_t* outm, int32_t* outl, uint8_t* in, int32_t inl)
{
if (!ctx)
return FAIL;
if (!outl && !in)
// it means caller wants us to record "inl" but we don't need it.
return SUCCESS;
abort_if_invalid_pointer_argument(outl);
abort_if_invalid_pointer_argument(in);
JNIEnv* env = GetJNIEnv();
jbyteArray inDataBytes = make_java_byte_array(env, inl);
(*env)->SetByteArrayRegion(env, inDataBytes, 0, inl, (jbyte*)in);
*outl = 0;
jbyteArray outDataBytes = (jbyteArray)(*env)->CallObjectMethod(env, ctx->cipher, g_cipherUpdateMethod, inDataBytes);
if (outDataBytes && outm)
{
jsize outDataBytesLen = (*env)->GetArrayLength(env, outDataBytes);
*outl = outDataBytesLen;
(*env)->GetByteArrayRegion(env, outDataBytes, 0, outDataBytesLen, (jbyte*) outm);
(*env)->DeleteLocalRef(env, outDataBytes);
}
(*env)->DeleteLocalRef(env, inDataBytes);
return CheckJNIExceptions(env) ? FAIL : SUCCESS;
}
int32_t AndroidCryptoNative_CipherFinalEx(CipherCtx* ctx, uint8_t* outm, int32_t* outl)
{
if (!ctx)
return FAIL;
abort_if_invalid_pointer_argument(outm);
abort_if_invalid_pointer_argument(outl);
JNIEnv* env = GetJNIEnv();
*outl = 0;
jbyteArray outBytes = (jbyteArray)(*env)->CallObjectMethod(env, ctx->cipher, g_cipherDoFinalMethod);
if (CheckJNIExceptions(env)) return FAIL;
jsize outBytesLen = (*env)->GetArrayLength(env, outBytes);
*outl = outBytesLen;
(*env)->GetByteArrayRegion(env, outBytes, 0, outBytesLen, (jbyte*) outm);
(*env)->DeleteLocalRef(env, outBytes);
return CheckJNIExceptions(env) ? FAIL : SUCCESS;
}
int32_t AndroidCryptoNative_AeadCipherFinalEx(CipherCtx* ctx, uint8_t* outm, int32_t* outl, int32_t* authTagMismatch)
{
if (!ctx)
return FAIL;
// outm is allowed to be NULL
abort_if_invalid_pointer_argument(outl);
abort_if_invalid_pointer_argument(authTagMismatch);
JNIEnv* env = GetJNIEnv();
*outl = 0;
*authTagMismatch = 0;
jbyteArray outBytes = (jbyteArray)(*env)->CallObjectMethod(env, ctx->cipher, g_cipherDoFinalMethod);
jthrowable ex = NULL;
if (TryGetJNIException(env, &ex, false))
{
if (ex == NULL)
{
return FAIL;
}
if ((*env)->IsInstanceOf(env, ex, g_AEADBadTagExceptionClass))
{
*authTagMismatch = 1;
}
(*env)->DeleteLocalRef(env, ex);
return FAIL;
}
jsize outBytesLen = (*env)->GetArrayLength(env, outBytes);
*outl = outBytesLen;
if (outBytesLen > 0)
{
abort_if_invalid_pointer_argument(outm);
(*env)->GetByteArrayRegion(env, outBytes, 0, outBytesLen, (jbyte*) outm);
}
(*env)->DeleteLocalRef(env, outBytes);
return CheckJNIExceptions(env) ? FAIL : SUCCESS;
}
int32_t AndroidCryptoNative_CipherCtxSetPadding(CipherCtx* ctx, int32_t padding)
{
if (!ctx)
return FAIL;
if (padding == 0)
{
return SUCCESS;
}
else
{
// TODO: re-init ctx->cipher ?
LOG_ERROR("Non-zero padding (%d) is not supported yet", (int)padding);
return FAIL;
}
}
int32_t AndroidCryptoNative_CipherReset(CipherCtx* ctx, uint8_t* pIv, int32_t cIv)
{
if (!ctx)
return FAIL;
JNIEnv* env = GetJNIEnv();
ReleaseGRef(env, ctx->cipher);
jobject algName = GetAlgorithmName(env, ctx->type);
if (!algName)
return FAIL;
// Resetting is only for the cipher, not the context.
// We recreate and reinitialize a cipher with the same context.
ctx->cipher = ToGRef(env, (*env)->CallStaticObjectMethod(env, g_cipherClass, g_cipherGetInstanceMethod, algName));
(*env)->DeleteLocalRef(env, algName);
if (CheckJNIExceptions(env))
return FAIL;
if (pIv)
{
if (ctx->ivLength != cIv)
{
return FAIL;
}
SaveTo(pIv, &ctx->iv, (size_t)ctx->ivLength, /* overwrite */ true);
}
else if (cIv != 0)
{
return FAIL;
}
return ReinitializeCipher(ctx);
}
int32_t AndroidCryptoNative_CipherSetNonceLength(CipherCtx* ctx, int32_t ivLength)
{
if (!ctx)
return FAIL;
ctx->ivLength = ivLength;
return SUCCESS;
}
void AndroidCryptoNative_CipherDestroy(CipherCtx* ctx)
{
if (ctx)
{
JNIEnv* env = GetJNIEnv();
ReleaseGRef(env, ctx->cipher);
free(ctx->key);
free(ctx->iv);
free(ctx);
}
}