3 * $Id: tlsprf.c,v 1.2 2001/04/06 22:05:53 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.2 2001/04/06 22:05:53 mdw
34 * Change dummy names in grand objects so that they say what sort of thing
37 * Revision 1.1 2001/04/04 20:10:52 mdw
38 * Add support for the TLS pseudo-random function.
42 /*----- Header files ------------------------------------------------------*/
44 #include <mLib/alloc.h>
45 #include <mLib/dstr.h>
54 /*----- The data expansion function ---------------------------------------*/
56 /* --- @tlsdx_init@ --- *
58 * Arguments: @tlsdx_ctx *c@ = pointer to a context
59 * @gmac *m@ = pointer to a generic MAC instance
60 * @const void *sd@ = pointer to the seed block
61 * @size_t sdsz@ = size of the seed block
65 * Use: Initializes a context for the TLS data expansion function.
66 * This doesn't take ownership of the MAC instance or the seed
67 * memory, nor does it allocate copies.
70 void tlsdx_init(tlsdx_ctx
*c
, gmac
*m
, const void *sd
, size_t sdsz
)
73 c
->hashsz
= c
->k
->ops
->c
->hashsz
;
74 c
->sd
= sd
; c
->sdsz
= sdsz
;
76 c
->i
= c
->k
->ops
->init(c
->k
);
77 c
->i
->ops
->hash(c
->i
, sd
, sdsz
);
78 c
->ai
= c
->i
->ops
->done(c
->i
, 0);
79 c
->o
= c
->k
->ops
->init(c
->k
);
80 c
->o
->ops
->hash(c
->o
, c
->ai
, c
->hashsz
);
81 c
->o
->ops
->hash(c
->o
, sd
, sdsz
);
82 c
->p
= c
->o
->ops
->done(c
->o
, 0);
86 /* --- @tlsdx_encrypt@ --- *
88 * Arguments: @tlsdx_ctx *c@ = pointer to a context
89 * @const void *src@ = pointer to source data
90 * @void *dest@ = pointer to destination buffer
91 * @size_t sz@ = size of buffer
95 * Use: Encrypts data using the TLS data expansion function. If the
96 * destination pointer is null, the generator is spun and no
97 * output is produced; if the source pointer is null, raw output
98 * from the generator is written; otherwise, the source data is
99 * XORed with the generator output.
102 void tlsdx_encrypt(tlsdx_ctx
*c
, const void *src
, void *dest
, size_t sz
)
104 const octet
*s
= src
;
114 h
= c
->k
->ops
->init(c
->k
);
115 h
->ops
->hash(h
, c
->ai
, c
->hashsz
);
116 c
->ai
= h
->ops
->done(h
, 0);
117 c
->i
->ops
->destroy(c
->i
);
119 c
->o
->ops
->destroy(c
->o
);
120 h
= c
->o
= c
->k
->ops
->init(c
->k
);
121 h
->ops
->hash(h
, c
->ai
, c
->hashsz
);
122 h
->ops
->hash(h
, c
->sd
, c
->sdsz
);
123 c
->p
= h
->ops
->done(h
, 0);
124 c
->sz
= n
= c
->hashsz
;
132 for (i
= 0; i
< n
; i
++) d
[i
] = s
[i
] ^ c
->p
[i
];
143 /* --- @tlsdx_free@ --- *
145 * Arguments: @tlsdx_ctx *c@ = pointer to the context block
149 * Use: Frees a context for the TLS data expansion function
152 void tlsdx_free(tlsdx_ctx
*c
)
154 c
->i
->ops
->destroy(c
->i
);
155 c
->o
->ops
->destroy(c
->o
);
158 /* --- Generic random number generator --- */
160 typedef struct dx_grctx
{
166 static void dx_grdestroy(grand
*r
)
168 dx_grctx
*g
= (dx_grctx
*)r
;
169 xfree((char *)g
->ops
.name
);
170 xfree((octet
*)g
->dx
.sd
);
171 g
->dx
.k
->ops
->destroy(g
->dx
.k
);
177 static void dx_seed(dx_grctx
*g
, const void *p
, size_t sz
)
180 xfree((octet
*)g
->dx
.sd
);
181 g
->dx
.sd
= q
= xmalloc(sz
);
186 static int dx_grmisc(grand
*r
, unsigned op
, ...)
188 dx_grctx
*g
= (dx_grctx
*)r
;
197 switch (va_arg(ap
, unsigned)) {
200 case GRAND_SEEDUINT32
:
201 case GRAND_SEEDBLOCK
:
211 i
= va_arg(ap
, unsigned);
213 dx_seed(g
, buf
, sizeof(buf
));
215 case GRAND_SEEDUINT32
:
216 i
= va_arg(ap
, uint32
);
218 dx_seed(g
, buf
, sizeof(buf
));
220 case GRAND_SEEDBLOCK
: {
221 const void *p
= va_arg(ap
, const void *);
222 size_t sz
= va_arg(ap
, size_t);
225 case GRAND_SEEDRAND
: {
226 grand
*rr
= va_arg(ap
, grand
*);
228 rr
->ops
->fill(rr
, buf
, sizeof(buf
));
229 dx_seed(g
, buf
, sizeof(buf
));
240 static octet
dx_grbyte(grand
*r
)
242 dx_grctx
*g
= (dx_grctx
*)r
;
244 tlsdx_encrypt(&g
->dx
, 0, &o
, 1);
248 static uint32
dx_grword(grand
*r
)
250 dx_grctx
*g
= (dx_grctx
*)r
;
252 tlsdx_encrypt(&g
->dx
, 0, &b
, sizeof(b
));
256 static void dx_grfill(grand
*r
, void *p
, size_t sz
)
258 dx_grctx
*g
= (dx_grctx
*)r
;
259 tlsdx_encrypt(&g
->dx
, 0, p
, sz
);
262 static const grand_ops dx_grops
= {
265 dx_grmisc
, dx_grdestroy
,
266 dx_grword
, dx_grbyte
, dx_grword
, grand_range
, dx_grfill
269 /* ---@tlsdx_rand@ --- *
271 * Arguments: @const gcmac *mc@ = MAC function to use
272 * @const void *k@ = pointer to the key material
273 * @size_t ksz@ = size of the key material
274 * @const void *sd@ = pointer to the seed material
275 * @size_t sdsz@ = size of the seed material
277 * Returns: Pointer to generic random number generator interface.
279 * Use: Creates a generic generator which does TLS data expansion.
282 grand
*tlsdx_rand(const gcmac
*mc
, const void *k
, size_t ksz
,
283 const void *sd
, size_t sdsz
)
285 dx_grctx
*g
= S_CREATE(dx_grctx
);
287 gmac
*m
= mc
->key(k
, ksz
);
288 octet
*q
= xmalloc(sdsz
);
290 dstr_putf(&d
, "tlsdx(%s)", mc
->name
);
292 g
->ops
.name
= xstrdup(d
.buf
);
295 tlsdx_init(&g
->dx
, m
, q
, sdsz
);
299 /* --- The actual very paranoid PRF ---------------------------------------*/
301 /* --- @tlsprf_init@ --- *
303 * Arguments: @tlsprf_ctx *c@ = pointer to context block
304 * @const gcmac *mcx, *mcy@ = left and right MAC functions
305 * @const void *k@ = pointer to the key material
306 * @size_t ksz@ = size of the key material
307 * @const void *sd@ = pointer to the seed material
308 * @size_t sdsz@ = size of the seed material
312 * Use: Initializes a TLS PRF context.
315 void tlsprf_init(tlsprf_ctx
*c
, const gcmac
*mcx
, const gcmac
*mcy
,
316 const void *k
, size_t ksz
, const void *sd
, size_t sdsz
)
318 size_t n
= (ksz
+ 1)/2;
320 tlsdx_init(&c
->px
, mcx
->key(kk
, n
), sd
, sdsz
);
321 tlsdx_init(&c
->py
, mcy
->key(kk
+ ksz
- n
, n
), sd
, sdsz
);
324 /* --- @tlsprf_encrypt@ --- *
326 * Arguments: @tlsprf_ctx *c@ = pointer to a context
327 * @const void *src@ = pointer to source data
328 * @void *dest@ = pointer to destination buffer
329 * @size_t sz@ = size of buffer
333 * Use: Encrypts data using the TLS pseudo-random function. If the
334 * destination pointer is null, the generator is spun and no
335 * output is produced; if the source pointer is null, raw output
336 * from the generator is written; otherwise, the source data is
337 * XORed with the generator output.
340 void tlsprf_encrypt(tlsprf_ctx
*c
, const void *src
, void *dest
, size_t sz
)
342 tlsdx_encrypt(&c
->px
, src
, dest
, sz
);
343 tlsdx_encrypt(&c
->py
, dest
, dest
, sz
);
346 /* --- @tlsprf_free@ --- *
348 * Arguments: @tlsprf_ctx *c@ = pointer to a context
352 * Use: Frees a TLS PRF context.
355 void tlsprf_free(tlsprf_ctx
*c
)
357 c
->px
.k
->ops
->destroy(c
->px
.k
);
358 c
->py
.k
->ops
->destroy(c
->py
.k
);
363 /* --- Generic random number generator --- */
365 typedef struct prf_grctx
{
371 static void prf_grdestroy(grand
*r
)
373 prf_grctx
*g
= (prf_grctx
*)r
;
374 xfree((char *)g
->ops
.name
);
375 xfree((octet
*)g
->prf
.px
.sd
);
376 tlsprf_free(&g
->prf
);
381 static void prf_seed(prf_grctx
*g
, const void *p
, size_t sz
)
385 xfree((octet
*)g
->prf
.px
.sz
);
386 g
->prf
.px
.sd
= g
->prf
.py
.sd
= q
= xmalloc(sz
);
388 g
->prf
.px
.sdsz
= g
->prf
.py
.sdsz
= sz
;
391 static int prf_grmisc(grand
*r
, unsigned op
, ...)
393 prf_grctx
*g
= (prf_grctx
*)r
;
402 switch (va_arg(ap
, unsigned)) {
405 case GRAND_SEEDUINT32
:
406 case GRAND_SEEDBLOCK
:
416 i
= va_arg(ap
, unsigned);
418 prf_seed(g
, buf
, sizeof(buf
));
420 case GRAND_SEEDUINT32
:
421 i
= va_arg(ap
, uint32
);
423 prf_seed(g
, buf
, sizeof(buf
));
425 case GRAND_SEEDBLOCK
: {
426 const void *p
= va_arg(ap
, const void *);
427 size_t sz
= va_arg(ap
, size_t);
430 case GRAND_SEEDRAND
: {
431 grand
*rr
= va_arg(ap
, grand
*);
433 rr
->ops
->fill(rr
, buf
, sizeof(buf
));
434 prf_seed(g
, buf
, sizeof(buf
));
445 static octet
prf_grbyte(grand
*r
)
447 prf_grctx
*g
= (prf_grctx
*)r
;
449 tlsprf_encrypt(&g
->prf
, 0, &o
, 1);
453 static uint32
prf_grword(grand
*r
)
455 prf_grctx
*g
= (prf_grctx
*)r
;
457 tlsprf_encrypt(&g
->prf
, 0, &b
, sizeof(b
));
461 static void prf_grfill(grand
*r
, void *p
, size_t sz
)
463 prf_grctx
*g
= (prf_grctx
*)r
;
464 tlsprf_encrypt(&g
->prf
, 0, p
, sz
);
467 static const grand_ops prf_grops
= {
470 prf_grmisc
, prf_grdestroy
,
471 prf_grword
, prf_grbyte
, prf_grword
, grand_range
, prf_grfill
474 /* ---@tlsprf_rand@ --- *
476 * Arguments: @const gcmac *mcx, *mcy@ = MAC function to use
477 * @const void *k@ = pointer to the key material
478 * @size_t ksz@ = size of the key material
479 * @const void *sd@ = pointer to the seed material
480 * @size_t sdsz@ = size of the seed material
482 * Returns: Pointer to generic random number generator interface.
484 * Use: Creates a generic generator which does TLS data expansion.
487 grand
*tlsprf_rand(const gcmac
*mcx
, const gcmac
*mcy
,
488 const void *k
, size_t ksz
, const void *sd
, size_t sdsz
)
490 prf_grctx
*g
= S_CREATE(prf_grctx
);
492 octet
*q
= xmalloc(sdsz
);
494 dstr_putf(&d
, "tlsprf(%s,%s)", mcx
->name
, mcy
->name
);
496 g
->ops
.name
= xstrdup(d
.buf
);
499 tlsprf_init(&g
->prf
, mcx
, mcy
, k
, ksz
, q
, sdsz
);
503 /*----- Test rig ----------------------------------------------------------*/
510 #include <mLib/quis.h>
511 #include <mLib/testrig.h>
513 #include "sha-hmac.h"
514 #include "md5-hmac.h"
516 static int v_generate(dstr
*v
)
522 g
= tlsprf_rand(&md5_hmac
, &sha_hmac
,
523 v
[0].buf
, v
[0].len
, v
[1].buf
, v
[1].len
);
524 dstr_ensure(&d
, v
[2].len
);
526 g
->ops
->fill(g
, d
.buf
, d
.len
);
528 if (memcmp(v
[2].buf
, d
.buf
, d
.len
) != 0) {
530 printf("\nfail tlsprf:"
532 type_hex
.dump(&v
[0], stdout
);
533 printf("\n\tseed = "); type_hex
.dump(&v
[1], stdout
);
534 printf("\n\texpected = "); type_hex
.dump(&v
[2], stdout
);
535 printf("\n\tcalculated = "); type_hex
.dump(&d
, stdout
);
541 static test_chunk defs
[] = {
542 { "tlsprf", v_generate
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
546 int main(int argc
, char *argv
[])
548 test_run(argc
, argv
, defs
, SRCDIR
"/tests/tlsprf");
554 /*----- That's all, folks -------------------------------------------------*/