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