|
| 1 | +{-# LANGUAGE DerivingStrategies #-} |
| 2 | +{-# LANGUAGE DerivingVia #-} |
| 3 | +{-# LANGUAGE ForeignFunctionInterface #-} |
| 4 | +{-# LANGUAGE GeneralizedNewtypeDeriving #-} |
| 5 | + |
| 6 | +module Simplex.Messaging.Crypto.BBS |
| 7 | + ( BBSSecretKey (..), |
| 8 | + BBSPublicKey (..), |
| 9 | + BBSSignature (..), |
| 10 | + BBSProof (..), |
| 11 | + BBSHeader (..), |
| 12 | + BBSPresHeader (..), |
| 13 | + bbsKeyGen, |
| 14 | + bbsSign, |
| 15 | + bbsProofGen, |
| 16 | + bbsProofVerify, |
| 17 | + ) where |
| 18 | + |
| 19 | +import Data.Aeson (FromJSON (..), ToJSON (..)) |
| 20 | +import Data.ByteString (ByteString) |
| 21 | +import qualified Data.ByteString as B |
| 22 | +import qualified Data.ByteString.Internal as BI |
| 23 | +import Foreign |
| 24 | +import Foreign.C |
| 25 | +import Simplex.Messaging.Encoding.String |
| 26 | + |
| 27 | +newtype BBSSecretKey = BBSSecretKey ByteString |
| 28 | + deriving newtype (Eq, Show, StrEncoding) |
| 29 | + |
| 30 | +instance ToJSON BBSSecretKey where |
| 31 | + toJSON = strToJSON |
| 32 | + toEncoding = strToJEncoding |
| 33 | + |
| 34 | +instance FromJSON BBSSecretKey where |
| 35 | + parseJSON = strParseJSON "BBSSecretKey" |
| 36 | + |
| 37 | +newtype BBSPublicKey = BBSPublicKey ByteString |
| 38 | + deriving newtype (Eq, Show, StrEncoding) |
| 39 | + deriving (ToJSON, FromJSON) via BBSSecretKey |
| 40 | + |
| 41 | +newtype BBSSignature = BBSSignature ByteString |
| 42 | + deriving newtype (Eq, Show, StrEncoding) |
| 43 | + deriving (ToJSON, FromJSON) via BBSSecretKey |
| 44 | + |
| 45 | +newtype BBSProof = BBSProof ByteString |
| 46 | + deriving newtype (Eq, Show, StrEncoding) |
| 47 | + deriving (ToJSON, FromJSON) via BBSSecretKey |
| 48 | + |
| 49 | +newtype BBSHeader = BBSHeader ByteString |
| 50 | + deriving newtype (Eq, Show, StrEncoding) |
| 51 | + deriving (ToJSON, FromJSON) via BBSSecretKey |
| 52 | + |
| 53 | +newtype BBSPresHeader = BBSPresHeader ByteString |
| 54 | + deriving newtype (Eq, Show, StrEncoding) |
| 55 | + deriving (ToJSON, FromJSON) via BBSSecretKey |
| 56 | + |
| 57 | +-- Constants |
| 58 | + |
| 59 | +bbsSkLen, bbsPkLen, bbsSigLen, bbsProofBaseLen, bbsProofUdElemLen :: Int |
| 60 | +bbsSkLen = 32 |
| 61 | +bbsPkLen = 96 |
| 62 | +bbsSigLen = 80 |
| 63 | +bbsProofBaseLen = 272 |
| 64 | +bbsProofUdElemLen = 32 |
| 65 | + |
| 66 | +bbsProofLen :: Int -> Int |
| 67 | +bbsProofLen numUndisclosed = bbsProofBaseLen + numUndisclosed * bbsProofUdElemLen |
| 68 | + |
| 69 | +-- FFI |
| 70 | + |
| 71 | +data BBS_Ciphersuite |
| 72 | + |
| 73 | +foreign import ccall "bbs_keygen_full" |
| 74 | + c_bbs_keygen_full :: Ptr BBS_Ciphersuite -> Ptr Word8 -> Ptr Word8 -> IO CInt |
| 75 | + |
| 76 | +foreign import ccall "bbs_sign" |
| 77 | + c_bbs_sign :: |
| 78 | + Ptr BBS_Ciphersuite -> |
| 79 | + Ptr Word8 -> Ptr Word8 -> Ptr Word8 -> |
| 80 | + Ptr Word8 -> CSize -> |
| 81 | + CSize -> Ptr (Ptr Word8) -> Ptr CSize -> |
| 82 | + IO CInt |
| 83 | + |
| 84 | +foreign import ccall "bbs_proof_gen" |
| 85 | + c_bbs_proof_gen :: |
| 86 | + Ptr BBS_Ciphersuite -> |
| 87 | + Ptr Word8 -> Ptr Word8 -> Ptr Word8 -> |
| 88 | + Ptr Word8 -> CSize -> |
| 89 | + Ptr Word8 -> CSize -> |
| 90 | + Ptr CSize -> CSize -> |
| 91 | + CSize -> Ptr (Ptr Word8) -> Ptr CSize -> |
| 92 | + IO CInt |
| 93 | + |
| 94 | +foreign import ccall "bbs_proof_verify" |
| 95 | + c_bbs_proof_verify :: |
| 96 | + Ptr BBS_Ciphersuite -> |
| 97 | + Ptr Word8 -> |
| 98 | + Ptr Word8 -> CSize -> |
| 99 | + Ptr Word8 -> CSize -> |
| 100 | + Ptr Word8 -> CSize -> |
| 101 | + Ptr CSize -> CSize -> |
| 102 | + CSize -> Ptr (Ptr Word8) -> Ptr CSize -> |
| 103 | + IO CInt |
| 104 | + |
| 105 | +foreign import ccall "&bbs_sha256_ciphersuite" |
| 106 | + c_bbs_sha256_ciphersuite :: Ptr (Ptr BBS_Ciphersuite) |
| 107 | + |
| 108 | +getCiphersuite :: IO (Ptr BBS_Ciphersuite) |
| 109 | +getCiphersuite = peek c_bbs_sha256_ciphersuite |
| 110 | + |
| 111 | +-- Helpers |
| 112 | + |
| 113 | +withBS :: ByteString -> (Ptr Word8 -> CSize -> IO a) -> IO a |
| 114 | +withBS bs f = let (fptr, off, len) = BI.toForeignPtr bs |
| 115 | + in withForeignPtr fptr $ \ptr -> f (ptr `plusPtr` off) (fromIntegral len) |
| 116 | + |
| 117 | +withMessages :: [ByteString] -> (Ptr (Ptr Word8) -> Ptr CSize -> CSize -> IO a) -> IO a |
| 118 | +withMessages msgs f = do |
| 119 | + let n = length msgs |
| 120 | + allocaArray n $ \msgsPtr -> |
| 121 | + allocaArray n $ \lensPtr -> do |
| 122 | + pokeMessages msgs msgsPtr lensPtr 0 |
| 123 | + f msgsPtr lensPtr (fromIntegral n) |
| 124 | + where |
| 125 | + pokeMessages [] _ _ _ = pure () |
| 126 | + pokeMessages (m : ms) msgsPtr lensPtr i = do |
| 127 | + let (fptr, off, len) = BI.toForeignPtr m |
| 128 | + withForeignPtr fptr $ \ptr -> do |
| 129 | + pokeElemOff msgsPtr i (ptr `plusPtr` off) |
| 130 | + pokeElemOff lensPtr i (fromIntegral len) |
| 131 | + pokeMessages ms msgsPtr lensPtr (i + 1) |
| 132 | + |
| 133 | +withIndexes :: [Int] -> (Ptr CSize -> CSize -> IO a) -> IO a |
| 134 | +withIndexes idxs f = do |
| 135 | + let n = length idxs |
| 136 | + allocaArray n $ \ptr -> do |
| 137 | + pokeArray ptr (map fromIntegral idxs) |
| 138 | + f ptr (fromIntegral n) |
| 139 | + |
| 140 | +-- Public API |
| 141 | + |
| 142 | +bbsKeyGen :: IO (BBSSecretKey, BBSPublicKey) |
| 143 | +bbsKeyGen = do |
| 144 | + cs <- getCiphersuite |
| 145 | + sk <- BI.create bbsSkLen $ \_ -> pure () |
| 146 | + pk <- BI.create bbsPkLen $ \_ -> pure () |
| 147 | + withBS sk $ \skPtr _ -> |
| 148 | + withBS pk $ \pkPtr _ -> do |
| 149 | + rc <- c_bbs_keygen_full cs skPtr pkPtr |
| 150 | + if rc == 0 |
| 151 | + then pure (BBSSecretKey sk, BBSPublicKey pk) |
| 152 | + else error "bbsKeyGen failed" |
| 153 | + |
| 154 | +bbsSign :: |
| 155 | + BBSSecretKey -> |
| 156 | + BBSPublicKey -> |
| 157 | + BBSHeader -> |
| 158 | + [ByteString] -> |
| 159 | + IO (Either String BBSSignature) |
| 160 | +bbsSign (BBSSecretKey sk) (BBSPublicKey pk) (BBSHeader header) msgs = do |
| 161 | + cs <- getCiphersuite |
| 162 | + sig <- BI.create bbsSigLen $ \_ -> pure () |
| 163 | + withBS sk $ \skPtr _ -> |
| 164 | + withBS pk $ \pkPtr _ -> |
| 165 | + withBS header $ \hdrPtr hdrLen -> |
| 166 | + withBS sig $ \sigPtr _ -> |
| 167 | + withMessages msgs $ \msgsPtr lensPtr n -> do |
| 168 | + rc <- c_bbs_sign cs skPtr pkPtr sigPtr hdrPtr hdrLen n msgsPtr lensPtr |
| 169 | + pure $ if rc == 0 |
| 170 | + then Right (BBSSignature sig) |
| 171 | + else Left "bbsSign failed" |
| 172 | + |
| 173 | +bbsProofGen :: |
| 174 | + BBSPublicKey -> |
| 175 | + BBSSignature -> |
| 176 | + BBSHeader -> |
| 177 | + BBSPresHeader -> |
| 178 | + [Int] -> |
| 179 | + [ByteString] -> |
| 180 | + IO (Either String BBSProof) |
| 181 | +bbsProofGen (BBSPublicKey pk) (BBSSignature sig) (BBSHeader header) (BBSPresHeader ph) disclosedIdxs msgs = do |
| 182 | + cs <- getCiphersuite |
| 183 | + let numUndisclosed = length msgs - length disclosedIdxs |
| 184 | + proofSz = bbsProofLen numUndisclosed |
| 185 | + proof <- BI.create proofSz $ \_ -> pure () |
| 186 | + withBS pk $ \pkPtr _ -> |
| 187 | + withBS sig $ \sigPtr _ -> |
| 188 | + withBS proof $ \proofPtr _ -> |
| 189 | + withBS header $ \hdrPtr hdrLen -> |
| 190 | + withBS ph $ \phPtr phLen -> |
| 191 | + withIndexes disclosedIdxs $ \idxsPtr idxsLen -> |
| 192 | + withMessages msgs $ \msgsPtr lensPtr n -> do |
| 193 | + rc <- c_bbs_proof_gen cs pkPtr sigPtr proofPtr hdrPtr hdrLen phPtr phLen idxsPtr idxsLen n msgsPtr lensPtr |
| 194 | + pure $ if rc == 0 |
| 195 | + then Right (BBSProof proof) |
| 196 | + else Left "bbsProofGen failed" |
| 197 | + |
| 198 | +bbsProofVerify :: |
| 199 | + BBSPublicKey -> |
| 200 | + BBSProof -> |
| 201 | + BBSHeader -> |
| 202 | + BBSPresHeader -> |
| 203 | + [Int] -> |
| 204 | + Int -> |
| 205 | + [ByteString] -> |
| 206 | + IO Bool |
| 207 | +bbsProofVerify (BBSPublicKey pk) (BBSProof proof) (BBSHeader header) (BBSPresHeader ph) disclosedIdxs numMessages disclosedMsgs = do |
| 208 | + cs <- getCiphersuite |
| 209 | + withBS pk $ \pkPtr _ -> |
| 210 | + withBS proof $ \proofPtr proofLen -> |
| 211 | + withBS header $ \hdrPtr hdrLen -> |
| 212 | + withBS ph $ \phPtr phLen -> |
| 213 | + withIndexes disclosedIdxs $ \idxsPtr idxsLen -> |
| 214 | + withMessages disclosedMsgs $ \msgsPtr lensPtr _ -> do |
| 215 | + rc <- c_bbs_proof_verify cs pkPtr proofPtr proofLen hdrPtr hdrLen phPtr phLen idxsPtr idxsLen (fromIntegral numMessages) msgsPtr lensPtr |
| 216 | + pure (rc == 0) |
| 217 | + |
0 commit comments