OSDN Git Service

add tstools.
[rec10/rec10-git.git] / tstools / DtsEdit / src / gpac / crypt.h
1 /*\r
2  *                      GPAC - Multimedia Framework C SDK\r
3  *\r
4  *                      Copyright (c) Jean Le Feuvre 2000-2005\r
5  *                                      All rights reserved\r
6  *\r
7  *  This file is part of GPAC / Crypto Tools sub-project\r
8  *\r
9  *  GPAC is free software; you can redistribute it and/or modify\r
10  *  it under the terms of the GNU Lesser General Public License as published by\r
11  *  the Free Software Foundation; either version 2, or (at your option)\r
12  *  any later version.\r
13  *   \r
14  *  GPAC is distributed in the hope that it will be useful,\r
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of\r
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
17  *  GNU Lesser General Public License for more details.\r
18  *   \r
19  *  You should have received a copy of the GNU Lesser General Public\r
20  *  License along with this library; see the file COPYING.  If not, write to\r
21  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. \r
22  *\r
23  */\r
24 \r
25 /*\r
26         The GPAC crypto lib is a simplified version of libmcrypt - not all algos are included.\r
27         Doc here is man mcrypt\r
28         Original libmcrypt license\r
29 */\r
30 \r
31 /*\r
32  * Copyright (C) 1998,1999,2000 Nikos Mavroyanopoulos\r
33  * \r
34  * This library is free software; you can redistribute it and/or modify it \r
35  * under the terms of the GNU Library General Public License as published \r
36  * by the Free Software Foundation; either version 2 of the License, or \r
37  * (at your option) any later version.\r
38  *\r
39  * This library is distributed in the hope that it will be useful,\r
40  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
41  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
42  * Library General Public License for more details.\r
43  *\r
44  * You should have received a copy of the GNU Library General Public\r
45  * License along with this library; if not, write to the\r
46  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,\r
47  * Boston, MA 02111-1307, USA.\r
48  */\r
49 \r
50 #ifndef _GF_CRYPT_H_\r
51 #define _GF_CRYPT_H_\r
52 \r
53 #ifdef __cplusplus\r
54 extern "C" {\r
55 #endif\r
56 \r
57 #include <gpac/tools.h>\r
58 \r
59 /*max number of possible key sizes for all supported modes*/\r
60 #define MAX_KEY_SIZES   4\r
61 \r
62 /*crypto lib handler*/\r
63 typedef struct _tag_crypt_stream GF_Crypt;\r
64 \r
65 /*supported modes (case insensitive): "CBC", "CFB", "CTR", "ECB", "nCFB", "nOFB", "OFB", "STREAM"*/\r
66 /*supported algos (case insensitive): \r
67         "AES-128" == "Rijndael-128"\r
68         "AES-192" == "Rijndael-192"\r
69         "AES-256" == "Rijndael-256"\r
70         "DES", "3DES"\r
71 */\r
72 \r
73 \r
74 /*opens crypto context - algo and mode SHALL NOT be NULL*/\r
75 GF_Crypt *gf_crypt_open(const char *algorithm, const char *mode);\r
76 /*close crypto context*/\r
77 void gf_crypt_close(GF_Crypt *gfc);\r
78 \r
79 /* sets the state of the algorithm. Can be used only with block algorithms and certain modes like CBC, CFB etc. \r
80 It is usefully if you want to restart or start a different encryption quickly. \r
81 */\r
82 GF_Err gf_crypt_set_state(GF_Crypt *gfc, const void *iv, int size);\r
83 /*gets the state of the algorithm. Can be used only certain modes and algorithms. \r
84 The size will hold the size of the state and the state must have enough bytes to hold it.\r
85 */\r
86 GF_Err gf_crypt_get_state(GF_Crypt *gfc, void *iv, int *size);\r
87 /*Returns 1 if the algorithm is a block algorithm or 0 if it is a stream algorithm.*/\r
88 Bool gf_crypt_is_block_algorithm(GF_Crypt *gfc);\r
89 /*Returns 1 if the mode is for use with block algorithms, otherwise it returns 0.*/\r
90 Bool gf_crypt_is_block_algorithm_mode(GF_Crypt *gfc);\r
91 /*Returns 1 if the mode outputs blocks of bytes or 0 if it outputs bytes. (eg. 1 for cbc and ecb, and 0 for cfb and stream)*/\r
92 Bool gf_crypt_is_block_mode(GF_Crypt *gfc);\r
93 /*Returns the block size of the algorithm specified by the encryption descriptor in bytes.*/\r
94 u32 gf_crypt_get_block_size(GF_Crypt *gfc);\r
95 /*Returns the maximum supported key size of the algorithm specified by the encryption descriptor in bytes.*/\r
96 u32 gf_crypt_get_key_size(GF_Crypt *gfc);\r
97 /*Returns the number of supported key sizes.\r
98 @keys: array of at least MAX_KEY_SIZES size - will hold the supported sizes*/\r
99 u32 gf_crypt_get_supported_key_sizes(GF_Crypt *gfc, u32 *key_sizes);\r
100 /*Returns size (in bytes) of the IV of the algorithm specified for the context. \r
101 If it is '0' then the IV is ignored in that algorithm. \r
102 IV is used in CBC, CFB, OFB modes, and in some algorithms in STREAM mode.\r
103 */\r
104 u32 gf_crypt_get_iv_size(GF_Crypt *gfc);\r
105 /*Returns 1 if the mode needs an IV, 0 otherwise. \r
106 Some 'stream' algorithms may need an IV even if the mode itself does not need an IV.\r
107 */\r
108 Bool gf_crypt_mode_has_iv(GF_Crypt *gfc);\r
109 \r
110 /*guess what these do...*/\r
111 const char *gf_crypt_get_algorithm_name(GF_Crypt *gfc);\r
112 u32 gf_crypt_get_algorithm_version(GF_Crypt *gfc);\r
113 const char *gf_crypt_get_mode_name(GF_Crypt *gfc);\r
114 u32 gf_crypt_get_mode_version(GF_Crypt *gfc);\r
115 \r
116 \r
117 /*\r
118 This function initializes all buffers for the specified context\r
119 @Lenofkey: key size in BYTES - maximum value of lenofkey should be the one obtained by \r
120 calling gf_crypt_get_key_size() and every value smaller than this is legal.\r
121 @IV: usually size of the algorithms block size - get it by calling gf_crypt_get_iv_size().\r
122         IV is ignored in ECB. IV MUST exist in CFB, CBC, STREAM, nOFB and OFB modes.\r
123         It needs to be random and unique (but not secret). The same IV must be used\r
124         for encryption/decryption. \r
125 After calling this function you can use the descriptor for encryption or decryption (not both). \r
126 */\r
127 GF_Err gf_crypt_init(GF_Crypt *gfc, void *key, u32 lenofkey, const void *IV);\r
128 /*releases context buffers - you may call gf_crypt_init after that, or gf_crypt_close*/\r
129 void gf_crypt_deinit(GF_Crypt *gfc);\r
130 /*changes key and IV*/\r
131 GF_Err gf_crypt_set_key(GF_Crypt *gfc, void *key, u32 keysize, const void *iv);\r
132 \r
133 /*\r
134 main encryption function. \r
135 @Plaintext, @len: plaintext to encrypt - len should be  k*algorithms_block_size if used in a mode\r
136 which operated in blocks (cbc, ecb, nofb), or whatever when used in cfb or ofb which operate in streams.\r
137 The plaintext is replaced by the ciphertext. \r
138 */\r
139 GF_Err gf_crypt_encrypt(GF_Crypt *gfc, void *plaintext, int len);\r
140 /*decryption function. It is almost the same with gf_crypt_generic.*/\r
141 GF_Err gf_crypt_decrypt(GF_Crypt *gfc, void *ciphertext, int len);\r
142 \r
143 /*various queries on both modes and algo*/\r
144 u32 gf_crypt_str_get_algorithm_version(const char *algorithm);\r
145 u32 gf_crypt_str_get_mode_version(const char *mode);\r
146 Bool gf_crypt_str_is_block_algorithm(const char *algorithm);\r
147 Bool gf_crypt_str_is_block_algorithm_mode(const char *algorithm);\r
148 Bool gf_crypt_str_is_block_mode(const char *mode);\r
149 u32 gf_crypt_str_module_get_algo_block_size(const char *algorithm);\r
150 u32 gf_crypt_str_module_get_algo_key_size(const char *algorithm);\r
151 u32 gf_crypt_str_get_algo_supported_key_sizes(const char *algorithm, int *keys);\r
152 \r
153 \r
154 \r
155 /*SHA1 from Christophe Devine*/\r
156 typedef struct\r
157 {\r
158     u32 total[2];\r
159     u32 state[5];\r
160     u8 buffer[64];\r
161 } GF_SHA1Context;\r
162 \r
163 /*\r
164  * Core SHA-1 functions\r
165  */\r
166 void gf_sha1_starts(GF_SHA1Context *ctx );\r
167 void gf_sha1_update(GF_SHA1Context *ctx, u8 *input, u32 length);\r
168 void gf_sha1_finish(GF_SHA1Context *ctx, u8 digest[20] );\r
169 \r
170 /*\r
171  * Output SHA-1(file contents), returns 0 if successful.\r
172  */\r
173 int gf_sha1_file(char *filename, u8 digest[20]);\r
174 \r
175 /*\r
176  * Output SHA-1(buf)\r
177  */\r
178 void gf_sha1_csum(u8 *buf, u32 buflen, u8 digest[20]);\r
179 \r
180 /*\r
181  * Output HMAC-SHA-1(key,buf)\r
182  */\r
183 void gf_sha1_hmac(u8 *key, u32 keylen, u8 *buf, u32 buflen, u8 digest[20]);\r
184 \r
185 #ifdef __cplusplus\r
186 }\r
187 #endif\r
188 \r
189 #endif  /*_GF_CRYPT_H_*/\r
190 \r