MagickCore 7.0.10
key.c
Go to the documentation of this file.
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% K K EEEEE Y Y %
7% K K E Y Y %
8% KKK EEE Y Y %
9% K K E Y %
10% K K EEEEE Y %
11% %
12% %
13% Wizard's Toolkit Key Methods %
14% %
15% Software Design %
16% Cristy %
17% March 2003 %
18% %
19% %
20% Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
21% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% https://imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% Wizard the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37%
38*/
39
40/*
41 Include declarations.
42*/
43#include "wizard/studio.h"
44#include "wizard/cipher.h"
45#include "wizard/exception.h"
47#include "wizard/hmac.h"
48#include "wizard/key.h"
49#include "wizard/memory_.h"
50#include "wizard/random_.h"
51#include "wizard/splay-tree.h"
52
53/*
54 Define declarations.
55*/
56#define KeymapCipher AESCipher
57#define KeymapMode CTRMode
58#define SessionKeyHash SHA2256Hash
59#define SessionKeyLength 512
60
61/*
62 Typedef declarations.
63*/
65{
68
72
75
78
79 time_t
81
82 size_t
84};
85
86/*
87%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88% %
89% %
90% %
91% A c q u i r e K e y I n f o %
92% %
93% %
94% %
95%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
96%
97% AcquireKeyInfo() allocates the KeyInfo structure.
98%
99% The format of the AcquireKeyInfo method is:
100%
101% KeyInfo *AcquireKeyInfo()
102%
103*/
104
105static void *DestroyNode(void *entry)
106{
107 entry=(void *) DestroyStringInfo((StringInfo *) entry);
108 return((void *) NULL);
109}
110
112{
113 KeyInfo
114 *key_info;
115
116 key_info=(KeyInfo *) AcquireWizardMemory(sizeof(*key_info));
117 if (key_info == (KeyInfo *) NULL)
119 (void) memset(key_info,0,sizeof(*key_info));
123 key_info->nonce=GenerateCipherNonce(key_info->cipher_info);
125 key_info->timestamp=time((time_t *) NULL);
126 key_info->signature=WizardSignature;
127 key_info->id=GenerateSessionKey(key_info);
128 return(key_info);
129}
130
131/*
132%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
133% %
134% %
135% %
136% D e s t r o y K e y I n f o %
137% %
138% %
139% %
140%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141%
142% DestroyKeyInfo() zeros memory associated with the KeyInfo
143% structure.
144%
145% The format of the DestroyKeyInfo method is:
146%
147% KeyInfo *DestroyKeyInfo(KeyInfo *key_info)
148%
149% A description of each parameter follows:
150%
151% o key_info: The key info.
152%
153*/
155{
157 WizardAssert(CipherDomain,key_info != (KeyInfo *) NULL);
159 if (key_info->cipher_info != (CipherInfo *) NULL)
160 key_info->cipher_info=DestroyCipherInfo(key_info->cipher_info);
161 if (key_info->random_info != (RandomInfo *) NULL)
162 key_info->random_info=DestroyRandomInfo(key_info->random_info);
163 if (key_info->nonce != (StringInfo *) NULL)
164 key_info->nonce=DestroyStringInfo(key_info->nonce);
165 if (key_info->id != (StringInfo *) NULL)
166 key_info->id=DestroyStringInfo(key_info->id);
167 if (key_info->key_map != (SplayTreeInfo *) NULL)
168 key_info->key_map=DestroySplayTree(key_info->key_map);
169 key_info->signature=(~WizardSignature);
170 key_info=(KeyInfo *) RelinquishWizardMemory(key_info);
171 return(key_info);
172}
173
174/*
175%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
176% %
177% %
178% %
179% G e n e r a t e S e s s i o n K e y %
180% %
181% %
182% %
183%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
184%
185% GenerateSessionKey() generate a session key.
186%
187% The format of the GenerateSessionKey method is:
188%
189% void GenerateSessionKey(KeyInfo *key_info)
190%
191% A description of each parameter follows:
192%
193% o key_info: The key info.
194%
195*/
197{
199 *hmac_info;
200
202 status;
203
205 *id,
206 *key,
207 *mac_key;
208
210 WizardAssert(CipherDomain,key_info != (KeyInfo *) NULL);
212 mac_key=GetRandomKey(key_info->random_info,SessionKeyLength/8);
215 ConstructHMAC(hmac_info,mac_key,key);
216 id=CloneStringInfo(GetHMACDigest(hmac_info));
217 hmac_info=DestroyHMACInfo(hmac_info);
218 mac_key=DestroyStringInfo(mac_key);
219 if (key_info->id != (StringInfo *) NULL)
220 status=SetKeyInfo(key_info,id,key);
221 else
222 status=AddValueToSplayTree(key_info->key_map,CloneStringInfo(id),
223 CloneStringInfo(key));
224 key=DestroyStringInfo(key);
225 if (status == WizardFalse)
227 key_info->timestamp=time((time_t *) NULL);
228 return(id);
229}
230
231/*
232%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
233% %
234% %
235% %
236% G e t K e y I n f o %
237% %
238% %
239% %
240%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241%
242% GetKeyInfo() gets a key from the key splay-tree.
243%
244% The format of the GetKeyInfo method is:
245%
246% StringInfo *GetKeyInfo(KeyInfo *key_info,const StringInfo *id)
247%
248% A description of each parameter follows:
249%
250% o key_info: The key info.
251%
252% o id: The key id.
253%
254*/
256{
258 *key,
259 *session_key;
260
262 WizardAssert(CipherDomain,key_info != (KeyInfo *) NULL);
263 WizardAssert(CipherDomain,id != (const StringInfo *) NULL);
264 session_key=(StringInfo *) GetValueFromSplayTree(key_info->key_map,
265 key_info->id);
266 if (session_key == (StringInfo *) NULL)
268 key=(StringInfo *) GetValueFromSplayTree(key_info->key_map,id);
269 if (key == (StringInfo *) NULL)
271 SetCipherKey(key_info->cipher_info,session_key);
272 SetCipherNonce(key_info->cipher_info,key_info->nonce);
273 key=CloneStringInfo(key);
274 (void) DecipherCipher(key_info->cipher_info,key);
275 return(key);
276}
277
278/*
279%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280% %
281% %
282% %
283% S e t K e y I n f o %
284% %
285% %
286% %
287%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
288%
289% SetKeyInfo() saves a key to the splay-tree.
290%
291% The format of the SetKeyInfo
292%
293% WizardBooleanType SetKeyInfo(KeyInfo *key_info,const StringInfo *id,
294% const StringInfo *key)
295%
296% A description of each parameter follows:
297%
298% o key_info: The key info.
299%
300% o id: The key id.
301%
302% o key: The key.
303%
304*/
306 const StringInfo *key)
307{
309 status;
310
312 *cipherkey,
313 *session_key;
314
316 WizardAssert(CipherDomain,key_info != (KeyInfo *) NULL);
317 WizardAssert(CipherDomain,id != (const StringInfo *) NULL);
318 WizardAssert(CipherDomain,key != (const StringInfo *) NULL);
319 session_key=(StringInfo *) GetValueFromSplayTree(key_info->key_map,
320 key_info->id);
321 if (session_key == (StringInfo *) NULL)
323 SetCipherKey(key_info->cipher_info,session_key);
324 SetCipherNonce(key_info->cipher_info,key_info->nonce);
325 cipherkey=CloneStringInfo(key);
326 (void) EncipherCipher(key_info->cipher_info,cipherkey);
327 status=AddValueToSplayTree(key_info->key_map,CloneStringInfo(id),cipherkey);
328 return(status);
329}
WizardExport CipherInfo * DestroyCipherInfo(CipherInfo *cipher_info)
Definition cipher.c:703
WizardExport void SetCipherNonce(CipherInfo *cipher_info, const StringInfo *nonce)
Definition cipher.c:1394
WizardExport void SetCipherKey(CipherInfo *cipher_info, const StringInfo *key)
Definition cipher.c:1429
WizardExport StringInfo * DecipherCipher(CipherInfo *cipher_info, StringInfo *ciphertext)
Definition cipher.c:405
WizardExport StringInfo * GenerateCipherNonce(CipherInfo *cipher_info)
Definition cipher.c:1278
WizardExport CipherInfo * AcquireCipherInfo(const CipherType cipher, const CipherMode mode)
Definition cipher.c:134
WizardExport StringInfo * EncipherCipher(CipherInfo *cipher_info, StringInfo *plaintext)
Definition cipher.c:946
#define WizardAssert(domain, predicate)
#define ThrowWizardFatalError(domain, error)
@ CipherDomain
Definition exception.h:34
@ KeymapDomain
Definition exception.h:35
@ KeyError
Definition exception.h:52
@ MemoryError
Definition exception.h:49
WizardExport void ConstructHMAC(HMACInfo *hmac_info, const StringInfo *key, const StringInfo *message)
Definition hmac.c:139
WizardExport HMACInfo * AcquireHMACInfo(const HashType hash)
Definition hmac.c:91
WizardExport const StringInfo * GetHMACDigest(const HMACInfo *hmac_info)
Definition hmac.c:250
WizardExport HMACInfo * DestroyHMACInfo(HMACInfo *hmac_info)
Definition hmac.c:174
WizardExport WizardBooleanType SetKeyInfo(KeyInfo *key_info, const StringInfo *id, const StringInfo *key)
Definition key.c:305
#define KeymapMode
Definition key.c:57
#define SessionKeyLength
Definition key.c:59
WizardExport StringInfo * GenerateSessionKey(KeyInfo *key_info)
Definition key.c:196
static void * DestroyNode(void *entry)
Definition key.c:105
WizardExport KeyInfo * AcquireKeyInfo(void)
Definition key.c:111
WizardExport StringInfo * GetKeyInfo(KeyInfo *key_info, const StringInfo *id)
Definition key.c:255
#define SessionKeyHash
Definition key.c:58
#define KeymapCipher
Definition key.c:56
WizardExport KeyInfo * DestroyKeyInfo(KeyInfo *key_info)
Definition key.c:154
WizardBooleanType LogWizardEvent(const LogEventType type, const char *module, const char *function, const size_t line, const char *format,...)
Definition log.c:1390
@ TraceEvent
Definition log.h:39
#define GetWizardModule()
Definition log.h:30
WizardExport void * AcquireWizardMemory(const size_t size)
Definition memory.c:586
WizardExport void * RelinquishWizardMemory(void *memory)
Definition memory.c:1039
#define WizardExport
#define WizardSignature
WizardExport RandomInfo * AcquireRandomInfo(const HashType hash)
Definition random.c:165
WizardExport StringInfo * GetRandomKey(RandomInfo *random_info, const size_t length)
Definition random.c:820
WizardExport RandomInfo * DestroyRandomInfo(RandomInfo *random_info)
Definition random.c:289
WizardExport const void * GetValueFromSplayTree(SplayTreeInfo *splay_tree, const void *key)
Definition splay-tree.c:874
WizardExport SplayTreeInfo * DestroySplayTree(SplayTreeInfo *splay_tree)
Definition splay-tree.c:678
WizardExport WizardBooleanType AddValueToSplayTree(SplayTreeInfo *splay_tree, const void *key, const void *value)
Definition splay-tree.c:152
WizardExport int CompareSplayTreeStringInfo(const void *target, const void *source)
Definition splay-tree.c:446
WizardExport SplayTreeInfo * NewSplayTree(int(*compare)(const void *, const void *), void *(*relinquish_key)(void *), void *(*relinquish_value)(void *))
WizardExport StringInfo * CloneStringInfo(const StringInfo *string_info)
Definition string.c:332
WizardExport StringInfo * DestroyStringInfo(StringInfo *string_info)
Definition string.c:857
Definition key.c:65
SplayTreeInfo * key_map
Definition key.c:67
time_t timestamp
Definition key.c:80
StringInfo * id
Definition key.c:70
size_t signature
Definition key.c:83
StringInfo * nonce
Definition key.c:71
RandomInfo * random_info
Definition key.c:77
CipherInfo * cipher_info
Definition key.c:74
WizardBooleanType
Definition wizard-type.h:26
@ WizardFalse
Definition wizard-type.h:27