mbed port of tinydtls
prng.h@1:bc8a649bad13, 2013-10-11 (annotated)
- Committer:
- ashleymills
- Date:
- Fri Oct 11 08:46:21 2013 +0000
- Revision:
- 1:bc8a649bad13
- Parent:
- 0:04990d454f45
Cleaned up all the debug stuff I added finding the hash table bug.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ashleymills | 0:04990d454f45 | 1 | /* prng.h -- Pseudo Random Numbers |
ashleymills | 0:04990d454f45 | 2 | * |
ashleymills | 0:04990d454f45 | 3 | * Copyright (C) 2010--2012 Olaf Bergmann <bergmann@tzi.org> |
ashleymills | 0:04990d454f45 | 4 | * |
ashleymills | 0:04990d454f45 | 5 | * This file is part of the library tinydtls. Please see |
ashleymills | 0:04990d454f45 | 6 | * README for terms of use. |
ashleymills | 0:04990d454f45 | 7 | */ |
ashleymills | 0:04990d454f45 | 8 | |
ashleymills | 0:04990d454f45 | 9 | /** |
ashleymills | 0:04990d454f45 | 10 | * @file prng.h |
ashleymills | 0:04990d454f45 | 11 | * @brief Pseudo Random Numbers |
ashleymills | 0:04990d454f45 | 12 | */ |
ashleymills | 0:04990d454f45 | 13 | |
ashleymills | 0:04990d454f45 | 14 | #ifndef _DTLS_PRNG_H_ |
ashleymills | 0:04990d454f45 | 15 | #define _DTLS_PRNG_H_ |
ashleymills | 0:04990d454f45 | 16 | |
ashleymills | 0:04990d454f45 | 17 | #include "config.h" |
ashleymills | 0:04990d454f45 | 18 | |
ashleymills | 0:04990d454f45 | 19 | /** |
ashleymills | 0:04990d454f45 | 20 | * @defgroup prng Pseudo Random Numbers |
ashleymills | 0:04990d454f45 | 21 | * @{ |
ashleymills | 0:04990d454f45 | 22 | */ |
ashleymills | 0:04990d454f45 | 23 | |
ashleymills | 0:04990d454f45 | 24 | #ifndef WITH_CONTIKI |
ashleymills | 0:04990d454f45 | 25 | #include <stdlib.h> |
ashleymills | 0:04990d454f45 | 26 | |
ashleymills | 0:04990d454f45 | 27 | /** |
ashleymills | 0:04990d454f45 | 28 | * Fills \p buf with \p len random bytes. This is the default |
ashleymills | 0:04990d454f45 | 29 | * implementation for prng(). You might want to change prng() to use |
ashleymills | 0:04990d454f45 | 30 | * a better PRNG on your specific platform. |
ashleymills | 0:04990d454f45 | 31 | */ |
ashleymills | 0:04990d454f45 | 32 | static inline int |
ashleymills | 0:04990d454f45 | 33 | dtls_prng_impl(unsigned char *buf, size_t len) { |
ashleymills | 0:04990d454f45 | 34 | while (len--) |
ashleymills | 0:04990d454f45 | 35 | *buf++ = rand() & 0xFF; |
ashleymills | 0:04990d454f45 | 36 | return 1; |
ashleymills | 0:04990d454f45 | 37 | } |
ashleymills | 0:04990d454f45 | 38 | #else /* WITH_CONTIKI */ |
ashleymills | 0:04990d454f45 | 39 | #include <string.h> |
ashleymills | 0:04990d454f45 | 40 | |
ashleymills | 0:04990d454f45 | 41 | #ifdef HAVE_PRNG |
ashleymills | 0:04990d454f45 | 42 | extern int contiki_prng_impl(unsigned char *buf, size_t len); |
ashleymills | 0:04990d454f45 | 43 | #else |
ashleymills | 0:04990d454f45 | 44 | /** |
ashleymills | 0:04990d454f45 | 45 | * Fills \p buf with \p len random bytes. This is the default |
ashleymills | 0:04990d454f45 | 46 | * implementation for prng(). You might want to change prng() to use |
ashleymills | 0:04990d454f45 | 47 | * a better PRNG on your specific platform. |
ashleymills | 0:04990d454f45 | 48 | */ |
ashleymills | 0:04990d454f45 | 49 | static inline int |
ashleymills | 0:04990d454f45 | 50 | contiki_prng_impl(unsigned char *buf, size_t len) { |
ashleymills | 0:04990d454f45 | 51 | unsigned short v = random_rand(); |
ashleymills | 0:04990d454f45 | 52 | while (len > sizeof(v)) { |
ashleymills | 0:04990d454f45 | 53 | memcpy(buf, &v, sizeof(v)); |
ashleymills | 0:04990d454f45 | 54 | len -= sizeof(v); |
ashleymills | 0:04990d454f45 | 55 | buf += sizeof(v); |
ashleymills | 0:04990d454f45 | 56 | v = random_rand(); |
ashleymills | 0:04990d454f45 | 57 | } |
ashleymills | 0:04990d454f45 | 58 | |
ashleymills | 0:04990d454f45 | 59 | memcpy(buf, &v, len); |
ashleymills | 0:04990d454f45 | 60 | return 1; |
ashleymills | 0:04990d454f45 | 61 | } |
ashleymills | 0:04990d454f45 | 62 | #endif /* HAVE_PRNG */ |
ashleymills | 0:04990d454f45 | 63 | |
ashleymills | 0:04990d454f45 | 64 | #define prng(Buf,Length) contiki_prng_impl((Buf), (Length)) |
ashleymills | 0:04990d454f45 | 65 | #define prng_init(Value) random_init((unsigned short)(Value)) |
ashleymills | 0:04990d454f45 | 66 | #endif /* WITH_CONTIKI */ |
ashleymills | 0:04990d454f45 | 67 | |
ashleymills | 0:04990d454f45 | 68 | #ifndef prng |
ashleymills | 0:04990d454f45 | 69 | /** |
ashleymills | 0:04990d454f45 | 70 | * Fills \p Buf with \p Length bytes of random data. |
ashleymills | 0:04990d454f45 | 71 | * |
ashleymills | 0:04990d454f45 | 72 | * @hideinitializer |
ashleymills | 0:04990d454f45 | 73 | */ |
ashleymills | 0:04990d454f45 | 74 | #define prng(Buf,Length) dtls_prng_impl((Buf), (Length)) |
ashleymills | 0:04990d454f45 | 75 | #endif |
ashleymills | 0:04990d454f45 | 76 | |
ashleymills | 0:04990d454f45 | 77 | #ifndef prng_init |
ashleymills | 0:04990d454f45 | 78 | /** |
ashleymills | 0:04990d454f45 | 79 | * Called to set the PRNG seed. You may want to re-define this to |
ashleymills | 0:04990d454f45 | 80 | * allow for a better PRNG. |
ashleymills | 0:04990d454f45 | 81 | * |
ashleymills | 0:04990d454f45 | 82 | * @hideinitializer |
ashleymills | 0:04990d454f45 | 83 | */ |
ashleymills | 0:04990d454f45 | 84 | #define prng_init(Value) srand((unsigned long)(Value)) |
ashleymills | 0:04990d454f45 | 85 | #endif |
ashleymills | 0:04990d454f45 | 86 | |
ashleymills | 0:04990d454f45 | 87 | /** @} */ |
ashleymills | 0:04990d454f45 | 88 | |
ashleymills | 0:04990d454f45 | 89 | #endif /* _DTLS_PRNG_H_ */ |