libqaeda

Unnamed repository; edit this file 'description' to name the repository.
Info | Log | Files | Refs | README | LICENSE

dummy.c (6933B)


      1 #include <stdlib.h>
      2 
      3 #include "lq/crypto.h"
      4 #include "lq/mem.h"
      5 #include "lq/err.h"
      6 
      7 
      8 // sha512sum "foo" 2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae
      9 static const char pubkey_dummy_transform[64] = {
     10 	0xf7, 0xfb, 0xba, 0x6e, 0x06, 0x36, 0xf8, 0x90,
     11 	0xe5, 0x6f, 0xbb, 0xf3, 0x28, 0x3e, 0x52, 0x4c,
     12 	0x6f, 0xa3, 0x20, 0x4a, 0xe2, 0x98, 0x38, 0x2d,
     13 	0x62, 0x47, 0x41, 0xd0, 0xdc, 0x66, 0x38, 0x32,
     14 	0x6e, 0x28, 0x2c, 0x41, 0xbe, 0x5e, 0x42, 0x54,
     15 	0xd8, 0x82, 0x07, 0x72, 0xc5, 0x51, 0x8a, 0x2c,
     16 	0x5a, 0x8c, 0x0c, 0x7f, 0x7e, 0xda, 0x19, 0x59,
     17 	0x4a, 0x7e, 0xb5, 0x39, 0x45, 0x3e, 0x1e, 0xd7,
     18 };
     19 
     20 // sha512sum "bar" d82c4eb5261cb9c8aa9855edd67d1bd10482f41529858d925094d173fa662aa91ff39bc5b188615273484021dfb16fd8284cf684ccf0fc795be3aa2fc1e6c181
     21 static const char sig_dummy_transform[64] = {
     22 	0xd8, 0x2c, 0x4e, 0xb5, 0x26, 0x1c, 0xb9, 0xc8,
     23 	0xaa, 0x98, 0x55, 0xed, 0xd6, 0x7d, 0x1b, 0xd1,
     24 	0x04, 0x82, 0xf4, 0x15, 0x29, 0x85, 0x8d, 0x92,
     25 	0x50, 0x94, 0xd1, 0x73, 0xfa, 0x66, 0x2a, 0xa9,
     26 	0x1f, 0xf3, 0x9b, 0xc5, 0xb1, 0x88, 0x61, 0x52,
     27 	0x73, 0x48, 0x40, 0x21, 0xdf, 0xb1, 0x6f, 0xd8,
     28 	0x28, 0x4c, 0xf6, 0x84, 0xcc, 0xf0, 0xfc, 0x79,
     29 	0x5b, 0xe3, 0xaa, 0x2f, 0xc1, 0xe6, 0xc1, 0x81,
     30 };
     31 
     32 // sha256sum "foo" 2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae
     33 static const char encrypt_dummy_transport[32] = {
     34 	0x2c, 0x26, 0xb4, 0x6b, 0x68, 0xff, 0xc6, 0x8f,
     35 	0xf9, 0x9b, 0x45, 0x3c, 0x1d, 0x30, 0x41, 0x34,
     36 	0x13, 0x42, 0x2d, 0x70, 0x64, 0x83, 0xbf, 0xa0,
     37 	0xf9, 0x8a, 0x5e, 0x88, 0x62, 0x66, 0xe7, 0xae,
     38 };
     39 
     40 // sha256sum "baz" baa5a0964d3320fbc0c6a922140453c8513ea24ab8fd0577034804a967248096
     41 static const char digest_dummy_transform[32] = {
     42 	0xba, 0xa5, 0xa0, 0x96, 0x4d, 0x33, 0x20, 0xfb,
     43 	0xc0, 0xc6, 0xa9, 0x22, 0x14, 0x04, 0x53, 0xc8,
     44 	0x51, 0x3e, 0xa2, 0x4a, 0xb8, 0xfd, 0x05, 0x77, 
     45 	0x03, 0x48, 0x04, 0xa9, 0x67, 0x24, 0x80, 0x96
     46 };
     47 
     48 struct dummycrypto {
     49 	char *data; ///< Literal private key data.
     50 	size_t len; ///< Length of private key data.
     51 };
     52 
     53 void fill_random(char *buf, size_t len) {
     54 	int i;
     55 
     56 	for (i = 0; i < len; i++) {
     57 		*(buf+i) = (char)(rand()%255);
     58 	}
     59 }
     60 
     61 void keylock_xor(LQPrivKey *pk, const char *passphrase_digest) {
     62 	int i;
     63 	struct dummycrypto *o;
     64 
     65 	o = (struct dummycrypto*)pk->impl;
     66 	for (i = 0; i < LQ_PRIVKEY_LEN; i++) {
     67 		*((char*)o->data+i) ^= encrypt_dummy_transport[i];
     68 	}
     69 }
     70 
     71 int lq_privatekey_unlock(LQPrivKey *pk, const char *passphrase, size_t passphrase_len) {
     72 	char b;
     73 	char digest[LQ_DIGEST_LEN];
     74 
     75 	if (pk == NULL) {
     76 		return ERR_INIT;
     77 	}
     78 	if ((pk->key_state & LQ_KEY_LOCK) == 0) {
     79 		return ERR_NOOP;
     80 	}
     81 	lq_digest(passphrase, passphrase_len, digest);
     82 	keylock_xor(pk, digest);
     83 	b = LQ_KEY_LOCK;
     84 	pk->key_state &= ~b;
     85 	return 0;
     86 }
     87 
     88 int lq_privatekey_lock(LQPrivKey *pk, const char *passphrase, size_t passphrase_len) {
     89 	char digest[LQ_DIGEST_LEN];
     90 
     91 	if (pk == NULL) {
     92 		return ERR_INIT;
     93 	}
     94 	if ((pk->key_state & LQ_KEY_LOCK) > 0) {
     95 		return ERR_NOOP;
     96 	}
     97 	lq_digest(passphrase, passphrase_len, digest);
     98 	keylock_xor(pk, digest);
     99 	pk->key_state |= LQ_KEY_LOCK;
    100 	return ERR_OK;
    101 }
    102 
    103 LQPrivKey* lq_privatekey_new(const char *passphrase, size_t passphrase_len) {
    104 	LQPrivKey *pk;
    105 	struct dummycrypto *o;
    106 
    107 	o = lq_alloc(sizeof(struct dummycrypto));
    108 	pk = lq_alloc(sizeof(LQPrivKey));
    109 	o->data = lq_alloc(LQ_PRIVKEY_LEN);
    110 	fill_random(o->data, LQ_PRIVKEY_LEN);
    111 	o->len = LQ_PRIVKEY_LEN;
    112 	pk->impl = o;
    113 	pk->key_typ = 0;
    114 	pk->key_state = 0;
    115 	if (passphrase != NULL) {
    116 		lq_privatekey_lock(pk, passphrase, passphrase_len);
    117 	}
    118 	return pk;
    119 }
    120 
    121 size_t lq_privatekey_bytes(LQPrivKey *pk, char **out) {
    122 	struct dummycrypto *o;
    123 
    124 	o = (struct dummycrypto*)pk->impl;
    125 	*out = o->data;
    126 	return o->len;
    127 }
    128 
    129 LQPubKey* lq_publickey_new(const char *full) {
    130 	LQPubKey *pubk;
    131 	struct dummycrypto *o;
    132 
    133 	o = lq_alloc(sizeof(struct dummycrypto));
    134 	pubk = lq_alloc(sizeof(LQPubKey));
    135 	pubk->pk = 0;
    136 	o->data = lq_alloc(65);
    137 	lq_cpy(o->data, full, 65);
    138 	o->len = 65;
    139 	pubk->impl = o;
    140 	pubk->key_typ = 0;
    141 
    142 	return pubk;
    143 }
    144 
    145 LQPubKey* lq_publickey_from_privatekey(LQPrivKey *pk) {
    146 	int i;
    147 	int ii;
    148 	char *src;
    149 	char *dst;
    150 	LQPubKey *pubk;
    151 	struct dummycrypto *o;
    152 	struct dummycrypto *opk;
    153 
    154 	o = lq_alloc(sizeof(struct dummycrypto));
    155 	pubk = lq_alloc(sizeof(LQPubKey));
    156 	pubk->pk = pk;
    157 	o->data = lq_alloc(65);
    158 	o->len = 65;
    159 	opk = (struct dummycrypto*)pubk->pk->impl;
    160 	for (i = 0; i < 64; i++) {
    161 		ii = i % 32;
    162 		src = opk->data + ii;
    163 		dst = o->data + i + 1;
    164 		*dst = *src ^ pubkey_dummy_transform[i];
    165 	}
    166 	*((char*)o->data) = 0x04;
    167 	pubk->impl = o;
    168 	pubk->key_typ = 0;
    169 
    170 	return pubk;
    171 }
    172 
    173 size_t lq_publickey_bytes(LQPubKey *pubk, char **out) {
    174 	struct dummycrypto *o;
    175 
    176 	if (pubk->impl == NULL) {
    177 		*out = "";
    178 		return 0;
    179 	}
    180 	o = (struct dummycrypto*)pubk->impl;
    181 	*out = o->data;
    182 	return o->len;
    183 }
    184 
    185 LQSig* lq_privatekey_sign(LQPrivKey *pk, const char *msg, size_t msg_len, const char *salt) {
    186 	int i;
    187 	const char *src;
    188 	char *dst;
    189 	LQSig *sig;
    190 	struct dummycrypto *o;
    191 
    192 	if ((pk->key_state & LQ_KEY_LOCK) > 0) {
    193 		return NULL;
    194 	}
    195 	if (msg_len != LQ_DIGEST_LEN) {
    196 		return NULL;
    197 	}
    198 
    199 	sig = lq_alloc(sizeof(LQSig));
    200 	sig->pubkey = lq_publickey_from_privatekey(pk);
    201 
    202 	o = lq_alloc(sizeof(struct dummycrypto));
    203 	o->len = msg_len * 2 + 1;
    204 	o->data = lq_alloc(o->len);
    205 
    206 	for (i = 0; i < msg_len; i++) {
    207 		src = msg + i;
    208 		dst = o->data + i;
    209 		*dst = *src ^ sig_dummy_transform[i];
    210 		if (salt != NULL) {
    211 			*dst ^= *(salt + (i % LQ_SALT_LEN));
    212 		}
    213 
    214 		src += msg_len;
    215 		dst += msg_len;
    216 		*dst = *src ^ sig_dummy_transform[i + msg_len];
    217 		if (salt != NULL) {
    218 			*dst ^= *(salt + (i % LQ_SALT_LEN));
    219 		}
    220 	}
    221 
    222 	*(((char*)o->data) + o->len) = 0x2a;
    223 	sig->impl = o;
    224 
    225 	return sig;
    226 }
    227 
    228 LQSig* lq_signature_from_bytes(const char *sig_data, size_t sig_len, LQPubKey *pubkey) {
    229 	struct dummycrypto *o;
    230 	LQSig *sig;
    231 
    232 	if (sig_data == NULL) {
    233 		return NULL;
    234 	}
    235 	if (sig_len != 65) {
    236 		return NULL;
    237 	}
    238 	o = lq_alloc(sizeof(struct dummycrypto));
    239 	sig = lq_alloc(sizeof(LQSig));
    240 	o->data = lq_alloc(sig_len);
    241 	lq_cpy(o, sig_data, sig_len);
    242 	sig->impl = o;
    243 	sig->pubkey = pubkey;
    244 	return sig;
    245 }
    246 
    247 size_t lq_signature_bytes(LQSig *sig, char **out) {
    248 	struct dummycrypto *o;
    249 
    250 	if (sig->impl == NULL) {
    251 		*out = "";
    252 		return 0;
    253 	}
    254 	o = (struct dummycrypto*)sig->impl;
    255 	*out = o->data;
    256 	return o->len;
    257 }
    258 
    259 void lq_privatekey_free(LQPrivKey *pk) {
    260 	struct dummycrypto *o;
    261 
    262 	o = (struct dummycrypto *)pk->impl;
    263 	lq_free(o->data);
    264 	lq_free(o);
    265 	lq_free(pk);
    266 }
    267 
    268 void lq_publickey_free(LQPubKey *pubk) {
    269 	struct dummycrypto *o;
    270 
    271 	o = (struct dummycrypto *)pubk->impl;
    272 	lq_free(o->data);
    273 	lq_free(o);
    274 	lq_free(pubk);
    275 }
    276 
    277 void lq_signature_free(LQSig *sig) {
    278 	struct dummycrypto *o;
    279 
    280 	o = (struct dummycrypto *)sig->impl;
    281 	lq_publickey_free(sig->pubkey);
    282 	lq_free(o->data);
    283 	lq_free(o);
    284 	lq_free(sig);
    285 }
    286 
    287 int lq_digest(const char *in, size_t in_len, char *out) {
    288 	int i;
    289 	int ii;
    290 	lq_set(out, 0, LQ_DIGEST_LEN);
    291 
    292 	for (i = 0; i < in_len; i++) {
    293 		ii = i % LQ_DIGEST_LEN;
    294 		*(out + ii) = *(in + i) ^ digest_dummy_transform[ii];
    295 	}
    296 
    297 	return 0;
    298 }