mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-08-22 14:23:50 -07:00
remove spurious spaces & tabs at end of lines
This commit is contained in:
parent
edc19f202a
commit
60f292b18e
249 changed files with 8481 additions and 8481 deletions
|
@ -30,7 +30,7 @@ int aes_encode(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *output, int l
|
|||
uint8_t iiv[16] = {0};
|
||||
if (iv)
|
||||
memcpy(iiv, iv, 16);
|
||||
|
||||
|
||||
mbedtls_aes_context aes;
|
||||
mbedtls_aes_init(&aes);
|
||||
if (mbedtls_aes_setkey_enc(&aes, key, 128))
|
||||
|
@ -46,7 +46,7 @@ int aes_decode(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *output, int l
|
|||
uint8_t iiv[16] = {0};
|
||||
if (iv)
|
||||
memcpy(iiv, iv, 16);
|
||||
|
||||
|
||||
mbedtls_aes_context aes;
|
||||
mbedtls_aes_init(&aes);
|
||||
if (mbedtls_aes_setkey_dec(&aes, key, 128))
|
||||
|
@ -62,20 +62,20 @@ int aes_decode(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *output, int l
|
|||
// https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/AES_CMAC.pdf
|
||||
int aes_cmac(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *mac, int length) {
|
||||
memset(mac, 0x00, 16);
|
||||
|
||||
// NIST 800-38B
|
||||
|
||||
// NIST 800-38B
|
||||
return mbedtls_aes_cmac_prf_128(key, MBEDTLS_AES_BLOCK_SIZE, input, length, mac);
|
||||
}
|
||||
|
||||
int aes_cmac8(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *mac, int length) {
|
||||
uint8_t cmac[16] = {0};
|
||||
memset(mac, 0x00, 8);
|
||||
|
||||
|
||||
int res = aes_cmac(iv, key, input, cmac, length);
|
||||
if (res)
|
||||
return res;
|
||||
|
||||
for(int i = 0; i < 8; i++)
|
||||
|
||||
for(int i = 0; i < 8; i++)
|
||||
mac[i] = cmac[i * 2 + 1];
|
||||
|
||||
return 0;
|
||||
|
@ -88,73 +88,73 @@ static int fixed_rand(void *rng_state, unsigned char *output, size_t len) {
|
|||
} else {
|
||||
memset(output, 0x00, len);
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sha256hash(uint8_t *input, int length, uint8_t *hash) {
|
||||
if (!hash || !input)
|
||||
return 1;
|
||||
|
||||
|
||||
mbedtls_sha256_context sctx;
|
||||
mbedtls_sha256_init(&sctx);
|
||||
mbedtls_sha256_starts(&sctx, 0); // SHA-256, not 224
|
||||
mbedtls_sha256_starts(&sctx, 0); // SHA-256, not 224
|
||||
mbedtls_sha256_update(&sctx, input, length);
|
||||
mbedtls_sha256_finish(&sctx, hash);
|
||||
mbedtls_sha256_finish(&sctx, hash);
|
||||
mbedtls_sha256_free(&sctx);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ecdsa_init_str(mbedtls_ecdsa_context *ctx, char * key_d, char *key_x, char *key_y) {
|
||||
if (!ctx)
|
||||
return 1;
|
||||
|
||||
|
||||
int res;
|
||||
|
||||
mbedtls_ecdsa_init(ctx);
|
||||
res = mbedtls_ecp_group_load(&ctx->grp, MBEDTLS_ECP_DP_SECP256R1); // secp256r1
|
||||
if (res)
|
||||
if (res)
|
||||
return res;
|
||||
|
||||
|
||||
if (key_d) {
|
||||
res = mbedtls_mpi_read_string(&ctx->d, 16, key_d);
|
||||
if (res)
|
||||
if (res)
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
if (key_x && key_y) {
|
||||
res = mbedtls_ecp_point_read_string(&ctx->Q, 16, key_x, key_y);
|
||||
if (res)
|
||||
if (res)
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ecdsa_init(mbedtls_ecdsa_context *ctx, uint8_t * key_d, uint8_t *key_xy) {
|
||||
if (!ctx)
|
||||
return 1;
|
||||
|
||||
|
||||
int res;
|
||||
|
||||
mbedtls_ecdsa_init(ctx);
|
||||
res = mbedtls_ecp_group_load(&ctx->grp, MBEDTLS_ECP_DP_SECP256R1); // secp256r1
|
||||
if (res)
|
||||
if (res)
|
||||
return res;
|
||||
|
||||
|
||||
if (key_d) {
|
||||
res = mbedtls_mpi_read_binary(&ctx->d, key_d, 32);
|
||||
if (res)
|
||||
if (res)
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
if (key_xy) {
|
||||
res = mbedtls_ecp_point_read_binary(&ctx->grp, &ctx->Q, key_xy, 32 * 2 + 1);
|
||||
if (res)
|
||||
if (res)
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -188,7 +188,7 @@ int ecdsa_key_create(uint8_t * key_d, uint8_t *key_xy) {
|
|||
res = mbedtls_ecp_point_write_binary(&ctx.grp, &ctx.Q, MBEDTLS_ECP_PF_UNCOMPRESSED, &keylen, public_key, sizeof(public_key));
|
||||
if (res)
|
||||
goto exit;
|
||||
|
||||
|
||||
if (keylen != 65) { // 0x04 <key x 32b><key y 32b>
|
||||
res = 1;
|
||||
goto exit;
|
||||
|
@ -212,20 +212,20 @@ char *ecdsa_get_error(int ret) {
|
|||
int ecdsa_public_key_from_pk(mbedtls_pk_context *pk, uint8_t *key, size_t keylen) {
|
||||
int res = 0;
|
||||
size_t realkeylen = 0;
|
||||
if (keylen < 65)
|
||||
if (keylen < 65)
|
||||
return 1;
|
||||
|
||||
|
||||
mbedtls_ecdsa_context ctx;
|
||||
mbedtls_ecdsa_init(&ctx);
|
||||
|
||||
|
||||
res = mbedtls_ecp_group_load(&ctx.grp, MBEDTLS_ECP_DP_SECP256R1); // secp256r1
|
||||
if (res)
|
||||
goto exit;
|
||||
|
||||
|
||||
res = mbedtls_ecdsa_from_keypair(&ctx, mbedtls_pk_ec(*pk) );
|
||||
if (res)
|
||||
goto exit;
|
||||
|
||||
|
||||
res = mbedtls_ecp_point_write_binary(&ctx.grp, &ctx.Q, MBEDTLS_ECP_PF_UNCOMPRESSED, &realkeylen, key, keylen);
|
||||
if (realkeylen != 65)
|
||||
res = 2;
|
||||
|
@ -237,8 +237,8 @@ exit:
|
|||
int ecdsa_signature_create(uint8_t *key_d, uint8_t *key_xy, uint8_t *input, int length, uint8_t *signature, size_t *signaturelen) {
|
||||
int res;
|
||||
*signaturelen = 0;
|
||||
|
||||
uint8_t shahash[32] = {0};
|
||||
|
||||
uint8_t shahash[32] = {0};
|
||||
res = sha256hash(input, length, shahash);
|
||||
if (res)
|
||||
return res;
|
||||
|
@ -254,10 +254,10 @@ int ecdsa_signature_create(uint8_t *key_d, uint8_t *key_xy, uint8_t *input, int
|
|||
if (res)
|
||||
goto exit;
|
||||
|
||||
mbedtls_ecdsa_context ctx;
|
||||
mbedtls_ecdsa_context ctx;
|
||||
ecdsa_init(&ctx, key_d, key_xy);
|
||||
res = mbedtls_ecdsa_write_signature(&ctx, MBEDTLS_MD_SHA256, shahash, sizeof(shahash), signature, signaturelen, mbedtls_ctr_drbg_random, &ctr_drbg);
|
||||
|
||||
|
||||
exit:
|
||||
mbedtls_ctr_drbg_free(&ctr_drbg);
|
||||
mbedtls_ecdsa_free(&ctx);
|
||||
|
@ -267,49 +267,49 @@ exit:
|
|||
int ecdsa_signature_create_test(char * key_d, char *key_x, char *key_y, char *random, uint8_t *input, int length, uint8_t *signature, size_t *signaturelen) {
|
||||
int res;
|
||||
*signaturelen = 0;
|
||||
|
||||
uint8_t shahash[32] = {0};
|
||||
|
||||
uint8_t shahash[32] = {0};
|
||||
res = sha256hash(input, length, shahash);
|
||||
if (res)
|
||||
return res;
|
||||
|
||||
int rndlen = 0;
|
||||
param_gethex_to_eol(random, 0, fixed_rand_value, sizeof(fixed_rand_value), &rndlen);
|
||||
|
||||
mbedtls_ecdsa_context ctx;
|
||||
|
||||
mbedtls_ecdsa_context ctx;
|
||||
ecdsa_init_str(&ctx, key_d, key_x, key_y);
|
||||
res = mbedtls_ecdsa_write_signature(&ctx, MBEDTLS_MD_SHA256, shahash, sizeof(shahash), signature, signaturelen, fixed_rand, NULL);
|
||||
|
||||
|
||||
mbedtls_ecdsa_free(&ctx);
|
||||
return res;
|
||||
}
|
||||
|
||||
int ecdsa_signature_verify_keystr(char *key_x, char *key_y, uint8_t *input, int length, uint8_t *signature, size_t signaturelen) {
|
||||
int res;
|
||||
uint8_t shahash[32] = {0};
|
||||
uint8_t shahash[32] = {0};
|
||||
res = sha256hash(input, length, shahash);
|
||||
if (res)
|
||||
return res;
|
||||
|
||||
mbedtls_ecdsa_context ctx;
|
||||
mbedtls_ecdsa_context ctx;
|
||||
ecdsa_init_str(&ctx, NULL, key_x, key_y);
|
||||
res = mbedtls_ecdsa_read_signature(&ctx, shahash, sizeof(shahash), signature, signaturelen);
|
||||
|
||||
|
||||
mbedtls_ecdsa_free(&ctx);
|
||||
return res;
|
||||
}
|
||||
|
||||
int ecdsa_signature_verify(uint8_t *key_xy, uint8_t *input, int length, uint8_t *signature, size_t signaturelen) {
|
||||
int res;
|
||||
uint8_t shahash[32] = {0};
|
||||
uint8_t shahash[32] = {0};
|
||||
res = sha256hash(input, length, shahash);
|
||||
if (res)
|
||||
return res;
|
||||
|
||||
mbedtls_ecdsa_context ctx;
|
||||
mbedtls_ecdsa_context ctx;
|
||||
ecdsa_init(&ctx, NULL, key_xy);
|
||||
res = mbedtls_ecdsa_read_signature(&ctx, shahash, sizeof(shahash), signature, signaturelen);
|
||||
|
||||
|
||||
mbedtls_ecdsa_free(&ctx);
|
||||
return res;
|
||||
}
|
||||
|
@ -325,8 +325,8 @@ int ecdsa_nist_test(bool verbose) {
|
|||
int res;
|
||||
uint8_t input[] = "Example of ECDSA with P-256";
|
||||
int length = strlen((char *)input);
|
||||
uint8_t signature[300] = {0};
|
||||
size_t siglen = 0;
|
||||
uint8_t signature[300] = {0};
|
||||
size_t siglen = 0;
|
||||
|
||||
// NIST ecdsa test
|
||||
if (verbose)
|
||||
|
@ -334,32 +334,32 @@ int ecdsa_nist_test(bool verbose) {
|
|||
// make signature
|
||||
res = ecdsa_signature_create_test(T_PRIVATE_KEY, T_Q_X, T_Q_Y, T_K, input, length, signature, &siglen);
|
||||
// printf("res: %x signature[%x]: %s\n", (res<0)?-res:res, siglen, sprint_hex(signature, siglen));
|
||||
if (res)
|
||||
if (res)
|
||||
goto exit;
|
||||
|
||||
// check vectors
|
||||
uint8_t rval[300] = {0};
|
||||
uint8_t sval[300] = {0};
|
||||
uint8_t rval[300] = {0};
|
||||
uint8_t sval[300] = {0};
|
||||
res = ecdsa_asn1_get_signature(signature, siglen, rval, sval);
|
||||
if (res)
|
||||
goto exit;
|
||||
|
||||
|
||||
int slen = 0;
|
||||
uint8_t rval_s[33] = {0};
|
||||
param_gethex_to_eol(T_R, 0, rval_s, sizeof(rval_s), &slen);
|
||||
uint8_t sval_s[33] = {0};
|
||||
uint8_t sval_s[33] = {0};
|
||||
param_gethex_to_eol(T_S, 0, sval_s, sizeof(sval_s), &slen);
|
||||
if (strncmp((char *)rval, (char *)rval_s, 32) || strncmp((char *)sval, (char *)sval_s, 32)) {
|
||||
printf("R or S check error\n");
|
||||
res = 100;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
||||
// verify signature
|
||||
res = ecdsa_signature_verify_keystr(T_Q_X, T_Q_Y, input, length, signature, siglen);
|
||||
if (res)
|
||||
if (res)
|
||||
goto exit;
|
||||
|
||||
|
||||
// verify wrong signature
|
||||
input[0] ^= 0xFF;
|
||||
res = ecdsa_signature_verify_keystr(T_Q_X, T_Q_Y, input, length, signature, siglen);
|
||||
|
@ -378,27 +378,27 @@ int ecdsa_nist_test(bool verbose) {
|
|||
uint8_t key_xy[32 * 2 + 2] = {0};
|
||||
memset(signature, 0x00, sizeof(signature));
|
||||
siglen = 0;
|
||||
|
||||
|
||||
res = ecdsa_key_create(key_d, key_xy);
|
||||
if (res)
|
||||
if (res)
|
||||
goto exit;
|
||||
|
||||
res = ecdsa_signature_create(key_d, key_xy, input, length, signature, &siglen);
|
||||
if (res)
|
||||
if (res)
|
||||
goto exit;
|
||||
|
||||
res = ecdsa_signature_verify(key_xy, input, length, signature, siglen);
|
||||
if (res)
|
||||
if (res)
|
||||
goto exit;
|
||||
|
||||
input[0] ^= 0xFF;
|
||||
res = ecdsa_signature_verify(key_xy, input, length, signature, siglen);
|
||||
if (!res)
|
||||
if (!res)
|
||||
goto exit;
|
||||
|
||||
|
||||
if (verbose)
|
||||
printf("passed\n\n");
|
||||
|
||||
|
||||
return 0;
|
||||
exit:
|
||||
if (verbose)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue