3 * $Id: sslprf.c,v 1.2 2004/04/08 01:36:15 mdw Exp $
5 * The SSL 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 /*----- Main code ---------------------------------------------------------*/
46 * Arguments: @sslprf_ctx *c@ = pointer to context structure
50 * Use: Steps the generator.
53 static void step(sslprf_ctx *c)
63 for (sz = c->i++; sz > 0; sz -= n) {
70 GH_HASH(h, c->k, c->ksz);
71 GH_HASH(h, c->sd, c->sdsz);
75 GH_HASH(hh, c->k, c->ksz);
76 GH_HASH(hh, p, c->ihashsz);
77 c->p = GH_DONE(hh, 0);
84 /* --- @sslprf_init@ --- *
86 * Arguments: @sslprf_ctx *c@ = pointer to a context structure
87 * @const gchash *hco, *hci@ = outer and inner hash functions
88 * @const void *k@ = pointer to secret buffer
89 * @size_t ksz@ = size of the secret
90 * @const void *sd@ = pointer to seed buffer
91 * @size_t sdsz@ = size of the seed
95 * Use: Initializes an SSL generator context.
98 void sslprf_init(sslprf_ctx *c, const gchash *hco, const gchash *hci,
99 const void *k, size_t ksz,
100 const void *sd, size_t sdsz)
102 c->co = hco; c->ci = hci;
103 c->ohashsz = hco->hashsz; c->ihashsz = hci->hashsz;
104 c->k = k; c->ksz = ksz; c->sd = sd; c->sdsz = sdsz;
109 /* --- @sslprf_encrypt@ --- *
111 * Arguments: @sslprf_ctx *c@ = pointer to a context structure
112 * @const void *src@ = pointer to source buffer
113 * @void *dest@ = pointer to destination buffer
114 * @size_t sz@ = size of the buffers
118 * Use: Encrypts data using the SSL pseudo-random function. If the
119 * destination pointer is null, the generator is spun and no
120 * output is produced; if the source pointer is null, raw output
121 * from the generator is written; otherwise, the source data is
122 * XORed with the generator output.
125 void sslprf_encrypt(sslprf_ctx *c, const void *src, void *dest, size_t sz)
127 const octet *s = src;
143 for (i = 0; i < n; i++) d[i] = s[i] ^ c->p[i];
154 /* --- @sslprf_free@ --- *
156 * Arguments: @sslprf_ctx@ = pointer to a context
160 * Use: Frees resources held in an SSL generator context.
163 void sslprf_free(sslprf_ctx *c)
168 /* --- Generic random number generator --- */
170 typedef struct grctx {
176 static void grdestroy(grand *r)
178 grctx *g = (grctx *)r;
179 xfree((char *)g->ops.name);
180 xfree((octet *)g->prf.sd);
181 sslprf_free(&g->prf);
186 static void seed(grctx *g, const void *p, size_t sz)
189 xfree((octet *)g->prf.sd);
190 g->prf.sd = q = xmalloc(sz);
195 static int grmisc(grand *r, unsigned op, ...)
197 grctx *g = (grctx *)r;
206 switch (va_arg(ap, unsigned)) {
209 case GRAND_SEEDUINT32:
210 case GRAND_SEEDBLOCK:
220 i = va_arg(ap, unsigned);
222 seed(g, buf, sizeof(buf));
224 case GRAND_SEEDUINT32:
225 i = va_arg(ap, uint32);
227 seed(g, buf, sizeof(buf));
229 case GRAND_SEEDBLOCK: {
230 const void *p = va_arg(ap, const void *);
231 size_t sz = va_arg(ap, size_t);
234 case GRAND_SEEDRAND: {
235 grand *rr = va_arg(ap, grand *);
237 rr->ops->fill(rr, buf, sizeof(buf));
238 seed(g, buf, sizeof(buf));
249 static octet grbyte(grand *r)
251 grctx *g = (grctx *)r;
253 sslprf_encrypt(&g->prf, 0, &o, 1);
257 static uint32 grword(grand *r)
259 grctx *g = (grctx *)r;
261 sslprf_encrypt(&g->prf, 0, &b, sizeof(b));
265 static void grfill(grand *r, void *p, size_t sz)
267 grctx *g = (grctx *)r;
268 sslprf_encrypt(&g->prf, 0, p, sz);
271 static const grand_ops grops = {
275 grword, grbyte, grword, grand_range, grfill
278 /* ---@sslprf_rand@ --- *
280 * Arguments: @const gchash *hco, const gchash *hci@ = hash functions
281 * @const void *k@ = pointer to the key material
282 * @size_t ksz@ = size of the key material
283 * @const void *sd@ = pointer to the seed material
284 * @size_t sdsz@ = size of the seed material
286 * Returns: Pointer to generic random number generator interface.
288 * Use: Creates a generic generator which does TLS data expansion.
291 grand *sslprf_rand(const gchash *hco, const gchash *hci,
292 const void *k, size_t ksz,
293 const void *sd, size_t sdsz)
295 grctx *g = S_CREATE(grctx);
297 octet *q = xmalloc(sdsz);
299 dstr_putf(&d, "sslprf(%s,%s)", hco->name, hci->name);
301 g->ops.name = xstrdup(d.buf);
304 sslprf_init(&g->prf, hco, hci, k, ksz, q, sdsz);
308 /*----- Test rig ----------------------------------------------------------*/
315 #include <mLib/quis.h>
316 #include <mLib/testrig.h>
321 static int v_generate(dstr *v)
327 g = sslprf_rand(&md5, &sha, v[0].buf, v[0].len, v[1].buf, v[1].len);
328 dstr_ensure(&d, v[2].len);
330 g->ops->fill(g, d.buf, d.len);
332 if (memcmp(v[2].buf, d.buf, d.len) != 0) {
334 printf("\nfail sslprf:"
336 type_hex.dump(&v[0], stdout);
337 printf("\n\tseed = "); type_hex.dump(&v[1], stdout);
338 printf("\n\texpected = "); type_hex.dump(&v[2], stdout);
339 printf("\n\tcalculated = "); type_hex.dump(&d, stdout);
345 static test_chunk defs[] = {
346 { "sslprf", v_generate, { &type_hex, &type_hex, &type_hex, 0 } },
350 int main(int argc, char *argv[])
352 test_run(argc, argv, defs, SRCDIR"/tests/sslprf");
358 /*----- That's all, folks -------------------------------------------------*/