test-aes.c
资源名称:vlc-1.0.5.zip [点击查看]
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:5k
源码类别:
midi
开发平台:
Unix_Linux
- /*
- * Secure RTP with libgcrypt
- * Copyright (C) 2007 Rémi Denis-Courmont <rdenis # simphalempin , com>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- */
- #include <stdio.h>
- #include "srtp.c"
- static void printhex (const void *buf, size_t len)
- {
- for (size_t i = 0; i < len; i++)
- printf ("%02X", ((uint8_t *)buf)[i]);
- fputc ('n', stdout);
- }
- static void fatal (const char *msg)
- {
- puts (msg);
- exit (1);
- }
- /** AES-CM key derivation test vectors */
- static void test_derivation (void)
- {
- static const uint8_t key[16] =
- "xE1xF9x7Ax0Dx3Ex01x8BxE0xD6x4FxA3x2Cx06xDEx41x39";
- static const uint8_t salt[14] =
- "x0ExC6x75xADx49x8AxFExEBxB6x96x0Bx3AxABxE6";
- static const uint8_t good_cipher[16] =
- "xC6x1Ex7Ax93x74x4Fx39xEEx10x73x4AxFEx3FxF7xA0x87";
- static const uint8_t good_salt[14] =
- "x30xCBxBCx08x86x3Dx8Cx85xD4x9DxB3x4Ax9AxE1";
- static const uint8_t good_auth[94] =
- "xCExBEx32x1Fx6FxF7x71x6Bx6FxD4xABx49xAFx25x6Ax15"
- "x6Dx38xBAxA4x8Fx0Ax0AxCFx3Cx34xE2x35x9Ex6CxDBxCE"
- "xE0x49x64x6Cx43xD9x32x7AxD1x75x57x8ExF7x22x70x98"
- "x63x71xC1x0Cx9Ax36x9AxC2xF9x4Ax8Cx5FxBCxDDxDCx25"
- "x6Dx6Ex91x9Ax48xB6x10xEFx17xC2x04x1Ex47x40x35x76"
- "x6Bx68x64x2Cx59xBBxFCx2Fx34xDBx60xDBxDFxB2";
- static const uint8_t r[6] = { 0, 0, 0, 0, 0, 0 };
- gcry_cipher_hd_t prf;
- uint8_t out[94];
- puts ("AES-CM key derivation test...");
- printf (" master key: ");
- printhex (key, sizeof (key));
- printf (" master salt: ");
- printhex (salt, sizeof (salt));
- if (gcry_cipher_open (&prf, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CTR, 0)
- || gcry_cipher_setkey (prf, key, sizeof (key)))
- fatal ("Internal PRF error");
- if (derive (prf, salt, r, sizeof (r), SRTP_CRYPT, out, 16))
- fatal ("Internal cipher derivation error");
- printf (" cipher key: ");
- printhex (out, 16);
- if (memcmp (out, good_cipher, 16))
- fatal ("Test failed");
- if (derive (prf, salt, r, sizeof (r), SRTP_SALT, out, 14))
- fatal ("Internal salt derivation error");
- printf (" cipher salt: ");
- printhex (out, 14);
- if (memcmp (out, good_salt, 14))
- fatal ("Test failed");
- if (derive (prf, salt, r, sizeof (r), SRTP_AUTH, out, 94))
- fatal ("Internal auth key derivation error");
- printf (" auth key: ");
- printhex (out, 94);
- if (memcmp (out, good_auth, 94))
- fatal ("Test failed");
- gcry_cipher_close (prf);
- }
- /** AES-CM key derivation test vectors */
- static void test_keystream (void)
- {
- static const uint8_t key[16] =
- "x2Bx7Ex15x16x28xAExD2xA6xABxF7x15x88x09xCFx4Fx3C";
- const uint32_t salt[4]=
- { htonl (0xf0f1f2f3), htonl (0xf4f5f6f7),
- htonl (0xf8f9fafb), htonl (0xfcfd0000) };
- puts ("AES-CM key stream test...");
- uint8_t *buf = calloc (0xff02, 16);
- if (buf == NULL)
- {
- fputs ("Not enough memory for testn", stderr);
- return;
- }
- printf (" session key: ");
- printhex (key, sizeof (key));
- gcry_cipher_hd_t hd;
- if (gcry_cipher_open (&hd, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CTR, 0))
- fatal ("Cipher initialization error");
- if (gcry_cipher_setkey (hd, key, sizeof (key)))
- fatal ("Cipher key error");
- if (rtp_crypt (hd, 0, 0, 0, salt, buf, 0xff020))
- fatal ("Encryption failure");
- gcry_cipher_close (hd);
- static const uint8_t good_start[48] =
- "xE0x3ExADx09x35xC9x5Ex80xE1x66xB1x6DxD9x2Bx4ExB4"
- "xD2x35x13x16x2Bx02xD0xF7x2Ax43xA2xFEx4Ax5Fx97xAB"
- "x41xE9x5Bx3BxB0xA2xE8xDDx47x79x01xE4xFCxA8x94xC0";
- static const uint8_t good_end[48] =
- "xECx8CxDFx73x98x60x7CxB0xF2xD2x16x75xEAx9ExA1xE4"
- "x36x2Bx7Cx3Cx67x73x51x63x18xA0x77xD7xFCx50x73xAE"
- "x6Ax2CxC3x78x78x89x37x4FxBExB4xC8x1Bx17xBAx6Cx44";
- printf (" key stream: ");
- printhex (buf, sizeof (good_start));
- printf (" ... cont'd : ");
- printhex (buf + 0xff020 - sizeof (good_end), sizeof (good_end));
- if (memcmp (buf, good_start, sizeof (good_start))
- || memcmp (buf + 0xff020 - sizeof (good_end), good_end,
- sizeof (good_end)))
- fatal ("Key stream test failed");
- free (buf);
- }
- static void srtp_test (void)
- {
- if (init_libgcrypt ())
- fatal ("Libgcrypt initialization error");
- test_derivation ();
- test_keystream ();
- }
- int main (void)
- {
- srtp_test ();
- return 0;
- }