2
0

str2key.c 2.5 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. * DES low level APIs are deprecated for public use, but still ok for internal
  11. * use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <openssl/crypto.h>
  15. #include "des_local.h"
  16. void DES_string_to_key(const char *str, DES_cblock *key)
  17. {
  18. DES_key_schedule ks;
  19. int i, length;
  20. memset(key, 0, 8);
  21. length = strlen(str);
  22. for (i = 0; i < length; i++) {
  23. register unsigned char j = str[i];
  24. if ((i % 16) < 8)
  25. (*key)[i % 8] ^= (j << 1);
  26. else {
  27. /* Reverse the bit order 05/05/92 eay */
  28. j = ((j << 4) & 0xf0) | ((j >> 4) & 0x0f);
  29. j = ((j << 2) & 0xcc) | ((j >> 2) & 0x33);
  30. j = ((j << 1) & 0xaa) | ((j >> 1) & 0x55);
  31. (*key)[7 - (i % 8)] ^= j;
  32. }
  33. }
  34. DES_set_odd_parity(key);
  35. DES_set_key_unchecked(key, &ks);
  36. DES_cbc_cksum((const unsigned char *)str, key, length, &ks, key);
  37. OPENSSL_cleanse(&ks, sizeof(ks));
  38. DES_set_odd_parity(key);
  39. }
  40. void DES_string_to_2keys(const char *str, DES_cblock *key1, DES_cblock *key2)
  41. {
  42. DES_key_schedule ks;
  43. int i, length;
  44. memset(key1, 0, 8);
  45. memset(key2, 0, 8);
  46. length = strlen(str);
  47. for (i = 0; i < length; i++) {
  48. register unsigned char j = str[i];
  49. if ((i % 32) < 16) {
  50. if ((i % 16) < 8)
  51. (*key1)[i % 8] ^= (j << 1);
  52. else
  53. (*key2)[i % 8] ^= (j << 1);
  54. } else {
  55. j = ((j << 4) & 0xf0) | ((j >> 4) & 0x0f);
  56. j = ((j << 2) & 0xcc) | ((j >> 2) & 0x33);
  57. j = ((j << 1) & 0xaa) | ((j >> 1) & 0x55);
  58. if ((i % 16) < 8)
  59. (*key1)[7 - (i % 8)] ^= j;
  60. else
  61. (*key2)[7 - (i % 8)] ^= j;
  62. }
  63. }
  64. if (length <= 8)
  65. memcpy(key2, key1, 8);
  66. DES_set_odd_parity(key1);
  67. DES_set_odd_parity(key2);
  68. DES_set_key_unchecked(key1, &ks);
  69. DES_cbc_cksum((const unsigned char *)str, key1, length, &ks, key1);
  70. DES_set_key_unchecked(key2, &ks);
  71. DES_cbc_cksum((const unsigned char *)str, key2, length, &ks, key2);
  72. OPENSSL_cleanse(&ks, sizeof(ks));
  73. DES_set_odd_parity(key1);
  74. DES_set_odd_parity(key2);
  75. }