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