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

データ暗号化・復号モジュールの内部ヘッダー。 More...

#include <stddef.h>
#include <stdint.h>
#include <porter_const.h>
Include dependency graph for crypto.h:
This graph shows which files directly or indirectly include this file:

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

データ暗号化・復号モジュールの内部ヘッダー。

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

プラットフォームごとに異なる暗号 API を共通インターフェースで抽象化します。

OS 使用ライブラリ アルゴリズム
Linux OpenSSL (libcrypto) EVP_aes_256_gcm() / EVP_sha256() AES-256-GCM / SHA-256
Windows CNG (BCrypt) BCRYPT_AES_ALGORITHM / BCRYPT_SHA256_ALGORITHM AES-256-GCM / SHA-256

暗号化ペイロードのフォーマット:

[暗号文: src_len バイト] [GCM 認証タグ: POTR_CRYPTO_TAG_SIZE バイト]
#define POTR_CRYPTO_TAG_SIZE
AES-256-GCM 認証タグサイズ (バイト)。暗号文の直後に付加する。

ノンス構成 (12 バイト):

[session_id: 4B NBO] [seq_num: 4B NBO] [padding: 4B 0x00]

追加認証データ (AAD): PotrPacket ヘッダー 32 バイト (NBO ワイヤーフォーマット)。 ヘッダーの改ざんも認証タグで検知される。

Definition in file crypto.h.

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 )
extern

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 )
extern

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 )
extern

任意のパスフレーズを 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: