Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Random

 Random

iPlayground 2020
Swift 4.2後開始支援Random相關Syntax
但實際上底層會因不同的作業系統,去撈取不同的API。
順便帶大家認識從iOS初期一直到現在都在用的Arc4random的由來,和簡單介紹RC4的演算法。

danny80916kimo

November 09, 2020
Tweet

Other Decks in Programming

Transcript

  1. 23 56 3 76 13 74 25 68 11 28

    31 90 47 62 23 56 3 76 13 74 25 68 11 28 31 90 47 62
  2. 亂數(Random number) 由 Seed 搭配演算法產出亂數(具有確定性): • 偽亂數⽣產器 Pseudorandom number generator,簡稱

    PRNG • ⼜稱為定性隨機比特⽣成器Deterministic random bit generators (DRBGs) • 密碼學安全偽亂數⽣成器 Cryptographically secure pseudorandom number generator,簡稱 CSPRNG 由物理世界的現象產出亂數(不具有確定性): • 真亂數⽣成器 True random number generator,簡稱 TRNG • ⼜稱為非定性隨機比特⽣成器 • non-deterministic random bit generators (NRBGs)
  3. ( 5 + 3 ) x 8 % 1 1

    = 9 ( 9 + 3 ) x 8 % 1 1 = 8 ( 8 + 3 ) x 8 % 1 1 = 0 ( 0 + 3 ) x 8 % 1 1 = 2 ( 2 + 3 ) x 8 % 1 1 = 7 ( 7 + 3 ) x 8 % 1 1 = 3
  4. static unsigned long int next = 1; int myrand(void) /*

    RAND_MAX assumed to be 32767 */ { next = next * 1103515245 + 12345; return((unsigned int)(next/65536) % 32768); } void mysrand(unsigned int seed) { next = seed; }
  5. Blum Blum Shub (B.B.S.) Algorithm p ≡ q ≡ 3(

    mod 4) X0 = s2 mod n Xi = (Xi−1 )2 mod n Bi = Xi mod 2
  6. These functions first appeared in OpenBSD 2.1. The original version

    of this random number generator used the RC4 (also known as ARC4) algorithm. In OpenBSD 5.5 it was replaced with the ChaCha20 cipher, and it may be replaced again in the future as cryptographic techniques advance. A good mnemonic is “A Replacement Call for Random”.
  7. "The next generation awesome random subsystem must be super, super

    secure, before we change 1 line of code to rely on it"
  8. RC4

  9. 柯克霍夫原則 (Kerckhoffs’s principle) 即使演算法完全洩漏,只要⾦鑰沒有洩漏,密⽂就是安全的 • Claude Shannon: "the enemy knows

    the system" • Bruce Schneier: 任何以隱藏設計作為防護(Security through obscurity)的保安 系統必然會失敗 • Kerckhoffs's principle 不是說密碼學演算法都必須公開,⽽是要確 保即使公開也 不會傷害安全性
  10. S box KSA 1 2 3 4 5 6 7

    0 3 1 4 1 5 3 1 4 Key S-box i J 0 0 3 1 5 2 3 3 6 4 7 5 3 6 6 7 6 j=(j+S[i]+K[i]) % 8 S0 S1 S2 S3 S4 S5 S6 S7 j=(0+0+3)=3
  11. S box KSA 1 2 3 4 5 6 7

    0 3 1 4 1 5 3 1 4 Key S-box i J 0 0 3 1 5 2 3 3 6 4 7 5 3 6 6 7 6 S0 S1 S2 S3 S4 S5 S6 S7 j=(j+S[i]+K[i]) % 8 j=(3+1+1)%8=5
  12. S box KSA 1 2 3 4 5 6 7

    0 3 1 4 1 5 3 1 4 Key S-box i J 0 0 3 1 5 2 3 3 6 4 7 5 3 6 6 7 6 S0 S1 S2 S3 S4 S5 S6 S7 j=(j+S[i]+K[i]) % 8 j=(5+4+2)%8=3
  13. S box KSA 1 2 3 4 5 6 7

    0 3 1 4 1 5 3 1 4 Key S-box i J 0 0 3 1 5 2 3 3 6 4 7 5 3 6 6 7 6 S0 S1 S2 S3 S4 S5 S6 S7 j=(j+S[i]+K[i]) % 8 j=(3+1+2)%8=6
  14. S box KSA 1 2 3 4 5 6 7

    0 3 1 4 1 5 3 1 4 Key S-box i J 0 0 3 1 5 2 3 3 6 4 7 5 3 6 6 7 6 S0 S1 S2 S3 S4 S5 S6 S7 j=(j+S[i]+K[i]) % 8 j=(3+1+2)%8=6
  15. S box KSA 1 2 3 4 5 6 7

    0 3 1 4 1 5 3 1 4 Key S-box i J 0 0 3 1 5 2 3 3 6 4 7 5 3 6 6 7 6 S0 S1 S2 S3 S4 S5 S6 S7 j=(j+S[i]+K[i]) % 8 j=(7+1+3)%8=3
  16. S box KSA 1 2 3 4 5 6 7

    0 3 1 4 1 5 3 1 4 Key S-box i J 0 0 3 1 5 2 3 3 6 4 7 5 3 6 6 7 6 S0 S1 S2 S3 S4 S5 S6 S7 j=(j+S[i]+K[i]) % 8 j=(3+1+2)%8=6
  17. S box KSA 1 2 3 4 5 6 7

    0 3 1 4 1 5 3 1 4 Key S-box i J 0 0 3 1 5 2 3 3 6 4 7 5 3 6 6 7 6 S0 S1 S2 S3 S4 S5 S6 S7 j=(j+S[i]+K[i]) % 8 j=(3+1+2)%8=6
  18. S box KSA 1 2 3 4 5 6 7

    0 3 1 4 1 5 3 1 4 Key S-box i J 0 0 3 1 5 2 3 3 6 4 7 5 3 6 6 7 6 S0 S1 S2 S3 S4 S5 S6 S7
  19. PRGA 1 2 3 4 5 6 7 0 1

    2 3 4 5 6 7 0 i j t KStream 0 1 5 3 1 2 5 5 0 3 6 5 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 j=(j+S[I])% 8 t=(S[j]+S[I] )% 8 KS=S[t]
  20. /// Platform Implementation of `SystemRandomNumberGenerator` /// ======================================================== /// /// While

    the system generator is automatically seeded and thread-safe on every /// platform, the cryptographic quality of the stream of random data produced by /// the generator may vary. For more detail, see the documentation for the APIs /// used by each platform. /// /// - Apple platforms use `arc4random_buf(3)`. /// - Linux platforms use `getrandom(2)` when available; otherwise, they read /// from `/dev/urandom`. /// - Windows uses `BCryptGenRandom`.