Created
November 20, 2008 06:47
-
-
Save hannahwhy/26970 to your computer and use it in GitHub Desktop.
braindead implementation of djb's salsa20
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Bits | |
import Data.Word (Word32, Word64) | |
-- Utilities | |
concatBytes :: (Bits a) => a -> a -> a | |
concatBytes b1 b2 = (.|.) (shiftL b1 8) b2 | |
le4 :: [Word32] -> [[Word32]] | |
le4 (x4:x3:x2:x1:[]) = [x1,x2,x3,x4]:[] | |
le4 (x4:x3:x2:x1:xs) = [x1,x2,x3,x4]:le4 xs | |
le4 _ = error "list length must be a multiple of 4" | |
-- Key and nonce setup (key and nonce values taken from djb's paper) | |
key :: [Word32] | |
key = map (foldl concatBytes 0) (take 8 (le4 [1..])) | |
nonce :: [Word32] | |
nonce = map (foldl concatBytes 0) (le4 [3,1,4,1,5,9,2,6]) | |
counter :: [Word32] | |
counter = map (foldl concatBytes 0) [[0,0,0,0],[0,0,0,7]] | |
-- Salsa20 core (from djb's paper) | |
core = [0x61707865, key !! 0, key !! 1, key !! 2, | |
key !! 3, 0x3320646e, nonce !! 0, nonce !! 1, | |
counter !! 1, counter !! 0, 0x79622d32, key !! 4, | |
key !! 5, key !! 6, key !! 7, 0x6b206574] | |
-- Test vectors (from djb's paper) | |
round0 :: [Word32] | |
round0 = [0x61707865, 0x04030201, 0x08070605, 0x0c0b0a09, | |
0x100f0e0d, 0x3320646e, 0x01040103, 0x06020905, | |
0x00000007, 0x00000000, 0x79622d32, 0x14131211, | |
0x18171615, 0x1c1b1a19, 0x201f1e1d, 0x6b206574] | |
round1 :: [Word32] | |
round1 = [0x61707865, 0x04030201, 0x08070605, 0x95b0c8b6, | |
0xd3c83331, 0x3320646e, 0x01040103, 0x06020905, | |
0x00000007, 0x91b3379b, 0x79622d32, 0x14131211, | |
0x18171615, 0x1c1b1a19, 0x130804a0, 0x6b206574] | |
round2 :: [Word32] | |
round2 = [0x61707865, 0x04030201, 0xdc64a31d, 0x95b0c8b6, | |
0xd3c83331, 0x3320646e, 0x01040103, 0xa45e5d04, | |
0x71572c6d, 0x91b3379b, 0x79622d32, 0x14131211, | |
0x18171615, 0xbb230990, 0x130804a0, 0x6b206574] | |
round3 :: [Word32] | |
round3 = [0x61707865, 0xcc266b9b, 0xdc64a31d, 0x95b0c8b6, | |
0xd3c83331, 0x3320646e, 0x95f3bcee, 0xa45e5d04, | |
0x71572c6d, 0x91b3379b, 0x79622d32, 0xf0a45550, | |
0xf3e4deb6, 0xbb230990, 0x130804a0, 0x6b206574] | |
round4 :: [Word32] | |
round4 = [0x4dfdec95, 0xcc266b9b, 0xdc64a31d, 0x95b0c8b6, | |
0xd3c83331, 0xe78e794b, 0x95f3bcee, 0xa45e5d04, | |
0x71572c6d, 0x91b3379b, 0xf94fe453, 0xf0a45550, | |
0xf3e4deb6, 0xbb230990, 0x130804a0, 0xa272317e] | |
final :: [Word32] | |
final = [0x4dfdec95, 0xd3c83331, 0x71572c6d, 0xf3e4deb6, | |
0xcc266b9b, 0xe78e794b, 0x91b3379b, 0xbb230990, | |
0xdc64a31d, 0x95f3bcee, 0xf94fe453, 0x130804a0, | |
0x95b0c8b6, 0xa45e5d04, 0xf0a45550, 0xa272317e] | |
final2 :: [Word32] | |
final2 = [0xba2409b1, 0x1b7cce6a, 0x29115dcf, 0x5037e027, | |
0x37b75378, 0x348d94c8, 0x3ea582b3, 0xc3a9a148, | |
0x825bfcb9, 0x226ae9eb, 0x63dd7748, 0x7129a215, | |
0x4effd1ec, 0x5f25dc72, 0xa6c3d164, 0x152a26d8] | |
final20 :: [Word32] | |
final20 = [0x58318d3e, 0x0292df4f, 0xa28d8215, 0xa1aca723, | |
0x697a34c7, 0xf2f00ba8, 0x63e9b0a1, 0x27250e3a, | |
0xb1c7f1f3, 0x62066edc, 0x66d3ccf1, 0xb0365cf3, | |
0x091ad09e, 0x64f0c40f, 0xd60d95ea, 0x00be78c9] | |
-- | |
-- x0:x1:x2:x3 x0 x1 x2 (X x3 (RL xf+xb) 7) | |
-- x4:x5:x6:x7 --> (X x4 (RL x0+xc 7)) x5 x6 x7 | |
-- x8:x9:xa:xb x8 (X x9 (RL x5+x1 7)) xa xb | |
-- xc:xd:xe:xf xc xd (X xe (RL xa+x6) 7) xf | |
-- | |
t1 :: [Word32] -> [Word32] | |
t1 (x0:x1:x2:x3:x4:x5:x6:x7:x8:x9:xa:xb:xc:xd:xe:xf:[]) = let m = \x y z -> xor x (rotateL (y+z) 7) in | |
x0:x1:x2:(m x3 xf xb):(m x4 x0 xc):x5:x6:x7:x8:(m x9 x5 x1):xa:xb:xc:xd:(m xe xa x6):xf:[] | |
-- | |
-- x0:x1:x2:x3 x0 x1 (X x2 (RL xa+xe) 9) x3 | |
-- x4:x5:x6:x7 --> x4 x5 x6 (X x7 (RL x3+xf) 9) | |
-- x8:x9:xa:xb (X x8 (RL x0+x4 9)) x9 xa xb | |
-- xc:xd:xe:xf xc (X xd (RL x5+x9 9)) xe xf | |
-- | |
t2 :: [Word32] -> [Word32] | |
t2 (x0:x1:x2:x3:x4:x5:x6:x7:x8:x9:xa:xb:xc:xd:xe:xf:[]) = let m = \x y z -> xor x (rotateL (y+z) 9) in | |
x0:x1:(m x2 xa xe):x3:x4:x5:x6:(m x7 x3 xf):(m x8 x0 x4):x9:xa:xb:xc:(m xd x5 x9):xe:xf:[] | |
-- | |
-- x0:x1:x2:x3 x0 (X x1 (RL x9+xd)) x2 x3 | |
-- x4:x5:x6:x7 --> x4 x5 (X x6 (RL xe+x2)) x7 | |
-- x8:x9:xa:xb x8 x9 xa (X xb (RL x3+x7)) | |
-- xc:xd:xe:xf (X xc (RL x4+x8)) xd xe xf | |
-- | |
t3 :: [Word32] -> [Word32] | |
t3 (x0:x1:x2:x3:x4:x5:x6:x7:x8:x9:xa:xb:xc:xd:xe:xf:[]) = let m = \x y z -> xor x (rotateL (y+z) 13) in | |
x0:(m x1 x9 xd):x2:x3:x4:x5:(m x6 xe x2):x7:x8:x9:xa:(m xb x3 x7):(m xc x4 x8):xd:xe:xf:[] | |
-- | |
-- x0:x1:x2:x3 (X x0 (RL x8 xc)) x1 x2 x3 | |
-- x4:x5:x6:x7 --> x4 (X x5 (RL xd+x1)) x6 x7 | |
-- x8:x9:xa:xb x8 x9 (X xa (RL x2+x6) xb | |
-- xc:xd:xe:xf xc xd xe (X xf (RL x7+xb)) | |
-- | |
t4 :: [Word32] -> [Word32] | |
t4 (x0:x1:x2:x3:x4:x5:x6:x7:x8:x9:xa:xb:xc:xd:xe:xf:[]) = let m = \x y z -> xor x (rotateL (y+z) 18) in | |
(m x0 x8 xc):x1:x2:x3:x4:(m x5 xd x1):x6:x7:x8:x9:(m xa x2 x6):xb:xc:xd:xe:(m xf x7 xb):[] | |
-- | |
-- x0:x1:x2:x3 x0:x4:x8:xc | |
-- x4:x5:x6:x7 --> x1:x5:x9:xd | |
-- x8:x9:xa:xb x2:x6:xa:xe | |
-- xc:xd:xe:xf x3:x7:xb:xf | |
-- | |
t5 :: [Word32] -> [Word32] | |
t5 (x0:x1:x2:x3:x4:x5:x6:x7:x8:x9:xa:xb:xc:xd:xe:xf:[]) = x0:x4:x8:xc:x1:x5:x9:xd:x2:x6:xa:xe:x3:x7:xb:xf:[] | |
doRound :: [Word32] -> [Word32] | |
doRound a = (t5 . t4 . t3 . t2 . t1) a | |
salsa :: Int -> [Word32] -> [Word32] | |
salsa 1 a = doRound a | |
salsa n a = salsa (n - 1) (doRound a) | |
-- key, nonce, stream (from counter 0) | |
salsa20Stream :: [Word32] -> [Word32] -> [Word32] | |
salsa20Stream key nonce = salsa20StreamFrom 0 key nonce | |
salsa20StreamFrom :: Word64 -> [Word32] -> [Word32] -> [Word32] | |
salsa20StreamFrom from key nonce = let msw = (fromIntegral $ shiftR ((.&.) from 0xffffffff00000000) 32) | |
lsw = (fromIntegral $ (.&.) from 0x00000000ffffffff) | |
core = [0x61707865, key !! 0, key !! 1, key !! 2, | |
key !! 3, 0x3320646e, nonce !! 0, nonce !! 1, | |
lsw, msw, 0x79622d32, key !! 4, | |
key !! 5, key !! 6, key !! 7, 0x6b206574] | |
in (salsa 20 core) ++ salsa20StreamFrom (from + 1) key nonce | |
-- | |
-- to replicate the example in djb's paper: | |
-- | |
-- salsa 20 core == final20 | |
-- take 16 (salsa20StreamFrom 7 key nonce) == final20 | |
-- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment