Created
April 3, 2017 00:31
-
-
Save yifanlu/19ead543722244c4c5de61962083f92f to your computer and use it in GitHub Desktop.
SBG6580 3DES key conversion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
static int convert_key(unsigned char *src, unsigned char *dst) { | |
unsigned char v0, v1, a2, *v1x, a0x; | |
// convert 7-bit groups to 8-bits | |
v0 = src[0]; | |
v0 &= 0xFE; | |
dst[0] = v0; | |
v1 = src[1]; | |
v1 >>= 1; | |
v1 &= 0x7E; | |
v0 = src[0]; | |
v0 &= 1; | |
v0 <<= 7; | |
v1 += v0; | |
dst[1] = v1; | |
v1 = src[2]; | |
v1 >>= 2; | |
v1 &= 0x3E; | |
v0 = src[1]; | |
v0 &= 3; | |
v0 <<= 6; | |
v1 += v0; | |
dst[2] = v1; | |
v1 = src[3]; | |
v1 >>= 3; | |
v1 &= 0x1E; | |
v0 = src[2]; | |
v0 &= 7; | |
v0 <<= 5; | |
v1 += v0; | |
dst[3] = v1; | |
v1 = src[4]; | |
v1 >>= 4; | |
v1 &= 0xE; | |
v0 = src[3]; | |
v0 &= 0xF; | |
v0 <<= 4; | |
v1 += v0; | |
dst[4] = v1; | |
v1 = src[5]; | |
v1 >>= 5; | |
v1 &= 6; | |
v0 = src[4]; | |
v0 &= 0x1F; | |
v0 <<= 3; | |
v1 += v0; | |
dst[5] = v1; | |
v1 = src[6]; | |
v1 >>= 6; | |
v1 &= 2; | |
v0 = src[5]; | |
v0 &= 0x3F; | |
v0 <<= 2; | |
v1 += v0; | |
dst[6] = v1; | |
v0 = src[6]; | |
v0 <<= 1; | |
dst[7] = v0; | |
// compute the parity bit | |
a2 = 0; | |
v1x = &dst[a2]; | |
do { | |
v0 = v1x[0]; | |
v0 ^= 1; | |
v1x[0] = v0; | |
a0x = 1; | |
v0 = v1x[0]; | |
do { | |
v0 = (char)v0 >> a0x; | |
v0 &= 1; | |
a0x += 1; | |
if (v0 != 0) { | |
v0 = v1x[0]; | |
v0 ^= 1; | |
v1x[0] = v0; | |
} | |
v0 = v1x[0]; | |
} while (a0x < 8); | |
a2 += 1; | |
v1x = &dst[a2]; | |
} while (a2 < 8); | |
return 0; | |
} | |
static int derive_key(unsigned char *src, unsigned char *dst) { | |
for (int j = 0; j < 7; j++) { | |
for (int i = 0; i < 3; i++) { | |
dst[7*i + j] = src[3*j + i] - j; | |
} | |
} | |
return 0; | |
} | |
int main(int argc, const char *argv[]) { | |
if (argc != 2) { | |
fprintf(stderr, "%s\n", "invalid args\n"); | |
return 1; | |
} | |
if (strnlen(argv[1], 64) != 42) { | |
fprintf(stderr, "%s\n", "key must be 42 hex characters\n"); | |
} | |
fprintf(stderr, "in : "); | |
unsigned char in[21]; | |
for (int i = 0; i < 21; i++) { | |
char sym[3]; | |
int cur; | |
int conv; | |
sym[0] = argv[1][2*i]; | |
sym[1] = argv[1][2*i+1]; | |
sym[2] = '\0'; | |
cur = strtoul(sym, NULL, 16); | |
fprintf(stderr, "%02X", cur); | |
in[i] = cur; | |
} | |
fprintf(stderr, "\n"); | |
unsigned char drv[21]; | |
derive_key(in, drv); | |
printf("out: "); | |
for (int i = 0; i < 3; i++) { | |
unsigned char dst[8]; | |
convert_key(&drv[7*i], dst); | |
for (int j = 0; j < 8; j++) { | |
printf("%02X", dst[j]); | |
} | |
} | |
printf("\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment