Read about our upcoming Code of Conduct on this issue

Commit 101c6212 authored by g3gg0's avatar g3gg0
Browse files

io_crypt: non-functional changes only. replaced tabs with spaces, added...

io_crypt: non-functional changes only. replaced tabs with spaces, added copyringt notices, some comments

--HG--
branch : unified
parent e769afc11194
......@@ -5,7 +5,7 @@ This module hooks file I/O functions and adds an proprietary encryption mechanis
Works on 5D3 and may work on 600D and 7D.
:Author: g3gg0
:License: GPL
:License: GPL, BigDigits
:Summary: Encrypt files in real time
:Website: http://www.magiclantern.fm/forum/index.php?topic=9963.0
......
/*
* Copyright (C) 2013 Magic Lantern Team
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef MODULE
......
//#define TRACE_DISABLED
/*
* Copyright (C) 2013 Magic Lantern Team
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/* as some code portions (RSA) are copied from BIGDIGITS demo code, this notice is mandatory */
/******************** SHORT COPYRIGHT NOTICE**************************
This source code is part of the BigDigits multiple-precision
arithmetic library Version 2.3 originally written by David Ireland,
copyright (c) 2001-11 D.I. Management Services Pty Limited, all rights
reserved. It is provided "as is" with no warranties. You may use
this software under the terms of the full copyright notice
"bigdigitsCopyright.txt" that should have been included with this
library or can be obtained from <www.di-mgt.com.au/bigdigits.html>.
This notice must always be retained in any copy.
******************* END OF COPYRIGHT NOTICE***************************/
#ifdef MODULE
......@@ -53,7 +84,7 @@ size_t getFileSize(const char * filename, int *ret)
struct stat st;
stat(filename, &st);
*ret = st.st_size;
return 0;
}
......@@ -68,7 +99,7 @@ char *module_card_drive = "";
#include "crypt_rsa.h"
#include "bigd.h"
#include "bigdigits.h"
#include "bigdigits.h"
#define assert(x) do { if(!(x)){ beep(); NotifyBox(5000, "ASSERT: "#x ); return -1;} } while(0)
......@@ -77,23 +108,23 @@ extern uint32_t iocrypt_trace_ctx;
static bdigit_t small_primes[] = {
3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,
47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101,
103, 107, 109, 113,
127, 131, 137, 139, 149, 151, 157, 163, 167, 173,
179, 181, 191, 193, 197, 199, 211, 223, 227, 229,
233, 239, 241, 251, 257, 263, 269, 271, 277, 281,
283, 293, 307, 311, 313, 317, 331, 337, 347, 349,
353, 359, 367, 373, 379, 383, 389, 397, 401, 409,
419, 421, 431, 433, 439, 443, 449, 457, 461, 463,
467, 479, 487, 491, 499, 503, 509, 521, 523, 541,
547, 557, 563, 569, 571, 577, 587, 593, 599, 601,
607, 613, 617, 619, 631, 641, 643, 647, 653, 659,
661, 673, 677, 683, 691, 701, 709, 719, 727, 733,
739, 743, 751, 757, 761, 769, 773, 787, 797, 809,
811, 821, 823, 827, 829, 839, 853, 857, 859, 863,
877, 881, 883, 887, 907, 911, 919, 929, 937, 941,
947, 953, 967, 971, 977, 983, 991, 997,
3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,
47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101,
103, 107, 109, 113,
127, 131, 137, 139, 149, 151, 157, 163, 167, 173,
179, 181, 191, 193, 197, 199, 211, 223, 227, 229,
233, 239, 241, 251, 257, 263, 269, 271, 277, 281,
283, 293, 307, 311, 313, 317, 331, 337, 347, 349,
353, 359, 367, 373, 379, 383, 389, 397, 401, 409,
419, 421, 431, 433, 439, 443, 449, 457, 461, 463,
467, 479, 487, 491, 499, 503, 509, 521, 523, 541,
547, 557, 563, 569, 571, 577, 587, 593, 599, 601,
607, 613, 617, 619, 631, 641, 643, 647, 653, 659,
661, 673, 677, 683, 691, 701, 709, 719, 727, 733,
739, 743, 751, 757, 761, 769, 773, 787, 797, 809,
811, 821, 823, 827, 829, 839, 853, 857, 859, 863,
877, 881, 883, 887, 907, 911, 919, 929, 937, 941,
947, 953, 967, 971, 977, 983, 991, 997,
};
#define N_SMALL_PRIMES (sizeof(small_primes)/sizeof(bdigit_t))
......@@ -107,33 +138,33 @@ static int crypt_rsa_rand(unsigned char *bytes, size_t nbytes, const unsigned ch
rand_seed(((uint32_t *)seed)[pos]);
}
}
while(((uint32_t)bytes % 4) && nbytes)
{
{
uint32_t rn = 0;
rand_fill(&rn, 1);
*bytes++ = rn & 0xFF;
*bytes++ = rn & 0xFF;
nbytes--;
}
}
uint32_t words = (nbytes / 4);
uint32_t remain = (nbytes % 4);
rand_fill((uint32_t*)bytes, words);
for(uint32_t pos = 0; pos < remain; pos++)
{
for(uint32_t pos = 0; pos < remain; pos++)
{
uint32_t rn = 0;
rand_fill(&rn, 1);
bytes[words * 4 + pos] = (rn & 0xFF);
}
bytes[words * 4 + pos] = (rn & 0xFF);
}
return 0;
return 0;
}
int generateRSAPrime(BIGD p, size_t nbits, bdigit_t e, size_t ntests,
unsigned char *seed, size_t seedlen, BD_RANDFUNC randFunc)
unsigned char *seed, size_t seedlen, BD_RANDFUNC randFunc)
/* Create a prime p such that gcd(p-1, e) = 1.
Returns # prime tests carried out or -1 if failed.
Sets the TWO highest bits to ensure that the
......@@ -145,309 +176,309 @@ int generateRSAPrime(BIGD p, size_t nbits, bdigit_t e, size_t ntests,
Uses improvement in trial division from Menezes 4.51.
*/
{
BIGD u;
size_t i, j, iloop, maxloops, maxodd;
int done, overflow, failedtrial;
int count = 0;
bdigit_t r[N_SMALL_PRIMES];
/* Create a temp */
u = bdNew();
maxodd = nbits * 100;
maxloops = 5;
done = 0;
for (iloop = 0; !done && iloop < maxloops; iloop++)
{
/* Set candidate n0 as random odd number */
bdRandomSeeded(p, nbits, seed, seedlen, randFunc);
/* Set two highest and low bits */
bdSetBit(p, nbits - 1, 1);
bdSetBit(p, nbits - 2, 1);
bdSetBit(p, 0, 1);
/* To improve trial division, compute table R[q] = n0 mod q
for each odd prime q <= B
*/
for (i = 0; i < N_SMALL_PRIMES; i++)
{
r[i] = bdShortMod(u, p, small_primes[i]);
}
done = overflow = 0;
/* Try every odd number n0, n0+2, n0+4,... until we succeed */
for (j = 0; j < maxodd; j++, overflow = bdShortAdd(p, p, 2))
{
/* Check for overflow */
if (overflow)
break;
count++;
/* Each time 2 is added to the current candidate
update table R[q] = (R[q] + 2) mod q */
if (j > 0)
{
for (i = 0; i < N_SMALL_PRIMES; i++)
{
r[i] = (r[i] + 2) % small_primes[i];
}
}
/* Candidate passes the trial division stage if and only if
NONE of the R[q] values equal zero */
for (failedtrial = 0, i = 0; i < N_SMALL_PRIMES; i++)
{
if (r[i] == 0)
{
failedtrial = 1;
break;
}
}
if (failedtrial)
continue;
/* If p mod e = 1 then gcd(p, e) > 1, so try again */
bdShortMod(u, p, e);
if (bdShortCmp(u, 1) == 0)
continue;
/* Do expensive primality test */
if (bdRabinMiller(p, ntests))
{ /* Success! - we have a prime */
done = 1;
break;
}
}
}
/* Clear up */
bdFree(&u);
return (done ? count : -1);
BIGD u;
size_t i, j, iloop, maxloops, maxodd;
int done, overflow, failedtrial;
int count = 0;
bdigit_t r[N_SMALL_PRIMES];
/* Create a temp */
u = bdNew();
maxodd = nbits * 100;
maxloops = 5;
done = 0;
for (iloop = 0; !done && iloop < maxloops; iloop++)
{
/* Set candidate n0 as random odd number */
bdRandomSeeded(p, nbits, seed, seedlen, randFunc);
/* Set two highest and low bits */
bdSetBit(p, nbits - 1, 1);
bdSetBit(p, nbits - 2, 1);
bdSetBit(p, 0, 1);
/* To improve trial division, compute table R[q] = n0 mod q
for each odd prime q <= B
*/
for (i = 0; i < N_SMALL_PRIMES; i++)
{
r[i] = bdShortMod(u, p, small_primes[i]);
}
done = overflow = 0;
/* Try every odd number n0, n0+2, n0+4,... until we succeed */
for (j = 0; j < maxodd; j++, overflow = bdShortAdd(p, p, 2))
{
/* Check for overflow */
if (overflow)
break;
count++;
/* Each time 2 is added to the current candidate
update table R[q] = (R[q] + 2) mod q */
if (j > 0)
{
for (i = 0; i < N_SMALL_PRIMES; i++)
{
r[i] = (r[i] + 2) % small_primes[i];
}
}
/* Candidate passes the trial division stage if and only if
NONE of the R[q] values equal zero */
for (failedtrial = 0, i = 0; i < N_SMALL_PRIMES; i++)
{
if (r[i] == 0)
{
failedtrial = 1;
break;
}
}
if (failedtrial)
continue;
/* If p mod e = 1 then gcd(p, e) > 1, so try again */
bdShortMod(u, p, e);
if (bdShortCmp(u, 1) == 0)
continue;
/* Do expensive primality test */
if (bdRabinMiller(p, ntests))
{ /* Success! - we have a prime */
done = 1;
break;
}
}
}
/* Clear up */
bdFree(&u);
return (done ? count : -1);
}
int generateRSAKey(BIGD n, BIGD e, BIGD d, BIGD p, BIGD q, BIGD dP, BIGD dQ, BIGD qInv,
size_t nbits, bdigit_t ee, size_t ntests, unsigned char *seed, size_t seedlen,
BD_RANDFUNC randFunc, uint32_t *progress)
size_t nbits, bdigit_t ee, size_t ntests, unsigned char *seed, size_t seedlen,
BD_RANDFUNC randFunc, uint32_t *progress)
{
BIGD g, p1, q1, phi;
size_t np, nq;
unsigned char *myseed = NULL;
/* Initialise */
g = bdNew();
p1 = bdNew();
q1 = bdNew();
phi = bdNew();
/* We add an extra byte to the user-supplied seed */
myseed = (unsigned char*)malloc(seedlen + 1);
if (!myseed)
BIGD g, p1, q1, phi;
size_t np, nq;
unsigned char *myseed = NULL;
/* Initialise */
g = bdNew();
p1 = bdNew();
q1 = bdNew();
phi = bdNew();
/* We add an extra byte to the user-supplied seed */
myseed = (unsigned char*)malloc(seedlen + 1);
if (!myseed)
{
return -1;
}
memcpy(myseed, seed, seedlen);
memcpy(myseed, seed, seedlen);
/* Do (p, q) in two halves, approx equal */
nq = nbits / 2 ;
np = nbits - nq;
/* Do (p, q) in two halves, approx equal */
nq = nbits / 2 ;
np = nbits - nq;
/* Make sure seeds are slightly different for p and q */
/* Make sure seeds are slightly different for p and q */
*progress = 1;
myseed[seedlen] = rand();
generateRSAPrime(p, np, ee, ntests, myseed, seedlen+1, randFunc);
myseed[seedlen] = rand();
generateRSAPrime(p, np, ee, ntests, myseed, seedlen+1, randFunc);
*progress = 33;
myseed[seedlen] = rand();
generateRSAPrime(q, nq, ee, ntests, myseed, seedlen+1, randFunc);
myseed[seedlen] = rand();
generateRSAPrime(q, nq, ee, ntests, myseed, seedlen+1, randFunc);
*progress = 66;
/* Check that p != q (if so, RNG is faulty!) */
assert(!bdIsEqual(p, q));
generateRSAPrime(e, nq, ee, ntests, myseed, seedlen+1, randFunc);
/* Check that p != q (if so, RNG is faulty!) */
assert(!bdIsEqual(p, q));
generateRSAPrime(e, nq, ee, ntests, myseed, seedlen+1, randFunc);
*progress = 95;
/* If q > p swap p and q so p > q */
if (bdCompare(p, q) < 1)
{
bdSetEqual(g, p);
bdSetEqual(p, q);
bdSetEqual(q, g);
}
/* Calc p-1 and q-1 */
bdSetEqual(p1, p);
bdDecrement(p1);
bdSetEqual(q1, q);
bdDecrement(q1);
/* Check gcd(p-1, e) = 1 */
bdGcd(g, p1, e);
assert(bdShortCmp(g, 1) == 0);
bdGcd(g, q1, e);
assert(bdShortCmp(g, 1) == 0);
/* Compute n = pq */
bdMultiply(n, p, q);
/* Compute d = e^-1 mod (p-1)(q-1) */
bdMultiply(phi, p1, q1);
bdModInv(d, e, phi);
/* Check ed = 1 mod phi */
bdModMult(g, e, d, phi);
assert(bdShortCmp(g, 1) == 0);
/* Calculate CRT key values */
bdModInv(dP, e, p1);
bdModInv(dQ, e, q1);
bdModInv(qInv, q, p);
/* Clean up */
/* If q > p swap p and q so p > q */
if (bdCompare(p, q) < 1)
{
bdSetEqual(g, p);
bdSetEqual(p, q);
bdSetEqual(q, g);
}
/* Calc p-1 and q-1 */
bdSetEqual(p1, p);
bdDecrement(p1);
bdSetEqual(q1, q);
bdDecrement(q1);
/* Check gcd(p-1, e) = 1 */
bdGcd(g, p1, e);
assert(bdShortCmp(g, 1) == 0);
bdGcd(g, q1, e);
assert(bdShortCmp(g, 1) == 0);
/* Compute n = pq */
bdMultiply(n, p, q);
/* Compute d = e^-1 mod (p-1)(q-1) */
bdMultiply(phi, p1, q1);
bdModInv(d, e, phi);
/* Check ed = 1 mod phi */
bdModMult(g, e, d, phi);
assert(bdShortCmp(g, 1) == 0);
/* Calculate CRT key values */
bdModInv(dP, e, p1);
bdModInv(dQ, e, q1);
bdModInv(qInv, q, p);
/* Clean up */
free(myseed);
bdFree(&g);
bdFree(&p1);
bdFree(&q1);
bdFree(&phi);
bdFree(&g);
bdFree(&p1);
bdFree(&q1);
bdFree(&phi);
*progress = 98;
return 0;
return 0;
}
int crypt_rsa_generate(int nbits, t_crypt_key *priv_key, t_crypt_key *pub_key, uint32_t *progress)
{
int res = 0;
char *buffer = NULL;
unsigned char *tmp_buf = NULL;
BIGD n, e, d, p, q, dP, dQ, qInv;
BIGD source;
BIGD result;
/* Initialise */
p = bdNew();
q = bdNew();
n = bdNew();
e = bdNew();
d = bdNew();
dP = bdNew();
dQ = bdNew();
qInv = bdNew();
/* Create RSA key pair (n, e),(d, p, q, dP, dQ, qInv) */
int res = 0;
char *buffer = NULL;
unsigned char *tmp_buf = NULL;
BIGD n, e, d, p, q, dP, dQ, qInv;
BIGD source;
BIGD result;
/* Initialise */
p = bdNew();
q = bdNew();
n = bdNew();
e = bdNew();
d = bdNew();
dP = bdNew();
dQ = bdNew();
qInv = bdNew();
/* Create RSA key pair (n, e),(d, p, q, dP, dQ, qInv) */
char seed[256];
rand_fill(seed, sizeof(seed)/4);
res = generateRSAKey(n, e, d, p, q, dP, dQ, qInv, nbits+1, 3, 50, seed, sizeof(seed), crypt_rsa_rand, progress);
if(res != 0)
{
NotifyBox(5000, "Failed to generate RSA key!\n");
goto clean_up;
}
priv_key->id = 0x00;
pub_key->id = 0x00;
priv_key->name = strdup("new key");
pub_key->name = strdup("new key");
size_t nchars = bdConvToHex(n, NULL, 0);
buffer = malloc(nchars+1);
nchars = bdConvToHex(n, buffer, nchars+1);
priv_key->primefac = (char*)strdup((char *)buffer);
pub_key->primefac = (char*)strdup((char *)buffer);
free(buffer);
res = generateRSAKey(n, e, d, p, q, dP, dQ, qInv, nbits+1, 3, 50, seed, sizeof(seed), crypt_rsa_rand, progress);
if(res != 0)
{
NotifyBox(5000, "Failed to generate RSA key!\n");
goto clean_up;
}
priv_key->id = 0x00;
pub_key->id = 0x00;
priv_key->name = strdup("new key");
pub_key->name = strdup("new key");
size_t nchars = bdConvToHex(n, NULL, 0);
buffer = malloc(nchars+1);
nchars = bdConvToHex(n, buffer, nchars+1);
priv_key->primefac = (char*)strdup((char *)buffer);
pub_key->primefac = (char*)strdup((char *)buffer);
free(buffer);
nchars = bdConvToHex(d, NULL, 0);
buffer = malloc(nchars+1);
nchars = bdConvToHex(d, buffer, nchars+1);
priv_key->key = (char*)strdup((char *)buffer);
free(buffer);