|
| 1 | +{-# LANGUAGE DeriveGeneric #-} |
| 2 | +{-# LANGUAGE DuplicateRecordFields #-} |
| 3 | +{-# LANGUAGE GeneralizedNewtypeDeriving #-} |
| 4 | +{-# LANGUAGE ImportQualifiedPost #-} |
| 5 | +{-# LANGUAGE OverloadedRecordDot #-} |
| 6 | +{-# LANGUAGE NoFieldSelectors #-} |
| 7 | + |
| 8 | +-- | |
| 9 | +-- Module : Crypto.Secp256k1.Internal.Recovery |
| 10 | +-- License : UNLICENSE |
| 11 | +-- Maintainer : Jean-Pierre Rupp <[email protected]> |
| 12 | +-- Stability : experimental |
| 13 | +-- Portability : POSIX |
| 14 | +-- |
| 15 | +-- Crytpographic functions related to recoverable signatures from Bitcoin’s secp256k1 library. |
| 16 | +-- |
| 17 | +-- The API for this module may change at any time. This is an internal module only |
| 18 | +-- exposed for hacking and experimentation. |
| 19 | +module Crypto.Secp256k1.Internal.Recovery where |
| 20 | + |
| 21 | +import Control.DeepSeq (NFData) |
| 22 | +import Control.Monad (unless, (<=<)) |
| 23 | +import Crypto.Secp256k1.Internal.Base (Msg (..), PubKey (..), SecKey (..), Sig (..)) |
| 24 | +import Crypto.Secp256k1.Internal.Context (Ctx (..)) |
| 25 | +import Crypto.Secp256k1.Internal.ForeignTypes |
| 26 | + ( isSuccess, |
| 27 | + ) |
| 28 | +import Crypto.Secp256k1.Internal.RecoveryOps |
| 29 | + ( ecdsaRecover, |
| 30 | + ecdsaRecoverableSignatureConvert, |
| 31 | + ecdsaRecoverableSignatureParseCompact, |
| 32 | + ecdsaRecoverableSignatureSerializeCompact, |
| 33 | + ecdsaSignRecoverable, |
| 34 | + ) |
| 35 | +import Crypto.Secp256k1.Internal.Util |
| 36 | + ( decodeHex, |
| 37 | + showsHex, |
| 38 | + unsafePackByteString, |
| 39 | + unsafeUseByteString, |
| 40 | + ) |
| 41 | +import Data.ByteString (ByteString) |
| 42 | +import Data.ByteString qualified as BS |
| 43 | +import Data.Hashable (Hashable (..)) |
| 44 | +import Data.Maybe (fromMaybe) |
| 45 | +import Data.String (IsString (..)) |
| 46 | +import Data.Word (Word8) |
| 47 | +import Foreign |
| 48 | + ( alloca, |
| 49 | + free, |
| 50 | + mallocBytes, |
| 51 | + nullFunPtr, |
| 52 | + nullPtr, |
| 53 | + peek, |
| 54 | + ) |
| 55 | +import GHC.Generics (Generic) |
| 56 | +import System.IO.Unsafe (unsafePerformIO) |
| 57 | +import Text.Read |
| 58 | + ( Lexeme (String), |
| 59 | + lexP, |
| 60 | + parens, |
| 61 | + pfail, |
| 62 | + readPrec, |
| 63 | + ) |
| 64 | + |
| 65 | +newtype RecSig = RecSig {get :: ByteString} |
| 66 | + deriving (Eq, Generic, NFData) |
| 67 | + |
| 68 | +data CompactRecSig = CompactRecSig |
| 69 | + { rs :: !ByteString, |
| 70 | + v :: {-# UNPACK #-} !Word8 |
| 71 | + } |
| 72 | + deriving (Eq, Generic) |
| 73 | + |
| 74 | +instance NFData CompactRecSig |
| 75 | + |
| 76 | +compactRecSig :: ByteString -> Maybe CompactRecSig |
| 77 | +compactRecSig bs |
| 78 | + | BS.length bs == 65, |
| 79 | + BS.last bs <= 3 = |
| 80 | + Just (CompactRecSig (BS.take 64 bs) (BS.last bs)) |
| 81 | + | otherwise = Nothing |
| 82 | + |
| 83 | +serializeCompactRecSig :: CompactRecSig -> ByteString |
| 84 | +serializeCompactRecSig (CompactRecSig bs v) = |
| 85 | + BS.snoc bs v |
| 86 | + |
| 87 | +compactRecSigFromString :: String -> Maybe CompactRecSig |
| 88 | +compactRecSigFromString = compactRecSig <=< decodeHex |
| 89 | + |
| 90 | +instance Read CompactRecSig where |
| 91 | + readPrec = parens $ do |
| 92 | + String str <- lexP |
| 93 | + maybe pfail return $ compactRecSigFromString str |
| 94 | + |
| 95 | +instance IsString CompactRecSig where |
| 96 | + fromString = fromMaybe e . compactRecSigFromString |
| 97 | + where |
| 98 | + e = error "Could not decode signature from hex string" |
| 99 | + |
| 100 | +instance Show CompactRecSig where |
| 101 | + showsPrec _ = showsHex . serializeCompactRecSig |
| 102 | + |
| 103 | +-- | Parse a compact ECDSA signature (64 bytes + recovery id). |
| 104 | +importCompactRecSig :: Ctx -> CompactRecSig -> Maybe RecSig |
| 105 | +importCompactRecSig (Ctx ctx) (CompactRecSig sig_rs sig_v) |
| 106 | + | BS.length sig_rs == 64, |
| 107 | + sig_v <= 3 = unsafePerformIO $ |
| 108 | + unsafeUseByteString sig_rs $ \(sig_rs_ptr, _) -> do |
| 109 | + out_rec_sig_ptr <- mallocBytes 65 |
| 110 | + ret <- |
| 111 | + ecdsaRecoverableSignatureParseCompact |
| 112 | + ctx |
| 113 | + out_rec_sig_ptr |
| 114 | + sig_rs_ptr |
| 115 | + (fromIntegral sig_v) |
| 116 | + if isSuccess ret |
| 117 | + then do |
| 118 | + out_bs <- unsafePackByteString (out_rec_sig_ptr, 65) |
| 119 | + return (Just (RecSig out_bs)) |
| 120 | + else do |
| 121 | + free out_rec_sig_ptr |
| 122 | + return Nothing |
| 123 | + | otherwise = Nothing |
| 124 | + |
| 125 | +-- | Serialize an ECDSA signature in compact format (64 bytes + recovery id). |
| 126 | +exportCompactRecSig :: Ctx -> RecSig -> CompactRecSig |
| 127 | +exportCompactRecSig (Ctx ctx) (RecSig rec_sig_bs) = unsafePerformIO $ |
| 128 | + unsafeUseByteString rec_sig_bs $ \(rec_sig_ptr, _) -> |
| 129 | + alloca $ \out_v_ptr -> do |
| 130 | + out_sig_ptr <- mallocBytes 64 |
| 131 | + ret <- |
| 132 | + ecdsaRecoverableSignatureSerializeCompact |
| 133 | + ctx |
| 134 | + out_sig_ptr |
| 135 | + out_v_ptr |
| 136 | + rec_sig_ptr |
| 137 | + unless (isSuccess ret) $ do |
| 138 | + free out_sig_ptr |
| 139 | + error "Could not obtain compact signature" |
| 140 | + out_bs <- unsafePackByteString (out_sig_ptr, 64) |
| 141 | + out_v <- peek out_v_ptr |
| 142 | + return $ CompactRecSig out_bs (fromIntegral out_v) |
| 143 | + |
| 144 | +-- | Convert a recoverable signature into a normal signature. |
| 145 | +convertRecSig :: Ctx -> RecSig -> Sig |
| 146 | +convertRecSig (Ctx ctx) (RecSig rec_sig_bs) = unsafePerformIO $ |
| 147 | + unsafeUseByteString rec_sig_bs $ \(rec_sig_ptr, _) -> do |
| 148 | + out_ptr <- mallocBytes 64 |
| 149 | + ret <- ecdsaRecoverableSignatureConvert ctx out_ptr rec_sig_ptr |
| 150 | + unless (isSuccess ret) $ |
| 151 | + error "Could not convert a recoverable signature" |
| 152 | + out_bs <- unsafePackByteString (out_ptr, 64) |
| 153 | + return $ Sig out_bs |
| 154 | + |
| 155 | +-- | Create a recoverable ECDSA signature. |
| 156 | +signRecMsg :: Ctx -> SecKey -> Msg -> RecSig |
| 157 | +signRecMsg (Ctx ctx) (SecKey sec_key) (Msg m) = unsafePerformIO $ |
| 158 | + unsafeUseByteString sec_key $ \(sec_key_ptr, _) -> |
| 159 | + unsafeUseByteString m $ \(msg_ptr, _) -> do |
| 160 | + rec_sig_ptr <- mallocBytes 65 |
| 161 | + ret <- ecdsaSignRecoverable ctx rec_sig_ptr msg_ptr sec_key_ptr nullFunPtr nullPtr |
| 162 | + unless (isSuccess ret) $ do |
| 163 | + free rec_sig_ptr |
| 164 | + error "could not sign message" |
| 165 | + RecSig <$> unsafePackByteString (rec_sig_ptr, 65) |
| 166 | + |
| 167 | +-- | Recover an ECDSA public key from a signature. |
| 168 | +recover :: Ctx -> RecSig -> Msg -> Maybe PubKey |
| 169 | +recover (Ctx ctx) (RecSig rec_sig) (Msg m) = unsafePerformIO $ |
| 170 | + unsafeUseByteString rec_sig $ \(rec_sig_ptr, _) -> |
| 171 | + unsafeUseByteString m $ \(msg_ptr, _) -> do |
| 172 | + pub_key_ptr <- mallocBytes 64 |
| 173 | + ret <- ecdsaRecover ctx pub_key_ptr rec_sig_ptr msg_ptr |
| 174 | + if isSuccess ret |
| 175 | + then do |
| 176 | + pub_key_bs <- unsafePackByteString (pub_key_ptr, 64) |
| 177 | + return (Just (PubKey pub_key_bs)) |
| 178 | + else do |
| 179 | + free pub_key_ptr |
| 180 | + return Nothing |
0 commit comments