mdc2dgst.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <openssl/crypto.h>
  13. #include <openssl/des.h>
  14. #include <openssl/mdc2.h>
  15. #undef c2l
  16. #define c2l(c,l) (l =((DES_LONG)(*((c)++))) , \
  17. l|=((DES_LONG)(*((c)++)))<< 8L, \
  18. l|=((DES_LONG)(*((c)++)))<<16L, \
  19. l|=((DES_LONG)(*((c)++)))<<24L)
  20. #undef l2c
  21. #define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
  22. *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
  23. *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
  24. *((c)++)=(unsigned char)(((l)>>24L)&0xff))
  25. static void mdc2_body(MDC2_CTX *c, const unsigned char *in, size_t len);
  26. int MDC2_Init(MDC2_CTX *c)
  27. {
  28. c->num = 0;
  29. c->pad_type = 1;
  30. memset(&(c->h[0]), 0x52, MDC2_BLOCK);
  31. memset(&(c->hh[0]), 0x25, MDC2_BLOCK);
  32. return 1;
  33. }
  34. int MDC2_Update(MDC2_CTX *c, const unsigned char *in, size_t len)
  35. {
  36. size_t i, j;
  37. i = c->num;
  38. if (i != 0) {
  39. if (len < MDC2_BLOCK - i) {
  40. /* partial block */
  41. memcpy(&(c->data[i]), in, len);
  42. c->num += (int)len;
  43. return 1;
  44. } else {
  45. /* filled one */
  46. j = MDC2_BLOCK - i;
  47. memcpy(&(c->data[i]), in, j);
  48. len -= j;
  49. in += j;
  50. c->num = 0;
  51. mdc2_body(c, &(c->data[0]), MDC2_BLOCK);
  52. }
  53. }
  54. i = len & ~((size_t)MDC2_BLOCK - 1);
  55. if (i > 0)
  56. mdc2_body(c, in, i);
  57. j = len - i;
  58. if (j > 0) {
  59. memcpy(&(c->data[0]), &(in[i]), j);
  60. c->num = (int)j;
  61. }
  62. return 1;
  63. }
  64. static void mdc2_body(MDC2_CTX *c, const unsigned char *in, size_t len)
  65. {
  66. register DES_LONG tin0, tin1;
  67. register DES_LONG ttin0, ttin1;
  68. DES_LONG d[2], dd[2];
  69. DES_key_schedule k;
  70. unsigned char *p;
  71. size_t i;
  72. for (i = 0; i < len; i += 8) {
  73. c2l(in, tin0);
  74. d[0] = dd[0] = tin0;
  75. c2l(in, tin1);
  76. d[1] = dd[1] = tin1;
  77. c->h[0] = (c->h[0] & 0x9f) | 0x40;
  78. c->hh[0] = (c->hh[0] & 0x9f) | 0x20;
  79. DES_set_odd_parity(&c->h);
  80. DES_set_key_unchecked(&c->h, &k);
  81. DES_encrypt1(d, &k, 1);
  82. DES_set_odd_parity(&c->hh);
  83. DES_set_key_unchecked(&c->hh, &k);
  84. DES_encrypt1(dd, &k, 1);
  85. ttin0 = tin0 ^ dd[0];
  86. ttin1 = tin1 ^ dd[1];
  87. tin0 ^= d[0];
  88. tin1 ^= d[1];
  89. p = c->h;
  90. l2c(tin0, p);
  91. l2c(ttin1, p);
  92. p = c->hh;
  93. l2c(ttin0, p);
  94. l2c(tin1, p);
  95. }
  96. }
  97. int MDC2_Final(unsigned char *md, MDC2_CTX *c)
  98. {
  99. unsigned int i;
  100. int j;
  101. i = c->num;
  102. j = c->pad_type;
  103. if ((i > 0) || (j == 2)) {
  104. if (j == 2)
  105. c->data[i++] = 0x80;
  106. memset(&(c->data[i]), 0, MDC2_BLOCK - i);
  107. mdc2_body(c, c->data, MDC2_BLOCK);
  108. }
  109. memcpy(md, (char *)c->h, MDC2_BLOCK);
  110. memcpy(&(md[MDC2_BLOCK]), (char *)c->hh, MDC2_BLOCK);
  111. return 1;
  112. }