Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / extlibs / tinydtls / crypto.c
1 /* dtls -- a very basic DTLS implementation
2  *
3  * Copyright (C) 2011--2012 Olaf Bergmann <bergmann@tzi.org>
4  * Copyright (C) 2013 Hauke Mehrtens <hauke@hauke-m.de>
5  *
6  *
7  * Modified source code for micro-ecc porting,
8  *
9  * Following functions are removed:
10  *   - dtls_ec_key_to_uint32
11  *   - dtls_ec_key_from_uint32
12  * Following functions are modified:
13  *   - dtls_ecdh_pre_master_secret
14  *   - dtls_ecdsa_generate_key
15  *   - dtls_ecdsa_create_sig_hash
16  *   - dtls_ecdsa_verify_sig_hash
17  *
18  * Permission is hereby granted, free of charge, to any person
19  * obtaining a copy of this software and associated documentation
20  * files (the "Software"), to deal in the Software without
21  * restriction, including without limitation the rights to use, copy,
22  * modify, merge, publish, distribute, sublicense, and/or sell copies
23  * of the Software, and to permit persons to whom the Software is
24  * furnished to do so, subject to the following conditions:
25  *
26  * The above copyright notice and this permission notice shall be
27  * included in all copies or substantial portions of the Software.
28  *
29  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
33  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
34  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
35  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36  * SOFTWARE.
37  */
38
39 #include <stdio.h>
40
41 #include "tinydtls.h"
42 #include "dtls_config.h"
43
44 #ifdef HAVE_ASSERT_H
45 #include <assert.h>
46 #else
47 #define assert(x)
48 #endif
49
50 #include "global.h"
51 #include "debug.h"
52 #include "numeric.h"
53 #include "dtls.h"
54 #include "crypto.h"
55 #include "ccm.h"
56 #include "ecc/ecc.h"
57 #include "aes/rijndael.h"
58 #include "sha2/sha2.h"
59 #include "prng.h"
60 #include "netq.h"
61
62 #ifndef WITH_CONTIKI
63 #include <pthread.h>
64 #endif
65
66 #define HMAC_UPDATE_SEED(Context,Seed,Length)           \
67   if (Seed) dtls_hmac_update(Context, (Seed), (Length))
68
69 static struct dtls_cipher_context_t cipher_context;
70 #ifndef WITH_CONTIKI
71 static pthread_mutex_t cipher_context_mutex = PTHREAD_MUTEX_INITIALIZER;
72 #endif
73
74 static struct dtls_cipher_context_t *dtls_cipher_context_get(void)
75 {
76 #ifndef WITH_CONTIKI
77   pthread_mutex_lock(&cipher_context_mutex);
78 #endif
79   return &cipher_context;
80 }
81
82 static void dtls_cipher_context_release(void)
83 {
84 #ifndef WITH_CONTIKI
85   pthread_mutex_unlock(&cipher_context_mutex);
86 #endif
87 }
88
89 #ifndef WITH_CONTIKI
90 void crypto_init()
91 {
92 }
93
94 static dtls_handshake_parameters_t *dtls_handshake_malloc() {
95   return malloc(sizeof(dtls_handshake_parameters_t));
96 }
97
98 static void dtls_handshake_dealloc(dtls_handshake_parameters_t *handshake) {
99   free(handshake);
100 }
101
102 static dtls_security_parameters_t *dtls_security_malloc() {
103   return malloc(sizeof(dtls_security_parameters_t));
104 }
105
106 static void dtls_security_dealloc(dtls_security_parameters_t *security) {
107   free(security);
108 }
109 #else /* WITH_CONTIKI */
110
111 #include "memb.h"
112 MEMB(handshake_storage, dtls_handshake_parameters_t, DTLS_HANDSHAKE_MAX);
113 MEMB(security_storage, dtls_security_parameters_t, DTLS_SECURITY_MAX);
114
115 void crypto_init() {
116   memb_init(&handshake_storage);
117   memb_init(&security_storage);
118 }
119
120 static dtls_handshake_parameters_t *dtls_handshake_malloc() {
121   return memb_alloc(&handshake_storage);
122 }
123
124 static void dtls_handshake_dealloc(dtls_handshake_parameters_t *handshake) {
125   memb_free(&handshake_storage, handshake);
126 }
127
128 static dtls_security_parameters_t *dtls_security_malloc() {
129   return memb_alloc(&security_storage);
130 }
131
132 static void dtls_security_dealloc(dtls_security_parameters_t *security) {
133   memb_free(&security_storage, security);
134 }
135 #endif /* WITH_CONTIKI */
136
137 dtls_handshake_parameters_t *dtls_handshake_new()
138 {
139   dtls_handshake_parameters_t *handshake;
140
141   handshake = dtls_handshake_malloc();
142   if (!handshake) {
143     dtls_crit("can not allocate a handshake struct\n");
144     return NULL;
145   }
146
147   memset(handshake, 0, sizeof(*handshake));
148
149   if (handshake) {
150     /* initialize the handshake hash wrt. the hard-coded DTLS version */
151     dtls_debug("DTLSv12: initialize HASH_SHA256\n");
152     /* TLS 1.2:  PRF(secret, label, seed) = P_<hash>(secret, label + seed) */
153     /* FIXME: we use the default SHA256 here, might need to support other
154               hash functions as well */
155     dtls_hash_init(&handshake->hs_state.hs_hash);
156   }
157   return handshake;
158 }
159
160 void dtls_handshake_free(dtls_handshake_parameters_t *handshake)
161 {
162   if (!handshake)
163     return;
164
165   netq_delete_all(handshake->reorder_queue);
166   dtls_handshake_dealloc(handshake);
167 }
168
169 dtls_security_parameters_t *dtls_security_new()
170 {
171   dtls_security_parameters_t *security;
172
173   security = dtls_security_malloc();
174   if (!security) {
175     dtls_crit("can not allocate a security struct\n");
176     return NULL;
177   }
178
179   memset(security, 0, sizeof(*security));
180
181   if (security) {
182     security->cipher = TLS_NULL_WITH_NULL_NULL;
183     security->compression = TLS_COMPRESSION_NULL;
184   }
185   return security;
186 }
187
188 void dtls_security_free(dtls_security_parameters_t *security)
189 {
190   if (!security)
191     return;
192
193   dtls_security_dealloc(security);
194 }
195
196 size_t
197 dtls_p_hash(dtls_hashfunc_t h,
198             const unsigned char *key, size_t keylen,
199             const unsigned char *label, size_t labellen,
200             const unsigned char *random1, size_t random1len,
201             const unsigned char *random2, size_t random2len,
202             unsigned char *buf, size_t buflen) {
203   dtls_hmac_context_t *hmac_a, *hmac_p;
204
205   unsigned char A[DTLS_HMAC_DIGEST_SIZE];
206   unsigned char tmp[DTLS_HMAC_DIGEST_SIZE];
207   size_t dlen;                  /* digest length */
208   size_t len = 0;                       /* result length */
209
210   hmac_a = dtls_hmac_new(key, keylen);
211   if (!hmac_a)
212     return 0;
213
214   /* calculate A(1) from A(0) == seed */
215   HMAC_UPDATE_SEED(hmac_a, label, labellen);
216   HMAC_UPDATE_SEED(hmac_a, random1, random1len);
217   HMAC_UPDATE_SEED(hmac_a, random2, random2len);
218
219   dlen = dtls_hmac_finalize(hmac_a, A);
220
221   hmac_p = dtls_hmac_new(key, keylen);
222   if (!hmac_p)
223     goto error;
224
225   while (len + dlen < buflen) {
226
227     /* FIXME: rewrite loop to avoid superflous call to dtls_hmac_init() */
228     dtls_hmac_init(hmac_p, key, keylen);
229     dtls_hmac_update(hmac_p, A, dlen);
230
231     HMAC_UPDATE_SEED(hmac_p, label, labellen);
232     HMAC_UPDATE_SEED(hmac_p, random1, random1len);
233     HMAC_UPDATE_SEED(hmac_p, random2, random2len);
234
235     len += dtls_hmac_finalize(hmac_p, tmp);
236     memcpy(buf, tmp, dlen);
237     buf += dlen;
238
239     /* calculate A(i+1) */
240     dtls_hmac_init(hmac_a, key, keylen);
241     dtls_hmac_update(hmac_a, A, dlen);
242     dtls_hmac_finalize(hmac_a, A);
243   }
244
245   dtls_hmac_init(hmac_p, key, keylen);
246   dtls_hmac_update(hmac_p, A, dlen);
247
248   HMAC_UPDATE_SEED(hmac_p, label, labellen);
249   HMAC_UPDATE_SEED(hmac_p, random1, random1len);
250   HMAC_UPDATE_SEED(hmac_p, random2, random2len);
251
252   dtls_hmac_finalize(hmac_p, tmp);
253   memcpy(buf, tmp, buflen - len);
254
255  error:
256   dtls_hmac_free(hmac_a);
257   dtls_hmac_free(hmac_p);
258
259   return buflen;
260 }
261
262 size_t
263 dtls_prf(const unsigned char *key, size_t keylen,
264          const unsigned char *label, size_t labellen,
265          const unsigned char *random1, size_t random1len,
266          const unsigned char *random2, size_t random2len,
267          unsigned char *buf, size_t buflen) {
268
269   /* Clear the result buffer */
270   memset(buf, 0, buflen);
271   return dtls_p_hash(HASH_SHA256,
272                      key, keylen,
273                      label, labellen,
274                      random1, random1len,
275                      random2, random2len,
276                      buf, buflen);
277 }
278
279 void
280 dtls_mac(dtls_hmac_context_t *hmac_ctx,
281          const unsigned char *record,
282          const unsigned char *packet, size_t length,
283          unsigned char *buf) {
284   uint16 L;
285   dtls_int_to_uint16(L, length);
286
287   assert(hmac_ctx);
288   dtls_hmac_update(hmac_ctx, record +3, sizeof(uint16) + sizeof(uint48));
289   dtls_hmac_update(hmac_ctx, record, sizeof(uint8) + sizeof(uint16));
290   dtls_hmac_update(hmac_ctx, L, sizeof(uint16));
291   dtls_hmac_update(hmac_ctx, packet, length);
292
293   dtls_hmac_finalize(hmac_ctx, buf);
294 }
295
296 static size_t
297 dtls_ccm_encrypt(aes128_t *ccm_ctx, const unsigned char *src, size_t srclen,
298                  unsigned char *buf,
299                  unsigned char *nounce,
300                  const unsigned char *aad, size_t la) {
301   long int len;
302
303   assert(ccm_ctx);
304
305   len = dtls_ccm_encrypt_message(&ccm_ctx->ctx, 8 /* M */,
306                                  max(2, 15 - DTLS_CCM_NONCE_SIZE),
307                                  nounce,
308                                  buf, srclen,
309                                  aad, la);
310   return len;
311 }
312
313 static size_t
314 dtls_ccm_decrypt(aes128_t *ccm_ctx, const unsigned char *src,
315                  size_t srclen, unsigned char *buf,
316                  unsigned char *nounce,
317                  const unsigned char *aad, size_t la) {
318   long int len;
319
320   assert(ccm_ctx);
321
322   len = dtls_ccm_decrypt_message(&ccm_ctx->ctx, 8 /* M */,
323                                  max(2, 15 - DTLS_CCM_NONCE_SIZE),
324                                  nounce,
325                                  buf, srclen,
326                                  aad, la);
327   return len;
328 }
329
330 static size_t
331 dtls_cbc_encrypt(aes128_t *aes_ctx,
332                  const unsigned char *iv,
333                  const unsigned char *src, size_t srclen,
334                  unsigned char *buf) {
335
336     unsigned char cbc[DTLS_BLK_LENGTH];
337     unsigned char tmp[DTLS_BLK_LENGTH];
338     unsigned char *pos;
339     dtls_hash_ctx shactx;
340     int i, j;
341     int blocks;
342
343     pos = buf;
344
345     dtls_hash_init(&shactx);
346     dtls_hash_update(&shactx, src, srclen);
347     dtls_hash_finalize(pos + srclen, &shactx);
348
349     memcpy(cbc, iv, DTLS_BLK_LENGTH);
350     blocks = (srclen + SHA256_DIGEST_LENGTH) / DTLS_BLK_LENGTH;
351
352     for (i = 0; i < blocks; i++) {
353         for (j = 0; j < DTLS_BLK_LENGTH; j++) {
354             cbc[j] ^= pos[j];
355         }
356
357         rijndael_encrypt(&aes_ctx->ctx, cbc, tmp);
358         memcpy(cbc, tmp, DTLS_BLK_LENGTH);
359         memcpy(pos, cbc, DTLS_BLK_LENGTH);
360         pos += DTLS_BLK_LENGTH;
361     }
362
363     dtls_debug_dump("Encrypted Data:", buf, srclen + SHA256_DIGEST_LENGTH);
364
365     return srclen + SHA256_DIGEST_LENGTH;
366 }
367
368
369 static size_t
370 dtls_cbc_decrypt(aes128_t *aes_ctx,
371                  const unsigned char *iv,
372                  const unsigned char *src, size_t srclen,
373                  unsigned char *buf) {
374
375     unsigned char cbc[DTLS_BLK_LENGTH];
376     unsigned char tmp[DTLS_BLK_LENGTH];
377     unsigned char tmp2[DTLS_BLK_LENGTH];
378     unsigned char msg_hash[SHA256_DIGEST_LENGTH];
379     unsigned char *pos;
380     dtls_hash_ctx shactx;
381     int i, j;
382     int blocks;
383
384     pos = buf;
385     memcpy(pos, src, srclen);
386
387     memcpy(cbc, iv, DTLS_BLK_LENGTH);
388     blocks = srclen / DTLS_BLK_LENGTH;
389
390     for (i = 0; i < blocks; i++)
391     {
392         memcpy(tmp, pos, DTLS_BLK_LENGTH);
393         rijndael_decrypt(&aes_ctx->ctx, pos, tmp2);
394         memcpy(pos, tmp2, DTLS_BLK_LENGTH);
395
396         for (j = 0; j < DTLS_BLK_LENGTH; j++) {
397             pos[j] ^= cbc[j];
398         }
399
400         memcpy(cbc, tmp, DTLS_BLK_LENGTH);
401         pos += DTLS_BLK_LENGTH;
402     }
403
404     dtls_hash_init(&shactx);
405     dtls_hash_update(&shactx, buf, srclen - SHA256_DIGEST_LENGTH);
406     dtls_hash_finalize(msg_hash, &shactx);
407
408     dtls_debug_dump("decrypted data:", buf, srclen);
409
410     if(memcmp(msg_hash, buf + (srclen - SHA256_DIGEST_LENGTH), SHA256_DIGEST_LENGTH) != 0)
411     {
412         dtls_warn("message is broken\n");
413         return -1;
414     }
415
416     return srclen - SHA256_DIGEST_LENGTH;
417 }
418
419 #ifdef DTLS_PSK
420 int
421 dtls_psk_pre_master_secret(unsigned char *key, size_t keylen,
422                            unsigned char *result, size_t result_len) {
423   unsigned char *p = result;
424
425   if (result_len < (2 * (sizeof(uint16) + keylen))) {
426     return -1;
427   }
428
429   dtls_int_to_uint16(p, keylen);
430   p += sizeof(uint16);
431
432   memset(p, 0, keylen);
433   p += keylen;
434
435   memcpy(p, result, sizeof(uint16));
436   p += sizeof(uint16);
437
438   memcpy(p, key, keylen);
439
440   return 2 * (sizeof(uint16) + keylen);
441 }
442 #endif /* DTLS_PSK */
443
444 #ifdef DTLS_ECC
445
446 int dtls_ec_key_from_uint32_asn1(const uint32_t *key, size_t key_size,
447                                  unsigned char *buf) {
448   int i;
449   unsigned char *buf_orig = buf;
450   int first = 1;
451
452   for (i = (key_size / sizeof(uint32_t)) - 1; i >= 0 ; i--) {
453     if (key[i] == 0)
454       continue;
455     /* the first bit has to be set to zero, to indicate a poritive integer */
456     if (first && key[i] & 0x80000000) {
457       *buf = 0;
458       buf++;
459       dtls_int_to_uint32(buf, key[i]);
460       buf += 4;
461     } else if (first && !(key[i] & 0xFF800000)) {
462       buf[0] = (key[i] >> 16) & 0xff;
463       buf[1] = (key[i] >> 8) & 0xff;
464       buf[2] = key[i] & 0xff;
465       buf += 3;
466     } else if (first && !(key[i] & 0xFFFF8000)) {
467       buf[0] = (key[i] >> 8) & 0xff;
468       buf[1] = key[i] & 0xff;
469       buf += 2;
470     } else if (first && !(key[i] & 0xFFFFFF80)) {
471       buf[0] = key[i] & 0xff;
472       buf += 1;
473     } else {
474       dtls_int_to_uint32(buf, key[i]);
475       buf += 4;
476     }
477     first = 0;
478   }
479   return buf - buf_orig;
480 }
481
482 int dtls_ecdh_pre_master_secret(unsigned char *priv_key,
483                                    unsigned char *pub_key_x,
484                                    unsigned char *pub_key_y,
485                                    size_t key_size,
486                                    unsigned char *result,
487                                    size_t result_len) {
488
489   uint8_t publicKey[64];
490   uint8_t privateKey[32];
491
492   if (result_len < key_size) {
493     return -1;
494   }
495
496
497   memcpy(publicKey, pub_key_x, 32);
498   memcpy(publicKey + 32, pub_key_y, 32);
499   memcpy(privateKey, priv_key, 32);
500   uECC_shared_secret(publicKey, privateKey, result);
501
502   return key_size;
503 }
504
505 void
506 dtls_ecdsa_generate_key(unsigned char *priv_key,
507                         unsigned char *pub_key_x,
508                         unsigned char *pub_key_y,
509                         size_t key_size) {
510
511   uint8_t publicKey[64];
512   uint8_t privateKey[32];
513
514   uECC_make_key(publicKey, privateKey);
515   memcpy(pub_key_x, publicKey, 32);
516   memcpy(pub_key_y, publicKey + 32, 32);
517   memcpy(priv_key, privateKey, 32);
518
519 }
520
521 /* rfc4492#section-5.4 */
522 void
523 dtls_ecdsa_create_sig_hash(const unsigned char *priv_key, size_t key_size,
524                            const unsigned char *sign_hash, size_t sign_hash_size,
525                            uint32_t point_r[9], uint32_t point_s[9]) {
526   int ret;
527
528   uint8_t privateKey[32];
529   uint8_t hashValue[32];
530   uint8_t sign[64];
531
532
533   uECC_sign(privateKey, hashValue, sign);
534   memcpy(point_r, sign, 32);
535   memcpy(point_s, sign + 32, 32);
536 }
537
538 void
539 dtls_ecdsa_create_sig(const unsigned char *priv_key, size_t key_size,
540                       const unsigned char *client_random, size_t client_random_size,
541                       const unsigned char *server_random, size_t server_random_size,
542                       const unsigned char *keyx_params, size_t keyx_params_size,
543                       uint32_t point_r[9], uint32_t point_s[9]) {
544   dtls_hash_ctx data;
545   unsigned char sha256hash[DTLS_HMAC_DIGEST_SIZE];
546
547   dtls_hash_init(&data);
548   dtls_hash_update(&data, client_random, client_random_size);
549   dtls_hash_update(&data, server_random, server_random_size);
550   dtls_hash_update(&data, keyx_params, keyx_params_size);
551   dtls_hash_finalize(sha256hash, &data);
552
553   dtls_ecdsa_create_sig_hash(priv_key, key_size, sha256hash,
554                              sizeof(sha256hash), point_r, point_s);
555 }
556
557 /* rfc4492#section-5.4 */
558 int
559 dtls_ecdsa_verify_sig_hash(const unsigned char *pub_key_x,
560                            const unsigned char *pub_key_y, size_t key_size,
561                            const unsigned char *sign_hash, size_t sign_hash_size,
562                            unsigned char *result_r, unsigned char *result_s) {
563
564   uint8_t publicKey[64];
565   uint8_t hashValue[32];
566   uint8_t sign[64];
567
568   memcpy(publicKey, pub_key_x, 32);
569   memcpy(publicKey + 32, pub_key_y, 32);
570   return uECC_verify(publicKey, hashValue, sign);
571 }
572
573 int
574 dtls_ecdsa_verify_sig(const unsigned char *pub_key_x,
575                       const unsigned char *pub_key_y, size_t key_size,
576                       const unsigned char *client_random, size_t client_random_size,
577                       const unsigned char *server_random, size_t server_random_size,
578                       const unsigned char *keyx_params, size_t keyx_params_size,
579                       unsigned char *result_r, unsigned char *result_s) {
580   dtls_hash_ctx data;
581   unsigned char sha256hash[DTLS_HMAC_DIGEST_SIZE];
582
583   dtls_hash_init(&data);
584   dtls_hash_update(&data, client_random, client_random_size);
585   dtls_hash_update(&data, server_random, server_random_size);
586   dtls_hash_update(&data, keyx_params, keyx_params_size);
587   dtls_hash_finalize(sha256hash, &data);
588
589   return dtls_ecdsa_verify_sig_hash(pub_key_x, pub_key_y, key_size, sha256hash,
590                                     sizeof(sha256hash), result_r, result_s);
591 }
592 #endif /* DTLS_ECC */
593
594 int
595 dtls_encrypt(const unsigned char *src, size_t length,
596              unsigned char *buf,
597              unsigned char *nounce,
598              unsigned char *key, size_t keylen,
599              const unsigned char *aad, size_t la,
600              const dtls_cipher_t cipher)
601 {
602   int ret = 0;
603   struct dtls_cipher_context_t *ctx = dtls_cipher_context_get();
604
605   if(cipher == TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 ||
606      cipher == TLS_PSK_WITH_AES_128_CCM_8) {
607       ret = rijndael_set_key_enc_only(&ctx->data.ctx, key, 8 * keylen);
608       if (ret < 0) {
609         /* cleanup everything in case the key has the wrong size */
610         dtls_warn("cannot set rijndael key\n");
611         goto error;
612       }
613
614       if (src != buf)
615         memmove(buf, src, length);
616       ret = dtls_ccm_encrypt(&ctx->data, src, length, buf, nounce, aad, la);
617   }
618   if(cipher == TLS_ECDH_anon_WITH_AES_128_CBC_SHA) {
619       ret = rijndael_set_key(&ctx->data.ctx, key, 8 * keylen);
620       if (ret < 0) {
621         /* cleanup everything in case the key has the wrong size */
622         dtls_warn("cannot set rijndael key\n");
623         goto error;
624       }
625
626       if (src != buf)
627         memmove(buf, src, length);
628       ret = dtls_cbc_encrypt(&ctx->data, nounce, src, length, buf);
629   }
630
631 error:
632   dtls_cipher_context_release();
633   return ret;
634 }
635
636 int
637 dtls_decrypt(const unsigned char *src, size_t length,
638              unsigned char *buf,
639              unsigned char *nounce,
640              unsigned char *key, size_t keylen,
641              const unsigned char *aad, size_t la,
642              const dtls_cipher_t cipher)
643 {
644   int ret = 0;
645   struct dtls_cipher_context_t *ctx = dtls_cipher_context_get();
646
647   if(cipher == TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 ||
648      cipher == TLS_PSK_WITH_AES_128_CCM_8) {
649       ret = rijndael_set_key_enc_only(&ctx->data.ctx, key, 8 * keylen);
650       if (ret < 0) {
651         /* cleanup everything in case the key has the wrong size */
652         dtls_warn("cannot set rijndael key\n");
653         goto error;
654       }
655
656       if (src != buf)
657         memmove(buf, src, length);
658       ret = dtls_ccm_decrypt(&ctx->data, src, length, buf, nounce, aad, la);
659   }
660
661   if(cipher == TLS_ECDH_anon_WITH_AES_128_CBC_SHA) {
662       ret = rijndael_set_key(&ctx->data.ctx, key, 8 * keylen);
663       if (ret < 0) {
664         /* cleanup everything in case the key has the wrong size */
665         dtls_warn("cannot set rijndael key\n");
666         goto error;
667       }
668
669       if (src != buf)
670         memmove(buf, src, length);
671       ret = dtls_cbc_decrypt(&ctx->data, nounce, src, length, buf);
672     }
673
674 error:
675   dtls_cipher_context_release();
676   return ret;
677 }
678