1*49d46fa3Schristos /* $NetBSD: orand_key.c,v 1.1 2009/07/19 23:30:58 christos Exp $ */
2*49d46fa3Schristos
3*49d46fa3Schristos /*
4*49d46fa3Schristos * Copyright (C) 2003 WIDE Project.
5*49d46fa3Schristos * All rights reserved.
6*49d46fa3Schristos *
7*49d46fa3Schristos * Redistribution and use in source and binary forms, with or without
8*49d46fa3Schristos * modification, are permitted provided that the following conditions
9*49d46fa3Schristos * are met:
10*49d46fa3Schristos * 1. Redistributions of source code must retain the above copyright
11*49d46fa3Schristos * notice, this list of conditions and the following disclaimer.
12*49d46fa3Schristos * 2. Redistributions in binary form must reproduce the above copyright
13*49d46fa3Schristos * notice, this list of conditions and the following disclaimer in the
14*49d46fa3Schristos * documentation and/or other materials provided with the distribution.
15*49d46fa3Schristos * 3. Neither the name of the project nor the names of its contributors
16*49d46fa3Schristos * may be used to endorse or promote products derived from this software
17*49d46fa3Schristos * without specific prior written permission.
18*49d46fa3Schristos *
19*49d46fa3Schristos * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20*49d46fa3Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*49d46fa3Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*49d46fa3Schristos * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23*49d46fa3Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*49d46fa3Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*49d46fa3Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*49d46fa3Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*49d46fa3Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*49d46fa3Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*49d46fa3Schristos * SUCH DAMAGE.
30*49d46fa3Schristos */
31*49d46fa3Schristos
32*49d46fa3Schristos #include "des_locl.h"
33*49d46fa3Schristos
des_random_seed(des_cblock * key)34*49d46fa3Schristos void des_random_seed(des_cblock *key)
35*49d46fa3Schristos {
36*49d46fa3Schristos }
37*49d46fa3Schristos
des_random_key(des_cblock * ret)38*49d46fa3Schristos int des_random_key(des_cblock *ret)
39*49d46fa3Schristos {
40*49d46fa3Schristos u_int32_t v;
41*49d46fa3Schristos u_int8_t *p, *ep;
42*49d46fa3Schristos
43*49d46fa3Schristos do {
44*49d46fa3Schristos p = (u_int8_t *)ret;
45*49d46fa3Schristos ep = (u_int8_t *)(ret + sizeof(*ret));
46*49d46fa3Schristos while (p + sizeof(v) <= ep) {
47*49d46fa3Schristos v = arc4random();
48*49d46fa3Schristos *(u_int32_t *)p = v;
49*49d46fa3Schristos p += sizeof(v);
50*49d46fa3Schristos }
51*49d46fa3Schristos if (ep - p) {
52*49d46fa3Schristos v = arc4random();
53*49d46fa3Schristos memcpy(p, &v, ep - p);
54*49d46fa3Schristos }
55*49d46fa3Schristos } while (des_is_weak_key(ret));
56*49d46fa3Schristos des_set_odd_parity(ret);
57*49d46fa3Schristos return (1);
58*49d46fa3Schristos }
59