Mistake on this page? Email us
pal_plat_Crypto.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright 2016, 2017 ARM Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *******************************************************************************/
16 
17 #ifndef _PAL_PLAT_CRYPTO_H_
18 #define _PAL_PLAT_CRYPTO_H_
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 #include "pal.h"
39 
46 
54 
62 
72 palStatus_t pal_plat_setAesKey(palAesHandle_t aes, const unsigned char* key, uint32_t keybits, palAesKeyType_t keyTarget);
73 
88 palStatus_t pal_plat_aesCTR(palAesHandle_t aes, const unsigned char* input, unsigned char* output, size_t inLen, unsigned char iv[16], bool zeroOffset);
89 
99 palStatus_t pal_plat_aesECB(palAesHandle_t aes, const unsigned char input[PAL_CRYPT_BLOCK_SIZE], unsigned char output[PAL_CRYPT_BLOCK_SIZE], palAesMode_t mode);
100 
109 palStatus_t pal_plat_sha256(const unsigned char* input, size_t inLen, unsigned char* output);
110 
118 
127 palStatus_t pal_plat_x509CertParse(palX509Handle_t x509, const unsigned char* input, size_t inLen);
128 
139 palStatus_t pal_plat_x509CertGetAttribute(palX509Handle_t x509Cert, palX509Attr_t attr, void* output, size_t outLenBytes, size_t* actualOutLenBytes);
140 
152 palStatus_t pal_plat_x509CertVerifyExtended(palX509Handle_t x509Cert, palX509Handle_t x509CertChain, int32_t* verifyResult);
153 
163 
171 
180 
189 palStatus_t pal_plat_mdUpdate(palMDHandle_t md, const unsigned char* input, size_t inLen);
190 
199 palStatus_t pal_plat_mdGetOutputSize(palMDHandle_t md, size_t* bufferSize);
200 
208 palStatus_t pal_plat_mdFinal(palMDHandle_t md, unsigned char* output);
209 
217 
229 palStatus_t pal_plat_verifySignature(palX509Handle_t x509, palMDType_t mdType, const unsigned char *hash, size_t hashLen, const unsigned char *sig, size_t sigLen );
230 
241 palStatus_t pal_plat_ASN1GetTag(unsigned char **position, const unsigned char *end, size_t *len, uint8_t tag );
242 
250 
258 
268 palStatus_t pal_plat_CCMSetKey(palCCMHandle_t ctx, palCipherID_t id, const unsigned char *key, unsigned int keybits);
269 
285 palStatus_t pal_plat_CCMDecrypt(palCCMHandle_t ctx, unsigned char* input, size_t inLen, unsigned char* iv, size_t ivLen, unsigned char* add, size_t addLen, unsigned char* tag, size_t tagLen, unsigned char* output);
286 
302 palStatus_t pal_plat_CCMEncrypt(palCCMHandle_t ctx, unsigned char* input, size_t inLen, unsigned char* iv, size_t ivLen, unsigned char* add, size_t addLen, unsigned char* output, unsigned char* tag, size_t tagLen);
303 
311 
319 
331 
340 palStatus_t pal_plat_CtrDRBGSeed(palCtrDrbgCtxHandle_t ctx, const void* seed, size_t len);
341 
350 palStatus_t pal_plat_CtrDRBGGenerate(palCtrDrbgCtxHandle_t ctx, unsigned char* out, size_t len);
351 
362 palStatus_t pal_plat_CtrDRBGGenerateWithAdditional(palCtrDrbgCtxHandle_t ctx, unsigned char* out, size_t len, unsigned char* additional, size_t additionalLen);
363 
364 #if PAL_CMAC_SUPPORT
365 
377 palStatus_t pal_plat_cipherCMAC(const unsigned char *key, size_t keyLenInBits, const unsigned char *input, size_t inputLenInBytes, unsigned char *output);
378 
388 palStatus_t pal_plat_CMACStart(palCMACHandle_t *ctx, const unsigned char *key, size_t keyLenBits, palCipherID_t cipherID);
389 
398 palStatus_t pal_plat_CMACUpdate(palCMACHandle_t ctx, const unsigned char *input, size_t inLen);
399 
408 palStatus_t pal_plat_CMACFinish(palCMACHandle_t *ctx, unsigned char *output, size_t* outLen);
409 
410 #endif //PAL_CMAC_SUPPORT
411 
424 palStatus_t pal_plat_mdHmacSha256(const unsigned char *key, size_t keyLenInBytes, const unsigned char *input, size_t inputLenInBytes, unsigned char *output, size_t* outputLenInBytes);
425 
436 palStatus_t pal_plat_ECCheckKey(palCurveHandle_t grp, palECKeyHandle_t key, uint32_t type, bool *verified);
437 
445 
453 
461 palStatus_t pal_plat_newKeyHandle( palKeyHandle_t *keyHandle, size_t key_size);
462 
470 
471 
480 palStatus_t pal_plat_parseECPrivateKeyFromDER(const unsigned char* prvDERKey, size_t keyLen, palECKeyHandle_t key);
481 
490 palStatus_t pal_plat_parseECPublicKeyFromDER(const unsigned char* pubDERKey, size_t keyLen, palECKeyHandle_t key);
491 
508 
518 palStatus_t pal_plat_writePrivateKeyToDer(palECKeyHandle_t key, unsigned char* derBuffer, size_t bufferSize, size_t* actualSize);
519 
529 palStatus_t pal_plat_writePublicKeyToDer(palECKeyHandle_t key, unsigned char* derBuffer, size_t bufferSize, size_t* actualSize);
530 
540 
549 
557 
565 palStatus_t pal_plat_x509CSRSetSubject(palx509CSRHandle_t x509CSR, const char* subjectName);
566 
575 
586 
595 
604 
615 palStatus_t pal_plat_x509CSRSetExtension(palx509CSRHandle_t x509CSR,const char* oid, size_t oidLen, const unsigned char* value, size_t valueLen);
616 
626 palStatus_t pal_plat_x509CSRWriteDER(palx509CSRHandle_t x509CSR, unsigned char* derBuf, size_t derBufLen, size_t* actualDerLen);
627 
638 palStatus_t pal_plat_x509CSRFromCertWriteDER(palX509Handle_t x509Cert, palx509CSRHandle_t x509CSR, unsigned char* derBuf, size_t derBufLen, size_t* actualDerBufLen);
639 
652 palStatus_t pal_plat_x509CertGetHTBS(palX509Handle_t x509Cert, palMDType_t hash_type, unsigned char* output, size_t outLenBytes, size_t* actualOutLenBytes);
653 
661 
671 palStatus_t pal_plat_ECDHComputeKey(const palCurveHandle_t grp, const palECKeyHandle_t peerPublicKey, const palECKeyHandle_t privateKey, palECKeyHandle_t outKey);
672 
686  const uint8_t *derPeerPublicKey,
687  size_t derPeerPublicKeySize,
688  const palECKeyHandle_t privateKeyHandle,
689  unsigned char *rawSharedSecretOut,
690  size_t rawSharedSecretMaxSize,
691  size_t *rawSharedSecretActSizeOut);
692 
693 
705 palStatus_t pal_plat_ECDSASign(palCurveHandle_t grp, palMDType_t mdType, palECKeyHandle_t prvKey, unsigned char* dgst, uint32_t dgstLen, unsigned char *sig, size_t *sigLen);
706 
719 palStatus_t pal_plat_ECDSAVerify(palECKeyHandle_t pubKey, unsigned char* dgst, uint32_t dgstLen, unsigned char* sig, size_t sigLen, bool* verified);
720 
732  const unsigned char *rawSignature,
733  size_t rawSignatureSize,
734  unsigned char *derSignatureOut,
735  size_t derSignatureMaxSize,
736  size_t *derSignatureActSizeOut);
737 
752 palStatus_t pal_plat_asymmetricSign(const palECKeyHandle_t privateKeyHandle, palMDType_t mdType, const unsigned char *hash, size_t hashSize, unsigned char *outSignature, size_t maxSignatureSize, size_t *actualOutSignatureSize);
753 
765 palStatus_t pal_plat_asymmetricVerify(const palECKeyHandle_t publicKeyHanlde, palMDType_t mdType, const unsigned char *hash, size_t hashSize, const unsigned char *signature, size_t signatureSize);
766 
774 
783 
784 #ifdef __cplusplus
785 }
786 #endif
787 #endif //_PAL_PLAT_CRYPTO_H_
palStatus_t pal_plat_aesECB(palAesHandle_t aes, const unsigned char input[PAL_CRYPT_BLOCK_SIZE], unsigned char output[PAL_CRYPT_BLOCK_SIZE], palAesMode_t mode)
Use AES-ECB encryption or decryption on a block.
enum palGroupIndex palGroupIndex_t
Supported curves.
enum palMDType palMDType_t
Message digest algorithms supported by PAL.
PAL. This file contains the general API to initiate and destroy the PAL component. This is part of the PAL service API.
palStatus_t pal_plat_convertRawSignatureToDer(const unsigned char *rawSignature, size_t rawSignatureSize, unsigned char *derSignatureOut, size_t derSignatureMaxSize, size_t *derSignatureActSizeOut)
Convert ECDSA signature in RAW format to DER format.
palStatus_t pal_plat_x509CertGetHTBS(palX509Handle_t x509Cert, palMDType_t hash_type, unsigned char *output, size_t outLenBytes, size_t *actualOutLenBytes)
Calculate the hash of the To Be Signed (TBS) part of an X.509 certificate.
enum palAesKeyType palAesKeyType_t
Key types to be set to the AES engine.
palStatus_t pal_plat_x509CSRSetExtendedKeyUsage(palx509CSRHandle_t x509CSR, uint32_t extKeyUsage)
Set flags for extended key usage extension.
palStatus_t pal_plat_mdUpdate(palMDHandle_t md, const unsigned char *input, size_t inLen)
Generic message digest (MD) process buffer.
palStatus_t pal_plat_CtrDRBGFree(palCtrDrbgCtxHandle_t *ctx)
Destroys a Counter mode Deterministic Random Byte Generation (CTR-DRBG) context.
palStatus_t pal_plat_x509CSRInit(palx509CSRHandle_t *x509CSR)
Allocate and initialize the X.509 certificate signing request (CSR) context.
palStatus_t pal_plat_CCMSetKey(palCCMHandle_t ctx, palCipherID_t id, const unsigned char *key, unsigned int keybits)
Set the CCM key.
#define PAL_CRYPT_BLOCK_SIZE
Definition: pal_Crypto.h:92
palStatus_t pal_plat_ECDHKeyAgreement(const uint8_t *derPeerPublicKey, size_t derPeerPublicKeySize, const palECKeyHandle_t privateKeyHandle, unsigned char *rawSharedSecretOut, size_t rawSharedSecretMaxSize, size_t *rawSharedSecretActSizeOut)
Compute the raw shared secret using elliptic curve Diffie�Hellman.
palStatus_t pal_plat_CtrDRBGGenerate(palCtrDrbgCtxHandle_t ctx, unsigned char *out, size_t len)
Generate a random value using a Counter mode Deterministic Random Byte Generation (CTR-DRBG) context...
palStatus_t pal_plat_x509CSRWriteDER(palx509CSRHandle_t x509CSR, unsigned char *derBuf, size_t derBufLen, size_t *actualDerLen)
Write a CSR to a DER structure.
palStatus_t pal_plat_initAes(palAesHandle_t *aes)
Initialize an AES context.
palStatus_t pal_plat_ECKeyNew(palECKeyHandle_t *key)
Allocate key context and initialize a key pair as an invalid pair.
palStatus_t pal_plat_CCMFree(palCCMHandle_t *ctx)
Destroy a CCM context.
palStatus_t pal_plat_sha256(const unsigned char *input, size_t inLen, unsigned char *output)
Process SHA-256 over the input buffer.
palStatus_t pal_plat_asymmetricSign(const palECKeyHandle_t privateKeyHandle, palMDType_t mdType, const unsigned char *hash, size_t hashSize, unsigned char *outSignature, size_t maxSignatureSize, size_t *actualOutSignatureSize)
Compute the ECDSA raw signature of a previously hashed message.
palStatus_t pal_plat_x509CertGetAttribute(palX509Handle_t x509Cert, palX509Attr_t attr, void *output, size_t outLenBytes, size_t *actualOutLenBytes)
Get attributes from the parsed certificate.
enum palX509Attr palX509Attr_t
Attributes to be retrieved from the X.509 certificate.
palStatus_t pal_plat_x509CSRSetKey(palx509CSRHandle_t x509CSR, palECKeyHandle_t pubKey, palECKeyHandle_t prvKey)
Set the key for a CSR.
palStatus_t pal_plat_mdInit(palMDHandle_t *md, palMDType_t mdType)
Initialize an message digest (MD) context and set up the required data according to the given algorit...
palStatus_t pal_plat_ECDSAVerify(palECKeyHandle_t pubKey, unsigned char *dgst, uint32_t dgstLen, unsigned char *sig, size_t sigLen, bool *verified)
Verify the ECDSA signature of a previously hashed message.
palStatus_t pal_plat_mdFinal(palMDHandle_t md, unsigned char *output)
Generic message digest (MD) final digest.
palStatus_t pal_plat_writePrivateKeyToDer(palECKeyHandle_t key, unsigned char *derBuffer, size_t bufferSize, size_t *actualSize)
Encode the given private key from the key handle to the DER buffer.
palStatus_t pal_plat_ECKeyGenerateKey(palGroupIndex_t grpID, palECKeyHandle_t key)
Generate a curve ID for a keypair.
palStatus_t pal_plat_ECGroupInitAndLoad(palCurveHandle_t *grp, palGroupIndex_t index)
Initialize an ECP group and set it using well-known domain parameters.
palStatus_t pal_plat_ECKeyGetCurve(palECKeyHandle_t key, palGroupIndex_t *grpID)
Retrieve the curve ID, if it exists, from the given key.
uintptr_t palAesHandle_t
Definition: pal_Crypto.h:37
palStatus_t pal_plat_x509CSRSetExtension(palx509CSRHandle_t x509CSR, const char *oid, size_t oidLen, const unsigned char *value, size_t valueLen)
Generic function to add to the CSR.
palStatus_t pal_plat_x509CSRSetSubject(palx509CSRHandle_t x509CSR, const char *subjectName)
Set the subject name for a certificate signing request (CSR). The subject name should contain a comma...
palStatus_t pal_plat_ASN1GetTag(unsigned char **position, const unsigned char *end, size_t *len, uint8_t tag)
Check for a specific tag. Updates the pointer to immediately after the tag and length.
palStatus_t pal_plat_setAesKey(palAesHandle_t aes, const unsigned char *key, uint32_t keybits, palAesKeyType_t keyTarget)
Set an AES key context for encryption or decryption.
palStatus_t pal_plat_initCrypto(void)
Initiate the Crypto library.
palStatus_t pal_plat_CtrDRBGInit(palCtrDrbgCtxHandle_t *ctx)
Initializes a Counter mode Deterministic Random Byte Generation (CTR-DRBG) context.
palStatus_t pal_plat_x509Free(palX509Handle_t *x509)
Deallocate all certificate data.
uintptr_t palMDHandle_t
Definition: pal_Crypto.h:39
palStatus_t pal_plat_mdFree(palMDHandle_t *md)
Free and clear the message digest (MD) context.
enum palCipherID palCipherID_t
palStatus_t pal_plat_parseECPublicKeyFromDER(const unsigned char *pubDERKey, size_t keyLen, palECKeyHandle_t key)
Parse a DER encoded public key.
palStatus_t pal_plat_writePublicKeyToDer(palECKeyHandle_t key, unsigned char *derBuffer, size_t bufferSize, size_t *actualSize)
Encode the given public key from the key handle to the DER buffer.
palStatus_t pal_plat_ECDHComputeKey(const palCurveHandle_t grp, const palECKeyHandle_t peerPublicKey, const palECKeyHandle_t privateKey, palECKeyHandle_t outKey)
Compute a shared secret.
palStatus_t pal_plat_x509CertParse(palX509Handle_t x509, const unsigned char *input, size_t inLen)
Parse one or more certificates and add them to the chained list.
palStatus_t pal_plat_ECDSASign(palCurveHandle_t grp, palMDType_t mdType, palECKeyHandle_t prvKey, unsigned char *dgst, uint32_t dgstLen, unsigned char *sig, size_t *sigLen)
Compute the ECDSA signature of a previously hashed message.
uintptr_t palX509Handle_t
Definition: pal_Crypto.h:38
palStatus_t pal_plat_ECCheckKey(palCurveHandle_t grp, palECKeyHandle_t key, uint32_t type, bool *verified)
Check that a private or public key is a valid key and the public key is on this curve.
palStatus_t pal_plat_x509Initiate(palX509Handle_t *x509)
Initialize a certificate chain context.
palStatus_t pal_plat_ECKeyFree(palECKeyHandle_t *key)
Free the components of a key pair.
palStatus_t pal_plat_asymmetricVerify(const palECKeyHandle_t publicKeyHanlde, palMDType_t mdType, const unsigned char *hash, size_t hashSize, const unsigned char *signature, size_t signatureSize)
Verify the ECDSA raw signature of a previously hashed message.
uintptr_t palCMACHandle_t
Definition: pal_Crypto.h:41
palStatus_t pal_plat_x509CertCheckExtendedKeyUsage(palX509Handle_t x509Cert, palExtKeyUsage_t usage)
palStatus_t pal_plat_x509CSRSetMD(palx509CSRHandle_t x509CSR, palMDType_t mdType)
Set the message digest (MD) algorithm to use for the signature.
palStatus_t pal_plat_CtrDRBGSeed(palCtrDrbgCtxHandle_t ctx, const void *seed, size_t len)
Set the initial seed for a Counter mode Deterministic Random Byte Generation (CTR-DRBG) context...
palStatus_t pal_plat_newKeyHandle(palKeyHandle_t *keyHandle, size_t key_size)
Initializes a handle to key according to its size.
uintptr_t palCtrDrbgCtxHandle_t
Definition: pal_Crypto.h:42
palStatus_t pal_plat_CCMEncrypt(palCCMHandle_t ctx, unsigned char *input, size_t inLen, unsigned char *iv, size_t ivLen, unsigned char *add, size_t addLen, unsigned char *output, unsigned char *tag, size_t tagLen)
Apply CCM encryption on a buffer.
palStatus_t pal_plat_x509CSRFromCertWriteDER(palX509Handle_t x509Cert, palx509CSRHandle_t x509CSR, unsigned char *derBuf, size_t derBufLen, size_t *actualDerBufLen)
Write a CSR from a given X.509 Certificate.
palStatus_t pal_plat_x509CSRSetKeyUsage(palx509CSRHandle_t x509CSR, uint32_t keyUsage)
Set flags for key usage extension.
palStatus_t pal_plat_freeKeyHandle(palKeyHandle_t *keyHandle)
frees a a key handle.
palStatus_t pal_plat_ECGroupFree(palCurveHandle_t *grp)
Free the components of an ECP group.
palStatus_t pal_plat_CCMDecrypt(palCCMHandle_t ctx, unsigned char *input, size_t inLen, unsigned char *iv, size_t ivLen, unsigned char *add, size_t addLen, unsigned char *tag, size_t tagLen, unsigned char *output)
Apply authenticated CCM decryption on a buffer.
palStatus_t pal_plat_verifySignature(palX509Handle_t x509, palMDType_t mdType, const unsigned char *hash, size_t hashLen, const unsigned char *sig, size_t sigLen)
Verify the signature.
uintptr_t palCurveHandle_t
Definition: pal_Crypto.h:43
palStatus_t pal_plat_x509CSRFree(palx509CSRHandle_t *x509CSR)
Free the X.509 CSR context.
palStatus_t pal_plat_cleanupCrypto(void)
Free resources for the Crypto library.
uintptr_t palx509CSRHandle_t
Definition: pal_Crypto.h:47
palStatus_t pal_plat_x509CertVerifyExtended(palX509Handle_t x509Cert, palX509Handle_t x509CertChain, int32_t *verifyResult)
Verify one or more X.509 DER formatted certificates.
palStatus_t pal_plat_CtrDRBGGenerateWithAdditional(palCtrDrbgCtxHandle_t ctx, unsigned char *out, size_t len, unsigned char *additional, size_t additionalLen)
Generate a random value with additional input using a Counter mode Deterministic Random Byte Generati...
palStatus_t pal_plat_mdHmacSha256(const unsigned char *key, size_t keyLenInBytes, const unsigned char *input, size_t inputLenInBytes, unsigned char *output, size_t *outputLenInBytes)
Apply a one-shot Message Digest HMAC cipher.
palStatus_t pal_plat_aesCTR(palAesHandle_t aes, const unsigned char *input, unsigned char *output, size_t inLen, unsigned char iv[16], bool zeroOffset)
Use AES-CTR encryption or decryption on a buffer.
palStatus_t pal_plat_parseECPublicKeyFromHandle(const palKeyHandle_t pubKeyHandle, palECKeyHandle_t ECKeyHandle)
Parse a public key.
palStatus_t pal_plat_freeAes(palAesHandle_t *aes)
Free an AES context.
uintptr_t palCCMHandle_t
Definition: pal_Crypto.h:40
palStatus_t pal_plat_CtrDRBGIsSeeded(palCtrDrbgCtxHandle_t ctx)
Check whether a Counter mode Deterministic Random Byte Generator (CTR-DRBG) context is seeded...
enum palExtKeyUsage palExtKeyUsage_t
Extended key usage options.
palStatus_t pal_plat_parseECPrivateKeyFromDER(const unsigned char *prvDERKey, size_t keyLen, palECKeyHandle_t key)
Parse a DER encoded private key.
palStatus_t pal_plat_mdGetOutputSize(palMDHandle_t md, size_t *bufferSize)
Generic message digest (MD) output buffer size getter.
palStatus_t pal_plat_parseECPrivateKeyFromHandle(const palKeyHandle_t prvKeyHandle, palECKeyHandle_t ECKeyHandle)
Parse a private key.
int32_t palStatus_t
Definition: pal_types.h:49
uintptr_t palKeyHandle_t
Definition: pal_Crypto.h:48
enum palAesMode palAesMode_t
AES mode for ECB encryption and decryption.
palStatus_t pal_plat_CCMInit(palCCMHandle_t *ctx)
Initialize a CCM context.
uintptr_t palECKeyHandle_t
Definition: pal_Crypto.h:45