]> granicus.if.org Git - postgresql/blob - doc/src/sgml/pgcrypto.sgml
doc: Use proper em and en dashes
[postgresql] / doc / src / sgml / pgcrypto.sgml
1 <!-- doc/src/sgml/pgcrypto.sgml -->
2
3 <sect1 id="pgcrypto" xreflabel="pgcrypto">
4  <title>pgcrypto</title>
5
6  <indexterm zone="pgcrypto">
7   <primary>pgcrypto</primary>
8  </indexterm>
9
10  <indexterm zone="pgcrypto">
11   <primary>encryption</primary>
12   <secondary>for specific columns</secondary>
13  </indexterm>
14
15  <para>
16   The <filename>pgcrypto</filename> module provides cryptographic functions for
17   <productname>PostgreSQL</productname>.
18  </para>
19
20  <sect2>
21   <title>General Hashing Functions</title>
22
23   <sect3>
24    <title><function>digest()</function></title>
25
26    <indexterm>
27     <primary>digest</primary>
28    </indexterm>
29
30 <synopsis>
31 digest(data text, type text) returns bytea
32 digest(data bytea, type text) returns bytea
33 </synopsis>
34
35    <para>
36     Computes a binary hash of the given <parameter>data</parameter>.
37     <parameter>type</parameter> is the algorithm to use.
38     Standard algorithms are <literal>md5</literal>, <literal>sha1</literal>,
39     <literal>sha224</literal>, <literal>sha256</literal>,
40     <literal>sha384</literal> and <literal>sha512</literal>.
41     If <filename>pgcrypto</filename> was built with
42     OpenSSL, more algorithms are available, as detailed in
43     <xref linkend="pgcrypto-with-without-openssl"/>.
44    </para>
45
46    <para>
47     If you want the digest as a hexadecimal string, use
48     <function>encode()</function> on the result.  For example:
49 <programlisting>
50 CREATE OR REPLACE FUNCTION sha1(bytea) returns text AS $$
51     SELECT encode(digest($1, 'sha1'), 'hex')
52 $$ LANGUAGE SQL STRICT IMMUTABLE;
53 </programlisting>
54    </para>
55   </sect3>
56
57   <sect3>
58    <title><function>hmac()</function></title>
59
60    <indexterm>
61     <primary>hmac</primary>
62    </indexterm>
63
64 <synopsis>
65 hmac(data text, key text, type text) returns bytea
66 hmac(data bytea, key bytea, type text) returns bytea
67 </synopsis>
68
69    <para>
70     Calculates hashed MAC for <parameter>data</parameter> with key <parameter>key</parameter>.
71     <parameter>type</parameter> is the same as in <function>digest()</function>.
72    </para>
73
74    <para>
75     This is similar to <function>digest()</function> but the hash can only be
76     recalculated knowing the key.  This prevents the scenario of someone
77     altering data and also changing the hash to match.
78    </para>
79
80    <para>
81     If the key is larger than the hash block size it will first be hashed and
82     the result will be used as key.
83    </para>
84   </sect3>
85  </sect2>
86
87  <sect2>
88   <title>Password Hashing Functions</title>
89
90   <para>
91    The functions <function>crypt()</function> and <function>gen_salt()</function>
92    are specifically designed for hashing passwords.
93    <function>crypt()</function> does the hashing and <function>gen_salt()</function>
94    prepares algorithm parameters for it.
95   </para>
96
97   <para>
98    The algorithms in <function>crypt()</function> differ from the usual
99    MD5 or SHA1 hashing algorithms in the following respects:
100   </para>
101
102   <orderedlist>
103    <listitem>
104     <para>
105      They are slow.  As the amount of data is so small, this is the only
106      way to make brute-forcing passwords hard.
107     </para>
108    </listitem>
109    <listitem>
110     <para>
111      They use a random value, called the <firstterm>salt</firstterm>, so that users
112      having the same password will have different encrypted passwords.
113      This is also an additional defense against reversing the algorithm.
114     </para>
115    </listitem>
116    <listitem>
117     <para>
118      They include the algorithm type in the result, so passwords hashed with
119      different algorithms can co-exist.
120     </para>
121    </listitem>
122    <listitem>
123     <para>
124      Some of them are adaptive &mdash; that means when computers get
125      faster, you can tune the algorithm to be slower, without
126      introducing incompatibility with existing passwords.
127     </para>
128    </listitem>
129   </orderedlist>
130
131   <para>
132    <xref linkend="pgcrypto-crypt-algorithms"/> lists the algorithms
133    supported by the <function>crypt()</function> function.
134   </para>
135
136   <table id="pgcrypto-crypt-algorithms">
137    <title>Supported Algorithms for <function>crypt()</function></title>
138    <tgroup cols="6">
139     <thead>
140      <row>
141       <entry>Algorithm</entry>
142       <entry>Max Password Length</entry>
143       <entry>Adaptive?</entry>
144       <entry>Salt Bits</entry>
145       <entry>Output Length</entry>
146       <entry>Description</entry>
147      </row>
148     </thead>
149     <tbody>
150      <row>
151       <entry><literal>bf</literal></entry>
152       <entry>72</entry>
153       <entry>yes</entry>
154       <entry>128</entry>
155       <entry>60</entry>
156       <entry>Blowfish-based, variant 2a</entry>
157      </row>
158      <row>
159       <entry><literal>md5</literal></entry>
160       <entry>unlimited</entry>
161       <entry>no</entry>
162       <entry>48</entry>
163       <entry>34</entry>
164       <entry>MD5-based crypt</entry>
165      </row>
166      <row>
167       <entry><literal>xdes</literal></entry>
168       <entry>8</entry>
169       <entry>yes</entry>
170       <entry>24</entry>
171       <entry>20</entry>
172       <entry>Extended DES</entry>
173      </row>
174      <row>
175       <entry><literal>des</literal></entry>
176       <entry>8</entry>
177       <entry>no</entry>
178       <entry>12</entry>
179       <entry>13</entry>
180       <entry>Original UNIX crypt</entry>
181      </row>
182     </tbody>
183    </tgroup>
184   </table>
185
186   <sect3>
187    <title><function>crypt()</function></title>
188
189    <indexterm>
190     <primary>crypt</primary>
191    </indexterm>
192
193 <synopsis>
194 crypt(password text, salt text) returns text
195 </synopsis>
196
197    <para>
198     Calculates a crypt(3)-style hash of <parameter>password</parameter>.
199     When storing a new password, you need to use
200     <function>gen_salt()</function> to generate a new <parameter>salt</parameter> value.
201     To check a password, pass the stored hash value as <parameter>salt</parameter>,
202     and test whether the result matches the stored value.
203    </para>
204    <para>
205     Example of setting a new password:
206 <programlisting>
207 UPDATE ... SET pswhash = crypt('new password', gen_salt('md5'));
208 </programlisting>
209    </para>
210    <para>
211     Example of authentication:
212 <programlisting>
213 SELECT (pswhash = crypt('entered password', pswhash)) AS pswmatch FROM ... ;
214 </programlisting>
215     This returns <literal>true</literal> if the entered password is correct.
216    </para>
217   </sect3>
218
219   <sect3>
220    <title><function>gen_salt()</function></title>
221
222   <indexterm>
223    <primary>gen_salt</primary>
224   </indexterm>
225
226 <synopsis>
227 gen_salt(type text [, iter_count integer ]) returns text
228 </synopsis>
229
230    <para>
231     Generates a new random salt string for use in <function>crypt()</function>.
232     The salt string also tells <function>crypt()</function> which algorithm to use.
233    </para>
234
235    <para>
236     The <parameter>type</parameter> parameter specifies the hashing algorithm.
237     The accepted types are: <literal>des</literal>, <literal>xdes</literal>,
238     <literal>md5</literal> and <literal>bf</literal>.
239    </para>
240
241    <para>
242     The <parameter>iter_count</parameter> parameter lets the user specify the iteration
243     count, for algorithms that have one.
244     The higher the count, the more time it takes to hash
245     the password and therefore the more time to break it.  Although with
246     too high a count the time to calculate a hash may be several years
247     &mdash; which is somewhat impractical.  If the <parameter>iter_count</parameter>
248     parameter is omitted, the default iteration count is used.
249     Allowed values for <parameter>iter_count</parameter> depend on the algorithm and
250     are shown in <xref linkend="pgcrypto-icfc-table"/>.
251    </para>
252
253    <table id="pgcrypto-icfc-table">
254     <title>Iteration Counts for <function>crypt()</function></title>
255     <tgroup cols="4">
256      <thead>
257       <row>
258        <entry>Algorithm</entry>
259        <entry>Default</entry>
260        <entry>Min</entry>
261        <entry>Max</entry>
262       </row>
263      </thead>
264      <tbody>
265       <row>
266        <entry><literal>xdes</literal></entry>
267        <entry>725</entry>
268        <entry>1</entry>
269        <entry>16777215</entry>
270       </row>
271       <row>
272        <entry><literal>bf</literal></entry>
273        <entry>6</entry>
274        <entry>4</entry>
275        <entry>31</entry>
276       </row>
277      </tbody>
278     </tgroup>
279    </table>
280
281    <para>
282     For <literal>xdes</literal> there is an additional limitation that the
283     iteration count must be an odd number.
284    </para>
285
286    <para>
287     To pick an appropriate iteration count, consider that
288     the original DES crypt was designed to have the speed of 4 hashes per
289     second on the hardware of that time.
290     Slower than 4 hashes per second would probably dampen usability.
291     Faster than 100 hashes per second is probably too fast.
292    </para>
293
294    <para>
295     <xref linkend="pgcrypto-hash-speed-table"/> gives an overview of the relative slowness
296     of different hashing algorithms.
297     The table shows how much time it would take to try all
298     combinations of characters in an 8-character password, assuming
299     that the password contains either only lower case letters, or
300     upper- and lower-case letters and numbers.
301     In the <literal>crypt-bf</literal> entries, the number after a slash is
302     the <parameter>iter_count</parameter> parameter of
303     <function>gen_salt</function>.
304    </para>
305
306    <table id="pgcrypto-hash-speed-table">
307     <title>Hash Algorithm Speeds</title>
308     <tgroup cols="5">
309      <thead>
310       <row>
311        <entry>Algorithm</entry>
312        <entry>Hashes/sec</entry>
313        <entry>For <literal>[a-z]</literal></entry>
314        <entry>For <literal>[A-Za-z0-9]</literal></entry>
315        <entry>Duration relative to <literal>md5 hash</literal></entry>
316       </row>
317      </thead>
318      <tbody>
319       <row>
320        <entry><literal>crypt-bf/8</literal></entry>
321        <entry>1792</entry>
322        <entry>4 years</entry>
323        <entry>3927 years</entry>
324        <entry>100k</entry>
325       </row>
326       <row>
327        <entry><literal>crypt-bf/7</literal></entry>
328        <entry>3648</entry>
329        <entry>2 years</entry>
330        <entry>1929 years</entry>
331        <entry>50k</entry>
332       </row>
333       <row>
334        <entry><literal>crypt-bf/6</literal></entry>
335        <entry>7168</entry>
336        <entry>1 year</entry>
337        <entry>982 years</entry>
338        <entry>25k</entry>
339       </row>
340       <row>
341        <entry><literal>crypt-bf/5</literal></entry>
342        <entry>13504</entry>
343        <entry>188 days</entry>
344        <entry>521 years</entry>
345        <entry>12.5k</entry>
346       </row>
347       <row>
348        <entry><literal>crypt-md5</literal></entry>
349        <entry>171584</entry>
350        <entry>15 days</entry>
351        <entry>41 years</entry>
352        <entry>1k</entry>
353       </row>
354       <row>
355        <entry><literal>crypt-des</literal></entry>
356        <entry>23221568</entry>
357        <entry>157.5 minutes</entry>
358        <entry>108 days</entry>
359        <entry>7</entry>
360       </row>
361       <row>
362        <entry><literal>sha1</literal></entry>
363        <entry>37774272</entry>
364        <entry>90 minutes</entry>
365        <entry>68 days</entry>
366        <entry>4</entry>
367       </row>
368       <row>
369        <entry><literal>md5</literal> (hash)</entry>
370        <entry>150085504</entry>
371        <entry>22.5 minutes</entry>
372        <entry>17 days</entry>
373        <entry>1</entry>
374       </row>
375      </tbody>
376     </tgroup>
377    </table>
378
379    <para>
380     Notes:
381    </para>
382
383    <itemizedlist>
384     <listitem>
385      <para>
386      The machine used is an Intel Mobile Core i3.
387      </para>
388     </listitem>
389     <listitem>
390      <para>
391       <literal>crypt-des</literal> and <literal>crypt-md5</literal> algorithm numbers are
392       taken from John the Ripper v1.6.38 <literal>-test</literal> output.
393      </para>
394     </listitem>
395     <listitem>
396      <para>
397       <literal>md5 hash</literal> numbers are from mdcrack 1.2.
398      </para>
399     </listitem>
400     <listitem>
401      <para>
402       <literal>sha1</literal> numbers are from lcrack-20031130-beta.
403      </para>
404     </listitem>
405     <listitem>
406      <para>
407       <literal>crypt-bf</literal> numbers are taken using a simple program that
408       loops over 1000 8-character passwords.  That way I can show the speed
409       with different numbers of iterations.  For reference: <literal>john
410       -test</literal> shows 13506 loops/sec for <literal>crypt-bf/5</literal>.
411       (The very small
412       difference in results is in accordance with the fact that the
413       <literal>crypt-bf</literal> implementation in <filename>pgcrypto</filename>
414       is the same one used in John the Ripper.)
415      </para>
416     </listitem>
417    </itemizedlist>
418
419    <para>
420     Note that <quote>try all combinations</quote> is not a realistic exercise.
421     Usually password cracking is done with the help of dictionaries, which
422     contain both regular words and various mutations of them.  So, even
423     somewhat word-like passwords could be cracked much faster than the above
424     numbers suggest, while a 6-character non-word-like password may escape
425     cracking.  Or not.
426    </para>
427   </sect3>
428  </sect2>
429
430  <sect2>
431   <title>PGP Encryption Functions</title>
432
433   <para>
434    The functions here implement the encryption part of the OpenPGP (RFC 4880)
435    standard.  Supported are both symmetric-key and public-key encryption.
436   </para>
437
438   <para>
439    An encrypted PGP message consists of 2 parts, or <firstterm>packets</firstterm>:
440   </para>
441   <itemizedlist>
442    <listitem>
443     <para>
444      Packet containing a session key &mdash; either symmetric-key or public-key
445      encrypted.
446     </para>
447    </listitem>
448    <listitem>
449     <para>
450      Packet containing data encrypted with the session key.
451     </para>
452    </listitem>
453   </itemizedlist>
454
455   <para>
456    When encrypting with a symmetric key (i.e., a password):
457   </para>
458   <orderedlist>
459    <listitem>
460     <para>
461      The given password is hashed using a String2Key (S2K) algorithm.  This is
462      rather similar to <function>crypt()</function> algorithms &mdash; purposefully
463      slow and with random salt &mdash; but it produces a full-length binary
464      key.
465     </para>
466    </listitem>
467    <listitem>
468     <para>
469      If a separate session key is requested, a new random key will be
470      generated.  Otherwise the S2K key will be used directly as the session
471      key.
472     </para>
473    </listitem>
474    <listitem>
475     <para>
476      If the S2K key is to be used directly, then only S2K settings will be put
477      into the session key packet.  Otherwise the session key will be encrypted
478      with the S2K key and put into the session key packet.
479     </para>
480    </listitem>
481   </orderedlist>
482
483   <para>
484    When encrypting with a public key:
485   </para>
486   <orderedlist>
487    <listitem>
488     <para>
489      A new random session key is generated.
490     </para>
491    </listitem>
492    <listitem>
493     <para>
494      It is encrypted using the public key and put into the session key packet.
495     </para>
496    </listitem>
497   </orderedlist>
498
499   <para>
500    In either case the data to be encrypted is processed as follows:
501   </para>
502   <orderedlist>
503    <listitem>
504     <para>
505      Optional data-manipulation: compression, conversion to UTF-8,
506      and/or conversion of line-endings.
507     </para>
508    </listitem>
509    <listitem>
510     <para>
511      The data is prefixed with a block of random bytes.  This is equivalent
512      to using a random IV.
513     </para>
514    </listitem>
515    <listitem>
516     <para>
517      An SHA1 hash of the random prefix and data is appended.
518     </para>
519    </listitem>
520    <listitem>
521     <para>
522      All this is encrypted with the session key and placed in the data packet.
523     </para>
524    </listitem>
525   </orderedlist>
526
527   <sect3>
528    <title><function>pgp_sym_encrypt()</function></title>
529
530    <indexterm>
531     <primary>pgp_sym_encrypt</primary>
532    </indexterm>
533
534    <indexterm>
535     <primary>pgp_sym_encrypt_bytea</primary>
536    </indexterm>
537
538 <synopsis>
539 pgp_sym_encrypt(data text, psw text [, options text ]) returns bytea
540 pgp_sym_encrypt_bytea(data bytea, psw text [, options text ]) returns bytea
541 </synopsis>
542    <para>
543     Encrypt <parameter>data</parameter> with a symmetric PGP key <parameter>psw</parameter>.
544     The <parameter>options</parameter> parameter can contain option settings,
545     as described below.
546    </para>
547   </sect3>
548
549   <sect3>
550    <title><function>pgp_sym_decrypt()</function></title>
551
552    <indexterm>
553     <primary>pgp_sym_decrypt</primary>
554    </indexterm>
555
556    <indexterm>
557     <primary>pgp_sym_decrypt_bytea</primary>
558    </indexterm>
559
560 <synopsis>
561 pgp_sym_decrypt(msg bytea, psw text [, options text ]) returns text
562 pgp_sym_decrypt_bytea(msg bytea, psw text [, options text ]) returns bytea
563 </synopsis>
564    <para>
565     Decrypt a symmetric-key-encrypted PGP message.
566    </para>
567    <para>
568     Decrypting <type>bytea</type> data with <function>pgp_sym_decrypt</function> is disallowed.
569     This is to avoid outputting invalid character data.  Decrypting
570     originally textual data with <function>pgp_sym_decrypt_bytea</function> is fine.
571    </para>
572    <para>
573     The <parameter>options</parameter> parameter can contain option settings,
574     as described below.
575    </para>
576   </sect3>
577
578   <sect3>
579    <title><function>pgp_pub_encrypt()</function></title>
580
581    <indexterm>
582     <primary>pgp_pub_encrypt</primary>
583    </indexterm>
584
585    <indexterm>
586     <primary>pgp_pub_encrypt_bytea</primary>
587    </indexterm>
588
589 <synopsis>
590 pgp_pub_encrypt(data text, key bytea [, options text ]) returns bytea
591 pgp_pub_encrypt_bytea(data bytea, key bytea [, options text ]) returns bytea
592 </synopsis>
593    <para>
594     Encrypt <parameter>data</parameter> with a public PGP key <parameter>key</parameter>.
595     Giving this function a secret key will produce an error.
596    </para>
597    <para>
598     The <parameter>options</parameter> parameter can contain option settings,
599     as described below.
600    </para>
601   </sect3>
602
603   <sect3>
604    <title><function>pgp_pub_decrypt()</function></title>
605
606    <indexterm>
607     <primary>pgp_pub_decrypt</primary>
608    </indexterm>
609
610    <indexterm>
611     <primary>pgp_pub_decrypt_bytea</primary>
612    </indexterm>
613
614 <synopsis>
615 pgp_pub_decrypt(msg bytea, key bytea [, psw text [, options text ]]) returns text
616 pgp_pub_decrypt_bytea(msg bytea, key bytea [, psw text [, options text ]]) returns bytea
617 </synopsis>
618    <para>
619     Decrypt a public-key-encrypted message.  <parameter>key</parameter> must be the
620     secret key corresponding to the public key that was used to encrypt.
621     If the secret key is password-protected, you must give the password in
622     <parameter>psw</parameter>.  If there is no password, but you want to specify
623     options, you need to give an empty password.
624    </para>
625    <para>
626     Decrypting <type>bytea</type> data with <function>pgp_pub_decrypt</function> is disallowed.
627     This is to avoid outputting invalid character data.  Decrypting
628     originally textual data with <function>pgp_pub_decrypt_bytea</function> is fine.
629    </para>
630    <para>
631     The <parameter>options</parameter> parameter can contain option settings,
632     as described below.
633    </para>
634   </sect3>
635
636   <sect3>
637    <title><function>pgp_key_id()</function></title>
638
639    <indexterm>
640     <primary>pgp_key_id</primary>
641    </indexterm>
642
643 <synopsis>
644 pgp_key_id(bytea) returns text
645 </synopsis>
646    <para>
647     <function>pgp_key_id</function> extracts the key ID of a PGP public or secret key.
648     Or it gives the key ID that was used for encrypting the data, if given
649     an encrypted message.
650    </para>
651    <para>
652     It can return 2 special key IDs:
653    </para>
654    <itemizedlist>
655     <listitem>
656      <para>
657       <literal>SYMKEY</literal>
658      </para>
659      <para>
660       The message is encrypted with a symmetric key.
661      </para>
662     </listitem>
663     <listitem>
664      <para>
665       <literal>ANYKEY</literal>
666      </para>
667      <para>
668       The message is public-key encrypted, but the key ID has been removed.
669       That means you will need to try all your secret keys on it to see
670       which one decrypts it.  <filename>pgcrypto</filename> itself does not produce
671       such messages.
672      </para>
673     </listitem>
674    </itemizedlist>
675    <para>
676     Note that different keys may have the same ID.   This is rare but a normal
677     event. The client application should then try to decrypt with each one,
678     to see which fits &mdash; like handling <literal>ANYKEY</literal>.
679    </para>
680   </sect3>
681
682   <sect3>
683    <title><function>armor()</function>, <function>dearmor()</function></title>
684
685    <indexterm>
686     <primary>armor</primary>
687    </indexterm>
688
689    <indexterm>
690     <primary>dearmor</primary>
691    </indexterm>
692
693 <synopsis>
694 armor(data bytea [ , keys text[], values text[] ]) returns text
695 dearmor(data text) returns bytea
696 </synopsis>
697    <para>
698     These functions wrap/unwrap binary data into PGP ASCII-armor format,
699     which is basically Base64 with CRC and additional formatting.
700    </para>
701
702    <para>
703     If the <parameter>keys</parameter> and <parameter>values</parameter> arrays are specified,
704     an <firstterm>armor header</firstterm> is added to the armored format for each
705     key/value pair. Both arrays must be single-dimensional, and they must
706     be of the same length.  The keys and values cannot contain any non-ASCII
707     characters.
708    </para>
709   </sect3>
710
711   <sect3>
712    <title><function>pgp_armor_headers</function></title>
713
714    <indexterm>
715     <primary>pgp_armor_headers</primary>
716    </indexterm>
717
718 <synopsis>
719 pgp_armor_headers(data text, key out text, value out text) returns setof record
720 </synopsis>
721    <para>
722     <function>pgp_armor_headers()</function> extracts the armor headers from
723     <parameter>data</parameter>.  The return value is a set of rows with two columns,
724     key and value.  If the keys or values contain any non-ASCII characters,
725     they are treated as UTF-8.
726    </para>
727   </sect3>
728
729   <sect3>
730    <title>Options for PGP Functions</title>
731
732    <para>
733     Options are named to be similar to GnuPG.  An option's value should be
734     given after an equal sign; separate options from each other with commas.
735     For example:
736 <programlisting>
737 pgp_sym_encrypt(data, psw, 'compress-algo=1, cipher-algo=aes256')
738 </programlisting>
739    </para>
740
741    <para>
742     All of the options except <literal>convert-crlf</literal> apply only to
743     encrypt functions.  Decrypt functions get the parameters from the PGP
744     data.
745    </para>
746
747    <para>
748     The most interesting options are probably
749     <literal>compress-algo</literal> and <literal>unicode-mode</literal>.
750     The rest should have reasonable defaults.
751    </para>
752
753   <sect4>
754    <title>cipher-algo</title>
755
756    <para>
757     Which cipher algorithm to use.
758    </para>
759 <literallayout>
760 Values: bf, aes128, aes192, aes256 (OpenSSL-only: <literal>3des</literal>, <literal>cast5</literal>)
761 Default: aes128
762 Applies to: pgp_sym_encrypt, pgp_pub_encrypt
763 </literallayout>
764   </sect4>
765
766   <sect4>
767    <title>compress-algo</title>
768
769    <para>
770     Which compression algorithm to use.  Only available if
771     <productname>PostgreSQL</productname> was built with zlib.
772    </para>
773 <literallayout>
774 Values:
775   0 - no compression
776   1 - ZIP compression
777   2 - ZLIB compression (= ZIP plus meta-data and block CRCs)
778 Default: 0
779 Applies to: pgp_sym_encrypt, pgp_pub_encrypt
780 </literallayout>
781   </sect4>
782
783   <sect4>
784    <title>compress-level</title>
785
786    <para>
787     How much to compress.  Higher levels compress smaller but are slower.
788     0 disables compression.
789    </para>
790 <literallayout>
791 Values: 0, 1-9
792 Default: 6
793 Applies to: pgp_sym_encrypt, pgp_pub_encrypt
794 </literallayout>
795   </sect4>
796
797   <sect4>
798    <title>convert-crlf</title>
799
800    <para>
801     Whether to convert <literal>\n</literal> into <literal>\r\n</literal> when
802     encrypting and <literal>\r\n</literal> to <literal>\n</literal> when
803     decrypting.  RFC 4880 specifies that text data should be stored using
804     <literal>\r\n</literal> line-feeds.  Use this to get fully RFC-compliant
805     behavior.
806    </para>
807 <literallayout>
808 Values: 0, 1
809 Default: 0
810 Applies to: pgp_sym_encrypt, pgp_pub_encrypt, pgp_sym_decrypt, pgp_pub_decrypt
811 </literallayout>
812   </sect4>
813
814   <sect4>
815    <title>disable-mdc</title>
816
817    <para>
818     Do not protect data with SHA-1.  The only good reason to use this
819     option is to achieve compatibility with ancient PGP products, predating
820     the addition of SHA-1 protected packets to RFC 4880.
821     Recent gnupg.org and pgp.com software supports it fine.
822    </para>
823 <literallayout>
824 Values: 0, 1
825 Default: 0
826 Applies to: pgp_sym_encrypt, pgp_pub_encrypt
827 </literallayout>
828   </sect4>
829
830   <sect4>
831    <title>sess-key</title>
832
833    <para>
834     Use separate session key.  Public-key encryption always uses a separate
835     session key; this option is for symmetric-key encryption, which by default
836     uses the S2K key directly.
837    </para>
838 <literallayout>
839 Values: 0, 1
840 Default: 0
841 Applies to: pgp_sym_encrypt
842 </literallayout>
843   </sect4>
844
845   <sect4>
846    <title>s2k-mode</title>
847
848    <para>
849     Which S2K algorithm to use.
850    </para>
851 <literallayout>
852 Values:
853   0 - Without salt.  Dangerous!
854   1 - With salt but with fixed iteration count.
855   3 - Variable iteration count.
856 Default: 3
857 Applies to: pgp_sym_encrypt
858 </literallayout>
859   </sect4>
860
861   <sect4>
862    <title>s2k-count</title>
863
864    <para>
865     The number of iterations of the S2K algorithm to use.  It must
866     be a value between 1024 and 65011712, inclusive.
867    </para>
868 <literallayout>
869 Default: A random value between 65536 and 253952
870 Applies to: pgp_sym_encrypt, only with s2k-mode=3
871 </literallayout>
872   </sect4>
873
874   <sect4>
875    <title>s2k-digest-algo</title>
876
877    <para>
878     Which digest algorithm to use in S2K calculation.
879    </para>
880 <literallayout>
881 Values: md5, sha1
882 Default: sha1
883 Applies to: pgp_sym_encrypt
884 </literallayout>
885   </sect4>
886
887   <sect4>
888    <title>s2k-cipher-algo</title>
889
890    <para>
891     Which cipher to use for encrypting separate session key.
892    </para>
893 <literallayout>
894 Values: bf, aes, aes128, aes192, aes256
895 Default: use cipher-algo
896 Applies to: pgp_sym_encrypt
897 </literallayout>
898   </sect4>
899
900   <sect4>
901    <title>unicode-mode</title>
902
903    <para>
904     Whether to convert textual data from database internal encoding to
905     UTF-8 and back.  If your database already is UTF-8, no conversion will
906     be done, but the message will be tagged as UTF-8.  Without this option
907     it will not be.
908    </para>
909 <literallayout>
910 Values: 0, 1
911 Default: 0
912 Applies to: pgp_sym_encrypt, pgp_pub_encrypt
913 </literallayout>
914   </sect4>
915   </sect3>
916
917  <sect3>
918   <title>Generating PGP Keys with GnuPG</title>
919
920   <para>
921    To generate a new key:
922 <programlisting>
923 gpg --gen-key
924 </programlisting>
925   </para>
926   <para>
927    The preferred key type is <quote>DSA and Elgamal</quote>.
928   </para>
929   <para>
930    For RSA encryption you must create either DSA or RSA sign-only key
931    as master and then add an RSA encryption subkey with
932    <literal>gpg --edit-key</literal>.
933   </para>
934   <para>
935    To list keys:
936 <programlisting>
937 gpg --list-secret-keys
938 </programlisting>
939   </para>
940   <para>
941    To export a public key in ASCII-armor format:
942 <programlisting>
943 gpg -a --export KEYID > public.key
944 </programlisting>
945   </para>
946   <para>
947    To export a secret key in ASCII-armor format:
948 <programlisting>
949 gpg -a --export-secret-keys KEYID > secret.key
950 </programlisting>
951   </para>
952   <para>
953    You need to use <function>dearmor()</function> on these keys before giving them to
954    the PGP functions.  Or if you can handle binary data, you can drop
955    <literal>-a</literal> from the command.
956   </para>
957   <para>
958    For more details see <literal>man gpg</literal>,
959    <ulink url="https://www.gnupg.org/gph/en/manual.html">The GNU
960    Privacy Handbook</ulink> and other documentation on
961    <ulink url="https://www.gnupg.org/"></ulink>.
962   </para>
963  </sect3>
964
965  <sect3>
966   <title>Limitations of PGP Code</title>
967
968   <itemizedlist>
969    <listitem>
970     <para>
971     No support for signing.  That also means that it is not checked
972     whether the encryption subkey belongs to the master key.
973     </para>
974    </listitem>
975    <listitem>
976     <para>
977     No support for encryption key as master key.  As such practice
978     is generally discouraged, this should not be a problem.
979     </para>
980    </listitem>
981    <listitem>
982     <para>
983     No support for several subkeys.  This may seem like a problem, as this
984     is common practice.  On the other hand, you should not use your regular
985     GPG/PGP keys with <filename>pgcrypto</filename>, but create new ones,
986     as the usage scenario is rather different.
987     </para>
988    </listitem>
989   </itemizedlist>
990   </sect3>
991  </sect2>
992
993  <sect2>
994   <title>Raw Encryption Functions</title>
995
996   <para>
997    These functions only run a cipher over data; they don't have any advanced
998    features of PGP encryption.  Therefore they have some major problems:
999   </para>
1000   <orderedlist>
1001    <listitem>
1002     <para>
1003     They use user key directly as cipher key.
1004     </para>
1005    </listitem>
1006    <listitem>
1007     <para>
1008     They don't provide any integrity checking, to see
1009     if the encrypted data was modified.
1010     </para>
1011    </listitem>
1012    <listitem>
1013     <para>
1014     They expect that users manage all encryption parameters
1015     themselves, even IV.
1016     </para>
1017    </listitem>
1018    <listitem>
1019     <para>
1020     They don't handle text.
1021     </para>
1022    </listitem>
1023   </orderedlist>
1024   <para>
1025    So, with the introduction of PGP encryption, usage of raw
1026    encryption functions is discouraged.
1027   </para>
1028
1029   <indexterm>
1030    <primary>encrypt</primary>
1031   </indexterm>
1032
1033   <indexterm>
1034    <primary>decrypt</primary>
1035   </indexterm>
1036
1037   <indexterm>
1038    <primary>encrypt_iv</primary>
1039   </indexterm>
1040
1041   <indexterm>
1042    <primary>decrypt_iv</primary>
1043   </indexterm>
1044
1045 <synopsis>
1046 encrypt(data bytea, key bytea, type text) returns bytea
1047 decrypt(data bytea, key bytea, type text) returns bytea
1048
1049 encrypt_iv(data bytea, key bytea, iv bytea, type text) returns bytea
1050 decrypt_iv(data bytea, key bytea, iv bytea, type text) returns bytea
1051 </synopsis>
1052
1053   <para>
1054    Encrypt/decrypt data using the cipher method specified by
1055    <parameter>type</parameter>.  The syntax of the
1056    <parameter>type</parameter> string is:
1057
1058 <synopsis>
1059 <replaceable>algorithm</replaceable> <optional> <literal>-</literal> <replaceable>mode</replaceable> </optional> <optional> <literal>/pad:</literal> <replaceable>padding</replaceable> </optional>
1060 </synopsis>
1061    where <replaceable>algorithm</replaceable> is one of:
1062
1063   <itemizedlist>
1064    <listitem><para><literal>bf</literal> &mdash; Blowfish</para></listitem>
1065    <listitem><para><literal>aes</literal> &mdash; AES (Rijndael-128, -192 or -256)</para></listitem>
1066   </itemizedlist>
1067    and <replaceable>mode</replaceable> is one of:
1068   <itemizedlist>
1069    <listitem>
1070     <para>
1071     <literal>cbc</literal> &mdash; next block depends on previous (default)
1072     </para>
1073    </listitem>
1074    <listitem>
1075     <para>
1076     <literal>ecb</literal> &mdash; each block is encrypted separately (for
1077     testing only)
1078     </para>
1079    </listitem>
1080   </itemizedlist>
1081    and <replaceable>padding</replaceable> is one of:
1082   <itemizedlist>
1083    <listitem>
1084     <para>
1085     <literal>pkcs</literal> &mdash; data may be any length (default)
1086     </para>
1087    </listitem>
1088    <listitem>
1089     <para>
1090     <literal>none</literal> &mdash; data must be multiple of cipher block size
1091     </para>
1092    </listitem>
1093   </itemizedlist>
1094   </para>
1095   <para>
1096    So, for example, these are equivalent:
1097 <programlisting>
1098 encrypt(data, 'fooz', 'bf')
1099 encrypt(data, 'fooz', 'bf-cbc/pad:pkcs')
1100 </programlisting>
1101   </para>
1102   <para>
1103    In <function>encrypt_iv</function> and <function>decrypt_iv</function>, the
1104    <parameter>iv</parameter> parameter is the initial value for the CBC mode;
1105    it is ignored for ECB.
1106    It is clipped or padded with zeroes if not exactly block size.
1107    It defaults to all zeroes in the functions without this parameter.
1108   </para>
1109  </sect2>
1110
1111  <sect2>
1112   <title>Random-Data Functions</title>
1113
1114   <indexterm>
1115    <primary>gen_random_bytes</primary>
1116   </indexterm>
1117
1118 <synopsis>
1119 gen_random_bytes(count integer) returns bytea
1120 </synopsis>
1121   <para>
1122    Returns <parameter>count</parameter> cryptographically strong random bytes.
1123    At most 1024 bytes can be extracted at a time.  This is to avoid
1124    draining the randomness generator pool.
1125   </para>
1126
1127   <indexterm>
1128    <primary>gen_random_uuid</primary>
1129   </indexterm>
1130
1131 <synopsis>
1132 gen_random_uuid() returns uuid
1133 </synopsis>
1134   <para>
1135    Returns a version 4 (random) UUID. (Obsolete, this function is now also
1136    included in core <productname>PostgreSQL</productname>.)
1137   </para>
1138  </sect2>
1139
1140  <sect2>
1141   <title>Notes</title>
1142
1143   <sect3>
1144    <title>Configuration</title>
1145
1146    <para>
1147     <filename>pgcrypto</filename> configures itself according to the findings of the
1148     main PostgreSQL <literal>configure</literal> script.  The options that
1149     affect it are <literal>--with-zlib</literal> and
1150     <literal>--with-openssl</literal>.
1151    </para>
1152
1153    <para>
1154     When compiled with zlib, PGP encryption functions are able to
1155     compress data before encrypting.
1156    </para>
1157
1158    <para>
1159     When compiled with OpenSSL, there will be more algorithms available.
1160     Also public-key encryption functions will be faster as OpenSSL
1161     has more optimized BIGNUM functions.
1162    </para>
1163
1164    <table id="pgcrypto-with-without-openssl">
1165     <title>Summary of Functionality with and without OpenSSL</title>
1166     <tgroup cols="3">
1167      <thead>
1168       <row>
1169        <entry>Functionality</entry>
1170        <entry>Built-in</entry>
1171        <entry>With OpenSSL</entry>
1172       </row>
1173      </thead>
1174      <tbody>
1175       <row>
1176        <entry>MD5</entry>
1177        <entry>yes</entry>
1178        <entry>yes</entry>
1179       </row>
1180       <row>
1181        <entry>SHA1</entry>
1182        <entry>yes</entry>
1183        <entry>yes</entry>
1184       </row>
1185       <row>
1186        <entry>SHA224/256/384/512</entry>
1187        <entry>yes</entry>
1188        <entry>yes</entry>
1189       </row>
1190       <row>
1191        <entry>Other digest algorithms</entry>
1192        <entry>no</entry>
1193        <entry>yes (Note 1)</entry>
1194       </row>
1195       <row>
1196        <entry>Blowfish</entry>
1197        <entry>yes</entry>
1198        <entry>yes</entry>
1199       </row>
1200       <row>
1201        <entry>AES</entry>
1202        <entry>yes</entry>
1203        <entry>yes</entry>
1204       </row>
1205       <row>
1206        <entry>DES/3DES/CAST5</entry>
1207        <entry>no</entry>
1208        <entry>yes</entry>
1209       </row>
1210       <row>
1211        <entry>Raw encryption</entry>
1212        <entry>yes</entry>
1213        <entry>yes</entry>
1214       </row>
1215       <row>
1216        <entry>PGP Symmetric encryption</entry>
1217        <entry>yes</entry>
1218        <entry>yes</entry>
1219       </row>
1220       <row>
1221        <entry>PGP Public-Key encryption</entry>
1222        <entry>yes</entry>
1223        <entry>yes</entry>
1224       </row>
1225      </tbody>
1226     </tgroup>
1227    </table>
1228
1229    <para>
1230     Notes:
1231    </para>
1232
1233    <orderedlist>
1234     <listitem>
1235      <para>
1236       Any digest algorithm OpenSSL supports is automatically picked up.
1237       This is not possible with ciphers, which need to be supported
1238       explicitly.
1239      </para>
1240     </listitem>
1241    </orderedlist>
1242   </sect3>
1243
1244   <sect3>
1245    <title>NULL Handling</title>
1246
1247    <para>
1248     As is standard in SQL, all functions return NULL, if any of the arguments
1249     are NULL.  This may create security risks on careless usage.
1250    </para>
1251   </sect3>
1252
1253   <sect3>
1254    <title>Security Limitations</title>
1255
1256    <para>
1257     All <filename>pgcrypto</filename> functions run inside the database server.
1258     That means that all
1259     the data and passwords move between <filename>pgcrypto</filename> and client
1260     applications in clear text.  Thus you must:
1261    </para>
1262
1263    <orderedlist>
1264     <listitem>
1265      <para>Connect locally or use SSL connections.</para>
1266     </listitem>
1267     <listitem>
1268      <para>Trust both system and database administrator.</para>
1269     </listitem>
1270    </orderedlist>
1271
1272    <para>
1273     If you cannot, then better do crypto inside client application.
1274    </para>
1275
1276    <para>
1277     The implementation does not resist
1278     <ulink url="https://en.wikipedia.org/wiki/Side-channel_attack">side-channel
1279     attacks</ulink>.  For example, the time required for
1280     a <filename>pgcrypto</filename> decryption function to complete varies among
1281     ciphertexts of a given size.
1282    </para>
1283   </sect3>
1284
1285   <sect3>
1286    <title>Useful Reading</title>
1287
1288    <itemizedlist>
1289     <listitem>
1290      <para><ulink url="https://www.gnupg.org/gph/en/manual.html"></ulink></para>
1291      <para>The GNU Privacy Handbook.</para>
1292     </listitem>
1293     <listitem>
1294      <para><ulink url="http://www.openwall.com/crypt/"></ulink></para>
1295      <para>Describes the crypt-blowfish algorithm.</para>
1296     </listitem>
1297     <listitem>
1298      <para>
1299       <ulink url="http://www.iusmentis.com/security/passphrasefaq/"></ulink>
1300      </para>
1301      <para>How to choose a good password.</para>
1302     </listitem>
1303     <listitem>
1304      <para><ulink url="http://world.std.com/~reinhold/diceware.html"></ulink></para>
1305      <para>Interesting idea for picking passwords.</para>
1306     </listitem>
1307     <listitem>
1308      <para>
1309       <ulink url="http://www.interhack.net/people/cmcurtin/snake-oil-faq.html"></ulink>
1310      </para>
1311      <para>Describes good and bad cryptography.</para>
1312     </listitem>
1313    </itemizedlist>
1314   </sect3>
1315
1316   <sect3>
1317    <title>Technical References</title>
1318
1319    <itemizedlist>
1320     <listitem>
1321      <para><ulink url="https://tools.ietf.org/html/rfc4880"></ulink></para>
1322      <para>OpenPGP message format.</para>
1323     </listitem>
1324     <listitem>
1325      <para><ulink url="https://tools.ietf.org/html/rfc1321"></ulink></para>
1326      <para>The MD5 Message-Digest Algorithm.</para>
1327     </listitem>
1328     <listitem>
1329      <para><ulink url="https://tools.ietf.org/html/rfc2104"></ulink></para>
1330      <para>HMAC: Keyed-Hashing for Message Authentication.</para>
1331     </listitem>
1332     <listitem>
1333      <para>
1334       <ulink url="https://www.usenix.org/legacy/events/usenix99/provos.html"></ulink>
1335      </para>
1336      <para>Comparison of crypt-des, crypt-md5 and bcrypt algorithms.</para>
1337     </listitem>
1338     <listitem>
1339      <para>
1340       <ulink url="https://en.wikipedia.org/wiki/Fortuna_(PRNG)"></ulink>
1341      </para>
1342      <para>Description of Fortuna CSPRNG.</para>
1343     </listitem>
1344     <listitem>
1345      <para><ulink url="http://jlcooke.ca/random/"></ulink></para>
1346      <para>Jean-Luc Cooke Fortuna-based <filename>/dev/random</filename> driver for Linux.</para>
1347     </listitem>
1348    </itemizedlist>
1349   </sect3>
1350  </sect2>
1351
1352  <sect2>
1353   <title>Author</title>
1354
1355   <para>
1356    Marko Kreen <email>markokr@gmail.com</email>
1357   </para>
1358
1359   <para>
1360    <filename>pgcrypto</filename> uses code from the following sources:
1361   </para>
1362
1363   <informaltable>
1364    <tgroup cols="3">
1365     <thead>
1366      <row>
1367       <entry>Algorithm</entry>
1368       <entry>Author</entry>
1369       <entry>Source origin</entry>
1370      </row>
1371     </thead>
1372     <tbody>
1373      <row>
1374       <entry>DES crypt</entry>
1375       <entry>David Burren and others</entry>
1376       <entry>FreeBSD libcrypt</entry>
1377      </row>
1378      <row>
1379       <entry>MD5 crypt</entry>
1380       <entry>Poul-Henning Kamp</entry>
1381       <entry>FreeBSD libcrypt</entry>
1382      </row>
1383      <row>
1384       <entry>Blowfish crypt</entry>
1385       <entry>Solar Designer</entry>
1386       <entry>www.openwall.com</entry>
1387      </row>
1388      <row>
1389       <entry>Blowfish cipher</entry>
1390       <entry>Simon Tatham</entry>
1391       <entry>PuTTY</entry>
1392      </row>
1393      <row>
1394       <entry>Rijndael cipher</entry>
1395       <entry>Brian Gladman</entry>
1396       <entry>OpenBSD sys/crypto</entry>
1397      </row>
1398      <row>
1399       <entry>MD5 hash and SHA1</entry>
1400       <entry>WIDE Project</entry>
1401       <entry>KAME kame/sys/crypto</entry>
1402      </row>
1403      <row>
1404       <entry>SHA256/384/512 </entry>
1405       <entry>Aaron D. Gifford</entry>
1406       <entry>OpenBSD sys/crypto</entry>
1407      </row>
1408      <row>
1409       <entry>BIGNUM math</entry>
1410       <entry>Michael J. Fromberger</entry>
1411       <entry>dartmouth.edu/~sting/sw/imath</entry>
1412      </row>
1413     </tbody>
1414    </tgroup>
1415   </informaltable>
1416  </sect2>
1417
1418 </sect1>