]>
git.saurik.com Git - apple/security.git/blob - SecurityTests/cspxutils/utilLib/ssleayUtils.cpp
2 * ssleayUtils.c - common routines for CDSA/openssl compatibility testing
8 #include <openssl/blowfish.h>
9 #include <openssl/cast.h>
10 #include <openssl/evp.h>
11 #include "ssleayUtils.h"
12 #include <Security/cssmerr.h>
16 * Caller sees EAY_KEY, we see a pointer to this.
21 BF_KEY bf
; // blowfish
27 * Create a symmetric key.
29 CSSM_RETURN
eayGenSymKey(
32 const CSSM_DATA
*keyData
,
33 EAY_KEY
*key
) // RETURNED
35 EayKeyPriv
*ekp
= (EayKeyPriv
*)malloc(sizeof(EayKeyPriv
));
36 memset(ekp
, 0, sizeof(*ekp
));
38 case CSSM_ALGID_BLOWFISH
:
39 BF_set_key(&ekp
->key
.bf
, keyData
->Length
, keyData
->Data
);
41 case CSSM_ALGID_CAST
: // cast128 only
42 CAST_set_key(&ekp
->key
.cast
, keyData
->Length
, keyData
->Data
);
45 printf("***eayGenSymKey: bad alg\n");
54 * Free a key created in eayGenSymKey
56 CSSM_RETURN
eayFreeKey(
59 memset(key
, 0, sizeof(EayKeyPriv
));
67 CSSM_RETURN
eayEncryptDecrypt(
70 CSSM_ALGORITHMS encrAlg
,
71 CSSM_ENCRYPT_MODE mode
, // CSSM_ALGMODE_CBC ONLY!
72 const CSSM_DATA
*iv
, //ĂŠoptional per mode
73 const CSSM_DATA
*inData
,
74 CSSM_DATA_PTR outData
) // CSSM_MALLOCd and RETURNED
76 EayKeyPriv
*ekp
= (EayKeyPriv
*)key
;
77 if((mode
!= CSSM_ALGMODE_CBC_IV8
) && (mode
!= CSSM_ALGMODE_ECB
)) {
78 printf("***eayEncryptDecrypt only does CBC_IV8, ECB\n");
82 bool cbc
= (mode
== CSSM_ALGMODE_ECB
) ? false : true;
84 outData
->Data
= (uint8
*)CSSM_MALLOC(inData
->Length
);
85 outData
->Length
= inData
->Length
;
87 /* BF_cbc_encrypt actually writes to IV */
88 CSSM_DATA ivc
= {0, NULL
};
90 ivc
.Data
= (uint8
*)malloc(iv
->Length
);
91 ivc
.Length
= iv
->Length
;
92 memmove(ivc
.Data
, iv
->Data
, ivc
.Length
);
95 case CSSM_ALGID_BLOWFISH
:
97 BF_cbc_encrypt(inData
->Data
,
102 forEncrypt
? BF_ENCRYPT
: BF_DECRYPT
);
105 CSSM_DATA intext
= *inData
;
106 CSSM_DATA outtext
= *outData
;
107 while(intext
.Length
) {
108 BF_ecb_encrypt(intext
.Data
,
111 forEncrypt
? BF_ENCRYPT
: BF_DECRYPT
);
118 case CSSM_ALGID_CAST
: // cast128 only
119 CAST_cbc_encrypt(inData
->Data
,
124 forEncrypt
? CAST_ENCRYPT
: CAST_DECRYPT
);
127 printf("***eayEncryptDecrypt: bad alg\n");
136 /*** EVP-based encrypt/decrypt ***/
138 int evpEncryptDecrypt(
139 CSSM_ALGORITHMS alg
, // AES 128 only for now
141 const CSSM_DATA
*keyData
, // may be larger than the key size we use
142 unsigned keyLengthInBits
,
143 CSSM_ENCRYPT_MODE mode
, // CSSM_ALGMODE_CBC_IV8, ECB, always padding
144 const CSSM_DATA
*iv
, // optional per mode
145 const CSSM_DATA
*inData
,
146 CSSM_DATA_PTR outData
) // CSSM_MALLOCd and RETURNED
149 const EVP_CIPHER
*cipher
;
151 unsigned outLen
= inData
->Length
;
157 case CSSM_ALGMODE_CBCPadIV8
:
158 switch(keyLengthInBits
) {
160 cipher
= EVP_aes_128_cbc();
163 cipher
= EVP_aes_192_cbc();
166 cipher
= EVP_aes_256_cbc();
169 printf("***Bad AES key length (%u)\n", keyLengthInBits
);
173 case CSSM_ALGMODE_ECB
:
174 switch(keyLengthInBits
) {
176 cipher
= EVP_aes_128_ecb();
179 cipher
= EVP_aes_192_ecb();
182 cipher
= EVP_aes_256_ecb();
185 printf("***Bad AES key length (%u)\n", keyLengthInBits
);
191 printf("***evpEncryptDecrypt only does CBC and ECB for now\n");
198 case CSSM_ALGMODE_CBCPadIV8
:
199 cipher
= EVP_des_cbc();
201 case CSSM_ALGMODE_ECB
:
202 cipher
= EVP_des_ecb();
206 printf("***evpEncryptDecrypt only does CBC and ECB for now\n");
212 printf("***evpEncryptDecrypt only does DES and AES 128 for now\n");
216 unsigned char *outp
= (uint8
*)CSSM_MALLOC(outLen
);
218 outData
->Data
= outp
;
221 int rtn
= EVP_EncryptInit(&ctx
, cipher
, keyData
->Data
, iv
? iv
->Data
: NULL
);
223 printf("EVP_EncryptInit error\n");
227 EVP_CIPHER_CTX_set_padding(&ctx
, 0);
229 if(!EVP_EncryptUpdate(&ctx
, outp
, &outl
, inData
->Data
, inData
->Length
)) {
230 printf("EVP_EncryptUpdate error\n");
235 int rtn
= EVP_DecryptInit(&ctx
, cipher
, keyData
->Data
, iv
? iv
->Data
: NULL
);
237 printf("EVP_DecryptInit error\n");
241 EVP_CIPHER_CTX_set_padding(&ctx
, 0);
244 if(!EVP_DecryptUpdate(&ctx
, outp
, &outl
, inData
->Data
, inData
->Length
)) {
245 printf("EVP_DecryptUpdate error\n");
249 outData
->Length
= outl
;
251 outl
= outLen
- outl
;
253 if(!EVP_EncryptFinal(&ctx
, outp
, &outl
)) {
254 printf("EVP_EncryptFinal error\n");
259 if(!EVP_DecryptFinal(&ctx
, outp
, &outl
)) {
260 printf("EVP_DecryptFinal error\n");
264 outData
->Length
+= outl
;