バージョンごとのドキュメント一覧

F.28. pgcrypto — 暗号関数 #

<title>pgcrypto &mdash; cryptographic functions</title>

The <filename>pgcrypto</filename> module provides cryptographic functions for <productname>PostgreSQL</productname>. pgcryptoモジュールはPostgreSQL用の暗号関数を提供します。

This module is considered <quote>trusted</quote>, that is, it can be installed by non-superusers who have <literal>CREATE</literal> privilege on the current database. このモジュールはtrustedと見なされます。つまり、現在のデータベースに対してCREATE権限を持つ非スーパーユーザがインストールできます。

<filename>pgcrypto</filename> requires OpenSSL and won't be installed if OpenSSL support was not selected when PostgreSQL was built. pgcryptoはOpenSSLを必要とし、PostgreSQLの構築時にOpenSSLサポートが選択されなかった場合にはインストールされません。

F.28.1. 汎用ハッシュ関数 #

<title>General Hashing Functions</title>

F.28.1.1. digest() #

digest(data text, type text) returns bytea
digest(data bytea, type text) returns bytea

Computes a binary hash of the given <parameter>data</parameter>. <parameter>type</parameter> is the algorithm to use. Standard algorithms are <literal>md5</literal>, <literal>sha1</literal>, <literal>sha224</literal>, <literal>sha256</literal>, <literal>sha384</literal> and <literal>sha512</literal>. Moreover, any digest algorithm <productname>OpenSSL</productname> supports is automatically picked up. 与えられたdataのバイナリハッシュを計算します。 typeは使用するアルゴリズムです。 標準アルゴリズムはmd5sha1sha224sha256sha384、およびsha512です。 さらに、OpenSSLがサポートするダイジェストアルゴリズムが自動的に選択されます。

If you want the digest as a hexadecimal string, use <function>encode()</function> on the result. For example: ダイジェストを16進数表記の文字列としたい場合は、結果に対してencode()を使用してください。 以下に例を示します。

CREATE OR REPLACE FUNCTION sha1(bytea) returns text AS $$
    SELECT encode(digest($1, 'sha1'), 'hex')
$$ LANGUAGE SQL STRICT IMMUTABLE;

F.28.1.2. hmac() #

hmac(data text, key text, type text) returns bytea
hmac(data bytea, key bytea, type text) returns bytea

Calculates hashed MAC for <parameter>data</parameter> with key <parameter>key</parameter>. <parameter>type</parameter> is the same as in <function>digest()</function>. keyをキーとしたdataのハッシュ化MACを計算します。 typedigest()の場合と同じです。

This is similar to <function>digest()</function> but the hash can only be recalculated knowing the key. This prevents the scenario of someone altering data and also changing the hash to match. digest()と似ていますが、ハッシュはキーを知っている場合にのみ再計算できます。 これは、誰かがデータを変更し、同時に一致するようにハッシュを変更するという状況を防ぎます。

If the key is larger than the hash block size it will first be hashed and the result will be used as key. キーがハッシュブロックサイズより大きい場合、まずハッシュ化され、その結果をキーとして使用します。

F.28.2. パスワードハッシュ化関数 #

<title>Password Hashing Functions</title>

The functions <function>crypt()</function> and <function>gen_salt()</function> are specifically designed for hashing passwords. <function>crypt()</function> does the hashing and <function>gen_salt()</function> prepares algorithm parameters for it. crypt()およびgen_salt()関数は特にパスワードのハッシュ化のために設計されたものです。 crypt()がハッシュ処理を行い、gen_salt()はハッシュ処理用のアルゴリズム上のパラメータを準備します。

The algorithms in <function>crypt()</function> differ from the usual MD5 or SHA1 hashing algorithms in the following respects: crypt()アルゴリズムは、以下の点で通常のMD5やSHA1のようなハッシュ処理アルゴリズムと異なります。

  1. They are slow. As the amount of data is so small, this is the only way to make brute-forcing passwords hard. 低速です。 データ量が少ないためパスワード総当たり攻撃に対して頑健にする唯一の方法です。

  2. They use a random value, called the <firstterm>salt</firstterm>, so that users having the same password will have different encrypted passwords. This is also an additional defense against reversing the algorithm. 結果にはソルトというランダムな値が含まれます。 このため同じパスワードのユーザでも異なった暗号化パスワードを持ちます。 これはアルゴリズムの逆処理に対する追加の防御です。

  3. They include the algorithm type in the result, so passwords hashed with different algorithms can co-exist. 結果内にアルゴリズムの種類が含まれます。 このため異なるアルゴリズムでハッシュ化したパスワードが混在可能です。

  4. Some of them are adaptive &mdash; that means when computers get faster, you can tune the algorithm to be slower, without introducing incompatibility with existing passwords. 一部は適応型です。 つまり、コンピュータが高速になったとしても、既存のパスワードとの互換性を損なうことなくアルゴリズムを低速に調整することができます。

<xref linkend="pgcrypto-crypt-algorithms"/> lists the algorithms supported by the <function>crypt()</function> function. crypt()関数がサポートするアルゴリズムを表 F.18に列挙します。

表F.18 crypt()がサポートするアルゴリズム

<title>Supported Algorithms for <function>crypt()</function></title>
アルゴリズムパスワード最大長適応型かどうかソルトビット長出力長説明
bf72はい12860Blowfishベース、2a版
md5無制限いいえ4834MD5ベースの暗号
xdes8はい2420拡張DES
des8いいえ1213元来のUNIX crypt

F.28.2.1. crypt() #

crypt(password text, salt text) returns text

Calculates a crypt(3)-style hash of <parameter>password</parameter>. When storing a new password, you need to use <function>gen_salt()</function> to generate a new <parameter>salt</parameter> value. To check a password, pass the stored hash value as <parameter>salt</parameter>, and test whether the result matches the stored value. passwordのcrypt(3)形式のハッシュを計算します。 新しいパスワードを保管する時には、gen_salt()を使用して新しいsaltを生成する必要があります。 パスワードを検査する時、既存のハッシュ値をsaltとして渡し、結果が格納された値と一致するかどうかを確認します。

Example of setting a new password: 新しいパスワードの設定例を以下に示します。

UPDATE ... SET pswhash = crypt('new password', gen_salt('md5'));

Example of authentication: 認証の例です。

SELECT (pswhash = crypt('entered password', pswhash)) AS pswmatch FROM ... ;

This returns <literal>true</literal> if the entered password is correct. 入力パスワードが正しければtrueを返します。

F.28.2.2. gen_salt() #

gen_salt(type text [, iter_count integer ]) returns text

Generates a new random salt string for use in <function>crypt()</function>. The salt string also tells <function>crypt()</function> which algorithm to use. crypt()で使用するランダムなソルト文字列を新規に生成します。 また、このソルト文字列はcrypt()にどのアルゴリズムを使用するかを通知します。

The <parameter>type</parameter> parameter specifies the hashing algorithm. The accepted types are: <literal>des</literal>, <literal>xdes</literal>, <literal>md5</literal> and <literal>bf</literal>. typeパラメータはハッシュ化アルゴリズムを指定します。 受付可能な種類は、desxdesmd5bfです。

The <parameter>iter_count</parameter> parameter lets the user specify the iteration count, for algorithms that have one. The higher the count, the more time it takes to hash the password and therefore the more time to break it. Although with too high a count the time to calculate a hash may be several years &mdash; which is somewhat impractical. If the <parameter>iter_count</parameter> parameter is omitted, the default iteration count is used. Allowed values for <parameter>iter_count</parameter> depend on the algorithm and are shown in <xref linkend="pgcrypto-icfc-table"/>. 繰り返し回数を持つアルゴリズムでは、ユーザはiter_countパラメータを使用して繰り返し回数を指定できます。 指定する回数を高くすれば、パスワードのハッシュ化にかかる時間が長くなり、それを破るための時間も長くなります。 しかし、あまりに多くの回数を指定すると、ハッシュ計算にかかる時間は数年に渡ってしまう可能性があります。 これは実用的ではありません。 iter_countパラメータを省略した場合、デフォルトの繰り返し回数が使用されます。 iter_countで受け付けられる値はアルゴリズムに依存し、表 F.19に示す通りです。

表F.19 crypt()用の繰り返し回数

<title>Iteration Counts for <function>crypt()</function></title>
アルゴリズムデフォルト最小最大
xdes725116777215
bf6431

For <literal>xdes</literal> there is an additional limitation that the iteration count must be an odd number. xdesの場合は他にも、回数が奇数でなければならないという制限があります。

To pick an appropriate iteration count, consider that the original DES crypt was designed to have the speed of 4 hashes per second on the hardware of that time. Slower than 4 hashes per second would probably dampen usability. Faster than 100 hashes per second is probably too fast. 適切な繰り返し回数を選択するために、元々のDES暗号は当時のハードウェアで1秒あたり4個のハッシュを持つことができるように設計されたことを考えてください。 毎秒4ハッシュより遅いと、おそらく使い勝手が悪いでしょう。 毎秒100ハッシュより速いというのは、十中八九、あまりにも速すぎるでしょう。

<xref linkend="pgcrypto-hash-speed-table"/> gives an overview of the relative slowness of different hashing algorithms. The table shows how much time it would take to try all combinations of characters in an 8-character password, assuming that the password contains either only lower case letters, or upper- and lower-case letters and numbers. In the <literal>crypt-bf</literal> entries, the number after a slash is the <parameter>iter_count</parameter> parameter of <function>gen_salt</function>. ハッシュ化アルゴリズム別に相対的な速度に関する概要を表 F.20にまとめました。 この表は、8文字のパスワード内のすべての文字の組合せを取るためにかかる時間を示します。 また、すべて小文字の英字のみのパスワードである場合と大文字小文字が混在した英字と数字のパスワードの場合を仮定します。 crypt-bfの項では、スラッシュの後の数値はgen_saltiter_countです。

表F.20 ハッシュアルゴリズムの速度

<title>Hash Algorithm Speeds</title>
アルゴリズム1秒当たりのハッシュ数[a-z]の場合[A-Za-z0-9]の場合md5ハッシュを単位とした持続期間
crypt-bf/817924年3927年100k
crypt-bf/736482年1929年50k
crypt-bf/671681年982年25k
crypt-bf/513504188日521年12.5k
crypt-md517158415日41年1k
crypt-des23221568157.5分108日7
sha13777427290分68日4
md5(ハッシュ)15008550422.5分17日1

Notes: 注意:

  • The machine used is an Intel Mobile Core i3. Intel Mobile Core i3のマシンを使用しました。

  • <literal>crypt-des</literal> and <literal>crypt-md5</literal> algorithm numbers are taken from John the Ripper v1.6.38 <literal>-test</literal> output. crypt-desおよびcrypt-md5アルゴリズムの数値はJohn the Ripper v1.6.38の-test出力から得たものです。

  • <literal>md5 hash</literal> numbers are from mdcrack 1.2. md5ハッシュの数値はmdcrack 1.2のものです。

  • <literal>sha1</literal> numbers are from lcrack-20031130-beta. sha1の数値はlcrack-20031130-betaのものです。

  • <literal>crypt-bf</literal> numbers are taken using a simple program that loops over 1000 8-character passwords. That way the speed with different numbers of iterations can be shown. For reference: <literal>john -test</literal> shows 13506 loops/sec for <literal>crypt-bf/5</literal>. (The very small difference in results is in accordance with the fact that the <literal>crypt-bf</literal> implementation in <filename>pgcrypto</filename> is the same one used in John the Ripper.) crypt-bfの数は、1000個の8文字パスワードをループする単純なプログラムを使用して得たものです。 こうして、異なる回数の速度を示すことができました。 参考までに、john -testcrypt-bf/5で13506 loops/secでした。 (結果の差異が非常に小さいことは、pgcryptoにおけるcrypt-bf実装がJohn the Ripperで使用されるものと同じであるという事実と一致します。)

Note that <quote>try all combinations</quote> is not a realistic exercise. Usually password cracking is done with the help of dictionaries, which contain both regular words and various mutations of them. So, even somewhat word-like passwords could be cracked much faster than the above numbers suggest, while a 6-character non-word-like password may escape cracking. Or not. すべての組み合わせを試行することは現実的な行使ではありません。 通常パスワード推定は、普通の単語とその変形の両方を含む辞書を使用して行われます。 ですので、いささかなりとも言葉に似たパスワードは上で示した数値よりも速く推定されます。 また6文字の単語に似ていないパスワードは推定を免れるかもしれませんし、免れないかもしれません。

F.28.3. PGP暗号化関数 #

<title>PGP Encryption Functions</title>

The functions here implement the encryption part of the OpenPGP (<ulink url="https://tools.ietf.org/html/rfc4880">RFC 4880</ulink>) standard. Supported are both symmetric-key and public-key encryption. ここで示す関数はOpenPGP(RFC 4880)標準の暗号処理部分を実装します。 対称鍵および公開鍵暗号化がサポートされます。

An encrypted PGP message consists of 2 parts, or <firstterm>packets</firstterm>: 暗号化されたPGPメッセージは次の2つの部品(またはパケット)から構成されます。

  • Packet containing a session key &mdash; either symmetric-key or public-key encrypted. セッションキーを含むパケット。 対称鍵または公開鍵で暗号化されています。

  • Packet containing data encrypted with the session key. セッションキーにより暗号化されたデータを含むパケット。

When encrypting with a symmetric key (i.e., a password): 対称鍵(つまりパスワード)で暗号化する場合

  1. The given password is hashed using a String2Key (S2K) algorithm. This is rather similar to <function>crypt()</function> algorithms &mdash; purposefully slow and with random salt &mdash; but it produces a full-length binary key. 与えられたパスワードはString2Key(S2K)アルゴリズムでハッシュ化されます。 これはどちらかというとcrypt()アルゴリズムと似て、意図的に低速で、かつランダムなソルトを使用します。 しかし、全長のバイナリキーを生成します。

  2. If a separate session key is requested, a new random key will be generated. Otherwise the S2K key will be used directly as the session key. 分離したセッションキーが要求された場合、新しいランダムなキーが生成されます。 さもなくば、S2Kキーがそのままセッションキーとして使用されます。

  3. If the S2K key is to be used directly, then only S2K settings will be put into the session key packet. Otherwise the session key will be encrypted with the S2K key and put into the session key packet. S2Kキーがそのまま使用される場合、S2K設定のみがセッションキーパケットに格納されます。 さもなくば、セッションキーはS2Kキーで暗号化され、セッションキーパケットに格納されます。

When encrypting with a public key: 公開鍵で暗号化する場合

  1. A new random session key is generated. 新しいランダムなセッションキーが生成されます。

  2. It is encrypted using the public key and put into the session key packet. これは公開鍵を使用して暗号化され、セッションキーパケットに格納されます。

In either case the data to be encrypted is processed as follows: どちらの場合でもデータ暗号化は以下のように処理されます。

  1. Optional data-manipulation: compression, conversion to UTF-8, and/or conversion of line-endings. 省略可能なデータ操作として、圧縮、UTF-8への変換、改行の変換があります。

  2. The data is prefixed with a block of random bytes. This is equivalent to using a random IV. データの前にはランダムなバイト数のブロックが付きます。 これはrandom IVを使用する場合と同じです。

  3. A SHA1 hash of the random prefix and data is appended. ランダムな前置ブロックとデータのSHA1ハッシュが後に付けられます。

  4. All this is encrypted with the session key and placed in the data packet. これをすべてセッションキーで暗号化し、データパケットに格納します。

F.28.3.1. pgp_sym_encrypt() #

pgp_sym_encrypt(data text, psw text [, options text ]) returns bytea
pgp_sym_encrypt_bytea(data bytea, psw text [, options text ]) returns bytea

Encrypt <parameter>data</parameter> with a symmetric PGP key <parameter>psw</parameter>. The <parameter>options</parameter> parameter can contain option settings, as described below. 対称PGPキーpswdataを暗号化します。 optionsパラメータには後述のオプション設定を含めることができます。

F.28.3.2. pgp_sym_decrypt() #

pgp_sym_decrypt(msg bytea, psw text [, options text ]) returns text
pgp_sym_decrypt_bytea(msg bytea, psw text [, options text ]) returns bytea

Decrypt a symmetric-key-encrypted PGP message. 対称鍵で暗号化されたPGPメッセージを復号します。

Decrypting <type>bytea</type> data with <function>pgp_sym_decrypt</function> is disallowed. This is to avoid outputting invalid character data. Decrypting originally textual data with <function>pgp_sym_decrypt_bytea</function> is fine. pgp_sym_decryptbytea型のデータを復号することはできません。 これは無効な文字データの出力を防止するためです。 元のテキストのデータをpgp_sym_decrypt_byteaで復号することが正しい方法です。

The <parameter>options</parameter> parameter can contain option settings, as described below. optionsパラメータには後述のオプション設定を含めることができます。

F.28.3.3. pgp_pub_encrypt() #

pgp_pub_encrypt(data text, key bytea [, options text ]) returns bytea
pgp_pub_encrypt_bytea(data bytea, key bytea [, options text ]) returns bytea

Encrypt <parameter>data</parameter> with a public PGP key <parameter>key</parameter>. Giving this function a secret key will produce an error. 公開PGPキーkeydataを暗号化します。 この関数に秘密キーを与えるとエラーになります。

The <parameter>options</parameter> parameter can contain option settings, as described below. optionsパラメータには後述のオプション設定を含めることができます。

F.28.3.4. pgp_pub_decrypt() #

pgp_pub_decrypt(msg bytea, key bytea [, psw text [, options text ]]) returns text
pgp_pub_decrypt_bytea(msg bytea, key bytea [, psw text [, options text ]]) returns bytea

Decrypt a public-key-encrypted message. <parameter>key</parameter> must be the secret key corresponding to the public key that was used to encrypt. If the secret key is password-protected, you must give the password in <parameter>psw</parameter>. If there is no password, but you want to specify options, you need to give an empty password. 公開鍵で暗号化されたメッセージを復号します。 keyは、暗号化に使用した公開鍵に対応する秘密鍵でなければなりません。 秘密鍵がパスワードで保護されている場合は、そのパスワードをpswで指定しなければなりません。 パスワードはないが、オプションを指定したい場合は空のパスワードを指定する必要があります。

Decrypting <type>bytea</type> data with <function>pgp_pub_decrypt</function> is disallowed. This is to avoid outputting invalid character data. Decrypting originally textual data with <function>pgp_pub_decrypt_bytea</function> is fine. pgp_pub_decryptbytea型のデータを復号することはできません。 これは無効な文字データの出力を防止するためです。 元のテキストのデータをpgp_pub_decrypt_byteaで復号することが正しい方法です。

The <parameter>options</parameter> parameter can contain option settings, as described below. optionsパラメータには後述のオプション設定を含めることができます。

F.28.3.5. pgp_key_id() #

pgp_key_id(bytea) returns text

<function>pgp_key_id</function> extracts the key ID of a PGP public or secret key. Or it gives the key ID that was used for encrypting the data, if given an encrypted message. pgp_key_idはPGP公開鍵または秘密鍵のキーIDを取り出します。 暗号化されたメッセージが指定された場合は、データの暗号化に使用されたキーIDを与えます。

It can return 2 special key IDs: 2つの特殊なキーIDを返すことがあります。

  • SYMKEY

    The message is encrypted with a symmetric key. メッセージは対称鍵で暗号化されました。

  • ANYKEY

    The message is public-key encrypted, but the key ID has been removed. That means you will need to try all your secret keys on it to see which one decrypts it. <filename>pgcrypto</filename> itself does not produce such messages. メッセージは公開鍵で暗号化されましたが、キーIDが消去されていました。 つまり、どれで復号できるかを判定するためにはすべての秘密キーを試行しなければならないことを意味します。 pgcrypto自身はこうしたメッセージを生成しません。

Note that different keys may have the same ID. This is rare but a normal event. The client application should then try to decrypt with each one, to see which fits &mdash; like handling <literal>ANYKEY</literal>. 異なるキーが同一IDを持つ場合があることに注意してください。 これは稀ですが、正常なイベントです。 この場合クライアントアプリケーションはどちらが当てはまるかを調べるために、ANYKEYの場合と同様に、それぞれのキーで復号を試行しなければなりません。

F.28.3.6. armor(), dearmor() #

armor(data bytea [ , keys text[], values text[] ]) returns text
dearmor(data text) returns bytea

These functions wrap/unwrap binary data into PGP ASCII-armor format, which is basically Base64 with CRC and additional formatting. PGPのASCIIアーマー形式にデータを隠す、または、データを取り出します。 ASCIIアーマーは基本的にCRC付きのBASE64という形式で、追加のフォーマットがあります。

If the <parameter>keys</parameter> and <parameter>values</parameter> arrays are specified, an <firstterm>armor header</firstterm> is added to the armored format for each key/value pair. Both arrays must be single-dimensional, and they must be of the same length. The keys and values cannot contain any non-ASCII characters. keysvaluesの配列が指定された場合には、各キーと値の対に対してアーマーヘッダがアーマー形式に追加されます。 どちらの配列も1次元で、同じ長さでなければなりません。 keysとvaluesに非ASCII文字を含めることはできません。

F.28.3.7. pgp_armor_headers #

pgp_armor_headers(data text, key out text, value out text) returns setof record

<function>pgp_armor_headers()</function> extracts the armor headers from <parameter>data</parameter>. The return value is a set of rows with two columns, key and value. If the keys or values contain any non-ASCII characters, they are treated as UTF-8. pgp_armor_headers()dataからアーマーヘッダを取り出します。 戻り値はキーと値の2つの列からなる行の集合です。 もしキーや値に非アスキー文字が含まれていれば、UTF-8として扱われます。

F.28.3.8. PGP関数用のオプション #

<title>Options for PGP Functions</title>

Options are named to be similar to GnuPG. An option's value should be given after an equal sign; separate options from each other with commas. For example: オプションはGnuPGに似せて命名しています。 オプションの値は等号記号の後に指定しなければなりません。 複数のオプションはカンマで区切ってください。 以下に例を示します。

pgp_sym_encrypt(data, psw, 'compress-algo=1, cipher-algo=aes256')

All of the options except <literal>convert-crlf</literal> apply only to encrypt functions. Decrypt functions get the parameters from the PGP data. convert-crlfを除くすべてのオプションは暗号化関数にのみ適用可能です。 復号関数はPGPデータからこうしたパラメータを入手します。

The most interesting options are probably <literal>compress-algo</literal> and <literal>unicode-mode</literal>. The rest should have reasonable defaults. もっとも興味深いオプションはおそらくcompress-algounicode-modeでしょう。 残りはデフォルトで問題ないはずです。

F.28.3.8.1. cipher-algo #

Which cipher algorithm to use. 使用する暗号アルゴリズム。



Values: bf, aes128, aes192, aes256, 3des, cast5
Default: aes128
Applies to: pgp_sym_encrypt, pgp_pub_encrypt

値: bf, aes128, aes192, aes256, 3des, cast5
デフォルト: aes128
適用範囲: pgp_sym_encrypt, pgp_pub_encrypt

F.28.3.8.2. compress-algo #

Which compression algorithm to use. Only available if <productname>PostgreSQL</productname> was built with zlib. 使用する圧縮アルゴリズム。 PostgreSQLがzlib付きで構築されている場合のみ利用可能です。



Values:
  0 - no compression
  1 - ZIP compression
  2 - ZLIB compression (= ZIP plus meta-data and block CRCs)
Default: 0
Applies to: pgp_sym_encrypt, pgp_pub_encrypt

値:
 0 - 非圧縮
 1 - ZIP圧縮
 2 - ZLIB圧縮 (ZIPにメタデータとブロックCRCを加えたもの)
デフォルト: 0
適用範囲: pgp_sym_encrypt, pgp_pub_encrypt

F.28.3.8.3. compress-level #

How much to compress. Higher levels compress smaller but are slower. 0 disables compression. どの程度圧縮するかです。 レベルが大きい程小さくなりますが、低速になります。 0は圧縮を無効にします。



Values: 0, 1-9
Default: 6
Applies to: pgp_sym_encrypt, pgp_pub_encrypt

値: 0, 1-9
デフォルト: 6
適用範囲: pgp_sym_encrypt, pgp_pub_encrypt

F.28.3.8.4. convert-crlf #

Whether to convert <literal>\n</literal> into <literal>\r\n</literal> when encrypting and <literal>\r\n</literal> to <literal>\n</literal> when decrypting. <acronym>RFC</acronym> 4880 specifies that text data should be stored using <literal>\r\n</literal> line-feeds. Use this to get fully RFC-compliant behavior. 暗号化の際に\n\r\nに、復号の際に\r\n\n に変換するかどうか。 RFC 4880では、テキストデータは改行コードとして\r\n を使用して格納すべきであると規定されています。 完全にRFC準拠の動作を行いたければ、これを使用してください。



Values: 0, 1
Default: 0
Applies to: pgp_sym_encrypt, pgp_pub_encrypt, pgp_sym_decrypt, pgp_pub_decrypt

値: 0, 1
デフォルト: 0
適用範囲: pgp_sym_encrypt, pgp_pub_encrypt, pgp_sym_decrypt, pgp_pub_decrypt

F.28.3.8.5. disable-mdc #

Do not protect data with SHA-1. The only good reason to use this option is to achieve compatibility with ancient PGP products, predating the addition of SHA-1 protected packets to <acronym>RFC</acronym> 4880. Recent gnupg.org and pgp.com software supports it fine. データをSHA-1で保護しません。 このオプションを使用することが良い唯一の理由は、SHA-1で保護されたパケットがRFC 4880に追加される前の、古いPGP製品との互換性を得るためです。 最近のgnupg.orgおよびpgp.comのソフトウェアではこれを正しくサポートしています。



Values: 0, 1
Default: 0
Applies to: pgp_sym_encrypt, pgp_pub_encrypt

値: 0, 1
デフォルト: 0
適用範囲: pgp_sym_encrypt, pgp_pub_encrypt

F.28.3.8.6. sess-key #

Use separate session key. Public-key encryption always uses a separate session key; this option is for symmetric-key encryption, which by default uses the S2K key directly. 分離したセッションキーを使用します。 公開鍵暗号では常に分離したセッションキーを使用します。 このオプションは対称鍵暗号向けのもので、デフォルトではS2Kキーをそのまま使用します。



Values: 0, 1
Default: 0
Applies to: pgp_sym_encrypt

値: 0, 1
デフォルト: 0
適用範囲: pgp_sym_encrypt

F.28.3.8.7. s2k-mode #

Which S2K algorithm to use. 使用するS2Kアルゴリズム。



Values:
  0 - Without salt.  Dangerous!
  1 - With salt but with fixed iteration count.
  3 - Variable iteration count.
Default: 3
Applies to: pgp_sym_encrypt

値:
  0 - ソルト無。危険です!
  1 - ソルト有。固定繰り返し回数。
  3 - 可変繰り返し回数。
デフォルト: 3
適用範囲: pgp_sym_encrypt

F.28.3.8.8. s2k-count #

The number of iterations of the S2K algorithm to use. It must be a value between 1024 and 65011712, inclusive. 使用するS2Kアルゴリズムで使う繰り返しの回数。 1024以上、65011712以下の値でなければなりません。



Default: A random value between 65536 and 253952
Applies to: pgp_sym_encrypt, only with s2k-mode=3

デフォルト: 65536から253952までの乱数値
適用範囲: s2k-mode=3と指定した時のpgp_sym_encrypt

F.28.3.8.9. s2k-digest-algo #

Which digest algorithm to use in S2K calculation. S2K計算で使用するダイジェストアルゴリズム。



Values: md5, sha1
Default: sha1
Applies to: pgp_sym_encrypt

値: md5, sha1
デフォルト: sha1
適用範囲: pgp_sym_encrypt

F.28.3.8.10. s2k-cipher-algo #

Which cipher to use for encrypting separate session key. 分離したセッションキーの暗号化に使用する暗号。



Values: bf, aes, aes128, aes192, aes256
Default: use cipher-algo
Applies to: pgp_sym_encrypt

値: bf, aes, aes128, aes192, aes256
デフォルト: cipher-algoを使用
適用範囲: pgp_sym_encrypt

F.28.3.8.11. unicode-mode #

Whether to convert textual data from database internal encoding to UTF-8 and back. If your database already is UTF-8, no conversion will be done, but the message will be tagged as UTF-8. Without this option it will not be. テキストデータをデータベース内部符号化方式からUTF-8に変換して戻すかどうかです。 データベースがすでにUTF-8であれば、変換は起こらず、データにUTF-8としてタグが付くのみです。 このオプションがないと、何も行われません。



Values: 0, 1
Default: 0
Applies to: pgp_sym_encrypt, pgp_pub_encrypt

値: 0, 1
デフォルト: 0
適用範囲: pgp_sym_encrypt, pgp_pub_encrypt

F.28.3.9. GnuPGを使用したキーの生成 #

<title>Generating PGP Keys with GnuPG</title>

To generate a new key: 新しいキーを生成します。

gpg --gen-key

The preferred key type is <quote>DSA and Elgamal</quote>. 推奨するキー種類はDSAとElgamalです。

For RSA encryption you must create either DSA or RSA sign-only key as master and then add an RSA encryption subkey with <literal>gpg &#45;-edit-key</literal>. RSA暗号化のためには、マスタとしてDSAまたはRSAで署名のみのキーを作成し、そしてgpg --edit-keyを使用してRSAで暗号化された副キーを追加しなければなりません

To list keys: キーを列挙します。

gpg --list-secret-keys

To export a public key in ASCII-armor format: ASCIIアーマー形式で公開鍵をエクスポートします。

gpg -a --export KEYID > public.key

To export a secret key in ASCII-armor format: ASCIIアーマー形式の秘密鍵をエクスポートします。

gpg -a --export-secret-keys KEYID > secret.key

You need to use <function>dearmor()</function> on these keys before giving them to the PGP functions. Or if you can handle binary data, you can drop <literal>-a</literal> from the command. PGP関数にこれらのキーを渡す前にdearmor()を使用する必要があります。 バイナリデータを扱うことができる場合、コマンドから-aフラグを省略することができます。

For more details see <literal>man gpg</literal>, <ulink url="https://www.gnupg.org/gph/en/manual.html">The GNU Privacy Handbook</ulink> and other documentation on <ulink url="https://www.gnupg.org/"></ulink>. 詳細はman gpgThe GNU Privacy Handbookhttps://www.gnupg.org/サイトの各種文書を参照してください。

F.28.3.10. PGPコードの制限 #

<title>Limitations of PGP Code</title>
  • No support for signing. That also means that it is not checked whether the encryption subkey belongs to the master key. 署名に関するサポートはありません。 これはまた、暗号化副キーがマスタキーに属しているかどうか検査しないことを意味します。

  • No support for encryption key as master key. As such practice is generally discouraged, this should not be a problem. マスタキーとして暗号化キーをサポートしません。 一般的にこうした状況は現実的ではありませんので、問題にならないはずです。

  • No support for several subkeys. This may seem like a problem, as this is common practice. On the other hand, you should not use your regular GPG/PGP keys with <filename>pgcrypto</filename>, but create new ones, as the usage scenario is rather different. 複数の副キーに関するサポートはありません。 よくありますので、これは問題になりそうに見えます。 一方、通常のGPG/PGPキーをpgcryptoで使用すべきではありません。 使用する状況が多少異なりますので新しく作成してください。

F.28.4. 暗号化そのものを行う関数 #

<title>Raw Encryption Functions</title>

These functions only run a cipher over data; they don't have any advanced features of PGP encryption. Therefore they have some major problems: これらの関数はデータ全体を暗号化するためだけに実行します。 PGP暗号化の持つ先端的な機能はありません。 したがって、大きな問題がいくつか存在します。

  1. They use user key directly as cipher key. 暗号キーとしてユーザキーをそのまま使用します。

  2. They don't provide any integrity checking, to see if the encrypted data was modified. 暗号化されたデータが変更されたかどうかを確認するための整合性検査をまったく提供しません。

  3. They expect that users manage all encryption parameters themselves, even IV. ユーザが、IVをも含め暗号化パラメータ自体をすべて管理していることを想定しています。

  4. They don't handle text. テキストは扱いません。

So, with the introduction of PGP encryption, usage of raw encryption functions is discouraged. このため、PGP暗号化の導入もあり、暗号化のみの関数はあまり使用されません。

encrypt(data bytea, key bytea, type text) returns bytea
decrypt(data bytea, key bytea, type text) returns bytea

encrypt_iv(data bytea, key bytea, iv bytea, type text) returns bytea
decrypt_iv(data bytea, key bytea, iv bytea, type text) returns bytea

Encrypt/decrypt data using the cipher method specified by <parameter>type</parameter>. The syntax of the <parameter>type</parameter> string is: typeで指定した暗号方法を使用してデータの暗号化・復号を行います。 type文字列の構文は以下の通りです。

algorithm [ - mode ] [ /pad: padding ]

where <replaceable>algorithm</replaceable> is one of: ここでalgorithmは以下の1つです。

  • bf — Blowfish

  • aes — AES (Rijndael-128, -192 or -256)

and <replaceable>mode</replaceable> is one of: またmodeは以下の1つです。

  • <literal>cbc</literal> &mdash; next block depends on previous (default) cbc — 次のブロックは前ブロックに依存します(デフォルト)

  • <literal>ecb</literal> &mdash; each block is encrypted separately (for testing only) ecb — 各ブロックは独自に暗号化されます(試験用途のみ)

and <replaceable>padding</replaceable> is one of: paddingは以下の1つです。

  • <literal>pkcs</literal> &mdash; data may be any length (default) pkcs — データ長に制限はありません(デフォルト)

  • <literal>none</literal> &mdash; data must be multiple of cipher block size none — データは暗号ブロックサイズの倍数でなければなりません

So, for example, these are equivalent: このため、例えば以下は同じです。

encrypt(data, 'fooz', 'bf')
encrypt(data, 'fooz', 'bf-cbc/pad:pkcs')

In <function>encrypt_iv</function> and <function>decrypt_iv</function>, the <parameter>iv</parameter> parameter is the initial value for the CBC mode; it is ignored for ECB. It is clipped or padded with zeroes if not exactly block size. It defaults to all zeroes in the functions without this parameter. encrypt_ivおよびdecrypt_ivでは、ivパラメータはCBCモード用の初期値となります。 ECBでは無視されます。 正確にブロック長でない場合、切り詰められるか、もしくはゼロで埋められます。 このパラメータがない場合、関数のデフォルト値はすべてゼロです。

F.28.5. ランダムデータ関数 #

<title>Random-Data Functions</title>
gen_random_bytes(count integer) returns bytea

Returns <parameter>count</parameter> cryptographically strong random bytes. At most 1024 bytes can be extracted at a time. This is to avoid draining the randomness generator pool. 暗号論的に強いランダムなcountバイトを返します。 一度に最大で1024バイトを抽出することができます。 ランダム性ジェネレータプールを空にすることを防止するためのものです。

gen_random_uuid() returns uuid

Returns a version 4 (random) UUID. (Obsolete, this function internally calls the <link linkend="functions-uuid">core function</link> of the same name.) バージョン4(ランダムな)UUIDを返します。 (廃れたものです。この関数は内部では同名のコア関数を呼び出します。)

F.28.6. 注釈 #

<title>Notes</title>

F.28.6.1. 設定 #

<title>Configuration</title>

<filename>pgcrypto</filename> configures itself according to the findings of the main PostgreSQL <literal>configure</literal> script. The options that affect it are <literal>&#45;-with-zlib</literal> and <literal>&#45;-with-ssl=openssl</literal>. pgcryptoは自身で主PostgreSQLのconfigureスクリプトの検出結果に従って構築します。 構築に影響するオプションは--with-zlib--with-opensslです。

When compiled with zlib, PGP encryption functions are able to compress data before encrypting. ZLIB付きでコンパイルされた場合、PGP暗号化関数は暗号化前にデータを圧縮することができます。

<filename>pgcrypto</filename> requires <productname>OpenSSL</productname>. Otherwise, it will not be built or installed. pgcryptoOpenSSLを必要とします。 そうでなければ、構築もインストールもされません。

When compiled against <productname>OpenSSL</productname> 3.0.0 and later versions, the legacy provider must be activated in the <filename>openssl.cnf</filename> configuration file in order to use older ciphers like DES or Blowfish. OpenSSL 3.0.0とそれ以降に対してコンパイルされた場合、DESやBlowfishのような古い暗号を使うためにはopenssl.cnf設定ファイルでレガシープロバイダを有効にしなければなりません。

F.28.6.2. NULLの扱い #

<title>NULL Handling</title>

As is standard in SQL, all functions return NULL, if any of the arguments are NULL. This may create security risks on careless usage. 標準SQLの通り、引数のいずれかがNULLの場合、すべての関数はNULLを返します。 注意せずに使用すると、これがセキュリティ上の問題になるかもしれません。

F.28.6.3. セキュリティ上の制限 #

<title>Security Limitations</title>

All <filename>pgcrypto</filename> functions run inside the database server. That means that all the data and passwords move between <filename>pgcrypto</filename> and client applications in clear text. Thus you must: pgcryptoの関数はすべてデータベースサーバ内部で実行されます。 これは、pgcryptoとクライアントアプリケーションとの間でやり取りされるデータはすべて平文であることを意味します。 したがって、以下を行う必要があります。

  1. <para>Connect locally or use SSL connections.</para>

    ローカルまたはSSL接続で接続

  2. <para>Trust both system and database administrator.</para>

    システム管理者およびデータベース管理者を信頼

If you cannot, then better do crypto inside client application. これらが不可能であれば、クライアントアプリケーション内で暗号化する方が望まれます。

The implementation does not resist <ulink url="https://en.wikipedia.org/wiki/Side-channel_attack">side-channel attacks</ulink>. For example, the time required for a <filename>pgcrypto</filename> decryption function to complete varies among ciphertexts of a given size. 実装はサイドチャネル攻撃に耐えられません。 例えば、pgcrypto復号関数が完了するのに掛かる時間は、一定の長さの暗号文に対して一様ではありません。

F.28.6.4. 有用な文書 #

<title>Useful Reading</title>

F.28.6.5. 技術的な参考情報 #

<title>Technical References</title>

F.28.7. 作者 #

<title>Author</title>

Marko Kreen

<filename>pgcrypto</filename> uses code from the following sources: pgcryptoは以下のソースを使用しています。

アルゴリズム作者元ソース
DES cryptDavid Burren他FreeBSD libcrypt
MD5 cryptPoul-Henning KampFreeBSD libcrypt
Blowfish cryptSolar Designerwww.openwall.com