From 6665922672948a45ce5d306458660ef094b37007 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sun, 19 Jan 2014 17:13:32 -0600 Subject: add cipher bindings for CommonCrypto --- .../hazmat/bindings/commoncrypto/binding.py | 1 + .../hazmat/bindings/commoncrypto/common_cryptor.py | 131 +++++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 cryptography/hazmat/bindings/commoncrypto/common_cryptor.py diff --git a/cryptography/hazmat/bindings/commoncrypto/binding.py b/cryptography/hazmat/bindings/commoncrypto/binding.py index 9c1af40a..a5a0dca8 100644 --- a/cryptography/hazmat/bindings/commoncrypto/binding.py +++ b/cryptography/hazmat/bindings/commoncrypto/binding.py @@ -26,6 +26,7 @@ class Binding(object): _modules = [ "common_digest", "common_hmac", + "common_cryptor", ] ffi = None diff --git a/cryptography/hazmat/bindings/commoncrypto/common_cryptor.py b/cryptography/hazmat/bindings/commoncrypto/common_cryptor.py new file mode 100644 index 00000000..69edf1e7 --- /dev/null +++ b/cryptography/hazmat/bindings/commoncrypto/common_cryptor.py @@ -0,0 +1,131 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +INCLUDES = """ +#include +""" + +TYPES = """ +enum { + kCCAlgorithmAES128 = 0, + kCCAlgorithmDES, + kCCAlgorithm3DES, + kCCAlgorithmCAST, + kCCAlgorithmRC4, + kCCAlgorithmRC2, + kCCAlgorithmBlowfish +}; +typedef uint32_t CCAlgorithm; +enum { + kCCSuccess = 0, + kCCParamError = -4300, + kCCBufferTooSmall = -4301, + kCCMemoryFailure = -4302, + kCCAlignmentError = -4303, + kCCDecodeError = -4304, + kCCUnimplemented = -4305 +}; +typedef int32_t CCCryptorStatus; +typedef uint32_t CCOptions; +enum { + kCCEncrypt = 0, + kCCDecrypt, +}; +typedef uint32_t CCOperation; +typedef ... *CCCryptorRef; + +enum { + kCCModeOptionCTR_LE = 0x0001, + kCCModeOptionCTR_BE = 0x0002 +}; + +typedef uint32_t CCModeOptions; + +enum { + kCCModeECB = 1, + kCCModeCBC = 2, + kCCModeCFB = 3, + kCCModeCTR = 4, + kCCModeF8 = 5, // Unimplemented for now (not included) + kCCModeLRW = 6, // Unimplemented for now (not included) + kCCModeOFB = 7, + kCCModeXTS = 8, + kCCModeRC4 = 9, + kCCModeCFB8 = 10, +}; +typedef uint32_t CCMode; +enum { + ccNoPadding = 0, + ccPKCS7Padding = 1, +}; +typedef uint32_t CCPadding; +""" + +FUNCTIONS = """ +CCCryptorStatus CCCryptorCreateWithMode( + CCOperation, /* kCCEncrypt, kCCEncrypt */ + CCMode, + CCAlgorithm, + CCPadding, + const void *, /* optional initialization vector */ + const void *, /* raw key material */ + size_t, + const void *, /* raw tweak material */ + size_t, + int, /* number of rounds. 0 == default */ + CCModeOptions, + CCCryptorRef *); /* RETURNED */ + +CCCryptorStatus CCCryptorCreate( + CCOperation, /* kCCEncrypt, etc. */ + CCAlgorithm, /* kCCAlgorithmDES, etc. */ + CCOptions, /* kCCOptionPKCS7Padding, etc. */ + const void *, /* raw key material */ + size_t, + const void *, /* optional initialization vector */ + CCCryptorRef *); /* RETURNED */ +CCCryptorStatus CCCryptorUpdate( + CCCryptorRef, + const void *, + size_t, + void *, /* data RETURNED here */ + size_t, + size_t *); /* number of bytes written */ +CCCryptorStatus CCCryptorFinal( + CCCryptorRef, + void *, + size_t, + size_t *); /* number of bytes written */ +CCCryptorStatus CCCryptorRelease(CCCryptorRef); + +/* GCM functions, 10.8+ iOS 5+ */ +enum { + kCCModeGCM = 11 +}; +CCCryptorStatus CCCryptorGCMAddIV(CCCryptorRef, const void *, size_t); +CCCryptorStatus CCCryptorGCMAddAAD(CCCryptorRef, const void *, size_t); +CCCryptorStatus CCCryptorGCMEncrypt(CCCryptorRef, const void *, size_t, + void *); +CCCryptorStatus CCCryptorGCMDecrypt(CCCryptorRef, const void *, size_t, + void *); +CCCryptorStatus CCCryptorGCMFinal(CCCryptorRef, const void *, size_t *); +CCCryptorStatus CCCryptorGCMReset(CCCryptorRef); +""" + +MACROS = """ +""" + +CUSTOMIZATIONS = """ +""" + +CONDITIONAL_NAMES = {} -- cgit v1.2.3 From 8a736087150e1fbd69fb16658f8797aa9125839d Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sun, 19 Jan 2014 17:42:22 -0600 Subject: reformat bindings and remove GCM for the moment --- .../hazmat/bindings/commoncrypto/common_cryptor.py | 94 +++++++--------------- 1 file changed, 29 insertions(+), 65 deletions(-) diff --git a/cryptography/hazmat/bindings/commoncrypto/common_cryptor.py b/cryptography/hazmat/bindings/commoncrypto/common_cryptor.py index 69edf1e7..8783ece8 100644 --- a/cryptography/hazmat/bindings/commoncrypto/common_cryptor.py +++ b/cryptography/hazmat/bindings/commoncrypto/common_cryptor.py @@ -27,13 +27,13 @@ enum { }; typedef uint32_t CCAlgorithm; enum { - kCCSuccess = 0, - kCCParamError = -4300, - kCCBufferTooSmall = -4301, - kCCMemoryFailure = -4302, - kCCAlignmentError = -4303, - kCCDecodeError = -4304, - kCCUnimplemented = -4305 + kCCSuccess = 0, + kCCParamError = -4300, + kCCBufferTooSmall = -4301, + kCCMemoryFailure = -4302, + kCCAlignmentError = -4303, + kCCDecodeError = -4304, + kCCUnimplemented = -4305 }; typedef int32_t CCCryptorStatus; typedef uint32_t CCOptions; @@ -52,74 +52,38 @@ enum { typedef uint32_t CCModeOptions; enum { - kCCModeECB = 1, - kCCModeCBC = 2, - kCCModeCFB = 3, - kCCModeCTR = 4, - kCCModeF8 = 5, // Unimplemented for now (not included) - kCCModeLRW = 6, // Unimplemented for now (not included) - kCCModeOFB = 7, - kCCModeXTS = 8, - kCCModeRC4 = 9, - kCCModeCFB8 = 10, + kCCModeECB = 1, + kCCModeCBC = 2, + kCCModeCFB = 3, + kCCModeCTR = 4, + kCCModeF8 = 5, + kCCModeLRW = 6, + kCCModeOFB = 7, + kCCModeXTS = 8, + kCCModeRC4 = 9, + kCCModeCFB8 = 10, }; typedef uint32_t CCMode; enum { - ccNoPadding = 0, - ccPKCS7Padding = 1, + ccNoPadding = 0, + ccPKCS7Padding = 1, }; typedef uint32_t CCPadding; """ FUNCTIONS = """ -CCCryptorStatus CCCryptorCreateWithMode( - CCOperation, /* kCCEncrypt, kCCEncrypt */ - CCMode, - CCAlgorithm, - CCPadding, - const void *, /* optional initialization vector */ - const void *, /* raw key material */ - size_t, - const void *, /* raw tweak material */ - size_t, - int, /* number of rounds. 0 == default */ - CCModeOptions, - CCCryptorRef *); /* RETURNED */ +CCCryptorStatus CCCryptorCreateWithMode(CCOperation, CCMode, CCAlgorithm, + CCPadding, const void *, const void *, + size_t, const void *, size_t, int, + CCModeOptions, CCCryptorRef *); -CCCryptorStatus CCCryptorCreate( - CCOperation, /* kCCEncrypt, etc. */ - CCAlgorithm, /* kCCAlgorithmDES, etc. */ - CCOptions, /* kCCOptionPKCS7Padding, etc. */ - const void *, /* raw key material */ - size_t, - const void *, /* optional initialization vector */ - CCCryptorRef *); /* RETURNED */ -CCCryptorStatus CCCryptorUpdate( - CCCryptorRef, - const void *, - size_t, - void *, /* data RETURNED here */ - size_t, - size_t *); /* number of bytes written */ -CCCryptorStatus CCCryptorFinal( - CCCryptorRef, - void *, - size_t, - size_t *); /* number of bytes written */ +CCCryptorStatus CCCryptorCreate(CCOperation, CCAlgorithm, CCOptions, + const void *, size_t, const void *, + CCCryptorRef *); +CCCryptorStatus CCCryptorUpdate(CCCryptorRef, const void *, size_t, void *, + size_t, size_t *); +CCCryptorStatus CCCryptorFinal(CCCryptorRef, void *, size_t, size_t *); CCCryptorStatus CCCryptorRelease(CCCryptorRef); - -/* GCM functions, 10.8+ iOS 5+ */ -enum { - kCCModeGCM = 11 -}; -CCCryptorStatus CCCryptorGCMAddIV(CCCryptorRef, const void *, size_t); -CCCryptorStatus CCCryptorGCMAddAAD(CCCryptorRef, const void *, size_t); -CCCryptorStatus CCCryptorGCMEncrypt(CCCryptorRef, const void *, size_t, - void *); -CCCryptorStatus CCCryptorGCMDecrypt(CCCryptorRef, const void *, size_t, - void *); -CCCryptorStatus CCCryptorGCMFinal(CCCryptorRef, const void *, size_t *); -CCCryptorStatus CCCryptorGCMReset(CCCryptorRef); """ MACROS = """ -- cgit v1.2.3 From 2dbb63be9d0bae50c3d657bdae2ec8ad0c666dc5 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sun, 19 Jan 2014 18:17:59 -0600 Subject: remove an extraneous linefeed --- cryptography/hazmat/bindings/commoncrypto/common_cryptor.py | 1 - 1 file changed, 1 deletion(-) diff --git a/cryptography/hazmat/bindings/commoncrypto/common_cryptor.py b/cryptography/hazmat/bindings/commoncrypto/common_cryptor.py index 8783ece8..ef0e7e10 100644 --- a/cryptography/hazmat/bindings/commoncrypto/common_cryptor.py +++ b/cryptography/hazmat/bindings/commoncrypto/common_cryptor.py @@ -76,7 +76,6 @@ CCCryptorStatus CCCryptorCreateWithMode(CCOperation, CCMode, CCAlgorithm, CCPadding, const void *, const void *, size_t, const void *, size_t, int, CCModeOptions, CCCryptorRef *); - CCCryptorStatus CCCryptorCreate(CCOperation, CCAlgorithm, CCOptions, const void *, size_t, const void *, CCCryptorRef *); -- cgit v1.2.3