You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

358 lines
8.3 KiB

  1. # RSA and Homomorphic Multiplication
  2. <img src="https://arnaucube.com/img/logoArnauCubeTransparent.png" style="max-width:20%; float:right;" />
  3. - https://arnaucube.com
  4. - https://github.com/arnaucube
  5. - https://twitter.com/arnaucube
  6. <br><br><br>
  7. <div style="float:right;font-size:80%;">
  8. <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/"><img src="https://licensebuttons.net/l/by-nc-sa/4.0/88x31.png" /></a>
  9. <br>
  10. 2018-11-30
  11. </div>
  12. ---
  13. - Intro
  14. - Public key cryptography
  15. - Basics of modular arithmetic
  16. - Brief history of RSA
  17. - Keys generation
  18. - Prime numbers
  19. - Encryption
  20. - Decryption
  21. - What's going on in encryption and decryption?
  22. - Signature
  23. - Verification of the signature
  24. - Homomorphic Multiplication with RSA
  25. - Resources
  26. ---
  27. # Intro
  28. - I'm not an expert on the field, neither a mathematician. Just an engineer with interest for cryptography
  29. - Short talk (15 min), with the objective to make a practical introduction to the RSA cryptosystem
  30. - Is not a talk about mathematical demostrations, is a talk with the objective to get the basic notions to be able to do a practical implementation of the algorithm
  31. - After the talk, we will do a practical workshop to implement the concepts. We can offer support for Go, Rust, Python and Nodejs (you can choose any other language, but we will not be able to help)
  32. ---
  33. # Public key cryptography
  34. ![pubkencr](https://upload.wikimedia.org/wikipedia/commons/f/f9/Public_key_encryption.svg "pubkencr")
  35. ---
  36. Some examples:
  37. - RSA
  38. - Paillier
  39. - ECC (Corba el·líptica)
  40. ---
  41. # Basics of modular arithmetic
  42. - Modulus, `mod`, `%`
  43. - Remainder after division of two numbers
  44. ![clocks](https://upload.wikimedia.org/wikipedia/commons/thumb/a/a4/Clock_group.svg/220px-Clock_group.svg.png "clocks")
  45. ```
  46. 5 mod 12 = 5
  47. 14 mod 12 = 2
  48. 83 mod 10 = 3
  49. ```
  50. ```
  51. 5 + 3 mod 6 = 8 mod 6 = 2
  52. ```
  53. ---
  54. # Brief history of RSA
  55. - RSA (Rivest–Shamir–Adleman): Ron Rivest, Adi Shamir, Leonard Adleman
  56. - year 1977
  57. - one of the first public key cryptosystems
  58. - based on the difficulty of factorization of the product of two big prime numbers
  59. ---
  60. # Prime numbers
  61. - We need an asymmetric key, in a way where we can decrypt a message encrypted with the asymetric key
  62. - Without allowing to find the private key from the public key
  63. - in RSA we resolve this with factorization of prime numbers
  64. - using prime numbers for $p$ and $q$, it's difficult factorize $n$ to obtain $p$ and $q$, where $n=p*q$
  65. ---
  66. Example:
  67. If we know $n$ which we need to find the $p$ and $q$ values where $p*q=n$:
  68. ```
  69. n = 35
  70. ```
  71. To obtain the possible factors, is needed to brute force trying different combinations, until we find:
  72. ```
  73. p = 5
  74. q = 7
  75. ```
  76. In this case is easy as it's a simple example with small numbers. The idea is to do this with big prime numbers
  77. ---
  78. Another exmample with more bigger prime numbers:
  79. ```
  80. n = 272604817800326282194810623604278579733
  81. ```
  82. From $n$, I don't have a 'direct' way to obtain $p$ and $q$. I need to try by brute force the different values until finding a correct combination.
  83. ```
  84. p = 17975460804519255043
  85. q = 15165386899666573831
  86. n = 17975460804519255043 * 15165386899666573831 = 272604817800326282194810623604278579733
  87. ```
  88. ---
  89. If we do this with non prime numbers:
  90. ```
  91. n = 32
  92. We can factorize 32 = 2 * 2 * 2 * 2 * 2
  93. combining that values in two values X * Y
  94. for example (2*2*2) * (2*2) = 8*4 = 32
  95. we can also take 2 * (2*2*2*2) = 2 * 16 = 32
  96. ...
  97. ```
  98. ---
  99. One example with bigger non prime numbers:
  100. ```
  101. n = 272604817800326282227951471308464408608
  102. We can take:
  103. p = 17975460804519255044
  104. q = 15165386899666573832
  105. Or also:
  106. p = 2
  107. q = 136302408900163141113975735654232204304
  108. ...
  109. ```
  110. In the real world:
  111. - https://en.wikipedia.org/wiki/RSA_numbers
  112. - https://en.wikipedia.org/wiki/RSA_Factoring_Challenge#The_prizes_and_records
  113. So, we are basing this in the fact that is not easy to factorize big numbers composed by big primes.
  114. ---
  115. # Keys generation
  116. - PubK: $e$, $n$
  117. - PrivK: $d$, $n$
  118. - are choosen randomly 2 big prime numbers $p$ and $q$, that will be secrets
  119. - $n = p * q$
  120. - $λ$ is the Carmichael function
  121. - $λ(n) = (p − 1) * (q − 1)$
  122. - Choose a prime number $e$ that satisfies $1 < e < λ(n)$ and $gcd(e, λ(n))=1$
  123. - Usually in examples is used $e = 2^16 + 1 = 65537$
  124. - $d$ such as $e * d = 1 mod λ(n)$
  125. - $d = e^(-1) mod λ(n) = e modinv λ(n)$
  126. ---
  127. ### Example
  128. - `p = 3`
  129. - `q = 11`
  130. - `e = 7` value choosen between 1 and λ(n)=20, where λ(n) is not divisible by this value
  131. - `n = 3 * 11 = 33`
  132. - `λ(n) = (3-1) * (11-1) = 2 * 10 = 20`
  133. - `d` such as `7 * d = 1 mod 20`
  134. - `d = 3`
  135. - PubK: `e=7, n=33`
  136. - PrivK: `d=3, n=33`
  137. ---
  138. ### Naive code
  139. ```python
  140. def egcd(a, b):
  141. if a == 0:
  142. return (b, 0, 1)
  143. g, y, x = egcd(b%a,a)
  144. return (g, x - (b//a) * y, y)
  145. def modinv(a, m):
  146. g, x, y = egcd(a, m)
  147. if g != 1:
  148. raise Exception('No modular inverse')
  149. return x%m
  150. ```
  151. ---
  152. ```
  153. def newKeys():
  154. p = number.getPrime(n_length)
  155. q = number.getPrime(n_length)
  156. # pubK e, n
  157. e = 65537
  158. n = p*q
  159. pubK = PubK(e, n)
  160. # privK d, n
  161. phi = (p-1) * (q-1)
  162. d = modinv(e, phi)
  163. privK = PrivK(d, n)
  164. return({'pubK': pubK, 'privK': privK})
  165. ```
  166. ---
  167. # Encryption
  168. - Brenna wants to send the message `m` to Alice, so, will use the Public Key from Alice to encrypt `m`
  169. - `m` powered at `e` of the public key from Alice
  170. - evaluate at modulus of `n`
  171. ### Example
  172. - message to encrypt `m = 5`
  173. - receiver public key: `e=7, n=33`
  174. - `c = 5 ^ 7 mod 33 = 78125 mod 33 = 14`
  175. ### Naive code
  176. ```python
  177. def encrypt(pubK, m):
  178. c = (m ** pubK.e) % pubK.n
  179. return c
  180. ```
  181. ---
  182. # Decrypt
  183. - from an encrypted value `c`
  184. - `c` powered at `d` of the private key of the person to who the message was encrypted
  185. - evaluate at modulus of `n`
  186. ### Example
  187. - receiver private key, PrivK: `d=3, n=33`
  188. - `m = 14 ^ 3 mod 33 = 2744 mod 33 = 5`
  189. ### Naive code
  190. ```python
  191. def decrypt(privK, c):
  192. m_d = (c ** privK.d) % privK.n
  193. return m_d
  194. ```
  195. ---
  196. # What's going on when encrypting and decrypting?
  197. ![encrdecr](https://cdn-images-1.medium.com/max/1600/1*4AXQNOrddQJud0fZJ3FNgg.png "encrdecr")
  198. ---
  199. ```
  200. n = pq
  201. e
  202. phi = (p-1)(q-1)
  203. d = e^-1 mod (phi) = e^-1 mod (p-1)(q-1)
  204. # encrypt
  205. c = m^e mod n = m^e mod pq
  206. # decrypt
  207. m' = c^d mod n = c ^(e^-1 mod (p-1)(q-1)) mod pq =
  208. = (m^e)^(e^-1 mod (p-1)(q-1)) mod pq =
  209. = m^(e * e^-1 mod (p-1)(q-1)) mod pq =
  210. = m^(1 mod (p-1)(q-1)) mod pq =
  211. [theorem in which we're not going into details]
  212. a ^ (1 mod λ(N)) mod N = a mod N
  213. [/theorem]
  214. = m mod pq
  215. ```
  216. ---
  217. # Signature
  218. - encryption operation but using PrivK instead of PubK, and PubK instead of PrivK
  219. - having a message `m`
  220. - power of `m` at `d` of the private key from the signer person
  221. - evaluated at modulus `n`
  222. ---
  223. ### Example
  224. - private key of the person emitter of the signature: `d = 3, n = 33`
  225. - message to be signed: `m=5`
  226. - signature: `s = 5 ** 3 % 33 = 26`
  227. ### Naive code
  228. ```python
  229. def sign(privK, m):
  230. s = (m ** privK.d) % privK.n
  231. return s
  232. ```
  233. ---
  234. # Verification of the signature
  235. - having message `m` and the signature `s`
  236. - elevate `m` at `e` of the public key from the signer
  237. - evaluate at modulus of `n`
  238. ---
  239. ### Example
  240. - public key from the singer person `e=7, n=33`
  241. - message `m=5`
  242. - signature `s=26`
  243. - verification `v = 26**7 % 33 = 5`
  244. - check that we have recovered the message (that `m` is equivalent to `v`) `m = 5 = v = 5`
  245. ### Naive code
  246. ```python
  247. def verifySign(pubK, s, m):
  248. v = (s ** pubK.e) % privK.n
  249. return v==m
  250. ```
  251. ---
  252. # Homomorphic Multiplication
  253. - from two values $a$ and $b$
  254. - encrypted are $a_{encr}$ and $b_{encr}$
  255. - we can compute the multiplication of the two encrypted values, obtaining the result encrypted
  256. - the encrypted result from the multiplication is calculated doing: $c_{encr} = a_{encr} * b_{encr} mod n$
  257. - we can decrypt $c_{encr}$ and we will obtain $c$, equivalent to $a * b$
  258. - Why:
  259. ```
  260. ((a^e mod n) * (b^e mod n)) mod n =
  261. = (a^e * b^e mod n) mod n = (a*b)^e mod n
  262. ```
  263. ---
  264. ### Example
  265. - PubK: `e=7, n=33`
  266. - PrivK: `d=3, n=33`
  267. - `a = 5`
  268. - `b = 8`
  269. - `a_encr = 5^7 mod 33 = 78125 mod 33 = 14`
  270. - `b_encr = 8^7 mod 33 = 2097152 mod 33 = 2`
  271. - `c_encr = (14 * 2) mod 33 = 28 mod 33 = 28`
  272. - `c = 28 ^ 3 mod 33 = 21952 mod 33 = 7`
  273. - `c = 7 = a * b % n = 5 * 8 % 33 = 7`, on `5*8 mod 33 = 7`
  274. - take a `n` enough big, if not the result will be cropped by the modulus
  275. ---
  276. ### Naive code
  277. ```python
  278. def homomorphic_mul(pubK, a, b):
  279. c = (a*b) % pubK.n
  280. return c
  281. ```
  282. ---
  283. # Small demo
  284. [...]
  285. # And now... practical implementation
  286. - full night long
  287. - big ints are your friends