File: rc5.c

package info (click to toggle)
uif2iso 0.1.7a-1
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 392 kB
  • ctags: 536
  • sloc: ansic: 4,552; makefile: 40
file content (82 lines) | stat: -rw-r--r-- 2,221 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
    Copyright (C) 2008 Luigi Auriemma

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program 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 General Public License for more details.

    http://www.gnu.org/licenses/gpl-2.0.txt
*/
// Code modified by Luigi Auriemma. for emulating the shameful customizations of MagicISO
// Code derived from the reference implementation of J.S.A.Kapp with deep modifications.

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <time.h>

typedef uint32_t word32;

	/* rotate functions */
#define ROTL(x,s) ((x)<<(s) | (x)>>(32-(s)))
#define ROTR(x,s) ((x)>>(s) | (x)<<(32-(s)))

	/* Magic Contants */
#define P 0xb7e15163
#define Q 0x9e3779b9

#define ANDVAL 31

#define KR(x) (2*((x)+1))
#define MAX(x, y) (((x) > (y)) ? (x) : (y))

void RC5decrypt(word32 *in, int len, int b, word32 *key) {
	int i;
    int t;

    for(t = b << 2; len; len--) {
        key += t + 1;
        for (i = 0; i < t; i += 2) {
            in[1] -= *key--;
            in[1] = ROTR(in[1], in[0]&ANDVAL) ^ in[0];
            in[0] -= *key--;
            in[0] ^= 0x63a72efb;
            in[0] = ROTR(in[0], in[1]&ANDVAL) ^ in[1];
        }
        in[1] -= *key--;
        in[0] -= *key;
        in += 2;
    }
}

void RC5key(unsigned char *key, int b, word32 **ctx) {
    int t = (b << 2) + 2;
	int i;
	int x = ((b-1)/sizeof(word32))+1;
	int mix = 3 * (MAX(t,x));
	word32 *L, *ky;
	word32 A = 0, B = 0;

	L = (word32 *) calloc((x+1), sizeof(word32));
	memcpy((unsigned char *)L, key, b);

	ky = (word32 *) calloc((t+1), sizeof(word32));
	*ky = P;
		/* Prep subkeys with magic Constants */
	for(i = 1; i < t; i++)
		ky[i] = ky[i-1] + Q;

	for(i = 0; i < mix; i++) {
		A = ky[i%t] = ROTL((ky[i%t] + A + B), 3&ANDVAL);
		B = L[i%x] = ROTL((L[i%x] + A + B), ((A + B)&ANDVAL));
	}
    free(L);
    *ctx = ky;
}