-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget.c
215 lines (189 loc) · 5.73 KB
/
get.c
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
/* Copyright (c) 2018 Atmark Techno, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include "scp.h"
#include "i2c_a7.h"
#include "smComSCI2C.h"
#include "smCom.h"
#include "sm_printf.h"
#include "sm_errors.h"
#include "sm_apdu.h"
#include "apduComm.h"
#include "configCmd.h"
#include "axCliUtil.h"
#include "axHostCrypto.h"
#include "ax_api.h"
#include "ax_util.h"
#include "HLSETypes.h"
#include "tstHostCrypto.h"
void usage(){
printf("USAGE : \n./get pub -c <hex_value> -x <int> -k <keyfile.pem>\n");
return;
}
/**
* a7xConfigCmdGetPub - get public key from pub key or key pair and save it in PEM format to file
*/
int a7xConfigCmdGetPub(int index, int type, char *szFilename, U16 *sw) {
HLSE_RET_CODE nRet = AX_CLI_EXEC_FAILED;
eccKeyComponents_t eccKc;
// Initialize data structure
eccKc.bits = 256;
eccKc.curve = ECCCurve_NIST_P256;
eccKc.pubLen = sizeof(eccKc.pub);
eccKc.privLen = sizeof(eccKc.priv);
// Read public ECC key from card
switch (type)
{
case A71_KEY_PUB_PAIR:
*sw = A71_GetPublicKeyEccKeyPair((U8)index, eccKc.pub, &eccKc.pubLen);
if (*sw != SW_OK) { return nRet; }
break;
case A71_KEY_PUBLIC_KEY:
*sw = A71_GetEccPublicKey((U8)index, eccKc.pub, &eccKc.pubLen);
if (*sw != SW_OK) { return nRet; }
break;
default:
return nRet;
break;
}
printf("LEN=%d\n",eccKc.pubLen);
printf("HEX= ");
for(int i = 0; i <= eccKc.pubLen + 1; i++) {
printf("%02X ",eccKc.pub[i]);
}
printf("\n");
return AX_CLI_EXEC_OK;
}
/**
* Retrieves the ECC Public Key - from a key pair - from the storage location \p index into the provided buffer.
* The public key retrieved is in ANSI X9.62 uncompressed format (including the leading 0x04 byte).
*
* @param[in] index Storage index of the key pair
* @param[in,out] publicKey IN: buffer to contain public key byte array; OUT: public key
* @param[in,out] publicKeyLen IN: size of provided buffer; OUT: Length of the retrieved public key
* @retval ::SW_OK Upon successful execution
* @retval ::ERR_BUF_TOO_SMALL \p publicKey buffer is too small
*/
U16 A71_GetPublicKeyEccKeyPair(SST_Index_t index, U8 *publicKey, U16 *publicKeyLen)
{
U16 rv = 0;
apdu_t apdu;
apdu_t * pApdu = (apdu_t *) &apdu;
U8 isOk = 0x00;
if ( (publicKey == NULL) || (*publicKeyLen < 65) ) {return ERR_BUF_TOO_SMALL;}
pApdu->cla = A71CH_CLA;
pApdu->ins = A71CH_INS_GET_ECC_KEYPAIR;
pApdu->p1 = index;
pApdu->p2 = 0x00;
AllocateAPDUBuffer(pApdu);
SetApduHeader(pApdu, USE_STANDARD_APDU_LEN);
rv = smCom_Transceive(pApdu);
if (rv == SMCOM_OK)
{
rv = smGetSw(pApdu, &isOk);
if (isOk)
{
rv = smApduGetResponseBody(pApdu, publicKey, publicKeyLen);
}
}
FreeAPDUBuffer(pApdu);
return rv;
}
/**
* Retrieves the ECC Public Key from the storage location \p index into the provided buffer.
* The public key is in ANSI X9.62 uncompressed format (including the leading 0x04 byte).
* @param[in] index Storage index of the public key to be retrieved.
* @param[in,out] publicKey IN: buffer to contain public key byte array; OUT: public key
* @param[in,out] publicKeyLen IN: size of provided buffer; OUT: Length of the retrieved public key
* @retval ::SW_OK Upon successful execution
* @retval ::ERR_BUF_TOO_SMALL \p publicKey buffer is too small
*/
U16 A71_GetEccPublicKey(SST_Index_t index, U8 *publicKey, U16 *publicKeyLen)
{
U16 rv = 0;
apdu_t apdu;
apdu_t * pApdu = (apdu_t *) &apdu;
U8 isOk = 0x00;
if ( (publicKey == NULL) || (*publicKeyLen < 65) ) {return ERR_BUF_TOO_SMALL;}
pApdu->cla = A71CH_CLA;
pApdu->ins = A71CH_INS_GET_ECC_PUBLIC_KEY;
pApdu->p1 = index;
pApdu->p2 = 0x00;
AllocateAPDUBuffer(pApdu);
SetApduHeader(pApdu, USE_STANDARD_APDU_LEN);
rv = smCom_Transceive(pApdu);
if (rv == SMCOM_OK)
{
rv = smGetSw(pApdu, &isOk);
if (isOk)
{
rv = smApduGetResponseBody(pApdu, publicKey, publicKeyLen);
}
}
FreeAPDUBuffer(pApdu);
return rv;
}
int main(int argc, char **argv)
{
int type = 0x30;
int index = 0;
U16 sw = SW_OK;
if(8 != argc){
usage();
printf("argment error\n");
return -1;
}
type = atoi(argv[3]);
index = atoi(argv[5]);
char *szFilename = argv[7];
printf("filename=%s\ntype=%d\nindex=%d\n",szFilename,type,index);
if (type == 10){
type = 0x10;
} else if (type == 20){
type = 0x20;
}
if (type != 0x10 && type != 0x20) {
printf("type error\n");
usage();
return -1;
}
int ret = 0;
U8 atr[64];
U16 atrLen = sizeof(atr);
ret = axI2CInit();
if(I2C_OK != ret){
return -1;
}
sw = smComSCI2C_Open(ESTABLISH_SCI2C, 0x00, atr, &atrLen);
if(SW_OK != sw){
return sw;
}
ret = a7xConfigCmdGetPub(index,type,szFilename,&sw);
if(sw != SW_OK || AX_CLI_EXEC_OK != ret){
printf("error : sw=0x%x\n",sw);
usage();
}
return sw;
}