Document of c-modernization-kit (porter) 1.0.0
Loading...
Searching...
No Matches
crypto_linux.c File Reference

Linux 向け暗号化・復号モジュール (OpenSSL AES-256-GCM)。 More...

#include <string.h>
#include <openssl/evp.h>
#include "crypto.h"
Include dependency graph for crypto_linux.c:

Go to the source code of this file.

Functions

int potr_encrypt (uint8_t *dst, size_t *dst_len, const uint8_t *src, size_t src_len, const uint8_t *key, const uint8_t *nonce, const uint8_t *aad, size_t aad_len)
 AES-256-GCM でデータを暗号化します。
int potr_decrypt (uint8_t *dst, size_t *dst_len, const uint8_t *src, size_t src_len, const uint8_t *key, const uint8_t *nonce, const uint8_t *aad, size_t aad_len)
 AES-256-GCM でデータを復号し、認証タグを検証します。
int potr_passphrase_to_key (uint8_t *key, const uint8_t *passphrase, size_t passphrase_len)
 任意のパスフレーズを SHA-256 ハッシュにより AES-256 鍵に変換します。

Detailed Description

Linux 向け暗号化・復号モジュール (OpenSSL AES-256-GCM)。

Author
c-modernization-kit sample team
Date
2026/03/12
Version
1.0.0

OpenSSL の EVP インターフェースを使用して AES-256-GCM 暗号化・復号を実装します。
Windows 実装 (BCrypt) と同一の wire フォーマット ([暗号文][GCM タグ 16B]) を使用するため、 クロスプラットフォーム通信に対応します。
本ファイルは Linux ビルドでのみコンパイルされます (_WIN32 未定義時)。

Definition in file crypto_linux.c.

Function Documentation

◆ potr_encrypt()

int potr_encrypt ( uint8_t * dst,
size_t * dst_len,
const uint8_t * src,
size_t src_len,
const uint8_t * key,
const uint8_t * nonce,
const uint8_t * aad,
size_t aad_len )

AES-256-GCM でデータを暗号化します。

Parameters
[out]dst暗号化後データを格納するバッファ。
内容: [暗号文: src_len バイト][GCM タグ: POTR_CRYPTO_TAG_SIZE バイト]
dst == src の in-place 暗号化も可能。
[in,out]dst_len入力: dst のバッファサイズ (>= src_len + POTR_CRYPTO_TAG_SIZE)。
出力: 書き込んだバイト数 (= src_len + POTR_CRYPTO_TAG_SIZE)。
[in]src平文データへのポインタ。
[in]src_len平文データのバイト数。
[in]keyAES-256 鍵 (POTR_CRYPTO_KEY_SIZE バイト)。
[in]nonceノンス (POTR_CRYPTO_NONCE_SIZE バイト)。
[in]aad追加認証データへのポインタ。NULL の場合は AAD なし。
[in]aad_lenAAD のバイト数。
Returns
成功時は 0、失敗時は -1 を返します。

Definition at line 29 of file crypto_linux.c.

References POTR_CRYPTO_NONCE_SIZE, and POTR_CRYPTO_TAG_SIZE.

Referenced by flush_packed(), flush_packed_peer(), health_thread_func(), n1_send_nack(), n1_send_ping_reply(), n1_send_reject(), peer_send_fin(), send_fin(), send_nack(), send_ping_reply(), send_reject(), tcp_health_thread_func(), and tcp_send_ping_reply().

Here is the caller graph for this function:

◆ potr_decrypt()

int potr_decrypt ( uint8_t * dst,
size_t * dst_len,
const uint8_t * src,
size_t src_len,
const uint8_t * key,
const uint8_t * nonce,
const uint8_t * aad,
size_t aad_len )

AES-256-GCM でデータを復号し、認証タグを検証します。

Parameters
[out]dst復号後データを格納するバッファ。
[in,out]dst_len入力: dst のバッファサイズ (>= src_len - POTR_CRYPTO_TAG_SIZE)。
出力: 書き込んだバイト数 (= src_len - POTR_CRYPTO_TAG_SIZE)。
[in]src暗号化データへのポインタ。
内容: [暗号文: src_len - POTR_CRYPTO_TAG_SIZE バイト][GCM タグ: POTR_CRYPTO_TAG_SIZE バイト]
[in]src_len暗号化データのバイト数 (タグを含む)。>= POTR_CRYPTO_TAG_SIZE。
[in]keyAES-256 鍵 (POTR_CRYPTO_KEY_SIZE バイト)。
[in]nonceノンス (POTR_CRYPTO_NONCE_SIZE バイト)。
[in]aad追加認証データへのポインタ。NULL の場合は AAD なし。
[in]aad_lenAAD のバイト数。
Returns
成功 (認証タグ検証 OK) 時は 0、失敗 (認証タグ不一致含む) 時は -1 を返します。

Definition at line 116 of file crypto_linux.c.

References POTR_CRYPTO_NONCE_SIZE, and POTR_CRYPTO_TAG_SIZE.

Referenced by recv_thread_func(), and tcp_recv_thread_func().

Here is the caller graph for this function:

◆ potr_passphrase_to_key()

int potr_passphrase_to_key ( uint8_t * key,
const uint8_t * passphrase,
size_t passphrase_len )

任意のパスフレーズを SHA-256 ハッシュにより AES-256 鍵に変換します。

設定ファイルの encrypt_key に 64 文字 hex 以外の文字列が指定された場合に使用します。
入力バイト列の SHA-256 ダイジェストをそのまま鍵として使用します。
送受信の双方で同一のパスフレーズを指定すれば、同一の鍵が導出されます。

Parameters
[out]key出力鍵バッファ (POTR_CRYPTO_KEY_SIZE = 32 バイト)。
[in]passphraseパスフレーズへのポインタ (任意のバイト列)。
[in]passphrase_lenパスフレーズの長さ (バイト)。0 の場合も有効。
Returns
成功時は 0、失敗時は -1 を返します。

Definition at line 213 of file crypto_linux.c.

References POTR_CRYPTO_KEY_SIZE.

Referenced by apply_service_kv().

Here is the caller graph for this function: