@@ -648,6 +648,21 @@ static uint8_t getCurveType(int curve_id)
648648 }
649649}
650650#endif /* WOLFSSL_MICROCHIP_TA100 */
651+
652+ #ifdef WOLFSSL_MICROCHIP_TA100
653+ static int getCurveSizeBytes (int curve_id )
654+ {
655+ switch (curve_id ) {
656+ case ECC_SECP224R1 : return 28 ;
657+ case ECC_SECP256R1 : return 32 ;
658+ case ECC_SECP384R1 : return 48 ;
659+ case ECC_SECP256K1 : return 32 ;
660+ case ECC_BRAINPOOLP256R1 : return 32 ;
661+ case ECC_CURVE_DEF : return 32 ;
662+ default : return -1 ;
663+ }
664+ }
665+ #endif /* WOLFSSL_MICROCHIP_TA100 */
651666int atmel_ecc_create_key (int slotId , int curve_id , byte * peerKey )
652667{
653668 int ret ;
@@ -665,9 +680,55 @@ int atmel_ecc_create_key(int slotId, int curve_id, byte* peerKey)
665680
666681#endif
667682 /* generate new ephemeral key on device */
683+ #ifdef WOLFSSL_MICROCHIP_TA100
684+ #if defined(TA100_ECC_TRACE )
685+ printf ("[TA100] atmel_ecc_create_key: slot=%d curve_id=%d curve_size=%d curve_type=%d\r\n" ,
686+ slotId , curve_id , getCurveSizeBytes (curve_id ), getCurveType (curve_id ));
687+ #endif
688+ {
689+ ATCA_STATUS status ;
690+ ta_element_attributes_t key_attr ;
691+ uint8_t is_valid = 0 ;
692+ int curve_size = getCurveSizeBytes (curve_id );
693+ int curve_type = getCurveType (curve_id );
694+ size_t pubkey_len = (size_t )(curve_size * 2 );
695+
696+ if (curve_size <= 0 || curve_type == MICROCHIP_INVALID_ECC )
697+ return NOT_COMPILED_IN ;
698+
699+ status = talib_is_handle_valid (atcab_get_device (),
700+ (uint32_t )MAP_TO_HANDLE (slotId ), & is_valid );
701+ if (status == ATCA_SUCCESS && is_valid == 0x01 ) {
702+ status = talib_delete_handle (atcab_get_device (),
703+ (uint32_t )MAP_TO_HANDLE (slotId ));
704+ }
705+ if (status != ATCA_SUCCESS )
706+ return atmel_ecc_translate_err (status );
707+
708+ status = talib_handle_init_private_key (& key_attr ,
709+ (uint8_t )curve_type , TA_ALG_MODE_ECC_ECDSA ,
710+ TA_PROP_SIGN_INT_EXT_DIGEST , TA_PROP_KEY_AGREEMENT_OUT_BUFF );
711+ if (status != ATCA_SUCCESS )
712+ return atmel_ecc_translate_err (status );
713+
714+ ta100_fix_property_endian (& key_attr );
715+ status = talib_create_element_with_handle (atcab_get_device (),
716+ (uint32_t )MAP_TO_HANDLE (slotId ), & key_attr );
717+ if (status != ATCA_SUCCESS )
718+ return atmel_ecc_translate_err (status );
719+
720+ status = talib_genkey_base (atcab_get_device (), TA_KEYGEN_MODE_NEWKEY ,
721+ (uint32_t )MAP_TO_HANDLE (slotId ), peerKey , & pubkey_len );
722+ #if defined(TA100_ECC_TRACE )
723+ printf ("[TA100] atmel_ecc_create_key: genkey status=%d pubkey_len=%u\r\n" ,
724+ status , (unsigned )pubkey_len );
725+ #endif
726+ return atmel_ecc_translate_err (status );
727+ }
728+ #endif
729+
668730 ret = atcab_genkey (MAP_TO_HANDLE (slotId ), peerKey );
669- ret = atmel_ecc_translate_err (ret );
670- return ret ;
731+ return atmel_ecc_translate_err (ret );
671732}
672733
673734int atmel_ecc_sign (int slotId , const byte * message , byte * signature )
@@ -692,6 +753,111 @@ int atmel_ecc_verify(const byte* message, const byte* signature,
692753 return ret ;
693754}
694755
756+ #ifdef WOLFSSL_MICROCHIP_TA100
757+ int atmel_ecc_sign_ex (int slotId , int curve_id , const byte * message ,
758+ word32 message_len , byte * signature )
759+ {
760+ int ret ;
761+ int curve_size = getCurveSizeBytes (curve_id );
762+ int curve_type = getCurveType (curve_id );
763+ uint16_t sign_size ;
764+ const byte * msg = message ;
765+ uint16_t msg_len ;
766+ byte tmp_msg [TA_SIGN_P384_MSG_SIZE ];
767+ byte tmp_sig [TA_SIGN_P384_SIG_SIZE ];
768+
769+ if (curve_size <= 0 || curve_type == MICROCHIP_INVALID_ECC )
770+ return NOT_COMPILED_IN ;
771+
772+ sign_size = (uint16_t )(curve_size * 2 );
773+ if (sign_size > sizeof (tmp_sig ))
774+ return BAD_FUNC_ARG ;
775+ msg_len = (uint16_t )message_len ;
776+ if (msg_len != (uint16_t )curve_size ) {
777+ if (msg_len > (uint16_t )curve_size ) {
778+ msg_len = (uint16_t )curve_size ;
779+ } else {
780+ XMEMSET (tmp_msg , 0 , (word32 )curve_size );
781+ XMEMCPY (tmp_msg + (curve_size - msg_len ), message , msg_len );
782+ msg = tmp_msg ;
783+ msg_len = (uint16_t )curve_size ;
784+ }
785+ }
786+ #if defined(TA100_ECC_TRACE )
787+ printf ("[TA100] atmel_ecc_sign_ex: curve_size=%d msg_len=%u\r\n" ,
788+ curve_size , (unsigned )msg_len );
789+ #endif
790+ ret = talib_sign_external (atcab_get_device (), (uint8_t )curve_type ,
791+ MAP_TO_HANDLE (slotId ), TA_HANDLE_INPUT_BUFFER , msg ,
792+ msg_len , tmp_sig , & sign_size );
793+
794+ if (ret != ATCA_SUCCESS )
795+ return atmel_ecc_translate_err (ret );
796+
797+ /* Always return raw R||S, each padded to curve size */
798+ XMEMSET (signature , 0 , (word32 )(curve_size * 2 ));
799+ if (sign_size == (uint16_t )(curve_size * 2 )) {
800+ XMEMCPY (signature , tmp_sig , sign_size );
801+ }
802+ else if ((sign_size % 2 ) == 0 && sign_size < (uint16_t )(curve_size * 2 )) {
803+ uint16_t half = (uint16_t )(sign_size / 2 );
804+ if (half > (uint16_t )curve_size )
805+ return BAD_FUNC_ARG ;
806+ XMEMCPY (signature + (curve_size - half ), tmp_sig , half );
807+ XMEMCPY (signature + curve_size + (curve_size - half ),
808+ tmp_sig + half , half );
809+ }
810+ else {
811+ return ASN_PARSE_E ;
812+ }
813+
814+ return 0 ;
815+ }
816+
817+ int atmel_ecc_verify_ex (const byte * message , word32 message_len ,
818+ const byte * signature , const byte * pubkey , word32 pubkey_len ,
819+ int curve_id , int * pVerified )
820+ {
821+ int ret ;
822+ int curve_size = getCurveSizeBytes (curve_id );
823+ int curve_type = getCurveType (curve_id );
824+ uint16_t sig_len ;
825+ const byte * msg = message ;
826+ uint16_t msg_len ;
827+ byte tmp_msg [TA_VERIFY_P384_MSG_SIZE ];
828+ bool verified = false;
829+
830+ if (curve_size <= 0 || curve_type == MICROCHIP_INVALID_ECC )
831+ return NOT_COMPILED_IN ;
832+
833+ sig_len = (uint16_t )(curve_size * 2 );
834+ msg_len = (uint16_t )message_len ;
835+ if (msg_len != (uint16_t )curve_size ) {
836+ if (msg_len > (uint16_t )curve_size ) {
837+ msg_len = (uint16_t )curve_size ;
838+ } else {
839+ XMEMSET (tmp_msg , 0 , (word32 )curve_size );
840+ XMEMCPY (tmp_msg + (curve_size - msg_len ), message , msg_len );
841+ msg = tmp_msg ;
842+ msg_len = (uint16_t )curve_size ;
843+ }
844+ }
845+ #if defined(TA100_ECC_TRACE )
846+ printf ("[TA100] atmel_ecc_verify_ex: curve_size=%d msg_len=%u\r\n" ,
847+ curve_size , (unsigned )msg_len );
848+ #endif
849+ ret = talib_verify (atcab_get_device (), (uint8_t )curve_type ,
850+ TA_HANDLE_INPUT_BUFFER , TA_HANDLE_INPUT_BUFFER , signature , sig_len ,
851+ msg , msg_len , pubkey , (uint16_t )pubkey_len ,
852+ & verified );
853+
854+ ret = atmel_ecc_translate_err (ret );
855+ if (pVerified )
856+ * pVerified = (int )verified ;
857+ return ret ;
858+ }
859+ #endif /* WOLFSSL_MICROCHIP_TA100 */
860+
695861#endif /* HAVE_ECC */
696862#endif /* WOLFSSL_ATECC508A || WOLFSSL_ATECC608A || WOLFSSL_MICROCHIP_TA100 */
697863
@@ -1335,7 +1501,11 @@ int atcatls_sign_certificate_cb(WOLFSSL* ssl, const byte* in, unsigned int inSz,
13351501 return WC_HW_WAIT_E ;
13361502
13371503 /* We can only sign with P-256 */
1504+ #ifdef WOLFSSL_MICROCHIP_TA100
1505+ ret = atmel_ecc_sign_ex (slotId , ECC_SECP256R1 , in , inSz , sigRs );
1506+ #else
13381507 ret = atmel_ecc_sign (MAP_TO_HANDLE (slotId ), in , sigRs );
1508+ #endif
13391509 if (ret != ATCA_SUCCESS ) {
13401510 ret = WC_HW_E ; goto exit ;
13411511 }
0 commit comments