CC26xx Driver Library
pka.c File Reference
#include "pka.h"

Macros

#define MAX(x, y)   (((x) > (y)) ? (x) : (y))
 
#define MIN(x, y)   (((x) < (y)) ? (x) : (y))
 
#define INRANGE(x, y, z)   ((x) > (y) && (x) < (z))
 
#define IS_WORD_ALIGNED(ptr)   (((uintptr_t)(ptr) << 30) == 0U)
 
#define PKA_MAX_CURVE_SIZE_32_BIT_WORD   12
 
#define PKA_MAX_LEN_IN_32_BIT_WORD   PKA_MAX_CURVE_SIZE_32_BIT_WORD
 
#define PKA_NO_POINTER_REG   0xFF
 

Functions

void PKAClearPkaRam (void)
 Zeroizes PKA RAM. More...
 
static uint32_t PKAWritePkaParam (const uint8_t *param, uint32_t paramLength, uint32_t paramOffset, uint32_t ptrRegOffset)
 
static uint32_t PKAWritePkaParamExtraOffset (const uint8_t *param, uint32_t paramLength, uint32_t paramOffset, uint32_t ptrRegOffset)
 
static uint32_t PKAGetBigNumResult (uint8_t *resultBuf, uint32_t *resultLength, uint32_t resultPKAMemAddr)
 
static uint32_t PKAGetBigNumResultRemainder (uint8_t *resultBuf, uint32_t *resultLength, uint32_t resultPKAMemAddr)
 
static uint32_t PKAGetECCResult (uint8_t *curvePointX, uint8_t *curvePointY, uint32_t resultPKAMemAddr, uint32_t length)
 
uint32_t PKAGetOpsStatus (void)
 Gets the PKA operation status. More...
 
bool PKAArrayAllZeros (const uint8_t *array, uint32_t arrayLength)
 Checks whether and array only consists of zeros. More...
 
void PKAZeroOutArray (const uint8_t *array, uint32_t arrayLength)
 Zeros-out an array. More...
 
uint32_t PKABigNumModStart (const uint8_t *bigNum, uint32_t bigNumLength, const uint8_t *modulus, uint32_t modulusLength, uint32_t *resultPKAMemAddr)
 Starts a big number modulus operation. More...
 
uint32_t PKABigNumModGetResult (uint8_t *resultBuf, uint32_t length, uint32_t resultPKAMemAddr)
 Gets the result of the big number modulus operation. More...
 
uint32_t PKABigNumDivideStart (const uint8_t *dividend, uint32_t dividendLength, const uint8_t *divisor, uint32_t divisorLength, uint32_t *resultQuotientMemAddr, uint32_t *resultRemainderMemAddr)
 Starts a big number divide operation. More...
 
uint32_t PKABigNumDivideGetQuotient (uint8_t *resultBuf, uint32_t *length, uint32_t resultQuotientMemAddr)
 Gets the quotient of the big number divide operation. More...
 
uint32_t PKABigNumDivideGetRemainder (uint8_t *resultBuf, uint32_t *length, uint32_t resultQuotientMemAddr)
 Gets the remainder of the big number divide operation. More...
 
uint32_t PKABigNumCmpStart (const uint8_t *bigNum1, const uint8_t *bigNum2, uint32_t length)
 Starts the comparison of two big numbers. More...
 
uint32_t PKABigNumCmpGetResult (void)
 Gets the result of the comparison operation of two big numbers. More...
 
uint32_t PKABigNumInvModStart (const uint8_t *bigNum, uint32_t bigNumLength, const uint8_t *modulus, uint32_t modulusLength, uint32_t *resultPKAMemAddr)
 Starts a big number inverse modulo operation. More...
 
uint32_t PKABigNumInvModGetResult (uint8_t *resultBuf, uint32_t length, uint32_t resultPKAMemAddr)
 Gets the result of the big number inverse modulo operation. More...
 
uint32_t PKABigNumExpModStart (const uint8_t *base, uint32_t baseLength, const uint8_t *exponent, uint32_t exponentLength, const uint8_t *modulus, uint32_t modulusLength, uint32_t *resultPKAMemAddr)
 Starts a big number modular exponentiation operation. More...
 
uint32_t PKABigNumExpModGetResult (uint8_t *resultBuf, uint32_t length, uint32_t resultPKAMemAddr)
 Gets the result of the big number modular exponentiation operation. More...
 
uint32_t PKABigNumMultiplyStart (const uint8_t *multiplicand, uint32_t multiplicandLength, const uint8_t *multiplier, uint32_t multiplierLength, uint32_t *resultPKAMemAddr)
 Starts the multiplication of two big numbers. More...
 
uint32_t PKABigNumMultGetResult (uint8_t *resultBuf, uint32_t *resultLength, uint32_t resultPKAMemAddr)
 Gets the result of the big number multiplication. More...
 
uint32_t PKABigNumAddStart (const uint8_t *bigNum1, uint32_t bigNum1Length, const uint8_t *bigNum2, uint32_t bigNum2Length, uint32_t *resultPKAMemAddr)
 Starts the addition of two big numbers. More...
 
uint32_t PKABigNumSubGetResult (uint8_t *resultBuf, uint32_t *resultLength, uint32_t resultPKAMemAddr)
 Gets the result of the subtraction operation on two big numbers. More...
 
uint32_t PKABigNumSubStart (const uint8_t *minuend, uint32_t minuendLength, const uint8_t *subtrahend, uint32_t subtrahendLength, uint32_t *resultPKAMemAddr)
 Starts the subtraction of one big number from another. More...
 
uint32_t PKABigNumAddGetResult (uint8_t *resultBuf, uint32_t *resultLength, uint32_t resultPKAMemAddr)
 Gets the result of the addition operation on two big numbers. More...
 
uint32_t PKAEccMultiplyStart (const uint8_t *scalar, const uint8_t *curvePointX, const uint8_t *curvePointY, const uint8_t *prime, const uint8_t *a, const uint8_t *b, uint32_t length, uint32_t *resultPKAMemAddr)
 Starts ECC multiplication. More...
 
uint32_t PKAEccMontgomeryMultiplyStart (const uint8_t *scalar, const uint8_t *curvePointX, const uint8_t *prime, const uint8_t *a, uint32_t length, uint32_t *resultPKAMemAddr)
 Starts ECC Montgomery multiplication. More...
 
uint32_t PKAEccMultiplyGetResult (uint8_t *curvePointX, uint8_t *curvePointY, uint32_t resultPKAMemAddr, uint32_t length)
 Gets the result of ECC multiplication. More...
 
uint32_t PKAEccAddStart (const uint8_t *curvePoint1X, const uint8_t *curvePoint1Y, const uint8_t *curvePoint2X, const uint8_t *curvePoint2Y, const uint8_t *prime, const uint8_t *a, uint32_t length, uint32_t *resultPKAMemAddr)
 Starts the ECC addition. More...
 
uint32_t PKAEccAddGetResult (uint8_t *curvePointX, uint8_t *curvePointY, uint32_t resultPKAMemAddr, uint32_t length)
 Gets the result of the ECC addition. More...
 
uint32_t PKAEccVerifyPublicKeyWeierstrassStart (const uint8_t *curvePointX, const uint8_t *curvePointY, const uint8_t *prime, const uint8_t *a, const uint8_t *b, const uint8_t *order, uint32_t length)
 Begins the validation of a public key against a Short-Weierstrass curve. More...
 

Variables

const PKA_EccPoint224 NISTP224_generator
 X coordinate of the generator point of the NISTP224 curve. More...
 
const PKA_EccParam224 NISTP224_prime
 Prime of the NISTP224 curve. More...
 
const PKA_EccParam224 NISTP224_a
 a constant of the NISTP224 curve when expressed in short Weierstrass form (y^3 = x^2 + a*x + b). More...
 
const PKA_EccParam224 NISTP224_b
 b constant of the NISTP224 curve when expressed in short Weierstrass form (y^3 = x^2 + a*x + b). More...
 
const PKA_EccParam224 NISTP224_order
 Order of the NISTP224 curve. More...
 
const PKA_EccPoint256 NISTP256_generator
 X coordinate of the generator point of the NISTP256 curve. More...
 
const PKA_EccParam256 NISTP256_prime
 Prime of the NISTP256 curve. More...
 
const PKA_EccParam256 NISTP256_a
 a constant of the NISTP256 curve when expressed in short Weierstrass form (y^3 = x^2 + a*x + b). More...
 
const PKA_EccParam256 NISTP256_b
 b constant of the NISTP256 curve when expressed in short Weierstrass form (y^3 = x^2 + a*x + b). More...
 
const PKA_EccParam256 NISTP256_order
 Order of the NISTP256 curve. More...
 
const PKA_EccPoint384 NISTP384_generator
 X coordinate of the generator point of the NISTP384 curve. More...
 
const PKA_EccParam384 NISTP384_prime
 Prime of the NISTP384 curve. More...
 
const PKA_EccParam384 NISTP384_a
 a constant of the NISTP384 curve when expressed in short Weierstrass form (y^3 = x^2 + a*x + b). More...
 
const PKA_EccParam384 NISTP384_b
 b constant of the NISTP384 curve when expressed in short Weierstrass form (y^3 = x^2 + a*x + b). More...
 
const PKA_EccParam384 NISTP384_order
 Order of the NISTP384 curve. More...
 
const PKA_EccPoint521 NISTP521_generator
 X coordinate of the generator point of the NISTP521 curve. More...
 
const PKA_EccParam521 NISTP521_prime
 Prime of the NISTP521 curve. More...
 
const PKA_EccParam521 NISTP521_a
 a constant of the NISTP521 curve when expressed in short Weierstrass form (y^3 = x^2 + a*x + b). More...
 
const PKA_EccParam521 NISTP521_b
 b constant of the NISTP521 curve when expressed in short Weierstrass form (y^3 = x^2 + a*x + b). More...
 
const PKA_EccParam521 NISTP521_order
 Order of the NISTP521 curve. More...
 
const PKA_EccPoint256 BrainpoolP256R1_generator
 X coordinate of the generator point of the BrainpoolP256R1 curve. More...
 
const PKA_EccParam256 BrainpoolP256R1_prime
 Prime of the BrainpoolP256R1 curve. More...
 
const PKA_EccParam256 BrainpoolP256R1_a
 a constant of the BrainpoolP256R1 curve when expressed in short Weierstrass form (y^3 = x^2 + a*x + b). More...
 
const PKA_EccParam256 BrainpoolP256R1_b
 b constant of the BrainpoolP256R1 curve when expressed in short Weierstrass form (y^3 = x^2 + a*x + b). More...
 
const PKA_EccParam256 BrainpoolP256R1_order
 Order of the BrainpoolP256R1 curve. More...
 
const PKA_EccPoint384 BrainpoolP384R1_generator
 X coordinate of the generator point of the BrainpoolP384R1 curve. More...
 
const PKA_EccParam384 BrainpoolP384R1_prime
 Prime of the BrainpoolP384R1 curve. More...
 
const PKA_EccParam384 BrainpoolP384R1_a
 a constant of the BrainpoolP384R1 curve when expressed in short Weierstrass form (y^3 = x^2 + a*x + b). More...
 
const PKA_EccParam384 BrainpoolP384R1_b
 b constant of the BrainpoolP384R1 curve when expressed in short Weierstrass form (y^3 = x^2 + a*x + b). More...
 
const PKA_EccParam384 BrainpoolP384R1_order
 Order of the BrainpoolP384R1 curve. More...
 
const PKA_EccPoint512 BrainpoolP512R1_generator
 X coordinate of the generator point of the BrainpoolP512R1 curve. More...
 
const PKA_EccParam512 BrainpoolP512R1_prime
 Prime of the BrainpoolP512R1 curve. More...
 
const PKA_EccParam512 BrainpoolP512R1_a
 a constant of the BrainpoolP512R1 curve when expressed in short Weierstrass form (y^3 = x^2 + a*x + b). More...
 
const PKA_EccParam512 BrainpoolP512R1_b
 b constant of the BrainpoolP512R1 curve when expressed in short Weierstrass form (y^3 = x^2 + a*x + b). More...
 
const PKA_EccParam512 BrainpoolP512R1_order
 Order of the BrainpoolP512R1 curve. More...
 
const PKA_EccPoint256 Curve25519_generator
 X coordinate of the generator point of the Curve25519 curve. More...
 
const PKA_EccParam256 Curve25519_prime
 Prime of the Curve25519 curve. More...
 
const PKA_EccParam256 Curve25519_a
 a constant of the Curve25519 curve when expressed in Montgomery form (By^2 = x^3 + a*x^2 + x). More...
 
const PKA_EccParam256 Curve25519_b
 b constant of the Curve25519 curve when expressed in Montgomery form (By^2 = x^3 + a*x^2 + x). More...
 
const PKA_EccParam256 Curve25519_order
 Order of the Curve25519 curve. More...
 

Macro Definition Documentation

§ INRANGE

#define INRANGE (   x,
  y,
 
)    ((x) > (y) && (x) < (z))

§ IS_WORD_ALIGNED

#define IS_WORD_ALIGNED (   ptr)    (((uintptr_t)(ptr) << 30) == 0U)

Referenced by PKAGetECCResult().

§ MAX

#define MAX (   x,
 
)    (((x) > (y)) ? (x) : (y))

§ MIN

#define MIN (   x,
 
)    (((x) < (y)) ? (x) : (y))

§ PKA_MAX_CURVE_SIZE_32_BIT_WORD

#define PKA_MAX_CURVE_SIZE_32_BIT_WORD   12

§ PKA_MAX_LEN_IN_32_BIT_WORD

#define PKA_MAX_LEN_IN_32_BIT_WORD   PKA_MAX_CURVE_SIZE_32_BIT_WORD

§ PKA_NO_POINTER_REG

#define PKA_NO_POINTER_REG   0xFF

Function Documentation

§ PKAGetBigNumResult()

static uint32_t PKAGetBigNumResult ( uint8_t *  resultBuf,
uint32_t *  resultLength,
uint32_t  resultPKAMemAddr 
)
static

Referenced by PKABigNumAddGetResult(), PKABigNumDivideGetQuotient(), PKABigNumExpModGetResult(), PKABigNumInvModGetResult(), PKABigNumMultGetResult(), and PKABigNumSubGetResult().

631 {
632  uint32_t mswOffset;
633  uint32_t lswOffset;
634  uint32_t lengthInWords;
635  uint32_t i;
636  uint32_t *resultWordAlias = (uint32_t *)resultBuf;
637 
638  // Check the arguments.
639  ASSERT(resultBuf);
640  ASSERT((resultPKAMemAddr > PKA_RAM_BASE) &&
641  (resultPKAMemAddr < (PKA_RAM_BASE + PKA_RAM_TOT_BYTE_SIZE)));
642 
643  // Verify that the operation is complete.
644  if (HWREG(PKA_BASE + PKA_O_FUNCTION) & PKA_FUNCTION_RUN) {
646  }
647 
648  // Get the MSW register value.
649  mswOffset = HWREG(PKA_BASE + PKA_O_MSW);
650 
651  // If the result vector is zero, write back one zero byte so the caller does not need
652  // to handle a special error for the perhaps valid result of zero.
653  // They will only get the error status if they do not provide a buffer
654  if (mswOffset & PKA_MSW_RESULT_IS_ZERO_M) {
655  if (*resultLength){
656  if(resultBuf){
657  resultBuf[0] = 0;
658  }
659 
660  *resultLength = 1;
661 
662  return PKA_STATUS_SUCCESS;
663  }
664  else {
666  }
667  }
668 
669  // Get the length of the result
670  mswOffset = ((mswOffset & PKA_MSW_MSW_ADDRESS_M) + 1);
671  lswOffset = ((resultPKAMemAddr - PKA_RAM_BASE) >> 2);
672 
673  if (mswOffset >= lswOffset) {
674  lengthInWords = mswOffset - lswOffset;
675  }
676  else {
678  }
679 
680  // Check if the provided buffer length is adequate to store the result data.
681  if (*resultLength < lengthInWords * sizeof(uint32_t)) {
683  }
684 
685  // Copy the resultant length.
686  *resultLength = lengthInWords * sizeof(uint32_t);
687 
688 
689  if (resultBuf) {
690  // Copy the result into the resultBuf.
691  for (i = 0; i < lengthInWords; i++) {
692  resultWordAlias[i] = HWREG(resultPKAMemAddr + sizeof(uint32_t) * i);
693  }
694  }
695 
696  return PKA_STATUS_SUCCESS;
697 }
#define PKA_STATUS_BUF_UNDERFLOW
Buffer underflow.
Definition: pka.h:124
#define PKA_STATUS_RESULT_ADDRESS_INCORRECT
The address of the result passed into one of the PKA*GetResult functions is incorrect.
Definition: pka.h:137
#define PKA_STATUS_OPERATION_BUSY
PKA operation is in progress.
Definition: pka.h:129
#define PKA_STATUS_SUCCESS
Success.
Definition: pka.h:121
#define ASSERT(expr)
Definition: debug.h:71

§ PKAGetBigNumResultRemainder()

static uint32_t PKAGetBigNumResultRemainder ( uint8_t *  resultBuf,
uint32_t *  resultLength,
uint32_t  resultPKAMemAddr 
)
static

Referenced by PKABigNumDivideGetRemainder(), and PKABigNumModGetResult().

705 {
706  uint32_t regMSWVal;
707  uint32_t lengthInWords;
708  uint32_t i;
709  uint32_t *resultWordAlias = (uint32_t *)resultBuf;
710 
711  // Check the arguments.
712  ASSERT(resultBuf);
713  ASSERT((resultPKAMemAddr > PKA_RAM_BASE) &&
714  (resultPKAMemAddr < (PKA_RAM_BASE + PKA_RAM_TOT_BYTE_SIZE)));
715 
716  // Verify that the operation is complete.
717  if (HWREG(PKA_BASE + PKA_O_FUNCTION) & PKA_FUNCTION_RUN) {
719  }
720 
721  // Get the MSW register value.
722  regMSWVal = HWREG(PKA_BASE + PKA_O_DIVMSW);
723 
724  // If the result vector is zero, write back one zero byte so the caller does not need
725  // to handle a special error for the perhaps valid result of zero.
726  // They will only get the error status if they do not provide a buffer
727  if (regMSWVal & PKA_DIVMSW_RESULT_IS_ZERO_M) {
728  if (*resultLength){
729  if(resultBuf){
730  resultBuf[0] = 0;
731  }
732 
733  *resultLength = 1;
734 
735  return PKA_STATUS_SUCCESS;
736  }
737  else {
739  }
740  }
741 
742  // Get the length of the result
743  lengthInWords = ((regMSWVal & PKA_DIVMSW_MSW_ADDRESS_M) + 1) - ((resultPKAMemAddr - PKA_RAM_BASE) >> 2);
744 
745  // Check if the provided buffer length is adequate to store the result data.
746  if (*resultLength < lengthInWords * sizeof(uint32_t)) {
748  }
749 
750  // Copy the resultant length.
751  *resultLength = lengthInWords * sizeof(uint32_t);
752 
753  if (resultBuf) {
754  // Copy the result into the resultBuf.
755  for (i = 0; i < lengthInWords; i++) {
756  resultWordAlias[i] = HWREG(resultPKAMemAddr + sizeof(uint32_t) * i);
757  }
758  }
759 
760  return PKA_STATUS_SUCCESS;
761 }
#define PKA_STATUS_BUF_UNDERFLOW
Buffer underflow.
Definition: pka.h:124
#define PKA_STATUS_OPERATION_BUSY
PKA operation is in progress.
Definition: pka.h:129
#define PKA_STATUS_SUCCESS
Success.
Definition: pka.h:121
#define ASSERT(expr)
Definition: debug.h:71

§ PKAGetECCResult()

static uint32_t PKAGetECCResult ( uint8_t *  curvePointX,
uint8_t *  curvePointY,
uint32_t  resultPKAMemAddr,
uint32_t  length 
)
static

Referenced by PKAEccAddGetResult(), and PKAEccMultiplyGetResult().

769 {
770  uint32_t i = 0;
771  uint32_t lengthInWordsCeiling = 0;
772  uint32_t tempWord = 0;
773  uint_fast8_t j = 0;
774  uint32_t *xWordAlias = (uint32_t *)curvePointX;
775  uint32_t *yWordAlias = (uint32_t *)curvePointY;
776 
777  // Check for the arguments.
778  ASSERT(curvePointX);
779  ASSERT(curvePointY);
780  ASSERT((resultPKAMemAddr > PKA_RAM_BASE) &&
781  (resultPKAMemAddr < (PKA_RAM_BASE + PKA_RAM_TOT_BYTE_SIZE)));
782 
783  // Verify that the operation is completed.
784  if (HWREG(PKA_BASE + PKA_O_FUNCTION) & PKA_FUNCTION_RUN) {
786  }
787 
788  if (HWREG(PKA_BASE + PKA_O_SHIFT)) {
789  return PKA_STATUS_FAILURE;
790  }
791 
792  // Check to make sure that the result vector is not the point at infinity.
793  if (HWREG(PKA_BASE + PKA_O_MSW) & PKA_MSW_RESULT_IS_ZERO) {
795  }
796 
797  if (curvePointX != NULL) {
798  // Copy the x coordinate value of the result from vector D into
799  // the curvePoint.
800  for (i = 0; i < (length / sizeof(uint32_t)); i++) {
801  // Check for word aligned address in x coordinate buffer
802  if (IS_WORD_ALIGNED(curvePointX)) {
803  // Copy x coordinate as word
804  xWordAlias[i] = HWREG(resultPKAMemAddr + sizeof(uint32_t) * i);
805  }
806  else {
807  // Copy x Coordinate as bytes
808  // Temporarily load the entire word line of the coordinate
809  tempWord = HWREG(resultPKAMemAddr + sizeof(tempWord) * i);
810 
811  // Write the bytes to the X coordinate
812  for (j = 0; j < sizeof(tempWord); j++) {
813  curvePointX[i * sizeof(tempWord) + j] = ((uint8_t *)&tempWord)[j];
814  }
815  }
816  }
817 
818  // If the length is not a word-multiple, fill up a temporary word and copy that in
819  // to avoid a bus error.
820  if (length % sizeof(uint32_t)) {
821  // Load the entire word line of the coordinate remainder
822  tempWord = HWREG(resultPKAMemAddr + sizeof(tempWord) * i);
823 
824  // Write all remaining bytes to the coordinate
825  for (j = 0; j < length % sizeof(uint32_t); j++) {
826  curvePointX[i * sizeof(uint32_t) + j] = ((uint8_t *)&tempWord)[j];
827  }
828 
829  }
830  }
831 
832  lengthInWordsCeiling = (length % sizeof(uint32_t)) ? length / sizeof(uint32_t) + 1 : length / sizeof(uint32_t);
833 
834  resultPKAMemAddr += sizeof(uint32_t) * (2 + lengthInWordsCeiling + (lengthInWordsCeiling % 2));
835 
836  if (curvePointY != NULL) {
837  // Copy the y coordinate value of the result from vector D into
838  // the curvePoint.
839  for (i = 0; i < (length / sizeof(uint32_t)); i++) {
840  // Check for word aligned address in y coordinate buffer
841  if (IS_WORD_ALIGNED(curvePointY)) {
842  // Copy y coordinate as word
843  yWordAlias[i] = HWREG(resultPKAMemAddr + sizeof(uint32_t) * i);
844  }
845  else {
846  // Copy y Coordinate as bytes
847  // Temporarily load the entire word line of the coordinate
848  tempWord = HWREG(resultPKAMemAddr + sizeof(tempWord) * i);
849 
850  // Write the bytes to the Y coordinate
851  for (j = 0; j < sizeof(tempWord); j++) {
852  curvePointY[i * sizeof(tempWord) + j] = ((uint8_t *)&tempWord)[j];
853  }
854  }
855  }
856 
857  // If the length is not a word-multiple, fill up a temporary word and copy that in
858  // to avoid a bus error.
859  if (length % sizeof(uint32_t)) {
860  // Load the entire word line of the coordinate remainder
861  tempWord = HWREG(resultPKAMemAddr + sizeof(tempWord) * i);
862 
863  // Write all remaining bytes to the coordinate
864  for (j = 0; j < length % sizeof(uint32_t); j++) {
865  curvePointY[i * sizeof(uint32_t) + j] = ((uint8_t *)&tempWord)[j];
866  }
867  }
868  }
869 
870 
871  return PKA_STATUS_SUCCESS;
872 }
#define PKA_STATUS_FAILURE
Failure.
Definition: pka.h:122
#define PKA_STATUS_OPERATION_BUSY
PKA operation is in progress.
Definition: pka.h:129
#define IS_WORD_ALIGNED(ptr)
Definition: pka.c:108
#define PKA_STATUS_SUCCESS
Success.
Definition: pka.h:121
#define ASSERT(expr)
Definition: debug.h:71
#define PKA_STATUS_POINT_AT_INFINITY
The ECC operation resulted in the point at infinity.
Definition: pka.h:138

§ PKAWritePkaParam()

static uint32_t PKAWritePkaParam ( const uint8_t *  param,
uint32_t  paramLength,
uint32_t  paramOffset,
uint32_t  ptrRegOffset 
)
static

Referenced by PKABigNumAddStart(), PKABigNumCmpStart(), PKABigNumDivideStart(), PKABigNumExpModStart(), PKABigNumInvModStart(), PKABigNumModStart(), PKABigNumMultiplyStart(), PKABigNumSubStart(), PKAEccMontgomeryMultiplyStart(), PKAEccMultiplyStart(), and PKAWritePkaParamExtraOffset().

550 {
551  uint32_t i;
552  uint32_t *paramWordAlias = (uint32_t *)param;
553  // Take the floor of paramLength in 32-bit words
554  uint32_t paramLengthInWords = paramLength / sizeof(uint32_t);
555 
556  // Only copy data if it is specified. We may wish to simply allocate another buffer and get
557  // the required offset.
558  if (param) {
559  // Load the number in PKA RAM
560  for (i = 0; i < paramLengthInWords; i++) {
561  HWREG(PKA_RAM_BASE + paramOffset + sizeof(uint32_t) * i) = paramWordAlias[i];
562  }
563 
564  // If the length is not a word-multiple, fill up a temporary word and copy that in
565  // to avoid a bus error. The extra zeros at the end should not matter, as the large
566  // number is little-endian and thus has no effect.
567  // We could have correctly calculated ceiling(paramLength / sizeof(uint32_t)) above.
568  // However, we would not have been able to zero-out the extra few most significant
569  // bytes of the most significant word. That would have resulted in doing maths operations
570  // on whatever follows param in RAM.
571  if (paramLength % sizeof(uint32_t)) {
572  uint32_t temp = 0;
573  uint8_t j;
574 
575  // Load the entire word line of the param remainder
576  temp = paramWordAlias[i];
577 
578  // Zero-out all bytes beyond the end of the param
579  for (j = paramLength % sizeof(uint32_t); j < sizeof(uint32_t); j++) {
580  ((uint8_t *)&temp)[j] = 0;
581  }
582 
583  HWREG(PKA_RAM_BASE + paramOffset + sizeof(uint32_t) * i) = temp;
584 
585  // Increment paramLengthInWords since we take the ceiling of length / sizeof(uint32_t)
586  paramLengthInWords++;
587  }
588  }
589 
590  // Update the A, B, C, or D pointer with the offset address of the PKA RAM location
591  // where the number will be stored.
592  switch (ptrRegOffset) {
593  case PKA_O_APTR:
594  HWREG(PKA_BASE + PKA_O_APTR) = paramOffset >> 2;
595  HWREG(PKA_BASE + PKA_O_ALENGTH) = paramLengthInWords;
596  break;
597  case PKA_O_BPTR:
598  HWREG(PKA_BASE + PKA_O_BPTR) = paramOffset >> 2;
599  HWREG(PKA_BASE + PKA_O_BLENGTH) = paramLengthInWords;
600  break;
601  case PKA_O_CPTR:
602  HWREG(PKA_BASE + PKA_O_CPTR) = paramOffset >> 2;
603  break;
604  case PKA_O_DPTR:
605  HWREG(PKA_BASE + PKA_O_DPTR) = paramOffset >> 2;
606  break;
607  }
608 
609  // Ensure 8-byte alignment of next parameter.
610  // Returns the offset for the next parameter.
611  return paramOffset + sizeof(uint32_t) * (paramLengthInWords + (paramLengthInWords % 2));
612 }

§ PKAWritePkaParamExtraOffset()

static uint32_t PKAWritePkaParamExtraOffset ( const uint8_t *  param,
uint32_t  paramLength,
uint32_t  paramOffset,
uint32_t  ptrRegOffset 
)
static

Referenced by PKABigNumDivideStart(), PKABigNumExpModStart(), PKABigNumModStart(), PKAEccAddStart(), PKAEccMontgomeryMultiplyStart(), and PKAEccMultiplyStart().

620 {
621  // Ensure 16-byte alignment.
622  return (sizeof(uint32_t) * 2) + PKAWritePkaParam(param, paramLength, paramOffset, ptrRegOffset);
623 }
static uint32_t PKAWritePkaParam(const uint8_t *param, uint32_t paramLength, uint32_t paramOffset, uint32_t ptrRegOffset)
Definition: pka.c:549
Here is the call graph for this function: