3 * $Id: tlsprf.c,v 1.3 2004/04/08 01:36:15 mdw Exp $
5 * The TLS pseudo-random function
7 * (c) 2001 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Header files ------------------------------------------------------*/
32 #include <mLib/alloc.h>
33 #include <mLib/dstr.h>
42 /*----- The data expansion function ---------------------------------------*/
44 /* --- @tlsdx_init@ --- *
46 * Arguments: @tlsdx_ctx *c@ = pointer to a context
47 * @gmac *m@ = pointer to a generic MAC instance
48 * @const void *sd@ = pointer to the seed block
49 * @size_t sdsz@ = size of the seed block
53 * Use: Initializes a context for the TLS data expansion function.
54 * This doesn't take ownership of the MAC instance or the seed
55 * memory, nor does it allocate copies.
58 void tlsdx_init(tlsdx_ctx *c, gmac *m, const void *sd, size_t sdsz)
61 c->hashsz = GM_CLASS(c->k)->hashsz;
62 c->sd = sd; c->sdsz = sdsz;
65 GH_HASH(c->i, sd, sdsz);
66 c->ai = GH_DONE(c->i, 0);
68 GH_HASH(c->o, c->ai, c->hashsz);
69 GH_HASH(c->o, sd, sdsz);
70 c->p = GH_DONE(c->o, 0);
74 /* --- @tlsdx_encrypt@ --- *
76 * Arguments: @tlsdx_ctx *c@ = pointer to a context
77 * @const void *src@ = pointer to source data
78 * @void *dest@ = pointer to destination buffer
79 * @size_t sz@ = size of buffer
83 * Use: Encrypts data using the TLS data expansion function. If the
84 * destination pointer is null, the generator is spun and no
85 * output is produced; if the source pointer is null, raw output
86 * from the generator is written; otherwise, the source data is
87 * XORed with the generator output.
90 void tlsdx_encrypt(tlsdx_ctx *c, const void *src, void *dest, size_t sz)
103 GH_HASH(h, c->ai, c->hashsz);
104 c->ai = GH_DONE(h, 0);
108 h = c->o = GM_INIT(c->k);
109 GH_HASH(h, c->ai, c->hashsz);
110 GH_HASH(h, c->sd, c->sdsz);
111 c->p = GH_DONE(h, 0);
112 c->sz = n = c->hashsz;
120 for (i = 0; i < n; i++) d[i] = s[i] ^ c->p[i];
131 /* --- @tlsdx_free@ --- *
133 * Arguments: @tlsdx_ctx *c@ = pointer to the context block
137 * Use: Frees a context for the TLS data expansion function
140 void tlsdx_free(tlsdx_ctx *c)
146 /* --- Generic random number generator --- */
148 typedef struct dx_grctx {
154 static void dx_grdestroy(grand *r)
156 dx_grctx *g = (dx_grctx *)r;
157 xfree((char *)g->ops.name);
158 xfree((octet *)g->dx.sd);
159 g->dx.k->ops->destroy(g->dx.k);
165 static void dx_seed(dx_grctx *g, const void *p, size_t sz)
168 xfree((octet *)g->dx.sd);
169 g->dx.sd = q = xmalloc(sz);
174 static int dx_grmisc(grand *r, unsigned op, ...)
176 dx_grctx *g = (dx_grctx *)r;
185 switch (va_arg(ap, unsigned)) {
188 case GRAND_SEEDUINT32:
189 case GRAND_SEEDBLOCK:
199 i = va_arg(ap, unsigned);
201 dx_seed(g, buf, sizeof(buf));
203 case GRAND_SEEDUINT32:
204 i = va_arg(ap, uint32);
206 dx_seed(g, buf, sizeof(buf));
208 case GRAND_SEEDBLOCK: {
209 const void *p = va_arg(ap, const void *);
210 size_t sz = va_arg(ap, size_t);
213 case GRAND_SEEDRAND: {
214 grand *rr = va_arg(ap, grand *);
216 rr->ops->fill(rr, buf, sizeof(buf));
217 dx_seed(g, buf, sizeof(buf));
228 static octet dx_grbyte(grand *r)
230 dx_grctx *g = (dx_grctx *)r;
232 tlsdx_encrypt(&g->dx, 0, &o, 1);
236 static uint32 dx_grword(grand *r)
238 dx_grctx *g = (dx_grctx *)r;
240 tlsdx_encrypt(&g->dx, 0, &b, sizeof(b));
244 static void dx_grfill(grand *r, void *p, size_t sz)
246 dx_grctx *g = (dx_grctx *)r;
247 tlsdx_encrypt(&g->dx, 0, p, sz);
250 static const grand_ops dx_grops = {
253 dx_grmisc, dx_grdestroy,
254 dx_grword, dx_grbyte, dx_grword, grand_range, dx_grfill
257 /* ---@tlsdx_rand@ --- *
259 * Arguments: @const gcmac *mc@ = MAC function to use
260 * @const void *k@ = pointer to the key material
261 * @size_t ksz@ = size of the key material
262 * @const void *sd@ = pointer to the seed material
263 * @size_t sdsz@ = size of the seed material
265 * Returns: Pointer to generic random number generator interface.
267 * Use: Creates a generic generator which does TLS data expansion.
270 grand *tlsdx_rand(const gcmac *mc, const void *k, size_t ksz,
271 const void *sd, size_t sdsz)
273 dx_grctx *g = S_CREATE(dx_grctx);
275 gmac *m = GM_KEY(mc, k, ksz);
276 octet *q = xmalloc(sdsz);
278 dstr_putf(&d, "tlsdx(%s)", mc->name);
280 g->ops.name = xstrdup(d.buf);
283 tlsdx_init(&g->dx, m, q, sdsz);
287 /* --- The actual very paranoid PRF ---------------------------------------*/
289 /* --- @tlsprf_init@ --- *
291 * Arguments: @tlsprf_ctx *c@ = pointer to context block
292 * @const gcmac *mcx, *mcy@ = left and right MAC functions
293 * @const void *k@ = pointer to the key material
294 * @size_t ksz@ = size of the key material
295 * @const void *sd@ = pointer to the seed material
296 * @size_t sdsz@ = size of the seed material
300 * Use: Initializes a TLS PRF context.
303 void tlsprf_init(tlsprf_ctx *c, const gcmac *mcx, const gcmac *mcy,
304 const void *k, size_t ksz, const void *sd, size_t sdsz)
306 size_t n = (ksz + 1)/2;
308 tlsdx_init(&c->px, mcx->key(kk, n), sd, sdsz);
309 tlsdx_init(&c->py, mcy->key(kk + ksz - n, n), sd, sdsz);
312 /* --- @tlsprf_encrypt@ --- *
314 * Arguments: @tlsprf_ctx *c@ = pointer to a context
315 * @const void *src@ = pointer to source data
316 * @void *dest@ = pointer to destination buffer
317 * @size_t sz@ = size of buffer
321 * Use: Encrypts data using the TLS pseudo-random function. If the
322 * destination pointer is null, the generator is spun and no
323 * output is produced; if the source pointer is null, raw output
324 * from the generator is written; otherwise, the source data is
325 * XORed with the generator output.
328 void tlsprf_encrypt(tlsprf_ctx *c, const void *src, void *dest, size_t sz)
330 tlsdx_encrypt(&c->px, src, dest, sz);
331 tlsdx_encrypt(&c->py, dest, dest, sz);
334 /* --- @tlsprf_free@ --- *
336 * Arguments: @tlsprf_ctx *c@ = pointer to a context
340 * Use: Frees a TLS PRF context.
343 void tlsprf_free(tlsprf_ctx *c)
345 c->px.k->ops->destroy(c->px.k);
346 c->py.k->ops->destroy(c->py.k);
351 /* --- Generic random number generator --- */
353 typedef struct prf_grctx {
359 static void prf_grdestroy(grand *r)
361 prf_grctx *g = (prf_grctx *)r;
362 xfree((char *)g->ops.name);
363 xfree((octet *)g->prf.px.sd);
364 tlsprf_free(&g->prf);
369 static void prf_seed(prf_grctx *g, const void *p, size_t sz)
373 xfree((octet *)g->prf.px.sz);
374 g->prf.px.sd = g->prf.py.sd = q = xmalloc(sz);
376 g->prf.px.sdsz = g->prf.py.sdsz = sz;
379 static int prf_grmisc(grand *r, unsigned op, ...)
381 prf_grctx *g = (prf_grctx *)r;
390 switch (va_arg(ap, unsigned)) {
393 case GRAND_SEEDUINT32:
394 case GRAND_SEEDBLOCK:
404 i = va_arg(ap, unsigned);
406 prf_seed(g, buf, sizeof(buf));
408 case GRAND_SEEDUINT32:
409 i = va_arg(ap, uint32);
411 prf_seed(g, buf, sizeof(buf));
413 case GRAND_SEEDBLOCK: {
414 const void *p = va_arg(ap, const void *);
415 size_t sz = va_arg(ap, size_t);
418 case GRAND_SEEDRAND: {
419 grand *rr = va_arg(ap, grand *);
421 rr->ops->fill(rr, buf, sizeof(buf));
422 prf_seed(g, buf, sizeof(buf));
433 static octet prf_grbyte(grand *r)
435 prf_grctx *g = (prf_grctx *)r;
437 tlsprf_encrypt(&g->prf, 0, &o, 1);
441 static uint32 prf_grword(grand *r)
443 prf_grctx *g = (prf_grctx *)r;
445 tlsprf_encrypt(&g->prf, 0, &b, sizeof(b));
449 static void prf_grfill(grand *r, void *p, size_t sz)
451 prf_grctx *g = (prf_grctx *)r;
452 tlsprf_encrypt(&g->prf, 0, p, sz);
455 static const grand_ops prf_grops = {
458 prf_grmisc, prf_grdestroy,
459 prf_grword, prf_grbyte, prf_grword, grand_range, prf_grfill
462 /* ---@tlsprf_rand@ --- *
464 * Arguments: @const gcmac *mcx, *mcy@ = MAC function to use
465 * @const void *k@ = pointer to the key material
466 * @size_t ksz@ = size of the key material
467 * @const void *sd@ = pointer to the seed material
468 * @size_t sdsz@ = size of the seed material
470 * Returns: Pointer to generic random number generator interface.
472 * Use: Creates a generic generator which does TLS data expansion.
475 grand *tlsprf_rand(const gcmac *mcx, const gcmac *mcy,
476 const void *k, size_t ksz, const void *sd, size_t sdsz)
478 prf_grctx *g = S_CREATE(prf_grctx);
480 octet *q = xmalloc(sdsz);
482 dstr_putf(&d, "tlsprf(%s,%s)", mcx->name, mcy->name);
484 g->ops.name = xstrdup(d.buf);
487 tlsprf_init(&g->prf, mcx, mcy, k, ksz, q, sdsz);
491 /*----- Test rig ----------------------------------------------------------*/
498 #include <mLib/quis.h>
499 #include <mLib/testrig.h>
501 #include "sha-hmac.h"
502 #include "md5-hmac.h"
504 static int v_generate(dstr *v)
510 g = tlsprf_rand(&md5_hmac, &sha_hmac,
511 v[0].buf, v[0].len, v[1].buf, v[1].len);
512 dstr_ensure(&d, v[2].len);
514 g->ops->fill(g, d.buf, d.len);
516 if (memcmp(v[2].buf, d.buf, d.len) != 0) {
518 printf("\nfail tlsprf:"
520 type_hex.dump(&v[0], stdout);
521 printf("\n\tseed = "); type_hex.dump(&v[1], stdout);
522 printf("\n\texpected = "); type_hex.dump(&v[2], stdout);
523 printf("\n\tcalculated = "); type_hex.dump(&d, stdout);
529 static test_chunk defs[] = {
530 { "tlsprf", v_generate, { &type_hex, &type_hex, &type_hex, 0 } },
534 int main(int argc, char *argv[])
536 test_run(argc, argv, defs, SRCDIR"/tests/tlsprf");
542 /*----- That's all, folks -------------------------------------------------*/