-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpgcrypto.sgml
1285 lines (1171 loc) · 34.6 KB
/
pgcrypto.sgml
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
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!-- doc/src/sgml/pgcrypto.sgml -->
<sect1 id="pgcrypto" xreflabel="pgcrypto">
<title>pgcrypto</title>
<indexterm zone="pgcrypto">
<primary>pgcrypto</primary>
</indexterm>
<indexterm zone="pgcrypto">
<primary>encryption</primary>
<secondary>for specific columns</secondary>
</indexterm>
<para>
The <filename>pgcrypto</> module provides cryptographic functions for
<productname>PostgreSQL</>.
</para>
<sect2>
<title>General Hashing Functions</title>
<sect3>
<title><function>digest()</function></title>
<synopsis>
digest(data text, type text) returns bytea
digest(data bytea, type text) returns bytea
</synopsis>
<para>
Computes a binary hash of the given <parameter>data</>.
<parameter>type</> is the algorithm to use.
Standard algorithms are <literal>md5</literal>, <literal>sha1</literal>,
<literal>sha224</literal>, <literal>sha256</literal>,
<literal>sha384</literal> and <literal>sha512</literal>.
If <filename>pgcrypto</> was built with
OpenSSL, more algorithms are available, as detailed in
<xref linkend="pgcrypto-with-without-openssl">.
</para>
<para>
If you want the digest as a hexadecimal string, use
<function>encode()</> on the result. For example:
<programlisting>
CREATE OR REPLACE FUNCTION sha1(bytea) returns text AS $$
SELECT encode(digest($1, 'sha1'), 'hex')
$$ LANGUAGE SQL STRICT IMMUTABLE;
</programlisting>
</para>
</sect3>
<sect3>
<title><function>hmac()</function></title>
<synopsis>
hmac(data text, key text, type text) returns bytea
hmac(data bytea, key text, type text) returns bytea
</synopsis>
<para>
Calculates hashed MAC for <parameter>data</> with key <parameter>key</>.
<parameter>type</> is the same as in <function>digest()</>.
</para>
<para>
This is similar to <function>digest()</> but the hash can only be
recalculated knowing the key. This prevents the scenario of someone
altering data and also changing the hash to match.
</para>
<para>
If the key is larger than the hash block size it will first be hashed and
the result will be used as key.
</para>
</sect3>
</sect2>
<sect2>
<title>Password Hashing Functions</title>
<para>
The functions <function>crypt()</> and <function>gen_salt()</>
are specifically designed for hashing passwords.
<function>crypt()</> does the hashing and <function>gen_salt()</>
prepares algorithm parameters for it.
</para>
<para>
The algorithms in <function>crypt()</> differ from usual hashing algorithms
like MD5 or SHA1 in the following respects:
</para>
<orderedlist>
<listitem>
<para>
They are slow. As the amount of data is so small, this is the only
way to make brute-forcing passwords hard.
</para>
</listitem>
<listitem>
<para>
They use a random value, called the <firstterm>salt</>, so that users
having the same password will have different encrypted passwords.
This is also an additional defense against reversing the algorithm.
</para>
</listitem>
<listitem>
<para>
They include the algorithm type in the result, so passwords hashed with
different algorithms can co-exist.
</para>
</listitem>
<listitem>
<para>
Some of them are adaptive — that means when computers get
faster, you can tune the algorithm to be slower, without
introducing incompatibility with existing passwords.
</para>
</listitem>
</orderedlist>
<para>
<xref linkend="pgcrypto-crypt-algorithms"> lists the algorithms
supported by the <function>crypt()</function> function.
</para>
<table id="pgcrypto-crypt-algorithms">
<title>Supported Algorithms for <function>crypt()</></title>
<tgroup cols="5">
<thead>
<row>
<entry>Algorithm</entry>
<entry>Max Password Length</entry>
<entry>Adaptive?</entry>
<entry>Salt Bits</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry><literal>bf</></entry>
<entry>72</entry>
<entry>yes</entry>
<entry>128</entry>
<entry>Blowfish-based, variant 2a</entry>
</row>
<row>
<entry><literal>md5</></entry>
<entry>unlimited</entry>
<entry>no</entry>
<entry>48</entry>
<entry>MD5-based crypt</entry>
</row>
<row>
<entry><literal>xdes</></entry>
<entry>8</entry>
<entry>yes</entry>
<entry>24</entry>
<entry>Extended DES</entry>
</row>
<row>
<entry><literal>des</></entry>
<entry>8</entry>
<entry>no</entry>
<entry>12</entry>
<entry>Original UNIX crypt</entry>
</row>
</tbody>
</tgroup>
</table>
<sect3>
<title><function>crypt()</></title>
<synopsis>
crypt(password text, salt text) returns text
</synopsis>
<para>
Calculates a crypt(3)-style hash of <parameter>password</>.
When storing a new password, you need to use
<function>gen_salt()</> to generate a new <parameter>salt</> value.
To check a password, pass the stored hash value as <parameter>salt</>,
and test whether the result matches the stored value.
</para>
<para>
Example of setting a new password:
<programlisting>
UPDATE ... SET pswhash = crypt('new password', gen_salt('md5'));
</programlisting>
</para>
<para>
Example of authentication:
<programlisting>
SELECT pswhash = crypt('entered password', pswhash) FROM ... ;
</programlisting>
This returns <literal>true</> if the entered password is correct.
</para>
</sect3>
<sect3>
<title><function>gen_salt()</></title>
<synopsis>
gen_salt(type text [, iter_count integer ]) returns text
</synopsis>
<para>
Generates a new random salt string for use in <function>crypt()</>.
The salt string also tells <function>crypt()</> which algorithm to use.
</para>
<para>
The <parameter>type</> parameter specifies the hashing algorithm.
The accepted types are: <literal>des</literal>, <literal>xdes</literal>,
<literal>md5</literal> and <literal>bf</literal>.
</para>
<para>
The <parameter>iter_count</> parameter lets the user specify the iteration
count, for algorithms that have one.
The higher the count, the more time it takes to hash
the password and therefore the more time to break it. Although with
too high a count the time to calculate a hash may be several years
— which is somewhat impractical. If the <parameter>iter_count</>
parameter is omitted, the default iteration count is used.
Allowed values for <parameter>iter_count</> depend on the algorithm and
are shown in <xref linkend="pgcrypto-icfc-table">.
</para>
<table id="pgcrypto-icfc-table">
<title>Iteration Counts for <function>crypt()</></title>
<tgroup cols="4">
<thead>
<row>
<entry>Algorithm</entry>
<entry>Default</entry>
<entry>Min</entry>
<entry>Max</entry>
</row>
</thead>
<tbody>
<row>
<entry><literal>xdes</></entry>
<entry>725</entry>
<entry>1</entry>
<entry>16777215</entry>
</row>
<row>
<entry><literal>bf</></entry>
<entry>6</entry>
<entry>4</entry>
<entry>31</entry>
</row>
</tbody>
</tgroup>
</table>
<para>
For <literal>xdes</literal> there is an additional limitation that the
iteration count must be an odd number.
</para>
<para>
To pick an appropriate iteration count, consider that
the original DES crypt was designed to have the speed of 4 hashes per
second on the hardware of that time.
Slower than 4 hashes per second would probably dampen usability.
Faster than 100 hashes per second is probably too fast.
</para>
<para>
<xref linkend="pgcrypto-hash-speed-table"> gives an overview of the relative slowness
of different hashing algorithms.
The table shows how much time it would take to try all
combinations of characters in an 8-character password, assuming
that the password contains either only lower case letters, or
upper- and lower-case letters and numbers.
In the <literal>crypt-bf</literal> entries, the number after a slash is
the <parameter>iter_count</parameter> parameter of
<function>gen_salt</function>.
</para>
<table id="pgcrypto-hash-speed-table">
<title>Hash Algorithm Speeds</title>
<tgroup cols="4">
<thead>
<row>
<entry>Algorithm</entry>
<entry>Hashes/sec</entry>
<entry>For <literal>[a-z]</></entry>
<entry>For <literal>[A-Za-z0-9]</></entry>
</row>
</thead>
<tbody>
<row>
<entry><literal>crypt-bf/8</></entry>
<entry>28</entry>
<entry>246 years</entry>
<entry>251322 years</entry>
</row>
<row>
<entry><literal>crypt-bf/7</></entry>
<entry>57</entry>
<entry>121 years</entry>
<entry>123457 years</entry>
</row>
<row>
<entry><literal>crypt-bf/6</></entry>
<entry>112</entry>
<entry>62 years</entry>
<entry>62831 years</entry>
</row>
<row>
<entry><literal>crypt-bf/5</></entry>
<entry>211</entry>
<entry>33 years</entry>
<entry>33351 years</entry>
</row>
<row>
<entry><literal>crypt-md5</></entry>
<entry>2681</entry>
<entry>2.6 years</entry>
<entry>2625 years</entry>
</row>
<row>
<entry><literal>crypt-des</></entry>
<entry>362837</entry>
<entry>7 days</entry>
<entry>19 years</entry>
</row>
<row>
<entry><literal>sha1</></entry>
<entry>590223</entry>
<entry>4 days</entry>
<entry>12 years</entry>
</row>
<row>
<entry><literal>md5</></entry>
<entry>2345086</entry>
<entry>1 day</entry>
<entry>3 years</entry>
</row>
</tbody>
</tgroup>
</table>
<para>
Notes:
</para>
<itemizedlist>
<listitem>
<para>
The machine used is a 1.5GHz Pentium 4.
</para>
</listitem>
<listitem>
<para>
<literal>crypt-des</> and <literal>crypt-md5</> algorithm numbers are
taken from John the Ripper v1.6.38 <literal>-test</> output.
</para>
</listitem>
<listitem>
<para>
<literal>md5</> numbers are from mdcrack 1.2.
</para>
</listitem>
<listitem>
<para>
<literal>sha1</> numbers are from lcrack-20031130-beta.
</para>
</listitem>
<listitem>
<para>
<literal>crypt-bf</literal> numbers are taken using a simple program that
loops over 1000 8-character passwords. That way I can show the speed
with different numbers of iterations. For reference: <literal>john
-test</literal> shows 213 loops/sec for <literal>crypt-bf/5</>.
(The very small
difference in results is in accordance with the fact that the
<literal>crypt-bf</literal> implementation in <filename>pgcrypto</>
is the same one used in John the Ripper.)
</para>
</listitem>
</itemizedlist>
<para>
Note that <quote>try all combinations</quote> is not a realistic exercise.
Usually password cracking is done with the help of dictionaries, which
contain both regular words and various mutations of them. So, even
somewhat word-like passwords could be cracked much faster than the above
numbers suggest, while a 6-character non-word-like password may escape
cracking. Or not.
</para>
</sect3>
</sect2>
<sect2>
<title>PGP Encryption Functions</title>
<para>
The functions here implement the encryption part of the OpenPGP (RFC 4880)
standard. Supported are both symmetric-key and public-key encryption.
</para>
<para>
An encrypted PGP message consists of 2 parts, or <firstterm>packets</>:
</para>
<itemizedlist>
<listitem>
<para>
Packet containing a session key — either symmetric-key or public-key
encrypted.
</para>
</listitem>
<listitem>
<para>
Packet containing data encrypted with the session key.
</para>
</listitem>
</itemizedlist>
<para>
When encrypting with a symmetric key (i.e., a password):
</para>
<orderedlist>
<listitem>
<para>
The given password is hashed using a String2Key (S2K) algorithm. This is
rather similar to <function>crypt()</> algorithms — purposefully
slow and with random salt — but it produces a full-length binary
key.
</para>
</listitem>
<listitem>
<para>
If a separate session key is requested, a new random key will be
generated. Otherwise the S2K key will be used directly as the session
key.
</para>
</listitem>
<listitem>
<para>
If the S2K key is to be used directly, then only S2K settings will be put
into the session key packet. Otherwise the session key will be encrypted
with the S2K key and put into the session key packet.
</para>
</listitem>
</orderedlist>
<para>
When encrypting with a public key:
</para>
<orderedlist>
<listitem>
<para>
A new random session key is generated.
</para>
</listitem>
<listitem>
<para>
It is encrypted using the public key and put into the session key packet.
</para>
</listitem>
</orderedlist>
<para>
In either case the data to be encrypted is processed as follows:
</para>
<orderedlist>
<listitem>
<para>
Optional data-manipulation: compression, conversion to UTF-8,
and/or conversion of line-endings.
</para>
</listitem>
<listitem>
<para>
The data is prefixed with a block of random bytes. This is equivalent
to using a random IV.
</para>
</listitem>
<listitem>
<para>
An SHA1 hash of the random prefix and data is appended.
</para>
</listitem>
<listitem>
<para>
All this is encrypted with the session key and placed in the data packet.
</para>
</listitem>
</orderedlist>
<sect3>
<title><function>pgp_sym_encrypt()</function></title>
<synopsis>
pgp_sym_encrypt(data text, psw text [, options text ]) returns bytea
pgp_sym_encrypt_bytea(data bytea, psw text [, options text ]) returns bytea
</synopsis>
<para>
Encrypt <parameter>data</> with a symmetric PGP key <parameter>psw</>.
The <parameter>options</> parameter can contain option settings,
as described below.
</para>
</sect3>
<sect3>
<title><function>pgp_sym_decrypt()</function></title>
<synopsis>
pgp_sym_decrypt(msg bytea, psw text [, options text ]) returns text
pgp_sym_decrypt_bytea(msg bytea, psw text [, options text ]) returns bytea
</synopsis>
<para>
Decrypt a symmetric-key-encrypted PGP message.
</para>
<para>
Decrypting <type>bytea</> data with <function>pgp_sym_decrypt</> is disallowed.
This is to avoid outputting invalid character data. Decrypting
originally textual data with <function>pgp_sym_decrypt_bytea</> is fine.
</para>
<para>
The <parameter>options</> parameter can contain option settings,
as described below.
</para>
</sect3>
<sect3>
<title><function>pgp_pub_encrypt()</function></title>
<synopsis>
pgp_pub_encrypt(data text, key bytea [, options text ]) returns bytea
pgp_pub_encrypt_bytea(data bytea, key bytea [, options text ]) returns bytea
</synopsis>
<para>
Encrypt <parameter>data</> with a public PGP key <parameter>key</>.
Giving this function a secret key will produce a error.
</para>
<para>
The <parameter>options</> parameter can contain option settings,
as described below.
</para>
</sect3>
<sect3>
<title><function>pgp_pub_decrypt()</function></title>
<synopsis>
pgp_pub_decrypt(msg bytea, key bytea [, psw text [, options text ]]) returns text
pgp_pub_decrypt_bytea(msg bytea, key bytea [, psw text [, options text ]]) returns bytea
</synopsis>
<para>
Decrypt a public-key-encrypted message. <parameter>key</> must be the
secret key corresponding to the public key that was used to encrypt.
If the secret key is password-protected, you must give the password in
<parameter>psw</>. If there is no password, but you want to specify
options, you need to give an empty password.
</para>
<para>
Decrypting <type>bytea</> data with <function>pgp_pub_decrypt</> is disallowed.
This is to avoid outputting invalid character data. Decrypting
originally textual data with <function>pgp_pub_decrypt_bytea</> is fine.
</para>
<para>
The <parameter>options</> parameter can contain option settings,
as described below.
</para>
</sect3>
<sect3>
<title><function>pgp_key_id()</function></title>
<synopsis>
pgp_key_id(bytea) returns text
</synopsis>
<para>
<function>pgp_key_id</> extracts the key ID of a PGP public or secret key.
Or it gives the key ID that was used for encrypting the data, if given
an encrypted message.
</para>
<para>
It can return 2 special key IDs:
</para>
<itemizedlist>
<listitem>
<para>
<literal>SYMKEY</>
</para>
<para>
The message is encrypted with a symmetric key.
</para>
</listitem>
<listitem>
<para>
<literal>ANYKEY</>
</para>
<para>
The message is public-key encrypted, but the key ID has been removed.
That means you will need to try all your secret keys on it to see
which one decrypts it. <filename>pgcrypto</> itself does not produce
such messages.
</para>
</listitem>
</itemizedlist>
<para>
Note that different keys may have the same ID. This is rare but a normal
event. The client application should then try to decrypt with each one,
to see which fits — like handling <literal>ANYKEY</>.
</para>
</sect3>
<sect3>
<title><function>armor()</function>, <function>dearmor()</function></title>
<synopsis>
armor(data bytea) returns text
dearmor(data text) returns bytea
</synopsis>
<para>
These functions wrap/unwrap binary data into PGP ASCII-armor format,
which is basically Base64 with CRC and additional formatting.
</para>
</sect3>
<sect3>
<title>Options for PGP Functions</title>
<para>
Options are named to be similar to GnuPG. An option's value should be
given after an equal sign; separate options from each other with commas.
For example:
<programlisting>
pgp_sym_encrypt(data, psw, 'compress-algo=1, cipher-algo=aes256')
</programlisting>
</para>
<para>
All of the options except <literal>convert-crlf</literal> apply only to
encrypt functions. Decrypt functions get the parameters from the PGP
data.
</para>
<para>
The most interesting options are probably
<literal>compress-algo</literal> and <literal>unicode-mode</literal>.
The rest should have reasonable defaults.
</para>
<sect4>
<title>cipher-algo</title>
<para>
Which cipher algorithm to use.
</para>
<literallayout>
Values: bf, aes128, aes192, aes256 (OpenSSL-only: <literal>3des</literal>, <literal>cast5</literal>)
Default: aes128
Applies to: pgp_sym_encrypt, pgp_pub_encrypt
</literallayout>
</sect4>
<sect4>
<title>compress-algo</title>
<para>
Which compression algorithm to use. Only available if
<productname>PostgreSQL</productname> was built with zlib.
</para>
<literallayout>
Values:
0 - no compression
1 - ZIP compression
2 - ZLIB compression (= ZIP plus meta-data and block CRCs)
Default: 0
Applies to: pgp_sym_encrypt, pgp_pub_encrypt
</literallayout>
</sect4>
<sect4>
<title>compress-level</title>
<para>
How much to compress. Higher levels compress smaller but are slower.
0 disables compression.
</para>
<literallayout>
Values: 0, 1-9
Default: 6
Applies to: pgp_sym_encrypt, pgp_pub_encrypt
</literallayout>
</sect4>
<sect4>
<title>convert-crlf</title>
<para>
Whether to convert <literal>\n</literal> into <literal>\r\n</literal> when
encrypting and <literal>\r\n</literal> to <literal>\n</literal> when
decrypting. RFC 4880 specifies that text data should be stored using
<literal>\r\n</literal> line-feeds. Use this to get fully RFC-compliant
behavior.
</para>
<literallayout>
Values: 0, 1
Default: 0
Applies to: pgp_sym_encrypt, pgp_pub_encrypt, pgp_sym_decrypt, pgp_pub_decrypt
</literallayout>
</sect4>
<sect4>
<title>disable-mdc</title>
<para>
Do not protect data with SHA-1. The only good reason to use this
option is to achieve compatibility with ancient PGP products, predating
the addition of SHA-1 protected packets to RFC 4880.
Recent gnupg.org and pgp.com software supports it fine.
</para>
<literallayout>
Values: 0, 1
Default: 0
Applies to: pgp_sym_encrypt, pgp_pub_encrypt
</literallayout>
</sect4>
<sect4>
<title>enable-session-key</title>
<para>
Use separate session key. Public-key encryption always uses a separate
session key; this is for symmetric-key encryption, which by default
uses the S2K key directly.
</para>
<literallayout>
Values: 0, 1
Default: 0
Applies to: pgp_sym_encrypt
</literallayout>
</sect4>
<sect4>
<title>s2k-mode</title>
<para>
Which S2K algorithm to use.
</para>
<literallayout>
Values:
0 - Without salt. Dangerous!
1 - With salt but with fixed iteration count.
3 - Variable iteration count.
Default: 3
Applies to: pgp_sym_encrypt
</literallayout>
</sect4>
<sect4>
<title>s2k-digest-algo</title>
<para>
Which digest algorithm to use in S2K calculation.
</para>
<literallayout>
Values: md5, sha1
Default: sha1
Applies to: pgp_sym_encrypt
</literallayout>
</sect4>
<sect4>
<title>s2k-cipher-algo</title>
<para>
Which cipher to use for encrypting separate session key.
</para>
<literallayout>
Values: bf, aes, aes128, aes192, aes256
Default: use cipher-algo
Applies to: pgp_sym_encrypt
</literallayout>
</sect4>
<sect4>
<title>unicode-mode</title>
<para>
Whether to convert textual data from database internal encoding to
UTF-8 and back. If your database already is UTF-8, no conversion will
be done, but the message will be tagged as UTF-8. Without this option
it will not be.
</para>
<literallayout>
Values: 0, 1
Default: 0
Applies to: pgp_sym_encrypt, pgp_pub_encrypt
</literallayout>
</sect4>
</sect3>
<sect3>
<title>Generating PGP Keys with GnuPG</title>
<para>
To generate a new key:
<programlisting>
gpg --gen-key
</programlisting>
</para>
<para>
The preferred key type is <quote>DSA and Elgamal</>.
</para>
<para>
For RSA encryption you must create either DSA or RSA sign-only key
as master and then add an RSA encryption subkey with
<literal>gpg --edit-key</literal>.
</para>
<para>
To list keys:
<programlisting>
gpg --list-secret-keys
</programlisting>
</para>
<para>
To export a public key in ASCII-armor format:
<programlisting>
gpg -a --export KEYID > public.key
</programlisting>
</para>
<para>
To export a secret key in ASCII-armor format:
<programlisting>
gpg -a --export-secret-keys KEYID > secret.key
</programlisting>
</para>
<para>
You need to use <function>dearmor()</> on these keys before giving them to
the PGP functions. Or if you can handle binary data, you can drop
<literal>-a</literal> from the command.
</para>
<para>
For more details see <literal>man gpg</literal>,
<ulink url="http://www.gnupg.org/gph/en/manual.html">The GNU
Privacy Handbook</ulink> and other documentation on
<ulink url="http://www.gnupg.org"></ulink>.
</para>
</sect3>
<sect3>
<title>Limitations of PGP Code</title>
<itemizedlist>
<listitem>
<para>
No support for signing. That also means that it is not checked
whether the encryption subkey belongs to the master key.
</para>
</listitem>
<listitem>
<para>
No support for encryption key as master key. As such practice
is generally discouraged, this should not be a problem.
</para>
</listitem>
<listitem>
<para>
No support for several subkeys. This may seem like a problem, as this
is common practice. On the other hand, you should not use your regular
GPG/PGP keys with <filename>pgcrypto</>, but create new ones,
as the usage scenario is rather different.
</para>
</listitem>
</itemizedlist>
</sect3>
</sect2>
<sect2>
<title>Raw Encryption Functions</title>
<para>
These functions only run a cipher over data; they don't have any advanced
features of PGP encryption. Therefore they have some major problems:
</para>
<orderedlist>
<listitem>
<para>
They use user key directly as cipher key.
</para>
</listitem>
<listitem>
<para>
They don't provide any integrity checking, to see
if the encrypted data was modified.
</para>
</listitem>
<listitem>
<para>
They expect that users manage all encryption parameters
themselves, even IV.
</para>
</listitem>
<listitem>
<para>
They don't handle text.
</para>
</listitem>
</orderedlist>
<para>
So, with the introduction of PGP encryption, usage of raw
encryption functions is discouraged.
</para>
<synopsis>
encrypt(data bytea, key bytea, type text) returns bytea
decrypt(data bytea, key bytea, type text) returns bytea
encrypt_iv(data bytea, key bytea, iv bytea, type text) returns bytea
decrypt_iv(data bytea, key bytea, iv bytea, type text) returns bytea
</synopsis>
<para>
Encrypt/decrypt data using the cipher method specified by
<parameter>type</parameter>. The syntax of the
<parameter>type</parameter> string is:
<synopsis>
<replaceable>algorithm</> <optional> <literal>-</> <replaceable>mode</> </optional> <optional> <literal>/pad:</> <replaceable>padding</> </optional>
</synopsis>
where <replaceable>algorithm</> is one of:
<itemizedlist>
<listitem><para><literal>bf</literal> — Blowfish</para></listitem>
<listitem><para><literal>aes</literal> — AES (Rijndael-128)</para></listitem>
</itemizedlist>
and <replaceable>mode</> is one of:
<itemizedlist>
<listitem>
<para>
<literal>cbc</literal> — next block depends on previous (default)
</para>
</listitem>
<listitem>
<para>
<literal>ecb</literal> — each block is encrypted separately (for
testing only)
</para>
</listitem>
</itemizedlist>
and <replaceable>padding</> is one of:
<itemizedlist>
<listitem>
<para>
<literal>pkcs</literal> — data may be any length (default)
</para>
</listitem>
<listitem>
<para>
<literal>none</literal> — data must be multiple of cipher block size
</para>
</listitem>
</itemizedlist>
</para>
<para>
So, for example, these are equivalent:
<programlisting>
encrypt(data, 'fooz', 'bf')
encrypt(data, 'fooz', 'bf-cbc/pad:pkcs')
</programlisting>
</para>
<para>
In <function>encrypt_iv</> and <function>decrypt_iv</>, the
<parameter>iv</> parameter is the initial value for the CBC mode;
it is ignored for ECB.
It is clipped or padded with zeroes if not exactly block size.
It defaults to all zeroes in the functions without this parameter.
</para>
</sect2>
<sect2>
<title>Random-Data Functions</title>
<synopsis>
gen_random_bytes(count integer) returns bytea
</synopsis>
<para>
Returns <parameter>count</> cryptographically strong random bytes.
At most 1024 bytes can be extracted at a time. This is to avoid
draining the randomness generator pool.
</para>
</sect2>
<sect2>
<title>Notes</title>
<sect3>
<title>Configuration</title>