2 * Copyright (c) 2010-2011 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 @header EncryptTransformUtilities
26 Provides utilies for the SecEncryptTransform file.
30 #include "EncryptTransformUtilities.h"
31 #include "SecEncryptTransform.h"
33 /* --------------------------------------------------------------------------
34 method: ConvertPaddingStringToEnum
35 description: Get the CSSM_PADDING value from a CFString
36 -------------------------------------------------------------------------- */
37 uint32
ConvertPaddingStringToEnum(CFStringRef paddingStr
)
39 uint32 result
= -1; // Guilty until proven
40 if (NULL
== paddingStr
)
45 if (CFEqual(paddingStr
, kSecPaddingNoneKey
))
47 result
= CSSM_PADDING_NONE
;
49 else if (CFEqual(paddingStr
, kSecPaddingPKCS1Key
))
51 result
= CSSM_PADDING_PKCS7
; //CSSM_PADDING_PKCS1 ois not supported
53 else if (CFEqual(paddingStr
, kSecPaddingPKCS5Key
))
55 result
= CSSM_PADDING_PKCS5
;
57 else if (CFEqual(paddingStr
, kSecPaddingPKCS7Key
))
59 result
= CSSM_PADDING_PKCS7
;
65 /* --------------------------------------------------------------------------
66 method: ConvertPaddingEnumToString
67 description: Get the corresponding CFStringRef for a CSSM_PADDING value
68 -------------------------------------------------------------------------- */
69 CFStringRef
ConvertPaddingEnumToString(CSSM_PADDING paddingEnum
)
71 CFStringRef result
= NULL
;
74 case CSSM_PADDING_NONE
:
75 result
= kSecPaddingNoneKey
;
78 case CSSM_PADDING_PKCS5
:
79 result
= kSecPaddingPKCS5Key
;
82 case CSSM_PADDING_PKCS7
:
83 result
= kSecPaddingPKCS7Key
;
86 case CSSM_PADDING_PKCS1
:
87 result
= kSecPaddingPKCS1Key
;
100 /* --------------------------------------------------------------------------
101 method: ConvertEncryptModeStringToEnum
102 description: Given a string that represents an encryption mode return the
103 CSSM_ENCRYPT_MODE value
104 -------------------------------------------------------------------------- */
105 uint32
ConvertEncryptModeStringToEnum(CFStringRef modeStr
, Boolean hasPadding
)
107 uint32 result
= -1; // Guilty until proven
114 if (CFEqual(modeStr
, kSecModeNoneKey
))
116 result
= (hasPadding
) ? CSSM_ALGMODE_ECBPad
: CSSM_ALGMODE_ECB
;
118 else if (CFEqual(modeStr
, kSecModeECBKey
))
120 result
= (hasPadding
) ? CSSM_ALGMODE_ECBPad
: CSSM_ALGMODE_ECB
;
122 else if (CFEqual(modeStr
, kSecModeCBCKey
))
124 result
= (hasPadding
) ? CSSM_ALGMODE_CBCPadIV8
: CSSM_ALGMODE_CBC
;
126 else if (CFEqual(modeStr
, kSecModeCFBKey
))
128 result
= (hasPadding
) ? CSSM_ALGMODE_CFBPadIV8
: CSSM_ALGMODE_CFB
;
130 else if (CFEqual(modeStr
, kSecModeOFBKey
))
132 result
= (hasPadding
) ? CSSM_ALGMODE_OFBPadIV8
: CSSM_ALGMODE_OFB
;
138 /* --------------------------------------------------------------------------
139 method: ConvertEncryptModeEnumToString
140 description: Given a CSSM_ENCRYPT_MODE value return the corresponding
141 CFString representation.
142 -------------------------------------------------------------------------- */
143 CFStringRef
ConvertEncryptModeEnumToString(CSSM_ENCRYPT_MODE paddingEnum
)
145 CFStringRef result
= NULL
;
149 case CSSM_ALGMODE_NONE
:
151 result
= kSecModeNoneKey
;
154 case CSSM_ALGMODE_ECB
:
155 case CSSM_ALGMODE_ECBPad
:
156 case CSSM_ALGMODE_ECB64
:
157 case CSSM_ALGMODE_ECB128
:
158 case CSSM_ALGMODE_ECB96
:
159 result
= kSecModeECBKey
;
162 case CSSM_ALGMODE_CBC
:
163 case CSSM_ALGMODE_CBC_IV8
:
164 case CSSM_ALGMODE_CBCPadIV8
:
165 case CSSM_ALGMODE_CBC64
:
166 case CSSM_ALGMODE_CBC128
:
167 result
= kSecModeCBCKey
;
170 case CSSM_ALGMODE_CFB
:
171 case CSSM_ALGMODE_CFB_IV8
:
172 case CSSM_ALGMODE_CFBPadIV8
:
173 case CSSM_ALGMODE_CFB32
:
174 case CSSM_ALGMODE_CFB16
:
175 case CSSM_ALGMODE_CFB8
:
176 result
= kSecModeCFBKey
;
179 case CSSM_ALGMODE_OFB
:
180 case CSSM_ALGMODE_OFB_IV8
:
181 case CSSM_ALGMODE_OFBPadIV8
:
182 case CSSM_ALGMODE_OFB64
:
183 result
= kSecModeOFBKey
;