1 <!-- doc/src/sgml/pgcrypto.sgml -->
3 <sect1 id=
"pgcrypto" xreflabel=
"pgcrypto">
4 <title>pgcrypto
— cryptographic functions
</title>
6 <indexterm zone=
"pgcrypto">
7 <primary>pgcrypto
</primary>
10 <indexterm zone=
"pgcrypto">
11 <primary>encryption
</primary>
12 <secondary>for specific columns
</secondary>
16 The
<filename>pgcrypto
</filename> module provides cryptographic functions for
17 <productname>PostgreSQL
</productname>.
21 This module is considered
<quote>trusted
</quote>, that is, it can be
22 installed by non-superusers who have
<literal>CREATE
</literal> privilege
23 on the current database.
27 <filename>pgcrypto
</filename> requires OpenSSL and won't be installed if
28 OpenSSL support was not selected when PostgreSQL was built.
31 <sect2 id=
"pgcrypto-general-hashing-funcs">
32 <title>General Hashing Functions
</title>
34 <sect3 id=
"pgcrypto-general-hashing-funcs-digest">
35 <title><function>digest()
</function></title>
38 <primary>digest
</primary>
42 digest(data text, type text) returns bytea
43 digest(data bytea, type text) returns bytea
47 Computes a binary hash of the given
<parameter>data
</parameter>.
48 <parameter>type
</parameter> is the algorithm to use.
49 Standard algorithms are
<literal>md5
</literal>,
<literal>sha1
</literal>,
50 <literal>sha224
</literal>,
<literal>sha256
</literal>,
51 <literal>sha384
</literal> and
<literal>sha512
</literal>.
52 Moreover, any digest algorithm
<productname>OpenSSL
</productname> supports
53 is automatically picked up.
57 If you want the digest as a hexadecimal string, use
58 <function>encode()
</function> on the result. For example:
60 CREATE OR REPLACE FUNCTION sha1(bytea) returns text AS $$
61 SELECT encode(digest($
1, 'sha1'), 'hex')
62 $$ LANGUAGE SQL STRICT IMMUTABLE;
67 <sect3 id=
"pgcrypto-general-hashing-funcs-hmac">
68 <title><function>hmac()
</function></title>
71 <primary>hmac
</primary>
75 hmac(data text, key text, type text) returns bytea
76 hmac(data bytea, key bytea, type text) returns bytea
80 Calculates hashed MAC for
<parameter>data
</parameter> with key
<parameter>key
</parameter>.
81 <parameter>type
</parameter> is the same as in
<function>digest()
</function>.
85 This is similar to
<function>digest()
</function> but the hash can only be
86 recalculated knowing the key. This prevents the scenario of someone
87 altering data and also changing the hash to match.
91 If the key is larger than the hash block size it will first be hashed and
92 the result will be used as key.
97 <sect2 id=
"pgcrypto-password-hashing-funcs">
98 <title>Password Hashing Functions
</title>
101 The functions
<function>crypt()
</function> and
<function>gen_salt()
</function>
102 are specifically designed for hashing passwords.
103 <function>crypt()
</function> does the hashing and
<function>gen_salt()
</function>
104 prepares algorithm parameters for it.
108 The algorithms in
<function>crypt()
</function> differ from the usual
109 MD5 or SHA-
1 hashing algorithms in the following respects:
115 They are slow. As the amount of data is so small, this is the only
116 way to make brute-forcing passwords hard.
121 They use a random value, called the
<firstterm>salt
</firstterm>, so that users
122 having the same password will have different encrypted passwords.
123 This is also an additional defense against reversing the algorithm.
128 They include the algorithm type in the result, so passwords hashed with
129 different algorithms can co-exist.
134 Some of them are adaptive
— that means when computers get
135 faster, you can tune the algorithm to be slower, without
136 introducing incompatibility with existing passwords.
142 <xref linkend=
"pgcrypto-crypt-algorithms"/> lists the algorithms
143 supported by the
<function>crypt()
</function> function.
146 <table id=
"pgcrypto-crypt-algorithms">
147 <title>Supported Algorithms for
<function>crypt()
</function></title>
151 <entry>Algorithm
</entry>
152 <entry>Max Password Length
</entry>
153 <entry>Adaptive?
</entry>
154 <entry>Salt Bits
</entry>
155 <entry>Output Length
</entry>
156 <entry>Description
</entry>
161 <entry><literal>bf
</literal></entry>
166 <entry>Blowfish-based, variant
2a
</entry>
169 <entry><literal>md5
</literal></entry>
170 <entry>unlimited
</entry>
174 <entry>MD5-based crypt
</entry>
177 <entry><literal>xdes
</literal></entry>
182 <entry>Extended DES
</entry>
185 <entry><literal>des
</literal></entry>
190 <entry>Original UNIX crypt
</entry>
196 <sect3 id=
"pgcrypto-password-hashing-funcs-crypt">
197 <title><function>crypt()
</function></title>
200 <primary>crypt
</primary>
204 crypt(password text, salt text) returns text
208 Calculates a crypt(
3)-style hash of
<parameter>password
</parameter>.
209 When storing a new password, you need to use
210 <function>gen_salt()
</function> to generate a new
<parameter>salt
</parameter> value.
211 To check a password, pass the stored hash value as
<parameter>salt
</parameter>,
212 and test whether the result matches the stored value.
215 Example of setting a new password:
217 UPDATE ... SET pswhash = crypt('new password', gen_salt('md5'));
221 Example of authentication:
223 SELECT (pswhash = crypt('entered password', pswhash)) AS pswmatch FROM ... ;
225 This returns
<literal>true
</literal> if the entered password is correct.
229 <sect3 id=
"pgcrypto-password-hashing-funcs-gen-salt">
230 <title><function>gen_salt()
</function></title>
233 <primary>gen_salt
</primary>
237 gen_salt(type text [, iter_count integer ]) returns text
241 Generates a new random salt string for use in
<function>crypt()
</function>.
242 The salt string also tells
<function>crypt()
</function> which algorithm to use.
246 The
<parameter>type
</parameter> parameter specifies the hashing algorithm.
247 The accepted types are:
<literal>des
</literal>,
<literal>xdes
</literal>,
248 <literal>md5
</literal> and
<literal>bf
</literal>.
252 The
<parameter>iter_count
</parameter> parameter lets the user specify the iteration
253 count, for algorithms that have one.
254 The higher the count, the more time it takes to hash
255 the password and therefore the more time to break it. Although with
256 too high a count the time to calculate a hash may be several years
257 — which is somewhat impractical. If the
<parameter>iter_count
</parameter>
258 parameter is omitted, the default iteration count is used.
259 Allowed values for
<parameter>iter_count
</parameter> depend on the algorithm and
260 are shown in
<xref linkend=
"pgcrypto-icfc-table"/>.
263 <table id=
"pgcrypto-icfc-table">
264 <title>Iteration Counts for
<function>crypt()
</function></title>
268 <entry>Algorithm
</entry>
269 <entry>Default
</entry>
276 <entry><literal>xdes
</literal></entry>
279 <entry>16777215</entry>
282 <entry><literal>bf
</literal></entry>
292 For
<literal>xdes
</literal> there is an additional limitation that the
293 iteration count must be an odd number.
297 To pick an appropriate iteration count, consider that
298 the original DES crypt was designed to have the speed of
4 hashes per
299 second on the hardware of that time.
300 Slower than
4 hashes per second would probably dampen usability.
301 Faster than
100 hashes per second is probably too fast.
305 <xref linkend=
"pgcrypto-hash-speed-table"/> gives an overview of the relative slowness
306 of different hashing algorithms.
307 The table shows how much time it would take to try all
308 combinations of characters in an
8-character password, assuming
309 that the password contains either only lower case letters, or
310 upper- and lower-case letters and numbers.
311 In the
<literal>crypt-bf
</literal> entries, the number after a slash is
312 the
<parameter>iter_count
</parameter> parameter of
313 <function>gen_salt
</function>.
316 <table id=
"pgcrypto-hash-speed-table">
317 <title>Hash Algorithm Speeds
</title>
321 <entry>Algorithm
</entry>
322 <entry>Hashes/sec
</entry>
323 <entry>For
<literal>[a-z]
</literal></entry>
324 <entry>For
<literal>[A-Za-z0-
9]
</literal></entry>
325 <entry>Duration relative to
<literal>md5 hash
</literal></entry>
330 <entry><literal>crypt-bf/
8</literal></entry>
332 <entry>4 years
</entry>
333 <entry>3927 years
</entry>
337 <entry><literal>crypt-bf/
7</literal></entry>
339 <entry>2 years
</entry>
340 <entry>1929 years
</entry>
344 <entry><literal>crypt-bf/
6</literal></entry>
346 <entry>1 year
</entry>
347 <entry>982 years
</entry>
351 <entry><literal>crypt-bf/
5</literal></entry>
353 <entry>188 days
</entry>
354 <entry>521 years
</entry>
358 <entry><literal>crypt-md5
</literal></entry>
359 <entry>171584</entry>
360 <entry>15 days
</entry>
361 <entry>41 years
</entry>
365 <entry><literal>crypt-des
</literal></entry>
366 <entry>23221568</entry>
367 <entry>157.5 minutes
</entry>
368 <entry>108 days
</entry>
372 <entry><literal>sha1
</literal></entry>
373 <entry>37774272</entry>
374 <entry>90 minutes
</entry>
375 <entry>68 days
</entry>
379 <entry><literal>md5
</literal> (hash)
</entry>
380 <entry>150085504</entry>
381 <entry>22.5 minutes
</entry>
382 <entry>17 days
</entry>
396 The machine used is an Intel Mobile Core i3.
401 <literal>crypt-des
</literal> and
<literal>crypt-md5
</literal> algorithm numbers are
402 taken from John the Ripper v1.6
.38 <literal>-test
</literal> output.
407 <literal>md5 hash
</literal> numbers are from mdcrack
1.2.
412 <literal>sha1
</literal> numbers are from lcrack-
20031130-beta.
417 <literal>crypt-bf
</literal> numbers are taken using a simple program that
418 loops over
1000 8-character passwords. That way the speed
419 with different numbers of iterations can be shown. For reference:
<literal>john
420 -test
</literal> shows
13506 loops/sec for
<literal>crypt-bf/
5</literal>.
422 difference in results is in accordance with the fact that the
423 <literal>crypt-bf
</literal> implementation in
<filename>pgcrypto
</filename>
424 is the same one used in John the Ripper.)
430 Note that
<quote>try all combinations
</quote> is not a realistic exercise.
431 Usually password cracking is done with the help of dictionaries, which
432 contain both regular words and various mutations of them. So, even
433 somewhat word-like passwords could be cracked much faster than the above
434 numbers suggest, while a
6-character non-word-like password may escape
440 <sect2 id=
"pgcrypto-pgp-enc-funcs">
441 <title>PGP Encryption Functions
</title>
444 The functions here implement the encryption part of the OpenPGP
445 (
<ulink url=
"https://datatracker.ietf.org/doc/html/rfc4880">RFC
4880</ulink>)
446 standard. Supported are both symmetric-key and public-key encryption.
450 An encrypted PGP message consists of
2 parts, or
<firstterm>packets
</firstterm>:
455 Packet containing a session key
— either symmetric-key or public-key
461 Packet containing data encrypted with the session key.
467 When encrypting with a symmetric key (i.e., a password):
472 The given password is hashed using a String2Key (S2K) algorithm. This is
473 rather similar to
<function>crypt()
</function> algorithms
— purposefully
474 slow and with random salt
— but it produces a full-length binary
480 If a separate session key is requested, a new random key will be
481 generated. Otherwise the S2K key will be used directly as the session
487 If the S2K key is to be used directly, then only S2K settings will be put
488 into the session key packet. Otherwise the session key will be encrypted
489 with the S2K key and put into the session key packet.
495 When encrypting with a public key:
500 A new random session key is generated.
505 It is encrypted using the public key and put into the session key packet.
511 In either case the data to be encrypted is processed as follows:
516 Optional data-manipulation: compression, conversion to UTF-
8,
517 and/or conversion of line-endings.
522 The data is prefixed with a block of random bytes. This is equivalent
523 to using a random IV.
528 A SHA-
1 hash of the random prefix and data is appended.
533 All this is encrypted with the session key and placed in the data packet.
538 <sect3 id=
"pgcrypto-pgp-enc-funcs-pgp-sym-encrypt">
539 <title><function>pgp_sym_encrypt()
</function></title>
542 <primary>pgp_sym_encrypt
</primary>
546 <primary>pgp_sym_encrypt_bytea
</primary>
550 pgp_sym_encrypt(data text, psw text [, options text ]) returns bytea
551 pgp_sym_encrypt_bytea(data bytea, psw text [, options text ]) returns bytea
554 Encrypt
<parameter>data
</parameter> with a symmetric PGP key
<parameter>psw
</parameter>.
555 The
<parameter>options
</parameter> parameter can contain option settings,
560 <sect3 id=
"pgcrypto-pgp-enc-funcs-pgp-sym-decrypt">
561 <title><function>pgp_sym_decrypt()
</function></title>
564 <primary>pgp_sym_decrypt
</primary>
568 <primary>pgp_sym_decrypt_bytea
</primary>
572 pgp_sym_decrypt(msg bytea, psw text [, options text ]) returns text
573 pgp_sym_decrypt_bytea(msg bytea, psw text [, options text ]) returns bytea
576 Decrypt a symmetric-key-encrypted PGP message.
579 Decrypting
<type>bytea
</type> data with
<function>pgp_sym_decrypt
</function> is disallowed.
580 This is to avoid outputting invalid character data. Decrypting
581 originally textual data with
<function>pgp_sym_decrypt_bytea
</function> is fine.
584 The
<parameter>options
</parameter> parameter can contain option settings,
589 <sect3 id=
"pgcrypto-pgp-enc-funcs-pgp-pub-encrypt">
590 <title><function>pgp_pub_encrypt()
</function></title>
593 <primary>pgp_pub_encrypt
</primary>
597 <primary>pgp_pub_encrypt_bytea
</primary>
601 pgp_pub_encrypt(data text, key bytea [, options text ]) returns bytea
602 pgp_pub_encrypt_bytea(data bytea, key bytea [, options text ]) returns bytea
605 Encrypt
<parameter>data
</parameter> with a public PGP key
<parameter>key
</parameter>.
606 Giving this function a secret key will produce an error.
609 The
<parameter>options
</parameter> parameter can contain option settings,
614 <sect3 id=
"pgcrypto-pgp-enc-funcs-pgp-pub-decrypt">
615 <title><function>pgp_pub_decrypt()
</function></title>
618 <primary>pgp_pub_decrypt
</primary>
622 <primary>pgp_pub_decrypt_bytea
</primary>
626 pgp_pub_decrypt(msg bytea, key bytea [, psw text [, options text ]]) returns text
627 pgp_pub_decrypt_bytea(msg bytea, key bytea [, psw text [, options text ]]) returns bytea
630 Decrypt a public-key-encrypted message.
<parameter>key
</parameter> must be the
631 secret key corresponding to the public key that was used to encrypt.
632 If the secret key is password-protected, you must give the password in
633 <parameter>psw
</parameter>. If there is no password, but you want to specify
634 options, you need to give an empty password.
637 Decrypting
<type>bytea
</type> data with
<function>pgp_pub_decrypt
</function> is disallowed.
638 This is to avoid outputting invalid character data. Decrypting
639 originally textual data with
<function>pgp_pub_decrypt_bytea
</function> is fine.
642 The
<parameter>options
</parameter> parameter can contain option settings,
647 <sect3 id=
"pgcrypto-pgp-enc-funcs-pgp-key-id">
648 <title><function>pgp_key_id()
</function></title>
651 <primary>pgp_key_id
</primary>
655 pgp_key_id(bytea) returns text
658 <function>pgp_key_id
</function> extracts the key ID of a PGP public or secret key.
659 Or it gives the key ID that was used for encrypting the data, if given
660 an encrypted message.
663 It can return
2 special key IDs:
668 <literal>SYMKEY
</literal>
671 The message is encrypted with a symmetric key.
676 <literal>ANYKEY
</literal>
679 The message is public-key encrypted, but the key ID has been removed.
680 That means you will need to try all your secret keys on it to see
681 which one decrypts it.
<filename>pgcrypto
</filename> itself does not produce
687 Note that different keys may have the same ID. This is rare but a normal
688 event. The client application should then try to decrypt with each one,
689 to see which fits
— like handling
<literal>ANYKEY
</literal>.
693 <sect3 id=
"pgcrypto-pgp-enc-funcs-armor">
694 <title><function>armor()
</function>,
<function>dearmor()
</function></title>
697 <primary>armor
</primary>
701 <primary>dearmor
</primary>
705 armor(data bytea [ , keys text[], values text[] ]) returns text
706 dearmor(data text) returns bytea
709 These functions wrap/unwrap binary data into PGP ASCII-armor format,
710 which is basically Base64 with CRC and additional formatting.
714 If the
<parameter>keys
</parameter> and
<parameter>values
</parameter> arrays are specified,
715 an
<firstterm>armor header
</firstterm> is added to the armored format for each
716 key/value pair. Both arrays must be single-dimensional, and they must
717 be of the same length. The keys and values cannot contain any non-ASCII
722 <sect3 id=
"pgcrypto-pgp-enc-funcs-pgp-armor-headers">
723 <title><function>pgp_armor_headers
</function></title>
726 <primary>pgp_armor_headers
</primary>
730 pgp_armor_headers(data text, key out text, value out text) returns setof record
733 <function>pgp_armor_headers()
</function> extracts the armor headers from
734 <parameter>data
</parameter>. The return value is a set of rows with two columns,
735 key and value. If the keys or values contain any non-ASCII characters,
736 they are treated as UTF-
8.
740 <sect3 id=
"pgcrypto-pgp-enc-funcs-opts">
741 <title>Options for PGP Functions
</title>
744 Options are named to be similar to GnuPG. An option's value should be
745 given after an equal sign; separate options from each other with commas.
748 pgp_sym_encrypt(data, psw, 'compress-algo=
1, cipher-algo=aes256')
753 All of the options except
<literal>convert-crlf
</literal> apply only to
754 encrypt functions. Decrypt functions get the parameters from the PGP
759 The most interesting options are probably
760 <literal>compress-algo
</literal> and
<literal>unicode-mode
</literal>.
761 The rest should have reasonable defaults.
764 <sect4 id=
"pgcrypto-pgp-enc-funcs-opts-cipher-algo">
765 <title>cipher-algo
</title>
768 Which cipher algorithm to use.
771 Values: bf, aes128, aes192, aes256,
3des, cast5
773 Applies to: pgp_sym_encrypt, pgp_pub_encrypt
777 <sect4 id=
"pgcrypto-pgp-enc-funcs-opts-compress-algo">
778 <title>compress-algo
</title>
781 Which compression algorithm to use. Only available if
782 <productname>PostgreSQL
</productname> was built with zlib.
788 2 - ZLIB compression (= ZIP plus meta-data and block CRCs)
790 Applies to: pgp_sym_encrypt, pgp_pub_encrypt
794 <sect4 id=
"pgcrypto-pgp-enc-funcs-opts-compress-level">
795 <title>compress-level
</title>
798 How much to compress. Higher levels compress smaller but are slower.
799 0 disables compression.
804 Applies to: pgp_sym_encrypt, pgp_pub_encrypt
808 <sect4 id=
"pgcrypto-pgp-enc-funcs-opts-convert-crlf">
809 <title>convert-crlf
</title>
812 Whether to convert
<literal>\n
</literal> into
<literal>\r\n
</literal> when
813 encrypting and
<literal>\r\n
</literal> to
<literal>\n
</literal> when
814 decrypting.
<acronym>RFC
</acronym> 4880 specifies that text data should be stored using
815 <literal>\r\n
</literal> line-feeds. Use this to get fully RFC-compliant
821 Applies to: pgp_sym_encrypt, pgp_pub_encrypt, pgp_sym_decrypt, pgp_pub_decrypt
825 <sect4 id=
"pgcrypto-pgp-enc-funcs-opts-disable-mdc">
826 <title>disable-mdc
</title>
829 Do not protect data with SHA-
1. The only good reason to use this
830 option is to achieve compatibility with ancient PGP products, predating
831 the addition of SHA-
1 protected packets to
<acronym>RFC
</acronym> 4880.
832 Recent gnupg.org and pgp.com software supports it fine.
837 Applies to: pgp_sym_encrypt, pgp_pub_encrypt
841 <sect4 id=
"pgcrypto-pgp-enc-funcs-opts-sess-key">
842 <title>sess-key
</title>
845 Use separate session key. Public-key encryption always uses a separate
846 session key; this option is for symmetric-key encryption, which by default
847 uses the S2K key directly.
852 Applies to: pgp_sym_encrypt
856 <sect4 id=
"pgcrypto-pgp-enc-funcs-opts-s2k-mode">
857 <title>s2k-mode
</title>
860 Which S2K algorithm to use.
864 0 - Without salt. Dangerous!
865 1 - With salt but with fixed iteration count.
866 3 - Variable iteration count.
868 Applies to: pgp_sym_encrypt
872 <sect4 id=
"pgcrypto-pgp-enc-funcs-opts-s2k-count">
873 <title>s2k-count
</title>
876 The number of iterations of the S2K algorithm to use. It must
877 be a value between
1024 and
65011712, inclusive.
880 Default: A random value between
65536 and
253952
881 Applies to: pgp_sym_encrypt, only with s2k-mode=
3
885 <sect4 id=
"pgcrypto-pgp-enc-funcs-opts-s2k-digest-algo">
886 <title>s2k-digest-algo
</title>
889 Which digest algorithm to use in S2K calculation.
894 Applies to: pgp_sym_encrypt
898 <sect4 id=
"pgcrypto-pgp-enc-funcs-opts-s2k-cipher-algo">
899 <title>s2k-cipher-algo
</title>
902 Which cipher to use for encrypting separate session key.
905 Values: bf, aes, aes128, aes192, aes256
906 Default: use cipher-algo
907 Applies to: pgp_sym_encrypt
911 <sect4 id=
"pgcrypto-pgp-enc-funcs-opts-unicode-mode">
912 <title>unicode-mode
</title>
915 Whether to convert textual data from database internal encoding to
916 UTF-
8 and back. If your database already is UTF-
8, no conversion will
917 be done, but the message will be tagged as UTF-
8. Without this option
923 Applies to: pgp_sym_encrypt, pgp_pub_encrypt
928 <sect3 id=
"pgcrypto-pgp-enc-funcs-gnupg">
929 <title>Generating PGP Keys with GnuPG
</title>
932 To generate a new key:
938 The preferred key type is
<quote>DSA and Elgamal
</quote>.
941 For RSA encryption you must create either DSA or RSA sign-only key
942 as master and then add an RSA encryption subkey with
943 <literal>gpg --edit-key
</literal>.
948 gpg --list-secret-keys
952 To export a public key in ASCII-armor format:
954 gpg -a --export KEYID
> public.key
958 To export a secret key in ASCII-armor format:
960 gpg -a --export-secret-keys KEYID
> secret.key
964 You need to use
<function>dearmor()
</function> on these keys before giving them to
965 the PGP functions. Or if you can handle binary data, you can drop
966 <literal>-a
</literal> from the command.
969 For more details see
<literal>man gpg
</literal>,
970 <ulink url=
"https://www.gnupg.org/gph/en/manual.html">The GNU
971 Privacy Handbook
</ulink> and other documentation on
972 <ulink url=
"https://www.gnupg.org/"></ulink>.
976 <sect3 id=
"pgcrypto-pgp-enc-funcs-limitations">
977 <title>Limitations of PGP Code
</title>
982 No support for signing. That also means that it is not checked
983 whether the encryption subkey belongs to the master key.
988 No support for encryption key as master key. As such practice
989 is generally discouraged, this should not be a problem.
994 No support for several subkeys. This may seem like a problem, as this
995 is common practice. On the other hand, you should not use your regular
996 GPG/PGP keys with
<filename>pgcrypto
</filename>, but create new ones,
997 as the usage scenario is rather different.
1004 <sect2 id=
"pgcrypto-raw-enc-funcs">
1005 <title>Raw Encryption Functions
</title>
1008 These functions only run a cipher over data; they don't have any advanced
1009 features of PGP encryption. Therefore they have some major problems:
1014 They use user key directly as cipher key.
1019 They don't provide any integrity checking, to see
1020 if the encrypted data was modified.
1025 They expect that users manage all encryption parameters
1026 themselves, even IV.
1031 They don't handle text.
1036 So, with the introduction of PGP encryption, usage of raw
1037 encryption functions is discouraged.
1041 <primary>encrypt
</primary>
1045 <primary>decrypt
</primary>
1049 <primary>encrypt_iv
</primary>
1053 <primary>decrypt_iv
</primary>
1057 encrypt(data bytea, key bytea, type text) returns bytea
1058 decrypt(data bytea, key bytea, type text) returns bytea
1060 encrypt_iv(data bytea, key bytea, iv bytea, type text) returns bytea
1061 decrypt_iv(data bytea, key bytea, iv bytea, type text) returns bytea
1065 Encrypt/decrypt data using the cipher method specified by
1066 <parameter>type
</parameter>. The syntax of the
1067 <parameter>type
</parameter> string is:
1070 <replaceable>algorithm
</replaceable> <optional> <literal>-
</literal> <replaceable>mode
</replaceable> </optional> <optional> <literal>/pad:
</literal> <replaceable>padding
</replaceable> </optional>
1072 where
<replaceable>algorithm
</replaceable> is one of:
1075 <listitem><para><literal>bf
</literal> — Blowfish
</para></listitem>
1076 <listitem><para><literal>aes
</literal> — AES (Rijndael-
128, -
192 or -
256)
</para></listitem>
1078 and
<replaceable>mode
</replaceable> is one of:
1082 <literal>cbc
</literal> — next block depends on previous (default)
1087 <literal>ecb
</literal> — each block is encrypted separately (for
1092 and
<replaceable>padding
</replaceable> is one of:
1096 <literal>pkcs
</literal> — data may be any length (default)
1101 <literal>none
</literal> — data must be multiple of cipher block size
1107 So, for example, these are equivalent:
1109 encrypt(data, 'fooz', 'bf')
1110 encrypt(data, 'fooz', 'bf-cbc/pad:pkcs')
1114 In
<function>encrypt_iv
</function> and
<function>decrypt_iv
</function>, the
1115 <parameter>iv
</parameter> parameter is the initial value for the CBC mode;
1116 it is ignored for ECB.
1117 It is clipped or padded with zeroes if not exactly block size.
1118 It defaults to all zeroes in the functions without this parameter.
1122 <sect2 id=
"pgcrypto-random-data-funcs">
1123 <title>Random-Data Functions
</title>
1126 <primary>gen_random_bytes
</primary>
1130 gen_random_bytes(count integer) returns bytea
1133 Returns
<parameter>count
</parameter> cryptographically strong random bytes.
1134 At most
1024 bytes can be extracted at a time. This is to avoid
1135 draining the randomness generator pool.
1139 <primary>gen_random_uuid
</primary>
1143 gen_random_uuid() returns uuid
1146 Returns a version
4 (random) UUID. (Obsolete, this function
1147 internally calls the
<link linkend=
"functions-uuid">core
1148 function
</link> of the same name.)
1152 <sect2 id=
"pgcrypto-notes">
1153 <title>Notes
</title>
1155 <sect3 id=
"pgcrypto-notes-config">
1156 <title>Configuration
</title>
1159 <filename>pgcrypto
</filename> configures itself according to the findings of the
1160 main PostgreSQL
<literal>configure
</literal> script. The options that
1161 affect it are
<literal>--with-zlib
</literal> and
1162 <literal>--with-ssl=openssl
</literal>.
1166 When compiled with zlib, PGP encryption functions are able to
1167 compress data before encrypting.
1171 <filename>pgcrypto
</filename> requires
<productname>OpenSSL
</productname>.
1172 Otherwise, it will not be built or installed.
1176 When compiled against
<productname>OpenSSL
</productname> 3.0.0 and later
1177 versions, the legacy provider must be activated in the
1178 <filename>openssl.cnf
</filename> configuration file in order to use older
1179 ciphers like DES or Blowfish.
1183 <sect3 id=
"pgcrypto-notes-null-handling">
1184 <title>NULL Handling
</title>
1187 As is standard in SQL, all functions return NULL, if any of the arguments
1188 are NULL. This may create security risks on careless usage.
1192 <sect3 id=
"pgcrypto-notes-sec-limits">
1193 <title>Security Limitations
</title>
1196 All
<filename>pgcrypto
</filename> functions run inside the database server.
1198 the data and passwords move between
<filename>pgcrypto
</filename> and client
1199 applications in clear text. Thus you must:
1204 <para>Connect locally or use SSL connections.
</para>
1207 <para>Trust both system and database administrator.
</para>
1212 If you cannot, then better do crypto inside client application.
1216 The implementation does not resist
1217 <ulink url=
"https://en.wikipedia.org/wiki/Side-channel_attack">side-channel
1218 attacks
</ulink>. For example, the time required for
1219 a
<filename>pgcrypto
</filename> decryption function to complete varies among
1220 ciphertexts of a given size.
1225 <sect2 id=
"pgcrypto-author">
1226 <title>Author
</title>
1229 Marko Kreen
<email>markokr@gmail.com
</email>
1233 <filename>pgcrypto
</filename> uses code from the following sources:
1240 <entry>Algorithm
</entry>
1241 <entry>Author
</entry>
1242 <entry>Source origin
</entry>
1247 <entry>DES crypt
</entry>
1248 <entry>David Burren and others
</entry>
1249 <entry>FreeBSD libcrypt
</entry>
1252 <entry>MD5 crypt
</entry>
1253 <entry>Poul-Henning Kamp
</entry>
1254 <entry>FreeBSD libcrypt
</entry>
1257 <entry>Blowfish crypt
</entry>
1258 <entry>Solar Designer
</entry>
1259 <entry>www.openwall.com
</entry>