simplified calculations
[gnutls.git] / lib / opencdk / seskey.c
blob8d1d929211f04383cadb39b8b29996bbbaf5f676
1 /* seskey.c - Session key routines
2 * Copyright (C) 1998-2012 Free Software Foundation, Inc.
4 * Author: Timo Schulz
6 * This file is part of OpenCDK.
8 * The OpenCDK library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 3 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 #include <stdio.h>
28 #include "opencdk.h"
29 #include "main.h"
30 #include "packet.h"
32 /**
33 * cdk_s2k_new:
34 * @ret_s2k: output for the new S2K object
35 * @mode: the S2K mode (simple, salted, iter+salted)
36 * @digest_algo: the hash algorithm
37 * @salt: random salt
39 * Create a new S2K object with the given parameter.
40 * The @salt parameter must be always 8 octets.
41 **/
42 cdk_error_t
43 cdk_s2k_new (cdk_s2k_t * ret_s2k, int mode, int digest_algo,
44 const byte * salt)
46 cdk_s2k_t s2k;
48 if (!ret_s2k)
49 return CDK_Inv_Value;
51 if (mode != 0x00 && mode != 0x01 && mode != 0x03)
52 return CDK_Inv_Mode;
54 if (_gnutls_hash_get_algo_len (digest_algo) <= 0)
55 return CDK_Inv_Algo;
57 s2k = cdk_calloc (1, sizeof *s2k);
58 if (!s2k)
59 return CDK_Out_Of_Core;
60 s2k->mode = mode;
61 s2k->hash_algo = digest_algo;
62 if (salt)
63 memcpy (s2k->salt, salt, 8);
64 *ret_s2k = s2k;
65 return 0;
69 /**
70 * cdk_s2k_free:
71 * @s2k: the S2K object
73 * Release the given S2K object.
74 **/
75 void
76 cdk_s2k_free (cdk_s2k_t s2k)
78 cdk_free (s2k);
82 /* Make a copy of the source s2k into R_DST. */
83 cdk_error_t
84 _cdk_s2k_copy (cdk_s2k_t * r_dst, cdk_s2k_t src)
86 cdk_s2k_t dst;
87 cdk_error_t err;
89 err = cdk_s2k_new (&dst, src->mode, src->hash_algo, src->salt);
90 if (err)
91 return err;
92 dst->count = src->count;
93 *r_dst = dst;
95 return 0;