3 * $Id: sslprf.c,v 1.1 2001/04/06 22:05:10 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 /*----- Revision history --------------------------------------------------*
33 * Revision 1.1 2001/04/06 22:05:10 mdw
34 * Add support for SSL pseudo-random function.
38 /*----- Header files ------------------------------------------------------*/
40 #include <mLib/alloc.h>
41 #include <mLib/dstr.h>
50 /*----- Main code ---------------------------------------------------------*/
54 * Arguments: @sslprf_ctx *c@ = pointer to context structure
58 * Use: Steps the generator.
61 static void step(sslprf_ctx
*c
)
71 for (sz
= c
->i
++; sz
> 0; sz
-= n
) {
76 h
->ops
->hash(h
, buf
, n
);
78 h
->ops
->hash(h
, c
->k
, c
->ksz
);
79 h
->ops
->hash(h
, c
->sd
, c
->sdsz
);
80 p
= h
->ops
->done(h
, 0);
83 hh
->ops
->hash(hh
, c
->k
, c
->ksz
);
84 hh
->ops
->hash(hh
, p
, c
->ihashsz
);
85 c
->p
= hh
->ops
->done(hh
, 0);
92 /* --- @sslprf_init@ --- *
94 * Arguments: @sslprf_ctx *c@ = pointer to a context structure
95 * @const gchash *hco, *hci@ = outer and inner hash functions
96 * @const void *k@ = pointer to secret buffer
97 * @size_t ksz@ = size of the secret
98 * @const void *sd@ = pointer to seed buffer
99 * @size_t sdsz@ = size of the seed
103 * Use: Initializes an SSL generator context.
106 void sslprf_init(sslprf_ctx
*c
, const gchash
*hco
, const gchash
*hci
,
107 const void *k
, size_t ksz
,
108 const void *sd
, size_t sdsz
)
110 c
->co
= hco
; c
->ci
= hci
;
111 c
->ohashsz
= hco
->hashsz
; c
->ihashsz
= hci
->hashsz
;
112 c
->k
= k
; c
->ksz
= ksz
; c
->sd
= sd
; c
->sdsz
= sdsz
;
117 /* --- @sslprf_encrypt@ --- *
119 * Arguments: @sslprf_ctx *c@ = pointer to a context structure
120 * @const void *src@ = pointer to source buffer
121 * @void *dest@ = pointer to destination buffer
122 * @size_t sz@ = size of the buffers
126 * Use: Encrypts data using the SSL pseudo-random function. If the
127 * destination pointer is null, the generator is spun and no
128 * output is produced; if the source pointer is null, raw output
129 * from the generator is written; otherwise, the source data is
130 * XORed with the generator output.
133 void sslprf_encrypt(sslprf_ctx
*c
, const void *src
, void *dest
, size_t sz
)
135 const octet
*s
= src
;
141 c
->h
->ops
->destroy(c
->h
);
151 for (i
= 0; i
< n
; i
++) d
[i
] = s
[i
] ^ c
->p
[i
];
162 /* --- @sslprf_free@ --- *
164 * Arguments: @sslprf_ctx@ = pointer to a context
168 * Use: Frees resources held in an SSL generator context.
171 void sslprf_free(sslprf_ctx
*c
)
173 c
->h
->ops
->destroy(c
->h
);
176 /* --- Generic random number generator --- */
178 typedef struct grctx
{
184 static void grdestroy(grand
*r
)
186 grctx
*g
= (grctx
*)r
;
187 xfree((char *)g
->ops
.name
);
188 xfree((octet
*)g
->prf
.sd
);
189 sslprf_free(&g
->prf
);
194 static void seed(grctx
*g
, const void *p
, size_t sz
)
197 xfree((octet
*)g
->prf
.sd
);
198 g
->prf
.sd
= q
= xmalloc(sz
);
203 static int grmisc(grand
*r
, unsigned op
, ...)
205 grctx
*g
= (grctx
*)r
;
214 switch (va_arg(ap
, unsigned)) {
217 case GRAND_SEEDUINT32
:
218 case GRAND_SEEDBLOCK
:
228 i
= va_arg(ap
, unsigned);
230 seed(g
, buf
, sizeof(buf
));
232 case GRAND_SEEDUINT32
:
233 i
= va_arg(ap
, uint32
);
235 seed(g
, buf
, sizeof(buf
));
237 case GRAND_SEEDBLOCK
: {
238 const void *p
= va_arg(ap
, const void *);
239 size_t sz
= va_arg(ap
, size_t);
242 case GRAND_SEEDRAND
: {
243 grand
*rr
= va_arg(ap
, grand
*);
245 rr
->ops
->fill(rr
, buf
, sizeof(buf
));
246 seed(g
, buf
, sizeof(buf
));
257 static octet
grbyte(grand
*r
)
259 grctx
*g
= (grctx
*)r
;
261 sslprf_encrypt(&g
->prf
, 0, &o
, 1);
265 static uint32
grword(grand
*r
)
267 grctx
*g
= (grctx
*)r
;
269 sslprf_encrypt(&g
->prf
, 0, &b
, sizeof(b
));
273 static void grfill(grand
*r
, void *p
, size_t sz
)
275 grctx
*g
= (grctx
*)r
;
276 sslprf_encrypt(&g
->prf
, 0, p
, sz
);
279 static const grand_ops grops
= {
283 grword
, grbyte
, grword
, grand_range
, grfill
286 /* ---@sslprf_rand@ --- *
288 * Arguments: @const gchash *hco, const gchash *hci@ = hash functions
289 * @const void *k@ = pointer to the key material
290 * @size_t ksz@ = size of the key material
291 * @const void *sd@ = pointer to the seed material
292 * @size_t sdsz@ = size of the seed material
294 * Returns: Pointer to generic random number generator interface.
296 * Use: Creates a generic generator which does TLS data expansion.
299 grand
*sslprf_rand(const gchash
*hco
, const gchash
*hci
,
300 const void *k
, size_t ksz
,
301 const void *sd
, size_t sdsz
)
303 grctx
*g
= S_CREATE(grctx
);
305 octet
*q
= xmalloc(sdsz
);
307 dstr_putf(&d
, "sslprf(%s,%s)", hco
->name
, hci
->name
);
309 g
->ops
.name
= xstrdup(d
.buf
);
312 sslprf_init(&g
->prf
, hco
, hci
, k
, ksz
, q
, sdsz
);
316 /*----- Test rig ----------------------------------------------------------*/
323 #include <mLib/quis.h>
324 #include <mLib/testrig.h>
329 static int v_generate(dstr
*v
)
335 g
= sslprf_rand(&md5
, &sha
, v
[0].buf
, v
[0].len
, v
[1].buf
, v
[1].len
);
336 dstr_ensure(&d
, v
[2].len
);
338 g
->ops
->fill(g
, d
.buf
, d
.len
);
340 if (memcmp(v
[2].buf
, d
.buf
, d
.len
) != 0) {
342 printf("\nfail sslprf:"
344 type_hex
.dump(&v
[0], stdout
);
345 printf("\n\tseed = "); type_hex
.dump(&v
[1], stdout
);
346 printf("\n\texpected = "); type_hex
.dump(&v
[2], stdout
);
347 printf("\n\tcalculated = "); type_hex
.dump(&d
, stdout
);
353 static test_chunk defs
[] = {
354 { "sslprf", v_generate
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
358 int main(int argc
, char *argv
[])
360 test_run(argc
, argv
, defs
, SRCDIR
"/tests/sslprf");
366 /*----- That's all, folks -------------------------------------------------*/