2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
20 * @brief This header file contains declarations of Pseudo Random Function based on ANSI X9.31 Appendix A.2.4.
22 #ifndef _FSEC_INTERNAL_PRNG_H_
23 #define _FSEC_INTERNAL_PRNG_H_
25 #include <unique_ptr.h>
26 #include <FBaseByteBuffer.h>
31 namespace Tizen { namespace Security
36 * @brief This class generates a pseudo random number as per X9.31 specification using algorithm passed as input..
39 * Generate random numbers as per X9.31 specification using algorithm passed as input.
41 * @see ISecureRandom, AesSecureRandom, DesSecureRandom, DesEdeSecureRandom
48 * Generate random numbers as per X9.31 specification using algorithm passed as input.
51 * @return Returns pointer to byte buffer containing generated random number.
52 * @param[in] pAlg Pointer to algorithm used for random number generation. Supports EVP_des_ecb(), EVP_des_ede3_ecb() and EVP_AES_128_ecb().
53 * @param[in] requiredLength Integer type indicating required size of random number.
55 static Tizen::Base::ByteBuffer* GetRandomBytesN(const evp_cipher_st* pAlg, long requiredLength);
60 _Prng(const _Prng& rhs);
61 _Prng& operator =(const _Prng& rhs);
65 * Defines the Prng context structure.
71 unsigned long lenSeed; //seed length
72 unsigned long blockSize; //block size
73 unsigned long randSize;
74 unsigned long curOffset;
75 unsigned long lenKey; //key length
76 std::unique_ptr <byte[]> pKey; //key
77 std::unique_ptr <byte[]> pSeed; //seed
78 byte* pRand; //holds only reference - memory pointer by this variable to be freed by caller
79 const evp_cipher_st* pAlg; //algorithm type
83 * Generate and fill keys in PrngContext.
86 * @return An error code.
87 * @param[in] prng Reference to PRNG context structure.
88 * @exception E_SUCCESS The method is successful.
89 * @exception E_OUT_OF_MEMORY The memory is insufficient.
91 static result GenerateKey(PrngContext& prng);
94 * Generate and fill seed in PrngContext.
97 * @return An error code.
98 * @param[in] prng Reference to PRNG context.
99 * @exception E_SUCCESS The method is successful.
100 * @exception E_OUT_OF_MEMORY The memory is insufficient.
102 static result GenerateSeed(PrngContext& prng);
105 * Perform XOR operation using content in in1 and in2 and store output in out.
108 * @param[in] pIn1 Pointer to input buffer 1.
109 * @param[in] pIn2 Pointer to input buffer 2.
110 * @param[in] inLen Length of input buffer.
111 * @param[out] pOut Pointer to out buffer to which output is stored.
112 * @exception E_SUCCESS The method is successful.
114 static void PerformXor(byte* pIn1, byte* pIn2, unsigned long inLen, byte* pOut);
117 * Generate random number.
120 * @return An error code.
121 * @param[in] prng Reference to PRNG context.
122 * @param[in] pSeed Pointer to byte buffer containing date factor .
123 * @exception E_SUCCESS The method is successful.
124 * @exception E_OUT_OF_MEMORY The memory is insufficient.
126 static result GenerateRandomBytes(PrngContext& prng, Tizen::Base::ByteBuffer* pSeed);
129 * Create PRNG context.
132 * @return Returns pointer to PRNG context on success,NULL on failure.
134 static PrngContext* CreatePrngContextN(void);
138 } } //Tizen::Security
140 #endif //_FSEC_INTERNAL_PRNG_H_