62 lines
2.5 KiB
C
62 lines
2.5 KiB
C
#ifndef GERBERA_H
|
|
#define GERBERA_H
|
|
|
|
/* 명령줄 진입점과 각 모듈이 함께 쓰는 공개 선언입니다. */
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
|
|
/* Gerbera 파일 형식과 메모리 작업 버퍼에서 사용하는 고정 크기입니다. */
|
|
#define GERBERA_MAGIC_SIZE 10
|
|
#define GERBERA_NONCE_SIZE 16
|
|
#define GERBERA_TAG_SIZE 16
|
|
#define GERBERA_BUFFER_SIZE 4096
|
|
|
|
/* 헤더에는 매직 바이트, nonce, 원문 길이가 저장됩니다. */
|
|
#define GERBERA_HEADER_SIZE (GERBERA_MAGIC_SIZE + GERBERA_NONCE_SIZE + 8)
|
|
|
|
/* 암호화 파일은 데이터 앞에 헤더를, 뒤에 인증 태그를 붙입니다. */
|
|
#define GERBERA_OVERHEAD_SIZE (GERBERA_HEADER_SIZE + GERBERA_TAG_SIZE)
|
|
|
|
/* 스트림 암호 상태: 순열 테이블, 파생 키, 인덱스, 바이트 위치입니다. */
|
|
typedef struct {
|
|
uint8_t state[256];
|
|
uint8_t key[32];
|
|
uint8_t i;
|
|
uint8_t j;
|
|
uint64_t position;
|
|
} GerberaCipher;
|
|
|
|
/* 고정 크기 인증 태그를 계산하고 검증하는 상태입니다. */
|
|
typedef struct {
|
|
uint8_t state[32];
|
|
uint64_t length;
|
|
} GerberaAuth;
|
|
|
|
/* 비밀번호와 파일별 nonce로 암호 스트림을 초기화합니다. */
|
|
void gerbera_cipher_init(GerberaCipher *ctx, const char *password,
|
|
const uint8_t nonce[GERBERA_NONCE_SIZE]);
|
|
|
|
/* 생성된 키스트림을 data에 XOR합니다. 다시 호출하면 스트림이 이어집니다. */
|
|
void gerbera_cipher_apply(GerberaCipher *ctx, uint8_t data[], size_t length);
|
|
|
|
/* 같은 비밀번호와 nonce로 인증 누산기를 초기화합니다. */
|
|
void gerbera_auth_init(GerberaAuth *ctx, const char *password,
|
|
const uint8_t nonce[GERBERA_NONCE_SIZE]);
|
|
|
|
/* 파일에 기록되는 순서대로 바이트를 인증 누산기에 넣습니다. */
|
|
void gerbera_auth_update(GerberaAuth *ctx, const uint8_t data[], size_t length);
|
|
|
|
/* 누산을 마무리하고 인증 태그를 씁니다. */
|
|
void gerbera_auth_finish(GerberaAuth *ctx, uint8_t tag[GERBERA_TAG_SIZE]);
|
|
|
|
/* 첫 차이에서 바로 끝내지 않고 태그 전체를 비교합니다. */
|
|
int gerbera_constant_time_equal(const uint8_t a[], const uint8_t b[],
|
|
size_t length);
|
|
|
|
/* 파일 전체를 암호화하거나 복호화하며, 성공 시 0, 오류 시 0이 아닌 값을 반환합니다. */
|
|
int encrypt_file(const char *input_path, const char *output_path,
|
|
const char *password);
|
|
int decrypt_file(const char *input_path, const char *output_path,
|
|
const char *password);
|
|
|
|
#endif
|