mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-08-21 22:03:42 -07:00
Implement ANSI X9.63 sha256 per spec
This commit is contained in:
parent
44f2e253d8
commit
610b8a7410
2 changed files with 36 additions and 0 deletions
|
@ -18,6 +18,7 @@
|
|||
|
||||
#include "crypto/libpcrypto.h"
|
||||
#include "crypto/asn1utils.h"
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
|
@ -34,6 +35,7 @@
|
|||
#include <mbedtls/entropy.h>
|
||||
#include <mbedtls/error.h>
|
||||
#include <mbedtls/blowfish.h>
|
||||
#include "libpcrypto.h"
|
||||
#include "util.h"
|
||||
#include "ui.h"
|
||||
|
||||
|
@ -632,3 +634,34 @@ int blowfish_decrypt(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *output,
|
|||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Implementation from http://www.secg.org/sec1-v2.pdf#subsubsection.3.6.1
|
||||
int ansi_x963_sha256(uint8_t *sharedSecret, size_t sharedSecretLen, uint8_t *sharedInfo, size_t sharedInfoLen, size_t keyDataLen, uint8_t *keyData) {
|
||||
// sha256 hash has (practically) no max input len, so skipping that step
|
||||
|
||||
if (keyDataLen >= 32 * (pow(2, 32) - 1)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint32_t counter = 0x00000001;
|
||||
|
||||
for (int i = 0; i < (keyDataLen / 32); ++i) {
|
||||
uint8_t *hashMaterial = malloc(4 + sharedSecretLen + sharedInfoLen);
|
||||
memcpy(hashMaterial, sharedSecret, sharedSecretLen);
|
||||
hashMaterial[sharedSecretLen] = (counter >> 24);
|
||||
hashMaterial[sharedSecretLen + 1] = (counter >> 16) & 0xFF;
|
||||
hashMaterial[sharedSecretLen + 2] = (counter >> 8) & 0xFF;
|
||||
hashMaterial[sharedSecretLen + 3] = counter & 0xFF;
|
||||
memcpy(hashMaterial + sharedSecretLen + 4, sharedInfo, sharedInfoLen);
|
||||
|
||||
uint8_t hash[32] = {0};
|
||||
sha256hash(hashMaterial, 4 + sharedSecretLen + sharedInfoLen, hash);
|
||||
free(hashMaterial);
|
||||
|
||||
memcpy(keyData + (32 * i), hash, 32);
|
||||
|
||||
counter++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -64,4 +64,7 @@ size_t FindISO9797M2PaddingDataLen(const uint8_t *data, size_t datalen);
|
|||
// BLOWFISH
|
||||
int blowfish_decrypt(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *output, int length);
|
||||
|
||||
// KDF
|
||||
int ansi_x963_sha256(uint8_t *sharedSecret, size_t sharedSecretLen, uint8_t *sharedInfo, size_t sharedInfoLen, size_t keyDataLen, uint8_t *keyData);
|
||||
|
||||
#endif /* libpcrypto.h */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue