cryptocipher 0.3.5 → 0.3.6
raw patch · 5 files changed
+9/−291 lines, 5 filesdep ~crypto-pubkey-typesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
Dependency ranges changed: crypto-pubkey-types
API changes (from Hackage documentation)
- Crypto.Cipher.AES.Haskell: initKey128, initKey256, initKey192 :: ByteString -> Either String Key
- Crypto.Cipher.Blowfish: encrypt, decrypt :: Key -> ByteString -> ByteString
- Crypto.Cipher.RSA: private_n :: PrivateKey -> Integer
- Crypto.Cipher.RSA: private_size :: PrivateKey -> Int
+ Crypto.Cipher.AES.Haskell: initKey128 :: ByteString -> Either String Key
+ Crypto.Cipher.AES.Haskell: initKey192 :: ByteString -> Either String Key
+ Crypto.Cipher.AES.Haskell: initKey256 :: ByteString -> Either String Key
+ Crypto.Cipher.Blowfish: decrypt :: Key -> ByteString -> ByteString
+ Crypto.Cipher.Blowfish: encrypt :: Key -> ByteString -> ByteString
+ Crypto.Cipher.RSA: private_pub :: PrivateKey -> PublicKey
- Crypto.Cipher.RSA: PrivateKey :: Int -> Integer -> Integer -> Integer -> Integer -> Integer -> Integer -> Integer -> PrivateKey
+ Crypto.Cipher.RSA: PrivateKey :: PublicKey -> Integer -> Integer -> Integer -> Integer -> Integer -> Integer -> PrivateKey
Files
- Crypto/Cipher/AES/X86NI.hs +0/−80
- Crypto/Cipher/RSA.hs +6/−7
- Tests/tests.hs +1/−2
- cbits/aes/x86ni.c +0/−191
- cryptocipher.cabal +2/−11
− Crypto/Cipher/AES/X86NI.hs
@@ -1,80 +0,0 @@-{-# LANGUAGE ForeignFunctionInterface #-}-module Crypto.Cipher.AES.X86NI- ( encrypt- , decrypt- , encryptCBC- , decryptCBC- , initKey128- ) where--import Foreign.Storable-import Foreign.Marshal.Alloc (alloca, allocaBytes)-import Foreign.Ptr-import Foreign.C.String-import Foreign.C.Types-import Data.ByteString (ByteString)-import Data.ByteString.Unsafe (unsafeUseAsCString)-import Data.ByteString.Internal (create, memcpy)-import Data.Bits (shiftR)-import qualified Data.ByteString as B--import System.IO.Unsafe--type IV = ByteString-newtype Key = Key ByteString--instance Storable Key where- sizeOf _ = 16 * 11 * 2- alignment _ = 16- poke ptr (Key b) = unsafeUseAsCString b (\cs -> memcpy (castPtr ptr) (castPtr cs) (16 * 11 * 2))- peek ptr = Key `fmap` create (16*11*2) (\bptr -> memcpy bptr (castPtr ptr) (16 * 11 * 2))--foreign import ccall safe "aes.h aes_generate_key128"- c_aes_generate_key128 :: Ptr Key -> CString -> IO ()--foreign import ccall safe "aes.h aes_encrypt"- c_aes_encrypt :: CString -> Ptr Key -> CString -> CUInt -> IO ()--foreign import ccall safe "aes.h aes_decrypt"- c_aes_decrypt :: CString -> Ptr Key -> CString -> CUInt -> IO ()--foreign import ccall safe "aes.h aes_encrypt_cbc"- c_aes_encrypt_cbc :: CString -> Ptr Key -> CString -> CString -> CUInt -> IO ()--foreign import ccall safe "aes.h aes_decrypt_cbc"- c_aes_decrypt_cbc :: CString -> Ptr Key -> CString -> CString -> CUInt -> IO ()--withKey :: Key -> (Ptr Key -> IO a) -> IO a-withKey k f = alloca (\ikey -> poke ikey k >> f ikey)--{-# NOINLINE initKey128 #-}-initKey128 :: ByteString -> Key-initKey128 b = unsafePerformIO $ unsafeUseAsCString b (\ikey ->- alloca (\key -> c_aes_generate_key128 key ikey >> peek key))--{-# NOINLINE encrypt #-}-encrypt :: Key -> ByteString -> ByteString-encrypt key input = unsafePerformIO $ allocateAndMapBlocks input $ \blocks o i ->- withKey key $ \k -> c_aes_encrypt o k i blocks--{-# NOINLINE decrypt #-}-decrypt :: Key -> ByteString -> ByteString-decrypt key input = unsafePerformIO $ allocateAndMapBlocks input $ \blocks o i ->- withKey key $ \k -> c_aes_decrypt o k i blocks--{-# NOINLINE encryptCBC #-}-encryptCBC :: Key -> IV -> ByteString -> ByteString-encryptCBC key iv input = unsafePerformIO $ allocateAndMapBlocks input $ \blocks o i ->- withKey key $ \k -> unsafeUseAsCString iv $ \ivptr -> c_aes_encrypt_cbc o k ivptr i blocks--{-# NOINLINE decryptCBC #-}-decryptCBC :: Key -> IV -> ByteString -> ByteString-decryptCBC key iv input = unsafePerformIO $ allocateAndMapBlocks input $ \blocks o i ->- withKey key $ \k -> unsafeUseAsCString iv $ \ivptr -> c_aes_decrypt_cbc o k ivptr i blocks--allocateAndMapBlocks :: ByteString -> (CUInt -> Ptr CChar -> CString -> IO ()) -> IO ByteString-allocateAndMapBlocks input f = allocaBytes len (\output -> do unsafeUseAsCString input (f nbBlocks output)- B.packCStringLen (output, len))- where len = B.length input- nbBlocks = fromIntegral (len `shiftR` 4)-
Crypto/Cipher/RSA.hs view
@@ -119,20 +119,19 @@ case inverse e phi of Nothing -> generate rng' size e Just d -> do+ let pub = PublicKey+ { public_size = size+ , public_n = n+ , public_e = e+ } let priv = PrivateKey- { private_size = size- , private_n = n+ { private_pub = pub , private_d = d , private_p = p , private_q = q , private_dP = d `mod` (p-1) , private_dQ = d `mod` (q-1) , private_qinv = fromJust $ inverse q p -- q and p are coprime, so fromJust is safe.- }- let pub = PublicKey- { public_size = size- , public_n = n- , public_e = e } Right ((pub, priv), rng') where
Tests/tests.hs view
@@ -142,8 +142,7 @@ prop_rsa_sign_slow_valid = prop_rsa_sign_valid False rsaPrivatekey = RSA.PrivateKey- { RSA.private_size = 128- , RSA.private_n = 140203425894164333410594309212077886844966070748523642084363106504571537866632850620326769291612455847330220940078873180639537021888802572151020701352955762744921926221566899281852945861389488419179600933178716009889963150132778947506523961974222282461654256451508762805133855866018054403911588630700228345151+ { RSA.private_pub = rsaPublickey , RSA.private_d = 133764127300370985476360382258931504810339098611363623122953018301285450176037234703101635770582297431466449863745848961134143024057267778947569638425565153896020107107895924597628599677345887446144410702679470631826418774397895304952287674790343620803686034122942606764275835668353720152078674967983573326257 , RSA.private_p = 12909745499610419492560645699977670082358944785082915010582495768046269235061708286800087976003942261296869875915181420265794156699308840835123749375331319 , RSA.private_q = 10860278066550210927914375228722265675263011756304443428318337179619069537063135098400347475029673115805419186390580990519363257108008103841271008948795129
− cbits/aes/x86ni.c
@@ -1,191 +0,0 @@-/*- * Copyright (c) 2012 Vincent Hanquez <vincent@snarc.org>- * - * All rights reserved.- * - * Redistribution and use in source and binary forms, with or without- * modification, are permitted provided that the following conditions- * are met:- * 1. Redistributions of source code must retain the above copyright- * notice, this list of conditions and the following disclaimer.- * 2. Redistributions in binary form must reproduce the above copyright- * notice, this list of conditions and the following disclaimer in the- * documentation and/or other materials provided with the distribution.- * 3. Neither the name of the author nor the names of his contributors- * may be used to endorse or promote products derived from this software- * without specific prior written permission.- * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE- * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF- * SUCH DAMAGE.- */-#include <wmmintrin.h>-#include <tmmintrin.h>-#include "aes.h"--static __m128i aes_128_key_expansion(__m128i key, __m128i keygened)-{- keygened = _mm_shuffle_epi32(keygened, _MM_SHUFFLE(3,3,3,3));- key = _mm_xor_si128(key, _mm_slli_si128(key, 4));- key = _mm_xor_si128(key, _mm_slli_si128(key, 4));- key = _mm_xor_si128(key, _mm_slli_si128(key, 4));- return _mm_xor_si128(key, keygened);-}--void aes_generate_key128(aes_key128 *key, uint8_t *ikey)-{- __m128i *k = (__m128i *) key->_data;- k[0] = _mm_loadu_si128((const __m128i*) ikey);--#define AES_128_key_exp(K, RCON) aes_128_key_expansion(K, _mm_aeskeygenassist_si128(K, RCON))- k[1] = AES_128_key_exp(k[0], 0x01);- k[2] = AES_128_key_exp(k[1], 0x02);- k[3] = AES_128_key_exp(k[2], 0x04);- k[4] = AES_128_key_exp(k[3], 0x08);- k[5] = AES_128_key_exp(k[4], 0x10);- k[6] = AES_128_key_exp(k[5], 0x20);- k[7] = AES_128_key_exp(k[6], 0x40);- k[8] = AES_128_key_exp(k[7], 0x80);- k[9] = AES_128_key_exp(k[8], 0x1B);- k[10] = AES_128_key_exp(k[9], 0x36);-- /* generate decryption keys in reverse order.- * k[10] is shared by last encryption and first decryption rounds- * k[20] is shared by first encryption round (and is the original user key) */- k[11] = _mm_aesimc_si128(k[9]);- k[12] = _mm_aesimc_si128(k[8]);- k[13] = _mm_aesimc_si128(k[7]);- k[14] = _mm_aesimc_si128(k[6]);- k[15] = _mm_aesimc_si128(k[5]);- k[16] = _mm_aesimc_si128(k[4]);- k[17] = _mm_aesimc_si128(k[3]);- k[18] = _mm_aesimc_si128(k[2]);- k[19] = _mm_aesimc_si128(k[1]);-}--#define PRELOAD_ENC_KEYS(k) \- __m128i K0 = k[0]; __m128i K1 = k[1]; __m128i K2 = k[2]; __m128i K3 = k[3]; \- __m128i K4 = k[4]; __m128i K5 = k[5]; __m128i K6 = k[6]; __m128i K7 = k[7]; \- __m128i K8 = k[8]; __m128i K9 = k[9]; __m128i K10 = k[10];--#define DO_ENC_BLOCK(m) \- m = _mm_xor_si128(m, K0); \- m = _mm_aesenc_si128(m, K1); \- m = _mm_aesenc_si128(m, K2); \- m = _mm_aesenc_si128(m, K3); \- m = _mm_aesenc_si128(m, K4); \- m = _mm_aesenc_si128(m, K5); \- m = _mm_aesenc_si128(m, K6); \- m = _mm_aesenc_si128(m, K7); \- m = _mm_aesenc_si128(m, K8); \- m = _mm_aesenc_si128(m, K9); \- m = _mm_aesenclast_si128(m, K10);--#define PRELOAD_DEC_KEYS(k) \- __m128i K0 = k[10+0]; __m128i K1 = k[10+1]; __m128i K2 = k[10+2]; __m128i K3 = k[10+3]; \- __m128i K4 = k[10+4]; __m128i K5 = k[10+5]; __m128i K6 = k[10+6]; __m128i K7 = k[10+7]; \- __m128i K8 = k[10+8]; __m128i K9 = k[10+9]; __m128i K10 = k[0];--#define DO_DEC_BLOCK(m) \- m = _mm_xor_si128(m, K0); \- m = _mm_aesdec_si128(m, K1); \- m = _mm_aesdec_si128(m, K2); \- m = _mm_aesdec_si128(m, K3); \- m = _mm_aesdec_si128(m, K4); \- m = _mm_aesdec_si128(m, K5); \- m = _mm_aesdec_si128(m, K6); \- m = _mm_aesdec_si128(m, K7); \- m = _mm_aesdec_si128(m, K8); \- m = _mm_aesdec_si128(m, K9); \- m = _mm_aesdeclast_si128(m, K10);--void aes_encrypt(uint8_t *out, aes_key128 *key, uint8_t *in, uint32_t blocks)-{- uint32_t i;- uint64_t _out[2] __attribute__((aligned(16)));- __m128i *k = (__m128i *) key->_data;-- PRELOAD_ENC_KEYS(k);-- for (i = 0; i < blocks; i++, in += 16, out += 16) {- __m128i m = _mm_loadu_si128((__m128i *) in);-- DO_ENC_BLOCK(m);-- _mm_store_si128((__m128i *) _out, m);- ((uint64_t *) out)[0] = (_out[0]);- ((uint64_t *) out)[1] = (_out[1]);- }-}--void aes_decrypt(uint8_t *out, aes_key128 *key, uint8_t *in, uint32_t blocks)-{- uint32_t i;- uint64_t _out[2] __attribute__((aligned(16)));- __m128i *k = (__m128i *) key->_data;-- PRELOAD_DEC_KEYS(k);-- for (i = 0; i < blocks; i++, in += 16, out += 16) {- __m128i m = _mm_loadu_si128((__m128i *) in);-- DO_DEC_BLOCK(m);-- _mm_store_si128((__m128i *) _out, m);- ((uint64_t *) out)[0] = (_out[0]);- ((uint64_t *) out)[1] = (_out[1]);- }-}--void aes_encrypt_cbc(uint8_t *out, aes_key128 *key, uint8_t *_iv, uint8_t *in, uint32_t blocks)-{- uint32_t i;- uint64_t _out[2] __attribute__((aligned(16)));- __m128i *k = (__m128i *) key->_data;- __m128i iv = _mm_loadu_si128((__m128i *) _iv);-- PRELOAD_ENC_KEYS(k);-- for (i = 0; i < blocks; i++, in += 16, out += 16) {- __m128i m = _mm_loadu_si128((__m128i *) in);- m = _mm_xor_si128(m, iv);-- DO_ENC_BLOCK(m);-- _mm_store_si128((__m128i *) _out, m);- iv = m;- ((uint64_t *) out)[0] = (_out[0]);- ((uint64_t *) out)[1] = (_out[1]);- }-}--void aes_decrypt_cbc(uint8_t *out, aes_key128 *key, uint8_t *_iv, uint8_t *in, uint32_t blocks)-{- uint32_t i;- uint64_t _out[2] __attribute__((aligned(16)));- __m128i *k = (__m128i *) key->_data;- __m128i iv = _mm_loadu_si128((__m128i *) _iv);-- PRELOAD_DEC_KEYS(k);-- for (i = 0; i < blocks; i++, in += 16, out += 16) {- __m128i m = _mm_loadu_si128((__m128i *) in);- __m128i ivnext = m;-- DO_DEC_BLOCK(m);- m = _mm_xor_si128(m, iv);-- _mm_store_si128((__m128i *) _out, m);- iv = ivnext;- ((uint64_t *) out)[0] = (_out[0]);- ((uint64_t *) out)[1] = (_out[1]);- }-}
cryptocipher.cabal view
@@ -1,5 +1,5 @@ Name: cryptocipher-Version: 0.3.5+Version: 0.3.6 Description: Symmetrical Block, Stream and PubKey Ciphers License: BSD3 License-file: LICENSE@@ -30,7 +30,7 @@ , ghc-prim , primitive , crypto-api >= 0.5- , crypto-pubkey-types >= 0.1 && < 0.2+ , crypto-pubkey-types >= 0.2 && < 0.3 , tagged , cereal Exposed-modules: Crypto.Cipher.RC4@@ -49,11 +49,6 @@ Number.Prime Crypto.Cipher.ElGamal ghc-options: -Wall- if flag(aesni)- cpp-options: -DHAVE_AESNI- Exposed-modules: Crypto.Cipher.AES.X86NI- C-sources: cbits/aes/x86ni.c- CC-options: -mssse3 -maes Test-Suite test-cryptocipher type: exitcode-stdio-1.0@@ -83,10 +78,6 @@ , mtl else Buildable: False- if flag(aesni)- cpp-options: -DHAVE_AESNI- C-sources: cbits/aes/x86ni.c- CC-options: -mssse3 -maes source-repository head type: git