PageRenderTime 62ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/MagickCore/cipher.c

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