|
globalplatform
|
This implements all crypto functions. More...
#include "crypto.h"#include "scp.h"#include "globalplatform/connection.h"#include "globalplatform/stringify.h"#include "globalplatform/errorcodes.h"#include "globalplatform/error.h"#include "globalplatform/debug.h"#include "util.h"#include <errno.h>#include <stdio.h>#include <string.h>#include <openssl/err.h>#include <openssl/rand.h>#include <openssl/evp.h>#include <openssl/pem.h>#include <openssl/x509.h>#include <openssl/x509v3.h>#include <openssl/aes.h>#include <openssl/cmac.h>#include <openssl/ec.h>#include <openssl/ecdsa.h>#include <openssl/bn.h>
Data Structures | |
| struct | SCP11_X509_CHAIN_CERTIFICATE |
Functions | |
| OPGP_NO_API OPGP_ERROR_STATUS | calculate_enc_cbc (BYTE key[16], BYTE *message, DWORD messageLength, BYTE *encryption, DWORD *encryptionLength) |
| OPGP_NO_API OPGP_ERROR_STATUS | calculate_MAC_aes (BYTE key[32], DWORD keyLength, BYTE *message, DWORD messageLength, BYTE mac[16]) |
| OPGP_ERROR_STATUS | calculate_cryptogram_SCP03 (BYTE key[32], DWORD keyLength, const BYTE derivationConstant, PBYTE context1, DWORD context1Length, PBYTE context2, DWORD context2Length, PBYTE cryptogram, DWORD cryptogramSize) |
| OPGP_ERROR_STATUS | calculate_CMAC_aes (PBYTE sMacKey, DWORD keyLength, BYTE *message, DWORD messageLength, PBYTE chainingValue, PBYTE mac) |
| Creates a MAC for commands (APDUs) using CMAC AES. This is used by SCP03. The MAC for the message is the first 8 Bytes of mac. The next chainingValue is the full 16 Bytes of mac. Save this value for the next command MAC calculation. More... | |
| OPGP_ERROR_STATUS | calculate_enc_ecb_SCP03 (BYTE key[32], DWORD keyLength, BYTE *message, DWORD messageLength, BYTE *encryption, DWORD *encryptionLength) |
| OPGP_ERROR_STATUS | calculate_enc_cbc_SCP03_min_padding (BYTE key[32], DWORD keyLength, BYTE *message, DWORD messageLength, BYTE icv[16], BYTE *encryption, DWORD *encryptionLength) |
| OPGP_ERROR_STATUS | calculate_enc_cbc_SCP03 (BYTE key[32], DWORD keyLength, BYTE *message, DWORD messageLength, BYTE icv[16], BYTE *encryption, DWORD *encryptionLength) |
| OPGP_ERROR_STATUS | calculate_dec_cbc_SCP03 (PBYTE key, DWORD keyLength, BYTE *message, DWORD messageLength, PBYTE icv, BYTE *decryption, DWORD *decryptionLength) |
| OPGP_ERROR_STATUS | calculate_enc_icv_SCP03 (PBYTE key, DWORD keyLength, LONG sessionEncryptionCounter, PBYTE icv, BOOL forResponse) |
| OPGP_ERROR_STATUS | calculate_enc_cbc_SCP02 (BYTE key[16], BYTE *message, DWORD messageLength, BYTE *encryption, DWORD *encryptionLength) |
| OPGP_ERROR_STATUS | calculate_card_cryptogram_SCP01 (BYTE S_ENCSessionKey[16], BYTE cardChallenge[8], BYTE hostChallenge[8], BYTE cardCryptogram[8]) |
| OPGP_ERROR_STATUS | calculate_card_cryptogram_SCP02 (BYTE S_ENCSessionKey[16], BYTE sequenceCounter[2], PBYTE cardChallenge, BYTE hostChallenge[8], BYTE cardCryptogram[8]) |
| OPGP_ERROR_STATUS | calculate_card_cryptogram_SCP03 (BYTE S_MACSessionKey[32], DWORD keyLength, BYTE cardChallenge[8], BYTE hostChallenge[8], BYTE cardCryptogram[8]) |
| OPGP_ERROR_STATUS | calculate_card_challenge_SCP03 (BYTE S_ENC[32], DWORD keyLength, BYTE sequenceCounter[3], PBYTE invokingAID, DWORD invokingAIDLength, BYTE cardChallenge[8]) |
| OPGP_ERROR_STATUS | calculate_host_cryptogram_SCP01 (BYTE S_ENCSessionKey[16], BYTE cardChallenge[8], BYTE hostChallenge[8], BYTE hostCryptogram[8]) |
| OPGP_ERROR_STATUS | calculate_host_cryptogram_SCP02 (BYTE S_ENCSessionKey[16], BYTE sequenceCounter[2], PBYTE cardChallenge, BYTE hostChallenge[8], BYTE hostCryptogram[8]) |
| OPGP_ERROR_STATUS | calculate_host_cryptogram_SCP03 (BYTE S_MACSessionKey[32], DWORD keyLength, BYTE cardChallenge[8], BYTE hostChallenge[8], BYTE hostCryptogram[8]) |
| OPGP_ERROR_STATUS | create_session_key_SCP01 (BYTE key[16], BYTE cardChallenge[8], BYTE hostChallenge[8], PBYTE sessionKey) |
| OPGP_ERROR_STATUS | create_session_key_SCP02 (BYTE key[16], BYTE constant[2], BYTE sequenceCounter[2], PBYTE sessionKey) |
| OPGP_ERROR_STATUS | create_session_key_SCP03 (BYTE key[32], DWORD keyLength, BYTE derivationConstant, BYTE cardChallenge[8], BYTE hostChallenge[8], PBYTE sessionKey) |
| OPGP_ERROR_STATUS | calculate_enc_ecb_two_key_triple_des (BYTE key[16], BYTE *message, DWORD messageLength, BYTE *encryption, DWORD *encryptionLength) |
| OPGP_ERROR_STATUS | calculate_enc_ecb_single_des (BYTE key[8], BYTE *message, DWORD messageLength, BYTE *encryption, DWORD *encryptionLength) |
| OPGP_ERROR_STATUS | calculate_MAC (BYTE sessionKey[16], BYTE *message, DWORD messageLength, BYTE icv[8], BYTE mac[8]) |
| OPGP_ERROR_STATUS | calculate_signature (PBYTE message, DWORD messageLength, OPGP_STRING PEMKeyFileName, char *passPhrase, PBYTE signature, PDWORD signatureLength) |
| OPGP_ERROR_STATUS | calculate_rsa_signature (PBYTE message, DWORD messageLength, OPGP_STRING PEMKeyFileName, char *passPhrase, PBYTE signature, PDWORD signatureLength) |
| OPGP_ERROR_STATUS | calculate_ecc_signature (PBYTE message, DWORD messageLength, OPGP_STRING PEMKeyFileName, char *passPhrase, PBYTE signature, PDWORD signatureLength) |
| OPGP_ERROR_STATUS | validate_signature (PBYTE message, DWORD messageLength, OPGP_STRING PEMKeyFileName, char *passPhrase, PBYTE signature, DWORD signatureLength) |
| OPGP_ERROR_STATUS | calculate_MAC_des_3des (BYTE _3des_key[16], BYTE *message, DWORD messageLength, BYTE initialICV[8], BYTE mac[8]) |
| OPGP_ERROR_STATUS | validate_receipt_ex (PBYTE validationData, DWORD validationDataLength, PBYTE receipt, DWORD receiptLength, PBYTE receiptKey, DWORD keyLength, BYTE receiptKeyType, OPGP_STRING PEMKeyFileName, char *passPhrase) |
| OPGP_ERROR_STATUS | validate_receipt (PBYTE validationData, DWORD validationDataLength, BYTE receipt[16], PBYTE receiptKey, DWORD keyLength, BYTE secureChannelProtocol) |
| OPGP_ERROR_STATUS | validate_delete_receipt (PBYTE receiptKey, DWORD keyLength, GP211_RECEIPT_DATA receiptData, PBYTE AID, DWORD AIDLength, BYTE receiptKeyType, OPGP_STRING PEMKeyFileName, char *passPhrase) |
| OPGP_ERROR_STATUS | encrypt_sensitive_data (GP211_SECURITY_INFO *secInfo, PBYTE data, DWORD dataLength, PBYTE encryptedData, PDWORD encryptedDataLength) |
| OPGP_ERROR_STATUS | calculate_key_check_value (GP211_SECURITY_INFO *secInfo, BYTE keyType, PBYTE keyData, DWORD keyDataLength, BYTE keyCheckValue[3]) |
| OPGP_ERROR_STATUS | get_key_data_field (GP211_SECURITY_INFO *secInfo, PBYTE keyData, DWORD keyDataLength, BYTE keyType, PBYTE keyDataField, PDWORD keyDataFieldLength, BYTE keyCheckValue[3], BOOL includeKeyCheckValue) |
| OPGP_ERROR_STATUS | wrap_command (PBYTE apduCommand, DWORD apduCommandLength, PBYTE wrappedApduCommand, PDWORD wrappedApduCommandLength, GP211_SECURITY_INFO *secInfo) |
| Wraps a command, i.e. encrypts and add the MAC to the APDU with the necessary security information according to secInfo. More... | |
| OPGP_ERROR_STATUS | validate_install_receipt (PBYTE receiptKey, DWORD keyLength, GP211_RECEIPT_DATA receiptData, PBYTE executableLoadFileAID, DWORD executableLoadFileAIDLength, PBYTE applicationAID, DWORD applicationAIDLength, BYTE receiptKeyType, OPGP_STRING PEMKeyFileName, char *passPhrase) |
| OPGP_ERROR_STATUS | validate_load_receipt (PBYTE receiptKey, DWORD keyLength, GP211_RECEIPT_DATA receiptData, PBYTE executableLoadFileAID, DWORD executableLoadFileAIDLength, PBYTE securityDomainAID, DWORD securityDomainAIDLength, BYTE receiptKeyType, OPGP_STRING PEMKeyFileName, char *passPhrase) |
| OPGP_ERROR_STATUS | validate_extradition_receipt (PBYTE receiptKey, DWORD keyLength, GP211_RECEIPT_DATA receiptData, PBYTE oldSecurityDomainAID, DWORD oldSecurityDomainAIDLength, PBYTE newSecurityDomainAID, DWORD newSecurityDomainAIDLength, PBYTE applicationOrExecutableLoadFileAID, DWORD applicationOrExecutableLoadFileAIDLength, BYTE receiptKeyType, OPGP_STRING PEMKeyFileName, char *passPhrase) |
| OPGP_ERROR_STATUS | validate_registry_update_receipt (PBYTE receiptKey, DWORD keyLength, GP211_RECEIPT_DATA receiptData, PBYTE oldSecurityDomainAID, DWORD oldSecurityDomainAIDLength, PBYTE applicationAID, DWORD applicationAIDLength, PBYTE newSecurityDomainAID, DWORD newSecurityDomainAIDLength, DWORD applicationPrivileges, PBYTE registryUpdateParameters, DWORD registryUpdateParametersLength, BYTE receiptKeyType, OPGP_STRING PEMKeyFileName, char *passPhrase) |
| OPGP_ERROR_STATUS | GP211_calculate_R_MAC (PBYTE apduCommand, DWORD apduCommandLength, PBYTE responseApdu, DWORD responseApduLength, GP211_SECURITY_INFO *secInfo, BYTE mac[8]) |
| Calculates a R-MAC. More... | |
| OPGP_ERROR_STATUS | unwrap_command (PBYTE apduCommand, DWORD apduCommandLength, PBYTE responseApdu, DWORD responseApduLength, PBYTE unwrappedResponseApdu, PDWORD unwrappedResponseApduLength, GP211_SECURITY_INFO *secInfo) |
| Unwraps a response, i.e. decrypt and check the R-MAC of a response APDU with the necessary security information according to secInfo. More... | |
| OPGP_ERROR_STATUS | GP211_check_R_MAC (PBYTE apduCommand, DWORD apduCommandLength, PBYTE responseApdu, DWORD responseApduLength, PBYTE unwrappedResponseApdu, PDWORD unwrappedResponseApduLength, GP211_SECURITY_INFO *secInfo) |
| Checks the R-MAC of an APDU with the necessary security information according to secInfo. More... | |
| OPGP_ERROR_STATUS | generate_ecc_keypair (int curveNid, PBYTE privateKey, PDWORD privateKeyLength, PBYTE publicKey, PDWORD publicKeyLength) |
| Generates an ECC key pair for a named OpenSSL curve. | |
| OPGP_ERROR_STATUS | calculate_ecc_shared_secret (int curveNid, PBYTE privateKey, DWORD privateKeyLength, PBYTE peerPublicKey, DWORD peerPublicKeyLength, PBYTE sharedSecret, PDWORD sharedSecretLength) |
| Calculates an ECDH shared secret for a named OpenSSL curve. | |
| BOOL | contains_pem_certificate_header (const BYTE *data, DWORD dataLength) |
| Checks whether data contains a PEM CERTIFICATE header. | |
| OPGP_ERROR_STATUS | convert_pem_certificate_list_to_der (const BYTE *pemData, DWORD pemDataLength, PBYTE derData, PDWORD derDataLength) |
| Converts one or more PEM CERTIFICATE blocks to concatenated DER certificates. | |
| OPGP_ERROR_STATUS | convert_der_certificate_list_to_pem (const BYTE *derData, DWORD derDataLength, PBYTE pemData, PDWORD pemDataLength) |
| Converts one or more concatenated DER X.509 certificates to PEM CERTIFICATE blocks. | |
| OPGP_ERROR_STATUS | prepare_scp11_der_x509_certificate_chain (const BYTE *derData, DWORD derDataLength, PBYTE preparedData, PDWORD preparedDataLength) |
| Orders a DER X.509 chain for SCP11 verification and omits a self-issued trust anchor. | |
| OPGP_ERROR_STATUS | extract_scp11_ca_kloc_identifier_from_certificate_chain (const BYTE *certificateChainData, DWORD certificateChainDataLength, PBYTE authorityIdentifier, PDWORD authorityIdentifierLength) |
| Extracts the CA-KLOC Identifier used to verify the first SCP11 certificate. | |
| OPGP_ERROR_STATUS | read_certificate_file (OPGP_STRING PEMKeyFileName, char *passPhrase, PBYTE certificateData, PDWORD certificateDataLength) |
| Reads a certificate from a PEM or DER file. | |
| OPGP_ERROR_STATUS | read_public_rsa_key (OPGP_STRING PEMKeyFileName, char *passPhrase, PBYTE rsaModulus, PDWORD rsaModulusLength, LONG *rsaExponent) |
| Reads a public RSA key from a file. More... | |
| OPGP_ERROR_STATUS | read_public_ecc_key_from_der_certificate (const BYTE *certificateData, DWORD certificateDataLength, PBYTE eccPublicPoint, PDWORD eccPublicPointLength, PBYTE eccKeyComponentType, PBYTE keyParameterReference) |
| Extracts a public ECC key from a DER encoded X.509 certificate. | |
| OPGP_ERROR_STATUS | read_public_ecc_key (OPGP_STRING PEMKeyFileName, char *passPhrase, PBYTE eccPublicPoint, PDWORD eccPublicPointLength, PBYTE eccKeyComponentType, PBYTE keyParameterReference, GP211_ECC_DOMAIN_PARAMETERS *domainParameters) |
| Reads a public ECC key from a file. More... | |
| OPGP_ERROR_STATUS | calculate_hash (PBYTE message, DWORD messageLength, BYTE hash[64], const EVP_MD *md) |
| OPGP_ERROR_STATUS | calculate_sha2_hash (PBYTE message, DWORD messageLength, BYTE hash[64], DWORD hashLength) |
| Calculates a SHA-256 hash. More... | |
| OPGP_ERROR_STATUS | calculate_sha1_hash (PBYTE message, DWORD messageLength, BYTE hash[20]) |
| Calculates a SHA-1 hash. More... | |
| OPGP_ERROR_STATUS | calculate_sm3_hash (PBYTE message, DWORD messageLength, BYTE hash[32]) |
| Calculates a SM3 hash. More... | |
| OPGP_ERROR_STATUS | calculate_MAC_right_des_3des (BYTE key[16], BYTE *message, DWORD messageLength, BYTE mac[8]) |
| Calculates a MAC using first DES and 3DES for the final round when the padding is applied. More... | |
| OPGP_ERROR_STATUS | get_random (BYTE *random, int randomLength) |
This implements all crypto functions.
| OPGP_ERROR_STATUS calculate_card_challenge_SCP03 | ( | BYTE | S_ENC[32], |
| DWORD | keyLength, | ||
| BYTE | sequenceCounter[3], | ||
| PBYTE | invokingAID, | ||
| DWORD | invokingAIDLength, | ||
| BYTE | cardChallenge[8] | ||
| ) |
Calculates the card challenge when using pseudo-random challenge generation for SCP03.
| S_ENC | [in] The static S-ENC Key. |
| keyLength | [in] The key length in bytes (16, 24 or 32). |
| sequenceCounter | [in] The sequence counter. |
| invokingAID | The invoking AID byte buffer. |
| invokingAIDLength | The length of the invoking AID byte buffer. |
| cardChallenge | [out] The calculated challenge. |
| OPGP_ERROR_STATUS calculate_card_cryptogram_SCP01 | ( | BYTE | S_ENCSessionKey[16], |
| BYTE | cardChallenge[8], | ||
| BYTE | hostChallenge[8], | ||
| BYTE | cardCryptogram[8] | ||
| ) |
Calculates the card cryptogram for SCP01.
| S_ENCSessionKey | [in] The S-ENC Session Key for calculating the card cryptogram. |
| cardChallenge | [in] The card challenge. |
| hostChallenge | [in] The host challenge. |
| cardCryptogram | [out] The calculated card cryptogram. |
| OPGP_ERROR_STATUS calculate_card_cryptogram_SCP02 | ( | BYTE | S_ENCSessionKey[16], |
| BYTE | sequenceCounter[2], | ||
| PBYTE | cardChallenge, | ||
| BYTE | hostChallenge[8], | ||
| BYTE | cardCryptogram[8] | ||
| ) |
Calculates the card cryptogram for SCP02.
| S_ENCSessionKey | [in] The S-ENC Session Key for calculating the card cryptogram. |
| sequenceCounter | [in] The sequence counter. |
| cardChallenge | [in] The card challenge. |
| hostChallenge | [in] The host challenge. |
| cardCryptogram | [out] The calculated card cryptogram. |
| OPGP_ERROR_STATUS calculate_card_cryptogram_SCP03 | ( | BYTE | S_MACSessionKey[32], |
| DWORD | keyLength, | ||
| BYTE | cardChallenge[8], | ||
| BYTE | hostChallenge[8], | ||
| BYTE | cardCryptogram[8] | ||
| ) |
Calculates the card cryptogram for SCP03.
| S_MACSessionKey | [in] The S-MAC Session Key for calculating the card cryptogram. |
| keyLength | [in] The key length in bytes (16, 24 or 32). |
| cardChallenge | [in] The card challenge. |
| hostChallenge | [in] The host challenge. |
| cardCryptogram | [out] The calculated host cryptogram. |
| OPGP_ERROR_STATUS calculate_CMAC_aes | ( | PBYTE | sMacKey, |
| DWORD | keyLength, | ||
| BYTE * | message, | ||
| DWORD | messageLength, | ||
| PBYTE | chainingValue, | ||
| PBYTE | mac | ||
| ) |
Creates a MAC for commands (APDUs) using CMAC AES. This is used by SCP03. The MAC for the message is the first 8 Bytes of mac. The next chainingValue is the full 16 Bytes of mac. Save this value for the next command MAC calculation.
| sMacKey | [in] The S-MAC key (session MAC key) to use for MAC generation. |
| keyLength | [in] The AES key length in bytes (16, 24, or 32). |
| message | [in] The message to generate the MAC for. |
| messageLength | [in] The length of the message. |
| chainingValue | [in] The chaining value to use for the MAC generation. This is usually the full 16 Byte MAC generated for the last command or 16 bytes 0x00 for the first one (i.e. EXTERNAL AUTHENTICATE). |
| mac | [out] The full 16 Byte MAC. Append the first 8 Bytes to the message. Save the full 16 Bytes for further MAC generation if needed. |
| OPGP_ERROR_STATUS calculate_cryptogram_SCP03 | ( | BYTE | key[32], |
| DWORD | keyLength, | ||
| const BYTE | derivationConstant, | ||
| PBYTE | context1, | ||
| DWORD | context1Length, | ||
| PBYTE | context2, | ||
| DWORD | context2Length, | ||
| PBYTE | cryptogram, | ||
| DWORD | cryptogramSize | ||
| ) |
Calculates the a cryptogram for multiple SCP03 operation like host cryptogram, card cryptogram and card challenge calculation.
| key | [in] The AES key to use for the calculation. |
| keyLength | [in] The AES key length in bytes (16, 24, or 32). |
| derivationConstant | [in] The derivation constant for the key derivation function. |
| context1 | [in] The context1 for the internal key derivation. |
| context1Length | [in] The length of the context1 buffer. |
| context2 | [in] The context2 for the internal key derivation. |
| context2Length | [in] The length of the context2 buffer. |
| cryptogram | [out] The calculated cryptogram. Must be large enough to hold the result. For session keys this is 128 bits, 64 bits otherwise. |
| cryptogramSize | [in] The result size in bits of the cryptogram. Must be a multiple of 8. |
| OPGP_ERROR_STATUS calculate_dec_cbc_SCP03 | ( | PBYTE | key, |
| DWORD | keyLength, | ||
| BYTE * | message, | ||
| DWORD | messageLength, | ||
| PBYTE | icv, | ||
| BYTE * | decryption, | ||
| DWORD * | decryptionLength | ||
| ) |
Calculates the decryption of a message in CBC mode for SCP03 using AES. Also take ISO7816-4 padding into account (0x80 and additional 0x00).
| key | [in] An AES key used to decrypt. |
| keyLength | [in] The key length. 16, 24 or 32 bytes. |
| *message | [in] The message to decrypt. |
| messageLength | [in] The length of the message. |
| icv | [in] The ICV to use. |
| *decryption | [out] The encryption. |
| *decryptionLength | [out] The length of the encryption. |
| OPGP_ERROR_STATUS calculate_ecc_signature | ( | PBYTE | message, |
| DWORD | messageLength, | ||
| OPGP_STRING | PEMKeyFileName, | ||
| char * | passPhrase, | ||
| PBYTE | signature, | ||
| PDWORD | signatureLength | ||
| ) |
Backward compatible wrapper for callers using ECC-specific naming.
| OPGP_ERROR_STATUS calculate_enc_cbc | ( | BYTE | key[16], |
| BYTE * | message, | ||
| DWORD | messageLength, | ||
| BYTE * | encryption, | ||
| DWORD * | encryptionLength | ||
| ) |
Calculates the encryption of a message in CBC mode. Pads the message with 0x80 and additional 0x00 if message length is not a multiple of 8.
| key | [in] A 3DES key used to encrypt. |
| *message | [in] The message to encrypt. |
| messageLength | [in] The length of the message. |
| *encryption | [out] The encryption. |
| *encryptionLength | [out] The length of the encryption. |
| OPGP_ERROR_STATUS calculate_enc_cbc_SCP02 | ( | BYTE | key[16], |
| BYTE * | message, | ||
| DWORD | messageLength, | ||
| BYTE * | encryption, | ||
| DWORD * | encryptionLength | ||
| ) |
Calculates the encryption of a message in CBC mode for SCP02. Pads the message with 0x80 and additional 0x00 until message length is a multiple of 8.
| key | [in] A 3DES key used to encrypt. |
| *message | [in] The message to encrypt. |
| messageLength | [in] The length of the message. |
| *encryption | [out] The encryption. |
| *encryptionLength | [out] The length of the encryption. |
| OPGP_ERROR_STATUS calculate_enc_cbc_SCP03 | ( | BYTE | key[32], |
| DWORD | keyLength, | ||
| BYTE * | message, | ||
| DWORD | messageLength, | ||
| BYTE | icv[16], | ||
| BYTE * | encryption, | ||
| DWORD * | encryptionLength | ||
| ) |
Calculates the encryption of a message in CBC mode for SCP03 using AES. Pads the message with 0x80 and additional 0x00 until message length is a multiple of the block size.
| key | [in] An AES key used to encrypt. |
| keyLength | [in] The key length. 16, 24 or 32 bytes. |
| *message | [in] The message to encrypt. |
| messageLength | [in] The length of the message. |
| icv | [in] The ICV to use. |
| *encryption | [out] The encryption. |
| *encryptionLength | [out] The length of the encryption. |
| OPGP_ERROR_STATUS calculate_enc_cbc_SCP03_min_padding | ( | BYTE | key[32], |
| DWORD | keyLength, | ||
| BYTE * | message, | ||
| DWORD | messageLength, | ||
| BYTE | icv[16], | ||
| BYTE * | encryption, | ||
| DWORD * | encryptionLength | ||
| ) |
Calculates the encryption of a message in CBC mode for SCP03 using AES with no padding if not needed.
| key | [in] An AES key used to encrypt. |
| keyLength | [in] The key length. 16, 24 or 32 bytes. |
| *message | [in] The message to encrypt. |
| messageLength | [in] The length of the message. |
| icv | [in] The ICV to use. |
| *encryption | [out] The encryption. |
| *encryptionLength | [out] The length of the encryption. |
| OPGP_ERROR_STATUS calculate_enc_ecb_single_des | ( | BYTE | key[8], |
| BYTE * | message, | ||
| DWORD | messageLength, | ||
| BYTE * | encryption, | ||
| DWORD * | encryptionLength | ||
| ) |
Calculates the encryption of a message in ECB mode with single DES. Pads the message with 0x80 and additional 0x00 if message length is not a multiple of 8.
| key | [in] A DES key used to encrypt. |
| *message | [in] The message to encrypt. |
| messageLength | [in] The length of the message. |
| *encryption | [out] The encryption. |
| *encryptionLength | [out] The length of the encryption. |
| OPGP_ERROR_STATUS calculate_enc_ecb_two_key_triple_des | ( | BYTE | key[16], |
| BYTE * | message, | ||
| DWORD | messageLength, | ||
| BYTE * | encryption, | ||
| DWORD * | encryptionLength | ||
| ) |
Calculates the encryption of a message in ECB mode with two key triple DES. Pads the message with 0x80 and additional 0x00 if message length is not a multiple of 8.
| key | [in] A 3DES key used to encrypt. |
| *message | [in] The message to encrypt. |
| messageLength | [in] The length of the message. |
| *encryption | [out] The encryption. |
| *encryptionLength | [out] The length of the encryption. |
| OPGP_ERROR_STATUS calculate_enc_icv_SCP03 | ( | PBYTE | key, |
| DWORD | keyLength, | ||
| LONG | sessionEncryptionCounter, | ||
| PBYTE | icv, | ||
| BOOL | forResponse | ||
| ) |
Calculates the encryption ICV for SCP03 using AES.
| key | [in] An AES key used to encrypt. |
| keyLength | [in] The key length. 16, 24 or 32 bytes. |
| sessionEncryptionCounter | [in] The session encryption counter. |
| icv | [out] The ICV to use. |
| forResponse | 1 if the calculation is performed for the response. |
| OPGP_ERROR_STATUS calculate_hash | ( | PBYTE | message, |
| DWORD | messageLength, | ||
| BYTE | hash[64], | ||
| const EVP_MD * | md | ||
| ) |
| message | [in] The message to generate the hash for. |
| messageLength | [in] The length of the message buffer. |
| hash | [out] The calculated hash. |
| md | [in] The message digest to use. |
| OPGP_ERROR_STATUS calculate_host_cryptogram_SCP01 | ( | BYTE | S_ENCSessionKey[16], |
| BYTE | cardChallenge[8], | ||
| BYTE | hostChallenge[8], | ||
| BYTE | hostCryptogram[8] | ||
| ) |
Calculates the host cryptogram for SCP01.
| S_ENCSessionKey | [in] The S-ENC Session Key for calculating the card cryptogram. |
| cardChallenge | [in] The card challenge. |
| hostChallenge | [in] The host challenge. |
| hostCryptogram | [out] The calculated host cryptogram. |
| OPGP_ERROR_STATUS calculate_host_cryptogram_SCP02 | ( | BYTE | S_ENCSessionKey[16], |
| BYTE | sequenceCounter[2], | ||
| PBYTE | cardChallenge, | ||
| BYTE | hostChallenge[8], | ||
| BYTE | hostCryptogram[8] | ||
| ) |
Calculates the host cryptogram for SCP02.
| S_ENCSessionKey | [in] The S-ENC Session Key for calculating the card cryptogram. |
| sequenceCounter | [in] The sequence counter. |
| cardChallenge | [in] The card challenge. |
| hostChallenge | [in] The host challenge. |
| hostCryptogram | [out] The calculated host cryptogram. |
| OPGP_ERROR_STATUS calculate_host_cryptogram_SCP03 | ( | BYTE | S_MACSessionKey[32], |
| DWORD | keyLength, | ||
| BYTE | cardChallenge[8], | ||
| BYTE | hostChallenge[8], | ||
| BYTE | hostCryptogram[8] | ||
| ) |
Calculates the host cryptogram for SCP03.
| S_MACSessionKey | [in] The S-MAC Session Key for calculating the card cryptogram. |
| keyLength | [in] The key length in bytes (16, 24 or 32). |
| cardChallenge | [in] The card challenge. |
| hostChallenge | [in] The host challenge. |
| hostCryptogram | [out] The calculated host cryptogram. |
| OPGP_ERROR_STATUS calculate_key_check_value | ( | GP211_SECURITY_INFO * | secInfo, |
| BYTE | keyType, | ||
| PBYTE | keyData, | ||
| DWORD | keyDataLength, | ||
| BYTE | keyCheckValue[3] | ||
| ) |
| *secInfo | [in, out] The pointer to the GP211_SECURITY_INFO structure returned by GP211_mutual_authentication(). |
| keyType | [in] The key type. See GP211_KEY_TYPE_AES. |
| keyData | [in] The key data. |
| keyDataLength | [in] The key data length. |
| keyCheckValue | [out] The key check value. |
| OPGP_ERROR_STATUS calculate_MAC | ( | BYTE | sessionKey[16], |
| BYTE * | message, | ||
| DWORD | messageLength, | ||
| BYTE | icv[8], | ||
| BYTE | mac[8] | ||
| ) |
Calculates a message authentication code. Pads the message always with 0x80 and additional 0x00 until message length is a multiple of 8.
| sessionKey | [in] A 3DES key used to sign. |
| *message | [in] The message to authenticate. |
| messageLength | [in] The message length. |
| icv | [in] The initial chaining vector. |
| mac | [out] The calculated MAC. |
| OPGP_ERROR_STATUS calculate_MAC_aes | ( | BYTE | key[32], |
| DWORD | keyLength, | ||
| BYTE * | message, | ||
| DWORD | messageLength, | ||
| BYTE | mac[16] | ||
| ) |
Calculates a message authentication code, using AES-128/224/256 in CBC mode. This is the algorithm specified in NIST 800-38B.
| key | [in] The AES key to use. |
| keyLength | [in] The AES key length in bytes (16, 24, or 32). |
| *message | [in] The message to calculate the MAC for. |
| messageLength | [in] The message length. |
| mac | [out] The calculated MAC. |
| OPGP_ERROR_STATUS calculate_MAC_des_3des | ( | BYTE | _3des_key[16], |
| BYTE * | message, | ||
| DWORD | messageLength, | ||
| BYTE | initialICV[8], | ||
| BYTE | mac[8] | ||
| ) |
Calculates a message authentication code using the left half key of a two key 3DES key and the full key for the final operation. Pads the message always with 0x80 and additional 0x00 until message length is a multiple of 8.
| _3des_key | [in] A 3DES key used to sign. |
| *message | [in] The message to authenticate. |
| messageLength | [in] The message length. |
| initialICV | [in] The initial chaining vector. |
| mac | [out] The calculated MAC. |
| OPGP_ERROR_STATUS calculate_MAC_right_des_3des | ( | BYTE | key[16], |
| BYTE * | message, | ||
| DWORD | messageLength, | ||
| BYTE | mac[8] | ||
| ) |
Calculates a MAC using first DES and 3DES for the final round when the padding is applied.
| key | [in] A 3DES key used to sign. For DES the right half of the key is used. |
| *message | [in] The message to authenticate. |
| messageLength | [in] The message length. |
| mac | [out] The calculated MAC. |
| OPGP_ERROR_STATUS calculate_rsa_signature | ( | PBYTE | message, |
| DWORD | messageLength, | ||
| OPGP_STRING | PEMKeyFileName, | ||
| char * | passPhrase, | ||
| PBYTE | signature, | ||
| PDWORD | signatureLength | ||
| ) |
Backward compatible wrapper for callers using RSA-specific naming.
| OPGP_ERROR_STATUS calculate_sha1_hash | ( | PBYTE | message, |
| DWORD | messageLength, | ||
| BYTE | hash[20] | ||
| ) |
Calculates a SHA-1 hash.
| message | [in] The message to generate the hash for. |
| messageLength | [in] The length of the message buffer. |
| hash | [out] The calculated hash. |
| OPGP_ERROR_STATUS calculate_sha2_hash | ( | PBYTE | message, |
| DWORD | messageLength, | ||
| BYTE | hash[64], | ||
| DWORD | hashLength | ||
| ) |
Calculates a SHA-256 hash.
| message | [in] The message to generate the hash for. |
| messageLength | [in] The length of the message buffer. |
| hash | [out] The calculated hash. |
| hashLength | [in] The hash length for SCP03: 32 for AES-128, 48 for AES-192, 64 for AES-256. |
| OPGP_ERROR_STATUS calculate_signature | ( | PBYTE | message, |
| DWORD | messageLength, | ||
| OPGP_STRING | PEMKeyFileName, | ||
| char * | passPhrase, | ||
| PBYTE | signature, | ||
| PDWORD | signatureLength | ||
| ) |
Calculates a signature using an RSA or ECC private key from a PEM file. RSA and ECC signature encoding rules are selected automatically from the private key type.
| message | [in] The data to sign. |
| messageLength | [in] The length of the message buffer. |
| PEMKeyFileName | [in] A PEM file name with the private key. |
| *passPhrase | [in] The passphrase. Must be an ASCII string. May be NULL for unencrypted keys. |
| signature | [out] The calculated signature buffer. |
| signatureLength | [in,out] On input: size of signature buffer. On output: actual signature length. |
| OPGP_ERROR_STATUS calculate_sm3_hash | ( | PBYTE | message, |
| DWORD | messageLength, | ||
| BYTE | hash[32] | ||
| ) |
Calculates a SM3 hash.
| message | [in] The message to generate the hash for. |
| messageLength | [in] The length of the message buffer. |
| hash | [out] The calculated hash (32 bytes for SM3). |
| OPGP_ERROR_STATUS create_session_key_SCP01 | ( | BYTE | key[16], |
| BYTE | cardChallenge[8], | ||
| BYTE | hostChallenge[8], | ||
| PBYTE | sessionKey | ||
| ) |
Creates the session key for SCP01.
| key | [in] The Secure Channel Encryption Key or Secure Channel Message Authentication Code Key for calculating the corresponding session key. |
| cardChallenge | [in] The card challenge. |
| hostChallenge | [in] The host challenge. |
| sessionKey | [out] The calculated 3DES session key. |
| OPGP_ERROR_STATUS create_session_key_SCP02 | ( | BYTE | key[16], |
| BYTE | constant[2], | ||
| BYTE | sequenceCounter[2], | ||
| PBYTE | sessionKey | ||
| ) |
Creates the session key for SCP02.
| key | [in] The Secure Channel Encryption Key or Secure Channel Message Authentication Code Key or Data Encryption Key for calculating the corresponding session key. |
| constant | [in] The constant for the corresponding session key. |
| sequenceCounter | [in] The sequence counter. |
| sessionKey | [out] The calculated 3DES session key. |
| OPGP_ERROR_STATUS create_session_key_SCP03 | ( | BYTE | key[32], |
| DWORD | keyLength, | ||
| BYTE | derivationConstant, | ||
| BYTE | cardChallenge[8], | ||
| BYTE | hostChallenge[8], | ||
| PBYTE | sessionKey | ||
| ) |
Creates an AES-128 session key for SCP03.
| key | [in] The Secure Channel Encryption Key or Secure Channel Message Authentication Code Key for calculating the corresponding session key. |
| keyLength | [in] The key length. 16, 24 or 32 bytes. |
| derivationConstant | [in] The derivation constant, as defined in "Table 4-1: Data derivation constants" of SCP03. |
| cardChallenge | [in] The card challenge. |
| hostChallenge | [in] The host challenge. |
| sessionKey | [out] The calculated AES session key. |
| OPGP_ERROR_STATUS encrypt_sensitive_data | ( | GP211_SECURITY_INFO * | secInfo, |
| PBYTE | data, | ||
| DWORD | dataLength, | ||
| PBYTE | encryptedData, | ||
| PDWORD | encryptedDataLength | ||
| ) |
| *secInfo | [in, out] The pointer to the GP211_SECURITY_INFO structure returned by GP211_mutual_authentication(). |
| data | [in] The data to encrypt. |
| dataLength | [in] The data length. |
| encryptedData | [out] The encrypted data. No length checking is done. The buffer must have sufficient size, e.g. the next block size or twice the data size if unsure. |
| encryptedDataLength | [out] The length of the encrypted data. |
| OPGP_ERROR_STATUS get_random | ( | BYTE * | random, |
| int | randomLength | ||
| ) |
| *random | [out] The random to generate. |
| randomLength | [in] The random length to generate. |
| OPGP_ERROR_STATUS GP211_calculate_R_MAC | ( | PBYTE | apduCommand, |
| DWORD | apduCommandLength, | ||
| PBYTE | responseApdu, | ||
| DWORD | responseApduLength, | ||
| GP211_SECURITY_INFO * | secInfo, | ||
| BYTE | mac[8] | ||
| ) |
Calculates a R-MAC.
| apduCommand | [in] The APDU command. |
| apduCommandLength | [in] The APDU command length. |
| responseApdu | [in] The APDU response APDU. |
| responseApduLength | [in] The APDU response APDU length. |
| *secInfo | [in] The pointer to the GP211_SECURITY_INFO structure returned by GP211_mutual_authentication(). |
| mac | [out] The R-MAC. |
| OPGP_ERROR_STATUS GP211_check_R_MAC | ( | PBYTE | apduCommand, |
| DWORD | apduCommandLength, | ||
| PBYTE | responseApdu, | ||
| DWORD | responseApduLength, | ||
| PBYTE | unwrappedResponseApdu, | ||
| PDWORD | unwrappedResponseApduLength, | ||
| GP211_SECURITY_INFO * | secInfo | ||
| ) |
Checks the R-MAC of an APDU with the necessary security information according to secInfo.
| apduCommand | [in] The command APDU. |
| apduCommandLength | [in] The length of the command APDU. |
| responseApdu | [in] The response APDU. |
| responseApduLength | [in] The length of the response APDU. |
| unwrappedResponseApdu | [out] The buffer for the unwrapped response APDU. |
| unwrappedResponseApduLength | [in, out] The available and returned modified length of the unwrappedResponseAPDU buffer. |
| *secInfo | [in] The pointer to the GP211_SECURITY_INFO structure returned by GP211_mutual_authentication(). |
| OPGP_ERROR_STATUS read_public_ecc_key | ( | OPGP_STRING | PEMKeyFileName, |
| char * | passPhrase, | ||
| PBYTE | eccPublicPoint, | ||
| PDWORD | eccPublicPointLength, | ||
| PBYTE | eccKeyComponentType, | ||
| PBYTE | keyParameterReference, | ||
| GP211_ECC_DOMAIN_PARAMETERS * | domainParameters | ||
| ) |
Reads a public ECC key from a file.
| PEMKeyFileName | [in] The key file. |
| *passPhrase | [in] The passphrase. Must be an ASCII string. |
| eccPublicPoint | [out] The ECC public point in uncompressed form. |
| eccPublicPointLength | [inout] The ECC public point length passed in and returned. |
| eccKeyComponentType | [out] GP211_KEY_TYPE_ECC_PUBLIC_OR_PRIVATE or GP211_KEY_TYPE_ECC_SM2_PUBLIC_OR_PRIVATE. |
| keyParameterReference | [out] ECC key parameter reference (see GP211_KEY_TYPE_ECC_KEY_PARAMETER_REFERENCE_*). |
| domainParameters | [out] Optional ECC domain parameters for own-parameter key loading. |
| OPGP_ERROR_STATUS read_public_rsa_key | ( | OPGP_STRING | PEMKeyFileName, |
| char * | passPhrase, | ||
| PBYTE | rsaModulus, | ||
| PDWORD | rsaModulusLength, | ||
| LONG * | rsaExponent | ||
| ) |
Reads a public RSA key from a file.
| PEMKeyFileName | [in] The key file. |
| *passPhrase | [in] The passphrase. Must be an ASCII string. |
| rsaModulus | [out] The RSA modulus. |
| rsaModulusLength | [inout] The RSA modules length passed in and returned. |
| rsaExponent | [out] The RSA exponent. |
| OPGP_ERROR_STATUS unwrap_command | ( | PBYTE | apduCommand, |
| DWORD | apduCommandLength, | ||
| PBYTE | responseApdu, | ||
| DWORD | responseApduLength, | ||
| PBYTE | unwrappedResponseApdu, | ||
| PDWORD | unwrappedResponseApduLength, | ||
| GP211_SECURITY_INFO * | secInfo | ||
| ) |
Unwraps a response, i.e. decrypt and check the R-MAC of a response APDU with the necessary security information according to secInfo.
| apduCommand | [in] The command APDU. |
| apduCommandLength | [in] The length of the command APDU. |
| responseApdu | [in] The response data. |
| responseApduLength | [in] The length of the response data. |
| unwrappedResponseApdu | [out] The buffer for the unwrapped response APDU. |
| unwrappedResponseApduLength | [in, out] The available and returned modified length of the unwrappedResponseAPDU buffer. |
| *secInfo | [in] The pointer to the GP211_SECURITY_INFO structure returned by GP211_mutual_authentication(). |
| OPGP_ERROR_STATUS wrap_command | ( | PBYTE | apduCommand, |
| DWORD | apduCommandLength, | ||
| PBYTE | wrappedApduCommand, | ||
| PDWORD | wrappedApduCommandLength, | ||
| GP211_SECURITY_INFO * | secInfo | ||
| ) |
Wraps a command, i.e. encrypts and add the MAC to the APDU with the necessary security information according to secInfo.
Wraps a APDU with the necessary security information according to secInfo. The wrappedapduCommand must be a buffer with enough space for the potential added padding for the encryption and the MAC. The maximum possible extra space to the apduCommandLength is 8 bytes for the MAC plus 7 bytes for padding and one Lc byte in the encryption process.
| apduCommand | [in] The command APDU. |
| apduCommandLength | [in] The length of the command APDU. |
| wrappedApduCommand | [out] The buffer for the wrapped APDU command. |
| wrappedApduCommandLength | [in, out] The available and returned modified length of the wrappedApduCommand buffer. |
| *secInfo | [in] The pointer to the GP211_SECURITY_INFO structure returned by GP211_mutual_authentication(). |