Skip to content

Commit 1ea9169

Browse files
author
Neil Conway
committed
pgcrypto update:
* openssl.c: Add 3des and AES support * README.pgcrypto: list only supported ciphers for openssl OpenSSL has pre-processor symbol OPENSSL_NO_AES, which isn't that helpful for detecting if it _does_ exist. Thus the hack with AES_ENCRYPT. Marko Kreen
1 parent b160d6b commit 1ea9169

File tree

2 files changed

+235
-4
lines changed

2 files changed

+235
-4
lines changed

contrib/pgcrypto/README.pgcrypto

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,9 @@ internal (default):
178178
Ciphers: Blowfish, Rijndael-128
179179

180180

181-
OpenSSL (0.9.6):
181+
OpenSSL (0.9.7):
182182
Hashes: MD5, SHA1, RIPEMD160, MD2
183-
Ciphers: DES, DESX, DES3, RC5, RC4, RC2, IDEA,
184-
Blowfish, CAST5
183+
Ciphers: Blowfish, AES, CAST5, DES, 3DES
185184
License: BSD-like with strong advertisement
186185
Url: http://www.openssl.org/
187186

contrib/pgcrypto/openssl.c

Lines changed: 233 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2727
* SUCH DAMAGE.
2828
*
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 $
3030
*/
3131

3232
#include <postgres.h>
@@ -35,6 +35,14 @@
3535

3636
#include <openssl/evp.h>
3737

38+
/*
39+
* Is OpenSSL compiled with AES?
40+
*/
41+
#undef GOT_AES
42+
#ifdef AES_ENCRYPT
43+
#define GOT_AES
44+
#endif
45+
3846
/*
3947
* Hashes
4048
*/
@@ -165,7 +173,14 @@ typedef struct
165173
{
166174
des_key_schedule key_schedule;
167175
} des;
176+
struct
177+
{
178+
des_key_schedule k1, k2, k3;
179+
} des3;
168180
CAST_KEY cast_key;
181+
#ifdef GOT_AES
182+
AES_KEY aes_key;
183+
#endif
169184
} u;
170185
uint8 key[EVP_MAX_KEY_LENGTH];
171186
uint8 iv[EVP_MAX_IV_LENGTH];
@@ -362,6 +377,91 @@ ossl_des_cbc_decrypt(PX_Cipher * c, const uint8 *data, unsigned dlen,
362377
return 0;
363378
}
364379

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+
365465
/* CAST5 */
366466

367467
static int
@@ -420,6 +520,103 @@ ossl_cast_cbc_decrypt(PX_Cipher * c, const uint8 *data, unsigned dlen, uint8 *re
420520
return 0;
421521
}
422522

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+
423620
/*
424621
* aliases
425622
*/
@@ -431,7 +628,14 @@ static PX_Alias ossl_aliases[] = {
431628
{"blowfish-ecb", "bf-ecb"},
432629
{"blowfish-cfb", "bf-cfb"},
433630
{"des", "des-cbc"},
631+
{"3des", "des3-cbc"},
632+
{"3des-ecb", "des3-ecb"},
633+
{"3des-cbc", "des3-cbc"},
434634
{"cast5", "cast5-cbc"},
635+
{"aes", "aes-cbc"},
636+
{"rijndael", "aes-cbc"},
637+
{"rijndael-cbc", "aes-cbc"},
638+
{"rijndael-ecb", "aes-ecb"},
435639
{NULL}
436640
};
437641

@@ -460,6 +664,16 @@ static const struct ossl_cipher ossl_des_cbc = {
460664
64 / 8, 64 / 8, 0
461665
};
462666

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+
463677
static const struct ossl_cipher ossl_cast_ecb = {
464678
ossl_cast_init, ossl_cast_ecb_encrypt, ossl_cast_ecb_decrypt,
465679
64 / 8, 128 / 8, 0
@@ -470,6 +684,18 @@ static const struct ossl_cipher ossl_cast_cbc = {
470684
64 / 8, 128 / 8, 0
471685
};
472686

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+
473699
/*
474700
* Special handlers
475701
*/
@@ -485,8 +711,14 @@ static const struct ossl_cipher_lookup ossl_cipher_types[] = {
485711
{"bf-cfb", &ossl_bf_cfb},
486712
{"des-ecb", &ossl_des_ecb},
487713
{"des-cbc", &ossl_des_cbc},
714+
{"des3-ecb", &ossl_des3_ecb},
715+
{"des3-cbc", &ossl_des3_cbc},
488716
{"cast5-ecb", &ossl_cast_ecb},
489717
{"cast5-cbc", &ossl_cast_cbc},
718+
#ifdef GOT_AES
719+
{"aes-ecb", &ossl_aes_ecb},
720+
{"aes-cbc", &ossl_aes_cbc},
721+
#endif
490722
{NULL}
491723
};
492724

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy