26
26
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
27
* SUCH DAMAGE.
28
28
*
29
- * $PostgreSQL: pgsql/contrib/pgcrypto/openssl.c,v 1.16 2005/03/21 05:19:55 neilc Exp $
29
+ * $PostgreSQL: pgsql/contrib/pgcrypto/openssl.c,v 1.17 2005/03/21 05:21:04 neilc Exp $
30
30
*/
31
31
32
32
#include <postgres.h>
35
35
36
36
#include <openssl/evp.h>
37
37
38
+ /*
39
+ * Is OpenSSL compiled with AES?
40
+ */
41
+ #undef GOT_AES
42
+ #ifdef AES_ENCRYPT
43
+ #define GOT_AES
44
+ #endif
45
+
38
46
/*
39
47
* Hashes
40
48
*/
@@ -165,7 +173,14 @@ typedef struct
165
173
{
166
174
des_key_schedule key_schedule ;
167
175
} des ;
176
+ struct
177
+ {
178
+ des_key_schedule k1 , k2 , k3 ;
179
+ } des3 ;
168
180
CAST_KEY cast_key ;
181
+ #ifdef GOT_AES
182
+ AES_KEY aes_key ;
183
+ #endif
169
184
} u ;
170
185
uint8 key [EVP_MAX_KEY_LENGTH ];
171
186
uint8 iv [EVP_MAX_IV_LENGTH ];
@@ -362,6 +377,91 @@ ossl_des_cbc_decrypt(PX_Cipher * c, const uint8 *data, unsigned dlen,
362
377
return 0 ;
363
378
}
364
379
380
+ /* DES3 */
381
+
382
+ static int
383
+ ossl_des3_init (PX_Cipher * c , const uint8 * key , unsigned klen , const uint8 * iv )
384
+ {
385
+ ossldata * od = c -> ptr ;
386
+ des_cblock xkey1 ,
387
+ xkey2 ,
388
+ xkey3 ;
389
+
390
+ memset (& xkey1 , 0 , sizeof (xkey1 ));
391
+ memset (& xkey2 , 0 , sizeof (xkey2 ));
392
+ memset (& xkey2 , 0 , sizeof (xkey2 ));
393
+ memcpy (& xkey1 , key , klen > 8 ? 8 : klen );
394
+ if (klen > 8 )
395
+ memcpy (& xkey2 , key + 8 , (klen - 8 ) > 8 ? 8 : (klen - 8 ));
396
+ if (klen > 16 )
397
+ memcpy (& xkey3 , key + 16 , (klen - 16 ) > 8 ? 8 : (klen - 16 ));
398
+
399
+ DES_set_key (& xkey1 , & od -> u .des3 .k1 );
400
+ DES_set_key (& xkey2 , & od -> u .des3 .k2 );
401
+ DES_set_key (& xkey3 , & od -> u .des3 .k3 );
402
+ memset (& xkey1 , 0 , sizeof (xkey1 ));
403
+ memset (& xkey2 , 0 , sizeof (xkey2 ));
404
+ memset (& xkey3 , 0 , sizeof (xkey3 ));
405
+
406
+ if (iv )
407
+ memcpy (od -> iv , iv , 8 );
408
+ else
409
+ memset (od -> iv , 0 , 8 );
410
+ return 0 ;
411
+ }
412
+
413
+ static int
414
+ ossl_des3_ecb_encrypt (PX_Cipher * c , const uint8 * data , unsigned dlen ,
415
+ uint8 * res )
416
+ {
417
+ unsigned bs = gen_ossl_block_size (c );
418
+ unsigned i ;
419
+ ossldata * od = c -> ptr ;
420
+
421
+ for (i = 0 ; i < dlen / bs ; i ++ )
422
+ DES_ecb3_encrypt (data + i * bs , res + i * bs ,
423
+ & od -> u .des3 .k1 , & od -> u .des3 .k2 , & od -> u .des3 .k3 , 1 );
424
+ return 0 ;
425
+ }
426
+
427
+ static int
428
+ ossl_des3_ecb_decrypt (PX_Cipher * c , const uint8 * data , unsigned dlen ,
429
+ uint8 * res )
430
+ {
431
+ unsigned bs = gen_ossl_block_size (c );
432
+ unsigned i ;
433
+ ossldata * od = c -> ptr ;
434
+
435
+ for (i = 0 ; i < dlen / bs ; i ++ )
436
+ DES_ecb3_encrypt (data + i * bs , res + i * bs ,
437
+ & od -> u .des3 .k1 , & od -> u .des3 .k2 , & od -> u .des3 .k3 , 0 );
438
+ return 0 ;
439
+ }
440
+
441
+ static int
442
+ ossl_des3_cbc_encrypt (PX_Cipher * c , const uint8 * data , unsigned dlen ,
443
+ uint8 * res )
444
+ {
445
+ ossldata * od = c -> ptr ;
446
+
447
+ DES_ede3_cbc_encrypt (data , res , dlen ,
448
+ & od -> u .des3 .k1 , & od -> u .des3 .k2 , & od -> u .des3 .k3 ,
449
+ (des_cblock * ) od -> iv , 1 );
450
+ return 0 ;
451
+ }
452
+
453
+ static int
454
+ ossl_des3_cbc_decrypt (PX_Cipher * c , const uint8 * data , unsigned dlen ,
455
+ uint8 * res )
456
+ {
457
+ ossldata * od = c -> ptr ;
458
+
459
+ DES_ede3_cbc_encrypt (data , res , dlen ,
460
+ & od -> u .des3 .k1 , & od -> u .des3 .k2 , & od -> u .des3 .k3 ,
461
+ (des_cblock * ) od -> iv , 0 );
462
+ return 0 ;
463
+ }
464
+
365
465
/* CAST5 */
366
466
367
467
static int
@@ -420,6 +520,103 @@ ossl_cast_cbc_decrypt(PX_Cipher * c, const uint8 *data, unsigned dlen, uint8 *re
420
520
return 0 ;
421
521
}
422
522
523
+ /* AES */
524
+
525
+ #ifdef GOT_AES
526
+
527
+ static int
528
+ ossl_aes_init (PX_Cipher * c , const uint8 * key , unsigned klen , const uint8 * iv )
529
+ {
530
+ ossldata * od = c -> ptr ;
531
+ unsigned bs = gen_ossl_block_size (c );
532
+
533
+ if (klen <= 128 /8 )
534
+ od -> klen = 128 /8 ;
535
+ else if (klen <= 192 /8 )
536
+ od -> klen = 192 /8 ;
537
+ else if (klen <= 256 /8 )
538
+ od -> klen = 256 /8 ;
539
+ else
540
+ return PXE_KEY_TOO_BIG ;
541
+
542
+ memcpy (od -> key , key , klen );
543
+
544
+ if (iv )
545
+ memcpy (od -> iv , iv , bs );
546
+ else
547
+ memset (od -> iv , 0 , bs );
548
+ return 0 ;
549
+ }
550
+
551
+ static void
552
+ ossl_aes_key_init (ossldata * od , int type )
553
+ {
554
+ if (type == AES_ENCRYPT )
555
+ AES_set_encrypt_key (od -> key , od -> klen * 8 , & od -> u .aes_key );
556
+ else
557
+ AES_set_decrypt_key (od -> key , od -> klen * 8 , & od -> u .aes_key );
558
+ od -> init = 1 ;
559
+ }
560
+
561
+ static int
562
+ ossl_aes_ecb_encrypt (PX_Cipher * c , const uint8 * data , unsigned dlen ,
563
+ uint8 * res )
564
+ {
565
+ unsigned bs = gen_ossl_block_size (c );
566
+ ossldata * od = c -> ptr ;
567
+ const uint8 * end = data + dlen - bs ;
568
+
569
+ if (!od -> init )
570
+ ossl_aes_key_init (od , AES_ENCRYPT );
571
+
572
+ for (; data <= end ; data += bs , res += bs )
573
+ AES_ecb_encrypt (data , res , & od -> u .aes_key , AES_ENCRYPT );
574
+ return 0 ;
575
+ }
576
+
577
+ static int
578
+ ossl_aes_ecb_decrypt (PX_Cipher * c , const uint8 * data , unsigned dlen ,
579
+ uint8 * res )
580
+ {
581
+ unsigned bs = gen_ossl_block_size (c );
582
+ ossldata * od = c -> ptr ;
583
+ const uint8 * end = data + dlen - bs ;
584
+
585
+ if (!od -> init )
586
+ ossl_aes_key_init (od , AES_DECRYPT );
587
+
588
+ for (; data <= end ; data += bs , res += bs )
589
+ AES_ecb_encrypt (data , res , & od -> u .aes_key , AES_DECRYPT );
590
+ return 0 ;
591
+ }
592
+
593
+ static int
594
+ ossl_aes_cbc_encrypt (PX_Cipher * c , const uint8 * data , unsigned dlen ,
595
+ uint8 * res )
596
+ {
597
+ ossldata * od = c -> ptr ;
598
+
599
+ if (!od -> init )
600
+ ossl_aes_key_init (od , AES_ENCRYPT );
601
+
602
+ AES_cbc_encrypt (data , res , dlen , & od -> u .aes_key , od -> iv , AES_ENCRYPT );
603
+ return 0 ;
604
+ }
605
+
606
+ static int
607
+ ossl_aes_cbc_decrypt (PX_Cipher * c , const uint8 * data , unsigned dlen ,
608
+ uint8 * res )
609
+ {
610
+ ossldata * od = c -> ptr ;
611
+
612
+ if (!od -> init )
613
+ ossl_aes_key_init (od , AES_DECRYPT );
614
+
615
+ AES_cbc_encrypt (data , res , dlen , & od -> u .aes_key , od -> iv , AES_DECRYPT );
616
+ return 0 ;
617
+ }
618
+ #endif
619
+
423
620
/*
424
621
* aliases
425
622
*/
@@ -431,7 +628,14 @@ static PX_Alias ossl_aliases[] = {
431
628
{"blowfish-ecb" , "bf-ecb" },
432
629
{"blowfish-cfb" , "bf-cfb" },
433
630
{"des" , "des-cbc" },
631
+ {"3des" , "des3-cbc" },
632
+ {"3des-ecb" , "des3-ecb" },
633
+ {"3des-cbc" , "des3-cbc" },
434
634
{"cast5" , "cast5-cbc" },
635
+ {"aes" , "aes-cbc" },
636
+ {"rijndael" , "aes-cbc" },
637
+ {"rijndael-cbc" , "aes-cbc" },
638
+ {"rijndael-ecb" , "aes-ecb" },
435
639
{NULL }
436
640
};
437
641
@@ -460,6 +664,16 @@ static const struct ossl_cipher ossl_des_cbc = {
460
664
64 / 8 , 64 / 8 , 0
461
665
};
462
666
667
+ static const struct ossl_cipher ossl_des3_ecb = {
668
+ ossl_des3_init , ossl_des3_ecb_encrypt , ossl_des3_ecb_decrypt ,
669
+ 64 / 8 , 192 / 8 , 0
670
+ };
671
+
672
+ static const struct ossl_cipher ossl_des3_cbc = {
673
+ ossl_des3_init , ossl_des3_cbc_encrypt , ossl_des3_cbc_decrypt ,
674
+ 64 / 8 , 192 / 8 , 0
675
+ };
676
+
463
677
static const struct ossl_cipher ossl_cast_ecb = {
464
678
ossl_cast_init , ossl_cast_ecb_encrypt , ossl_cast_ecb_decrypt ,
465
679
64 / 8 , 128 / 8 , 0
@@ -470,6 +684,18 @@ static const struct ossl_cipher ossl_cast_cbc = {
470
684
64 / 8 , 128 / 8 , 0
471
685
};
472
686
687
+ #ifdef GOT_AES
688
+ static const struct ossl_cipher ossl_aes_ecb = {
689
+ ossl_aes_init , ossl_aes_ecb_encrypt , ossl_aes_ecb_decrypt ,
690
+ 128 / 8 , 256 / 8 , 0
691
+ };
692
+
693
+ static const struct ossl_cipher ossl_aes_cbc = {
694
+ ossl_aes_init , ossl_aes_cbc_encrypt , ossl_aes_cbc_decrypt ,
695
+ 128 / 8 , 256 / 8 , 0
696
+ };
697
+ #endif
698
+
473
699
/*
474
700
* Special handlers
475
701
*/
@@ -485,8 +711,14 @@ static const struct ossl_cipher_lookup ossl_cipher_types[] = {
485
711
{"bf-cfb" , & ossl_bf_cfb },
486
712
{"des-ecb" , & ossl_des_ecb },
487
713
{"des-cbc" , & ossl_des_cbc },
714
+ {"des3-ecb" , & ossl_des3_ecb },
715
+ {"des3-cbc" , & ossl_des3_cbc },
488
716
{"cast5-ecb" , & ossl_cast_ecb },
489
717
{"cast5-cbc" , & ossl_cast_cbc },
718
+ #ifdef GOT_AES
719
+ {"aes-ecb" , & ossl_aes_ecb },
720
+ {"aes-cbc" , & ossl_aes_cbc },
721
+ #endif
490
722
{NULL }
491
723
};
492
724
0 commit comments