hv_hdcp_BigDigits.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * @file hv_hdcp_BigDigits.h
  3. * @brief hdcp engine interface.
  4. * @verbatim
  5. * ==============================================================================
  6. * ##### How to use this driver #####
  7. * ==============================================================================
  8. * (+) Use hv_engine_Divide(...) to Divide Computes integer division of u by v such that u=qv+r .
  9. * (+) Use hv_engine_Multiply(...) to Computes product w = u * v.
  10. * (+) Use hv_engine_Modulo(...) to Computes remainder r = u mod v.
  11. * @endverbatim
  12. *
  13. * @author HiView SoC Software Team
  14. * @version 0.0.1
  15. * @date 2022-08-22
  16. */
  17. #include "hv_vos_Comm.h"
  18. #ifndef HV_HDCP_BIGDIGITS_H_
  19. #define HV_HDCP_BIGDIGITS_H_
  20. #include "Common/hv_comm_DataType.h"
  21. /** Computes w = u + v, returns carry */
  22. UINT32 Hv_Engine_Add(UINT32 w[], const UINT32 u[], const UINT32 v[], size_t ndigits);
  23. /** Computes w = u - v, returns borrow */
  24. UINT32 Hv_Engine_Subtract(UINT32 w[], const UINT32 u[], const UINT32 v[], size_t ndigits);
  25. /** Computes product w = u * v
  26. @param[out] w To receive the product, an array of size 2 x `ndigits`
  27. @param[in] u An array of size `ndigits`
  28. @param[in] v An array of size `ndigits`
  29. @param[in] ndigits size of arrays `u` and `v`
  30. @warning The product must be of size 2 x `ndigits`
  31. */
  32. int Hv_Engine_Multiply(UINT32 w[], const UINT32 u[], const UINT32 v[], size_t ndigits);
  33. /** Computes integer division of u by v such that u=qv+r
  34. @param[out] q to receive quotient = u div v, an array of size `udigits`
  35. @param[out] r to receive divisor = u mod v, an array of size `udigits`
  36. @param[in] u dividend of size `udigits`
  37. @param[in] udigits size of arrays `q` `r` and `u`
  38. @param[in] v divisor of size `vdigits`
  39. @param[in] vdigits size of array `v`
  40. @warning Trashes q and r first
  41. */
  42. int Hv_Engine_Divide(UINT32 q[], UINT32 r[], const UINT32 u[],
  43. size_t udigits, UINT32 v[], size_t vdigits);
  44. /** Computes remainder r = u mod v
  45. @param[out] r to receive divisor = u mod v, an array of size `vdigits`
  46. @param[in] u dividend of size `udigits`
  47. @param[in] udigits size of arrays `r` and `u`
  48. @param[in] v divisor of size `vdigits`
  49. @param[in] vdigits size of array `v`
  50. @remark Note that `r` is `vdigits` long here, but is `udigits` long in mpDivide().
  51. */
  52. int Hv_Engine_Modulo(UINT32 r[], const UINT32 u[], size_t udigits, UINT32 v[], size_t vdigits);
  53. /** Computes square w = x^2
  54. @param[out] w array of size 2 x `ndigits` to receive square
  55. @param[in] x array of size `ndigits`
  56. @param[in] ndigits size of array `x`
  57. @warning The product `w` must be of size 2 x `ndigits`
  58. */
  59. int Hv_Engine_Square(UINT32 w[], const UINT32 x[], size_t ndigits);
  60. /** Returns true if a is zero, else false, using constant-time algorithm
  61. * @remark Constant-time with respect to `ndigits`
  62. */
  63. int Hv_Engine_IsZero(const UINT32 a[], size_t ndigits);
  64. /*************************/
  65. /* COMPARISON OPERATIONS */
  66. /*************************/
  67. /* [v2.5] Changed to constant-time algorithms */
  68. /** Returns true if a == b, else false, using constant-time algorithm
  69. * @remark Constant-time with respect to `ndigits`
  70. */
  71. int Hv_Engine_Equal(const UINT32 a[], const UINT32 b[], size_t ndigits);
  72. /** Returns sign of `(a-b)` as `{-1,0,+1}` using constant-time algorithm
  73. * @remark Constant-time with respect to `ndigits`
  74. */
  75. int Hv_Engine_Compare(const UINT32 a[], const UINT32 b[], size_t ndigits);
  76. /** Computes a = (x * y) mod m */
  77. int Hv_Engine_ModMult(UINT32 a[], const UINT32 x[], const UINT32 y[], UINT32 m[], size_t ndigits);
  78. /** Computes the inverse of `u` modulo `m`, inv = u^{-1} mod m */
  79. int Hv_Engine_ModInv(UINT32 inv[], const UINT32 u[], const UINT32 m[], size_t ndigits);
  80. /** Returns number of significant bits in a */
  81. size_t Hv_Engine_BitLength(const UINT32 a[], size_t ndigits);
  82. /** Computes a = b << x */
  83. UINT32 Hv_Engine_ShiftLeft(UINT32 a[], const UINT32 b[], size_t x, size_t ndigits);
  84. /** Computes a = b >> x */
  85. UINT32 Hv_Engine_ShiftRight(UINT32 a[], const UINT32 b[], size_t x, size_t ndigits);
  86. /** Sets bit n of a (0..nbits-1) with value 1 or 0 */
  87. int mpSetBit(UINT32 a[], size_t ndigits, UINT32 n, int value);
  88. /** Sets a = 0 */
  89. volatile UINT32 Hv_Engine_SetZero(volatile UINT32 a[], size_t ndigits);
  90. /** Sets a = d where d is a single digit */
  91. void Hv_Engine_SetDigit(UINT32 a[], UINT32 d, size_t ndigits);
  92. /** Sets a = b */
  93. void Hv_Engine_SetEqual(UINT32 a[], const UINT32 b[], size_t ndigits);
  94. /** Returns value 1 or 0 of bit n (0..nbits-1) */
  95. int Hv_Engine_GetBit(UINT32 a[], size_t ndigits, size_t n);
  96. /** Returns number of significant non-zero digits in a */
  97. size_t Hv_Engine_Sizeof(const UINT32 a[], size_t ndigits);
  98. /** Computes quotient q = u div d, returns remainder */
  99. UINT32 Hv_Engine_ShortDiv(UINT32 q[], const UINT32 u[], UINT32 d, size_t ndigits);
  100. /** Returns sign of (a - d) where d is a single digit */
  101. int Hv_Engine_ShortCmp(const UINT32 a[], UINT32 d, size_t ndigits);
  102. /** Computes p = x * y, where x and y are single digits */
  103. int Hv_Engine_DualMultiply(UINT32 p[2], UINT32 x, UINT32 y);
  104. /** Computes quotient q = u div v, remainder r = u mod v, where q, r and v are single digits */
  105. UINT32 Hv_Engine_DualDivide(UINT32 *q, UINT32 *r, const UINT32 u[2], UINT32 v);
  106. size_t Hv_Engine_ConvFromOctets(UINT32 a[], size_t ndigits, const unsigned char *c, size_t nbytes);
  107. /** Converts big digit a into string of octets, in big-endian order, padding to nbytes or truncating if necessary.
  108. @returns number of non-zero octets required. */
  109. size_t Hv_Engine_ConvToOctets(const UINT32 a[], size_t ndigits, unsigned char *c, size_t nbytes);
  110. /** Computes y = x^e mod m */
  111. int Hv_Engine_ModExp(UINT32 y[], const UINT32 x[], const UINT32 e[], UINT32 m[], size_t ndigits);
  112. /** @endcond */
  113. #endif /* BIGDIGITS_H_ */