sodium_crypto_stream_xchacha20_xor_ic

nonce と秘密鍵を使ってメッセージを暗号化する(認証なし)

説明

string sodium_crypto_stream_xchacha20_xor_ic(
    string $message,
    string $nonce,
    int $counter,
    string $key
)

この関数は、sodium_crypto_stream_xchacha20_xor に似ていますが、ブロックカウンタの初期値を非ゼロの値に設定する機能が追加されています。 これによって、以前の値を計算せずに直接任意のブロックにアクセスすることができます。

警告

この暗号化処理は認証を行いませんし、 選択暗号文攻撃(chosen-ciphertext attack) を防ぐことができません。 必ず暗号化されたテキストを認証コードと組み合わせるようにして下さい。 たとえば、 sodium_crypto_aead_xchacha20poly1305_ietf_encryptsodium_crypto_auth を使うことが考えられます。

パラメータ

message

暗号化するメッセージ。

nonce

24バイトの nonce。

counter

ブロックカウンタの初期値。

key

暗号化キー。 sodium_crypto_stream_xchacha20_keygen で生成されたものです。

戻り値

暗号化されたメッセージを返します。 失敗した場合に false を返します

例1 sodium_crypto_stream_xchacha20_xor_ic の例

<?php
$n2 
random_bytes(SODIUM_CRYPTO_STREAM_XCHACHA20_NONCEBYTES);
$left  str_repeat("\x01"64);
$right str_repeat("\xfe"64);

// All at once:
$stream7_unified sodium_crypto_stream_xchacha20_xor($left $right$n2$key);

// Piecewise, with initial counter:
$stream7_left  sodium_crypto_stream_xchacha20_xor_ic($left$n20$key);
$stream7_right sodium_crypto_stream_xchacha20_xor_ic($right$n21$key);
$stream7_concat $stream7_left $stream7_right;

var_dump(strlen($stream7_concat));
var_dump($stream7_unified === $stream7_concat);
?>

上の例の出力は、 たとえば以下のようになります。

int(128)
bool(true)

参考

  • sodium_crypto_stream_xchacha20_xor