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