curve25519-ppc64le-core.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2024- IBM Corp.
  4. *
  5. * X25519 scalar multiplication with 51 bits limbs for PPC64le.
  6. * Based on RFC7748 and AArch64 optimized implementation for X25519
  7. * - Algorithm 1 Scalar multiplication of a variable point
  8. */
  9. #include <crypto/curve25519.h>
  10. #include <crypto/internal/kpp.h>
  11. #include <linux/types.h>
  12. #include <linux/jump_label.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/scatterlist.h>
  16. #include <linux/cpufeature.h>
  17. #include <linux/processor.h>
  18. typedef uint64_t fe51[5];
  19. asmlinkage void x25519_fe51_mul(fe51 h, const fe51 f, const fe51 g);
  20. asmlinkage void x25519_fe51_sqr(fe51 h, const fe51 f);
  21. asmlinkage void x25519_fe51_mul121666(fe51 h, fe51 f);
  22. asmlinkage void x25519_fe51_sqr_times(fe51 h, const fe51 f, int n);
  23. asmlinkage void x25519_fe51_frombytes(fe51 h, const uint8_t *s);
  24. asmlinkage void x25519_fe51_tobytes(uint8_t *s, const fe51 h);
  25. asmlinkage void x25519_cswap(fe51 p, fe51 q, unsigned int bit);
  26. #define fmul x25519_fe51_mul
  27. #define fsqr x25519_fe51_sqr
  28. #define fmul121666 x25519_fe51_mul121666
  29. #define fe51_tobytes x25519_fe51_tobytes
  30. static void fadd(fe51 h, const fe51 f, const fe51 g)
  31. {
  32. h[0] = f[0] + g[0];
  33. h[1] = f[1] + g[1];
  34. h[2] = f[2] + g[2];
  35. h[3] = f[3] + g[3];
  36. h[4] = f[4] + g[4];
  37. }
  38. /*
  39. * Prime = 2 ** 255 - 19, 255 bits
  40. * (0x7fffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffed)
  41. *
  42. * Prime in 5 51-bit limbs
  43. */
  44. static fe51 prime51 = { 0x7ffffffffffed, 0x7ffffffffffff, 0x7ffffffffffff, 0x7ffffffffffff, 0x7ffffffffffff};
  45. static void fsub(fe51 h, const fe51 f, const fe51 g)
  46. {
  47. h[0] = (f[0] + ((prime51[0] * 2))) - g[0];
  48. h[1] = (f[1] + ((prime51[1] * 2))) - g[1];
  49. h[2] = (f[2] + ((prime51[2] * 2))) - g[2];
  50. h[3] = (f[3] + ((prime51[3] * 2))) - g[3];
  51. h[4] = (f[4] + ((prime51[4] * 2))) - g[4];
  52. }
  53. static void fe51_frombytes(fe51 h, const uint8_t *s)
  54. {
  55. /*
  56. * Make sure 64-bit aligned.
  57. */
  58. unsigned char sbuf[32+8];
  59. unsigned char *sb = PTR_ALIGN((void *)sbuf, 8);
  60. memcpy(sb, s, 32);
  61. x25519_fe51_frombytes(h, sb);
  62. }
  63. static void finv(fe51 o, const fe51 i)
  64. {
  65. fe51 a0, b, c, t00;
  66. fsqr(a0, i);
  67. x25519_fe51_sqr_times(t00, a0, 2);
  68. fmul(b, t00, i);
  69. fmul(a0, b, a0);
  70. fsqr(t00, a0);
  71. fmul(b, t00, b);
  72. x25519_fe51_sqr_times(t00, b, 5);
  73. fmul(b, t00, b);
  74. x25519_fe51_sqr_times(t00, b, 10);
  75. fmul(c, t00, b);
  76. x25519_fe51_sqr_times(t00, c, 20);
  77. fmul(t00, t00, c);
  78. x25519_fe51_sqr_times(t00, t00, 10);
  79. fmul(b, t00, b);
  80. x25519_fe51_sqr_times(t00, b, 50);
  81. fmul(c, t00, b);
  82. x25519_fe51_sqr_times(t00, c, 100);
  83. fmul(t00, t00, c);
  84. x25519_fe51_sqr_times(t00, t00, 50);
  85. fmul(t00, t00, b);
  86. x25519_fe51_sqr_times(t00, t00, 5);
  87. fmul(o, t00, a0);
  88. }
  89. static void curve25519_fe51(uint8_t out[32], const uint8_t scalar[32],
  90. const uint8_t point[32])
  91. {
  92. fe51 x1, x2, z2, x3, z3;
  93. uint8_t s[32];
  94. unsigned int swap = 0;
  95. int i;
  96. memcpy(s, scalar, 32);
  97. s[0] &= 0xf8;
  98. s[31] &= 0x7f;
  99. s[31] |= 0x40;
  100. fe51_frombytes(x1, point);
  101. z2[0] = z2[1] = z2[2] = z2[3] = z2[4] = 0;
  102. x3[0] = x1[0];
  103. x3[1] = x1[1];
  104. x3[2] = x1[2];
  105. x3[3] = x1[3];
  106. x3[4] = x1[4];
  107. x2[0] = z3[0] = 1;
  108. x2[1] = z3[1] = 0;
  109. x2[2] = z3[2] = 0;
  110. x2[3] = z3[3] = 0;
  111. x2[4] = z3[4] = 0;
  112. for (i = 254; i >= 0; --i) {
  113. unsigned int k_t = 1 & (s[i / 8] >> (i & 7));
  114. fe51 a, b, c, d, e;
  115. fe51 da, cb, aa, bb;
  116. fe51 dacb_p, dacb_m;
  117. swap ^= k_t;
  118. x25519_cswap(x2, x3, swap);
  119. x25519_cswap(z2, z3, swap);
  120. swap = k_t;
  121. fsub(b, x2, z2); // B = x_2 - z_2
  122. fadd(a, x2, z2); // A = x_2 + z_2
  123. fsub(d, x3, z3); // D = x_3 - z_3
  124. fadd(c, x3, z3); // C = x_3 + z_3
  125. fsqr(bb, b); // BB = B^2
  126. fsqr(aa, a); // AA = A^2
  127. fmul(da, d, a); // DA = D * A
  128. fmul(cb, c, b); // CB = C * B
  129. fsub(e, aa, bb); // E = AA - BB
  130. fmul(x2, aa, bb); // x2 = AA * BB
  131. fadd(dacb_p, da, cb); // DA + CB
  132. fsub(dacb_m, da, cb); // DA - CB
  133. fmul121666(z3, e); // 121666 * E
  134. fsqr(z2, dacb_m); // (DA - CB)^2
  135. fsqr(x3, dacb_p); // x3 = (DA + CB)^2
  136. fadd(b, bb, z3); // BB + 121666 * E
  137. fmul(z3, x1, z2); // z3 = x1 * (DA - CB)^2
  138. fmul(z2, e, b); // z2 = e * (BB + (DA + CB)^2)
  139. }
  140. finv(z2, z2);
  141. fmul(x2, x2, z2);
  142. fe51_tobytes(out, x2);
  143. }
  144. void curve25519_arch(u8 mypublic[CURVE25519_KEY_SIZE],
  145. const u8 secret[CURVE25519_KEY_SIZE],
  146. const u8 basepoint[CURVE25519_KEY_SIZE])
  147. {
  148. curve25519_fe51(mypublic, secret, basepoint);
  149. }
  150. EXPORT_SYMBOL(curve25519_arch);
  151. void curve25519_base_arch(u8 pub[CURVE25519_KEY_SIZE],
  152. const u8 secret[CURVE25519_KEY_SIZE])
  153. {
  154. curve25519_fe51(pub, secret, curve25519_base_point);
  155. }
  156. EXPORT_SYMBOL(curve25519_base_arch);
  157. static int curve25519_set_secret(struct crypto_kpp *tfm, const void *buf,
  158. unsigned int len)
  159. {
  160. u8 *secret = kpp_tfm_ctx(tfm);
  161. if (!len)
  162. curve25519_generate_secret(secret);
  163. else if (len == CURVE25519_KEY_SIZE &&
  164. crypto_memneq(buf, curve25519_null_point, CURVE25519_KEY_SIZE))
  165. memcpy(secret, buf, CURVE25519_KEY_SIZE);
  166. else
  167. return -EINVAL;
  168. return 0;
  169. }
  170. static int curve25519_generate_public_key(struct kpp_request *req)
  171. {
  172. struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
  173. const u8 *secret = kpp_tfm_ctx(tfm);
  174. u8 buf[CURVE25519_KEY_SIZE];
  175. int copied, nbytes;
  176. if (req->src)
  177. return -EINVAL;
  178. curve25519_base_arch(buf, secret);
  179. /* might want less than we've got */
  180. nbytes = min_t(size_t, CURVE25519_KEY_SIZE, req->dst_len);
  181. copied = sg_copy_from_buffer(req->dst, sg_nents_for_len(req->dst,
  182. nbytes),
  183. buf, nbytes);
  184. if (copied != nbytes)
  185. return -EINVAL;
  186. return 0;
  187. }
  188. static int curve25519_compute_shared_secret(struct kpp_request *req)
  189. {
  190. struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
  191. const u8 *secret = kpp_tfm_ctx(tfm);
  192. u8 public_key[CURVE25519_KEY_SIZE];
  193. u8 buf[CURVE25519_KEY_SIZE];
  194. int copied, nbytes;
  195. if (!req->src)
  196. return -EINVAL;
  197. copied = sg_copy_to_buffer(req->src,
  198. sg_nents_for_len(req->src,
  199. CURVE25519_KEY_SIZE),
  200. public_key, CURVE25519_KEY_SIZE);
  201. if (copied != CURVE25519_KEY_SIZE)
  202. return -EINVAL;
  203. curve25519_arch(buf, secret, public_key);
  204. /* might want less than we've got */
  205. nbytes = min_t(size_t, CURVE25519_KEY_SIZE, req->dst_len);
  206. copied = sg_copy_from_buffer(req->dst, sg_nents_for_len(req->dst,
  207. nbytes),
  208. buf, nbytes);
  209. if (copied != nbytes)
  210. return -EINVAL;
  211. return 0;
  212. }
  213. static unsigned int curve25519_max_size(struct crypto_kpp *tfm)
  214. {
  215. return CURVE25519_KEY_SIZE;
  216. }
  217. static struct kpp_alg curve25519_alg = {
  218. .base.cra_name = "curve25519",
  219. .base.cra_driver_name = "curve25519-ppc64le",
  220. .base.cra_priority = 200,
  221. .base.cra_module = THIS_MODULE,
  222. .base.cra_ctxsize = CURVE25519_KEY_SIZE,
  223. .set_secret = curve25519_set_secret,
  224. .generate_public_key = curve25519_generate_public_key,
  225. .compute_shared_secret = curve25519_compute_shared_secret,
  226. .max_size = curve25519_max_size,
  227. };
  228. static int __init curve25519_mod_init(void)
  229. {
  230. return IS_REACHABLE(CONFIG_CRYPTO_KPP) ?
  231. crypto_register_kpp(&curve25519_alg) : 0;
  232. }
  233. static void __exit curve25519_mod_exit(void)
  234. {
  235. if (IS_REACHABLE(CONFIG_CRYPTO_KPP))
  236. crypto_unregister_kpp(&curve25519_alg);
  237. }
  238. module_init(curve25519_mod_init);
  239. module_exit(curve25519_mod_exit);
  240. MODULE_ALIAS_CRYPTO("curve25519");
  241. MODULE_ALIAS_CRYPTO("curve25519-ppc64le");
  242. MODULE_DESCRIPTION("PPC64le Curve25519 scalar multiplication with 51 bits limbs");
  243. MODULE_LICENSE("GPL v2");
  244. MODULE_AUTHOR("Danny Tsen <dtsen@us.ibm.com>");