Skip to content

Commit

Permalink
sha384, padding
Browse files Browse the repository at this point in the history
  • Loading branch information
miyako committed Dec 7, 2024
1 parent 1061aed commit a2425ea
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 80 deletions.
3 changes: 3 additions & 0 deletions hpki/Project/Sources/Classes/pkiForm.4dm
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Function onLoad()

Form:C1466.SHA1:=False:C215
Form:C1466.SHA256:=True:C214
Form:C1466.SHA384:=False:C215
Form:C1466.SHA512:=False:C215

Form:C1466.list()
Expand All @@ -31,6 +32,8 @@ Function get algorithm : Text
return "sha1"
: (Form:C1466.SHA512)
return "sha512"
: (Form:C1466.SHA384)
return "sha384"
Else
return "sha256"
End case
Expand Down
107 changes: 61 additions & 46 deletions hpki/Project/Sources/Forms/hpki/form.4DForm
Original file line number Diff line number Diff line change
Expand Up @@ -354,51 +354,6 @@
"sizingY": "fixed",
"sizingX": "fixed"
},
"SHA1": {
"type": "radio",
"text": "SHA1",
"top": 432,
"left": 329,
"width": 71,
"height": 25,
"events": [
"onClick"
],
"dataSourceTypeHint": "boolean",
"dataSource": "Form:C1466.SHA1",
"radioGroup": "SHA",
"focusable": false
},
"SHA2": {
"type": "radio",
"text": "SHA256",
"top": 432,
"left": 409,
"width": 91,
"height": 25,
"events": [
"onClick"
],
"dataSourceTypeHint": "boolean",
"dataSource": "Form:C1466.SHA256",
"radioGroup": "SHA",
"focusable": false
},
"SHA5": {
"type": "radio",
"text": "SHA512",
"top": 432,
"left": 509,
"width": 91,
"height": 25,
"events": [
"onClick"
],
"dataSourceTypeHint": "boolean",
"dataSource": "Form:C1466.SHA512",
"radioGroup": "SHA",
"focusable": false
},
"digestInfo": {
"type": "input",
"left": 107,
Expand Down Expand Up @@ -455,6 +410,66 @@
"enterable": false,
"sizingX": "grow",
"sizingY": "grow"
},
"SHA1": {
"type": "radio",
"text": "SHA1",
"top": 432,
"left": 259,
"width": 80,
"height": 25,
"events": [
"onClick"
],
"dataSourceTypeHint": "boolean",
"dataSource": "Form:C1466.SHA1",
"radioGroup": "SHA",
"focusable": false
},
"SHA2": {
"type": "radio",
"text": "SHA256",
"top": 432,
"left": 346,
"width": 80,
"height": 25,
"events": [
"onClick"
],
"dataSourceTypeHint": "boolean",
"dataSource": "Form:C1466.SHA256",
"radioGroup": "SHA",
"focusable": false
},
"SHA5": {
"type": "radio",
"text": "SHA512",
"top": 432,
"left": 520,
"width": 80,
"height": 25,
"events": [
"onClick"
],
"dataSourceTypeHint": "boolean",
"dataSource": "Form:C1466.SHA512",
"radioGroup": "SHA",
"focusable": false
},
"SHA3": {
"type": "radio",
"text": "SHA384",
"top": 432,
"left": 433,
"width": 80,
"height": 25,
"events": [
"onClick"
],
"dataSourceTypeHint": "boolean",
"dataSource": "Form:C1466.SHA384",
"radioGroup": "SHA",
"focusable": false
}
}
},
Expand Down Expand Up @@ -512,7 +527,7 @@
}
],
"method": "method.4dm",
"geometryStamp": 1611,
"geometryStamp": 1615,
"editor": {
"activeView": "View 1",
"defaultView": "View 1",
Expand Down
29 changes: 22 additions & 7 deletions hpki/Project/Sources/Forms/jpki/form.4DForm
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,8 @@
"type": "radio",
"text": "SHA1",
"top": 432,
"left": 329,
"width": 71,
"left": 259,
"width": 80,
"height": 25,
"events": [
"onClick"
Expand All @@ -537,8 +537,8 @@
"type": "radio",
"text": "SHA256",
"top": 432,
"left": 409,
"width": 91,
"left": 346,
"width": 80,
"height": 25,
"events": [
"onClick"
Expand All @@ -552,8 +552,8 @@
"type": "radio",
"text": "SHA512",
"top": 432,
"left": 509,
"width": 91,
"left": 520,
"width": 80,
"height": 25,
"events": [
"onClick"
Expand Down Expand Up @@ -590,6 +590,21 @@
"enterable": false,
"sizingX": "grow",
"sizingY": "grow"
},
"SHA3": {
"type": "radio",
"text": "SHA384",
"top": 432,
"left": 433,
"width": 80,
"height": 25,
"events": [
"onClick"
],
"dataSourceTypeHint": "boolean",
"dataSource": "Form:C1466.SHA384",
"radioGroup": "SHA",
"focusable": false
}
}
},
Expand Down Expand Up @@ -647,7 +662,7 @@
}
],
"method": "method.4dm",
"geometryStamp": 1678,
"geometryStamp": 1739,
"editor": {
"activeView": "View 1",
"defaultView": "View 1",
Expand Down
Binary file modified hpki/Resources/bin/macOS/hpki
Binary file not shown.
81 changes: 54 additions & 27 deletions include/My-Number-Card_macOS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,42 +199,53 @@ static void _apdu_compute_digital_signature_hpki(dispatch_semaphore_t sem,
TKSmartCard *smartCard,
Json::Value& threadCtx) {

hash_algorithm algorithm = (hash_algorithm)threadCtx["algorithm"].asInt();
size_t APDU_size;
switch (algorithm) {
case hash_algorithm_sha512:
APDU_size = sizeof(APDU_COMPUTE_DIGITAL_SIGNATURE_KEY_JPKI);
break;
case hash_algorithm_sha384:
APDU_size = sizeof(APDU_COMPUTE_DIGITAL_SIGNATURE_KEY_JPKI) - 0x10;
break;
case hash_algorithm_sha1:
APDU_size = sizeof(APDU_COMPUTE_DIGITAL_SIGNATURE_KEY_JPKI) - 0x30;
break;
default:
APDU_size = sizeof(APDU_COMPUTE_DIGITAL_SIGNATURE_KEY_JPKI) - 0x20;
break;
}
size_t key_length_bits = 2048;
size_t key_length = key_length_bits / 8;
std::vector<uint8_t>pkcs(key_length);

std::vector<uint8_t>data(sizeof(APDU_COMPUTE_DIGITAL_SIGNATURE_KEY_JPKI));
memcpy(&data[0],
APDU_COMPUTE_DIGITAL_SIGNATURE_KEY_JPKI,
sizeof(APDU_COMPUTE_DIGITAL_SIGNATURE_KEY_JPKI));


//key length=2048; signature length=256

pkcs[0] = 0x00;
pkcs[1] = 0x01;

//padding string
memset(&pkcs[2],
0xFF,
pkcs.size() - 2);

std::string digestInfo = threadCtx["digestInfo"].asString();
std::vector<uint8_t>buf(0);
hex_to_bytes(digestInfo, buf);

data[0] = 0x00;//0x80
data[1] = 0x2A;//0x2A
data[2] = 0x9E;//0x9E
data[3] = 0x9A;//0x9A
data[4] = buf.size();
size_t p = pkcs.size() - buf.size();
memcpy(&pkcs[p], &buf[0], buf.size());
pkcs[p - 1] = 0x00;

memcpy(&data[5], &buf[0], buf.size());
std::vector<uint8_t>data(7 + pkcs.size()+ 2);

data[0] = 0x00;
data[1] = 0x2A;
data[2] = 0x9E;
data[3] = 0x9A;

data[4] = 0x00;
data[5] = key_length >> 8;
data[6] = key_length & 0x00FF;

memcpy(&data[7], &pkcs[0], pkcs.size());

data[data.size()-2] = 0x00;
data[data.size()-1] = 0x00;

std::string apdu;
bytes_to_hex(&data[0], data.size(), apdu);
threadCtx["apdu_pkcs"] = apdu;

[smartCard
transmitRequest:[NSData dataWithBytes:&data[0]
length:APDU_size]
length:data.size()]
reply:^(NSData *response, NSError *error) {
if ((error == nil) && (_is_response_positive(response, threadCtx))) {
size_t signature_length = [response length] -2;
Expand Down Expand Up @@ -715,6 +726,22 @@ static void _read_block_cert_jpki(dispatch_semaphore_t sem,
ASN1_TIME *notBefore = X509_get_notBefore(x509);
ASN1_TIME *notAfter = X509_get_notAfter(x509);

EVP_PKEY *pubkey = X509_get0_pubkey(x509);
if(pubkey) {
const RSA *rsa = EVP_PKEY_get0_RSA(pubkey);
if(rsa) {
BIGNUM *modulus = NULL;
BIGNUM *public_exponent = NULL;
RSA_get0_key(rsa,
(const BIGNUM **)&modulus,
(const BIGNUM **)&public_exponent,
NULL);
if (modulus != NULL) {
certificate["length"] = BN_num_bits(modulus);
}
}
}

std::string textValue;
asn_time_to_iso(notBefore, textValue);
certificate["notBefore"] = textValue;
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,38 @@
uuid = "FE0331A1-10C6-484E-8433-F305536C871E"
type = "1"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "215556E1-2944-41C0-906B-E94AA14B140D"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "../include/My-Number-Card_macOS.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "198"
endingLineNumber = "198"
landmarkName = "_apdu_compute_digital_signature_hpki(sem, smartCard, threadCtx)"
landmarkType = "9">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "B3F9944E-C13A-4E80-BB0E-34ADCF77E0A0"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "../include/My-Number-Card_macOS.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "491"
endingLineNumber = "491"
landmarkName = "_apdu_select_hpki_key(sem, smartCard, threadCtx)"
landmarkType = "9">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
1 change: 1 addition & 0 deletions macOS/hpki/hpki.m
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ int hpki(int argc, char *argv[]) {

bytes_to_hex(&digestInfo[0], digestInfo.size(), hex);

threadCtx["hi"] = "hi";
threadCtx["digestInfo"] = hex;
threadCtx["algorithm"] = algorithm;
}
Expand Down

0 comments on commit a2425ea

Please sign in to comment.