block_cipher.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #ifndef GMSSL_BLOCK_CIPHER_H
  10. #define GMSSL_BLOCK_CIPHER_H
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #include <gmssl/aes.h>
  15. #include <gmssl/sm4.h>
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. #define BLOCK_CIPHER_BLOCK_SIZE 16
  20. #define BLOCK_CIPHER_MIN_KEY_SIZE 16
  21. #define BLOCK_CIPHER_MAX_KEY_SIZE 32
  22. typedef struct BLOCK_CIPHER BLOCK_CIPHER;
  23. typedef struct BLOCK_CIPHER_KEY BLOCK_CIPHER_KEY;
  24. struct BLOCK_CIPHER_KEY {
  25. union {
  26. SM4_KEY sm4_key;
  27. AES_KEY aes_key;
  28. } u;
  29. const BLOCK_CIPHER *cipher;
  30. };
  31. typedef void (*block_cipher_set_encrypt_key_func)(BLOCK_CIPHER_KEY *key, const uint8_t *raw_key);
  32. typedef void (*block_cipher_set_decrypt_key_func)(BLOCK_CIPHER_KEY *key, const uint8_t *raw_key);
  33. typedef void (*block_cipher_encrypt_func)(const BLOCK_CIPHER_KEY *key, const uint8_t *in, uint8_t *out);
  34. typedef void (*block_cipher_decrypt_func)(const BLOCK_CIPHER_KEY *key, const uint8_t *in, uint8_t *out);
  35. struct BLOCK_CIPHER {
  36. int oid;
  37. size_t key_size;
  38. size_t block_size;
  39. block_cipher_set_encrypt_key_func set_encrypt_key;
  40. block_cipher_set_decrypt_key_func set_decrypt_key;
  41. block_cipher_encrypt_func encrypt;
  42. block_cipher_decrypt_func decrypt;
  43. };
  44. const BLOCK_CIPHER *BLOCK_CIPHER_sm4(void);
  45. const BLOCK_CIPHER *BLOCK_CIPHER_aes128(void);
  46. const BLOCK_CIPHER *block_cipher_from_name(const char *name);
  47. const char *block_cipher_name(const BLOCK_CIPHER *cipher);
  48. int block_cipher_set_encrypt_key(BLOCK_CIPHER_KEY *key, const BLOCK_CIPHER *cipher, const uint8_t *raw_key);
  49. int block_cipher_set_decrypt_key(BLOCK_CIPHER_KEY *key, const BLOCK_CIPHER *cipher, const uint8_t *raw_key);
  50. int block_cipher_encrypt(const BLOCK_CIPHER_KEY *key, const uint8_t *in, uint8_t *out);
  51. int block_cipher_decrypt(const BLOCK_CIPHER_KEY *key, const uint8_t *in, uint8_t *out);
  52. #ifdef __cplusplus
  53. }
  54. #endif
  55. #endif