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
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
|
/*-
* Copyright (c) 2009 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Alistair Crooks (agc@NetBSD.org)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 2005-2008 Nominet UK (www.nic.uk)
* All rights reserved.
* Contributors: Ben Laurie, Rachel Willmer. The Contributors have asserted
* their moral rights under the UK Copyright Design and Patents Act 1988 to
* be recorded as the authors of this copyright work.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License.
*
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "config.h"
#ifdef HAVE_SYS_CDEFS_H
#include <sys/cdefs.h>
#endif
#if defined(__NetBSD__)
__COPYRIGHT("@(#) Copyright (c) 2009 The NetBSD Foundation, Inc. All rights reserved.");
__RCSID("$NetBSD: symmetric.c,v 1.18 2010/11/07 08:39:59 agc Exp $");
#endif
#include "crypto.h"
#include "packet-show.h"
#include <string.h>
#ifdef HAVE_OPENSSL_CAST_H
#include <openssl/cast.h>
#endif
#ifdef HAVE_OPENSSL_IDEA_H
#include <openssl/idea.h>
#endif
#ifdef HAVE_OPENSSL_AES_H
#include <openssl/aes.h>
#endif
#ifdef HAVE_OPENSSL_DES_H
#include <openssl/des.h>
#endif
#ifdef HAVE_OPENSSL_CAMELLIA_H
#include <openssl/camellia.h>
#endif
#include "crypto.h"
#include "netpgpdefs.h"
static void
std_set_iv(pgp_crypt_t *crypt, const uint8_t *iv)
{
(void) memcpy(crypt->iv, iv, crypt->blocksize);
crypt->num = 0;
}
static void
std_set_key(pgp_crypt_t *crypt, const uint8_t *key)
{
(void) memcpy(crypt->key, key, crypt->keysize);
}
static void
std_resync(pgp_crypt_t *decrypt)
{
if ((size_t) decrypt->num == decrypt->blocksize) {
return;
}
memmove(decrypt->civ + decrypt->blocksize - decrypt->num, decrypt->civ,
(unsigned)decrypt->num);
(void) memcpy(decrypt->civ, decrypt->siv + decrypt->num,
decrypt->blocksize - decrypt->num);
decrypt->num = 0;
}
static void
std_finish(pgp_crypt_t *crypt)
{
if (crypt->encrypt_key) {
free(crypt->encrypt_key);
crypt->encrypt_key = NULL;
}
if (crypt->decrypt_key) {
free(crypt->decrypt_key);
crypt->decrypt_key = NULL;
}
}
static int
cast5_init(pgp_crypt_t *crypt)
{
if (crypt->encrypt_key) {
free(crypt->encrypt_key);
}
if ((crypt->encrypt_key = calloc(1, sizeof(CAST_KEY))) == NULL) {
(void) fprintf(stderr, "cast5_init: alloc failure\n");
return 0;
}
CAST_set_key(crypt->encrypt_key, (int)crypt->keysize, crypt->key);
if ((crypt->decrypt_key = calloc(1, sizeof(CAST_KEY))) == NULL) {
(void) fprintf(stderr, "cast5_init: alloc failure\n");
return 0;
}
CAST_set_key(crypt->decrypt_key, (int)crypt->keysize, crypt->key);
return 1;
}
static void
cast5_block_encrypt(pgp_crypt_t *crypt, void *out, const void *in)
{
CAST_ecb_encrypt(in, out, crypt->encrypt_key, CAST_ENCRYPT);
}
static void
cast5_block_decrypt(pgp_crypt_t *crypt, void *out, const void *in)
{
CAST_ecb_encrypt(in, out, crypt->encrypt_key, CAST_DECRYPT);
}
static void
cast5_cfb_encrypt(pgp_crypt_t *crypt, void *out, const void *in, size_t count)
{
CAST_cfb64_encrypt(in, out, (long)count,
crypt->encrypt_key, crypt->iv, &crypt->num,
CAST_ENCRYPT);
}
static void
cast5_cfb_decrypt(pgp_crypt_t *crypt, void *out, const void *in, size_t count)
{
CAST_cfb64_encrypt(in, out, (long)count,
crypt->encrypt_key, crypt->iv, &crypt->num,
CAST_DECRYPT);
}
#define TRAILER "","","","",0,NULL,NULL
static pgp_crypt_t cast5 =
{
PGP_SA_CAST5,
CAST_BLOCK,
CAST_KEY_LENGTH,
std_set_iv,
std_set_key,
cast5_init,
std_resync,
cast5_block_encrypt,
cast5_block_decrypt,
cast5_cfb_encrypt,
cast5_cfb_decrypt,
std_finish,
TRAILER
};
#ifndef OPENSSL_NO_IDEA
static int
idea_init(pgp_crypt_t *crypt)
{
if (crypt->keysize != IDEA_KEY_LENGTH) {
(void) fprintf(stderr, "idea_init: keysize wrong\n");
return 0;
}
if (crypt->encrypt_key) {
free(crypt->encrypt_key);
}
if ((crypt->encrypt_key = calloc(1, sizeof(IDEA_KEY_SCHEDULE))) == NULL) {
(void) fprintf(stderr, "idea_init: alloc failure\n");
return 0;
}
/* note that we don't invert the key when decrypting for CFB mode */
idea_set_encrypt_key(crypt->key, crypt->encrypt_key);
if (crypt->decrypt_key) {
free(crypt->decrypt_key);
}
if ((crypt->decrypt_key = calloc(1, sizeof(IDEA_KEY_SCHEDULE))) == NULL) {
(void) fprintf(stderr, "idea_init: alloc failure\n");
return 0;
}
idea_set_decrypt_key(crypt->encrypt_key, crypt->decrypt_key);
return 1;
}
static void
idea_block_encrypt(pgp_crypt_t *crypt, void *out, const void *in)
{
idea_ecb_encrypt(in, out, crypt->encrypt_key);
}
static void
idea_block_decrypt(pgp_crypt_t *crypt, void *out, const void *in)
{
idea_ecb_encrypt(in, out, crypt->decrypt_key);
}
static void
idea_cfb_encrypt(pgp_crypt_t *crypt, void *out, const void *in, size_t count)
{
idea_cfb64_encrypt(in, out, (long)count,
crypt->encrypt_key, crypt->iv, &crypt->num,
CAST_ENCRYPT);
}
static void
idea_cfb_decrypt(pgp_crypt_t *crypt, void *out, const void *in, size_t count)
{
idea_cfb64_encrypt(in, out, (long)count,
crypt->decrypt_key, crypt->iv, &crypt->num,
CAST_DECRYPT);
}
static const pgp_crypt_t idea =
{
PGP_SA_IDEA,
IDEA_BLOCK,
IDEA_KEY_LENGTH,
std_set_iv,
std_set_key,
idea_init,
std_resync,
idea_block_encrypt,
idea_block_decrypt,
idea_cfb_encrypt,
idea_cfb_decrypt,
std_finish,
TRAILER
};
#endif /* OPENSSL_NO_IDEA */
/* AES with 128-bit key (AES) */
#define KEYBITS_AES128 128
static int
aes128_init(pgp_crypt_t *crypt)
{
if (crypt->encrypt_key) {
free(crypt->encrypt_key);
}
if ((crypt->encrypt_key = calloc(1, sizeof(AES_KEY))) == NULL) {
(void) fprintf(stderr, "aes128_init: alloc failure\n");
return 0;
}
if (AES_set_encrypt_key(crypt->key, KEYBITS_AES128,
crypt->encrypt_key)) {
fprintf(stderr, "aes128_init: Error setting encrypt_key\n");
}
if (crypt->decrypt_key) {
free(crypt->decrypt_key);
}
if ((crypt->decrypt_key = calloc(1, sizeof(AES_KEY))) == NULL) {
(void) fprintf(stderr, "aes128_init: alloc failure\n");
return 0;
}
if (AES_set_decrypt_key(crypt->key, KEYBITS_AES128,
crypt->decrypt_key)) {
fprintf(stderr, "aes128_init: Error setting decrypt_key\n");
}
return 1;
}
static void
aes_block_encrypt(pgp_crypt_t *crypt, void *out, const void *in)
{
AES_encrypt(in, out, crypt->encrypt_key);
}
static void
aes_block_decrypt(pgp_crypt_t *crypt, void *out, const void *in)
{
AES_decrypt(in, out, crypt->decrypt_key);
}
static void
aes_cfb_encrypt(pgp_crypt_t *crypt, void *out, const void *in, size_t count)
{
AES_cfb128_encrypt(in, out, (unsigned)count,
crypt->encrypt_key, crypt->iv, &crypt->num,
AES_ENCRYPT);
}
static void
aes_cfb_decrypt(pgp_crypt_t *crypt, void *out, const void *in, size_t count)
{
AES_cfb128_encrypt(in, out, (unsigned)count,
crypt->encrypt_key, crypt->iv, &crypt->num,
AES_DECRYPT);
}
static const pgp_crypt_t aes128 =
{
PGP_SA_AES_128,
AES_BLOCK_SIZE,
KEYBITS_AES128 / 8,
std_set_iv,
std_set_key,
aes128_init,
std_resync,
aes_block_encrypt,
aes_block_decrypt,
aes_cfb_encrypt,
aes_cfb_decrypt,
std_finish,
TRAILER
};
/* AES with 256-bit key */
#define KEYBITS_AES256 256
static int
aes256_init(pgp_crypt_t *crypt)
{
if (crypt->encrypt_key) {
free(crypt->encrypt_key);
}
if ((crypt->encrypt_key = calloc(1, sizeof(AES_KEY))) == NULL) {
(void) fprintf(stderr, "aes256_init: alloc failure\n");
return 0;
}
if (AES_set_encrypt_key(crypt->key, KEYBITS_AES256,
crypt->encrypt_key)) {
fprintf(stderr, "aes256_init: Error setting encrypt_key\n");
free(crypt->encrypt_key);
crypt->encrypt_key = NULL;
return 0;
}
if (crypt->decrypt_key) {
free(crypt->decrypt_key);
}
if ((crypt->decrypt_key = calloc(1, sizeof(AES_KEY))) == NULL) {
(void) fprintf(stderr, "aes256_init: alloc failure\n");
free(crypt->encrypt_key);
crypt->encrypt_key = NULL;
return 0;
}
if (AES_set_decrypt_key(crypt->key, KEYBITS_AES256,
crypt->decrypt_key)) {
fprintf(stderr, "aes256_init: Error setting decrypt_key\n");
free(crypt->encrypt_key);
crypt->encrypt_key = NULL;
free(crypt->decrypt_key);
crypt->decrypt_key = NULL;
return 0;
}
return 1;
}
static const pgp_crypt_t aes256 =
{
PGP_SA_AES_256,
AES_BLOCK_SIZE,
KEYBITS_AES256 / 8,
std_set_iv,
std_set_key,
aes256_init,
std_resync,
aes_block_encrypt,
aes_block_decrypt,
aes_cfb_encrypt,
aes_cfb_decrypt,
std_finish,
TRAILER
};
/* Triple DES */
static int
tripledes_init(pgp_crypt_t *crypt)
{
DES_key_schedule *keys;
int n;
if (crypt->encrypt_key) {
free(crypt->encrypt_key);
}
if ((keys = crypt->encrypt_key = calloc(1, 3 * sizeof(DES_key_schedule))) == NULL) {
(void) fprintf(stderr, "tripledes_init: alloc failure\n");
return 0;
}
for (n = 0; n < 3; ++n) {
DES_set_key((DES_cblock *)(void *)(crypt->key + n * 8),
&keys[n]);
}
return 1;
}
static void
tripledes_block_encrypt(pgp_crypt_t *crypt, void *out, const void *in)
{
DES_key_schedule *keys = crypt->encrypt_key;
DES_ecb3_encrypt(__UNCONST(in), out, &keys[0], &keys[1], &keys[2],
DES_ENCRYPT);
}
static void
tripledes_block_decrypt(pgp_crypt_t *crypt, void *out, const void *in)
{
DES_key_schedule *keys = crypt->encrypt_key;
DES_ecb3_encrypt(__UNCONST(in), out, &keys[0], &keys[1], &keys[2],
DES_DECRYPT);
}
static void
tripledes_cfb_encrypt(pgp_crypt_t *crypt, void *out, const void *in,
size_t count)
{
DES_key_schedule *keys = crypt->encrypt_key;
DES_ede3_cfb64_encrypt(in, out, (long)count,
&keys[0], &keys[1], &keys[2], (DES_cblock *)(void *)crypt->iv,
&crypt->num, DES_ENCRYPT);
}
static void
tripledes_cfb_decrypt(pgp_crypt_t *crypt, void *out, const void *in,
size_t count)
{
DES_key_schedule *keys = crypt->encrypt_key;
DES_ede3_cfb64_encrypt(in, out, (long)count,
&keys[0], &keys[1], &keys[2], (DES_cblock *)(void *)crypt->iv,
&crypt->num, DES_DECRYPT);
}
static const pgp_crypt_t tripledes =
{
PGP_SA_TRIPLEDES,
8,
24,
std_set_iv,
std_set_key,
tripledes_init,
std_resync,
tripledes_block_encrypt,
tripledes_block_decrypt,
tripledes_cfb_encrypt,
tripledes_cfb_decrypt,
std_finish,
TRAILER
};
#if defined(HAVE_OPENSSL_CAMELLIA_H) && !defined(OPENSSL_NO_CAMELLIA)
/* Camellia with 128-bit key (CAMELLIA) */
#define KEYBITS_CAMELLIA128 128
static int
camellia128_init(pgp_crypt_t *crypt)
{
if (crypt->encrypt_key) {
free(crypt->encrypt_key);
}
if ((crypt->encrypt_key = calloc(1, sizeof(CAMELLIA_KEY))) == NULL) {
(void) fprintf(stderr, "camellia128_init: alloc failure\n");
return 0;
}
if (Camellia_set_key(crypt->key, KEYBITS_CAMELLIA128, crypt->encrypt_key)) {
fprintf(stderr, "camellia128_init: Error setting encrypt_key\n");
}
if (crypt->decrypt_key) {
free(crypt->decrypt_key);
}
if ((crypt->decrypt_key = calloc(1, sizeof(CAMELLIA_KEY))) == NULL) {
(void) fprintf(stderr, "camellia128_init: alloc failure\n");
return 0;
}
if (Camellia_set_key(crypt->key, KEYBITS_CAMELLIA128, crypt->decrypt_key)) {
fprintf(stderr, "camellia128_init: Error setting decrypt_key\n");
}
return 1;
}
static void
camellia_block_encrypt(pgp_crypt_t *crypt, void *out, const void *in)
{
Camellia_encrypt(in, out, crypt->encrypt_key);
}
static void
camellia_block_decrypt(pgp_crypt_t *crypt, void *out, const void *in)
{
Camellia_decrypt(in, out, crypt->decrypt_key);
}
static void
camellia_cfb_encrypt(pgp_crypt_t *crypt, void *out, const void *in, size_t count)
{
Camellia_cfb128_encrypt(in, out, (unsigned)count,
crypt->encrypt_key, crypt->iv, &crypt->num,
CAMELLIA_ENCRYPT);
}
static void
camellia_cfb_decrypt(pgp_crypt_t *crypt, void *out, const void *in, size_t count)
{
Camellia_cfb128_encrypt(in, out, (unsigned)count,
crypt->encrypt_key, crypt->iv, &crypt->num,
CAMELLIA_DECRYPT);
}
static const pgp_crypt_t camellia128 =
{
PGP_SA_CAMELLIA_128,
CAMELLIA_BLOCK_SIZE,
KEYBITS_CAMELLIA128 / 8,
std_set_iv,
std_set_key,
camellia128_init,
std_resync,
camellia_block_encrypt,
camellia_block_decrypt,
camellia_cfb_encrypt,
camellia_cfb_decrypt,
std_finish,
TRAILER
};
/* Camellia with 256-bit key (CAMELLIA) */
#define KEYBITS_CAMELLIA256 256
static int
camellia256_init(pgp_crypt_t *crypt)
{
if (crypt->encrypt_key) {
free(crypt->encrypt_key);
}
if ((crypt->encrypt_key = calloc(1, sizeof(CAMELLIA_KEY))) == NULL) {
(void) fprintf(stderr, "camellia256_init: alloc failure\n");
return 0;
}
if (Camellia_set_key(crypt->key, KEYBITS_CAMELLIA256, crypt->encrypt_key)) {
fprintf(stderr, "camellia256_init: Error setting encrypt_key\n");
}
if (crypt->decrypt_key) {
free(crypt->decrypt_key);
}
if ((crypt->decrypt_key = calloc(1, sizeof(CAMELLIA_KEY))) == NULL) {
(void) fprintf(stderr, "camellia256_init: alloc failure\n");
return 0;
}
if (Camellia_set_key(crypt->key, KEYBITS_CAMELLIA256, crypt->decrypt_key)) {
fprintf(stderr, "camellia256_init: Error setting decrypt_key\n");
}
return 1;
}
static const pgp_crypt_t camellia256 =
{
PGP_SA_CAMELLIA_256,
CAMELLIA_BLOCK_SIZE,
KEYBITS_CAMELLIA256 / 8,
std_set_iv,
std_set_key,
camellia256_init,
std_resync,
camellia_block_encrypt,
camellia_block_decrypt,
camellia_cfb_encrypt,
camellia_cfb_decrypt,
std_finish,
TRAILER
};
#endif
static const pgp_crypt_t *
get_proto(pgp_symm_alg_t alg)
{
switch (alg) {
case PGP_SA_CAST5:
return &cast5;
#ifndef OPENSSL_NO_IDEA
case PGP_SA_IDEA:
return &idea;
#endif /* OPENSSL_NO_IDEA */
case PGP_SA_AES_128:
return &aes128;
case PGP_SA_AES_256:
return &aes256;
#if defined(HAVE_OPENSSL_CAMELLIA_H) && !defined(OPENSSL_NO_CAMELLIA)
case PGP_SA_CAMELLIA_128:
return &camellia128;
case PGP_SA_CAMELLIA_256:
return &camellia256;
#endif
case PGP_SA_TRIPLEDES:
return &tripledes;
default:
(void) fprintf(stderr, "Unknown algorithm: %d (%s)\n",
alg, pgp_show_symm_alg(alg));
}
return NULL;
}
int
pgp_crypt_any(pgp_crypt_t *crypt, pgp_symm_alg_t alg)
{
const pgp_crypt_t *ptr = get_proto(alg);
if (ptr) {
*crypt = *ptr;
return 1;
} else {
(void) memset(crypt, 0x0, sizeof(*crypt));
return 0;
}
}
unsigned
pgp_block_size(pgp_symm_alg_t alg)
{
const pgp_crypt_t *p = get_proto(alg);
return (p == NULL) ? 0 : (unsigned)p->blocksize;
}
unsigned
pgp_key_size(pgp_symm_alg_t alg)
{
const pgp_crypt_t *p = get_proto(alg);
return (p == NULL) ? 0 : (unsigned)p->keysize;
}
void
pgp_encrypt_init(pgp_crypt_t *encrypt)
{
/* \todo should there be a separate pgp_encrypt_init? */
pgp_decrypt_init(encrypt);
}
void
pgp_decrypt_init(pgp_crypt_t *decrypt)
{
decrypt->base_init(decrypt);
decrypt->block_encrypt(decrypt, decrypt->siv, decrypt->iv);
(void) memcpy(decrypt->civ, decrypt->siv, decrypt->blocksize);
decrypt->num = 0;
}
size_t
pgp_decrypt_se(pgp_crypt_t *decrypt, void *outvoid, const void *invoid,
size_t count)
{
const uint8_t *in = invoid;
uint8_t *out = outvoid;
int saved = (int)count;
/*
* in order to support v3's weird resyncing we have to implement CFB
* mode ourselves
*/
while (count-- > 0) {
uint8_t t;
if ((size_t) decrypt->num == decrypt->blocksize) {
(void) memcpy(decrypt->siv, decrypt->civ,
decrypt->blocksize);
decrypt->block_decrypt(decrypt, decrypt->civ,
decrypt->civ);
decrypt->num = 0;
}
t = decrypt->civ[decrypt->num];
*out++ = t ^ (decrypt->civ[decrypt->num++] = *in++);
}
return (size_t)saved;
}
size_t
pgp_encrypt_se(pgp_crypt_t *encrypt, void *outvoid, const void *invoid,
size_t count)
{
const uint8_t *in = invoid;
uint8_t *out = outvoid;
int saved = (int)count;
/*
* in order to support v3's weird resyncing we have to implement CFB
* mode ourselves
*/
while (count-- > 0) {
if ((size_t) encrypt->num == encrypt->blocksize) {
(void) memcpy(encrypt->siv, encrypt->civ,
encrypt->blocksize);
encrypt->block_encrypt(encrypt, encrypt->civ,
encrypt->civ);
encrypt->num = 0;
}
encrypt->civ[encrypt->num] = *out++ =
encrypt->civ[encrypt->num] ^ *in++;
++encrypt->num;
}
return (size_t)saved;
}
/**
\ingroup HighLevel_Supported
\brief Is this Symmetric Algorithm supported?
\param alg Symmetric Algorithm to check
\return 1 if supported; else 0
*/
unsigned
pgp_is_sa_supported(pgp_symm_alg_t alg)
{
switch (alg) {
case PGP_SA_AES_128:
case PGP_SA_AES_256:
case PGP_SA_CAST5:
case PGP_SA_TRIPLEDES:
#if defined(HAVE_OPENSSL_CAMELLIA_H) && !defined(OPENSSL_NO_CAMELLIA)
case PGP_SA_CAMELLIA_128:
case PGP_SA_CAMELLIA_256:
#endif
#ifndef OPENSSL_NO_IDEA
case PGP_SA_IDEA:
#endif
return 1;
default:
fprintf(stderr, "\nWarning: %s not supported\n",
pgp_show_symm_alg(alg));
return 0;
}
}
size_t
pgp_encrypt_se_ip(pgp_crypt_t *crypt, void *out, const void *in,
size_t count)
{
if (!pgp_is_sa_supported(crypt->alg)) {
return 0;
}
crypt->cfb_encrypt(crypt, out, in, count);
/* \todo test this number was encrypted */
return count;
}
size_t
pgp_decrypt_se_ip(pgp_crypt_t *crypt, void *out, const void *in,
size_t count)
{
if (!pgp_is_sa_supported(crypt->alg)) {
return 0;
}
crypt->cfb_decrypt(crypt, out, in, count);
/* \todo check this number was in fact decrypted */
return count;
}
|