aboutsummaryrefslogtreecommitdiff
path: root/src/util/crypto_hkdf.c
diff options
context:
space:
mode:
authorNils Durner <durner@gnunet.org>2010-09-26 14:11:23 +0000
committerNils Durner <durner@gnunet.org>2010-09-26 14:11:23 +0000
commitaa51482a950a56419580587df54c17603d0390f1 (patch)
tree437974f0cc39a7919dd84b3081cdb8c70f6d0ebe /src/util/crypto_hkdf.c
parentda6e4e260bdf7eb4440a9ba53bcc7e497d66181f (diff)
downloadgnunet-aa51482a950a56419580587df54c17603d0390f1.tar.gz
gnunet-aa51482a950a56419580587df54c17603d0390f1.zip
support chunked context parameter for HKDF
Diffstat (limited to 'src/util/crypto_hkdf.c')
-rw-r--r--src/util/crypto_hkdf.c45
1 files changed, 36 insertions, 9 deletions
diff --git a/src/util/crypto_hkdf.c b/src/util/crypto_hkdf.c
index e442aef1c..4415b7551 100644
--- a/src/util/crypto_hkdf.c
+++ b/src/util/crypto_hkdf.c
@@ -93,6 +93,8 @@ static void dump(char *src, void *p, unsigned int l)
93 93
94/** 94/**
95 * @brief Derive key 95 * @brief Derive key
96 * @param result buffer for the derived key, allocated by caller
97 * @param out_len desired length of the derived key
96 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_... 98 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
97 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_... 99 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
98 * @param xts salt 100 * @param xts salt
@@ -101,15 +103,12 @@ static void dump(char *src, void *p, unsigned int l)
101 * @param skm_len length of skm 103 * @param skm_len length of skm
102 * @param ctx context info 104 * @param ctx context info
103 * @param ctx_len length of ctx 105 * @param ctx_len length of ctx
104 * @param out_len desired length of the derived key
105 * @param result buffer for the derived key, allocated by caller
106 * @return GNUNET_YES on success 106 * @return GNUNET_YES on success
107 */ 107 */
108int 108int
109GNUNET_CRYPTO_hkdf (int xtr_algo, int prf_algo, const void *xts, 109GNUNET_CRYPTO_hkdf (void *result, const unsigned long long out_len,
110 const size_t xts_len, const void *skm, const size_t skm_len, 110 int xtr_algo, int prf_algo, const void *xts, const size_t xts_len,
111 const void *ctx, const size_t ctx_len, const unsigned long long out_len, 111 const void *skm, const size_t skm_len, ...)
112 void *result)
113{ 112{
114 void *prk, *hc, *plain; 113 void *prk, *hc, *plain;
115 unsigned long long plain_len; 114 unsigned long long plain_len;
@@ -117,6 +116,8 @@ GNUNET_CRYPTO_hkdf (int xtr_algo, int prf_algo, const void *xts,
117 unsigned int k, xtr_len; 116 unsigned int k, xtr_len;
118 int ret; 117 int ret;
119 gcry_md_hd_t xtr, prf; 118 gcry_md_hd_t xtr, prf;
119 size_t ctx_len;
120 va_list argp;
120 121
121 prk = plain = NULL; 122 prk = plain = NULL;
122 xtr_len = gcry_md_get_algo_dlen (xtr_algo); 123 xtr_len = gcry_md_get_algo_dlen (xtr_algo);
@@ -127,6 +128,11 @@ GNUNET_CRYPTO_hkdf (int xtr_algo, int prf_algo, const void *xts,
127 if (out_len > (2 ^ 32 * k) || !xtr_algo || !prf_algo) 128 if (out_len > (2 ^ 32 * k) || !xtr_algo || !prf_algo)
128 return GNUNET_SYSERR; 129 return GNUNET_SYSERR;
129 130
131 va_start(argp, skm_len);
132 for (ctx_len = 0; va_arg (argp, void *);)
133 ctx_len += va_arg (argp, size_t);
134 va_end(argp);
135
130 prk = GNUNET_malloc (xtr_len); 136 prk = GNUNET_malloc (xtr_len);
131 137
132 memset (result, 0, out_len); 138 memset (result, 0, out_len);
@@ -146,8 +152,21 @@ GNUNET_CRYPTO_hkdf (int xtr_algo, int prf_algo, const void *xts,
146 plain = GNUNET_malloc (plain_len); 152 plain = GNUNET_malloc (plain_len);
147 if (t > 0) 153 if (t > 0)
148 { 154 {
149 memcpy (plain, ctx, ctx_len); 155 void *ctx, *dst;
150 memset (plain + ctx_len, 1, 1); 156
157 dst = plain;
158 va_start (argp, skm_len);
159 while ((ctx = va_arg (argp, void *)))
160 {
161 size_t len;
162
163 len = va_arg (argp, size_t);
164 memcpy (dst, ctx, len);
165 dst += len;
166 }
167 va_end (argp);
168
169 memset (dst, 1, 1);
151 gcry_md_reset (prf); 170 gcry_md_reset (prf);
152#if DEBUG_HKDF 171#if DEBUG_HKDF
153 dump("K(1)", plain, plain_len); 172 dump("K(1)", plain, plain_len);
@@ -160,7 +179,15 @@ GNUNET_CRYPTO_hkdf (int xtr_algo, int prf_algo, const void *xts,
160 } 179 }
161 180
162 if (t > 1 || d > 0) 181 if (t > 1 || d > 0)
163 memcpy (plain + k, ctx, ctx_len); 182 {
183 void *ctx, *dst;
184
185 dst = plain + k;
186 va_start(argp, skm_len);
187 while ((ctx = va_arg (argp, void *)))
188 memcpy (dst, ctx, va_arg (argp, size_t));
189 va_end (argp);
190 }
164 191
165 /* K(i+1) */ 192 /* K(i+1) */
166 for (i = 1; i < t; i++) 193 for (i = 1; i < t; i++)