3 * Exampel entropy estimator for key-like input events. */
5 /* nettle, low-level cryptographics library
7 * Copyright (C) 2001 Niels Möller
9 * The nettle library is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or (at your
12 * option) any later version.
14 * The nettle library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 * License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with the nettle library; see the file COPYING.LIB. If not, write to
21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
32 yarrow_key_event_init(struct yarrow_key_event_ctx
*ctx
)
39 for (i
= 0; i
< YARROW_KEY_EVENT_BUFFER
; i
++)
44 yarrow_key_event_estimate(struct yarrow_key_event_ctx
*ctx
,
45 unsigned key
, unsigned time
)
50 /* Look at timing first. */
51 if (ctx
->previous
&& (time
> ctx
->previous
) )
53 if ( (time
- ctx
->previous
) >= 256)
61 for (i
= 0; i
< YARROW_KEY_EVENT_BUFFER
; i
++)
62 if (key
== ctx
->chars
[i
])
63 /* This is a recent character. Ignore it. */
66 /* Count one bit of entropy, unless this was one of the initial 16
68 if (ctx
->chars
[ctx
->index
])
71 /* Remember the character. */
73 ctx
->chars
[ctx
->index
] = key
;
74 ctx
->index
= (ctx
->index
+ 1) % YARROW_KEY_EVENT_BUFFER
;