summaryrefslogtreecommitdiff
path: root/common/cmd_sha2.c
blob: 7cdab2010985a4a7a53221bd2cc89268d5c330b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
 * Copyright (C) 2014-2017 Amlogic, Inc. All rights reserved.
 *
 * All information contained herein is Amlogic confidential.
 *
 * This software is provided to you pursuant to Software License Agreement
 * (SLA) with Amlogic Inc ("Amlogic"). This software may be used
 * only in accordance with the terms of this agreement.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification is strictly prohibited without prior written permission from
 * Amlogic.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
/*
 * Functions in this file implement Amlogic SHA2 function
 *
 * Author : zhongfu.luo@amlogic.com
 *
 * Feature: implement uboot command for SHA2 common usage
 *          command : sha2 addr_in len [addr_out]
 *                         addr_in[IN]  : DDR address for input buffer
 *                         len[IN]      : total data size to be processed for SHA2
 *                         addr_out[OUT]: user defined output buffer for the SHA2 (if not set then the SHA2 of input will shown only but not stored)
 *
 * History: 1. 2017.06.01 function init
 *
 *
*/

#include <common.h>
#include <malloc.h>
#include <asm/arch/regs.h>
#include <u-boot/sha256.h>

#define DATA_MAX_LEN    (100<<20) //max length of SHA2 is 100MB
static int do_sha2(cmd_tbl_t *cmdtp, int flag, int argc,
			char * const argv[])
{
	int nReturn=CMD_RET_USAGE;
	ulong addr_in,nLength,addr_out=0;
	unsigned char szSHA2[32];
	unsigned char *pSHA2 = szSHA2;
	int nSHA2Type = 256;
	char *endp;
	int i;

	/* need at least three arguments */
	if (argc < 3)
		goto exit;

	nReturn = __LINE__;

	if ( 0 == *argv[1] )
	{
		printf("[addr_in] format error! \n" );
		goto exit;
	}
	addr_in = simple_strtoul(argv[1], &endp, 16);
	if ( 0 != *endp )
	{
		printf("[addr_in] format error! \n" );
		goto exit;
	}
	nReturn = __LINE__;

	if ( 0 == *argv[2])
	{
		printf("[Length] format error! \n" );
		goto exit;
	}
	nLength = simple_strtoul(argv[2], &endp, 16);
	if (  0 != *endp )
	{
		printf("[Length] format error! \n" );
		goto exit;
	}
	if ( !nLength || nLength > DATA_MAX_LEN)
		{
			printf("Length range: 0x00000001 ~ 0x%08x Byte! \n", DATA_MAX_LEN );
			goto exit;
		}

	nReturn = __LINE__;



	if (argc > 3)
	{
		if ( 0 == *argv[3] )
		{
			printf("[addr_out] format error! \n" );
			goto exit;
		}
		addr_out = simple_strtoul(argv[3], &endp, 16);
		if ( 0 != *endp )
		{
			printf("[addr_out] format error! \n" );
			goto exit;
		}
		pSHA2=(unsigned char *)addr_out;
	}


	sha256_csum_wd((unsigned char *)addr_in, nLength,pSHA2,0);

	if (argc > 3)
	printf("\nSHA%d of addr_in: 0x%08x, len: 0x%08x, addr_out: 0x%08x \n", nSHA2Type, (unsigned int)addr_in, (unsigned int)nLength,(unsigned int)addr_out);
	else
	printf("\nSHA%d of addr_in: 0x%08x, len: 0x%08x \n", nSHA2Type, (unsigned int)addr_in, (unsigned int)nLength);


	for (i=0; i<SHA256_SUM_LEN; i++)
		printf("%02x%s", pSHA2[i], ((i+1) % 16==0) ? "\n" :" ");

	nReturn = 0;

exit:

	return nReturn;
}

#undef DATA_MAX_LEN

U_BOOT_CMD(
	sha2, 4,	1,	do_sha2,
	"SHA2 command",
	"addr_in len [addr_out] \n"
);



/*
 * Feature: implement uboot command for test SHA2 common usage
 *          command : sha2test [writeval] [len]
 *                         writeval[IN]  : the value of data for test(default is 0x01)
 *                         len[IN]      : total data size to be processed for SHA2 (default is 128k bytes)
 *
 * History: 1. 2017.06.01 function init
 *
 *
*/

//#define CONFIG_AML_SHA2_TEST
#if defined(CONFIG_AML_SHA2_TEST)

#define writel(val,reg) (*((volatile unsigned *)(reg)))=(val)
#define readl(reg)		(*((volatile unsigned *)(reg)))
#define TEST_MAX_LEN    (10<<20)
#define TEST_MIN_LEN    (64)

static int do_sha2test(cmd_tbl_t *cmdtp, int flag, int argc,
			char * const argv[])
{

	int nReturn = __LINE__;
	unsigned char *pBuffer = 0;

	int i;
	int nLength = TEST_MAX_LEN;
	unsigned int byPattern = 1;
	char *endp;
	int nSHA2Type = 256;
	unsigned int ntime1,ntime2,ntime;
	unsigned char szSHA2[32];
	int testLength[8]={TEST_MAX_LEN,1024*1024,100*1024,10*1024,1024,512,128,TEST_MIN_LEN};
	int c=0;

	#ifdef CONFIG_AML_HW_SHA2
		printf("aml log : Amlogic HW SHA2 \n");
	#else
		printf("aml log : Software SHA2 \n");
	#endif


	//pattern
	if (argc > 1)
	{
		if ( 0 == *argv[1] )
		{
			printf("[writeval] format error! \n" );
			goto exit;
		}
		byPattern = simple_strtoul(argv[1], &endp, 16);
		if ( 0 != *endp )
		{
			printf("[writeval] format error! \n" );
			goto exit;
		}
		if (  byPattern > 0xff)
		{
			printf("writeval range: 0x00 ~ 0xff! \n" );
			goto exit;
		}

	}

	nReturn = __LINE__;

	//length
	if (argc > 2)
	{
		if ( 0== *argv[2] )
		{
			printf("[Length] format error! \n" );
			goto exit;
		}
		nLength = simple_strtoul(argv[2], &endp, 16);
		if ( 0 != *endp )
		{
			printf("[Length] format error! \n" );
			goto exit;
		}

		if ( !nLength || nLength > TEST_MAX_LEN)
		{
			printf("Length range: 0x00000001 ~ 0x%08x Byte! \n", TEST_MAX_LEN );
			goto exit;
		}
	}

	nReturn = __LINE__;

	pBuffer=(unsigned char*)malloc(nLength);

	if (!pBuffer)
		{
		printf("malloc fail! \n" );
		goto exit;
	}

	//set buffer with dedicated pattern
	memset(pBuffer,byPattern,nLength);


	do
	{
		ntime1=readl(P_ISA_TIMERE);
		sha256_csum_wd(pBuffer, nLength,szSHA2,0 );
		ntime2=readl(P_ISA_TIMERE);

		ntime = ntime2 - ntime1;

		printf("\nSHA%d of value: 0x%02x, len: 0x%08x, time used: %d us, bandwidth: %d kB/s \n",\
			nSHA2Type, (unsigned int)byPattern, nLength,ntime, (unsigned int)((float)nLength/1024/ntime*1000000));

		//SHA2 dump
		for (i=0; i<SHA256_SUM_LEN; i++)
			printf("%02x%s", szSHA2[i], ((i+1) % 16==0) ? "\n" :" ");

		if (argc > 2)
			break;

		nLength =testLength[++c];
	}
	while (nLength > TEST_MIN_LEN) ;

	nReturn = 0;

exit:

	if (pBuffer)
	{
		free(pBuffer);
		pBuffer = 0;
	}

	return nReturn;

}

#undef writel
#undef readl
#undef TEST_MAX_LEN
#undef TEST_MIN_LEN

U_BOOT_CMD(
	sha2test, 3,	1,	do_sha2test,
	"test SHA2 fuction",
	"[writeval] [len] \n"
);

#endif //#if defined(CONFIG_AML_SHA2_TEST)