1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "smp_aes_encryption.h"
16 
17 #include <string.h>
18 
19 #include "log.h"
20 #include "openssl/aes.h"
21 
22 #include "smp.h"
23 
SMP_ReverseData(const uint8_t * intput,uint8_t * output,int size)24 static void SMP_ReverseData(const uint8_t *intput, uint8_t *output, int size)
25 {
26     for (int i = 0x00; i < size; i++) {
27         output[i] = intput[size - 0x01 - i];
28     }
29 }
30 
SMP_Aes128Internal(const uint8_t key[AES_BLOCK_SIZE],const uint8_t in[AES_BLOCK_SIZE],uint8_t out[AES_BLOCK_SIZE])31 static int SMP_Aes128Internal(
32     const uint8_t key[AES_BLOCK_SIZE], const uint8_t in[AES_BLOCK_SIZE], uint8_t out[AES_BLOCK_SIZE])
33 {
34     if ((in == NULL) || (key == NULL) || (out == NULL)) {
35         return -1;
36     }
37 
38     uint8_t inReverse[AES_BLOCK_SIZE];
39     uint8_t keyReverse[AES_BLOCK_SIZE];
40     uint8_t outReverse[AES_BLOCK_SIZE];
41     AES_KEY aesKey;
42 
43     SMP_ReverseData(key, keyReverse, sizeof(keyReverse));
44     SMP_ReverseData(in, inReverse, sizeof(inReverse));
45 
46     AES_set_encrypt_key(keyReverse, 0x80, &aesKey);
47     AES_encrypt(inReverse, outReverse, &aesKey);
48 
49     SMP_ReverseData(outReverse, out, sizeof(outReverse));
50     (void)memset_s(keyReverse, sizeof(keyReverse), 0x00, sizeof(keyReverse));
51 
52     return 0;
53 }
54 
SMP_Aes128(const uint8_t * key,const uint8_t keyLen,const uint8_t * in,const uint8_t inLen,uint8_t out[AES_BLOCK_SIZE])55 int SMP_Aes128(
56     const uint8_t *key, const uint8_t keyLen, const uint8_t *in, const uint8_t inLen, uint8_t out[AES_BLOCK_SIZE])
57 {
58     if ((key == NULL) || (in == NULL) || (out == NULL)) {
59         return -1;
60     }
61 
62     uint8_t input[AES_BLOCK_SIZE] = {0x00};
63     if (inLen > AES_BLOCK_SIZE) {
64         LOG_WARN("aes_128 inLen longer than AES_BLOCK_SIZE(16).");
65         (void)memcpy_s(input, AES_BLOCK_SIZE, in, AES_BLOCK_SIZE);
66     } else {
67         (void)memcpy_s(input, AES_BLOCK_SIZE, in, inLen);
68     }
69 
70     uint8_t keyInput[AES_BLOCK_SIZE] = {0x00};
71     if (keyLen > AES_BLOCK_SIZE) {
72         LOG_WARN("aes_128 keyLen longer than AES_BLOCK_SIZE(16).");
73         (void)memcpy_s(keyInput, AES_BLOCK_SIZE, key, AES_BLOCK_SIZE);
74     } else {
75         (void)memcpy_s(keyInput, AES_BLOCK_SIZE, key, keyLen);
76     }
77 
78     return SMP_Aes128Internal(&keyInput[0], &input[0], &out[0]);
79 }