Adding a `constEq` (or similar) function for security use cases

Open
#697 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
haskell
Domain
security

Research direction

Start by reviewing the issue's proposed constEq signature and example, then inspect the bytestring API to determine the appropriate integration point and required security guarantees. Done means agreeing on the API and behavior for byte comparisons, implementing it safely, and adding verification for the intended constant-time use case.

Written by the indexing model from the issue text.

Description

To prevent timing attacks, sometimes you'd want to compare bytes (like hashes) in constant time, since this can give the attacker information on how far they have matched the bytes.

This is a semi-quick example of what it might/would look like (did not compile, might have errors)
(based it off of constEq from the memory package)

constEq :: ByteString -> ByteString -> Bool
constEq (BS fptr1 len1) (BS fptr2 len2)
    | len1 /= len2 = False
    | otherwise = accursedUnutterablePerformIO $ do
        withForeignPtr fptr1 $ \ptr1 ->
        withForeignPtr fptr2 $ \ptr2 ->
          let loop i !acc
                  | i == len1 = pure $! acc == 0
                  | otherwise = do
                      mZero <- xor <$> peekByteOff i ptr1 <*> (peekByteOff i ptr2 :: IO Word8)
                      loop (i + 1) (acc .|. mZero)
           in loop 0 0           
Dominant language
Haskell
Stars
302
Forks
146
Avg merge
7d 21h
Merged PRs (30d)
1

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from haskell/bytestring

All issues in haskell/bytestring

Similar issues

More Haskell issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.