• 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 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <gmssl/oid.h>
16 #include <gmssl/block_cipher.h>
17 #include <gmssl/endian.h>
18 
19 
block_cipher_set_encrypt_key(BLOCK_CIPHER_KEY * key,const BLOCK_CIPHER * cipher,const uint8_t * raw_key)20 int block_cipher_set_encrypt_key(BLOCK_CIPHER_KEY *key, const BLOCK_CIPHER *cipher, const uint8_t *raw_key)
21 {
22 	memset(key, 0, sizeof(BLOCK_CIPHER_KEY));
23 	cipher->set_encrypt_key(key, raw_key);
24 	key->cipher = cipher;
25 	return 1;
26 }
27 
block_cipher_set_decrypt_key(BLOCK_CIPHER_KEY * key,const BLOCK_CIPHER * cipher,const uint8_t * raw_key)28 int block_cipher_set_decrypt_key(BLOCK_CIPHER_KEY *key, const BLOCK_CIPHER *cipher, const uint8_t *raw_key)
29 {
30 	memset(key, 0, sizeof(BLOCK_CIPHER_KEY));
31 	cipher->set_decrypt_key(key, raw_key);
32 	key->cipher = cipher;
33 	return 1;
34 }
35 
block_cipher_encrypt(const BLOCK_CIPHER_KEY * key,const uint8_t * in,uint8_t * out)36 int block_cipher_encrypt(const BLOCK_CIPHER_KEY *key, const uint8_t *in, uint8_t *out)
37 {
38 	key->cipher->encrypt(key, in, out);
39 	return 1;
40 }
41 
block_cipher_decrypt(const BLOCK_CIPHER_KEY * key,const uint8_t * in,uint8_t * out)42 int block_cipher_decrypt(const BLOCK_CIPHER_KEY *key, const uint8_t *in, uint8_t *out)
43 {
44 	key->cipher->decrypt(key, in, out);
45 	return 1;
46 }
47 
48 static const BLOCK_CIPHER sm4_block_cipher_object = {
49 	OID_sm4,
50 	SM4_KEY_SIZE,
51 	SM4_BLOCK_SIZE,
52 	(block_cipher_set_encrypt_key_func)sm4_set_encrypt_key,
53 	(block_cipher_set_decrypt_key_func)sm4_set_decrypt_key,
54 	(block_cipher_encrypt_func)sm4_encrypt,
55 	(block_cipher_decrypt_func)sm4_encrypt,
56 };
57 
BLOCK_CIPHER_sm4(void)58 const BLOCK_CIPHER *BLOCK_CIPHER_sm4(void) {
59 	return &sm4_block_cipher_object;
60 }
61 
aes128_set_encrypt_key(AES_KEY * aes_key,const uint8_t key[16])62 static int aes128_set_encrypt_key(AES_KEY *aes_key, const uint8_t key[16]) {
63 	return aes_set_encrypt_key(aes_key, key, 16);
64 }
65 
aes128_set_decrypt_key(AES_KEY * aes_key,const uint8_t key[16])66 static int aes128_set_decrypt_key(AES_KEY *aes_key, const uint8_t key[16]) {
67 	return aes_set_decrypt_key(aes_key, key, 16);
68 }
69 
70 static const BLOCK_CIPHER aes128_block_cipher_object = {
71 	OID_aes128,
72 	AES128_KEY_SIZE,
73 	AES_BLOCK_SIZE,
74 	(block_cipher_set_encrypt_key_func)aes128_set_encrypt_key,
75 	(block_cipher_set_decrypt_key_func)aes128_set_decrypt_key,
76 	(block_cipher_encrypt_func)aes_encrypt,
77 	(block_cipher_decrypt_func)aes_encrypt,
78 };
79 
BLOCK_CIPHER_aes128(void)80 const BLOCK_CIPHER *BLOCK_CIPHER_aes128(void) {
81 	return &aes128_block_cipher_object;
82 }
83