xref: /optee_os/core/lib/libtomcrypt/src/stream/rc4/rc4_test.c (revision 8411e6ad673d20c4742ed30c785e3f5cdea54dfa)
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 
4 #include "tomcrypt_private.h"
5 
6 #ifdef LTC_RC4_STREAM
7 
rc4_stream_test(void)8 int rc4_stream_test(void)
9 {
10 #ifndef LTC_TEST
11    return CRYPT_NOP;
12 #else
13    rc4_state st;
14    int err;
15    const unsigned char key[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
16    const unsigned char pt[]  = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
17    const unsigned char ct[]  = { 0x75, 0xb7, 0x87, 0x80, 0x99, 0xe0, 0xc5, 0x96 };
18    unsigned char buf[10];
19 
20    if ((err = rc4_stream_setup(&st, key, sizeof(key))) != CRYPT_OK)    return err;
21    if ((err = rc4_stream_crypt(&st, pt, sizeof(pt), buf)) != CRYPT_OK) return err;
22    if (compare_testvector(buf, sizeof(ct), ct, sizeof(ct), "RC4-TV1", 0))  return CRYPT_FAIL_TESTVECTOR;
23    if ((err = rc4_stream_done(&st)) != CRYPT_OK)                       return err;
24 
25    /* crypt in a single call */
26    if ((err = rc4_stream_memory(key, sizeof(key), pt, sizeof(pt), buf)) != CRYPT_OK) return err;
27    if (compare_testvector(buf, sizeof(ct), ct, sizeof(ct), "RC4-TV2", 0))  return CRYPT_FAIL_TESTVECTOR;
28 
29    return CRYPT_OK;
30 #endif
31 }
32 
33 #endif
34