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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
Proposal for OSSL_PARAM futures
===============================
Format:
```perl
{-
use OpenSSL::paramnames qw(produce_param_handlers);
-}
/*
* Machine generated parameter handling
* generated by util/perl/OpenSSL/paramnames.pm
*/
{-
produce_param_handlers(
'name' => 'kdf_scrypt',
'functions' => 'both', # getter or setter being the other options
'prologue' => "KDF_SCRYPT *ctx = vctx;",
"static" => "yes", # "yes" to generate static functions (default) or
# "no" to not
'params' => (
'KDF_PARAM_PASSWORD' => (
'type' => 'octet string',
'access' => 'writeonly',
'setaction' => qq(
if (!scrypt_set_membuf(&ctx->pass, &ctx->pass_len, p))
return 0;
),
),
'KDF_PARAM_SALT' => (
'type' => 'octet string',
'access' => 'readwrite',
'setaction' => qq(
if (!scrypt_set_membuf(&ctx->salt, &ctx->salt_len, p))
return 0;
),
'getaction' => qq(
p->return_size = ctx->salt_len;
if (p->data_size >= ctx->salt_len)
memcpy(p->data, ctx->salt, p->data_size >= ctx->salt_len);
),
),
'KDF_PARAM_SCRYPT_N' => (
'type' => 'integer',
'ctype' => 'uint64_t',
'access' => 'readwrite',
'field' => "ctx->N",
'sanitycheck' => "value > 1 && is_power_of_two(value)"
),
'KDF_PARAM_SCRYPT_R' => (
'type' => 'integer',
'ctype' => 'uint64_t',
'access' => 'readwrite',
'field' => "ctx->r",
'sanitycheck' => "value >= 1",
),
'KDF_PARAM_SCRYPT_P' => (
'type' => 'integer',
'ctype' => 'uint64_t',
'access' => 'readwrite',
'field' => "ctx->p",
'sanitycheck' => "value >= 1",
),
'KDF_PARAM_SCRYPT_MAXMEM' => (
'type' => 'integer',
'ctype' => 'uint64_t',
'access' => 'readwrite',
'field' => "ctx->maxmem_bytes",
'sanitycheck' => "value >= 1",
),
'KDF_PARAM_PROPERTIES' => (
'type' => 'utf8_string',
'access' => 'readwrite',
'setaction' => qq(
if (!set_property_query(ctx, p->data) || !set_digest(ctx))
return 0;
),
),
'KDF_PARAM_SIZE' => (
'type' => 'integer',
'ctype' => 'size_t',
'access' => 'readonly',
'field' => "SIZE_MAX",
),
);
);
-}
/* End of generated code */
```
THe top level attributes are:
- "name" is the name the functions will derive from e.g. "kdf_scrypt" to this
will be appended _[gs]et[_ctx]_params
- "functions" is the functions to generate. By default both setters and
getters but either can be omitted.
- "prologue" defines some introductory code emited in the generated functions.
Function arguments are: `void *vctx, OSSL_PARAM params[]` and this
can be used to specialise the void pointer or declare locals.
- "epilogue" defines some post decode code emited in the generated function
- "params" defines the parameters both gettable and settable
Within the "params" the fields specify each parameter by label.
Each parameter is then specialised with attributes:
- "type" is the OSSL_PARAM type
- "ctype" is the underlying C type (e.g. for an integer parameter size_t
could be the C type)
- "access" is readwrite, readonly or writeonly. This determines if the
parameter is a settable, gettable or both
- "field" is an accessor to the field itself
- "sanitycheck" is a validation check for the parameter. If present, code
will be generated `if (!(sanitycheck)) return 0;`
The local variable `var` will contain the C value if specified.
- "setaction" is C code to execute when the parameter is being set. It will
define an OSSL_PARAM pointer p to set.
- "code" set to "no" skips code generation for this parameter, it defaults
to "yes" which generates handlers. This is useful when a parameter
is duplicated with differenting types (e.g. utf8 string and integer).
- "published" set to "yes" includes the parameter in the gettable/settable
lists. Set to "no" and it isn't included (but will still be processed).
It defaults to "yes".
- Flags include:
- nostatic: do not make the function static
- nocode: do not generate code for this parameter
- This allows, e.g., two different types for a parameter (int & string)
- unpublished: do not generate this parameter in the gettable/settable list
- "engine" is the only one like this
- readonly: create a getter but not a setter
- writeonly: create a setting but not a getter
The idea is that the gettable and get functions will be simultaneously
generated along with fast decoder to look up parameter names quickly.
The getter and setter functions will be pre-populated with some local variable:
```c
OSSL_PARAM *p; /* The matching parameter */
type val; /* The value of the parameter after a get/set call */
/* (for C types) */
```
A worked example for scrypt:
Would generate something along the lines of:
```c
enum kdf_scrypt_ctx_param_e {
kdf_scrypt_ctx_param_INVALID,
kdf_scrypt_ctx_param_OSSL_KDF_PARAM_PASSWORD,
kdf_scrypt_ctx_param_OSSL_KDF_PARAM_PROPERTIES,
kdf_scrypt_ctx_param_OSSL_KDF_PARAM_SALT,
kdf_scrypt_ctx_param_OSSL_KDF_PARAM_SCRYPT_MAXMEM,
kdf_scrypt_ctx_param_OSSL_KDF_PARAM_SCRYPT_N,
kdf_scrypt_ctx_param_OSSL_KDF_PARAM_SCRYPT_P,
kdf_scrypt_ctx_param_OSSL_KDF_PARAM_SCRYPT_R,
kdf_scrypt_ctx_param_OSSL_KDF_PARAM_SIZE
};
static enum kdf_scrypt_ctx_param_e kdf_scrypt_ctx_lookup(const OSSL_PARAM *p) {
/* magic decoder */
return kdf_scrypt_ctx_param_INVALID;
}
static int kdf_scrypt_set_ctx_params(void *vctx, const OSSL_PARAM params[])
{
const OSSL_PARAM *p;
KDF_SCRYPT *ctx = vctx;
if (params == NULL)
return 1;
for (p = params; p->key != NULL; p++) {
switch (kdf_scrypt_ctx_lookup(p)) {
default:
break;
case kdf_scrypt_ctx_param_OSSL_KDF_PARAM_PASSWORD:
if (!scrypt_set_membuf(&ctx->pass, &ctx->pass_len, p))
return 0;
break;
case kdf_scrypt_ctx_param_OSSL_KDF_PARAM_SALT:
if (!scrypt_set_membuf(&ctx->salt, &ctx->salt_len, p))
return 0;
break;
case kdf_scrypt_ctx_param_OSSL_KDF_PARAM_SCRYPT_N: {
uint64_t value;
if (!OSSL_PARAM_get_uint64(p, &value) {
if (!(value > 1 && is_power_of_two(u64_value)))
return 0;
ctx->N = value;
}
break;
}
case kdf_scrypt_ctx_param_OSSL_KDF_PARAM_SCRYPT_R: {
uint64_t value;
if (!OSSL_PARAM_get_uint64(p, &value) {
if (!(value >= 1))
return 0;
ctx->r = value;
}
break;
}
case kdf_scrypt_ctx_param_OSSL_KDF_PARAM_SCRYPT_P: {
uint64_t value;
if (!OSSL_PARAM_get_uint64(p, &value) {
if (!(value >= 1))
return 0;
ctx->p = value;
}
break;
}
case kdf_scrypt_ctx_param_OSSL_KDF_PARAM_SCRYPT_MAXMEM: {
uint64_t value;
if (!OSSL_PARAM_get_uint64(p, &value) {
if (!(value >= 1))
return 0;
ctx->p = value;
}
break;
}
case kdf_scrypt_ctx_param_OSSL_KDF_PARAM_PROPERTIES:
if (p != NULL) {
if (p->data_type != OSSL_PARAM_UTF8_STRING) {
if (!set_property_query(ctx, p->data) || !set_digest(ctx))
return 0;
}
}
}
}
return 1;
}
static const OSSL_PARAM *kdf_scrypt_settable_ctx_params(ossl_unused void *ctx,
ossl_unused void *p_ctx)
{
static const OSSL_PARAM known_settable_ctx_params[] = {
OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PASSWORD, NULL, 0),
OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),
OSSL_PARAM_uint64(OSSL_KDF_PARAM_SCRYPT_N, NULL),
OSSL_PARAM_uint32(OSSL_KDF_PARAM_SCRYPT_R, NULL),
OSSL_PARAM_uint32(OSSL_KDF_PARAM_SCRYPT_P, NULL),
OSSL_PARAM_uint64(OSSL_KDF_PARAM_SCRYPT_MAXMEM, NULL),
OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
OSSL_PARAM_END
};
return known_settable_ctx_params;
}
static int kdf_scrypt_get_ctx_params(void *vctx, OSSL_PARAM params[])
{
const OSSL_PARAM *p;
KDF_SCRYPT *ctx = vctx;
if (params == NULL)
return 1;
for (p = params; p->key != NULL; p++) {
switch (kdf_scrypt_ctx_lookup(p)) {
default:
break;
case kdf_scrypt_ctx_param_OSSL_KDF_PARAM_PASSWORD:
if (!scrypt_set_membuf(&ctx->pass, &ctx->pass_len, p))
return 0;
break;
case kdf_scrypt_ctx_param_OSSL_KDF_PARAM_SALT:
p->return_size = ctx->salt_len;
if (p->data_size >= ctx->salt_len)
memcpy(p->data, ctx->salt, ctx->salt_len);
break;
case kdf_scrypt_ctx_param_OSSL_KDF_PARAM_SCRYPT_N: {
if (!OSSL_PARAM_set_uint64(p, &ctx->N)
return 0;
break;
}
case kdf_scrypt_ctx_param_OSSL_KDF_PARAM_SCRYPT_R: {
if (!OSSL_PARAM_set_uint64(p, &ctx->r)
return 0;
break;
}
case kdf_scrypt_ctx_param_OSSL_KDF_PARAM_SCRYPT_P: {
if (!OSSL_PARAM_set_uint64(p, &ctx->p
|