EncryptTransformUtilities.cpp
1 /* 2 * Copyright (c) 2010-2011 Apple Inc. All Rights Reserved. 3 * 4 * @APPLE_LICENSE_HEADER_START@ 5 * 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 11 * file. 12 * 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. 20 * 21 * @APPLE_LICENSE_HEADER_END@ 22 */ 23 24 /*! 25 @header EncryptTransformUtilities 26 Provides utilies for the SecEncryptTransform file. 27 28 */ 29 30 #include "EncryptTransformUtilities.h" 31 #include "SecEncryptTransform.h" 32 33 /* -------------------------------------------------------------------------- 34 method: ConvertPaddingStringToEnum 35 description: Get the CSSM_PADDING value from a CFString 36 -------------------------------------------------------------------------- */ 37 uint32 ConvertPaddingStringToEnum(CFStringRef paddingStr) 38 { 39 uint32 result = -1; // Guilty until proven 40 if (NULL == paddingStr) 41 { 42 return result; 43 } 44 45 if (CFEqual(paddingStr, kSecPaddingNoneKey)) 46 { 47 result = CSSM_PADDING_NONE; 48 } 49 else if (CFEqual(paddingStr, kSecPaddingPKCS1Key)) 50 { 51 result = CSSM_PADDING_PKCS7; //CSSM_PADDING_PKCS1 ois not supported 52 } 53 else if (CFEqual(paddingStr, kSecPaddingPKCS5Key)) 54 { 55 result = CSSM_PADDING_PKCS5; 56 } 57 else if (CFEqual(paddingStr, kSecPaddingPKCS7Key)) 58 { 59 result = CSSM_PADDING_PKCS7; 60 } 61 return result; 62 } 63 64 65 /* -------------------------------------------------------------------------- 66 method: ConvertPaddingEnumToString 67 description: Get the corresponding CFStringRef for a CSSM_PADDING value 68 -------------------------------------------------------------------------- */ 69 CFStringRef ConvertPaddingEnumToString(CSSM_PADDING paddingEnum) 70 { 71 CFStringRef result = NULL; 72 switch (paddingEnum) 73 { 74 case CSSM_PADDING_NONE: 75 result = kSecPaddingNoneKey; 76 break; 77 78 case CSSM_PADDING_PKCS5: 79 result = kSecPaddingPKCS5Key; 80 break; 81 82 case CSSM_PADDING_PKCS7: 83 result = kSecPaddingPKCS7Key; 84 break; 85 86 case CSSM_PADDING_PKCS1: 87 result = kSecPaddingPKCS1Key; 88 break; 89 90 default: 91 result = NULL; 92 break; 93 } 94 95 return result; 96 } 97 98 99 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) 106 { 107 uint32 result = -1; // Guilty until proven 108 109 if (NULL == modeStr) 110 { 111 return result; 112 } 113 114 if (CFEqual(modeStr, kSecModeNoneKey)) 115 { 116 result = (hasPadding) ? CSSM_ALGMODE_ECBPad : CSSM_ALGMODE_ECB; 117 } 118 else if (CFEqual(modeStr, kSecModeECBKey)) 119 { 120 result = (hasPadding) ? CSSM_ALGMODE_ECBPad : CSSM_ALGMODE_ECB; 121 } 122 else if (CFEqual(modeStr, kSecModeCBCKey)) 123 { 124 result = (hasPadding) ? CSSM_ALGMODE_CBCPadIV8 : CSSM_ALGMODE_CBC; 125 } 126 else if (CFEqual(modeStr, kSecModeCFBKey)) 127 { 128 result = (hasPadding) ? CSSM_ALGMODE_CFBPadIV8 : CSSM_ALGMODE_CFB; 129 } 130 else if (CFEqual(modeStr, kSecModeOFBKey)) 131 { 132 result = (hasPadding) ? CSSM_ALGMODE_OFBPadIV8 : CSSM_ALGMODE_OFB; 133 } 134 135 return result; 136 } 137 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) 144 { 145 CFStringRef result = NULL; 146 147 switch (paddingEnum) 148 { 149 case CSSM_ALGMODE_NONE: 150 default: 151 result = kSecModeNoneKey; 152 break; 153 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; 160 break; 161 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; 168 break; 169 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; 177 break; 178 179 case CSSM_ALGMODE_OFB: 180 case CSSM_ALGMODE_OFB_IV8: 181 case CSSM_ALGMODE_OFBPadIV8: 182 case CSSM_ALGMODE_OFB64: 183 result = kSecModeOFBKey; 184 break; 185 } 186 187 return result; 188 } 189