sync with tizen_2.0
[platform/framework/native/appfw.git] / src / security / FSec_Prng.h
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 /**
19  *      @file   FSec_Prng.h
20  *      @brief  This header file contains declarations of Pseudo Random Function based on ANSI X9.31 Appendix A.2.4.
21  */
22 #ifndef _FSEC_INTERNAL_PRNG_H_
23 #define _FSEC_INTERNAL_PRNG_H_
24
25 #include <unique_ptr.h>
26 #include <FBaseByteBuffer.h>
27
28 struct evp_cipher_st;
29
30
31 namespace Tizen { namespace Security
32 {
33
34 /**
35  *      @class          _Prng
36  *      @brief          This class generates a pseudo random number as per X9.31 specification using algorithm passed as input..
37  *      @since 2.1
38  *
39  *  Generate random numbers as per X9.31 specification using algorithm passed as input.
40  *
41  *      @see    ISecureRandom, AesSecureRandom, DesSecureRandom, DesEdeSecureRandom
42  */
43 class _Prng
44 {
45
46 public:
47         /**
48         * Generate random numbers as per X9.31 specification using algorithm passed as input.
49         *
50         * @since 2.1
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.
54         */
55         static Tizen::Base::ByteBuffer* GetRandomBytesN(const evp_cipher_st* pAlg, long requiredLength);
56
57 private:
58
59         _Prng(void);
60         _Prng(const _Prng& rhs);
61         _Prng& operator =(const _Prng& rhs);
62         ~_Prng(void);
63
64         /**
65          * Defines the Prng context structure.
66          *
67          * @since 2.1
68          */
69         struct PrngContext
70         {
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
80         };
81
82         /**
83          * Generate and fill keys in PrngContext.
84          *
85          * @since 2.1
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.
90          */
91         static result GenerateKey(PrngContext& prng);
92
93         /**
94          * Generate and fill seed in PrngContext.
95          *
96          * @since 2.1
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.
101          */
102         static result GenerateSeed(PrngContext& prng);
103
104         /**
105          * Perform XOR operation using content in in1 and in2 and store output in out.
106          *
107          * @since 2.1
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.
113          */
114         static void PerformXor(byte* pIn1, byte* pIn2, unsigned long inLen, byte* pOut);
115
116         /**
117          * Generate random number.
118          *
119          * @since 2.1
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.
125          */
126         static result GenerateRandomBytes(PrngContext& prng, Tizen::Base::ByteBuffer* pSeed);
127
128         /**
129          * Create PRNG context.
130          *
131          * @since 2.1
132          * @return              Returns pointer to PRNG context on success,NULL on failure.
133          */
134         static PrngContext* CreatePrngContextN(void);
135
136 }; //_Prng
137
138 } } //Tizen::Security
139
140 #endif //_FSEC_INTERNAL_PRNG_H_