des.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the License); you may
  5. * not use this file except in compliance with the License.
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. */
  9. /* FIPS PUB 46-3 "Data Encryption Standard (DES)" */
  10. #ifndef GMSSL_DES_H
  11. #define GMSSL_DES_H
  12. #include <stdint.h>
  13. #include <stdlib.h>
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. #define DES_KEY_BITS 56
  18. #define DES_BLOCK_BITS 64
  19. #define DES_KEY_SIZE ((DES_KEY_BITS)/7)
  20. #define DES_BLOCK_SIZE (DES_BLOCK_BITS/8)
  21. #define DES_RK_BITS 48
  22. #define DES_RK_SIZE (DES_RK_BITS/8)
  23. #define DES_ROUNDS 16
  24. #define DES_EDE_KEY_SIZE (DES_KEY_SIZE * 3)
  25. typedef struct {
  26. uint64_t rk[DES_ROUNDS];
  27. } DES_KEY;
  28. void des_set_encrypt_key(DES_KEY *key, const uint8_t raw_key[DES_KEY_SIZE]);
  29. void des_set_decrypt_key(DES_KEY *key, const uint8_t raw_key[DES_KEY_SIZE]);
  30. void des_encrypt(DES_KEY *key, const uint8_t in[DES_BLOCK_SIZE], uint8_t out[DES_BLOCK_SIZE]);
  31. typedef struct {
  32. DES_KEY K[3];
  33. } DES_EDE_KEY;
  34. void des_ede_set_encrypt_key(DES_EDE_KEY *key, const uint8_t raw_key[DES_EDE_KEY_SIZE]);
  35. void des_ede_set_decrypt_key(DES_EDE_KEY *key, const uint8_t raw_key[DES_EDE_KEY_SIZE]);
  36. void des_ede_encrypt(DES_EDE_KEY *key, const uint8_t in[DES_BLOCK_SIZE], uint8_t out[DES_BLOCK_SIZE]);
  37. #ifdef __cplusplus
  38. }
  39. #endif
  40. #endif