summaryrefslogtreecommitdiff
path: root/beecrypt/javaglue.c
blob: 251abfc9b91daf941e1836119788370f6ff62edc (plain)
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
/** \ingroup JAVA_m
 * \file javaglue.c
 */

#define BEECRYPT_DLL_EXPORT

#define	JNIEXPORT /*@unused@*/
#define	JNICALL

#include "beecrypt.h"
#include "blockmode.h"

#undef	JAVAGLUE
#define	JAVAGLUE	0	/* XXX disable for now */

#if JAVAGLUE

#if HAVE_STDLIB_H
# include <stdlib.h>
#endif
#if HAVE_MALLOC_H
# include <malloc.h>
#endif

#include "javaglue.h"

/* For now, I'm lazy ... */
/*@-nullpass -nullret -shiftsigned -usedef -temptrans -freshtrans @*/
/*@-noeffectuncon -globs -globnoglobs -modunconnomods -modnomods @*/
/*@-mustfree@*/

#ifndef WORDS_BIGENDIAN
# define WORDS_BIGENDIAN	0
#endif

/*@observer@*/
static const char* JAVA_OUT_OF_MEMORY_ERROR = "java/lang/OutOfMemoryError";
/*@observer@*/
static const char* JAVA_PROVIDER_EXCEPTION = "java/security/ProviderException";
/*@observer@*/
static const char* JAVA_INVALID_KEY_EXCEPTION = "java/security/InvalidKeyException";
/*@observer@*/
static const char* MSG_OUT_OF_MEMORY = "out of memory";
/*@observer@*/
static const char* MSG_NO_SUCH_ALGORITHM = "algorithm not available";
/*@observer@*/
static const char* MSG_NO_ENTROPY_SOURCE = "no entropy source";
/*@observer@*/
static const char* MSG_INVALID_KEY = "invalid key";

/* NativeMessageDigest */

jlong JNICALL Java_beecrypt_security_NativeMessageDigest_find(JNIEnv* env, /*@unused@*/ jclass dummy, jstring algorithm)
{
	const char* name = (*env)->GetStringUTFChars(env, algorithm, (jboolean*) 0);
	const hashFunction* hash = hashFunctionFind(name);
	(*env)->ReleaseStringUTFChars(env, algorithm, name);
	if (hash == (hashFunction*) 0)
	{
		jclass ex = (*env)->FindClass(env, JAVA_PROVIDER_EXCEPTION);
		if (ex != (jclass) 0)
			(void) (*env)->ThrowNew(env, ex, MSG_NO_SUCH_ALGORITHM);
	}
	return (jlong) hash;
}

jlong JNICALL Java_beecrypt_security_NativeMessageDigest_allocParam(JNIEnv* env, /*@unused@*/ jclass dummy, jlong hash)
{
	void *param = malloc(((const hashFunction*) hash)->paramsize);
	if (param == (void*) 0)
	{
		jclass ex = (*env)->FindClass(env, JAVA_OUT_OF_MEMORY_ERROR);
		if (ex != (jclass) 0)
			(void) (*env)->ThrowNew(env, ex, MSG_OUT_OF_MEMORY);
	}
	return (jlong) param;
}

jlong JNICALL Java_beecrypt_security_NativeMessageDigest_cloneParam(/*@unused@*/ JNIEnv* env, /*@unused@*/ jclass dummy, jlong hash, jlong param)
{
	unsigned int paramsize = ((const hashFunction*) hash)->paramsize;
	void *clone = malloc(paramsize);
	memcpy(clone, (void*) param, paramsize);
	return (jlong) clone;
}

void JNICALL Java_beecrypt_security_NativeMessageDigest_freeParam(/*@unused@*/ JNIEnv* env, /*@unused@*/ jclass dummy, jlong param)
{
	if (param)
		free((void*) param);
}

void JNICALL Java_beecrypt_security_NativeMessageDigest_reset(/*@unused@*/ JNIEnv* env, /*@unused@*/ jclass dummy, jlong hash, jlong param)
{
	(void) ((const hashFunction*) hash)->reset((hashFunctionParam*) param);
}

void JNICALL Java_beecrypt_security_NativeMessageDigest_update(/*@unused@*/ JNIEnv* env, /*@unused@*/ jclass dummy, jlong hash, jlong param, jbyte input)
{
	(void) ((const hashFunction*) hash)->update((hashFunctionParam*) param, (const byte*) &input, 1);
}

void JNICALL Java_beecrypt_security_NativeMessageDigest_updateBlock(JNIEnv* env, /*@unused@*/ jclass dummy, jlong hash, jlong param, jbyteArray input, jint offset, jint len)
{
	jbyte* data = (*env)->GetByteArrayElements(env, input, (jboolean*) 0);
	if (data == (jbyte*) 0)
	{
		jclass ex = (*env)->FindClass(env, JAVA_OUT_OF_MEMORY_ERROR);
		(*env)->ReleaseByteArrayElements(env, input, data, JNI_ABORT);
		if (ex)
			(void) (*env)->ThrowNew(env, ex, MSG_OUT_OF_MEMORY);
		return;
	}
	(void) ((const hashFunction*) hash)->update((hashFunctionParam*) param, (const byte*) data+offset, len);
	(*env)->ReleaseByteArrayElements(env, input, data, JNI_ABORT);
}

jbyteArray JNICALL Java_beecrypt_security_NativeMessageDigest_digest(JNIEnv* env, /*@unused@*/ jclass dummy, jlong hash, jlong param)
{
	jbyteArray digestArray;
	jbyte* digest;

	int digestsize = (jsize) ((const hashFunction*) hash)->digestsize;
	int digestwords = digestsize >> 2;

	digestArray = (*env)->NewByteArray(env, digestsize);
	digest = (*env)->GetByteArrayElements(env, digestArray, (jboolean*) 0);

	if (digest == (jbyte*) 0)
	{
		jclass ex = (*env)->FindClass(env, JAVA_OUT_OF_MEMORY_ERROR);
		if (ex)
			(void) (*env)->ThrowNew(env, ex, MSG_OUT_OF_MEMORY);
		return (jbyteArray) 0;
	}

	if (!WORDS_BIGENDIAN || (int) digest & 0x3)
	{	/* unaligned, or swap necessary */
		uint32* data = (uint32*) malloc(digestwords * sizeof(uint32));

		if (data == (uint32*) 0)
		{
			jclass ex = (*env)->FindClass(env, JAVA_OUT_OF_MEMORY_ERROR);
			(*env)->ReleaseByteArrayElements(env, digestArray, digest, JNI_ABORT);
			if (ex)
				(void) (*env)->ThrowNew(env, ex, MSG_OUT_OF_MEMORY);
			return (jbyteArray) 0;
		}

		(void) ((const hashFunction*) hash)->digest((hashFunctionParam*) param, data);
		(void) encodeInts((const javaint*) data, digest, digestwords);
		free(data);
	}
	else
	{	/* aligned */
		(void) ((const hashFunction*) hash)->digest((hashFunctionParam*) param, (uint32*) digest);
	}

	(*env)->ReleaseByteArrayElements(env, digestArray, digest, 0);

	return digestArray;
}

jint JNICALL Java_beecrypt_security_NativeMessageDigest_digestLength(/*@unused@*/ JNIEnv* env, /*@unused@*/ jclass dummy, jlong hash)
{
	return (jint) ((const hashFunction*) hash)->digestsize;
}

/* NativeSecureRandom */

jlong JNICALL Java_beecrypt_security_NativeSecureRandom_find(JNIEnv* env, /*@unused@*/ jclass dummy, jstring algorithm)
{
	const char* name = (*env)->GetStringUTFChars(env, algorithm, (jboolean*) 0);
	const randomGenerator* prng = randomGeneratorFind(name);
	(*env)->ReleaseStringUTFChars(env, algorithm, name);
	if (prng == (randomGenerator*) 0)
	{
		jclass ex = (*env)->FindClass(env, JAVA_PROVIDER_EXCEPTION);
		if (ex)
			(void) (*env)->ThrowNew(env, ex, MSG_NO_SUCH_ALGORITHM);
	}
	return (jlong) prng;
}

jlong JNICALL Java_beecrypt_security_NativeSecureRandom_allocParam(JNIEnv* env, /*@unused@*/ jclass dummy, jlong prng)
{
	void *param = malloc(((const randomGenerator*) prng)->paramsize);
	if (param == (void*) 0)
	{
		jclass ex = (*env)->FindClass(env, JAVA_OUT_OF_MEMORY_ERROR);
		if (ex)
			(void) (*env)->ThrowNew(env, ex, MSG_OUT_OF_MEMORY);
	}
	return (jlong) param;
}

jlong JNICALL Java_beecrypt_security_NativeSecureRandom_cloneParam(/*@unused@*/ JNIEnv* env, /*@unused@*/ jclass dummy, jlong prng, jlong param)
{
	unsigned int paramsize = ((const randomGenerator*) prng)->paramsize;
	void *clone = malloc(paramsize);
	memcpy(clone, (void*) param, paramsize);
	return (jlong) clone;
}

void JNICALL Java_beecrypt_security_NativeSecureRandom_freeParam(/*@unused@*/ JNIEnv* env, /*@unused@*/ jclass dummy, jlong param)
{
	if (param)
		free((void*) param);
}

void JNICALL Java_beecrypt_security_NativeSecureRandom_setup(/*@unused@*/ JNIEnv* env, /*@unused@*/ jclass dummy, jlong prng, jlong param)
{
	(void) ((const randomGenerator*) prng)->setup((randomGeneratorParam*) param);
}

void JNICALL Java_beecrypt_security_NativeSecureRandom_setSeed(JNIEnv* env, /*@unused@*/ jclass dummy, jlong prng, jlong param, jbyteArray seedArray)
{
	/* BeeCrypt takes size in words */
	jsize seedSize = (*env)->GetArrayLength(env, seedArray);
	if (seedSize)
	{
		jbyte* seed = (*env)->GetByteArrayElements(env, seedArray, (jboolean*) 0);
		if (seed == (jbyte*) 0)
		{
			jclass ex = (*env)->FindClass(env, JAVA_OUT_OF_MEMORY_ERROR);
			if (ex)
				(void) (*env)->ThrowNew(env, ex, MSG_OUT_OF_MEMORY);
			return;
		}
		/* check memory alignment of seed and seedSize */
		if (((int) seed & 0x3) || (seedSize & 0x3))
		{	/* unaligned */
			int size = (seedSize+3) >> 2;
			uint32* data = (uint32*) malloc(size * sizeof(uint32));

			if (data == (uint32*) 0)
			{
				jclass ex = (*env)->FindClass(env, JAVA_OUT_OF_MEMORY_ERROR);
				(*env)->ReleaseByteArrayElements(env, seedArray, seed, JNI_ABORT);
				if (ex)
					(void) (*env)->ThrowNew(env, ex, MSG_OUT_OF_MEMORY);
				return;
			}
			(void) decodeIntsPartial(data, seed, seedSize);
			(void) ((const randomGenerator*) prng)->seed((randomGeneratorParam*) param, data, size);
			free(data);
		}
		else
		{	/* aligned and properly sized */
			(void) ((const randomGenerator*) prng)->seed((randomGeneratorParam*) param, (uint32*) seed, seedSize >> 2);
		}

		(*env)->ReleaseByteArrayElements(env, seedArray, seed, JNI_ABORT);
	}
}

void JNICALL Java_beecrypt_security_NativeSecureRandom_nextBytes(JNIEnv* env, /*@unused@*/ jclass dummy, jlong prng, jlong param, jbyteArray bytesArray)
{
	/* BeeCrypt takes size in words */
	jsize bytesSize = (*env)->GetArrayLength(env, bytesArray);
	if (bytesSize)
	{
		jbyte* bytes = (*env)->GetByteArrayElements(env, bytesArray, (jboolean*) 0);
		if (bytes == (jbyte*) 0)
		{
			jclass ex = (*env)->FindClass(env, JAVA_OUT_OF_MEMORY_ERROR);
			if (ex)
				(void) (*env)->ThrowNew(env, ex, MSG_OUT_OF_MEMORY);
			return;
		}

		/* check memory alignment of bytes and bytesSize */
		if (((int) bytes & 0x3) || (bytesSize & 0x3))
		{	/* unaligned */
			int size = (bytesSize+3) >> 2;
			uint32* data = (uint32*) malloc(size * sizeof(uint32));

			if (data == (uint32*) 0)
			{
				jclass ex = (*env)->FindClass(env, JAVA_OUT_OF_MEMORY_ERROR);
				(*env)->ReleaseByteArrayElements(env, bytesArray, bytes, JNI_ABORT);
				if (ex)
					(void) (*env)->ThrowNew(env, ex, MSG_OUT_OF_MEMORY);
				return;
			}

			(void) ((const randomGenerator*) prng)->next((randomGeneratorParam*) param, data, size);
			memcpy(bytes, data, bytesSize);
			free(data);
		}
		else
		{	/* aligned and properly sized */
			(void) ((const randomGenerator*) prng)->next((randomGeneratorParam*) param, (uint32*) bytes, bytesSize >> 2);
		}

		(*env)->ReleaseByteArrayElements(env, bytesArray, bytes, 0);
	}
}

void JNICALL Java_beecrypt_security_NativeSecureRandom_generateSeed(JNIEnv* env, /*@unused@*/ jclass dummy, jbyteArray seedArray)
{
	/* BeeCrypt takes size in words */
	jsize seedSize = (*env)->GetArrayLength(env, seedArray);

	if (seedSize)
	{
		jbyte* seed = (*env)->GetByteArrayElements(env, seedArray, (jboolean*) 0);

		const entropySource* ents = entropySourceDefault();
	
		if (seed == (jbyte*) 0)
		{
			jclass ex = (*env)->FindClass(env, JAVA_OUT_OF_MEMORY_ERROR);
			if (ex)
				(void) (*env)->ThrowNew(env, ex, MSG_OUT_OF_MEMORY);
			return;
		}

		if (ents == (entropySource*) 0)
		{
			jclass ex = (*env)->FindClass(env, JAVA_PROVIDER_EXCEPTION);
			(*env)->ReleaseByteArrayElements(env, seedArray, seed, JNI_ABORT);
			if (ex)
				(void) (*env)->ThrowNew(env, ex, MSG_NO_ENTROPY_SOURCE);
			return;
		}

		/* check memory alignment of seed and seedSize */
		if (((int) seed & 0x3) || (seedSize & 0x3))
		{	/* unaligned */
			int size = (seedSize+3) >> 2;
			uint32* data = (uint32*) malloc(size * sizeof(uint32));
			(void) ents->next(data, size);
			memcpy(seed, data, seedSize);
			free(data);
		}
		else
		{	/* aligned */
			(void) ents->next((uint32*) seed, seedSize >> 2);
		}

		(*env)->ReleaseByteArrayElements(env, seedArray, seed, 0);
	}
}

/* NativeBlockCipher */

jlong JNICALL Java_beecrypt_crypto_NativeBlockCipher_find(JNIEnv* env, /*@unused@*/ jclass dummy, jstring algorithm)
{
	const char* name = (*env)->GetStringUTFChars(env, algorithm, (jboolean*) 0);
	const blockCipher* ciph = blockCipherFind(name);
	(*env)->ReleaseStringUTFChars(env, algorithm, name);
	if (ciph == (blockCipher*) 0)
	{
		jclass ex = (*env)->FindClass(env, JAVA_PROVIDER_EXCEPTION);
		if (ex)
			(void) (*env)->ThrowNew(env, ex, MSG_NO_SUCH_ALGORITHM);
	}
	return (jlong) ciph;
}

jlong JNICALL Java_beecrypt_crypto_NativeBlockCipher_allocParam(JNIEnv* env, /*@unused@*/ jclass dummy, jlong ciph)
{
	void *param = malloc(((const blockCipher*) ciph)->paramsize);
	if (param == (void*) 0)
	{
		jclass ex = (*env)->FindClass(env, JAVA_OUT_OF_MEMORY_ERROR);
		if (ex)
			(void) (*env)->ThrowNew(env, ex, MSG_OUT_OF_MEMORY);
	}
	return (jlong) param;
}

void JNICALL Java_beecrypt_crypto_NativeBlockCipher_freeParam(/*@unused@*/ JNIEnv* env, /*@unused@*/ jclass dummy, jlong param)
{
	if (param)
		free((void*) param);
}

jint JNICALL Java_beecrypt_crypto_NativeBlockCipher_getBlockSize(/*@unused@*/ JNIEnv* env, /*@unused@*/ jclass dummy, jlong ciph)
{
	return ((const blockCipher*) ciph)->blocksize;
}

void JNICALL Java_beecrypt_crypto_NativeBlockCipher_setup(JNIEnv* env, /*@unused@*/ jclass dummy, jlong ciph, jlong param, jint mode, jbyteArray keyArray)
{
	/* BeeCrypt takes key in 32 bit words with size in bits */
	jsize keysize = (*env)->GetArrayLength(env, keyArray);

	if (keysize)
	{
		int rc;
		cipherOperation nativeop;
		jbyte* key;

		switch (mode)
		{
		case javax_crypto_Cipher_ENCRYPT_MODE:
			nativeop = ENCRYPT;
			break;
		case javax_crypto_Cipher_DECRYPT_MODE:
			nativeop = DECRYPT;
			break;
		}
		
		key = (*env)->GetByteArrayElements(env, keyArray, (jboolean*) 0);
		if (key == (jbyte*) 0)
		{
			jclass ex = (*env)->FindClass(env, JAVA_OUT_OF_MEMORY_ERROR);
			if (ex)
				(void) (*env)->ThrowNew(env, ex, MSG_OUT_OF_MEMORY);
			return;
		}

		if (!WORDS_BIGENDIAN || ((int) key & 0x3) || (keysize & 0x3))
		{	/* unaligned */
			int size = (keysize + 3) >> 2;
			uint32* data = (uint32*) malloc(size * sizeof(uint32));

			if (data == (uint32*) 0)
			{
				jclass ex = (*env)->FindClass(env, JAVA_OUT_OF_MEMORY_ERROR);
				(*env)->ReleaseByteArrayElements(env, keyArray, key, JNI_ABORT);
				if (ex)
					(void) (*env)->ThrowNew(env, ex, MSG_OUT_OF_MEMORY);
				return;
			}

			(void) decodeIntsPartial(data, key, keysize);
			rc = ((const blockCipher*) ciph)->setup((blockCipherParam*) param, data, keysize << 3, nativeop);
			free(data);
		}
		else
		{	/* aligned and properly sized */
			rc = ((const blockCipher*) ciph)->setup((blockCipherParam*) param, (const uint32*) key, keysize << 3, nativeop);
		}

		if (rc != 0)
		{
			jclass ex = (*env)->FindClass(env, JAVA_INVALID_KEY_EXCEPTION);
			if (ex)
				(void) (*env)->ThrowNew(env, ex, MSG_INVALID_KEY);
		}

		(*env)->ReleaseByteArrayElements(env, keyArray, key, JNI_ABORT);
	}
}

void JNICALL Java_beecrypt_crypto_NativeBlockCipher_setIV(JNIEnv* env, /*@unused@*/ jclass dummy, jlong ciph, jlong param, jbyteArray ivArray)
{
	if (ivArray == (jbyteArray) 0)
	{
		(void) ((const blockCipher*) ciph)->setiv((blockCipherParam*) param, 0);
	}
	else
	{
		jsize ivsize = (*env)->GetArrayLength(env, ivArray);

		if (ivsize > 0)
		{
			jbyte* iv = (*env)->GetByteArrayElements(env, ivArray, (jboolean*) 0);
		
			if (iv == (jbyte*) 0)
			{
				jclass ex = (*env)->FindClass(env, JAVA_OUT_OF_MEMORY_ERROR);
				if (ex)
					(void) (*env)->ThrowNew(env, ex, MSG_OUT_OF_MEMORY);
				return;
			}
		
			if (!WORDS_BIGENDIAN || ((int) iv & 0x3) || (ivsize & 0x3))
			{	/* unaligned */
				int size = (ivsize + 3) >> 2;
				uint32* data = (uint32*) malloc(size * sizeof(uint32));

				if (data == (uint32*) 0)
				{
					jclass ex = (*env)->FindClass(env, JAVA_OUT_OF_MEMORY_ERROR);
					(*env)->ReleaseByteArrayElements(env, ivArray, iv, JNI_ABORT);
					if (ex)
						(void) (*env)->ThrowNew(env, ex, MSG_OUT_OF_MEMORY);
					return;
				}

				(void) decodeIntsPartial(data, iv, ivsize);
				(void) ((const blockCipher*) ciph)->setiv((blockCipherParam*) param, data);
				free(data);
			}
			else
			{	/* aligned */
				(void) ((const blockCipher*) ciph)->setiv((blockCipherParam*) param, (uint32*) iv);
			}
			(*env)->ReleaseByteArrayElements(env, ivArray, iv, JNI_ABORT);
		}
	}
}

void JNICALL Java_beecrypt_crypto_NativeBlockCipher_encryptECB(JNIEnv* env, /*@unused@*/ jclass dummy, jlong ciph, jlong param, jbyteArray inputArray, jint inputOffset, jbyteArray outputArray, jint outputOffset, jint blocks)
{
	jbyte* input;
	jbyte* output;

	input = (*env)->GetByteArrayElements(env, inputArray, (jboolean*) 0);
	if (input == (jbyte*) 0)
	{
		jclass ex = (*env)->FindClass(env, JAVA_OUT_OF_MEMORY_ERROR);
		if (ex)
			(void) (*env)->ThrowNew(env, ex, MSG_OUT_OF_MEMORY);
		return;
	}
	output = (*env)->GetByteArrayElements(env, outputArray, (jboolean*) 0);
	if (input == (jbyte*) 0)
	{
		jclass ex = (*env)->FindClass(env, JAVA_OUT_OF_MEMORY_ERROR);
		(*env)->ReleaseByteArrayElements(env, inputArray, input, JNI_ABORT);
		if (ex)
			(void) (*env)->ThrowNew(env, ex, MSG_OUT_OF_MEMORY);
		return;
	}

	if (((long) (input+inputOffset) & 0x3) || ((long) (output+outputOffset) & 0x3))
	{	/* unaligned */
		uint32* datain;
		uint32* dataout;

		datain = (uint32*) malloc(blocks * sizeof(uint32));
		if (datain == (uint32*) 0)
		{
			jclass ex = (*env)->FindClass(env, JAVA_OUT_OF_MEMORY_ERROR);
			(*env)->ReleaseByteArrayElements(env, inputArray, input, JNI_ABORT);
			(*env)->ReleaseByteArrayElements(env, outputArray, output, JNI_ABORT);
			if (ex)
				(void) (*env)->ThrowNew(env, ex, MSG_OUT_OF_MEMORY);
			return;
		}
		dataout = (uint32*) malloc(blocks * sizeof(uint32));
		if (dataout == (uint32*) 0)
		{
			jclass ex = (*env)->FindClass(env, JAVA_OUT_OF_MEMORY_ERROR);
			free(datain);
			(*env)->ReleaseByteArrayElements(env, inputArray, input, JNI_ABORT);
			(*env)->ReleaseByteArrayElements(env, outputArray, output, JNI_ABORT);
			if (ex)
				(void) (*env)->ThrowNew(env, ex, MSG_OUT_OF_MEMORY);
			return;
		}
		memcpy(datain, input+inputOffset, blocks * sizeof(uint32));
		(void) blockEncrypt((const blockCipher*) ciph, (blockCipherParam*) param, ECB, blocks, dataout, datain);
		memcpy(output+outputOffset, dataout, blocks * sizeof(uint32));
	}
	else
	{	/* aligned */
		(void) blockEncrypt((const blockCipher*) ciph, (blockCipherParam*) param, ECB, blocks, (uint32*)(output+outputOffset), (uint32*) (input+inputOffset));
	}

	(*env)->ReleaseByteArrayElements(env, inputArray, input, JNI_ABORT);
	(*env)->ReleaseByteArrayElements(env, outputArray, output, 0);
}

void JNICALL Java_beecrypt_crypto_NativeBlockCipher_decryptECB(JNIEnv* env, /*@unused@*/ jclass dummy, jlong ciph, jlong param, jbyteArray inputArray, jint inputOffset, jbyteArray outputArray, jint outputOffset, jint blocks)
{
	jbyte* input;
	jbyte* output;

	input = (*env)->GetByteArrayElements(env, inputArray, (jboolean*) 0);
	if (input == (jbyte*) 0)
	{
		jclass ex = (*env)->FindClass(env, JAVA_OUT_OF_MEMORY_ERROR);
		if (ex)
			(void) (*env)->ThrowNew(env, ex, MSG_OUT_OF_MEMORY);
		return;
	}
	output = (*env)->GetByteArrayElements(env, outputArray, (jboolean*) 0);
	if (input == (jbyte*) 0)
	{
		jclass ex = (*env)->FindClass(env, JAVA_OUT_OF_MEMORY_ERROR);
		(*env)->ReleaseByteArrayElements(env, inputArray, input, JNI_ABORT);
		if (ex)
			(void) (*env)->ThrowNew(env, ex, MSG_OUT_OF_MEMORY);
		return;
	}

	if (((long) (input+inputOffset) & 0x3) || ((long) (output+outputOffset) & 0x3))
	{	/* unaligned */
		uint32* datain;
		uint32* dataout;

		datain = (uint32*) malloc(blocks * sizeof(uint32));
		if (datain == (uint32*) 0)
		{
			jclass ex = (*env)->FindClass(env, JAVA_OUT_OF_MEMORY_ERROR);
			(*env)->ReleaseByteArrayElements(env, inputArray, input, JNI_ABORT);
			(*env)->ReleaseByteArrayElements(env, outputArray, output, JNI_ABORT);
			if (ex)
				(void) (*env)->ThrowNew(env, ex, MSG_OUT_OF_MEMORY);
			return;
		}
		dataout = (uint32*) malloc(blocks * sizeof(uint32));
		if (dataout == (uint32*) 0)
		{
			jclass ex = (*env)->FindClass(env, JAVA_OUT_OF_MEMORY_ERROR);
			free(datain);
			(*env)->ReleaseByteArrayElements(env, inputArray, input, JNI_ABORT);
			(*env)->ReleaseByteArrayElements(env, outputArray, output, JNI_ABORT);
			if (ex)
				(void) (*env)->ThrowNew(env, ex, MSG_OUT_OF_MEMORY);
			return;
		}
		memcpy(datain, input+inputOffset, blocks * sizeof(uint32));
		(void) blockDecrypt((const blockCipher*) ciph, (blockCipherParam*) param, ECB, blocks, dataout, datain);
		memcpy(output+outputOffset, dataout, blocks * sizeof(uint32));
	}
	else
	{	/* aligned */
		(void) blockDecrypt((const blockCipher*) ciph, (blockCipherParam*) param, ECB, blocks, (uint32*)(output+outputOffset), (uint32*) (input+inputOffset));
	}

	(*env)->ReleaseByteArrayElements(env, inputArray, input, JNI_ABORT);
	(*env)->ReleaseByteArrayElements(env, outputArray, output, 0);
}

void JNICALL Java_beecrypt_crypto_NativeBlockCipher_encryptCBC(JNIEnv* env, /*@unused@*/ jclass dummy, jlong ciph, jlong param, jbyteArray inputArray, jint inputOffset, jbyteArray outputArray, jint outputOffset, jint blocks)
{
	jbyte* input;
	jbyte* output;

	input = (*env)->GetByteArrayElements(env, inputArray, (jboolean*) 0);
	if (input == (jbyte*) 0)
	{
		jclass ex = (*env)->FindClass(env, JAVA_OUT_OF_MEMORY_ERROR);
		if (ex)
			(void) (*env)->ThrowNew(env, ex, MSG_OUT_OF_MEMORY);
		return;
	}
	output = (*env)->GetByteArrayElements(env, outputArray, (jboolean*) 0);
	if (input == (jbyte*) 0)
	{
		jclass ex = (*env)->FindClass(env, JAVA_OUT_OF_MEMORY_ERROR);
		(*env)->ReleaseByteArrayElements(env, inputArray, input, JNI_ABORT);
		if (ex)
			(void) (*env)->ThrowNew(env, ex, MSG_OUT_OF_MEMORY);
		return;
	}

	if (((long) (input+inputOffset) & 0x3) || ((long) (output+outputOffset) & 0x3))
	{	/* unaligned */
		uint32* datain;
		uint32* dataout;

		datain = (uint32*) malloc(blocks * sizeof(uint32));
		if (datain == (uint32*) 0)
		{
			jclass ex = (*env)->FindClass(env, JAVA_OUT_OF_MEMORY_ERROR);
			(*env)->ReleaseByteArrayElements(env, inputArray, input, JNI_ABORT);
			(*env)->ReleaseByteArrayElements(env, outputArray, output, JNI_ABORT);
			if (ex)
				(void) (*env)->ThrowNew(env, ex, MSG_OUT_OF_MEMORY);
			return;
		}
		dataout = (uint32*) malloc(blocks * sizeof(uint32));
		if (dataout == (uint32*) 0)
		{
			jclass ex = (*env)->FindClass(env, JAVA_OUT_OF_MEMORY_ERROR);
			free(datain);
			(*env)->ReleaseByteArrayElements(env, inputArray, input, JNI_ABORT);
			(*env)->ReleaseByteArrayElements(env, outputArray, output, JNI_ABORT);
			if (ex)
				(void) (*env)->ThrowNew(env, ex, MSG_OUT_OF_MEMORY);
			return;
		}
		memcpy(datain, input+inputOffset, blocks * sizeof(uint32));
		(void) blockEncrypt((const blockCipher*) ciph, (blockCipherParam*) param, CBC, blocks, dataout, datain);
		memcpy(output+outputOffset, dataout, blocks * sizeof(uint32));
	}
	else
	{	/* aligned */
		(void) blockEncrypt((const blockCipher*) ciph, (blockCipherParam*) param, CBC, blocks, (uint32*)(output+outputOffset), (uint32*) (input+inputOffset));
	}

	(*env)->ReleaseByteArrayElements(env, inputArray, input, JNI_ABORT);
	(*env)->ReleaseByteArrayElements(env, outputArray, output, 0);
}

void JNICALL Java_beecrypt_crypto_NativeBlockCipher_decryptCBC(JNIEnv* env, /*@unused@*/ jclass dummy, jlong ciph, jlong param, jbyteArray inputArray, jint inputOffset, jbyteArray outputArray, jint outputOffset, jint blocks)
{
	jbyte* input;
	jbyte* output;

	input = (*env)->GetByteArrayElements(env, inputArray, (jboolean*) 0);
	if (input == (jbyte*) 0)
	{
		jclass ex = (*env)->FindClass(env, JAVA_OUT_OF_MEMORY_ERROR);
		if (ex)
			(void) (void) (*env)->ThrowNew(env, ex, MSG_OUT_OF_MEMORY);
		return;
	}
	output = (*env)->GetByteArrayElements(env, outputArray, (jboolean*) 0);
	if (input == (jbyte*) 0)
	{
		jclass ex = (*env)->FindClass(env, JAVA_OUT_OF_MEMORY_ERROR);
		(*env)->ReleaseByteArrayElements(env, inputArray, input, JNI_ABORT);
		if (ex)
			(void) (*env)->ThrowNew(env, ex, MSG_OUT_OF_MEMORY);
		return;
	}

	if (((long) (input+inputOffset) & 0x3) || ((long) (output+outputOffset) & 0x3))
	{	/* unaligned */
		uint32* datain;
		uint32* dataout;

		datain = (uint32*) malloc(blocks * sizeof(uint32));
		if (datain == (uint32*) 0)
		{
			jclass ex = (*env)->FindClass(env, JAVA_OUT_OF_MEMORY_ERROR);
			(*env)->ReleaseByteArrayElements(env, inputArray, input, JNI_ABORT);
			(*env)->ReleaseByteArrayElements(env, outputArray, output, JNI_ABORT);
			if (ex)
				(void) (*env)->ThrowNew(env, ex, MSG_OUT_OF_MEMORY);
			return;
		}
		dataout = (uint32*) malloc(blocks * sizeof(uint32));
		if (dataout == (uint32*) 0)
		{
			jclass ex = (*env)->FindClass(env, JAVA_OUT_OF_MEMORY_ERROR);
			free(datain);
			(*env)->ReleaseByteArrayElements(env, inputArray, input, JNI_ABORT);
			(*env)->ReleaseByteArrayElements(env, outputArray, output, JNI_ABORT);
			if (ex)
				(void) (*env)->ThrowNew(env, ex, MSG_OUT_OF_MEMORY);
			return;
		}
		memcpy(datain, input+inputOffset, blocks * sizeof(uint32));
		(void) blockDecrypt((const blockCipher*) ciph, (blockCipherParam*) param, CBC, blocks, dataout, datain);
		memcpy(output+outputOffset, dataout, blocks * sizeof(uint32));
	}
	else
	{	/* aligned */
		(void) blockDecrypt((const blockCipher*) ciph, (blockCipherParam*) param, CBC, blocks, (uint32*)(output+outputOffset), (uint32*) (input+inputOffset));
	}

	(*env)->ReleaseByteArrayElements(env, inputArray, input, JNI_ABORT);
	(*env)->ReleaseByteArrayElements(env, outputArray, output, 0);
}

/*@=mustfree@*/
/*@=noeffectuncon =globs =globnoglobs =modunconnomods =modnomods @*/
/*@=nullpass =nullret =shiftsigned =usedef =temptrans =freshtrans @*/

#endif