module Network.TLS.MAC (
macSSL,
hmac,
prf_MD5,
prf_SHA1,
prf_SHA256,
prf_TLS,
prf_MD5SHA1,
PRF,
) where
import Data.ByteArray (ByteArray, ByteArrayAccess)
import qualified Data.ByteArray as BA
import Network.TLS.Crypto
import Network.TLS.Imports
import Network.TLS.Types
type HMAC = Secret -> ByteString -> Secret
macSSL :: Hash -> HMAC
macSSL :: Hash -> HMAC
macSSL Hash
alg ScrubbedBytes
secret ByteString
msg =
ScrubbedBytes -> ScrubbedBytes
f (ScrubbedBytes -> ScrubbedBytes) -> ScrubbedBytes -> ScrubbedBytes
forall a b. (a -> b) -> a -> b
$
[ScrubbedBytes] -> ScrubbedBytes
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
[bin] -> bout
BA.concat
[ ScrubbedBytes
secret
, Int -> Word8 -> ScrubbedBytes
forall ba. ByteArray ba => Int -> Word8 -> ba
BA.replicate Int
padLen Word8
0x5c
, ScrubbedBytes -> ScrubbedBytes
f (ScrubbedBytes -> ScrubbedBytes) -> ScrubbedBytes -> ScrubbedBytes
forall a b. (a -> b) -> a -> b
$ [ScrubbedBytes] -> ScrubbedBytes
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
[bin] -> bout
BA.concat [ScrubbedBytes
secret, Int -> Word8 -> ScrubbedBytes
forall ba. ByteArray ba => Int -> Word8 -> ba
BA.replicate Int
padLen Word8
0x36, ByteString -> ScrubbedBytes
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert ByteString
msg]
]
where
padLen :: Int
padLen = case Hash
alg of
Hash
MD5 -> Int
48
Hash
SHA1 -> Int
40
Hash
_ -> [Char] -> Int
forall a. HasCallStack => [Char] -> a
error ([Char]
"internal error: macSSL called with " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Hash -> [Char]
forall a. Show a => a -> [Char]
show Hash
alg)
f :: ScrubbedBytes -> ScrubbedBytes
f = Hash -> ScrubbedBytes -> ScrubbedBytes
forall ba. (ByteArray ba, ByteArrayAccess ba) => Hash -> ba -> ba
hash Hash
alg
hmac :: (ByteArray ba, ByteArrayAccess ba) => Hash -> ba -> ByteString -> ba
hmac :: forall ba.
(ByteArray ba, ByteArrayAccess ba) =>
Hash -> ba -> ByteString -> ba
hmac Hash
alg ba
secret ByteString
msg = ba -> ba
f (ba -> ba) -> ba -> ba
forall a b. (a -> b) -> a -> b
$ ba -> ba -> ba
forall bs. ByteArray bs => bs -> bs -> bs
BA.append ba
opad (ba -> ba
f (ba -> ba) -> ba -> ba
forall a b. (a -> b) -> a -> b
$ ba -> ba -> ba
forall bs. ByteArray bs => bs -> bs -> bs
BA.append ba
ipad (ba -> ba) -> ba -> ba
forall a b. (a -> b) -> a -> b
$ ByteString -> ba
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert ByteString
msg)
where
opad :: ba
opad = (Word8 -> Word8) -> ba -> ba
forall ba.
(ByteArrayAccess ba, ByteArray ba) =>
(Word8 -> Word8) -> ba -> ba
BA.map (Word8
0x5c Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
`xor`) ba
k'
ipad :: ba
ipad = (Word8 -> Word8) -> ba -> ba
forall ba.
(ByteArrayAccess ba, ByteArray ba) =>
(Word8 -> Word8) -> ba -> ba
BA.map (Word8
0x36 Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
`xor`) ba
k'
f :: ba -> ba
f = Hash -> ba -> ba
forall ba. (ByteArray ba, ByteArrayAccess ba) => Hash -> ba -> ba
hash Hash
alg
bl :: Int
bl = Hash -> Int
hashBlockSize Hash
alg
k' :: ba
k' = ba -> ba -> ba
forall bs. ByteArray bs => bs -> bs -> bs
BA.append ba
kt ba
pad
where
kt :: ba
kt = if ba -> Int
forall ba. ByteArrayAccess ba => ba -> Int
BA.length ba
secret Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
bl then ba -> ba
f ba
secret else ba
secret
pad :: ba
pad = Int -> Word8 -> ba
forall ba. ByteArray ba => Int -> Word8 -> ba
BA.replicate (Int -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
bl Int -> Int -> Int
forall a. Num a => a -> a -> a
- ba -> Int
forall ba. ByteArrayAccess ba => ba -> Int
BA.length ba
kt) Word8
0
hmacIter
:: HMAC -> Secret -> ByteString -> ByteString -> Int -> [Secret]
hmacIter :: HMAC
-> ScrubbedBytes
-> ByteString
-> ByteString
-> Int
-> [ScrubbedBytes]
hmacIter HMAC
f ScrubbedBytes
secret ByteString
seed ByteString
aprev Int
len =
let an :: ScrubbedBytes
an = HMAC
f ScrubbedBytes
secret ByteString
aprev
in let out :: ScrubbedBytes
out = HMAC
f ScrubbedBytes
secret ([ScrubbedBytes] -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
[bin] -> bout
BA.concat [ScrubbedBytes
an, ByteString -> ScrubbedBytes
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert ByteString
seed])
in let digestsize :: Int
digestsize = ScrubbedBytes -> Int
forall ba. ByteArrayAccess ba => ba -> Int
BA.length ScrubbedBytes
out
in if Int
digestsize Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
len
then [Int -> ScrubbedBytes -> ScrubbedBytes
forall bs. ByteArray bs => Int -> bs -> bs
BA.take (Int -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len) ScrubbedBytes
out]
else ScrubbedBytes
out ScrubbedBytes -> [ScrubbedBytes] -> [ScrubbedBytes]
forall a. a -> [a] -> [a]
: HMAC
-> ScrubbedBytes
-> ByteString
-> ByteString
-> Int
-> [ScrubbedBytes]
hmacIter HMAC
f ScrubbedBytes
secret ByteString
seed (ScrubbedBytes -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert ScrubbedBytes
an) (Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
digestsize)
type PRF = Secret -> ByteString -> Int -> Secret
prf_SHA1 :: PRF
prf_SHA1 :: PRF
prf_SHA1 ScrubbedBytes
secret ByteString
seed Int
len = [ScrubbedBytes] -> ScrubbedBytes
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
[bin] -> bout
BA.concat ([ScrubbedBytes] -> ScrubbedBytes)
-> [ScrubbedBytes] -> ScrubbedBytes
forall a b. (a -> b) -> a -> b
$ HMAC
-> ScrubbedBytes
-> ByteString
-> ByteString
-> Int
-> [ScrubbedBytes]
hmacIter (Hash -> HMAC
forall ba.
(ByteArray ba, ByteArrayAccess ba) =>
Hash -> ba -> ByteString -> ba
hmac Hash
SHA1) ScrubbedBytes
secret ByteString
seed ByteString
seed Int
len
prf_MD5 :: PRF
prf_MD5 :: PRF
prf_MD5 ScrubbedBytes
secret ByteString
seed Int
len = [ScrubbedBytes] -> ScrubbedBytes
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
[bin] -> bout
BA.concat ([ScrubbedBytes] -> ScrubbedBytes)
-> [ScrubbedBytes] -> ScrubbedBytes
forall a b. (a -> b) -> a -> b
$ HMAC
-> ScrubbedBytes
-> ByteString
-> ByteString
-> Int
-> [ScrubbedBytes]
hmacIter (Hash -> HMAC
forall ba.
(ByteArray ba, ByteArrayAccess ba) =>
Hash -> ba -> ByteString -> ba
hmac Hash
MD5) ScrubbedBytes
secret ByteString
seed ByteString
seed Int
len
prf_MD5SHA1 :: PRF
prf_MD5SHA1 :: PRF
prf_MD5SHA1 ScrubbedBytes
secret ByteString
seed Int
len =
ScrubbedBytes -> ScrubbedBytes -> ScrubbedBytes
forall a b c.
(ByteArrayAccess a, ByteArrayAccess b, ByteArray c) =>
a -> b -> c
BA.xor (PRF
prf_MD5 ScrubbedBytes
s1 ByteString
seed Int
len) (PRF
prf_SHA1 ScrubbedBytes
s2 ByteString
seed Int
len)
where
slen :: Int
slen = ScrubbedBytes -> Int
forall ba. ByteArrayAccess ba => ba -> Int
BA.length ScrubbedBytes
secret
s1 :: ScrubbedBytes
s1 = Int -> ScrubbedBytes -> ScrubbedBytes
forall bs. ByteArray bs => Int -> bs -> bs
BA.take (Int
slen Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
slen Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
2) ScrubbedBytes
secret
s2 :: ScrubbedBytes
s2 = Int -> ScrubbedBytes -> ScrubbedBytes
forall bs. ByteArray bs => Int -> bs -> bs
BA.drop (Int
slen Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2) ScrubbedBytes
secret
prf_SHA256 :: PRF
prf_SHA256 :: PRF
prf_SHA256 ScrubbedBytes
secret ByteString
seed Int
len = [ScrubbedBytes] -> ScrubbedBytes
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
[bin] -> bout
BA.concat ([ScrubbedBytes] -> ScrubbedBytes)
-> [ScrubbedBytes] -> ScrubbedBytes
forall a b. (a -> b) -> a -> b
$ HMAC
-> ScrubbedBytes
-> ByteString
-> ByteString
-> Int
-> [ScrubbedBytes]
hmacIter (Hash -> HMAC
forall ba.
(ByteArray ba, ByteArrayAccess ba) =>
Hash -> ba -> ByteString -> ba
hmac Hash
SHA256) ScrubbedBytes
secret ByteString
seed ByteString
seed Int
len
prf_TLS :: Version -> Hash -> PRF
prf_TLS :: Version -> Hash -> PRF
prf_TLS Version
_ Hash
halg ScrubbedBytes
secret ByteString
seed Int
len =
[ScrubbedBytes] -> ScrubbedBytes
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
[bin] -> bout
BA.concat ([ScrubbedBytes] -> ScrubbedBytes)
-> [ScrubbedBytes] -> ScrubbedBytes
forall a b. (a -> b) -> a -> b
$ HMAC
-> ScrubbedBytes
-> ByteString
-> ByteString
-> Int
-> [ScrubbedBytes]
hmacIter (Hash -> HMAC
forall ba.
(ByteArray ba, ByteArrayAccess ba) =>
Hash -> ba -> ByteString -> ba
hmac Hash
halg) ScrubbedBytes
secret ByteString
seed ByteString
seed Int
len