]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
gnutls_prf_rfc5705: optimize in the common use case, by avoiding malloc
authorNikos Mavrogiannopoulos <nmav@redhat.com>
Mon, 20 Jul 2015 12:59:37 +0000 (14:59 +0200)
committerNikos Mavrogiannopoulos <nmav@redhat.com>
Mon, 20 Jul 2015 13:28:16 +0000 (15:28 +0200)
Also don't handle specially the case of non-NULL context and context_size of zero.

lib/gnutls_state.c
tests/prf.c

index b070b21b418b49663a73de40c3de9a31df899167..36b279b9fa576e8ab319916ef94042fad9eb3024 100644 (file)
@@ -1099,26 +1099,29 @@ gnutls_prf_rfc5705(gnutls_session_t session,
                   size_t context_size, const char *context,
                   size_t outsize, char *out)
 {
-       char *pctx;
+       char *pctx = NULL;
        int ret;
 
-       if (context_size > 65535)  {
+       if (context != NULL && context_size > 65535)  {
                gnutls_assert();
                return GNUTLS_E_INVALID_REQUEST;
        }
 
-       pctx = gnutls_malloc(context_size+2);
-       if (!pctx) {
-               gnutls_assert();
-               return GNUTLS_E_MEMORY_ERROR;
-       }
+       if (context != NULL && context_size != 0) {
+               pctx = gnutls_malloc(context_size+2);
+               if (!pctx) {
+                       gnutls_assert();
+                       return GNUTLS_E_MEMORY_ERROR;
+               }
 
-       _gnutls_write_uint16(context_size, (void*)pctx);
-       if (context && context_size) {
                memcpy(pctx+2, context, context_size);
+               _gnutls_write_uint16(context_size, (void*)pctx);
+               context_size += 2;
        }
+
        ret = gnutls_prf(session, label_size, label, 0,
-                        context_size+2, pctx, outsize, out);
+                        context_size, pctx, outsize, out);
+
        gnutls_free(pctx);
        return ret;
 }
index 7fe2a9aa52739bf32ada0e23f50e5acfb2324d5d..eff9c9a85ac22f3df2b4d0bc8b4e1fb0266aa757 100644 (file)
@@ -200,8 +200,8 @@ static void check_prfs(gnutls_session_t session)
        unsigned char key_material[512];
        int ret;
 
-       TRY(13, "key expansion", 0, NULL, 34, (uint8_t*)"\x7f\x91\x8f\xf1\x66\x6d\xfb\x06\xcf\x39\x0d\x48\xea\x51\x29\x1a\x0d\x9a\x63\xde\x0f\x9f\xaf\xf5\x30\xc0\xa3\xe0\x5e\xa6\xc0\x64\x55\x43");
-       TRY(6, "hello", 0, NULL, 31, (uint8_t*)"\xd4\x73\x55\xf9\xf3\xbc\x6d\x6f\xf4\x7e\x2c\x80\x16\x50\xda\x50\xdc\xc4\x39\xf7\x10\xcc\xba\x48\x94\x80\x71\x6d\x8c\x8e\xb7");
+       TRY(13, "key expansion", 0, NULL, 34, (uint8_t*)"\xcf\x3e\x1c\x03\x47\x1a\xdf\x4a\x8e\x74\xc6\xda\xcd\xda\x22\xa4\x8e\xa5\xf7\x62\xef\xd6\x47\xe7\x41\x20\xea\x44\xb8\x5d\x66\x87\x0a\x61");
+       TRY(6, "hello", 0, NULL, 31, (uint8_t*)"\x83\x6c\xc7\x8e\x1b\x62\xc7\x06\x17\x99\x37\x95\x2e\xb8\x42\x5c\x42\xcd\x75\x65\x2c\xa3\x16\x2b\xab\x0a\xcf\xfc\xc8\x90\x30");
        TRY(7, "context", 5, "abcd\xfa", 31, (uint8_t*)"\x5b\xc7\x72\xe9\xda\xe4\x79\x3e\xfe\x9a\xc5\x6f\xf4\x8d\x5a\xfe\x4c\x8d\x16\xa7\xf0\x13\x13\xf1\x93\xdd\x4b\x43\x65\xc1\x94");
 
        TRY_OLD(6, "hello", 0, NULL, 31, (uint8_t*)"\x53\x35\x9b\x1c\xbf\xf2\x50\x85\xa1\xbc\x42\xfb\x45\x92\xc3\xbe\x20\x24\x24\xe2\xeb\x6e\xf7\x4f\xc0\xee\xe3\xaa\x46\x36\xfd");