1 /*
2 * Copyright (c) 2014-2016 Alibaba Group. All rights reserved.
3 * License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
6 * not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include <string.h>
20
21 #include "mbedtls/md5.h"
22 #include "mbedtls/sha1.h"
23
24 #define KEY_IOPAD_SIZE 64
25 #define MD5_DIGEST_SIZE 16
26 #define SHA1_DIGEST_SIZE 20
27
iotx_digest_2_base16(char * out,unsigned char * digest,int n)28 void iotx_digest_2_base16(char *out, unsigned char *digest, int n)
29 {
30 static char const encode[] = "0123456789ABCDEF";
31 int j = 0;
32 int i = 0;
33 for (i = 0; i < n; i++) {
34 int a = digest[i];
35 out[j++] = encode[(a >> 4) & 0xf];
36 out[j++] = encode[a & 0xf];
37 }
38 }
39
iotx_hmac_md5(const char * msg,int msg_len,char * digest,const char * key,int key_len)40 void iotx_hmac_md5(const char *msg, int msg_len, char *digest, const char *key,
41 int key_len)
42 {
43 int index = 0;
44 mbedtls_md5_context ctx;
45 unsigned char
46 k_ipad[KEY_IOPAD_SIZE]; /* inner padding - key XORd with ipad */
47 unsigned char
48 k_opad[KEY_IOPAD_SIZE]; /* outer padding - key XORd with opad */
49 unsigned char out[MD5_DIGEST_SIZE];
50
51 memset(k_ipad, 0x36, sizeof(k_ipad));
52 memset(k_opad, 0x5C, sizeof(k_opad));
53 for (index = 0; index < key_len; index++) {
54 k_ipad[index] = (unsigned char)(k_ipad[index] ^ key[index]);
55 k_opad[index] = (unsigned char)(k_opad[index] ^ key[index]);
56 }
57
58 /* perform inner MD5 */
59 mbedtls_md5_init(&ctx);
60 mbedtls_md5_starts(&ctx);
61 mbedtls_md5_update(&ctx, k_ipad, KEY_IOPAD_SIZE);
62 mbedtls_md5_update(&ctx, (unsigned char *)msg, msg_len);
63 mbedtls_md5_finish(&ctx, out);
64
65 /* perform outer MD5 */
66 mbedtls_md5_init(&ctx);
67 mbedtls_md5_starts(&ctx);
68 mbedtls_md5_update(&ctx, k_opad, KEY_IOPAD_SIZE);
69 mbedtls_md5_update(&ctx, out, MD5_DIGEST_SIZE);
70 mbedtls_md5_finish(&ctx, out);
71
72 iotx_digest_2_base16(digest, out, 16);
73
74 mbedtls_md5_free(&ctx);
75 }
76
iotx_hmac_sha1(const char * msg,int msg_len,char * digest,const char * key,int key_len)77 void iotx_hmac_sha1(const char *msg, int msg_len, char *digest, const char *key,
78 int key_len)
79 {
80 int index = 0;
81 mbedtls_sha1_context ctx;
82 unsigned char
83 k_ipad[KEY_IOPAD_SIZE]; /* inner padding - key XORd with ipad */
84 unsigned char
85 k_opad[KEY_IOPAD_SIZE]; /* outer padding - key XORd with opad */
86 unsigned char out[SHA1_DIGEST_SIZE];
87
88 memset(k_ipad, 0x36, sizeof(k_ipad));
89 memset(k_opad, 0x5C, sizeof(k_opad));
90 for (index = 0; index < key_len; index++) {
91 k_ipad[index] = (unsigned char)(k_ipad[index] ^ key[index]);
92 k_opad[index] = (unsigned char)(k_opad[index] ^ key[index]);
93 }
94 /* perform inner sha1 */
95 mbedtls_sha1_init(&ctx);
96 mbedtls_sha1_starts(&ctx);
97 mbedtls_sha1_update(&ctx, k_ipad, KEY_IOPAD_SIZE);
98 mbedtls_sha1_update(&ctx, (unsigned char *)msg, msg_len);
99 mbedtls_sha1_finish(&ctx, out);
100
101 /* perform outer sha1 */
102 mbedtls_sha1_init(&ctx);
103 mbedtls_sha1_starts(&ctx);
104 mbedtls_sha1_update(&ctx, k_opad, KEY_IOPAD_SIZE);
105 mbedtls_sha1_update(&ctx, out, SHA1_DIGEST_SIZE);
106 mbedtls_sha1_finish(&ctx, out);
107
108 iotx_digest_2_base16(digest, out, 20);
109
110 mbedtls_sha1_free(&ctx);
111 }
112