fork of cyassl-lib
Dependents: TLS_cyassl TLS_cyassl
cyassl/ctaocrypt/random.h@4:f377303c41be, 2013-09-16 (annotated)
- Committer:
- feb11
- Date:
- Mon Sep 16 09:53:35 2013 +0000
- Revision:
- 4:f377303c41be
- Parent:
- 0:714293de3836
changed settings
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ashleymills | 0:714293de3836 | 1 | /* random.h |
ashleymills | 0:714293de3836 | 2 | * |
ashleymills | 0:714293de3836 | 3 | * Copyright (C) 2006-2013 wolfSSL Inc. |
ashleymills | 0:714293de3836 | 4 | * |
ashleymills | 0:714293de3836 | 5 | * This file is part of CyaSSL. |
ashleymills | 0:714293de3836 | 6 | * |
ashleymills | 0:714293de3836 | 7 | * CyaSSL is free software; you can redistribute it and/or modify |
ashleymills | 0:714293de3836 | 8 | * it under the terms of the GNU General Public License as published by |
ashleymills | 0:714293de3836 | 9 | * the Free Software Foundation; either version 2 of the License, or |
ashleymills | 0:714293de3836 | 10 | * (at your option) any later version. |
ashleymills | 0:714293de3836 | 11 | * |
ashleymills | 0:714293de3836 | 12 | * CyaSSL is distributed in the hope that it will be useful, |
ashleymills | 0:714293de3836 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
ashleymills | 0:714293de3836 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ashleymills | 0:714293de3836 | 15 | * GNU General Public License for more details. |
ashleymills | 0:714293de3836 | 16 | * |
ashleymills | 0:714293de3836 | 17 | * You should have received a copy of the GNU General Public License |
ashleymills | 0:714293de3836 | 18 | * along with this program; if not, write to the Free Software |
ashleymills | 0:714293de3836 | 19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
ashleymills | 0:714293de3836 | 20 | */ |
ashleymills | 0:714293de3836 | 21 | |
ashleymills | 0:714293de3836 | 22 | |
ashleymills | 0:714293de3836 | 23 | #ifndef CTAO_CRYPT_RANDOM_H |
ashleymills | 0:714293de3836 | 24 | #define CTAO_CRYPT_RANDOM_H |
ashleymills | 0:714293de3836 | 25 | |
ashleymills | 0:714293de3836 | 26 | #include <cyassl/ctaocrypt/types.h> |
ashleymills | 0:714293de3836 | 27 | |
ashleymills | 0:714293de3836 | 28 | #ifndef NO_RC4 |
ashleymills | 0:714293de3836 | 29 | #include <cyassl/ctaocrypt/arc4.h> |
ashleymills | 0:714293de3836 | 30 | #else |
ashleymills | 0:714293de3836 | 31 | #include <cyassl/ctaocrypt/sha256.h> |
ashleymills | 0:714293de3836 | 32 | #endif |
ashleymills | 0:714293de3836 | 33 | |
ashleymills | 0:714293de3836 | 34 | #ifdef __cplusplus |
ashleymills | 0:714293de3836 | 35 | extern "C" { |
ashleymills | 0:714293de3836 | 36 | #endif |
ashleymills | 0:714293de3836 | 37 | |
ashleymills | 0:714293de3836 | 38 | |
ashleymills | 0:714293de3836 | 39 | #if defined(USE_WINDOWS_API) |
ashleymills | 0:714293de3836 | 40 | #if defined(_WIN64) |
ashleymills | 0:714293de3836 | 41 | typedef unsigned __int64 ProviderHandle; |
ashleymills | 0:714293de3836 | 42 | /* type HCRYPTPROV, avoid #include <windows.h> */ |
ashleymills | 0:714293de3836 | 43 | #else |
ashleymills | 0:714293de3836 | 44 | typedef unsigned long ProviderHandle; |
ashleymills | 0:714293de3836 | 45 | #endif |
ashleymills | 0:714293de3836 | 46 | #endif |
ashleymills | 0:714293de3836 | 47 | |
ashleymills | 0:714293de3836 | 48 | |
ashleymills | 0:714293de3836 | 49 | /* OS specific seeder */ |
ashleymills | 0:714293de3836 | 50 | typedef struct OS_Seed { |
ashleymills | 0:714293de3836 | 51 | #if defined(USE_WINDOWS_API) |
ashleymills | 0:714293de3836 | 52 | ProviderHandle handle; |
ashleymills | 0:714293de3836 | 53 | #else |
ashleymills | 0:714293de3836 | 54 | int fd; |
ashleymills | 0:714293de3836 | 55 | #endif |
ashleymills | 0:714293de3836 | 56 | } OS_Seed; |
ashleymills | 0:714293de3836 | 57 | |
ashleymills | 0:714293de3836 | 58 | |
ashleymills | 0:714293de3836 | 59 | CYASSL_LOCAL |
ashleymills | 0:714293de3836 | 60 | int GenerateSeed(OS_Seed* os, byte* seed, word32 sz); |
ashleymills | 0:714293de3836 | 61 | |
ashleymills | 0:714293de3836 | 62 | #if defined(CYASSL_MDK_ARM) |
ashleymills | 0:714293de3836 | 63 | #undef RNG |
ashleymills | 0:714293de3836 | 64 | #define RNG CyaSSL_RNG /* for avoiding name conflict in "stm32f2xx.h" */ |
ashleymills | 0:714293de3836 | 65 | #endif |
ashleymills | 0:714293de3836 | 66 | |
ashleymills | 0:714293de3836 | 67 | #ifndef NO_RC4 |
ashleymills | 0:714293de3836 | 68 | |
ashleymills | 0:714293de3836 | 69 | #define CYASSL_RNG_CAVIUM_MAGIC 0xBEEF0004 |
ashleymills | 0:714293de3836 | 70 | |
ashleymills | 0:714293de3836 | 71 | /* secure Random Nnumber Generator */ |
ashleymills | 0:714293de3836 | 72 | |
ashleymills | 0:714293de3836 | 73 | |
ashleymills | 0:714293de3836 | 74 | typedef struct RNG { |
ashleymills | 0:714293de3836 | 75 | OS_Seed seed; |
ashleymills | 0:714293de3836 | 76 | Arc4 cipher; |
ashleymills | 0:714293de3836 | 77 | #ifdef HAVE_CAVIUM |
ashleymills | 0:714293de3836 | 78 | int devId; /* nitrox device id */ |
ashleymills | 0:714293de3836 | 79 | word32 magic; /* using cavium magic */ |
ashleymills | 0:714293de3836 | 80 | #endif |
ashleymills | 0:714293de3836 | 81 | } RNG; |
ashleymills | 0:714293de3836 | 82 | |
ashleymills | 0:714293de3836 | 83 | |
ashleymills | 0:714293de3836 | 84 | #ifdef HAVE_CAVIUM |
ashleymills | 0:714293de3836 | 85 | CYASSL_API int InitRngCavium(RNG*, int); |
ashleymills | 0:714293de3836 | 86 | #endif |
ashleymills | 0:714293de3836 | 87 | |
ashleymills | 0:714293de3836 | 88 | #else /* NO_RC4 */ |
ashleymills | 0:714293de3836 | 89 | |
ashleymills | 0:714293de3836 | 90 | #define DBRG_SEED_LEN (440/8) |
ashleymills | 0:714293de3836 | 91 | |
ashleymills | 0:714293de3836 | 92 | |
ashleymills | 0:714293de3836 | 93 | /* secure Random Nnumber Generator */ |
ashleymills | 0:714293de3836 | 94 | typedef struct RNG { |
ashleymills | 0:714293de3836 | 95 | OS_Seed seed; |
ashleymills | 0:714293de3836 | 96 | |
ashleymills | 0:714293de3836 | 97 | Sha256 sha; |
ashleymills | 0:714293de3836 | 98 | byte digest[SHA256_DIGEST_SIZE]; |
ashleymills | 0:714293de3836 | 99 | byte V[DBRG_SEED_LEN]; |
ashleymills | 0:714293de3836 | 100 | byte C[DBRG_SEED_LEN]; |
ashleymills | 0:714293de3836 | 101 | word64 reseed_ctr; |
ashleymills | 0:714293de3836 | 102 | } RNG; |
ashleymills | 0:714293de3836 | 103 | |
ashleymills | 0:714293de3836 | 104 | #endif |
ashleymills | 0:714293de3836 | 105 | |
ashleymills | 0:714293de3836 | 106 | CYASSL_API int InitRng(RNG*); |
ashleymills | 0:714293de3836 | 107 | CYASSL_API void RNG_GenerateBlock(RNG*, byte*, word32 sz); |
ashleymills | 0:714293de3836 | 108 | CYASSL_API byte RNG_GenerateByte(RNG*); |
ashleymills | 0:714293de3836 | 109 | |
ashleymills | 0:714293de3836 | 110 | #ifdef NO_RC4 |
ashleymills | 0:714293de3836 | 111 | CYASSL_API void FreeRng(RNG*); |
ashleymills | 0:714293de3836 | 112 | #endif |
ashleymills | 0:714293de3836 | 113 | |
ashleymills | 0:714293de3836 | 114 | #ifdef __cplusplus |
ashleymills | 0:714293de3836 | 115 | } /* extern "C" */ |
ashleymills | 0:714293de3836 | 116 | #endif |
ashleymills | 0:714293de3836 | 117 | |
ashleymills | 0:714293de3836 | 118 | #endif /* CTAO_CRYPT_RANDOM_H */ |
ashleymills | 0:714293de3836 | 119 |