~ubuntu-branches/ubuntu/utopic/dropbear/utopic-proposed

« back to all changes in this revision

Viewing changes to libtomcrypt/src/prngs/rng_make_prng.c

  • Committer: Bazaar Package Importer
  • Author(s): Matt Johnston
  • Date: 2005-12-08 19:20:21 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051208192021-nyp9rwnt77nsg6ty
Tags: 0.47-1
* New upstream release.
* SECURITY: Fix incorrect buffer sizing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
 
2
 *
 
3
 * LibTomCrypt is a library that provides various cryptographic
 
4
 * algorithms in a highly modular and flexible manner.
 
5
 *
 
6
 * The library is free for all purposes without any express
 
7
 * guarantee it works.
 
8
 *
 
9
 * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.org
 
10
 */
 
11
#include "tomcrypt.h"
 
12
 
 
13
/** 
 
14
  @file rng_make_prng.c
 
15
  portable way to get secure random bits to feed a PRNG  (Tom St Denis)
 
16
*/
 
17
 
 
18
/**
 
19
  Create a PRNG from a RNG
 
20
  @param bits     Number of bits of entropy desired (64 ... 1024)
 
21
  @param wprng    Index of which PRNG to setup
 
22
  @param prng     [out] PRNG state to initialize
 
23
  @param callback A pointer to a void function for when the RNG is slow, this can be NULL
 
24
  @return CRYPT_OK if successful
 
25
*/  
 
26
int rng_make_prng(int bits, int wprng, prng_state *prng, 
 
27
                  void (*callback)(void))
 
28
{
 
29
   unsigned char buf[256];
 
30
   int err;
 
31
   
 
32
   LTC_ARGCHK(prng != NULL);
 
33
 
 
34
   /* check parameter */
 
35
   if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
 
36
      return err;
 
37
   }
 
38
 
 
39
   if (bits < 64 || bits > 1024) {
 
40
      return CRYPT_INVALID_PRNGSIZE;
 
41
   }
 
42
 
 
43
   if ((err = prng_descriptor[wprng].start(prng)) != CRYPT_OK) {
 
44
      return err;
 
45
   }
 
46
 
 
47
   bits = ((bits/8)+((bits&7)!=0?1:0)) * 2;
 
48
   if (rng_get_bytes(buf, (unsigned long)bits, callback) != (unsigned long)bits) {
 
49
      return CRYPT_ERROR_READPRNG;
 
50
   }
 
51
 
 
52
   if ((err = prng_descriptor[wprng].add_entropy(buf, (unsigned long)bits, prng)) != CRYPT_OK) {
 
53
      return err;
 
54
   }
 
55
 
 
56
   if ((err = prng_descriptor[wprng].ready(prng)) != CRYPT_OK) {
 
57
      return err;
 
58
   }
 
59
 
 
60
   #ifdef LTC_CLEAN_STACK
 
61
      zeromem(buf, sizeof(buf));
 
62
   #endif
 
63
   return CRYPT_OK;
 
64
}
 
65
 
 
66
 
 
67
/* $Source: /cvs/libtom/libtomcrypt/src/prngs/rng_make_prng.c,v $ */
 
68
/* $Revision: 1.3 $ */
 
69
/* $Date: 2005/05/05 14:35:59 $ */