1 /*
   2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.  Oracle designates this
   7  * particular file as subject to the "Classpath" exception as provided
   8  * by Oracle in the LICENSE file that accompanied this code.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 // This file is available under and governed by the GNU General Public
  26 // License version 2 only, as published by the Free Software Foundation.
  27 // However, the following notice accompanied the original version of this
  28 // file:
  29 //
  30 //---------------------------------------------------------------------------------
  31 //
  32 //  Little Color Management System
  33 //  Copyright (c) 1998-2020 Marti Maria Saguer
  34 //
  35 // Permission is hereby granted, free of charge, to any person obtaining
  36 // a copy of this software and associated documentation files (the "Software"),
  37 // to deal in the Software without restriction, including without limitation
  38 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  39 // and/or sell copies of the Software, and to permit persons to whom the Software
  40 // is furnished to do so, subject to the following conditions:
  41 //
  42 // The above copyright notice and this permission notice shall be included in
  43 // all copies or substantial portions of the Software.
  44 //
  45 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  46 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  47 // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  48 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  49 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  50 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  51 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  52 //
  53 //---------------------------------------------------------------------------------
  54 
  55 
  56 #include "lcms2_internal.h"
  57 
  58 #ifdef CMS_USE_BIG_ENDIAN
  59 
  60 static
  61 void byteReverse(cmsUInt8Number * buf, cmsUInt32Number longs)
  62 {
  63     do {
  64 
  65         cmsUInt32Number t = _cmsAdjustEndianess32(*(cmsUInt32Number *) buf);
  66         *(cmsUInt32Number *) buf = t;
  67         buf += sizeof(cmsUInt32Number);
  68 
  69     } while (--longs);
  70 
  71 }
  72 
  73 #else
  74 #define byteReverse(buf, len)
  75 #endif
  76 
  77 
  78 typedef struct {
  79 
  80     cmsUInt32Number buf[4];
  81     cmsUInt32Number bits[2];
  82     cmsUInt8Number in[64];
  83     cmsContext ContextID;
  84 
  85 } _cmsMD5;
  86 
  87 #define F1(x, y, z) (z ^ (x & (y ^ z)))
  88 #define F2(x, y, z) F1(z, x, y)
  89 #define F3(x, y, z) (x ^ y ^ z)
  90 #define F4(x, y, z) (y ^ (x | ~z))
  91 
  92 #define STEP(f, w, x, y, z, data, s) \
  93     ( w += f(x, y, z) + data,  w = w<<s | w>>(32-s),  w += x )
  94 
  95 
  96 static
  97 void cmsMD5_Transform(cmsUInt32Number buf[4], cmsUInt32Number in[16])
  98 {
  99     CMSREGISTER cmsUInt32Number a, b, c, d;
 100 
 101     a = buf[0];
 102     b = buf[1];
 103     c = buf[2];
 104     d = buf[3];
 105 
 106     STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
 107     STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
 108     STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
 109     STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
 110     STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
 111     STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
 112     STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
 113     STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
 114     STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
 115     STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
 116     STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
 117     STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
 118     STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
 119     STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
 120     STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
 121     STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
 122 
 123     STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
 124     STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
 125     STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
 126     STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
 127     STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
 128     STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
 129     STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
 130     STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
 131     STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
 132     STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
 133     STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
 134     STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
 135     STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
 136     STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
 137     STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
 138     STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
 139 
 140     STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
 141     STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
 142     STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
 143     STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
 144     STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
 145     STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
 146     STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
 147     STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
 148     STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
 149     STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
 150     STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
 151     STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
 152     STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
 153     STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
 154     STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
 155     STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
 156 
 157     STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
 158     STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
 159     STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
 160     STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
 161     STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
 162     STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
 163     STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
 164     STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
 165     STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
 166     STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
 167     STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
 168     STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
 169     STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
 170     STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
 171     STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
 172     STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
 173 
 174     buf[0] += a;
 175     buf[1] += b;
 176     buf[2] += c;
 177     buf[3] += d;
 178 }
 179 
 180 
 181 // Create a MD5 object
 182 
 183 cmsHANDLE CMSEXPORT cmsMD5alloc(cmsContext ContextID)
 184 {
 185     _cmsMD5* ctx = (_cmsMD5*) _cmsMallocZero(ContextID, sizeof(_cmsMD5));
 186     if (ctx == NULL) return NULL;
 187 
 188     ctx ->ContextID = ContextID;
 189 
 190     ctx->buf[0] = 0x67452301;
 191     ctx->buf[1] = 0xefcdab89;
 192     ctx->buf[2] = 0x98badcfe;
 193     ctx->buf[3] = 0x10325476;
 194 
 195     ctx->bits[0] = 0;
 196     ctx->bits[1] = 0;
 197 
 198     return (cmsHANDLE) ctx;
 199 }
 200 
 201 void CMSEXPORT cmsMD5add(cmsHANDLE Handle, const cmsUInt8Number* buf, cmsUInt32Number len)
 202 {
 203     _cmsMD5* ctx = (_cmsMD5*) Handle;
 204     cmsUInt32Number t;
 205 
 206     t = ctx->bits[0];
 207     if ((ctx->bits[0] = t + (len << 3)) < t)
 208         ctx->bits[1]++;
 209 
 210     ctx->bits[1] += len >> 29;
 211 
 212     t = (t >> 3) & 0x3f;
 213 
 214     if (t) {
 215 
 216         cmsUInt8Number *p = (cmsUInt8Number *) ctx->in + t;
 217 
 218         t = 64 - t;
 219         if (len < t) {
 220             memmove(p, buf, len);
 221             return;
 222         }
 223 
 224         memmove(p, buf, t);
 225         byteReverse(ctx->in, 16);
 226 
 227         cmsMD5_Transform(ctx->buf, (cmsUInt32Number *) ctx->in);
 228         buf += t;
 229         len -= t;
 230     }
 231 
 232     while (len >= 64) {
 233         memmove(ctx->in, buf, 64);
 234         byteReverse(ctx->in, 16);
 235         cmsMD5_Transform(ctx->buf, (cmsUInt32Number *) ctx->in);
 236         buf += 64;
 237         len -= 64;
 238     }
 239 
 240     memmove(ctx->in, buf, len);
 241 }
 242 
 243 // Destroy the object and return the checksum
 244 void CMSEXPORT cmsMD5finish(cmsProfileID* ProfileID,  cmsHANDLE Handle)
 245 {
 246     _cmsMD5* ctx = (_cmsMD5*) Handle;
 247     cmsUInt32Number count;
 248     cmsUInt8Number *p;
 249 
 250     count = (ctx->bits[0] >> 3) & 0x3F;
 251 
 252     p = ctx->in + count;
 253     *p++ = 0x80;
 254 
 255     count = 64 - 1 - count;
 256 
 257     if (count < 8) {
 258 
 259         memset(p, 0, count);
 260         byteReverse(ctx->in, 16);
 261         cmsMD5_Transform(ctx->buf, (cmsUInt32Number *) ctx->in);
 262 
 263         memset(ctx->in, 0, 56);
 264     } else {
 265         memset(p, 0, count - 8);
 266     }
 267     byteReverse(ctx->in, 14);
 268 
 269     ((cmsUInt32Number *) ctx->in)[14] = ctx->bits[0];
 270     ((cmsUInt32Number *) ctx->in)[15] = ctx->bits[1];
 271 
 272     cmsMD5_Transform(ctx->buf, (cmsUInt32Number *) ctx->in);
 273 
 274     byteReverse((cmsUInt8Number *) ctx->buf, 4);
 275     memmove(ProfileID ->ID8, ctx->buf, 16);
 276 
 277     _cmsFree(ctx ->ContextID, ctx);
 278 }
 279 
 280 
 281 
 282 // Assuming io points to an ICC profile, compute and store MD5 checksum
 283 // In the header, rendering intentent, attributes and ID should be set to zero
 284 // before computing MD5 checksum (per 6.1.13 in ICC spec)
 285 
 286 cmsBool CMSEXPORT cmsMD5computeID(cmsHPROFILE hProfile)
 287 {
 288     cmsContext   ContextID;
 289     cmsUInt32Number BytesNeeded;
 290     cmsUInt8Number* Mem = NULL;
 291     cmsHANDLE  MD5 = NULL;
 292     _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
 293     _cmsICCPROFILE Keep;
 294 
 295     _cmsAssert(hProfile != NULL);
 296 
 297     ContextID = cmsGetProfileContextID(hProfile);
 298 
 299     // Save a copy of the profile header
 300     memmove(&Keep, Icc, sizeof(_cmsICCPROFILE));
 301 
 302     // Set RI, attributes and ID
 303     memset(&Icc ->attributes, 0, sizeof(Icc ->attributes));
 304     Icc ->RenderingIntent = 0;
 305     memset(&Icc ->ProfileID, 0, sizeof(Icc ->ProfileID));
 306 
 307     // Compute needed storage
 308     if (!cmsSaveProfileToMem(hProfile, NULL, &BytesNeeded)) goto Error;
 309 
 310     // Allocate memory
 311     Mem = (cmsUInt8Number*) _cmsMalloc(ContextID, BytesNeeded);
 312     if (Mem == NULL) goto Error;
 313 
 314     // Save to temporary storage
 315     if (!cmsSaveProfileToMem(hProfile, Mem, &BytesNeeded)) goto Error;
 316 
 317     // Create MD5 object
 318     MD5 = cmsMD5alloc(ContextID);
 319     if (MD5 == NULL) goto Error;
 320 
 321     // Add all bytes
 322     cmsMD5add(MD5, Mem, BytesNeeded);
 323 
 324     // Temp storage is no longer needed
 325     _cmsFree(ContextID, Mem);
 326 
 327     // Restore header
 328     memmove(Icc, &Keep, sizeof(_cmsICCPROFILE));
 329 
 330     // And store the ID
 331     cmsMD5finish(&Icc ->ProfileID,  MD5);
 332     return TRUE;
 333 
 334 Error:
 335 
 336     // Free resources as something went wrong
 337     // "MD5" cannot be other than NULL here, so no need to free it
 338     if (Mem != NULL) _cmsFree(ContextID, Mem);
 339     memmove(Icc, &Keep, sizeof(_cmsICCPROFILE));
 340     return FALSE;
 341 }
 342