3 * The SSL pseudo-random function
5 * (c) 2001 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
30 #include <mLib/alloc.h>
31 #include <mLib/dstr.h>
40 /*----- Main code ---------------------------------------------------------*/
44 * Arguments: @sslprf_ctx *c@ = pointer to context structure
48 * Use: Steps the generator.
51 static void step(sslprf_ctx *c)
61 for (sz = c->i++; sz > 0; sz -= n) {
68 GH_HASH(h, c->k, c->ksz);
69 GH_HASH(h, c->sd, c->sdsz);
73 GH_HASH(hh, c->k, c->ksz);
74 GH_HASH(hh, p, c->ihashsz);
75 c->p = GH_DONE(hh, 0);
82 /* --- @sslprf_init@ --- *
84 * Arguments: @sslprf_ctx *c@ = pointer to a context structure
85 * @const gchash *hco, *hci@ = outer and inner hash functions
86 * @const void *k@ = pointer to secret buffer
87 * @size_t ksz@ = size of the secret
88 * @const void *sd@ = pointer to seed buffer
89 * @size_t sdsz@ = size of the seed
93 * Use: Initializes an SSL generator context.
96 void sslprf_init(sslprf_ctx *c, const gchash *hco, const gchash *hci,
97 const void *k, size_t ksz,
98 const void *sd, size_t sdsz)
100 c->co = hco; c->ci = hci;
101 c->ohashsz = hco->hashsz; c->ihashsz = hci->hashsz;
102 c->k = k; c->ksz = ksz; c->sd = sd; c->sdsz = sdsz;
107 /* --- @sslprf_encrypt@ --- *
109 * Arguments: @sslprf_ctx *c@ = pointer to a context structure
110 * @const void *src@ = pointer to source buffer
111 * @void *dest@ = pointer to destination buffer
112 * @size_t sz@ = size of the buffers
116 * Use: Encrypts data using the SSL pseudo-random function. If the
117 * destination pointer is null, the generator is spun and no
118 * output is produced; if the source pointer is null, raw output
119 * from the generator is written; otherwise, the source data is
120 * XORed with the generator output.
123 void sslprf_encrypt(sslprf_ctx *c, const void *src, void *dest, size_t sz)
125 const octet *s = src;
141 for (i = 0; i < n; i++) d[i] = s[i] ^ c->p[i];
152 /* --- @sslprf_free@ --- *
154 * Arguments: @sslprf_ctx@ = pointer to a context
158 * Use: Frees resources held in an SSL generator context.
161 void sslprf_free(sslprf_ctx *c)
166 /* --- Generic random number generator --- */
168 typedef struct grctx {
174 static void grdestroy(grand *r)
176 grctx *g = (grctx *)r;
177 xfree((char *)g->ops.name);
178 xfree((octet *)g->prf.sd);
179 sslprf_free(&g->prf);
184 static void seed(grctx *g, const void *p, size_t sz)
187 xfree((octet *)g->prf.sd);
188 g->prf.sd = q = xmalloc(sz);
193 static int grmisc(grand *r, unsigned op, ...)
195 grctx *g = (grctx *)r;
204 switch (va_arg(ap, unsigned)) {
207 case GRAND_SEEDUINT32:
208 case GRAND_SEEDBLOCK:
218 i = va_arg(ap, unsigned);
220 seed(g, buf, sizeof(buf));
222 case GRAND_SEEDUINT32:
223 i = va_arg(ap, uint32);
225 seed(g, buf, sizeof(buf));
227 case GRAND_SEEDBLOCK: {
228 const void *p = va_arg(ap, const void *);
229 size_t sz = va_arg(ap, size_t);
232 case GRAND_SEEDRAND: {
233 grand *rr = va_arg(ap, grand *);
235 rr->ops->fill(rr, buf, sizeof(buf));
236 seed(g, buf, sizeof(buf));
247 static octet grbyte(grand *r)
249 grctx *g = (grctx *)r;
251 sslprf_encrypt(&g->prf, 0, &o, 1);
255 static uint32 grword(grand *r)
257 grctx *g = (grctx *)r;
259 sslprf_encrypt(&g->prf, 0, &b, sizeof(b));
263 static void grfill(grand *r, void *p, size_t sz)
265 grctx *g = (grctx *)r;
266 sslprf_encrypt(&g->prf, 0, p, sz);
269 static const grand_ops grops = {
273 grword, grbyte, grword, grand_defaultrange, grfill
276 /* ---@sslprf_rand@ --- *
278 * Arguments: @const gchash *hco, const gchash *hci@ = hash functions
279 * @const void *k@ = pointer to the key material
280 * @size_t ksz@ = size of the key material
281 * @const void *sd@ = pointer to the seed material
282 * @size_t sdsz@ = size of the seed material
284 * Returns: Pointer to generic random number generator interface.
286 * Use: Creates a generic generator which does TLS data expansion.
289 grand *sslprf_rand(const gchash *hco, const gchash *hci,
290 const void *k, size_t ksz,
291 const void *sd, size_t sdsz)
293 grctx *g = S_CREATE(grctx);
295 octet *q = xmalloc(sdsz);
297 dstr_putf(&d, "sslprf(%s,%s)", hco->name, hci->name);
299 g->ops.name = xstrdup(d.buf);
302 sslprf_init(&g->prf, hco, hci, k, ksz, q, sdsz);
306 /*----- Test rig ----------------------------------------------------------*/
313 #include <mLib/quis.h>
314 #include <mLib/testrig.h>
319 static int v_generate(dstr *v)
325 g = sslprf_rand(&md5, &sha, v[0].buf, v[0].len, v[1].buf, v[1].len);
326 dstr_ensure(&d, v[2].len);
328 g->ops->fill(g, d.buf, d.len);
330 if (memcmp(v[2].buf, d.buf, d.len) != 0) {
332 printf("\nfail sslprf:"
334 type_hex.dump(&v[0], stdout);
335 printf("\n\tseed = "); type_hex.dump(&v[1], stdout);
336 printf("\n\texpected = "); type_hex.dump(&v[2], stdout);
337 printf("\n\tcalculated = "); type_hex.dump(&d, stdout);
343 static test_chunk defs[] = {
344 { "sslprf", v_generate, { &type_hex, &type_hex, &type_hex, 0 } },
348 int main(int argc, char *argv[])
350 test_run(argc, argv, defs, SRCDIR"/t/sslprf");
356 /*----- That's all, folks -------------------------------------------------*/