des_crypt.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * des_crypt.c, DES encryption library routines
  3. * Copyright (c) 2010, Oracle America, Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following
  13. * disclaimer in the documentation and/or other materials
  14. * provided with the distribution.
  15. * * Neither the name of the "Oracle America, Inc." nor the names of its
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  24. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  26. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include <sys/types.h>
  33. #include <rpc/des_crypt.h>
  34. #include <shlib-compat.h>
  35. #include "des.h"
  36. extern int _des_crypt (char *, unsigned, struct desparams *);
  37. /*
  38. * Copy 8 bytes
  39. */
  40. #define COPY8(src, dst) { \
  41. register char *a = (char *) dst; \
  42. register char *b = (char *) src; \
  43. *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
  44. *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
  45. }
  46. /*
  47. * Copy multiple of 8 bytes
  48. */
  49. #define DESCOPY(src, dst, len) { \
  50. register char *a = (char *) dst; \
  51. register char *b = (char *) src; \
  52. register int i; \
  53. for (i = (int) len; i > 0; i -= 8) { \
  54. *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
  55. *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
  56. } \
  57. }
  58. /*
  59. * Common code to cbc_crypt() & ecb_crypt()
  60. */
  61. static int
  62. common_crypt (char *key, char *buf, register unsigned len,
  63. unsigned mode, register struct desparams *desp)
  64. {
  65. register int desdev;
  66. if ((len % 8) != 0 || len > DES_MAXDATA)
  67. return DESERR_BADPARAM;
  68. desp->des_dir =
  69. ((mode & DES_DIRMASK) == DES_ENCRYPT) ? ENCRYPT : DECRYPT;
  70. desdev = mode & DES_DEVMASK;
  71. COPY8 (key, desp->des_key);
  72. /*
  73. * software
  74. */
  75. if (!_des_crypt (buf, len, desp))
  76. return DESERR_HWERROR;
  77. return desdev == DES_SW ? DESERR_NONE : DESERR_NOHWDEVICE;
  78. }
  79. /* Note: these cannot be excluded from the build yet, because they are
  80. still used internally. */
  81. /*
  82. * CBC mode encryption
  83. */
  84. int
  85. cbc_crypt (char *key, char *buf, unsigned int len, unsigned int mode,
  86. char *ivec)
  87. {
  88. int err;
  89. struct desparams dp;
  90. dp.des_mode = CBC;
  91. COPY8 (ivec, dp.des_ivec);
  92. err = common_crypt (key, buf, len, mode, &dp);
  93. COPY8 (dp.des_ivec, ivec);
  94. return err;
  95. }
  96. hidden_nolink (cbc_crypt, libc, GLIBC_2_1)
  97. /*
  98. * ECB mode encryption
  99. */
  100. int
  101. ecb_crypt (char *key, char *buf, unsigned int len, unsigned int mode)
  102. {
  103. struct desparams dp;
  104. dp.des_mode = ECB;
  105. return common_crypt (key, buf, len, mode, &dp);
  106. }
  107. hidden_nolink (ecb_crypt, libc, GLIBC_2_1)