forked from MidnightBlueLabs/TETRA_crypto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_ta61.c
108 lines (93 loc) · 3.33 KB
/
gen_ta61.c
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2023, Midnight Blue.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <sodium.h>
#include <unistd.h>
#include <sys/sysinfo.h>
// Imports from tetra implementation
#include "common.h"
#include "taa1.h"
int main(int argc, const char* argv[]) {
if (argc != 4) {
printf("[+] TA61 identity encryption/decryption\n");
printf(" Usage: %s e cck ssi\n", argv[0]);
printf(" Usage: %s d cck esi\n", argv[0]);
printf(" TA61 intermediate secret is supported\n");
printf(" Example vectors:\n");
printf(" %s e 11111111111111111111 100\n", argv[0]);
printf(" %s d 11111111111111111111 12845063\n", argv[0]);
exit(1);
}
char bOp;
int dwArgsParsed = sscanf(argv[1], "%c", &bOp);
if (dwArgsParsed != 1 || (bOp != 'e' && bOp != 'd')) {
printf("[-] Can't parse operation type, should be e for encrypt or d for decrypt\n");
exit(1);
}
uint8_t abCck[10];
int dwKeyLen;
if (strlen(argv[2]) == 20) {
dwKeyLen = 10;
} else if (strlen(argv[2]) == 16) {
dwKeyLen = 8;
} else {
printf("[-] Unexpected cck length, should be 8 or 10 bytes\n");
exit(1);
}
for (int i = 0; i < dwKeyLen; i++) {
if (!sscanf(&argv[2][2*i], "%02hhX", &abCck[i])) {
printf("[-] Can't parse key %d\n", i);
exit(1);
}
}
int dwSsi, dwSsiOut;
dwArgsParsed = sscanf(argv[3], "%d", &dwSsi);
if (dwArgsParsed != 1 || (dwSsi > 0xFFFFFF)) {
printf("[-] Can't parse SSI\n");
exit(1);
}
uint8_t abSsi[3];
abSsi[0] = (dwSsi >> 16) & 0xFF;
abSsi[1] = (dwSsi >> 8) & 0xFF;
abSsi[2] = (dwSsi) & 0xFF;
printf("TA61 ");
uint8_t abInt[8];
if (bOp == 'e') {
if (dwKeyLen == 10) {
ta61_compute_c(abCck, abInt);
ta61_inner(abInt, abSsi, abSsi);
printf("intermediate: %02X%02X%02X%02X%02X%02X%02X%02X: ",
abInt[0], abInt[1], abInt[2], abInt[3], abInt[4], abInt[5], abInt[6], abInt[7]);
} else {
ta61_inner(abCck, abSsi, abSsi);
}
} else {
if (dwKeyLen == 10) {
ta61_compute_c(abCck, abInt);
ta61_inner_inv(abInt, abSsi, abSsi);
printf("intermediate: %02X%02X%02X%02X%02X%02X%02X%02X: ",
abInt[0], abInt[1], abInt[2], abInt[3], abInt[4], abInt[5], abInt[6], abInt[7]);
} else {
ta61_inner_inv(abCck, abSsi, abSsi);
}
}
dwSsiOut = (abSsi[0] << 16) | (abSsi[1] << 8) | abSsi[2];
printf("%d -> %d\n", dwSsi, dwSsiOut);
return 0;
}