DES.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. /* Setting this will cause LibTomCrypt to return CRYPT_INVALID_ARG when its
  27. * assert-like LTC_ARGCHK macro fails. */
  28. #define ARGTYPE 4
  29. /* Include the actial DES implementation */
  30. #include "libtom/tomcrypt_des.c"
  31. #undef DES /* this is needed because tomcrypt_custom.h defines DES to an empty string */
  32. #include <assert.h>
  33. #include "Python.h"
  34. typedef struct {
  35. symmetric_key sk;
  36. } block_state;
  37. static void ltcseterr(int rc)
  38. {
  39. /* error */
  40. switch (rc) {
  41. case CRYPT_INVALID_ARG:
  42. PyErr_SetString(PyExc_AssertionError, "CRYPT_INVALID_ARG");
  43. break;
  44. case CRYPT_INVALID_KEYSIZE:
  45. PyErr_SetString(PyExc_ValueError, "Invalid key size (must be either 16 or 24 bytes long)");
  46. break;
  47. case CRYPT_INVALID_ROUNDS:
  48. PyErr_SetString(PyExc_ValueError, "Invalid number of rounds specified");
  49. break;
  50. default:
  51. PyErr_Format(PyExc_RuntimeError,
  52. "unexpected run-time error (LTC#%d)", rc);
  53. }
  54. }
  55. static void block_init(block_state *self, unsigned char *key, int keylen)
  56. {
  57. int rc;
  58. #ifdef PCT_DES3_MODULE
  59. rc = des3_setup(key, keylen, 0, &self->sk);
  60. #else
  61. rc = des_setup(key, keylen, 0, &self->sk);
  62. #endif
  63. if (rc != CRYPT_OK) {
  64. ltcseterr(rc);
  65. }
  66. }
  67. static void block_encrypt(block_state *self, unsigned char *in, unsigned char *out)
  68. {
  69. int rc;
  70. #ifdef PCT_DES3_MODULE
  71. rc = des3_ecb_encrypt(in, out, &self->sk);
  72. #else
  73. rc = des_ecb_encrypt(in, out, &self->sk);
  74. #endif
  75. assert(rc == CRYPT_OK);
  76. }
  77. static void block_decrypt(block_state *self, unsigned char *in, unsigned char *out)
  78. {
  79. int rc;
  80. #ifdef PCT_DES3_MODULE
  81. rc = des3_ecb_decrypt(in, out, &self->sk);
  82. #else
  83. rc = des_ecb_decrypt(in, out, &self->sk);
  84. #endif
  85. assert(rc == CRYPT_OK);
  86. }
  87. #ifdef PCT_DES3_MODULE
  88. # define MODULE_NAME _DES3 /* triple DES */
  89. # define BLOCK_SIZE 8 /* 64-bit block size */
  90. # define KEY_SIZE 0 /* variable key size (can be 128 or 192 bits (including parity) */
  91. #else
  92. # define MODULE_NAME _DES /* single DES */
  93. # define BLOCK_SIZE 8 /* 64-bit block size */
  94. # define KEY_SIZE 8 /* 64-bit keys (including parity) */
  95. #endif
  96. #include "block_template.c"