qud_cksm.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * From "Message Authentication" R.R. Jueneman, S.M. Matyas, C.H. Meyer IEEE
  11. * Communications Magazine Sept 1985 Vol. 23 No. 9 p 29-40 This module in
  12. * only based on the code in this paper and is almost definitely not the same
  13. * as the MIT implementation.
  14. */
  15. #include "des_locl.h"
  16. #define Q_B0(a) (((DES_LONG)(a)))
  17. #define Q_B1(a) (((DES_LONG)(a))<<8)
  18. #define Q_B2(a) (((DES_LONG)(a))<<16)
  19. #define Q_B3(a) (((DES_LONG)(a))<<24)
  20. /* used to scramble things a bit */
  21. /* Got the value MIT uses via brute force :-) 2/10/90 eay */
  22. #define NOISE ((DES_LONG)83653421L)
  23. DES_LONG DES_quad_cksum(const unsigned char *input, DES_cblock output[],
  24. long length, int out_count, DES_cblock *seed)
  25. {
  26. DES_LONG z0, z1, t0, t1;
  27. int i;
  28. long l;
  29. const unsigned char *cp;
  30. DES_LONG *lp;
  31. if (out_count < 1)
  32. out_count = 1;
  33. lp = (DES_LONG *)&(output[0])[0];
  34. z0 = Q_B0((*seed)[0]) | Q_B1((*seed)[1]) | Q_B2((*seed)[2]) |
  35. Q_B3((*seed)[3]);
  36. z1 = Q_B0((*seed)[4]) | Q_B1((*seed)[5]) | Q_B2((*seed)[6]) |
  37. Q_B3((*seed)[7]);
  38. for (i = 0; ((i < 4) && (i < out_count)); i++) {
  39. cp = input;
  40. l = length;
  41. while (l > 0) {
  42. if (l > 1) {
  43. t0 = (DES_LONG)(*(cp++));
  44. t0 |= (DES_LONG)Q_B1(*(cp++));
  45. l--;
  46. } else
  47. t0 = (DES_LONG)(*(cp++));
  48. l--;
  49. /* add */
  50. t0 += z0;
  51. t0 &= 0xffffffffL;
  52. t1 = z1;
  53. /* square, well sort of square */
  54. z0 = ((((t0 * t0) & 0xffffffffL) + ((t1 * t1) & 0xffffffffL))
  55. & 0xffffffffL) % 0x7fffffffL;
  56. z1 = ((t0 * ((t1 + NOISE) & 0xffffffffL)) & 0xffffffffL) %
  57. 0x7fffffffL;
  58. }
  59. if (lp != NULL) {
  60. /*
  61. * The MIT library assumes that the checksum is composed of
  62. * 2*out_count 32 bit ints
  63. */
  64. *lp++ = z0;
  65. *lp++ = z1;
  66. }
  67. }
  68. return z0;
  69. }