-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathshield_brain.h
More file actions
233 lines (201 loc) · 6.65 KB
/
Copy pathshield_brain.h
File metadata and controls
233 lines (201 loc) · 6.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
* @file shield_brain.h
* @brief Brain FFI Public Interface
*
* Interface for connecting Shield C guards to Brain Python engines.
*
* @author SENTINEL Team
* @date January 2026
*/
#ifndef SHIELD_BRAIN_H
#define SHIELD_BRAIN_H
#include <stddef.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
/* ============================================================================
* Engine Types
* ============================================================================ */
/**
* @brief Brain engine categories
*/
typedef enum {
BRAIN_ENGINE_INJECTION, /**< Prompt injection detection */
BRAIN_ENGINE_JAILBREAK, /**< Jailbreak attempt detection */
BRAIN_ENGINE_RAG_POISONING, /**< RAG poisoning detection */
BRAIN_ENGINE_AGENT_MANIPULATION, /**< Agent manipulation detection */
BRAIN_ENGINE_TOOL_HIJACKING, /**< Tool hijacking detection */
BRAIN_ENGINE_MCP_ATTACK, /**< MCP protocol attacks */
BRAIN_ENGINE_PII, /**< PII detection */
BRAIN_ENGINE_EXFILTRATION, /**< Data exfiltration */
BRAIN_ENGINE_ALL /**< Run all applicable engines */
} brain_engine_category_t;
/**
* @brief Severity levels
*/
typedef enum {
BRAIN_SEVERITY_NONE = 0,
BRAIN_SEVERITY_LOW = 1,
BRAIN_SEVERITY_MEDIUM = 2,
BRAIN_SEVERITY_HIGH = 3,
BRAIN_SEVERITY_CRITICAL = 4
} brain_severity_t;
/* ============================================================================
* Result Types
* ============================================================================ */
/**
* @brief Single engine result
*/
typedef struct {
bool detected; /**< Threat detected */
double confidence; /**< Confidence score (0-1) */
brain_severity_t severity; /**< Severity level */
const char *engine_name; /**< Engine that detected (may be NULL) */
const char *reason; /**< Human-readable reason (may be NULL) */
const char *attack_type; /**< Attack classification (may be NULL) */
double latency_ms; /**< Engine execution time */
} brain_result_t;
/**
* @brief Aggregate result from multiple engines
*/
typedef struct {
bool any_detected; /**< Any engine detected threat */
double max_confidence; /**< Highest confidence score */
brain_severity_t max_severity; /**< Highest severity */
size_t engines_run; /**< Number of engines executed */
size_t engines_triggered; /**< Number of engines that triggered */
double total_latency_ms; /**< Total execution time */
brain_result_t *results; /**< Individual results */
size_t result_count; /**< Number of results */
} brain_aggregate_result_t;
/* ============================================================================
* Lifecycle
* ============================================================================ */
/**
* @brief Initialize Brain FFI
*
* @param python_home Path to Python installation (NULL for default)
* @param brain_path Path to Brain module (NULL for default)
* @return 0 on success, -1 on error
*/
int brain_ffi_init(const char *python_home, const char *brain_path);
/**
* @brief Shutdown Brain FFI
*/
void brain_ffi_shutdown(void);
/**
* @brief Check if Brain is available
*
* @return true if Brain engines can be called
*/
bool brain_available(void);
/**
* @brief Get Brain mode
*
* @return "embedded" (Python), "http" (API fallback), or "stub" (no Brain)
*/
const char* brain_mode(void);
/* ============================================================================
* Analysis
* ============================================================================ */
/**
* @brief Analyze input with specific engine category
*
* @param input Input text to analyze
* @param category Engine category to use
* @param result Result structure (caller provides)
* @return 0 on success
*/
int brain_ffi_analyze(
const char *input,
brain_engine_category_t category,
brain_result_t *result
);
/**
* @brief Analyze input with all engines
*
* @param input Input text to analyze
* @param result Aggregate result (caller provides)
* @return 0 on success
*/
int brain_ffi_analyze_all(
const char *input,
brain_aggregate_result_t *result
);
/**
* @brief Analyze with specific engine by name
*
* @param input Input text
* @param engine_name Full engine name (e.g., "injection_engine.InjectionEngine")
* @param result Result structure
* @return 0 on success
*/
int brain_ffi_analyze_engine(
const char *input,
const char *engine_name,
brain_result_t *result
);
/* ============================================================================
* Result Management
* ============================================================================ */
/**
* @brief Free aggregate result
*/
void brain_result_free(brain_aggregate_result_t *result);
/**
* @brief Get severity as string
*/
const char* brain_severity_string(brain_severity_t severity);
/* ============================================================================
* Engine Discovery
* ============================================================================ */
/**
* @brief Get number of available engines
*/
size_t brain_engine_count(void);
/**
* @brief Get engine names
*
* @param names Array to fill with engine names
* @param max_count Maximum number of names
* @return Actual number of names written
*/
size_t brain_engine_names(const char **names, size_t max_count);
/**
* @brief Check if specific engine is available
*/
bool brain_engine_available(const char *engine_name);
/* ============================================================================
* Configuration
* ============================================================================ */
/**
* @brief Brain FFI configuration
*/
typedef struct {
int timeout_ms; /**< Engine call timeout (default: 1000) */
bool enable_caching; /**< Cache results (default: false) */
int cache_ttl_seconds; /**< Cache TTL (default: 60) */
bool use_http_fallback; /**< Fall back to HTTP if Python fails */
const char *http_endpoint; /**< Brain API endpoint for fallback */
int max_concurrent; /**< Max concurrent engine calls (default: 4) */
} brain_ffi_config_t;
/**
* @brief Default configuration
*/
#define BRAIN_FFI_CONFIG_DEFAULT { \
.timeout_ms = 1000, \
.enable_caching = false, \
.cache_ttl_seconds = 60, \
.use_http_fallback = true, \
.http_endpoint = "http://localhost:8000", \
.max_concurrent = 4 \
}
/**
* @brief Set configuration
*/
int brain_ffi_configure(const brain_ffi_config_t *config);
#ifdef __cplusplus
}
#endif
#endif /* SHIELD_BRAIN_H */