6 * Copyright (C) 2010 pier11 <pier11@operamail.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * Cypher routines implementation based on NSS.
33 #include "sipe-crypt.h"
39 sipe_crypt_ctx_create(CK_MECHANISM_TYPE cipherMech
, const guchar
*key
, gsize key_length
)
46 PK11Context
* EncContext
;
49 slot
= PK11_GetBestSlot(cipherMech
, NULL
);
51 keyItem
.type
= siBuffer
;
52 keyItem
.data
= (unsigned char *)key
;
53 keyItem
.len
= key_length
;
55 SymKey
= PK11_ImportSymKey(slot
, cipherMech
, PK11_OriginUnwrap
, CKA_ENCRYPT
, &keyItem
, NULL
);
57 /* Parameter for crypto context */
58 ivItem
.type
= siBuffer
;
61 SecParam
= PK11_ParamFromIV(cipherMech
, &ivItem
);
63 EncContext
= PK11_CreateContextBySymKey(cipherMech
, CKA_ENCRYPT
, SymKey
, SecParam
);
65 PK11_FreeSymKey(SymKey
);
66 SECITEM_FreeItem(SecParam
, PR_TRUE
);
73 sipe_crypt_ctx_encrypt(PK11Context
* EncContext
, const guchar
*in
, gsize length
, guchar
*out
)
77 PK11_CipherOp(EncContext
, out
, &tmp1_outlen
, length
, (unsigned char *)in
, length
);
81 sipe_crypt_ctx_destroy(PK11Context
* EncContext
)
83 PK11_DestroyContext(EncContext
, PR_TRUE
);
87 sipe_crypt(CK_MECHANISM_TYPE cipherMech
,
88 const guchar
*key
, gsize key_length
,
89 const guchar
*plaintext
, gsize plaintext_length
,
90 guchar
*encrypted_text
)
94 EncContext
= sipe_crypt_ctx_create(cipherMech
, key
, key_length
);
95 sipe_crypt_ctx_encrypt(EncContext
, plaintext
, plaintext_length
, encrypted_text
);
96 sipe_crypt_ctx_destroy(EncContext
);
103 sipe_crypt_des(const guchar
*key
,
104 const guchar
*plaintext
, gsize plaintext_length
,
105 guchar
*encrypted_text
)
107 sipe_crypt(CKM_DES_ECB
, key
, 8, plaintext
, plaintext_length
, encrypted_text
);
111 sipe_crypt_rc4(const guchar
*key
, gsize key_length
,
112 const guchar
*plaintext
, gsize plaintext_length
,
113 guchar
*encrypted_text
)
115 sipe_crypt(CKM_RC4
, key
, key_length
, plaintext
, plaintext_length
, encrypted_text
);
118 /* Stream RC4 cipher for file transfer */
120 sipe_crypt_ft_start(const guchar
*key
)
122 return sipe_crypt_ctx_create(CKM_RC4
, key
, 16);
126 sipe_crypt_ft_stream(gpointer context
,
127 const guchar
*in
, gsize length
,
130 sipe_crypt_ctx_encrypt(context
, in
, length
, out
);
134 sipe_crypt_ft_destroy(gpointer context
)
136 sipe_crypt_ctx_destroy(context
);