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