From 82f8b6a7b169955f1f8ef4eff45c646cc5129d7f Mon Sep 17 00:00:00 2001
From: Jean-Paul Calderone <exarkun@twistedmatrix.com>
Date: Fri, 12 May 2023 11:48:56 -0400
Subject: [PATCH] Convert the `make-keypairs` program to cryptonite

---
 make-keypairs/Main.hs | 19 ++++++++++---------
 tahoe-ssk.cabal       |  1 +
 2 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/make-keypairs/Main.hs b/make-keypairs/Main.hs
index c537ae4..d77171e 100644
--- a/make-keypairs/Main.hs
+++ b/make-keypairs/Main.hs
@@ -1,11 +1,11 @@
 module Main where
 
-import Codec.Crypto.RSA (generateKeyPair)
-import Crypto.Random (CryptoRandomGen (newGenIO), SystemRandom)
+import qualified Crypto.PubKey.RSA as RSA
 import Data.ASN1.BinaryEncoding (DER (DER))
 import Data.ASN1.Encoding (ASN1Encoding (encodeASN1))
 import Data.ASN1.Types (ASN1Object (toASN1))
 import qualified Data.ByteString.Lazy as LB
+import Data.X509 (PrivKey (PrivKeyRSA))
 
 -- | The size of the keys to generate.
 bits :: Int
@@ -17,11 +17,12 @@ count = 5
 
 main :: IO ()
 main = do
-    g <- newGenIO :: IO SystemRandom
-    mapM_ (genKey g) [0 .. count - 1]
+    mapM_ genKey [0 .. count - 1]
 
-genKey :: (Show a, CryptoRandomGen c) => c -> a -> IO ()
-genKey g n =
-    let (_, priv, _) = generateKeyPair g bits
-        bytes = encodeASN1 DER (toASN1 priv [])
-     in LB.writeFile ("test/data/rsa-privkey-" <> show n <> ".der") bytes
+genKey :: Show a => a -> IO ()
+genKey n = do
+    (_, priv) <- RSA.generate bits e
+    let bytes = encodeASN1 DER (toASN1 (PrivKeyRSA priv) [])
+    LB.writeFile ("test/data/rsa-privkey-" <> show n <> ".der") bytes
+  where
+    e = 0x10001
diff --git a/tahoe-ssk.cabal b/tahoe-ssk.cabal
index 470d125..7a873be 100644
--- a/tahoe-ssk.cabal
+++ b/tahoe-ssk.cabal
@@ -143,3 +143,4 @@ executable make-keypairs
     , base
     , bytestring
     , cryptonite
+    , x509
-- 
GitLab