2
0

mdc2dgst.c 3.4 KB

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