1 /* 2 * Copyright (c) 2015-2016, Freescale Semiconductor, Inc. 3 * Copyright 2016-2020 NXP 4 * All rights reserved. 5 * 6 * SPDX-License-Identifier: BSD-3-Clause 7 */ 8 #ifndef _FSL_LTC_H_ 9 #define _FSL_LTC_H_ 10 11 #include "fsl_common.h" 12 13 /******************************************************************************* 14 * Definitions 15 *******************************************************************************/ 16 17 /*! 18 * @addtogroup ltc 19 * @{ 20 */ 21 /*! @name Driver version */ 22 /*@{*/ 23 /*! @brief LTC driver version. Version 2.0.16. 24 * 25 * Current version: 2.0.16 26 * 27 * Change log: 28 * - Version 2.0.1 29 * - fixed warning during g++ compilation 30 * 31 * - Version 2.0.2 32 * - fixed [KPSDK-10932][LTC][SHA] LTC_HASH() blocks indefinitely when message size exceeds 4080 bytes 33 * 34 * - Version 2.0.3 35 * - fixed LTC_PKHA_CompareBigNum() in case an integer argument is an array of all zeroes 36 * 37 * - Version 2.0.4 38 * - constant LTC_PKHA_CompareBigNum() processing time 39 * 40 * - Version 2.0.5 41 * - Fix MISRA issues 42 * 43 * - Version 2.0.6 44 * - fixed [KPSDK-23603][LTC] AES Decrypt in ECB and CBC modes fail when ciphertext size > 0xff0 bytes 45 * 46 * - Version 2.0.7 47 * - Fix MISRA-2012 issues 48 * 49 * - Version 2.0.8 50 * - Fix Coverity issues 51 * 52 * - Version 2.0.9 53 * - Fix sign-compare warning in ltc_set_context and in ltc_get_context 54 * 55 * - Version 2.0.10 56 * - Fix MISRA-2012 issues 57 * 58 * - Version 2.0.11 59 * - Fix MISRA-2012 issues 60 * 61 * - Version 2.0.12 62 * - Fix AES Decrypt in CBC modes fail when used kLTC_DecryptKey. 63 * 64 * - Version 2.0.13 65 * - Add feature macro FSL_FEATURE_LTC_HAS_NO_CLOCK_CONTROL_BIT into LTC_Init function. 66 * 67 * - Version 2.0.14 68 * - Add feature macro FSL_FEATURE_LTC_HAS_NO_CLOCK_CONTROL_BIT into LTC_Deinit function. 69 * 70 * - Version 2.0.15 71 * - Fix MISRA-2012 issues 72 * 73 * - Version 2.0.16 74 * - Fix unitialized GCC warning in LTC_AES_GenerateDecryptKey() 75 */ 76 #define FSL_LTC_DRIVER_VERSION (MAKE_VERSION(2, 0, 16)) 77 /*@}*/ 78 /*! @} */ 79 80 /******************************************************************************* 81 * AES Definitions 82 *******************************************************************************/ 83 /*! 84 * @addtogroup ltc_driver_aes 85 * @{ 86 */ 87 /*! AES block size in bytes */ 88 #define LTC_AES_BLOCK_SIZE 16U 89 /*! AES Input Vector size in bytes */ 90 #define LTC_AES_IV_SIZE 16 91 92 /*! @brief Type of AES key for ECB and CBC decrypt operations. */ 93 typedef enum _ltc_aes_key_t 94 { 95 kLTC_EncryptKey = 0U, /*!< Input key is an encrypt key */ 96 kLTC_DecryptKey = 1U, /*!< Input key is a decrypt key */ 97 } ltc_aes_key_t; 98 99 /*! 100 *@} 101 */ 102 103 /******************************************************************************* 104 * DES Definitions 105 *******************************************************************************/ 106 /*! 107 * @addtogroup ltc_driver_des 108 * @{ 109 */ 110 111 /*! @brief LTC DES key size - 64 bits. */ 112 #define LTC_DES_KEY_SIZE 8 113 114 /*! @brief LTC DES IV size - 8 bytes */ 115 #define LTC_DES_IV_SIZE 8 116 117 /*! 118 *@} 119 */ 120 121 /******************************************************************************* 122 * HASH Definitions 123 ******************************************************************************/ 124 /*! 125 * @addtogroup ltc_driver_hash 126 * @{ 127 */ 128 /*! Supported cryptographic block cipher functions for HASH creation */ 129 typedef enum _ltc_hash_algo_t 130 { 131 kLTC_XcbcMac = 0, /*!< XCBC-MAC (AES engine) */ 132 kLTC_Cmac, /*!< CMAC (AES engine) */ 133 #if defined(FSL_FEATURE_LTC_HAS_SHA) && FSL_FEATURE_LTC_HAS_SHA 134 kLTC_Sha1, /*!< SHA_1 (MDHA engine) */ 135 kLTC_Sha224, /*!< SHA_224 (MDHA engine) */ 136 kLTC_Sha256, /*!< SHA_256 (MDHA engine) */ 137 #endif /* FSL_FEATURE_LTC_HAS_SHA */ 138 } ltc_hash_algo_t; 139 140 /*! @brief LTC HASH Context size. */ 141 #if defined(FSL_FEATURE_LTC_HAS_SHA) && FSL_FEATURE_LTC_HAS_SHA 142 #define LTC_HASH_CTX_SIZE 41 143 #else 144 #define LTC_HASH_CTX_SIZE 29 145 #endif /* FSL_FEATURE_LTC_HAS_SHA */ 146 147 /*! @brief Storage type used to save hash context. */ 148 typedef struct _ltc_hash_ctx_t 149 { 150 uint32_t x[LTC_HASH_CTX_SIZE]; 151 } ltc_hash_ctx_t; 152 153 /*! 154 *@} 155 */ 156 /******************************************************************************* 157 * PKHA Definitions 158 ******************************************************************************/ 159 /*! 160 * @addtogroup ltc_driver_pkha 161 * @{ 162 */ 163 /*! PKHA ECC point structure */ 164 typedef struct _ltc_pkha_ecc_point_t 165 { 166 uint8_t *X; /*!< X coordinate (affine) */ 167 uint8_t *Y; /*!< Y coordinate (affine) */ 168 } ltc_pkha_ecc_point_t; 169 170 /*! @brief Use of timing equalized version of a PKHA function. */ 171 typedef enum _ltc_pkha_timing_t 172 { 173 kLTC_PKHA_NoTimingEqualized = 0U, /*!< Normal version of a PKHA operation */ 174 kLTC_PKHA_TimingEqualized = 1U /*!< Timing-equalized version of a PKHA operation */ 175 } ltc_pkha_timing_t; 176 177 /*! @brief Integer vs binary polynomial arithmetic selection. */ 178 typedef enum _ltc_pkha_f2m_t 179 { 180 kLTC_PKHA_IntegerArith = 0U, /*!< Use integer arithmetic */ 181 kLTC_PKHA_F2mArith = 1U /*!< Use binary polynomial arithmetic */ 182 } ltc_pkha_f2m_t; 183 184 /*! @brief Montgomery or normal PKHA input format. */ 185 typedef enum _ltc_pkha_montgomery_form_t 186 { 187 kLTC_PKHA_NormalValue = 0U, /*!< PKHA number is normal integer */ 188 kLTC_PKHA_MontgomeryFormat = 1U /*!< PKHA number is in montgomery format */ 189 } ltc_pkha_montgomery_form_t; 190 191 /*! 192 *@} 193 */ 194 195 /******************************************************************************* 196 * API 197 ******************************************************************************/ 198 199 #if defined(__cplusplus) 200 extern "C" { 201 #endif 202 203 /*! 204 * @addtogroup ltc 205 * @{ 206 */ 207 208 /*! 209 * @brief Initializes the LTC driver. 210 * This function initializes the LTC driver. 211 * @param base LTC peripheral base address 212 */ 213 void LTC_Init(LTC_Type *base); 214 215 /*! 216 * @brief Deinitializes the LTC driver. 217 * This function deinitializes the LTC driver. 218 * @param base LTC peripheral base address 219 */ 220 void LTC_Deinit(LTC_Type *base); 221 222 #if defined(FSL_FEATURE_LTC_HAS_DPAMS) && FSL_FEATURE_LTC_HAS_DPAMS 223 /*! 224 * @brief Sets the DPA Mask Seed register. 225 * 226 * The DPA Mask Seed register reseeds the mask that provides resistance against DPA (differential power analysis) 227 * attacks on AES or DES keys. 228 * 229 * Differential Power Analysis Mask (DPA) resistance uses a randomly changing mask that introduces 230 * "noise" into the power consumed by the AES or DES. This reduces the signal-to-noise ratio that differential 231 * power analysis attacks use to "guess" bits of the key. This randomly changing mask should be 232 * seeded at POR, and continues to provide DPA resistance from that point on. However, to provide even more 233 * DPA protection it is recommended that the DPA mask be reseeded after every 50,000 blocks have 234 * been processed. At that time, software can opt to write a new seed (preferably obtained from an RNG) 235 * into the DPA Mask Seed register (DPAMS), or software can opt to provide the new seed earlier or 236 * later, or not at all. DPA resistance continues even if the DPA mask is never reseeded. 237 * 238 * @param base LTC peripheral base address 239 * @param mask The DPA mask seed. 240 */ 241 void LTC_SetDpaMaskSeed(LTC_Type *base, uint32_t mask); 242 #endif /* FSL_FEATURE_LTC_HAS_DPAMS */ 243 244 /*! 245 *@} 246 */ 247 248 /******************************************************************************* 249 * AES API 250 ******************************************************************************/ 251 252 /*! 253 * @addtogroup ltc_driver_aes 254 * @{ 255 */ 256 257 /*! 258 * @brief Transforms an AES encrypt key (forward AES) into the decrypt key (inverse AES). 259 * 260 * Transforms the AES encrypt key (forward AES) into the decrypt key (inverse AES). 261 * The key derived by this function can be used as a direct load decrypt key 262 * for AES ECB and CBC decryption operations (keyType argument). 263 * 264 * @param base LTC peripheral base address 265 * @param encryptKey Input key for decrypt key transformation 266 * @param[out] decryptKey Output key, the decrypt form of the AES key. 267 * @param keySize Size of the input key and output key in bytes. Must be 16, 24, or 32. 268 * @return Status from key generation operation 269 */ 270 status_t LTC_AES_GenerateDecryptKey(LTC_Type *base, const uint8_t *encryptKey, uint8_t *decryptKey, uint32_t keySize); 271 272 /*! 273 * @brief Encrypts AES using the ECB block mode. 274 * 275 * Encrypts AES using the ECB block mode. 276 * 277 * @param base LTC peripheral base address 278 * @param plaintext Input plain text to encrypt 279 * @param[out] ciphertext Output cipher text 280 * @param size Size of input and output data in bytes. Must be multiple of 16 bytes. 281 * @param key Input key to use for encryption 282 * @param keySize Size of the input key, in bytes. Must be 16, 24, or 32. 283 * @return Status from encrypt operation 284 */ 285 status_t LTC_AES_EncryptEcb( 286 LTC_Type *base, const uint8_t *plaintext, uint8_t *ciphertext, uint32_t size, const uint8_t *key, uint32_t keySize); 287 288 /*! 289 * @brief Decrypts AES using ECB block mode. 290 * 291 * Decrypts AES using ECB block mode. 292 * 293 * @param base LTC peripheral base address 294 * @param ciphertext Input cipher text to decrypt 295 * @param[out] plaintext Output plain text 296 * @param size Size of input and output data in bytes. Must be multiple of 16 bytes. 297 * @param key Input key. 298 * @param keySize Size of the input key, in bytes. Must be 16, 24, or 32. 299 * @param keyType Input type of the key (allows to directly load decrypt key for AES ECB decrypt operation.) 300 * @return Status from decrypt operation 301 */ 302 status_t LTC_AES_DecryptEcb(LTC_Type *base, 303 const uint8_t *ciphertext, 304 uint8_t *plaintext, 305 uint32_t size, 306 const uint8_t *key, 307 uint32_t keySize, 308 ltc_aes_key_t keyType); 309 310 /*! 311 * @brief Encrypts AES using CBC block mode. 312 * 313 * @param base LTC peripheral base address 314 * @param plaintext Input plain text to encrypt 315 * @param[out] ciphertext Output cipher text 316 * @param size Size of input and output data in bytes. Must be multiple of 16 bytes. 317 * @param iv Input initial vector to combine with the first input block. 318 * @param key Input key to use for encryption 319 * @param keySize Size of the input key, in bytes. Must be 16, 24, or 32. 320 * @return Status from encrypt operation 321 */ 322 status_t LTC_AES_EncryptCbc(LTC_Type *base, 323 const uint8_t *plaintext, 324 uint8_t *ciphertext, 325 uint32_t size, 326 const uint8_t iv[LTC_AES_IV_SIZE], 327 const uint8_t *key, 328 uint32_t keySize); 329 330 /*! 331 * @brief Decrypts AES using CBC block mode. 332 * 333 * @param base LTC peripheral base address 334 * @param ciphertext Input cipher text to decrypt 335 * @param[out] plaintext Output plain text 336 * @param size Size of input and output data in bytes. Must be multiple of 16 bytes. 337 * @param iv Input initial vector to combine with the first input block. 338 * @param key Input key to use for decryption 339 * @param keySize Size of the input key, in bytes. Must be 16, 24, or 32. 340 * @param keyType Input type of the key (allows to directly load decrypt key for AES CBC decrypt operation.) 341 * @return Status from decrypt operation 342 */ 343 status_t LTC_AES_DecryptCbc(LTC_Type *base, 344 const uint8_t *ciphertext, 345 uint8_t *plaintext, 346 uint32_t size, 347 const uint8_t iv[LTC_AES_IV_SIZE], 348 const uint8_t *key, 349 uint32_t keySize, 350 ltc_aes_key_t keyType); 351 352 /*! 353 * @brief Encrypts or decrypts AES using CTR block mode. 354 * 355 * Encrypts or decrypts AES using CTR block mode. 356 * AES CTR mode uses only forward AES cipher and same algorithm for encryption and decryption. 357 * The only difference between encryption and decryption is that, for encryption, the input argument 358 * is plain text and the output argument is cipher text. For decryption, the input argument is cipher text 359 * and the output argument is plain text. 360 * 361 * @param base LTC peripheral base address 362 * @param input Input data for CTR block mode 363 * @param[out] output Output data for CTR block mode 364 * @param size Size of input and output data in bytes 365 * @param[in,out] counter Input counter (updates on return) 366 * @param key Input key to use for forward AES cipher 367 * @param keySize Size of the input key, in bytes. Must be 16, 24, or 32. 368 * @param[out] counterlast Output cipher of last counter, for chained CTR calls. NULL can be passed if chained calls are 369 * not used. 370 * @param[out] szLeft Output number of bytes in left unused in counterlast block. NULL can be passed if chained calls 371 * are not used. 372 * @return Status from encrypt operation 373 */ 374 status_t LTC_AES_CryptCtr(LTC_Type *base, 375 const uint8_t *input, 376 uint8_t *output, 377 uint32_t size, 378 uint8_t counter[LTC_AES_BLOCK_SIZE], 379 const uint8_t *key, 380 uint32_t keySize, 381 uint8_t counterlast[LTC_AES_BLOCK_SIZE], 382 uint32_t *szLeft); 383 384 /*! AES CTR decrypt is mapped to the AES CTR generic operation */ 385 #define LTC_AES_DecryptCtr(base, input, output, size, counter, key, keySize, counterlast, szLeft) \ 386 LTC_AES_CryptCtr(base, input, output, size, counter, key, keySize, counterlast, szLeft) 387 388 /*! AES CTR encrypt is mapped to the AES CTR generic operation */ 389 #define LTC_AES_EncryptCtr(base, input, output, size, counter, key, keySize, counterlast, szLeft) \ 390 LTC_AES_CryptCtr(base, input, output, size, counter, key, keySize, counterlast, szLeft) 391 392 #if defined(FSL_FEATURE_LTC_HAS_GCM) && FSL_FEATURE_LTC_HAS_GCM 393 /*! 394 * @brief Encrypts AES and tags using GCM block mode. 395 * 396 * Encrypts AES and optionally tags using GCM block mode. If plaintext is NULL, only the GHASH is calculated and output 397 * in the 'tag' field. 398 * 399 * @param base LTC peripheral base address 400 * @param plaintext Input plain text to encrypt 401 * @param[out] ciphertext Output cipher text. 402 * @param size Size of input and output data in bytes 403 * @param iv Input initial vector 404 * @param ivSize Size of the IV 405 * @param aad Input additional authentication data 406 * @param aadSize Input size in bytes of AAD 407 * @param key Input key to use for encryption 408 * @param keySize Size of the input key, in bytes. Must be 16, 24, or 32. 409 * @param[out] tag Output hash tag. Set to NULL to skip tag processing. 410 * @param tagSize Input size of the tag to generate, in bytes. Must be 4,8,12,13,14,15 or 16. 411 * @return Status from encrypt operation 412 */ 413 status_t LTC_AES_EncryptTagGcm(LTC_Type *base, 414 const uint8_t *plaintext, 415 uint8_t *ciphertext, 416 uint32_t size, 417 const uint8_t *iv, 418 uint32_t ivSize, 419 const uint8_t *aad, 420 uint32_t aadSize, 421 const uint8_t *key, 422 uint32_t keySize, 423 uint8_t *tag, 424 uint32_t tagSize); 425 426 /*! 427 * @brief Decrypts AES and authenticates using GCM block mode. 428 * 429 * Decrypts AES and optionally authenticates using GCM block mode. If ciphertext is NULL, only the GHASH is calculated 430 * and compared with the received GHASH in 'tag' field. 431 * 432 * @param base LTC peripheral base address 433 * @param ciphertext Input cipher text to decrypt 434 * @param[out] plaintext Output plain text. 435 * @param size Size of input and output data in bytes 436 * @param iv Input initial vector 437 * @param ivSize Size of the IV 438 * @param aad Input additional authentication data 439 * @param aadSize Input size in bytes of AAD 440 * @param key Input key to use for encryption 441 * @param keySize Size of the input key, in bytes. Must be 16, 24, or 32. 442 * @param tag Input hash tag to compare. Set to NULL to skip tag processing. 443 * @param tagSize Input size of the tag, in bytes. Must be 4, 8, 12, 13, 14, 15, or 16. 444 * @return Status from decrypt operation 445 */ 446 status_t LTC_AES_DecryptTagGcm(LTC_Type *base, 447 const uint8_t *ciphertext, 448 uint8_t *plaintext, 449 uint32_t size, 450 const uint8_t *iv, 451 uint32_t ivSize, 452 const uint8_t *aad, 453 uint32_t aadSize, 454 const uint8_t *key, 455 uint32_t keySize, 456 const uint8_t *tag, 457 uint32_t tagSize); 458 #endif /* FSL_FEATURE_LTC_HAS_GCM */ 459 460 /*! 461 * @brief Encrypts AES and tags using CCM block mode. 462 * 463 * Encrypts AES and optionally tags using CCM block mode. 464 * 465 * @param base LTC peripheral base address 466 * @param plaintext Input plain text to encrypt 467 * @param[out] ciphertext Output cipher text. 468 * @param size Size of input and output data in bytes. Zero means authentication only. 469 * @param iv Nonce 470 * @param ivSize Length of the Nonce in bytes. Must be 7, 8, 9, 10, 11, 12, or 13. 471 * @param aad Input additional authentication data. Can be NULL if aadSize is zero. 472 * @param aadSize Input size in bytes of AAD. Zero means data mode only (authentication skipped). 473 * @param key Input key to use for encryption 474 * @param keySize Size of the input key, in bytes. Must be 16, 24, or 32. 475 * @param[out] tag Generated output tag. Set to NULL to skip tag processing. 476 * @param tagSize Input size of the tag to generate, in bytes. Must be 4, 6, 8, 10, 12, 14, or 16. 477 * @return Status from encrypt operation 478 */ 479 status_t LTC_AES_EncryptTagCcm(LTC_Type *base, 480 const uint8_t *plaintext, 481 uint8_t *ciphertext, 482 uint32_t size, 483 const uint8_t *iv, 484 uint32_t ivSize, 485 const uint8_t *aad, 486 uint32_t aadSize, 487 const uint8_t *key, 488 uint32_t keySize, 489 uint8_t *tag, 490 uint32_t tagSize); 491 492 /*! 493 * @brief Decrypts AES and authenticates using CCM block mode. 494 * 495 * Decrypts AES and optionally authenticates using CCM block mode. 496 * 497 * @param base LTC peripheral base address 498 * @param ciphertext Input cipher text to decrypt 499 * @param[out] plaintext Output plain text. 500 * @param size Size of input and output data in bytes. Zero means authentication only. 501 * @param iv Nonce 502 * @param ivSize Length of the Nonce in bytes. Must be 7, 8, 9, 10, 11, 12, or 13. 503 * @param aad Input additional authentication data. Can be NULL if aadSize is zero. 504 * @param aadSize Input size in bytes of AAD. Zero means data mode only (authentication skipped). 505 * @param key Input key to use for decryption 506 * @param keySize Size of the input key, in bytes. Must be 16, 24, or 32. 507 * @param tag Received tag. Set to NULL to skip tag processing. 508 * @param tagSize Input size of the received tag to compare with the computed tag, in bytes. Must be 4, 6, 8, 10, 12, 509 * 14, or 16. 510 * @return Status from decrypt operation 511 */ 512 status_t LTC_AES_DecryptTagCcm(LTC_Type *base, 513 const uint8_t *ciphertext, 514 uint8_t *plaintext, 515 uint32_t size, 516 const uint8_t *iv, 517 uint32_t ivSize, 518 const uint8_t *aad, 519 uint32_t aadSize, 520 const uint8_t *key, 521 uint32_t keySize, 522 const uint8_t *tag, 523 uint32_t tagSize); 524 525 /*! 526 *@} 527 */ 528 529 #if defined(FSL_FEATURE_LTC_HAS_DES) && FSL_FEATURE_LTC_HAS_DES 530 /******************************************************************************* 531 * DES API 532 ******************************************************************************/ 533 /*! 534 * @addtogroup ltc_driver_des 535 * @{ 536 */ 537 /*! 538 * @brief Encrypts DES using ECB block mode. 539 * 540 * Encrypts DES using ECB block mode. 541 * 542 * @param base LTC peripheral base address 543 * @param plaintext Input plaintext to encrypt 544 * @param[out] ciphertext Output ciphertext 545 * @param size Size of input and output data in bytes. Must be multiple of 8 bytes. 546 * @param key Input key to use for encryption 547 * @return Status from encrypt/decrypt operation 548 */ 549 status_t LTC_DES_EncryptEcb( 550 LTC_Type *base, const uint8_t *plaintext, uint8_t *ciphertext, uint32_t size, const uint8_t key[LTC_DES_KEY_SIZE]); 551 552 /*! 553 * @brief Decrypts DES using ECB block mode. 554 * 555 * Decrypts DES using ECB block mode. 556 * 557 * @param base LTC peripheral base address 558 * @param ciphertext Input ciphertext to decrypt 559 * @param[out] plaintext Output plaintext 560 * @param size Size of input and output data in bytes. Must be multiple of 8 bytes. 561 * @param key Input key to use for decryption 562 * @return Status from encrypt/decrypt operation 563 */ 564 status_t LTC_DES_DecryptEcb( 565 LTC_Type *base, const uint8_t *ciphertext, uint8_t *plaintext, uint32_t size, const uint8_t key[LTC_DES_KEY_SIZE]); 566 567 /*! 568 * @brief Encrypts DES using CBC block mode. 569 * 570 * Encrypts DES using CBC block mode. 571 * 572 * @param base LTC peripheral base address 573 * @param plaintext Input plaintext to encrypt 574 * @param[out] ciphertext Ouput ciphertext 575 * @param size Size of input and output data in bytes 576 * @param iv Input initial vector to combine with the first plaintext block. 577 * The iv does not need to be secret, but it must be unpredictable. 578 * @param key Input key to use for encryption 579 * @return Status from encrypt/decrypt operation 580 */ 581 status_t LTC_DES_EncryptCbc(LTC_Type *base, 582 const uint8_t *plaintext, 583 uint8_t *ciphertext, 584 uint32_t size, 585 const uint8_t iv[LTC_DES_IV_SIZE], 586 const uint8_t key[LTC_DES_KEY_SIZE]); 587 588 /*! 589 * @brief Decrypts DES using CBC block mode. 590 * 591 * Decrypts DES using CBC block mode. 592 * 593 * @param base LTC peripheral base address 594 * @param ciphertext Input ciphertext to decrypt 595 * @param[out] plaintext Output plaintext 596 * @param size Size of input data in bytes 597 * @param iv Input initial vector to combine with the first plaintext block. 598 * The iv does not need to be secret, but it must be unpredictable. 599 * @param key Input key to use for decryption 600 * @return Status from encrypt/decrypt operation 601 */ 602 status_t LTC_DES_DecryptCbc(LTC_Type *base, 603 const uint8_t *ciphertext, 604 uint8_t *plaintext, 605 uint32_t size, 606 const uint8_t iv[LTC_DES_IV_SIZE], 607 const uint8_t key[LTC_DES_KEY_SIZE]); 608 609 /*! 610 * @brief Encrypts DES using CFB block mode. 611 * 612 * Encrypts DES using CFB block mode. 613 * 614 * @param base LTC peripheral base address 615 * @param plaintext Input plaintext to encrypt 616 * @param size Size of input data in bytes 617 * @param iv Input initial block. 618 * @param key Input key to use for encryption 619 * @param[out] ciphertext Output ciphertext 620 * @return Status from encrypt/decrypt operation 621 */ 622 status_t LTC_DES_EncryptCfb(LTC_Type *base, 623 const uint8_t *plaintext, 624 uint8_t *ciphertext, 625 uint32_t size, 626 const uint8_t iv[LTC_DES_IV_SIZE], 627 const uint8_t key[LTC_DES_KEY_SIZE]); 628 629 /*! 630 * @brief Decrypts DES using CFB block mode. 631 * 632 * Decrypts DES using CFB block mode. 633 * 634 * @param base LTC peripheral base address 635 * @param ciphertext Input ciphertext to decrypt 636 * @param[out] plaintext Output plaintext 637 * @param size Size of input and output data in bytes 638 * @param iv Input initial block. 639 * @param key Input key to use for decryption 640 * @return Status from encrypt/decrypt operation 641 */ 642 status_t LTC_DES_DecryptCfb(LTC_Type *base, 643 const uint8_t *ciphertext, 644 uint8_t *plaintext, 645 uint32_t size, 646 const uint8_t iv[LTC_DES_IV_SIZE], 647 const uint8_t key[LTC_DES_KEY_SIZE]); 648 649 /*! 650 * @brief Encrypts DES using OFB block mode. 651 * 652 * Encrypts DES using OFB block mode. 653 * 654 * @param base LTC peripheral base address 655 * @param plaintext Input plaintext to encrypt 656 * @param[out] ciphertext Output ciphertext 657 * @param size Size of input and output data in bytes 658 * @param iv Input unique input vector. The OFB mode requires that the IV be unique 659 * for each execution of the mode under the given key. 660 * @param key Input key to use for encryption 661 * @return Status from encrypt/decrypt operation 662 */ 663 status_t LTC_DES_EncryptOfb(LTC_Type *base, 664 const uint8_t *plaintext, 665 uint8_t *ciphertext, 666 uint32_t size, 667 const uint8_t iv[LTC_DES_IV_SIZE], 668 const uint8_t key[LTC_DES_KEY_SIZE]); 669 670 /*! 671 * @brief Decrypts DES using OFB block mode. 672 * 673 * Decrypts DES using OFB block mode. 674 * 675 * @param base LTC peripheral base address 676 * @param ciphertext Input ciphertext to decrypt 677 * @param[out] plaintext Output plaintext 678 * @param size Size of input and output data in bytes. Must be multiple of 8 bytes. 679 * @param iv Input unique input vector. The OFB mode requires that the IV be unique 680 * for each execution of the mode under the given key. 681 * @param key Input key to use for decryption 682 * @return Status from encrypt/decrypt operation 683 */ 684 status_t LTC_DES_DecryptOfb(LTC_Type *base, 685 const uint8_t *ciphertext, 686 uint8_t *plaintext, 687 uint32_t size, 688 const uint8_t iv[LTC_DES_IV_SIZE], 689 const uint8_t key[LTC_DES_KEY_SIZE]); 690 691 /*! 692 * @brief Encrypts triple DES using ECB block mode with two keys. 693 * 694 * Encrypts triple DES using ECB block mode with two keys. 695 * 696 * @param base LTC peripheral base address 697 * @param plaintext Input plaintext to encrypt 698 * @param[out] ciphertext Output ciphertext 699 * @param size Size of input and output data in bytes. Must be multiple of 8 bytes. 700 * @param key1 First input key for key bundle 701 * @param key2 Second input key for key bundle 702 * @return Status from encrypt/decrypt operation 703 */ 704 status_t LTC_DES2_EncryptEcb(LTC_Type *base, 705 const uint8_t *plaintext, 706 uint8_t *ciphertext, 707 uint32_t size, 708 const uint8_t key1[LTC_DES_KEY_SIZE], 709 const uint8_t key2[LTC_DES_KEY_SIZE]); 710 711 /*! 712 * @brief Decrypts triple DES using ECB block mode with two keys. 713 * 714 * Decrypts triple DES using ECB block mode with two keys. 715 * 716 * @param base LTC peripheral base address 717 * @param ciphertext Input ciphertext to decrypt 718 * @param[out] plaintext Output plaintext 719 * @param size Size of input and output data in bytes. Must be multiple of 8 bytes. 720 * @param key1 First input key for key bundle 721 * @param key2 Second input key for key bundle 722 * @return Status from encrypt/decrypt operation 723 */ 724 status_t LTC_DES2_DecryptEcb(LTC_Type *base, 725 const uint8_t *ciphertext, 726 uint8_t *plaintext, 727 uint32_t size, 728 const uint8_t key1[LTC_DES_KEY_SIZE], 729 const uint8_t key2[LTC_DES_KEY_SIZE]); 730 731 /*! 732 * @brief Encrypts triple DES using CBC block mode with two keys. 733 * 734 * Encrypts triple DES using CBC block mode with two keys. 735 * 736 * @param base LTC peripheral base address 737 * @param plaintext Input plaintext to encrypt 738 * @param[out] ciphertext Output ciphertext 739 * @param size Size of input and output data in bytes 740 * @param iv Input initial vector to combine with the first plaintext block. 741 * The iv does not need to be secret, but it must be unpredictable. 742 * @param key1 First input key for key bundle 743 * @param key2 Second input key for key bundle 744 * @return Status from encrypt/decrypt operation 745 */ 746 status_t LTC_DES2_EncryptCbc(LTC_Type *base, 747 const uint8_t *plaintext, 748 uint8_t *ciphertext, 749 uint32_t size, 750 const uint8_t iv[LTC_DES_IV_SIZE], 751 const uint8_t key1[LTC_DES_KEY_SIZE], 752 const uint8_t key2[LTC_DES_KEY_SIZE]); 753 754 /*! 755 * @brief Decrypts triple DES using CBC block mode with two keys. 756 * 757 * Decrypts triple DES using CBC block mode with two keys. 758 * 759 * @param base LTC peripheral base address 760 * @param ciphertext Input ciphertext to decrypt 761 * @param[out] plaintext Output plaintext 762 * @param size Size of input and output data in bytes 763 * @param iv Input initial vector to combine with the first plaintext block. 764 * The iv does not need to be secret, but it must be unpredictable. 765 * @param key1 First input key for key bundle 766 * @param key2 Second input key for key bundle 767 * @return Status from encrypt/decrypt operation 768 */ 769 status_t LTC_DES2_DecryptCbc(LTC_Type *base, 770 const uint8_t *ciphertext, 771 uint8_t *plaintext, 772 uint32_t size, 773 const uint8_t iv[LTC_DES_IV_SIZE], 774 const uint8_t key1[LTC_DES_KEY_SIZE], 775 const uint8_t key2[LTC_DES_KEY_SIZE]); 776 777 /*! 778 * @brief Encrypts triple DES using CFB block mode with two keys. 779 * 780 * Encrypts triple DES using CFB block mode with two keys. 781 * 782 * @param base LTC peripheral base address 783 * @param plaintext Input plaintext to encrypt 784 * @param[out] ciphertext Output ciphertext 785 * @param size Size of input and output data in bytes 786 * @param iv Input initial block. 787 * @param key1 First input key for key bundle 788 * @param key2 Second input key for key bundle 789 * @return Status from encrypt/decrypt operation 790 */ 791 status_t LTC_DES2_EncryptCfb(LTC_Type *base, 792 const uint8_t *plaintext, 793 uint8_t *ciphertext, 794 uint32_t size, 795 const uint8_t iv[LTC_DES_IV_SIZE], 796 const uint8_t key1[LTC_DES_KEY_SIZE], 797 const uint8_t key2[LTC_DES_KEY_SIZE]); 798 799 /*! 800 * @brief Decrypts triple DES using CFB block mode with two keys. 801 * 802 * Decrypts triple DES using CFB block mode with two keys. 803 * 804 * @param base LTC peripheral base address 805 * @param ciphertext Input ciphertext to decrypt 806 * @param[out] plaintext Output plaintext 807 * @param size Size of input and output data in bytes 808 * @param iv Input initial block. 809 * @param key1 First input key for key bundle 810 * @param key2 Second input key for key bundle 811 * @return Status from encrypt/decrypt operation 812 */ 813 status_t LTC_DES2_DecryptCfb(LTC_Type *base, 814 const uint8_t *ciphertext, 815 uint8_t *plaintext, 816 uint32_t size, 817 const uint8_t iv[LTC_DES_IV_SIZE], 818 const uint8_t key1[LTC_DES_KEY_SIZE], 819 const uint8_t key2[LTC_DES_KEY_SIZE]); 820 821 /*! 822 * @brief Encrypts triple DES using OFB block mode with two keys. 823 * 824 * Encrypts triple DES using OFB block mode with two keys. 825 * 826 * @param base LTC peripheral base address 827 * @param plaintext Input plaintext to encrypt 828 * @param[out] ciphertext Output ciphertext 829 * @param size Size of input and output data in bytes 830 * @param iv Input unique input vector. The OFB mode requires that the IV be unique 831 * for each execution of the mode under the given key. 832 * @param key1 First input key for key bundle 833 * @param key2 Second input key for key bundle 834 * @return Status from encrypt/decrypt operation 835 */ 836 status_t LTC_DES2_EncryptOfb(LTC_Type *base, 837 const uint8_t *plaintext, 838 uint8_t *ciphertext, 839 uint32_t size, 840 const uint8_t iv[LTC_DES_IV_SIZE], 841 const uint8_t key1[LTC_DES_KEY_SIZE], 842 const uint8_t key2[LTC_DES_KEY_SIZE]); 843 844 /*! 845 * @brief Decrypts triple DES using OFB block mode with two keys. 846 * 847 * Decrypts triple DES using OFB block mode with two keys. 848 * 849 * @param base LTC peripheral base address 850 * @param ciphertext Input ciphertext to decrypt 851 * @param[out] plaintext Output plaintext 852 * @param size Size of input and output data in bytes 853 * @param iv Input unique input vector. The OFB mode requires that the IV be unique 854 * for each execution of the mode under the given key. 855 * @param key1 First input key for key bundle 856 * @param key2 Second input key for key bundle 857 * @return Status from encrypt/decrypt operation 858 */ 859 status_t LTC_DES2_DecryptOfb(LTC_Type *base, 860 const uint8_t *ciphertext, 861 uint8_t *plaintext, 862 uint32_t size, 863 const uint8_t iv[LTC_DES_IV_SIZE], 864 const uint8_t key1[LTC_DES_KEY_SIZE], 865 const uint8_t key2[LTC_DES_KEY_SIZE]); 866 867 /*! 868 * @brief Encrypts triple DES using ECB block mode with three keys. 869 * 870 * Encrypts triple DES using ECB block mode with three keys. 871 * 872 * @param base LTC peripheral base address 873 * @param plaintext Input plaintext to encrypt 874 * @param[out] ciphertext Output ciphertext 875 * @param size Size of input and output data in bytes. Must be multiple of 8 bytes. 876 * @param key1 First input key for key bundle 877 * @param key2 Second input key for key bundle 878 * @param key3 Third input key for key bundle 879 * @return Status from encrypt/decrypt operation 880 */ 881 status_t LTC_DES3_EncryptEcb(LTC_Type *base, 882 const uint8_t *plaintext, 883 uint8_t *ciphertext, 884 uint32_t size, 885 const uint8_t key1[LTC_DES_KEY_SIZE], 886 const uint8_t key2[LTC_DES_KEY_SIZE], 887 const uint8_t key3[LTC_DES_KEY_SIZE]); 888 889 /*! 890 * @brief Decrypts triple DES using ECB block mode with three keys. 891 * 892 * Decrypts triple DES using ECB block mode with three keys. 893 * 894 * @param base LTC peripheral base address 895 * @param ciphertext Input ciphertext to decrypt 896 * @param[out] plaintext Output plaintext 897 * @param size Size of input and output data in bytes. Must be multiple of 8 bytes. 898 * @param key1 First input key for key bundle 899 * @param key2 Second input key for key bundle 900 * @param key3 Third input key for key bundle 901 * @return Status from encrypt/decrypt operation 902 */ 903 status_t LTC_DES3_DecryptEcb(LTC_Type *base, 904 const uint8_t *ciphertext, 905 uint8_t *plaintext, 906 uint32_t size, 907 const uint8_t key1[LTC_DES_KEY_SIZE], 908 const uint8_t key2[LTC_DES_KEY_SIZE], 909 const uint8_t key3[LTC_DES_KEY_SIZE]); 910 911 /*! 912 * @brief Encrypts triple DES using CBC block mode with three keys. 913 * 914 * Encrypts triple DES using CBC block mode with three keys. 915 * 916 * @param base LTC peripheral base address 917 * @param plaintext Input plaintext to encrypt 918 * @param[out] ciphertext Output ciphertext 919 * @param size Size of input data in bytes 920 * @param iv Input initial vector to combine with the first plaintext block. 921 * The iv does not need to be secret, but it must be unpredictable. 922 * @param key1 First input key for key bundle 923 * @param key2 Second input key for key bundle 924 * @param key3 Third input key for key bundle 925 * @return Status from encrypt/decrypt operation 926 */ 927 status_t LTC_DES3_EncryptCbc(LTC_Type *base, 928 const uint8_t *plaintext, 929 uint8_t *ciphertext, 930 uint32_t size, 931 const uint8_t iv[LTC_DES_IV_SIZE], 932 const uint8_t key1[LTC_DES_KEY_SIZE], 933 const uint8_t key2[LTC_DES_KEY_SIZE], 934 const uint8_t key3[LTC_DES_KEY_SIZE]); 935 936 /*! 937 * @brief Decrypts triple DES using CBC block mode with three keys. 938 * 939 * Decrypts triple DES using CBC block mode with three keys. 940 * 941 * @param base LTC peripheral base address 942 * @param ciphertext Input ciphertext to decrypt 943 * @param[out] plaintext Output plaintext 944 * @param size Size of input and output data in bytes 945 * @param iv Input initial vector to combine with the first plaintext block. 946 * The iv does not need to be secret, but it must be unpredictable. 947 * @param key1 First input key for key bundle 948 * @param key2 Second input key for key bundle 949 * @param key3 Third input key for key bundle 950 * @return Status from encrypt/decrypt operation 951 */ 952 status_t LTC_DES3_DecryptCbc(LTC_Type *base, 953 const uint8_t *ciphertext, 954 uint8_t *plaintext, 955 uint32_t size, 956 const uint8_t iv[LTC_DES_IV_SIZE], 957 const uint8_t key1[LTC_DES_KEY_SIZE], 958 const uint8_t key2[LTC_DES_KEY_SIZE], 959 const uint8_t key3[LTC_DES_KEY_SIZE]); 960 961 /*! 962 * @brief Encrypts triple DES using CFB block mode with three keys. 963 * 964 * Encrypts triple DES using CFB block mode with three keys. 965 * 966 * @param base LTC peripheral base address 967 * @param plaintext Input plaintext to encrypt 968 * @param[out] ciphertext Output ciphertext 969 * @param size Size of input and ouput data in bytes 970 * @param iv Input initial block. 971 * @param key1 First input key for key bundle 972 * @param key2 Second input key for key bundle 973 * @param key3 Third input key for key bundle 974 * @return Status from encrypt/decrypt operation 975 */ 976 status_t LTC_DES3_EncryptCfb(LTC_Type *base, 977 const uint8_t *plaintext, 978 uint8_t *ciphertext, 979 uint32_t size, 980 const uint8_t iv[LTC_DES_IV_SIZE], 981 const uint8_t key1[LTC_DES_KEY_SIZE], 982 const uint8_t key2[LTC_DES_KEY_SIZE], 983 const uint8_t key3[LTC_DES_KEY_SIZE]); 984 985 /*! 986 * @brief Decrypts triple DES using CFB block mode with three keys. 987 * 988 * Decrypts triple DES using CFB block mode with three keys. 989 * 990 * @param base LTC peripheral base address 991 * @param ciphertext Input ciphertext to decrypt 992 * @param[out] plaintext Output plaintext 993 * @param size Size of input data in bytes 994 * @param iv Input initial block. 995 * @param key1 First input key for key bundle 996 * @param key2 Second input key for key bundle 997 * @param key3 Third input key for key bundle 998 * @return Status from encrypt/decrypt operation 999 */ 1000 status_t LTC_DES3_DecryptCfb(LTC_Type *base, 1001 const uint8_t *ciphertext, 1002 uint8_t *plaintext, 1003 uint32_t size, 1004 const uint8_t iv[LTC_DES_IV_SIZE], 1005 const uint8_t key1[LTC_DES_KEY_SIZE], 1006 const uint8_t key2[LTC_DES_KEY_SIZE], 1007 const uint8_t key3[LTC_DES_KEY_SIZE]); 1008 1009 /*! 1010 * @brief Encrypts triple DES using OFB block mode with three keys. 1011 * 1012 * Encrypts triple DES using OFB block mode with three keys. 1013 * 1014 * @param base LTC peripheral base address 1015 * @param plaintext Input plaintext to encrypt 1016 * @param[out] ciphertext Output ciphertext 1017 * @param size Size of input and output data in bytes 1018 * @param iv Input unique input vector. The OFB mode requires that the IV be unique 1019 * for each execution of the mode under the given key. 1020 * @param key1 First input key for key bundle 1021 * @param key2 Second input key for key bundle 1022 * @param key3 Third input key for key bundle 1023 * @return Status from encrypt/decrypt operation 1024 */ 1025 status_t LTC_DES3_EncryptOfb(LTC_Type *base, 1026 const uint8_t *plaintext, 1027 uint8_t *ciphertext, 1028 uint32_t size, 1029 const uint8_t iv[LTC_DES_IV_SIZE], 1030 const uint8_t key1[LTC_DES_KEY_SIZE], 1031 const uint8_t key2[LTC_DES_KEY_SIZE], 1032 const uint8_t key3[LTC_DES_KEY_SIZE]); 1033 1034 /*! 1035 * @brief Decrypts triple DES using OFB block mode with three keys. 1036 * 1037 * Decrypts triple DES using OFB block mode with three keys. 1038 * 1039 * @param base LTC peripheral base address 1040 * @param ciphertext Input ciphertext to decrypt 1041 * @param[out] plaintext Output plaintext 1042 * @param size Size of input and output data in bytes 1043 * @param iv Input unique input vector. The OFB mode requires that the IV be unique 1044 * for each execution of the mode under the given key. 1045 * @param key1 First input key for key bundle 1046 * @param key2 Second input key for key bundle 1047 * @param key3 Third input key for key bundle 1048 * @return Status from encrypt/decrypt operation 1049 */ 1050 status_t LTC_DES3_DecryptOfb(LTC_Type *base, 1051 const uint8_t *ciphertext, 1052 uint8_t *plaintext, 1053 uint32_t size, 1054 const uint8_t iv[LTC_DES_IV_SIZE], 1055 const uint8_t key1[LTC_DES_KEY_SIZE], 1056 const uint8_t key2[LTC_DES_KEY_SIZE], 1057 const uint8_t key3[LTC_DES_KEY_SIZE]); 1058 #endif /* FSL_FEATURE_LTC_HAS_DES */ 1059 1060 /*! 1061 *@} 1062 */ 1063 1064 /******************************************************************************* 1065 * HASH API 1066 ******************************************************************************/ 1067 1068 /*! 1069 * @addtogroup ltc_driver_hash 1070 * @{ 1071 */ 1072 /*! 1073 * @brief Initialize HASH context 1074 * 1075 * This function initialize the HASH. 1076 * Key shall be supplied if the underlaying algoritm is AES XCBC-MAC or CMAC. 1077 * Key shall be NULL if the underlaying algoritm is SHA. 1078 * 1079 * For XCBC-MAC, the key length must be 16. For CMAC, the key length can be 1080 * the AES key lengths supported by AES engine. For MDHA the key length argument 1081 * is ignored. 1082 * 1083 * @param base LTC peripheral base address 1084 * @param[out] ctx Output hash context 1085 * @param algo Underlaying algorithm to use for hash computation. 1086 * @param key Input key (NULL if underlaying algorithm is SHA) 1087 * @param keySize Size of input key in bytes 1088 * @return Status of initialization 1089 */ 1090 status_t LTC_HASH_Init(LTC_Type *base, ltc_hash_ctx_t *ctx, ltc_hash_algo_t algo, const uint8_t *key, uint32_t keySize); 1091 1092 /*! 1093 * @brief Add data to current HASH 1094 * 1095 * Add data to current HASH. This can be called repeatedly with an arbitrary amount of data to be 1096 * hashed. 1097 * 1098 * @param[in,out] ctx HASH context 1099 * @param input Input data 1100 * @param inputSize Size of input data in bytes 1101 * @return Status of the hash update operation 1102 */ 1103 status_t LTC_HASH_Update(ltc_hash_ctx_t *ctx, const uint8_t *input, uint32_t inputSize); 1104 1105 /*! 1106 * @brief Finalize hashing 1107 * 1108 * Outputs the final hash and erases the context. 1109 * 1110 * @param[in,out] ctx Input hash context 1111 * @param[out] output Output hash data 1112 * @param[out] outputSize Output parameter storing the size of the output hash in bytes 1113 * @return Status of the hash finish operation 1114 */ 1115 status_t LTC_HASH_Finish(ltc_hash_ctx_t *ctx, uint8_t *output, uint32_t *outputSize); 1116 1117 /*! 1118 * @brief Create HASH on given data 1119 * 1120 * Perform the full keyed HASH in one function call. 1121 * 1122 * @param base LTC peripheral base address 1123 * @param algo Block cipher algorithm to use for CMAC creation 1124 * @param input Input data 1125 * @param inputSize Size of input data in bytes 1126 * @param key Input key 1127 * @param keySize Size of input key in bytes 1128 * @param[out] output Output hash data 1129 * @param[out] outputSize Output parameter storing the size of the output hash in bytes 1130 * @return Status of the one call hash operation. 1131 */ 1132 status_t LTC_HASH(LTC_Type *base, 1133 ltc_hash_algo_t algo, 1134 const uint8_t *input, 1135 uint32_t inputSize, 1136 const uint8_t *key, 1137 uint32_t keySize, 1138 uint8_t *output, 1139 uint32_t *outputSize); 1140 /*! 1141 *@} 1142 */ 1143 1144 #if defined(FSL_FEATURE_LTC_HAS_PKHA) && FSL_FEATURE_LTC_HAS_PKHA 1145 /******************************************************************************* 1146 * PKHA API 1147 ******************************************************************************/ 1148 /*! 1149 * @addtogroup ltc_driver_pkha 1150 * @{ 1151 */ 1152 1153 /*! 1154 * @brief Compare two PKHA big numbers. 1155 * 1156 * Compare two PKHA big numbers. Return 1 for a > b, -1 for a < b and 0 if they are same. 1157 * PKHA big number is lsbyte first. Thus the comparison starts at msbyte which is the last member of tested arrays. 1158 * 1159 * @param a First integer represented as an array of bytes, lsbyte first. 1160 * @param sizeA Size in bytes of the first integer. 1161 * @param b Second integer represented as an array of bytes, lsbyte first. 1162 * @param sizeB Size in bytes of the second integer. 1163 * @return 1 if a > b. 1164 * @return -1 if a < b. 1165 * @return 0 if a = b. 1166 */ 1167 int LTC_PKHA_CompareBigNum(const uint8_t *a, size_t sizeA, const uint8_t *b, size_t sizeB); 1168 1169 /*! 1170 * @brief Converts from integer to Montgomery format. 1171 * 1172 * This function computes R2 mod N and optionally converts A or B into Montgomery format of A or B. 1173 * 1174 * @param base LTC peripheral base address 1175 * @param N modulus 1176 * @param sizeN size of N in bytes 1177 * @param[in,out] A The first input in non-Montgomery format. Output Montgomery format of the first input. 1178 * @param[in,out] sizeA pointer to size variable. On input it holds size of input A in bytes. On output it holds size of 1179 * Montgomery format of A in bytes. 1180 * @param[in,out] B Second input in non-Montgomery format. Output Montgomery format of the second input. 1181 * @param[in,out] sizeB pointer to size variable. On input it holds size of input B in bytes. On output it holds size of 1182 * Montgomery format of B in bytes. 1183 * @param[out] R2 Output Montgomery factor R2 mod N. 1184 * @param[out] sizeR2 pointer to size variable. On output it holds size of Montgomery factor R2 mod N in bytes. 1185 * @param equalTime Run the function time equalized or no timing equalization. 1186 * @param arithType Type of arithmetic to perform (integer or F2m) 1187 * @return Operation status. 1188 */ 1189 status_t LTC_PKHA_NormalToMontgomery(LTC_Type *base, 1190 const uint8_t *N, 1191 uint16_t sizeN, 1192 uint8_t *A, 1193 uint16_t *sizeA, 1194 uint8_t *B, 1195 uint16_t *sizeB, 1196 uint8_t *R2, 1197 uint16_t *sizeR2, 1198 ltc_pkha_timing_t equalTime, 1199 ltc_pkha_f2m_t arithType); 1200 1201 /*! 1202 * @brief Converts from Montgomery format to int. 1203 * 1204 * This function converts Montgomery format of A or B into int A or B. 1205 * 1206 * @param base LTC peripheral base address 1207 * @param N modulus. 1208 * @param sizeN size of N modulus in bytes. 1209 * @param[in,out] A Input first number in Montgomery format. Output is non-Montgomery format. 1210 * @param[in,out] sizeA pointer to size variable. On input it holds size of the input A in bytes. On output it holds 1211 * size of non-Montgomery A in bytes. 1212 * @param[in,out] B Input first number in Montgomery format. Output is non-Montgomery format. 1213 * @param[in,out] sizeB pointer to size variable. On input it holds size of the input B in bytes. On output it holds 1214 * size of non-Montgomery B in bytes. 1215 * @param equalTime Run the function time equalized or no timing equalization. 1216 * @param arithType Type of arithmetic to perform (integer or F2m) 1217 * @return Operation status. 1218 */ 1219 status_t LTC_PKHA_MontgomeryToNormal(LTC_Type *base, 1220 const uint8_t *N, 1221 uint16_t sizeN, 1222 uint8_t *A, 1223 uint16_t *sizeA, 1224 uint8_t *B, 1225 uint16_t *sizeB, 1226 ltc_pkha_timing_t equalTime, 1227 ltc_pkha_f2m_t arithType); 1228 1229 /*! 1230 * @brief Performs modular addition - (A + B) mod N. 1231 * 1232 * This function performs modular addition of (A + B) mod N, with either 1233 * integer or binary polynomial (F2m) inputs. In the F2m form, this function is 1234 * equivalent to a bitwise XOR and it is functionally the same as subtraction. 1235 * 1236 * @param base LTC peripheral base address 1237 * @param A first addend (integer or binary polynomial) 1238 * @param sizeA Size of A in bytes 1239 * @param B second addend (integer or binary polynomial) 1240 * @param sizeB Size of B in bytes 1241 * @param N modulus. For F2m operation this can be NULL, as N is ignored during F2m polynomial addition. 1242 * @param sizeN Size of N in bytes. This must be given for both integer and F2m polynomial additions. 1243 * @param[out] result Output array to store result of operation 1244 * @param[out] resultSize Output size of operation in bytes 1245 * @param arithType Type of arithmetic to perform (integer or F2m) 1246 * @return Operation status. 1247 */ 1248 status_t LTC_PKHA_ModAdd(LTC_Type *base, 1249 const uint8_t *A, 1250 uint16_t sizeA, 1251 const uint8_t *B, 1252 uint16_t sizeB, 1253 const uint8_t *N, 1254 uint16_t sizeN, 1255 uint8_t *result, 1256 uint16_t *resultSize, 1257 ltc_pkha_f2m_t arithType); 1258 1259 /*! 1260 * @brief Performs modular subtraction - (A - B) mod N. 1261 * 1262 * This function performs modular subtraction of (A - B) mod N with 1263 * integer inputs. 1264 * 1265 * @param base LTC peripheral base address 1266 * @param A first addend (integer or binary polynomial) 1267 * @param sizeA Size of A in bytes 1268 * @param B second addend (integer or binary polynomial) 1269 * @param sizeB Size of B in bytes 1270 * @param N modulus 1271 * @param sizeN Size of N in bytes 1272 * @param[out] result Output array to store result of operation 1273 * @param[out] resultSize Output size of operation in bytes 1274 * @return Operation status. 1275 */ 1276 status_t LTC_PKHA_ModSub1(LTC_Type *base, 1277 const uint8_t *A, 1278 uint16_t sizeA, 1279 const uint8_t *B, 1280 uint16_t sizeB, 1281 const uint8_t *N, 1282 uint16_t sizeN, 1283 uint8_t *result, 1284 uint16_t *resultSize); 1285 1286 /*! 1287 * @brief Performs modular subtraction - (B - A) mod N. 1288 * 1289 * This function performs modular subtraction of (B - A) mod N, 1290 * with integer inputs. 1291 * 1292 * @param base LTC peripheral base address 1293 * @param A first addend (integer or binary polynomial) 1294 * @param sizeA Size of A in bytes 1295 * @param B second addend (integer or binary polynomial) 1296 * @param sizeB Size of B in bytes 1297 * @param N modulus 1298 * @param sizeN Size of N in bytes 1299 * @param[out] result Output array to store result of operation 1300 * @param[out] resultSize Output size of operation in bytes 1301 * @return Operation status. 1302 */ 1303 status_t LTC_PKHA_ModSub2(LTC_Type *base, 1304 const uint8_t *A, 1305 uint16_t sizeA, 1306 const uint8_t *B, 1307 uint16_t sizeB, 1308 const uint8_t *N, 1309 uint16_t sizeN, 1310 uint8_t *result, 1311 uint16_t *resultSize); 1312 1313 /*! 1314 * @brief Performs modular multiplication - (A x B) mod N. 1315 * 1316 * This function performs modular multiplication with either integer or 1317 * binary polynomial (F2m) inputs. It can optionally specify whether inputs 1318 * and/or outputs will be in Montgomery form or not. 1319 * 1320 * @param base LTC peripheral base address 1321 * @param A first addend (integer or binary polynomial) 1322 * @param sizeA Size of A in bytes 1323 * @param B second addend (integer or binary polynomial) 1324 * @param sizeB Size of B in bytes 1325 * @param N modulus. 1326 * @param sizeN Size of N in bytes 1327 * @param[out] result Output array to store result of operation 1328 * @param[out] resultSize Output size of operation in bytes 1329 * @param arithType Type of arithmetic to perform (integer or F2m) 1330 * @param montIn Format of inputs 1331 * @param montOut Format of output 1332 * @param equalTime Run the function time equalized or no timing equalization. This argument is ignored for F2m modular 1333 * multiplication. 1334 * @return Operation status. 1335 */ 1336 status_t LTC_PKHA_ModMul(LTC_Type *base, 1337 const uint8_t *A, 1338 uint16_t sizeA, 1339 const uint8_t *B, 1340 uint16_t sizeB, 1341 const uint8_t *N, 1342 uint16_t sizeN, 1343 uint8_t *result, 1344 uint16_t *resultSize, 1345 ltc_pkha_f2m_t arithType, 1346 ltc_pkha_montgomery_form_t montIn, 1347 ltc_pkha_montgomery_form_t montOut, 1348 ltc_pkha_timing_t equalTime); 1349 1350 /*! 1351 * @brief Performs modular exponentiation - (A^E) mod N. 1352 * 1353 * This function performs modular exponentiation with either integer or 1354 * binary polynomial (F2m) inputs. 1355 * 1356 * @param base LTC peripheral base address 1357 * @param A first addend (integer or binary polynomial) 1358 * @param sizeA Size of A in bytes 1359 * @param N modulus 1360 * @param sizeN Size of N in bytes 1361 * @param E exponent 1362 * @param sizeE Size of E in bytes 1363 * @param[out] result Output array to store result of operation 1364 * @param[out] resultSize Output size of operation in bytes 1365 * @param montIn Format of A input (normal or Montgomery) 1366 * @param arithType Type of arithmetic to perform (integer or F2m) 1367 * @param equalTime Run the function time equalized or no timing equalization. 1368 * @return Operation status. 1369 */ 1370 status_t LTC_PKHA_ModExp(LTC_Type *base, 1371 const uint8_t *A, 1372 uint16_t sizeA, 1373 const uint8_t *N, 1374 uint16_t sizeN, 1375 const uint8_t *E, 1376 uint16_t sizeE, 1377 uint8_t *result, 1378 uint16_t *resultSize, 1379 ltc_pkha_f2m_t arithType, 1380 ltc_pkha_montgomery_form_t montIn, 1381 ltc_pkha_timing_t equalTime); 1382 1383 /*! 1384 * @brief Performs modular reduction - (A) mod N. 1385 * 1386 * This function performs modular reduction with either integer or 1387 * binary polynomial (F2m) inputs. 1388 * 1389 * @param base LTC peripheral base address 1390 * @param A first addend (integer or binary polynomial) 1391 * @param sizeA Size of A in bytes 1392 * @param N modulus 1393 * @param sizeN Size of N in bytes 1394 * @param[out] result Output array to store result of operation 1395 * @param[out] resultSize Output size of operation in bytes 1396 * @param arithType Type of arithmetic to perform (integer or F2m) 1397 * @return Operation status. 1398 */ 1399 status_t LTC_PKHA_ModRed(LTC_Type *base, 1400 const uint8_t *A, 1401 uint16_t sizeA, 1402 const uint8_t *N, 1403 uint16_t sizeN, 1404 uint8_t *result, 1405 uint16_t *resultSize, 1406 ltc_pkha_f2m_t arithType); 1407 1408 /*! 1409 * @brief Performs modular inversion - (A^-1) mod N. 1410 * 1411 * This function performs modular inversion with either integer or 1412 * binary polynomial (F2m) inputs. 1413 * 1414 * @param base LTC peripheral base address 1415 * @param A first addend (integer or binary polynomial) 1416 * @param sizeA Size of A in bytes 1417 * @param N modulus 1418 * @param sizeN Size of N in bytes 1419 * @param[out] result Output array to store result of operation 1420 * @param[out] resultSize Output size of operation in bytes 1421 * @param arithType Type of arithmetic to perform (integer or F2m) 1422 * @return Operation status. 1423 */ 1424 status_t LTC_PKHA_ModInv(LTC_Type *base, 1425 const uint8_t *A, 1426 uint16_t sizeA, 1427 const uint8_t *N, 1428 uint16_t sizeN, 1429 uint8_t *result, 1430 uint16_t *resultSize, 1431 ltc_pkha_f2m_t arithType); 1432 1433 /*! 1434 * @brief Computes integer Montgomery factor R^2 mod N. 1435 * 1436 * This function computes a constant to assist in converting operands 1437 * into the Montgomery residue system representation. 1438 * 1439 * @param base LTC peripheral base address 1440 * @param N modulus 1441 * @param sizeN Size of N in bytes 1442 * @param[out] result Output array to store result of operation 1443 * @param[out] resultSize Output size of operation in bytes 1444 * @param arithType Type of arithmetic to perform (integer or F2m) 1445 * @return Operation status. 1446 */ 1447 status_t LTC_PKHA_ModR2( 1448 LTC_Type *base, const uint8_t *N, uint16_t sizeN, uint8_t *result, uint16_t *resultSize, ltc_pkha_f2m_t arithType); 1449 1450 /*! 1451 * @brief Calculates the greatest common divisor - GCD (A, N). 1452 * 1453 * This function calculates the greatest common divisor of two inputs with 1454 * either integer or binary polynomial (F2m) inputs. 1455 * 1456 * @param base LTC peripheral base address 1457 * @param A first value (must be smaller than or equal to N) 1458 * @param sizeA Size of A in bytes 1459 * @param N second value (must be non-zero) 1460 * @param sizeN Size of N in bytes 1461 * @param[out] result Output array to store result of operation 1462 * @param[out] resultSize Output size of operation in bytes 1463 * @param arithType Type of arithmetic to perform (integer or F2m) 1464 * @return Operation status. 1465 */ 1466 status_t LTC_PKHA_GCD(LTC_Type *base, 1467 const uint8_t *A, 1468 uint16_t sizeA, 1469 const uint8_t *N, 1470 uint16_t sizeN, 1471 uint8_t *result, 1472 uint16_t *resultSize, 1473 ltc_pkha_f2m_t arithType); 1474 1475 /*! 1476 * @brief Executes Miller-Rabin primality test. 1477 * 1478 * This function calculates whether or not a candidate prime number is likely 1479 * to be a prime. 1480 * 1481 * @param base LTC peripheral base address 1482 * @param A initial random seed 1483 * @param sizeA Size of A in bytes 1484 * @param B number of trial runs 1485 * @param sizeB Size of B in bytes 1486 * @param N candidate prime integer 1487 * @param sizeN Size of N in bytes 1488 * @param[out] res True if the value is likely prime or false otherwise 1489 * @return Operation status. 1490 */ 1491 status_t LTC_PKHA_PrimalityTest(LTC_Type *base, 1492 const uint8_t *A, 1493 uint16_t sizeA, 1494 const uint8_t *B, 1495 uint16_t sizeB, 1496 const uint8_t *N, 1497 uint16_t sizeN, 1498 bool *res); 1499 1500 /*! 1501 * @brief Adds elliptic curve points - A + B. 1502 * 1503 * This function performs ECC point addition over a prime field (Fp) or binary field (F2m) using 1504 * affine coordinates. 1505 * 1506 * @param base LTC peripheral base address 1507 * @param A Left-hand point 1508 * @param B Right-hand point 1509 * @param N Prime modulus of the field 1510 * @param R2modN NULL (the function computes R2modN internally) or pointer to pre-computed R2modN (obtained from 1511 * LTC_PKHA_ModR2() function). 1512 * @param aCurveParam A parameter from curve equation 1513 * @param bCurveParam B parameter from curve equation (constant) 1514 * @param size Size in bytes of curve points and parameters 1515 * @param arithType Type of arithmetic to perform (integer or F2m) 1516 * @param[out] result Result point 1517 * @return Operation status. 1518 */ 1519 status_t LTC_PKHA_ECC_PointAdd(LTC_Type *base, 1520 const ltc_pkha_ecc_point_t *A, 1521 const ltc_pkha_ecc_point_t *B, 1522 const uint8_t *N, 1523 const uint8_t *R2modN, 1524 const uint8_t *aCurveParam, 1525 const uint8_t *bCurveParam, 1526 uint8_t size, 1527 ltc_pkha_f2m_t arithType, 1528 ltc_pkha_ecc_point_t *result); 1529 1530 /*! 1531 * @brief Doubles elliptic curve points - B + B. 1532 * 1533 * This function performs ECC point doubling over a prime field (Fp) or binary field (F2m) using 1534 * affine coordinates. 1535 * 1536 * @param base LTC peripheral base address 1537 * @param B Point to double 1538 * @param N Prime modulus of the field 1539 * @param aCurveParam A parameter from curve equation 1540 * @param bCurveParam B parameter from curve equation (constant) 1541 * @param size Size in bytes of curve points and parameters 1542 * @param arithType Type of arithmetic to perform (integer or F2m) 1543 * @param[out] result Result point 1544 * @return Operation status. 1545 */ 1546 status_t LTC_PKHA_ECC_PointDouble(LTC_Type *base, 1547 const ltc_pkha_ecc_point_t *B, 1548 const uint8_t *N, 1549 const uint8_t *aCurveParam, 1550 const uint8_t *bCurveParam, 1551 uint8_t size, 1552 ltc_pkha_f2m_t arithType, 1553 ltc_pkha_ecc_point_t *result); 1554 1555 /*! 1556 * @brief Multiplies an elliptic curve point by a scalar - E x (A0, A1). 1557 * 1558 * This function performs ECC point multiplication to multiply an ECC point by 1559 * a scalar integer multiplier over a prime field (Fp) or a binary field (F2m). 1560 * 1561 * @param base LTC peripheral base address 1562 * @param A Point as multiplicand 1563 * @param E Scalar multiple 1564 * @param sizeE The size of E, in bytes 1565 * @param N Modulus, a prime number for the Fp field or Irreducible polynomial for F2m field. 1566 * @param R2modN NULL (the function computes R2modN internally) or pointer to pre-computed R2modN (obtained from 1567 * LTC_PKHA_ModR2() function). 1568 * @param aCurveParam A parameter from curve equation 1569 * @param bCurveParam B parameter from curve equation (C parameter for operation over F2m). 1570 * @param size Size in bytes of curve points and parameters 1571 * @param equalTime Run the function time equalized or no timing equalization. 1572 * @param arithType Type of arithmetic to perform (integer or F2m) 1573 * @param[out] result Result point 1574 * @param[out] infinity Output true if the result is point of infinity, and false otherwise. Writing of this output will 1575 * be ignored if the argument is NULL. 1576 * @return Operation status. 1577 */ 1578 status_t LTC_PKHA_ECC_PointMul(LTC_Type *base, 1579 const ltc_pkha_ecc_point_t *A, 1580 const uint8_t *E, 1581 uint8_t sizeE, 1582 const uint8_t *N, 1583 const uint8_t *R2modN, 1584 const uint8_t *aCurveParam, 1585 const uint8_t *bCurveParam, 1586 uint8_t size, 1587 ltc_pkha_timing_t equalTime, 1588 ltc_pkha_f2m_t arithType, 1589 ltc_pkha_ecc_point_t *result, 1590 bool *infinity); 1591 #endif /* FSL_FEATURE_LTC_HAS_PKHA */ 1592 1593 /*! 1594 *@} 1595 */ 1596 1597 /******************************************************************************* 1598 * Private - only used internally to share code between fsl_ltc_edma.c and fsl_ltc.c 1599 ******************************************************************************/ 1600 /*! 1601 * @internal 1602 * @{ 1603 */ 1604 1605 #define LTC_MD_ALG_AES (0x10U) /*!< Bit field value for LTC_MD_ALG: AES */ 1606 #define LTC_MD_ALG_DES (0x20U) /*!< Bit field value for LTC_MD_ALG: DES */ 1607 #define LTC_MD_ALG_TRIPLE_DES (0x21U) /*!< Bit field value for LTC_MD_ALG: 3DES */ 1608 #define LTC_MD_ALG_SHA1 (0x41U) /*!< Bit field value for LTC_MD_ALG: SHA-1 */ 1609 #define LTC_MD_ALG_SHA224 (0x42U) /*!< Bit field value for LTC_MD_ALG: SHA-224 */ 1610 #define LTC_MD_ALG_SHA256 (0x43U) /*!< Bit field value for LTC_MD_ALG: SHA-256 */ 1611 #define LTC_MDPK_ALG_PKHA (0x80U) /*!< Bit field value for LTC_MDPK_ALG: PKHA */ 1612 #define LTC_MD_ENC_DECRYPT (0U) /*!< Bit field value for LTC_MD_ENC: Decrypt. */ 1613 #define LTC_MD_ENC_ENCRYPT (0x1U) /*!< Bit field value for LTC_MD_ENC: Encrypt. */ 1614 #define LTC_MD_AS_UPDATE (0U) /*!< Bit field value for LTC_MD_AS: Update */ 1615 #define LTC_MD_AS_INITIALIZE (0x1U) /*!< Bit field value for LTC_MD_AS: Initialize */ 1616 #define LTC_MD_AS_FINALIZE (0x2U) /*!< Bit field value for LTC_MD_AS: Finalize */ 1617 #define LTC_MD_AS_INIT_FINAL (0x3U) /*!< Bit field value for LTC_MD_AS: Initialize/Finalize */ 1618 1619 /*! Full word representing the actual bit values for the LTC mode register. */ 1620 typedef uint32_t ltc_mode_t; 1621 1622 typedef enum _ltc_algorithm 1623 { 1624 #if defined(FSL_FEATURE_LTC_HAS_PKHA) && FSL_FEATURE_LTC_HAS_PKHA 1625 kLTC_AlgorithmPKHA = LTC_MDPK_ALG_PKHA << LTC_MD_ALG_SHIFT, 1626 #endif /* FSL_FEATURE_LTC_HAS_PKHA */ 1627 kLTC_AlgorithmAES = LTC_MD_ALG_AES << LTC_MD_ALG_SHIFT, 1628 #if defined(FSL_FEATURE_LTC_HAS_DES) && FSL_FEATURE_LTC_HAS_DES 1629 kLTC_AlgorithmDES = LTC_MD_ALG_DES << LTC_MD_ALG_SHIFT, 1630 kLTC_Algorithm3DES = LTC_MD_ALG_TRIPLE_DES << LTC_MD_ALG_SHIFT, 1631 #endif /* FSL_FEATURE_LTC_HAS_DES */ 1632 #if defined(FSL_FEATURE_LTC_HAS_SHA) && FSL_FEATURE_LTC_HAS_SHA 1633 kLTC_AlgorithmSHA1 = LTC_MD_ALG_SHA1 << LTC_MD_ALG_SHIFT, 1634 kLTC_AlgorithmSHA224 = LTC_MD_ALG_SHA224 << LTC_MD_ALG_SHIFT, 1635 kLTC_AlgorithmSHA256 = LTC_MD_ALG_SHA256 << LTC_MD_ALG_SHIFT, 1636 #endif /* FSL_FEATURE_LTC_HAS_SHA */ 1637 } ltc_algorithm_t; 1638 1639 typedef enum _ltc_mode_symmetric_alg 1640 { 1641 kLTC_ModeCTR = 0x00U << LTC_MD_AAI_SHIFT, 1642 kLTC_ModeCBC = 0x10U << LTC_MD_AAI_SHIFT, 1643 kLTC_ModeECB = 0x20U << LTC_MD_AAI_SHIFT, 1644 kLTC_ModeCFB = 0x30U << LTC_MD_AAI_SHIFT, 1645 kLTC_ModeOFB = 0x40U << LTC_MD_AAI_SHIFT, 1646 kLTC_ModeCMAC = 0x60U << LTC_MD_AAI_SHIFT, 1647 kLTC_ModeXCBCMAC = 0x70U << LTC_MD_AAI_SHIFT, 1648 kLTC_ModeCCM = 0x80U << LTC_MD_AAI_SHIFT, 1649 kLTC_ModeGCM = 0x90U << LTC_MD_AAI_SHIFT, 1650 } ltc_mode_symmetric_alg_t; 1651 1652 typedef enum _ltc_mode_encrypt 1653 { 1654 kLTC_ModeDecrypt = LTC_MD_ENC_DECRYPT << LTC_MD_ENC_SHIFT, 1655 kLTC_ModeEncrypt = LTC_MD_ENC_ENCRYPT << LTC_MD_ENC_SHIFT, 1656 } ltc_mode_encrypt_t; 1657 1658 typedef enum _ltc_mode_algorithm_state 1659 { 1660 kLTC_ModeUpdate = LTC_MD_AS_UPDATE << LTC_MD_AS_SHIFT, 1661 kLTC_ModeInit = LTC_MD_AS_INITIALIZE << LTC_MD_AS_SHIFT, 1662 kLTC_ModeFinalize = LTC_MD_AS_FINALIZE << LTC_MD_AS_SHIFT, 1663 kLTC_ModeInitFinal = LTC_MD_AS_INIT_FINAL << LTC_MD_AS_SHIFT 1664 } ltc_mode_algorithm_state_t; 1665 1666 extern status_t ltc_get_context(LTC_Type *base, uint8_t *dest, uint8_t dataSize, uint8_t startIndex); 1667 extern status_t ltc_set_context(LTC_Type *base, const uint8_t *data, uint8_t dataSize, uint8_t startIndex); 1668 extern status_t ltc_symmetric_update(LTC_Type *base, 1669 const uint8_t *key, 1670 uint8_t keySize, 1671 ltc_algorithm_t alg, 1672 ltc_mode_symmetric_alg_t mode, 1673 ltc_mode_encrypt_t enc); 1674 extern void ltc_memcpy(void *dst, const void *src, size_t size); 1675 extern bool ltc_check_key_size(const uint32_t keySize); 1676 extern status_t ltc_wait(LTC_Type *base); 1677 extern void ltc_clear_all(LTC_Type *base, bool addPKHA); 1678 #if defined(FSL_FEATURE_LTC_HAS_DES) && FSL_FEATURE_LTC_HAS_DES 1679 extern status_t ltc_3des_check_input_args(ltc_mode_symmetric_alg_t modeAs, 1680 uint32_t size, 1681 const uint8_t *key1, 1682 const uint8_t *key2); 1683 #endif /* FSL_FEATURE_LTC_HAS_DES */ 1684 extern void ltc_symmetric_process(LTC_Type *base, uint32_t inSize, const uint8_t **inData, uint8_t **outData); 1685 extern status_t ltc_symmetric_process_data(LTC_Type *base, const uint8_t *inData, uint32_t inSize, uint8_t *outData); 1686 /*! 1687 *@} 1688 */ 1689 1690 #if defined(__cplusplus) 1691 } 1692 #endif 1693 1694 /*! 1695 *@} 1696 */ 1697 1698 #endif /* _FSL_LTC_H_ */ 1699