conf: allow to reject incoming AV conference call
[siplcs.git] / src / core / sipe-crypt.c
blob929c23aa6806f194e415736101659bf26d60a7d5
1 /**
2 * @file sipe-crypt.c
4 * pidgin-sipe
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
23 /**
24 * Cypher routines implementation based on NSS.
25 * Includes: RC4, DES
28 #include "glib.h"
30 #include "nss.h"
31 #include "pk11pub.h"
33 #include "sipe-crypt.h"
36 /* PRIVATE methons */
38 static PK11Context*
39 sipe_crypt_ctx_create(CK_MECHANISM_TYPE cipherMech, const guchar *key, gsize key_length)
41 PK11SlotInfo* slot;
42 SECItem keyItem;
43 SECItem ivItem;
44 PK11SymKey* SymKey;
45 SECItem *SecParam;
46 PK11Context* EncContext;
48 /* For key */
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;
59 ivItem.data = NULL;
60 ivItem.len = 0;
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);
67 PK11_FreeSlot(slot);
69 return EncContext;
72 static void
73 sipe_crypt_ctx_encrypt(PK11Context* EncContext, const guchar *in, gsize length, guchar *out)
75 int tmp1_outlen;
77 PK11_CipherOp(EncContext, out, &tmp1_outlen, length, (unsigned char *)in, length);
80 static void
81 sipe_crypt_ctx_destroy(PK11Context* EncContext)
83 PK11_DestroyContext(EncContext, PR_TRUE);
86 static void
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)
92 void *EncContext;
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);
100 /* PUBLIC methons */
102 void
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);
110 void
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 */
119 gpointer
120 sipe_crypt_ft_start(const guchar *key)
122 return sipe_crypt_ctx_create(CKM_RC4, key, 16);
125 void
126 sipe_crypt_ft_stream(gpointer context,
127 const guchar *in, gsize length,
128 guchar *out)
130 sipe_crypt_ctx_encrypt(context, in, length, out);
133 void
134 sipe_crypt_ft_destroy(gpointer context)
136 sipe_crypt_ctx_destroy(context);
140 Local Variables:
141 mode: c
142 c-file-style: "bsd"
143 indent-tabs-mode: t
144 tab-width: 8
145 End: