3 * $Id: tlsprf.c,v 1.1 2001/04/04 20:10:52 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 /*----- Revision history --------------------------------------------------*
33 * Revision 1.1 2001/04/04 20:10:52 mdw
34 * Add support for the TLS pseudo-random function.
38 /*----- Header files ------------------------------------------------------*/
40 #include <mLib/alloc.h>
41 #include <mLib/dstr.h>
50 /*----- The data expansion function ---------------------------------------*/
52 /* --- @tlsdx_init@ --- *
54 * Arguments: @tlsdx_ctx *c@ = pointer to a context
55 * @gmac *m@ = pointer to a generic MAC instance
56 * @const void *sd@ = pointer to the seed block
57 * @size_t sdsz@ = size of the seed block
61 * Use: Initializes a context for the TLS data expansion function.
62 * This doesn't take ownership of the MAC instance or the seed
63 * memory, nor does it allocate copies.
66 void tlsdx_init(tlsdx_ctx
*c
, gmac
*m
, const void *sd
, size_t sdsz
)
69 c
->hashsz
= c
->k
->ops
->c
->hashsz
;
70 c
->sd
= sd
; c
->sdsz
= sdsz
;
71 c
->i
= c
->k
->ops
->init(c
->k
);
72 c
->i
->ops
->hash(c
->i
, sd
, sdsz
);
73 c
->ai
= c
->i
->ops
->done(c
->i
, 0);
74 c
->o
= c
->k
->ops
->init(c
->k
);
75 c
->o
->ops
->hash(c
->o
, c
->ai
, c
->hashsz
);
76 c
->o
->ops
->hash(c
->o
, sd
, sdsz
);
77 c
->p
= c
->o
->ops
->done(c
->o
, 0);
81 /* --- @tlsdx_encrypt@ --- *
83 * Arguments: @tlsdx_ctx *c@ = pointer to a context
84 * @const void *src@ = pointer to source data
85 * @void *dest@ = pointer to destination buffer
86 * @size_t sz@ = size of buffer
90 * Use: Encrypts data using the TLS data expansion function. If the
91 * destination pointer is null, the generator is spun and no
92 * output is produced; if the source pointer is null, raw output
93 * from the generator is written; otherwise, the source data is
94 * XORed with the generator output.
97 void tlsdx_encrypt(tlsdx_ctx
*c
, const void *src
, void *dest
, size_t sz
)
109 h
= c
->k
->ops
->init(c
->k
);
110 h
->ops
->hash(h
, c
->ai
, c
->hashsz
);
111 c
->ai
= h
->ops
->done(h
, 0);
112 c
->i
->ops
->destroy(c
->i
);
114 c
->o
->ops
->destroy(c
->o
);
115 h
= c
->o
= c
->k
->ops
->init(c
->k
);
116 h
->ops
->hash(h
, c
->ai
, c
->hashsz
);
117 h
->ops
->hash(h
, c
->sd
, c
->sdsz
);
118 c
->p
= h
->ops
->done(h
, 0);
119 c
->sz
= n
= c
->hashsz
;
127 for (i
= 0; i
< n
; i
++) d
[i
] = s
[i
] ^ c
->p
[i
];
138 /* --- @tlsdx_free@ --- *
140 * Arguments: @tlsdx_ctx *c@ = pointer to the context block
144 * Use: Frees a context for the TLS data expansion function
147 void tlsdx_free(tlsdx_ctx
*c
)
149 c
->i
->ops
->destroy(c
->i
);
150 c
->o
->ops
->destroy(c
->o
);
153 /* --- Generic random number generator --- */
155 typedef struct dx_grctx
{
161 static void dx_grdestroy(grand
*r
)
163 dx_grctx
*g
= (dx_grctx
*)r
;
164 xfree((char *)g
->ops
.name
);
165 xfree((octet
*)g
->dx
.sd
);
166 g
->dx
.k
->ops
->destroy(g
->dx
.k
);
172 static void dx_seed(dx_grctx
*g
, const void *p
, size_t sz
)
175 xfree((octet
*)g
->dx
.sd
);
176 g
->dx
.sd
= q
= xmalloc(sz
);
181 static int dx_grmisc(grand
*r
, unsigned op
, ...)
183 dx_grctx
*g
= (dx_grctx
*)r
;
192 switch (va_arg(ap
, unsigned)) {
195 case GRAND_SEEDUINT32
:
196 case GRAND_SEEDBLOCK
:
206 i
= va_arg(ap
, unsigned);
208 dx_seed(g
, buf
, sizeof(buf
));
210 case GRAND_SEEDUINT32
:
211 i
= va_arg(ap
, uint32
);
213 dx_seed(g
, buf
, sizeof(buf
));
215 case GRAND_SEEDBLOCK
: {
216 const void *p
= va_arg(ap
, const void *);
217 size_t sz
= va_arg(ap
, size_t);
220 case GRAND_SEEDRAND
: {
221 grand
*rr
= va_arg(ap
, grand
*);
223 rr
->ops
->fill(rr
, buf
, sizeof(buf
));
224 dx_seed(g
, buf
, sizeof(buf
));
235 static octet
dx_grbyte(grand
*r
)
237 dx_grctx
*g
= (dx_grctx
*)r
;
239 tlsdx_encrypt(&g
->dx
, 0, &o
, 1);
243 static uint32
dx_grword(grand
*r
)
245 dx_grctx
*g
= (dx_grctx
*)r
;
247 tlsdx_encrypt(&g
->dx
, 0, &b
, sizeof(b
));
251 static void dx_grfill(grand
*r
, void *p
, size_t sz
)
253 dx_grctx
*g
= (dx_grctx
*)r
;
254 tlsdx_encrypt(&g
->dx
, 0, p
, sz
);
257 static const grand_ops dx_grops
= {
260 dx_grmisc
, dx_grdestroy
,
261 dx_grword
, dx_grbyte
, dx_grword
, grand_range
, dx_grfill
264 /* ---@tlsdx_rand@ --- *
266 * Arguments: @const gcmac *mc@ = MAC function to use
267 * @const void *k@ = pointer to the key material
268 * @size_t ksz@ = size of the key material
269 * @const void *sd@ = pointer to the seed material
270 * @size_t sdsz@ = size of the seed material
272 * Returns: Pointer to generic random number generator interface.
274 * Use: Creates a generic generator which does TLS data expansion.
277 grand
*tlsdx_rand(const gcmac
*mc
, const void *k
, size_t ksz
,
278 const void *sd
, size_t sdsz
)
280 dx_grctx
*g
= S_CREATE(dx_grctx
);
282 gmac
*m
= mc
->key(k
, ksz
);
283 octet
*q
= xmalloc(sdsz
);
285 dstr_putf(&d
, "tlsdx(%s)", mc
->name
);
287 g
->ops
.name
= xstrdup(d
.buf
);
290 tlsdx_init(&g
->dx
, m
, q
, sdsz
);
294 /* --- The actual very paranoid PRF ---------------------------------------*/
296 /* --- @tlsprf_init@ --- *
298 * Arguments: @tlsprf_ctx *c@ = pointer to context block
299 * @const gcmac *mcx, *mcy@ = left and right MAC functions
300 * @const void *k@ = pointer to the key material
301 * @size_t ksz@ = size of the key material
302 * @const void *sd@ = pointer to the seed material
303 * @size_t sdsz@ = size of the seed material
307 * Use: Initializes a TLS PRF context.
310 void tlsprf_init(tlsprf_ctx
*c
, const gcmac
*mcx
, const gcmac
*mcy
,
311 const void *k
, size_t ksz
, const void *sd
, size_t sdsz
)
313 size_t n
= (ksz
+ 1)/2;
315 tlsdx_init(&c
->px
, mcx
->key(kk
, n
), sd
, sdsz
);
316 tlsdx_init(&c
->py
, mcy
->key(kk
+ ksz
- n
, n
), sd
, sdsz
);
319 /* --- @tlsprf_encrypt@ --- *
321 * Arguments: @tlsprf_ctx *c@ = pointer to a context
322 * @const void *src@ = pointer to source data
323 * @void *dest@ = pointer to destination buffer
324 * @size_t sz@ = size of buffer
328 * Use: Encrypts data using the TLS pseudo-random function. If the
329 * destination pointer is null, the generator is spun and no
330 * output is produced; if the source pointer is null, raw output
331 * from the generator is written; otherwise, the source data is
332 * XORed with the generator output.
335 void tlsprf_encrypt(tlsprf_ctx
*c
, const void *src
, void *dest
, size_t sz
)
337 tlsdx_encrypt(&c
->px
, src
, dest
, sz
);
338 tlsdx_encrypt(&c
->py
, dest
, dest
, sz
);
341 /* --- @tlsprf_free@ --- *
343 * Arguments: @tlsprf_ctx *c@ = pointer to a context
347 * Use: Frees a TLS PRF context.
350 void tlsprf_free(tlsprf_ctx
*c
)
352 c
->px
.k
->ops
->destroy(c
->px
.k
);
353 c
->py
.k
->ops
->destroy(c
->py
.k
);
358 /* --- Generic random number generator --- */
360 typedef struct prf_grctx
{
366 static void prf_grdestroy(grand
*r
)
368 prf_grctx
*g
= (prf_grctx
*)r
;
369 xfree((char *)g
->ops
.name
);
370 xfree((octet
*)g
->prf
.px
.sd
);
371 tlsprf_free(&g
->prf
);
376 static void prf_seed(prf_grctx
*g
, const void *p
, size_t sz
)
380 xfree((octet
*)g
->prf
.px
.sz
);
381 g
->prf
.px
.sd
= g
->prf
.py
.sd
= q
= xmalloc(sz
);
383 g
->prf
.px
.sdsz
= g
->prf
.py
.sdsz
= sz
;
386 static int prf_grmisc(grand
*r
, unsigned op
, ...)
388 prf_grctx
*g
= (prf_grctx
*)r
;
397 switch (va_arg(ap
, unsigned)) {
400 case GRAND_SEEDUINT32
:
401 case GRAND_SEEDBLOCK
:
411 i
= va_arg(ap
, unsigned);
413 prf_seed(g
, buf
, sizeof(buf
));
415 case GRAND_SEEDUINT32
:
416 i
= va_arg(ap
, uint32
);
418 prf_seed(g
, buf
, sizeof(buf
));
420 case GRAND_SEEDBLOCK
: {
421 const void *p
= va_arg(ap
, const void *);
422 size_t sz
= va_arg(ap
, size_t);
425 case GRAND_SEEDRAND
: {
426 grand
*rr
= va_arg(ap
, grand
*);
428 rr
->ops
->fill(rr
, buf
, sizeof(buf
));
429 prf_seed(g
, buf
, sizeof(buf
));
440 static octet
prf_grbyte(grand
*r
)
442 prf_grctx
*g
= (prf_grctx
*)r
;
444 tlsprf_encrypt(&g
->prf
, 0, &o
, 1);
448 static uint32
prf_grword(grand
*r
)
450 prf_grctx
*g
= (prf_grctx
*)r
;
452 tlsprf_encrypt(&g
->prf
, 0, &b
, sizeof(b
));
456 static void prf_grfill(grand
*r
, void *p
, size_t sz
)
458 prf_grctx
*g
= (prf_grctx
*)r
;
459 tlsprf_encrypt(&g
->prf
, 0, p
, sz
);
462 static const grand_ops prf_grops
= {
465 prf_grmisc
, prf_grdestroy
,
466 prf_grword
, prf_grbyte
, prf_grword
, grand_range
, prf_grfill
469 /* ---@tlsprf_rand@ --- *
471 * Arguments: @const gcmac *mcx, *mcy@ = MAC function to use
472 * @const void *k@ = pointer to the key material
473 * @size_t ksz@ = size of the key material
474 * @const void *sd@ = pointer to the seed material
475 * @size_t sdsz@ = size of the seed material
477 * Returns: Pointer to generic random number generator interface.
479 * Use: Creates a generic generator which does TLS data expansion.
482 grand
*tlsprf_rand(const gcmac
*mcx
, const gcmac
*mcy
,
483 const void *k
, size_t ksz
, const void *sd
, size_t sdsz
)
485 prf_grctx
*g
= S_CREATE(prf_grctx
);
487 octet
*q
= xmalloc(sdsz
);
489 dstr_putf(&d
, "tlsprf(%s,%s)", mcx
->name
, mcy
->name
);
491 g
->ops
.name
= xstrdup(d
.buf
);
494 tlsprf_init(&g
->prf
, mcx
, mcy
, k
, ksz
, q
, sdsz
);
498 /*----- Test rig ----------------------------------------------------------*/
505 #include <mLib/quis.h>
506 #include <mLib/testrig.h>
508 #include "sha-hmac.h"
509 #include "md5-hmac.h"
511 static int v_generate(dstr
*v
)
517 g
= tlsprf_rand(&md5_hmac
, &sha_hmac
,
518 v
[0].buf
, v
[0].len
, v
[1].buf
, v
[1].len
);
519 dstr_ensure(&d
, v
[2].len
);
521 g
->ops
->fill(g
, d
.buf
, d
.len
);
523 if (memcmp(v
[2].buf
, d
.buf
, d
.len
) != 0) {
525 printf("\nfail tlsprf:"
527 type_hex
.dump(&v
[0], stdout
);
528 printf("\n\tseed = "); type_hex
.dump(&v
[1], stdout
);
529 printf("\n\texpected = "); type_hex
.dump(&v
[2], stdout
);
530 printf("\n\tcalculated = "); type_hex
.dump(&d
, stdout
);
536 static test_chunk defs
[] = {
537 { "tlsprf", v_generate
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
541 int main(int argc
, char *argv
[])
543 test_run(argc
, argv
, defs
, SRCDIR
"/tests/tlsprf");
549 /*----- That's all, folks -------------------------------------------------*/