MagickCore 7.1.2-36
Convert, Edit, Or Compose Bitmap Images
Loading...
Searching...
No Matches
cipher.c
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% CCCC IIIII PPPP H H EEEEE RRRR %
6% C I P P H H E R R %
7% C I PPPP HHHHH EEE RRRR %
8% C I P H H E R R %
9% CCCC IIIII P H H EEEEE R R %
10% %
11% %
12% MagickCore Cipher Methods %
13% %
14% Software Design %
15% Cristy %
16% %
17% %
18% Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
19% dedicated to making software imaging solutions freely available. %
20% %
21% You may not use this file except in compliance with the License. You may %
22% obtain a copy of the License at %
23% %
24% https://imagemagick.org/license/ %
25% %
26% Unless required by applicable law or agreed to in writing, software %
27% distributed under the License is distributed on an "AS IS" BASIS, %
28% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
29% See the License for the specific language governing permissions and %
30% limitations under the License. %
31% %
32%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33%
34%
35*/
36␌
37/*
38 Include declarations.
39*/
40#include "MagickCore/studio.h"
41#include "MagickCore/cache.h"
42#include "MagickCore/cipher.h"
43#include "MagickCore/exception.h"
44#include "MagickCore/exception-private.h"
45#include "MagickCore/image.h"
46#include "MagickCore/image-private.h"
47#include "MagickCore/linked-list.h"
48#include "MagickCore/list.h"
49#include "MagickCore/memory_.h"
50#include "MagickCore/memory-private.h"
51#include "MagickCore/monitor.h"
52#include "MagickCore/monitor-private.h"
53#include "MagickCore/property.h"
54#include "MagickCore/quantum-private.h"
55#include "MagickCore/registry.h"
56#include "MagickCore/semaphore.h"
57#include "MagickCore/signature-private.h"
58#include "MagickCore/splay-tree.h"
59#include "MagickCore/statistic.h"
60#include "MagickCore/string_.h"
61#include "MagickCore/timer-private.h"
62␌
63#if defined(MAGICKCORE_CIPHER_SUPPORT)
64/*
65 Define declarations.
66*/
67#define AESBlocksize 16
68␌
69/*
70 Typedef declarations.
71*/
72typedef struct _AESInfo
73{
74 StringInfo
75 *key;
76
77 unsigned int
78 blocksize,
79 *encipher_key,
80 *decipher_key;
81
82 ssize_t
83 rounds;
84
85 time_t
86 timestamp;
87
88 size_t
89 signature;
90} AESInfo;
91␌
92/*
93 Global declarations.
94*/
95static unsigned char
96 InverseLog[256] =
97 {
98 1, 3, 5, 15, 17, 51, 85, 255, 26, 46, 114, 150, 161, 248,
99 19, 53, 95, 225, 56, 72, 216, 115, 149, 164, 247, 2, 6, 10,
100 30, 34, 102, 170, 229, 52, 92, 228, 55, 89, 235, 38, 106, 190,
101 217, 112, 144, 171, 230, 49, 83, 245, 4, 12, 20, 60, 68, 204,
102 79, 209, 104, 184, 211, 110, 178, 205, 76, 212, 103, 169, 224, 59,
103 77, 215, 98, 166, 241, 8, 24, 40, 120, 136, 131, 158, 185, 208,
104 107, 189, 220, 127, 129, 152, 179, 206, 73, 219, 118, 154, 181, 196,
105 87, 249, 16, 48, 80, 240, 11, 29, 39, 105, 187, 214, 97, 163,
106 254, 25, 43, 125, 135, 146, 173, 236, 47, 113, 147, 174, 233, 32,
107 96, 160, 251, 22, 58, 78, 210, 109, 183, 194, 93, 231, 50, 86,
108 250, 21, 63, 65, 195, 94, 226, 61, 71, 201, 64, 192, 91, 237,
109 44, 116, 156, 191, 218, 117, 159, 186, 213, 100, 172, 239, 42, 126,
110 130, 157, 188, 223, 122, 142, 137, 128, 155, 182, 193, 88, 232, 35,
111 101, 175, 234, 37, 111, 177, 200, 67, 197, 84, 252, 31, 33, 99,
112 165, 244, 7, 9, 27, 45, 119, 153, 176, 203, 70, 202, 69, 207,
113 74, 222, 121, 139, 134, 145, 168, 227, 62, 66, 198, 81, 243, 14,
114 18, 54, 90, 238, 41, 123, 141, 140, 143, 138, 133, 148, 167, 242,
115 13, 23, 57, 75, 221, 124, 132, 151, 162, 253, 28, 36, 108, 180,
116 199, 82, 246, 1
117 },
118 Log[256] =
119 {
120 0, 0, 25, 1, 50, 2, 26, 198, 75, 199, 27, 104, 51, 238,
121 223, 3, 100, 4, 224, 14, 52, 141, 129, 239, 76, 113, 8, 200,
122 248, 105, 28, 193, 125, 194, 29, 181, 249, 185, 39, 106, 77, 228,
123 166, 114, 154, 201, 9, 120, 101, 47, 138, 5, 33, 15, 225, 36,
124 18, 240, 130, 69, 53, 147, 218, 142, 150, 143, 219, 189, 54, 208,
125 206, 148, 19, 92, 210, 241, 64, 70, 131, 56, 102, 221, 253, 48,
126 191, 6, 139, 98, 179, 37, 226, 152, 34, 136, 145, 16, 126, 110,
127 72, 195, 163, 182, 30, 66, 58, 107, 40, 84, 250, 133, 61, 186,
128 43, 121, 10, 21, 155, 159, 94, 202, 78, 212, 172, 229, 243, 115,
129 167, 87, 175, 88, 168, 80, 244, 234, 214, 116, 79, 174, 233, 213,
130 231, 230, 173, 232, 44, 215, 117, 122, 235, 22, 11, 245, 89, 203,
131 95, 176, 156, 169, 81, 160, 127, 12, 246, 111, 23, 196, 73, 236,
132 216, 67, 31, 45, 164, 118, 123, 183, 204, 187, 62, 90, 251, 96,
133 177, 134, 59, 82, 161, 108, 170, 85, 41, 157, 151, 178, 135, 144,
134 97, 190, 220, 252, 188, 149, 207, 205, 55, 63, 91, 209, 83, 57,
135 132, 60, 65, 162, 109, 71, 20, 42, 158, 93, 86, 242, 211, 171,
136 68, 17, 146, 217, 35, 32, 46, 137, 180, 124, 184, 38, 119, 153,
137 227, 165, 103, 74, 237, 222, 197, 49, 254, 24, 13, 99, 140, 128,
138 192, 247, 112, 7,
139 },
140 SBox[256] =
141 {
142 99, 124, 119, 123, 242, 107, 111, 197, 48, 1, 103, 43, 254, 215,
143 171, 118, 202, 130, 201, 125, 250, 89, 71, 240, 173, 212, 162, 175,
144 156, 164, 114, 192, 183, 253, 147, 38, 54, 63, 247, 204, 52, 165,
145 229, 241, 113, 216, 49, 21, 4, 199, 35, 195, 24, 150, 5, 154,
146 7, 18, 128, 226, 235, 39, 178, 117, 9, 131, 44, 26, 27, 110,
147 90, 160, 82, 59, 214, 179, 41, 227, 47, 132, 83, 209, 0, 237,
148 32, 252, 177, 91, 106, 203, 190, 57, 74, 76, 88, 207, 208, 239,
149 170, 251, 67, 77, 51, 133, 69, 249, 2, 127, 80, 60, 159, 168,
150 81, 163, 64, 143, 146, 157, 56, 245, 188, 182, 218, 33, 16, 255,
151 243, 210, 205, 12, 19, 236, 95, 151, 68, 23, 196, 167, 126, 61,
152 100, 93, 25, 115, 96, 129, 79, 220, 34, 42, 144, 136, 70, 238,
153 184, 20, 222, 94, 11, 219, 224, 50, 58, 10, 73, 6, 36, 92,
154 194, 211, 172, 98, 145, 149, 228, 121, 231, 200, 55, 109, 141, 213,
155 78, 169, 108, 86, 244, 234, 101, 122, 174, 8, 186, 120, 37, 46,
156 28, 166, 180, 198, 232, 221, 116, 31, 75, 189, 139, 138, 112, 62,
157 181, 102, 72, 3, 246, 14, 97, 53, 87, 185, 134, 193, 29, 158,
158 225, 248, 152, 17, 105, 217, 142, 148, 155, 30, 135, 233, 206, 85,
159 40, 223, 140, 161, 137, 13, 191, 230, 66, 104, 65, 153, 45, 15,
160 176, 84, 187, 22
161 };
162␌
163/*
164 Forward declarations.
165*/
166static AESInfo
167 *DestroyAESInfo(AESInfo *);
168
169static void
170 EncipherAESBlock(AESInfo *,const unsigned char *,unsigned char *),
171 SetAESKey(AESInfo *,const StringInfo *);
172␌
173/*
174%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
175% %
176% %
177% %
178% A c q u i r e A E S I n f o %
179% %
180% %
181% %
182%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183%
184% AcquireAESInfo() allocate the AESInfo structure.
185%
186% The format of the AcquireAESInfo method is:
187%
188% AESInfo *AcquireAESInfo(void)
189%
190*/
191static AESInfo *AcquireAESInfo(void)
192{
193 AESInfo
194 *aes_info;
195
196 aes_info=(AESInfo *) AcquireCriticalMemory(sizeof(*aes_info));
197 (void) memset(aes_info,0,sizeof(*aes_info));
198 aes_info->blocksize=AESBlocksize;
199 aes_info->key=AcquireStringInfo(32);
200 aes_info->encipher_key=(unsigned int *) AcquireQuantumMemory(60UL,sizeof(
201 *aes_info->encipher_key));
202 aes_info->decipher_key=(unsigned int *) AcquireQuantumMemory(60UL,sizeof(
203 *aes_info->decipher_key));
204 if ((aes_info->key == (StringInfo *) NULL) ||
205 (aes_info->encipher_key == (unsigned int *) NULL) ||
206 (aes_info->decipher_key == (unsigned int *) NULL))
207 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
208 aes_info->timestamp=GetMagickTime();
209 aes_info->signature=MagickCoreSignature;
210 return(aes_info);
211}
212␌
213/*
214%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
215% %
216% %
217% %
218% D e s t r o y A E S I n f o %
219% %
220% %
221% %
222%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
223%
224% DestroyAESInfo() zeros memory associated with the AESInfo structure.
225%
226% The format of the DestroyAESInfo method is:
227%
228% AESInfo *DestroyAESInfo(AESInfo *aes_info)
229%
230% A description of each parameter follows:
231%
232% o aes_info: the cipher context.
233%
234*/
235static AESInfo *DestroyAESInfo(AESInfo *aes_info)
236{
237 assert(aes_info != (AESInfo *) NULL);
238 assert(aes_info->signature == MagickCoreSignature);
239 if (IsEventLogging() != MagickFalse)
240 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
241 if (aes_info->decipher_key != (unsigned int *) NULL)
242 aes_info->decipher_key=(unsigned int *) RelinquishMagickMemory(
243 aes_info->decipher_key);
244 if (aes_info->encipher_key != (unsigned int *) NULL)
245 aes_info->encipher_key=(unsigned int *) RelinquishMagickMemory(
246 aes_info->encipher_key);
247 if (aes_info->key != (StringInfo *) NULL)
248 aes_info->key=DestroyStringInfo(aes_info->key);
249 aes_info->signature=(~MagickCoreSignature);
250 aes_info=(AESInfo *) RelinquishMagickMemory(aes_info);
251 return(aes_info);
252}
253␌
254/*
255%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
256% %
257% %
258% %
259% E n c i p h e r A E S B l o c k %
260% %
261% %
262% %
263%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
264%
265% EncipherAESBlock() enciphers a single block of plaintext to produce a block
266% of ciphertext.
267%
268% The format of the EncipherAESBlock method is:
269%
270% void EncipherAES(AESInfo *aes_info,const unsigned char *plaintext,
271% unsigned char *ciphertext)
272%
273% A description of each parameter follows:
274%
275% o aes_info: the cipher context.
276%
277% o plaintext: the plain text.
278%
279% o ciphertext: the cipher text.
280%
281*/
282
283static inline void AddRoundKey(const unsigned int *ciphertext,
284 const unsigned int *key,unsigned int *plaintext)
285{
286 ssize_t
287 i;
288
289 /*
290 Xor corresponding text input and round key input bytes.
291 */
292 for (i=0; i < 4; i++)
293 plaintext[i]=key[i] ^ ciphertext[i];
294}
295
296static inline unsigned int ByteMultiply(const unsigned char alpha,
297 const unsigned char beta)
298{
299 /*
300 Byte multiply two elements of GF(2^m) (mix columns and inverse mix columns).
301 */
302 if ((alpha == 0) || (beta == 0))
303 return(0);
304 return((unsigned int) InverseLog[(Log[alpha]+Log[beta]) % 0xff]);
305}
306
307static inline unsigned int ByteSubTransform(unsigned int x,
308 unsigned char *s_box)
309{
310 unsigned int
311 key;
312
313 /*
314 Non-linear layer resists differential and linear cryptoanalysis attacks.
315 */
316 key=((unsigned int) s_box[x & 0xff]) |
317 ((unsigned int) s_box[(x >> 8) & 0xff] << 8) |
318 ((unsigned int) s_box[(x >> 16) & 0xff] << 16) |
319 ((unsigned int) s_box[(x >> 24) & 0xff] << 24);
320 return(key);
321}
322
323static void FinalizeRoundKey(const unsigned int *ciphertext,
324 const unsigned int *key,unsigned char *plaintext)
325{
326 unsigned char
327 *p;
328
329 unsigned int
330 i,
331 j;
332
333 unsigned int
334 value;
335
336 /*
337 The round key is XORed with the result of the mix-column transformation.
338 */
339 p=plaintext;
340 for (i=0; i < 4; i++)
341 {
342 value=ciphertext[i] ^ key[i];
343 for (j=0; j < 4; j++)
344 *p++=(unsigned char) ((value >> (8*j)) & 0xff);
345 }
346 value=0;
347}
348
349static void InitializeRoundKey(const unsigned char *ciphertext,
350 const unsigned int *key,unsigned int *plaintext)
351{
352 const unsigned char
353 *p;
354
355 unsigned int
356 i,
357 j;
358
359 unsigned int
360 value;
361
362 p=ciphertext;
363 for (i=0; i < 4; i++)
364 {
365 value=0;
366 for (j=0; j < 4; j++)
367 value|=((unsigned int) *p++ << (8*j));
368 plaintext[i]=key[i] ^ value;
369 }
370 value=0;
371}
372
373static inline unsigned int RotateLeft(const unsigned int x)
374{
375 return(((x << 8) | ((x >> 24) & 0xff)));
376}
377
378static void EncipherAESBlock(AESInfo *aes_info,const unsigned char *plaintext,
379 unsigned char *ciphertext)
380{
381 ssize_t
382 i,
383 j;
384
385 static int
386 map[4][4] =
387 {
388 { 0, 1, 2, 3 },
389 { 1, 2, 3, 0 },
390 { 2, 3, 0, 1 },
391 { 3, 0, 1, 2 }
392 };
393
394 static unsigned int
395 D[] =
396 {
397 0xa56363c6U, 0x847c7cf8U, 0x997777eeU, 0x8d7b7bf6U, 0x0df2f2ffU,
398 0xbd6b6bd6U, 0xb16f6fdeU, 0x54c5c591U, 0x50303060U, 0x03010102U,
399 0xa96767ceU, 0x7d2b2b56U, 0x19fefee7U, 0x62d7d7b5U, 0xe6abab4dU,
400 0x9a7676ecU, 0x45caca8fU, 0x9d82821fU, 0x40c9c989U, 0x877d7dfaU,
401 0x15fafaefU, 0xeb5959b2U, 0xc947478eU, 0x0bf0f0fbU, 0xecadad41U,
402 0x67d4d4b3U, 0xfda2a25fU, 0xeaafaf45U, 0xbf9c9c23U, 0xf7a4a453U,
403 0x967272e4U, 0x5bc0c09bU, 0xc2b7b775U, 0x1cfdfde1U, 0xae93933dU,
404 0x6a26264cU, 0x5a36366cU, 0x413f3f7eU, 0x02f7f7f5U, 0x4fcccc83U,
405 0x5c343468U, 0xf4a5a551U, 0x34e5e5d1U, 0x08f1f1f9U, 0x937171e2U,
406 0x73d8d8abU, 0x53313162U, 0x3f15152aU, 0x0c040408U, 0x52c7c795U,
407 0x65232346U, 0x5ec3c39dU, 0x28181830U, 0xa1969637U, 0x0f05050aU,
408 0xb59a9a2fU, 0x0907070eU, 0x36121224U, 0x9b80801bU, 0x3de2e2dfU,
409 0x26ebebcdU, 0x6927274eU, 0xcdb2b27fU, 0x9f7575eaU, 0x1b090912U,
410 0x9e83831dU, 0x742c2c58U, 0x2e1a1a34U, 0x2d1b1b36U, 0xb26e6edcU,
411 0xee5a5ab4U, 0xfba0a05bU, 0xf65252a4U, 0x4d3b3b76U, 0x61d6d6b7U,
412 0xceb3b37dU, 0x7b292952U, 0x3ee3e3ddU, 0x712f2f5eU, 0x97848413U,
413 0xf55353a6U, 0x68d1d1b9U, 0x00000000U, 0x2cededc1U, 0x60202040U,
414 0x1ffcfce3U, 0xc8b1b179U, 0xed5b5bb6U, 0xbe6a6ad4U, 0x46cbcb8dU,
415 0xd9bebe67U, 0x4b393972U, 0xde4a4a94U, 0xd44c4c98U, 0xe85858b0U,
416 0x4acfcf85U, 0x6bd0d0bbU, 0x2aefefc5U, 0xe5aaaa4fU, 0x16fbfbedU,
417 0xc5434386U, 0xd74d4d9aU, 0x55333366U, 0x94858511U, 0xcf45458aU,
418 0x10f9f9e9U, 0x06020204U, 0x817f7ffeU, 0xf05050a0U, 0x443c3c78U,
419 0xba9f9f25U, 0xe3a8a84bU, 0xf35151a2U, 0xfea3a35dU, 0xc0404080U,
420 0x8a8f8f05U, 0xad92923fU, 0xbc9d9d21U, 0x48383870U, 0x04f5f5f1U,
421 0xdfbcbc63U, 0xc1b6b677U, 0x75dadaafU, 0x63212142U, 0x30101020U,
422 0x1affffe5U, 0x0ef3f3fdU, 0x6dd2d2bfU, 0x4ccdcd81U, 0x140c0c18U,
423 0x35131326U, 0x2fececc3U, 0xe15f5fbeU, 0xa2979735U, 0xcc444488U,
424 0x3917172eU, 0x57c4c493U, 0xf2a7a755U, 0x827e7efcU, 0x473d3d7aU,
425 0xac6464c8U, 0xe75d5dbaU, 0x2b191932U, 0x957373e6U, 0xa06060c0U,
426 0x98818119U, 0xd14f4f9eU, 0x7fdcdca3U, 0x66222244U, 0x7e2a2a54U,
427 0xab90903bU, 0x8388880bU, 0xca46468cU, 0x29eeeec7U, 0xd3b8b86bU,
428 0x3c141428U, 0x79dedea7U, 0xe25e5ebcU, 0x1d0b0b16U, 0x76dbdbadU,
429 0x3be0e0dbU, 0x56323264U, 0x4e3a3a74U, 0x1e0a0a14U, 0xdb494992U,
430 0x0a06060cU, 0x6c242448U, 0xe45c5cb8U, 0x5dc2c29fU, 0x6ed3d3bdU,
431 0xefacac43U, 0xa66262c4U, 0xa8919139U, 0xa4959531U, 0x37e4e4d3U,
432 0x8b7979f2U, 0x32e7e7d5U, 0x43c8c88bU, 0x5937376eU, 0xb76d6ddaU,
433 0x8c8d8d01U, 0x64d5d5b1U, 0xd24e4e9cU, 0xe0a9a949U, 0xb46c6cd8U,
434 0xfa5656acU, 0x07f4f4f3U, 0x25eaeacfU, 0xaf6565caU, 0x8e7a7af4U,
435 0xe9aeae47U, 0x18080810U, 0xd5baba6fU, 0x887878f0U, 0x6f25254aU,
436 0x722e2e5cU, 0x241c1c38U, 0xf1a6a657U, 0xc7b4b473U, 0x51c6c697U,
437 0x23e8e8cbU, 0x7cdddda1U, 0x9c7474e8U, 0x211f1f3eU, 0xdd4b4b96U,
438 0xdcbdbd61U, 0x868b8b0dU, 0x858a8a0fU, 0x907070e0U, 0x423e3e7cU,
439 0xc4b5b571U, 0xaa6666ccU, 0xd8484890U, 0x05030306U, 0x01f6f6f7U,
440 0x120e0e1cU, 0xa36161c2U, 0x5f35356aU, 0xf95757aeU, 0xd0b9b969U,
441 0x91868617U, 0x58c1c199U, 0x271d1d3aU, 0xb99e9e27U, 0x38e1e1d9U,
442 0x13f8f8ebU, 0xb398982bU, 0x33111122U, 0xbb6969d2U, 0x70d9d9a9U,
443 0x898e8e07U, 0xa7949433U, 0xb69b9b2dU, 0x221e1e3cU, 0x92878715U,
444 0x20e9e9c9U, 0x49cece87U, 0xff5555aaU, 0x78282850U, 0x7adfdfa5U,
445 0x8f8c8c03U, 0xf8a1a159U, 0x80898909U, 0x170d0d1aU, 0xdabfbf65U,
446 0x31e6e6d7U, 0xc6424284U, 0xb86868d0U, 0xc3414182U, 0xb0999929U,
447 0x772d2d5aU, 0x110f0f1eU, 0xcbb0b07bU, 0xfc5454a8U, 0xd6bbbb6dU,
448 0x3a16162cU
449 };
450
451 unsigned int
452 alpha,
453 key[4],
454 text[4];
455
456 /*
457 Encipher one block.
458 */
459 (void) memset(text,0,sizeof(text));
460 InitializeRoundKey(plaintext,aes_info->encipher_key,text);
461 for (i=1; i < aes_info->rounds; i++)
462 {
463 for (j=0; j < 4; j++)
464 key[j]=D[text[j] & 0xff] ^
465 RotateLeft(D[(text[map[1][j]] >> 8) & 0xff] ^
466 RotateLeft(D[(text[map[2][j]] >> 16) & 0xff] ^
467 RotateLeft(D[(text[map[3][j]] >> 24) & 0xff])));
468 AddRoundKey(key,aes_info->encipher_key+4*i,text);
469 }
470 for (i=0; i < 4; i++)
471 {
472 alpha=(text[i] & 0x000000ff) | ((text[map[1][i]]) & 0x0000ff00) |
473 ((text[map[2][i]]) & 0x00ff0000) | ((text[map[3][i]]) & 0xff000000);
474 key[i]=ByteSubTransform(alpha,SBox);
475 }
476 FinalizeRoundKey(key,aes_info->encipher_key+4*aes_info->rounds,ciphertext);
477 alpha=0;
478 (void) ResetMagickMemory(key,0,sizeof(key));
479 (void) ResetMagickMemory(text,0,sizeof(text));
480}
481␌
482/*
483%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
484% %
485% %
486% %
487% P a s s k e y D e c i p h e r I m a g e %
488% %
489% %
490% %
491%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
492%
493% PasskeyDecipherImage() converts cipher pixels to plain pixels.
494%
495*/
496
497static inline MagickBooleanType IncrementCipherNonce(const size_t length,
498 unsigned char *nonce)
499{
500 ssize_t
501 i;
502
503 for (i=(ssize_t) (length-1); i >= 0; i--)
504 {
505 nonce[i]++;
506 if (nonce[i] != 0)
507 return(MagickTrue);
508 }
509 return(MagickFalse);
510}
511
512MagickExport MagickBooleanType DecipherImage(Image *image,
513 const char *passphrase,ExceptionInfo *exception)
514{
515 MagickBooleanType
516 status;
517
518 StringInfo
519 *passkey;
520
521 if (passphrase == (const char *) NULL)
522 return(MagickTrue);
523 passkey=StringToStringInfo(passphrase);
524 if (passkey == (StringInfo *) NULL)
525 return(MagickFalse);
526 status=PasskeyDecipherImage(image,passkey,exception);
527 passkey=DestroyStringInfo(passkey);
528 return(status);
529}
530
531MagickExport MagickBooleanType PasskeyDecipherImage(Image *image,
532 const StringInfo *passkey,ExceptionInfo *exception)
533{
534#define DecipherImageTag "Decipher/Image "
535
536 AESInfo
537 *aes_info;
538
539 CacheView
540 *image_view;
541
542 const unsigned char
543 *digest;
544
545 MagickBooleanType
546 proceed;
547
548 MagickSizeType
549 extent;
550
551 QuantumInfo
552 *quantum_info;
553
554 QuantumType
555 quantum_type;
556
557 SignatureInfo
558 *signature_info;
559
560 unsigned char
561 *p;
562
563 size_t
564 length;
565
566 ssize_t
567 y;
568
569 StringInfo
570 *key,
571 *nonce;
572
573 unsigned char
574 input_block[AESBlocksize],
575 output_block[AESBlocksize],
576 *pixels;
577
578 assert(image != (Image *) NULL);
579 assert(image->signature == MagickCoreSignature);
580 assert(exception != (ExceptionInfo *) NULL);
581 assert(exception->signature == MagickCoreSignature);
582 if (IsEventLogging() != MagickFalse)
583 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
584 if (passkey == (const StringInfo *) NULL)
585 return(MagickTrue);
586
587 aes_info=AcquireAESInfo();
588 key=CloneStringInfo(passkey);
589 if (key == (StringInfo *) NULL)
590 {
591 aes_info=DestroyAESInfo(aes_info);
592 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
593 image->filename);
594 }
595 nonce=SplitStringInfo(key,GetStringInfoLength(key)/2);
596 if (nonce == (StringInfo *) NULL)
597 {
598 key=DestroyStringInfo(key);
599 aes_info=DestroyAESInfo(aes_info);
600 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
601 image->filename);
602 }
603 SetAESKey(aes_info,key);
604 key=DestroyStringInfo(key);
605 signature_info=AcquireSignatureInfo();
606 UpdateSignature(signature_info,nonce);
607 extent=(MagickSizeType) image->columns*image->rows;
608 SetStringInfoLength(nonce,sizeof(extent));
609 SetStringInfoDatum(nonce,(const unsigned char *) &extent);
610 UpdateSignature(signature_info,nonce);
611 nonce=DestroyStringInfo(nonce);
612 FinalizeSignature(signature_info);
613 (void) memset(input_block,0,sizeof(input_block));
614 digest=GetStringInfoDatum(GetSignatureDigest(signature_info));
615 (void) memcpy(input_block,digest,MagickMin(AESBlocksize,
616 GetSignatureDigestsize(signature_info))*sizeof(*input_block));
617 signature_info=DestroySignatureInfo(signature_info);
618
619 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
620 if (quantum_info == (QuantumInfo *) NULL)
621 {
622 aes_info=DestroyAESInfo(aes_info);
623 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
624 image->filename);
625 }
626 quantum_type=GetQuantumType(image,exception);
627 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
628 image_view=AcquireAuthenticCacheView(image,exception);
629 for (y=0; y < (ssize_t) image->rows; y++)
630 {
631 ssize_t
632 i,
633 x;
634
635 Quantum
636 *magick_restrict q;
637
638 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
639 if (q == (Quantum *) NULL)
640 break;
641 length=ExportQuantumPixels(image,image_view,quantum_info,quantum_type,
642 pixels,exception);
643 p=pixels;
644 for (x=0; x < (ssize_t) length; x+=AESBlocksize)
645 {
646 (void) memcpy(output_block,input_block,AESBlocksize*
647 sizeof(*output_block));
648 if (IncrementCipherNonce(AESBlocksize,input_block) == MagickFalse)
649 {
650 image_view=DestroyCacheView(image_view);
651 quantum_info=DestroyQuantumInfo(quantum_info);
652 aes_info=DestroyAESInfo(aes_info);
653 ThrowBinaryException(ResourceLimitError,"SequenceWrapError",image->filename);
654 }
655 EncipherAESBlock(aes_info,output_block,output_block);
656 for (i=0; i < AESBlocksize; i++)
657 p[i]^=output_block[i];
658 p+=(ptrdiff_t) AESBlocksize;
659 }
660 (void) memcpy(output_block,input_block,AESBlocksize*
661 sizeof(*output_block));
662 EncipherAESBlock(aes_info,output_block,output_block);
663 for (i=0; x < (ssize_t) length; x++)
664 {
665 p[i]^=output_block[i];
666 i++;
667 }
668 (void) ImportQuantumPixels(image,image_view,quantum_info,quantum_type,
669 pixels,exception);
670 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
671 break;
672 proceed=SetImageProgress(image,DecipherImageTag,(MagickOffsetType) y,
673 image->rows);
674 if (proceed == MagickFalse)
675 break;
676 }
677 image_view=DestroyCacheView(image_view);
678 (void) DeleteImageProperty(image,"cipher:type");
679 (void) DeleteImageProperty(image,"cipher:mode");
680 (void) DeleteImageProperty(image,"cipher:nonce");
681 image->taint=MagickFalse;
682
683 quantum_info=DestroyQuantumInfo(quantum_info);
684 aes_info=DestroyAESInfo(aes_info);
685 (void) ResetMagickMemory(input_block,0,sizeof(input_block));
686 (void) ResetMagickMemory(output_block,0,sizeof(output_block));
687 return(y == (ssize_t) image->rows ? MagickTrue : MagickFalse);
688}
689␌
690/*
691%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
692% %
693% %
694% %
695% P a s s k e y E n c i p h e r I m a g e %
696% %
697% %
698% %
699%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
700%
701% PasskeyEncipherImage() converts pixels to cipher-pixels.
702%
703*/
704
705MagickExport MagickBooleanType EncipherImage(Image *image,
706 const char *passphrase,ExceptionInfo *exception)
707{
708 MagickBooleanType
709 status;
710
711 StringInfo
712 *passkey;
713
714 if (passphrase == (const char *) NULL)
715 return(MagickTrue);
716 passkey=StringToStringInfo(passphrase);
717 if (passkey == (StringInfo *) NULL)
718 return(MagickFalse);
719 status=PasskeyEncipherImage(image,passkey,exception);
720 passkey=DestroyStringInfo(passkey);
721 return(status);
722}
723
724MagickExport MagickBooleanType PasskeyEncipherImage(Image *image,
725 const StringInfo *passkey,ExceptionInfo *exception)
726{
727#define EncipherImageTag "Encipher/Image "
728
729 AESInfo
730 *aes_info;
731
732 CacheView
733 *image_view;
734
735 char
736 *signature;
737
738 const unsigned char
739 *digest;
740
741 MagickBooleanType
742 proceed;
743
744 MagickSizeType
745 extent;
746
747 QuantumInfo
748 *quantum_info;
749
750 QuantumType
751 quantum_type;
752
753 unsigned char
754 *p;
755
756 SignatureInfo
757 *signature_info;
758
759 size_t
760 length;
761
762 ssize_t
763 y;
764
765 StringInfo
766 *key,
767 *nonce;
768
769 unsigned char
770 input_block[AESBlocksize],
771 output_block[AESBlocksize],
772 *pixels;
773
774 assert(image != (Image *) NULL);
775 assert(image->signature == MagickCoreSignature);
776 assert(exception != (ExceptionInfo *) NULL);
777 assert(exception->signature == MagickCoreSignature);
778 if (IsEventLogging() != MagickFalse)
779 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
780 if (passkey == (const StringInfo *) NULL)
781 return(MagickTrue);
782 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
783 return(MagickFalse);
784
785 aes_info=AcquireAESInfo();
786 key=CloneStringInfo(passkey);
787 if (key == (StringInfo *) NULL)
788 {
789 aes_info=DestroyAESInfo(aes_info);
790 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
791 image->filename);
792 }
793 nonce=SplitStringInfo(key,GetStringInfoLength(key)/2);
794 if (nonce == (StringInfo *) NULL)
795 {
796 key=DestroyStringInfo(key);
797 aes_info=DestroyAESInfo(aes_info);
798 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
799 image->filename);
800 }
801 SetAESKey(aes_info,key);
802 key=DestroyStringInfo(key);
803 signature_info=AcquireSignatureInfo();
804 UpdateSignature(signature_info,nonce);
805 extent=(MagickSizeType) image->columns*image->rows;
806 SetStringInfoLength(nonce,sizeof(extent));
807 SetStringInfoDatum(nonce,(const unsigned char *) &extent);
808 UpdateSignature(signature_info,nonce);
809 nonce=DestroyStringInfo(nonce);
810 FinalizeSignature(signature_info);
811 signature=StringInfoToHexString(GetSignatureDigest(signature_info));
812 (void) SetImageProperty(image,"cipher:type","AES",exception);
813 (void) SetImageProperty(image,"cipher:mode","CTR",exception);
814 (void) SetImageProperty(image,"cipher:nonce",signature,exception);
815 signature=DestroyString(signature);
816
817 (void) memset(input_block,0,sizeof(input_block));
818 digest=GetStringInfoDatum(GetSignatureDigest(signature_info));
819 (void) memcpy(input_block,digest,MagickMin(AESBlocksize,
820 GetSignatureDigestsize(signature_info))*sizeof(*input_block));
821 signature_info=DestroySignatureInfo(signature_info);
822
823 quantum_info=AcquireQuantumInfo((const ImageInfo *) NULL,image);
824 if (quantum_info == (QuantumInfo *) NULL)
825 {
826 aes_info=DestroyAESInfo(aes_info);
827 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
828 image->filename);
829 }
830 quantum_type=GetQuantumType(image,exception);
831 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
832 image_view=AcquireAuthenticCacheView(image,exception);
833 for (y=0; y < (ssize_t) image->rows; y++)
834 {
835 ssize_t
836 i,
837 x;
838
839 Quantum
840 *magick_restrict q;
841
842 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
843 if (q == (Quantum *) NULL)
844 break;
845 length=ExportQuantumPixels(image,image_view,quantum_info,quantum_type,
846 pixels,exception);
847 p=pixels;
848 for (x=0; x < (ssize_t) length; x+=AESBlocksize)
849 {
850 (void) memcpy(output_block,input_block,AESBlocksize*
851 sizeof(*output_block));
852 if (IncrementCipherNonce(AESBlocksize,input_block) == MagickFalse)
853 {
854 image_view=DestroyCacheView(image_view);
855 quantum_info=DestroyQuantumInfo(quantum_info);
856 aes_info=DestroyAESInfo(aes_info);
857 ThrowBinaryException(ResourceLimitError,"SequenceWrapError",image->filename);
858 }
859 EncipherAESBlock(aes_info,output_block,output_block);
860 for (i=0; i < AESBlocksize; i++)
861 p[i]^=output_block[i];
862 p+=(ptrdiff_t) AESBlocksize;
863 }
864 (void) memcpy(output_block,input_block,AESBlocksize*
865 sizeof(*output_block));
866 EncipherAESBlock(aes_info,output_block,output_block);
867 for (i=0; x < (ssize_t) length; x++)
868 {
869 p[i]^=output_block[i];
870 i++;
871 }
872 (void) ImportQuantumPixels(image,image_view,quantum_info,quantum_type,
873 pixels,exception);
874 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
875 break;
876 proceed=SetImageProgress(image,EncipherImageTag,(MagickOffsetType) y,
877 image->rows);
878 if (proceed == MagickFalse)
879 break;
880 }
881 image_view=DestroyCacheView(image_view);
882 image->taint=MagickFalse;
883
884 quantum_info=DestroyQuantumInfo(quantum_info);
885 aes_info=DestroyAESInfo(aes_info);
886 (void) ResetMagickMemory(input_block,0,sizeof(input_block));
887 (void) ResetMagickMemory(output_block,0,sizeof(output_block));
888 return(y == (ssize_t) image->rows ? MagickTrue : MagickFalse);
889}
890␌
891/*
892%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
893% %
894% %
895% %
896% S e t A E S K e y %
897% %
898% %
899% %
900%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
901%
902% SetAESKey() sets the key for the AES cipher.
903%
904*/
905
906static inline void InverseAddRoundKey(const unsigned int *alpha,
907 unsigned int *beta)
908{
909 unsigned int
910 i,
911 j;
912
913 for (i=0; i < 4; i++)
914 {
915 beta[i]=0;
916 for (j=0; j < 4; j++)
917 beta[i]|=(ByteMultiply(0xe,(alpha[i] >> (8*j)) & 0xff) ^
918 ByteMultiply(0xb,(alpha[i] >> (8*((j+1) % 4))) & 0xff) ^
919 ByteMultiply(0xd,(alpha[i] >> (8*((j+2) % 4))) & 0xff) ^
920 ByteMultiply(0x9,(alpha[i] >> (8*((j+3) % 4))) & 0xff)) << (8*j);
921 }
922}
923
924static inline unsigned int XTime(unsigned char alpha)
925{
926 unsigned char
927 beta;
928
929 beta=(unsigned char) ((alpha & 0x80) != 0 ? 0x1b : 0);
930 alpha<<=1;
931 alpha^=beta;
932 return(alpha);
933}
934
935static inline unsigned int RotateRight(const unsigned int x)
936{
937 return((x >> 8) | ((x & 0xff) << 24));
938}
939
940static void SetAESKey(AESInfo *aes_info,const StringInfo *key)
941{
942 ssize_t
943 bytes,
944 i,
945 n;
946
947 unsigned char
948 *datum;
949
950 unsigned int
951 alpha,
952 beta;
953
954 assert(aes_info != (AESInfo *) NULL);
955 assert(aes_info->signature == MagickCoreSignature);
956 assert(key != (StringInfo *) NULL);
957 if (IsEventLogging() != MagickFalse)
958 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
959 n=4;
960 aes_info->rounds=10;
961 if ((8*GetStringInfoLength(key)) >= 256)
962 {
963 n=8;
964 aes_info->rounds=14;
965 }
966 else
967 if ((8*GetStringInfoLength(key)) >= 192)
968 {
969 n=6;
970 aes_info->rounds=12;
971 }
972
973 datum=GetStringInfoDatum(aes_info->key);
974 (void) memset(datum,0,GetStringInfoLength(aes_info->key));
975 (void) memcpy(datum,GetStringInfoDatum(key),MagickMin(
976 GetStringInfoLength(key),GetStringInfoLength(aes_info->key)));
977 for (i=0; i < n; i++)
978 aes_info->encipher_key[i]=(unsigned int) datum[4*i] |
979 ((unsigned int) datum[4*i+1] << 8) |
980 ((unsigned int) datum[4*i+2] << 16) |
981 ((unsigned int) datum[4*i+3] << 24);
982 beta=1;
983 bytes=(AESBlocksize/4)*(aes_info->rounds+1);
984 for (i=n; i < bytes; i++)
985 {
986 alpha=aes_info->encipher_key[i-1];
987 if ((i % n) == 0)
988 {
989 alpha=ByteSubTransform(RotateRight(alpha),SBox) ^ beta;
990 beta=XTime((unsigned char) (beta & 0xff));
991 }
992 else
993 if ((n > 6) && ((i % n) == 4))
994 alpha=ByteSubTransform(alpha,SBox);
995 aes_info->encipher_key[i]=aes_info->encipher_key[i-n] ^ alpha;
996 }
997 for (i=0; i < 4; i++)
998 {
999 aes_info->decipher_key[i]=aes_info->encipher_key[i];
1000 aes_info->decipher_key[bytes-4+i]=aes_info->encipher_key[bytes-4+i];
1001 }
1002 for (i=4; i < (bytes-4); i+=4)
1003 InverseAddRoundKey(aes_info->encipher_key+i,aes_info->decipher_key+i);
1004 datum=GetStringInfoDatum(aes_info->key);
1005 (void) memset(datum,0,GetStringInfoLength(aes_info->key));
1006 alpha=0;
1007 beta=0;
1008}
1009#else
1010␌
1011/*
1012 Disabled cipher support fallback stubs.
1013*/
1014MagickExport MagickBooleanType DecipherImage(Image *image,
1015 const char *passphrase,ExceptionInfo *exception)
1016{
1017 assert(image != (Image *) NULL);
1018 assert(image->signature == MagickCoreSignature);
1019 assert(exception != (ExceptionInfo *) NULL);
1020 assert(exception->signature == MagickCoreSignature);
1021 if (IsEventLogging() != MagickFalse)
1022 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1023 (void) passphrase;
1024 ThrowBinaryException(ImageError,"CipherSupportNotEnabled",image->filename);
1025}
1026
1027MagickExport MagickBooleanType PasskeyDecipherImage(Image *image,
1028 const StringInfo *passkey,ExceptionInfo *exception)
1029{
1030 assert(image != (Image *) NULL);
1031 assert(image->signature == MagickCoreSignature);
1032 assert(exception != (ExceptionInfo *) NULL);
1033 assert(exception->signature == MagickCoreSignature);
1034 if (IsEventLogging() != MagickFalse)
1035 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1036 (void) passkey;
1037 ThrowBinaryException(ImageError,"CipherSupportNotEnabled",image->filename);
1038}
1039
1040MagickExport MagickBooleanType EncipherImage(Image *image,
1041 const char *passphrase,ExceptionInfo *exception)
1042{
1043 assert(image != (Image *) NULL);
1044 assert(image->signature == MagickCoreSignature);
1045 assert(exception != (ExceptionInfo *) NULL);
1046 assert(exception->signature == MagickCoreSignature);
1047 if (IsEventLogging() != MagickFalse)
1048 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1049 (void) passphrase;
1050 ThrowBinaryException(ImageError,"CipherSupportNotEnabled",image->filename);
1051}
1052
1053MagickExport MagickBooleanType PasskeyEncipherImage(Image *image,
1054 const StringInfo *passkey,ExceptionInfo *exception)
1055{
1056 assert(image != (Image *) NULL);
1057 assert(image->signature == MagickCoreSignature);
1058 assert(exception != (ExceptionInfo *) NULL);
1059 assert(exception->signature == MagickCoreSignature);
1060 if (IsEventLogging() != MagickFalse)
1061 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1062 (void) passkey;
1063 ThrowBinaryException(ImageError,"CipherSupportNotEnabled",image->filename);
1064}
1065#endif