source: mainline/uspace/lib/crypto/rc4.c@ cc575ef9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since cc575ef9 was cc575ef9, checked in by Jan Kolarik <kolarik@…>, 10 years ago

Appending Michael MIC footer in TKIP communication, tested MIC algorithm itself, but needs testing on real data transmission

  • Property mode set to 100644
File size: 3.4 KB
Line 
1/*
2 * Copyright (c) 2015 Jan Kolarik
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @file rc4.c
30 *
31 * Implementation of ARC4 symmetric cipher cryptographic algorithm.
32 *
33 */
34
35#include <errno.h>
36#include <mem.h>
37
38#include "crypto.h"
39
40/* Sbox table size. */
41#define SBOX_SIZE 256
42
43/**
44 * Swap two values in sbox.
45 *
46 * @param i First index of value in sbox to be swapped.
47 * @param j Second index of value in sbox to be swapped.
48 * @param sbox Sbox to be modified.
49 */
50static void swap(size_t i, size_t j, uint8_t *sbox)
51{
52 uint8_t temp = sbox[i];
53 sbox[i] = sbox[j];
54 sbox[j] = temp;
55}
56
57/**
58 * Sbox initialization procedure.
59 *
60 * @param key Input key.
61 * @param key_size Size of key sequence.
62 * @param sbox Place for result sbox.
63 */
64static void create_sbox(uint8_t *key, size_t key_size, uint8_t *sbox)
65{
66 for(size_t i = 0; i < SBOX_SIZE; i++) {
67 sbox[i] = i;
68 }
69
70 uint8_t j = 0;
71 for(size_t i = 0; i < SBOX_SIZE; i++) {
72 j = j + sbox[i] + key[i % key_size];
73 swap(i, j, sbox);
74 }
75}
76
77/**
78 * ARC4 encryption/decryption algorithm.
79 *
80 * @param key Input key.
81 * @param key_size Size of key sequence.
82 * @param input Input data sequence to be processed.
83 * @param input_size Size of input data sequence.
84 * @param skip Number of bytes to be skipped from the beginning of key stream.
85 * @param output Result data sequence.
86 *
87 * @return EINVAL when input or key not specified, ENOMEM when pointer for
88 * output is not allocated, otherwise EOK.
89 */
90int rc4(uint8_t *key, size_t key_size, uint8_t *input, size_t input_size,
91 size_t skip, uint8_t *output)
92{
93 if(!key || !input)
94 return EINVAL;
95
96 if(!output)
97 return ENOMEM;
98
99 /* Initialize sbox. */
100 uint8_t sbox[SBOX_SIZE];
101 create_sbox(key, key_size, sbox);
102
103 /* Skip first x bytes. */
104 uint8_t i = 0, j = 0;
105 for(size_t k = 0; k < skip; k++) {
106 i = i + 1;
107 j = j + sbox[i];
108 swap(i, j, sbox);
109 }
110
111 /* Processing loop. */
112 uint8_t val;
113 for(size_t k = 0; k < input_size; k++) {
114 i = i + 1;
115 j = j + sbox[i];
116 swap(i, j, sbox);
117 val = sbox[sbox[i] + sbox[j]];
118 output[k] = val ^ input[k];
119 }
120
121 return EOK;
122}
Note: See TracBrowser for help on using the repository browser.