2 * encypt/decrypt using wrapped key
9 #include <Security/cssm.h>
12 #include <security_cdsa_utils/cuFileIo.h>
15 static void usage(char **argv
)
18 printf(" %s w keyFile passPhrase1 passPhrase2 textToEncrypt textFile\n", argv
[0]);
19 printf(" %s u keyFile passPhrase1 textFile\n", argv
[0]);
23 char *iv
= (char *)"someInit";
24 char *salt
= (char *)"some20bytesOfGrainySalt";
26 char *wrapLabel
= (char *)"wrapLabel";
27 char *encrLabel
= (char *)"encrLabel";
31 #define ITER_COUNT 1000
32 #define WRAPPING_KEY_ALG CSSM_ALGID_3DES_3KEY
33 #define WRAPPING_ALG CSSM_ALGID_3DES_3KEY_EDE
34 #define WRAPPING_KEY_SIZE 192
35 #define ENCRYPTING_KEY_ALG CSSM_ALGID_3DES_3KEY
36 #define ENCRYPTING_ALG CSSM_ALGID_3DES_3KEY_EDE
37 #define ENCRYPTING_KEY_SIZE 192
39 int main(int argc
, char **argv
)
43 CSSM_CSP_HANDLE cspHand
;
44 CSSM_KEY_PTR wrappingKey
;
45 CSSM_DATA saltData
= {SALT_LEN
, (uint8
*)salt
};
46 CSSM_DATA ivData
= {IV_LEN
, (uint8
*)iv
};
48 unsigned char *keyFileData
;
50 unsigned char *textFileData
;
76 cspHand
= cspDlDbStartup(CSSM_TRUE
, NULL
);
81 /* passphrase1 ==> wrappingKey */
82 phraseData
.Data
= (uint8
*)argv
[3];
83 phraseData
.Length
= strlen(argv
[3]);
84 wrappingKey
= cspDeriveKey(cspHand
,
85 CSSM_ALGID_PKCS5_PBKDF2
,
96 if(wrappingKey
== NULL
) {
97 printf("Error creating key from \'%s\'\n", argv
[3]);
102 /* passphrase2 ==> encrKey */
103 CSSM_KEY_PTR encrKey
;
104 phraseData
.Data
= (uint8
*)argv
[4];
105 phraseData
.Length
= strlen(argv
[4]);
106 encrKey
= cspDeriveKey(cspHand
,
107 CSSM_ALGID_PKCS5_PBKDF2
,
113 CSSM_TRUE
, // ref key
118 if(encrKey
== NULL
) {
119 printf("Error creating key from \'%s\'\n", argv
[4]);
123 /* encrypt textToEncrypt, write it to textFile */
124 ptext
.Data
= (uint8
*)argv
[5];
125 ptext
.Length
= strlen(argv
[5]);
128 crtn
= cspEncrypt(cspHand
,
129 CSSM_ALGID_3DES_3KEY_EDE
,
130 CSSM_ALGMODE_CBCPadIV8
,
141 printf("Error encrypting.\n");
144 if(writeFile(argv
[6], ctext
.Data
, ctext
.Length
)) {
145 printf("Error writing to %s\n", argv
[6]);
149 /* now wrap encrKey with wrappingKey and write the wrapped blob */
150 crtn
= cspWrapKey(cspHand
,
154 CSSM_ALGMODE_CBCPadIV8
,
155 CSSM_KEYBLOB_WRAPPED_FORMAT_NONE
,
163 if(writeFile(argv
[2], wrappedKey
.KeyData
.Data
, wrappedKey
.KeyData
.Length
)) {
164 printf("error writing to %s\n", argv
[2]);
167 printf("...wrote %lu bytes of encrypted text to %s\n",
168 ctext
.Length
, argv
[6]);
169 printf("...wrote %lu bytes of wrapped key data to %s\n",
170 wrappedKey
.KeyData
.Length
, argv
[2]);
173 /* read in encrypted text and wrapped key blob */
175 CSSM_DATA outDescData2
= {0, NULL
};
177 if(readFile(argv
[2], &keyFileData
, &keyFileLen
)) {
178 printf("Error reading %s\n", argv
[2]);
180 if(readFile(argv
[4], &textFileData
, &textFileLen
)) {
181 printf("Error reading %s\n", argv
[2]);
184 /* cook up a reasonable "wrapped key" */
185 memset(&wrappedKey
, 0, sizeof(CSSM_KEY
));
186 wrappedKey
.KeyHeader
.HeaderVersion
= CSSM_KEYHEADER_VERSION
;
187 wrappedKey
.KeyHeader
.BlobType
= CSSM_KEYBLOB_WRAPPED
;
188 wrappedKey
.KeyHeader
.Format
= CSSM_KEYBLOB_WRAPPED_FORMAT_APPLE_CUSTOM
;
189 wrappedKey
.KeyHeader
.AlgorithmId
= CSSM_ALGID_3DES_3KEY
;
190 wrappedKey
.KeyHeader
.KeyClass
= CSSM_KEYCLASS_SESSION_KEY
;
191 wrappedKey
.KeyHeader
.LogicalKeySizeInBits
= ENCRYPTING_KEY_SIZE
;
192 wrappedKey
.KeyHeader
.KeyAttr
= CSSM_KEYATTR_EXTRACTABLE
;
193 wrappedKey
.KeyHeader
.KeyUsage
= CSSM_KEYUSE_ANY
;
194 wrappedKey
.KeyHeader
.WrapAlgorithmId
= WRAPPING_ALG
;
195 wrappedKey
.KeyHeader
.WrapMode
= CSSM_ALGMODE_CBCPadIV8
;
196 wrappedKey
.KeyData
.Data
= keyFileData
;
197 wrappedKey
.KeyData
.Length
= keyFileLen
;
199 /* unwrap the key to get decrypting key */
200 crtn
= cspUnwrapKey(cspHand
,
204 CSSM_ALGMODE_CBCPadIV8
,
212 printf("Error on unwrap.\n");
216 /* decrypt the text file and print its result */
217 ctext
.Data
= textFileData
;
218 ctext
.Length
= textFileLen
;
221 crtn
= cspDecrypt(cspHand
,
222 CSSM_ALGID_3DES_3KEY_EDE
,
223 CSSM_ALGMODE_CBCPadIV8
,
234 printf("Error on decrypt.\n");
237 printf("...original text: ");
238 for(i
=0; i
<ptext
.Length
; i
++) {
239 if(isprint(ptext
.Data
[i
])) {
240 printf("%c", ptext
.Data
[i
]);
243 printf("-%02X-", ptext
.Data
[i
]);