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