DES.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * DES.c: DES/3DES support for PyCrypto using LibTomCrypt
  3. *
  4. * Written in 2009 by Dwayne C. Litzenberger <dlitz@dlitz.net>
  5. *
  6. * ===================================================================
  7. * The contents of this file are dedicated to the public domain. To
  8. * the extent that dedication to the public domain is not available,
  9. * everyone is granted a worldwide, perpetual, royalty-free,
  10. * non-exclusive license to exercise all rights associated with the
  11. * contents of this file for any purpose whatsoever.
  12. * No rights are reserved.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  18. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  19. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. * ===================================================================
  23. *
  24. * Country of origin: Canada
  25. */
  26. #include "common.h"
  27. #include "block_base.h"
  28. #ifndef PCT_DES3_MODULE
  29. FAKE_INIT(raw_des)
  30. #else
  31. FAKE_INIT(raw_des3)
  32. #endif
  33. /* Setting this will cause LibTomCrypt to return CRYPT_INVALID_ARG when its
  34. * assert-like LTC_ARGCHK macro fails. */
  35. #define ARGTYPE 4
  36. /* Include the actial DES implementation */
  37. #define LTC_NO_PROTOTYPES
  38. #include "libtom/tomcrypt_des.c"
  39. struct block_state {
  40. symmetric_key sk;
  41. };
  42. static int block_init(struct block_state *self, const uint8_t *key, size_t keylen)
  43. {
  44. int rc;
  45. #ifdef PCT_DES3_MODULE
  46. rc = des3_setup(key, keylen, 0, &self->sk);
  47. #else
  48. rc = des_setup(key, keylen, 0, &self->sk);
  49. #endif
  50. switch (rc) {
  51. case CRYPT_OK:
  52. return 0;
  53. case CRYPT_INVALID_KEYSIZE:
  54. return ERR_KEY_SIZE;
  55. case CRYPT_INVALID_ROUNDS:
  56. return ERR_NR_ROUNDS;
  57. case CRYPT_INVALID_ARG:
  58. return ERR_UNKNOWN;
  59. }
  60. return ERR_UNKNOWN;
  61. }
  62. static void block_finalize(struct block_state *self)
  63. {
  64. }
  65. static void block_encrypt(struct block_state *self, const uint8_t *in, uint8_t *out)
  66. {
  67. #ifdef PCT_DES3_MODULE
  68. des3_ecb_encrypt(in, out, &self->sk);
  69. #else
  70. des_ecb_encrypt(in, out, &self->sk);
  71. #endif
  72. }
  73. static void block_decrypt(struct block_state *self, const uint8_t *in, uint8_t *out)
  74. {
  75. #ifdef PCT_DES3_MODULE
  76. des3_ecb_decrypt(in, out, &self->sk);
  77. #else
  78. des_ecb_decrypt(in, out, &self->sk);
  79. #endif
  80. }
  81. #ifdef PCT_DES3_MODULE
  82. # define MODULE_NAME DES3 /* triple DES */
  83. # define BLOCK_SIZE 8 /* 64-bit block size */
  84. # define KEY_SIZE 0 /* variable key size (can be 128 or 192 bits (including parity) */
  85. #else
  86. # define MODULE_NAME DES /* single DES */
  87. # define BLOCK_SIZE 8 /* 64-bit block size */
  88. # define KEY_SIZE 8 /* 64-bit keys (including parity) */
  89. #endif
  90. #include "block_common.c"