2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
19 * @brief This header file contains declarations of Pseudo Random Function based on ANSI X9.31 Appendix A.2.4.
21 #ifndef _FSEC_INTERNAL_PRNG_H_
22 #define _FSEC_INTERNAL_PRNG_H_
24 #include <unique_ptr.h>
25 #include <FBaseByteBuffer.h>
30 namespace Tizen { namespace Security
35 * @brief This class generates a pseudo random number as per X9.31 specification using algorithm passed as input..
38 * Generate random numbers as per X9.31 specification using algorithm passed as input.
40 * @see ISecureRandom, AesSecureRandom, DesSecureRandom, DesEdeSecureRandom
47 * Generate random numbers as per X9.31 specification using algorithm passed as input.
50 * @return Returns pointer to byte buffer containing generated random number.
51 * @param[in] pAlg Pointer to algorithm used for random number generation. Supports EVP_des_ecb(), EVP_des_ede3_ecb() and EVP_AES_128_ecb().
52 * @param[in] requiredLength Integer type indicating required size of random number.
54 static Tizen::Base::ByteBuffer* GetRandomBytesN(const evp_cipher_st* pAlg, long requiredLength);
59 _Prng(const _Prng& rhs);
60 _Prng& operator =(const _Prng& rhs);
64 * Defines the Prng context structure.
70 unsigned long lenSeed; //seed length
71 unsigned long blockSize; //block size
72 unsigned long randSize;
73 unsigned long curOffset;
74 unsigned long lenKey; //key length
75 std::unique_ptr <byte[]> pKey; //key
76 std::unique_ptr <byte[]> pSeed; //seed
77 byte* pRand; //holds only reference - memory pointer by this variable to be freed by caller
78 const evp_cipher_st* pAlg; //algorithm type
82 * Generate and fill keys in PrngContext.
85 * @return An error code.
86 * @param[in] prng Reference to PRNG context structure.
87 * @exception E_SUCCESS The method is successful.
88 * @exception E_OUT_OF_MEMORY The memory is insufficient.
90 static result GenerateKey(PrngContext& prng);
93 * Generate and fill seed in PrngContext.
96 * @return An error code.
97 * @param[in] prng Reference to PRNG context.
98 * @exception E_SUCCESS The method is successful.
99 * @exception E_OUT_OF_MEMORY The memory is insufficient.
101 static result GenerateSeed(PrngContext& prng);
104 * Perform XOR operation using content in in1 and in2 and store output in out.
107 * @param[in] pIn1 Pointer to input buffer 1.
108 * @param[in] pIn2 Pointer to input buffer 2.
109 * @param[in] inLen Length of input buffer.
110 * @param[out] pOut Pointer to out buffer to which output is stored.
111 * @exception E_SUCCESS The method is successful.
113 static void PerformXor(byte* pIn1, byte* pIn2, unsigned long inLen, byte* pOut);
116 * Generate random number.
119 * @return An error code.
120 * @param[in] prng Reference to PRNG context.
121 * @param[in] pSeed Pointer to byte buffer containing date factor .
122 * @exception E_SUCCESS The method is successful.
123 * @exception E_OUT_OF_MEMORY The memory is insufficient.
125 static result GenerateRandomBytes(PrngContext& prng, Tizen::Base::ByteBuffer* pSeed);
128 * Create PRNG context.
131 * @return Returns pointer to PRNG context on success,NULL on failure.
133 static PrngContext* CreatePrngContextN(void);
137 } } //Tizen::Security
139 #endif //_FSEC_INTERNAL_PRNG_H_