qud_cksm.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. /*
  16. * DES low level APIs are deprecated for public use, but still ok for internal
  17. * use.
  18. */
  19. #include "internal/deprecated.h"
  20. #include "des_local.h"
  21. #define Q_B0(a) (((DES_LONG)(a)))
  22. #define Q_B1(a) (((DES_LONG)(a))<<8)
  23. #define Q_B2(a) (((DES_LONG)(a))<<16)
  24. #define Q_B3(a) (((DES_LONG)(a))<<24)
  25. /* used to scramble things a bit */
  26. /* Got the value MIT uses via brute force :-) 2/10/90 eay */
  27. #define NOISE ((DES_LONG)83653421L)
  28. DES_LONG DES_quad_cksum(const unsigned char *input, DES_cblock output[],
  29. long length, int out_count, DES_cblock *seed)
  30. {
  31. DES_LONG z0, z1, t0, t1;
  32. int i;
  33. long l;
  34. const unsigned char *cp;
  35. DES_LONG *lp;
  36. if (out_count < 1)
  37. out_count = 1;
  38. lp = (DES_LONG *)&(output[0])[0];
  39. z0 = Q_B0((*seed)[0]) | Q_B1((*seed)[1]) | Q_B2((*seed)[2]) |
  40. Q_B3((*seed)[3]);
  41. z1 = Q_B0((*seed)[4]) | Q_B1((*seed)[5]) | Q_B2((*seed)[6]) |
  42. Q_B3((*seed)[7]);
  43. for (i = 0; ((i < 4) && (i < out_count)); i++) {
  44. cp = input;
  45. l = length;
  46. while (l > 0) {
  47. if (l > 1) {
  48. t0 = (DES_LONG)(*(cp++));
  49. t0 |= (DES_LONG)Q_B1(*(cp++));
  50. l--;
  51. } else
  52. t0 = (DES_LONG)(*(cp++));
  53. l--;
  54. /* add */
  55. t0 += z0;
  56. t0 &= 0xffffffffL;
  57. t1 = z1;
  58. /* square, well sort of square */
  59. z0 = ((((t0 * t0) & 0xffffffffL) + ((t1 * t1) & 0xffffffffL))
  60. & 0xffffffffL) % 0x7fffffffL;
  61. z1 = ((t0 * ((t1 + NOISE) & 0xffffffffL)) & 0xffffffffL) %
  62. 0x7fffffffL;
  63. }
  64. if (lp != NULL) {
  65. /*
  66. * The MIT library assumes that the checksum is composed of
  67. * 2*out_count 32 bit ints
  68. */
  69. *lp++ = z0;
  70. *lp++ = z1;
  71. }
  72. }
  73. return z0;
  74. }