Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1ea9169

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

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+
#ifdefAES_ENCRYPT
43+
#defineGOT_AES
44+
#endif
45+
3846
/*
3947
* Hashes
4048
*/
@@ -165,7 +173,14 @@ typedef struct
165173
{
166174
des_key_schedulekey_schedule;
167175
}des;
176+
struct
177+
{
178+
des_key_schedulek1,k2,k3;
179+
}des3;
168180
CAST_KEYcast_key;
181+
#ifdefGOT_AES
182+
AES_KEYaes_key;
183+
#endif
169184
}u;
170185
uint8key[EVP_MAX_KEY_LENGTH];
171186
uint8iv[EVP_MAX_IV_LENGTH];
@@ -362,6 +377,91 @@ ossl_des_cbc_decrypt(PX_Cipher * c, const uint8 *data, unsigned dlen,
362377
return0;
363378
}
364379

380+
/* DES3 */
381+
382+
staticint
383+
ossl_des3_init(PX_Cipher*c,constuint8*key,unsignedklen,constuint8*iv)
384+
{
385+
ossldata*od=c->ptr;
386+
des_cblockxkey1,
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+
return0;
411+
}
412+
413+
staticint
414+
ossl_des3_ecb_encrypt(PX_Cipher*c,constuint8*data,unsigneddlen,
415+
uint8*res)
416+
{
417+
unsignedbs=gen_ossl_block_size(c);
418+
unsignedi;
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+
return0;
425+
}
426+
427+
staticint
428+
ossl_des3_ecb_decrypt(PX_Cipher*c,constuint8*data,unsigneddlen,
429+
uint8*res)
430+
{
431+
unsignedbs=gen_ossl_block_size(c);
432+
unsignedi;
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+
return0;
439+
}
440+
441+
staticint
442+
ossl_des3_cbc_encrypt(PX_Cipher*c,constuint8*data,unsigneddlen,
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+
return0;
451+
}
452+
453+
staticint
454+
ossl_des3_cbc_decrypt(PX_Cipher*c,constuint8*data,unsigneddlen,
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+
return0;
463+
}
464+
365465
/* CAST5 */
366466

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

523+
/* AES */
524+
525+
#ifdefGOT_AES
526+
527+
staticint
528+
ossl_aes_init(PX_Cipher*c,constuint8*key,unsignedklen,constuint8*iv)
529+
{
530+
ossldata*od=c->ptr;
531+
unsignedbs=gen_ossl_block_size(c);
532+
533+
if (klen <=128/8)
534+
od->klen=128/8;
535+
elseif (klen <=192/8)
536+
od->klen=192/8;
537+
elseif (klen <=256/8)
538+
od->klen=256/8;
539+
else
540+
returnPXE_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+
return0;
549+
}
550+
551+
staticvoid
552+
ossl_aes_key_init(ossldata*od,inttype)
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+
staticint
562+
ossl_aes_ecb_encrypt(PX_Cipher*c,constuint8*data,unsigneddlen,
563+
uint8*res)
564+
{
565+
unsignedbs=gen_ossl_block_size(c);
566+
ossldata*od=c->ptr;
567+
constuint8*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+
return0;
575+
}
576+
577+
staticint
578+
ossl_aes_ecb_decrypt(PX_Cipher*c,constuint8*data,unsigneddlen,
579+
uint8*res)
580+
{
581+
unsignedbs=gen_ossl_block_size(c);
582+
ossldata*od=c->ptr;
583+
constuint8*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+
return0;
591+
}
592+
593+
staticint
594+
ossl_aes_cbc_encrypt(PX_Cipher*c,constuint8*data,unsigneddlen,
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+
return0;
604+
}
605+
606+
staticint
607+
ossl_aes_cbc_decrypt(PX_Cipher*c,constuint8*data,unsigneddlen,
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+
return0;
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+
staticconststructossl_cipherossl_des3_ecb= {
668+
ossl_des3_init,ossl_des3_ecb_encrypt,ossl_des3_ecb_decrypt,
669+
64 /8,192 /8,0
670+
};
671+
672+
staticconststructossl_cipherossl_des3_cbc= {
673+
ossl_des3_init,ossl_des3_cbc_encrypt,ossl_des3_cbc_decrypt,
674+
64 /8,192 /8,0
675+
};
676+
463677
staticconststructossl_cipherossl_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+
#ifdefGOT_AES
688+
staticconststructossl_cipherossl_aes_ecb= {
689+
ossl_aes_init,ossl_aes_ecb_encrypt,ossl_aes_ecb_decrypt,
690+
128 /8,256 /8,0
691+
};
692+
693+
staticconststructossl_cipherossl_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+
#ifdefGOT_AES
719+
{"aes-ecb",&ossl_aes_ecb},
720+
{"aes-cbc",&ossl_aes_cbc},
721+
#endif
490722
{NULL}
491723
};
492724

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp