mirror of
https://github.com/nh-server/mkey.git
synced 2026-08-15 20:20:06 -07:00
Merge remote-tracking branch 'upstream/master'
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2012 neimod
|
||||
Copyright (c) 2014 3DSGuy
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -0,0 +1,18 @@
|
||||
SOURCES = source source/polarssl
|
||||
|
||||
CFILES := $(foreach dir, $(SOURCES), $(wildcard $(dir)/*.c))
|
||||
CPPFILES := $(foreach dir, $(SOURCES), $(wildcard $(dir)/*.cpp))
|
||||
|
||||
OBJS = $(CFILES:.c=.o) $(CPPFILES:.cpp=.o)
|
||||
|
||||
LIBS = -static-libstdc++ -static
|
||||
CXXFLAGS = -Isource
|
||||
CFLAGS = -std=c11 -Wall -Wextra -Os -posix -Isource -D_GNU_SOURCE
|
||||
OUTPUT = mkey
|
||||
CC = gcc
|
||||
|
||||
main: $(OBJS)
|
||||
g++ -o $(OUTPUT) $(LIBS) $(OBJS)
|
||||
|
||||
clean:
|
||||
rm -f $(OUTPUT) $(OUTPUT).exe $(OBJS)
|
||||
@@ -5,6 +5,7 @@ Compatible with: 1.0-11.16 US,EU,JP,KR,TW,CN</br>
|
||||
- Attempt to unlock your parental controls from your 3DS's System Settings until it gives you an "Inquiry Number" (chose "I forgot" twice).
|
||||
- Next, read the rem comments in the "mkey.bat" script in a text editor for the remaining instructions, then run the same .bat script.
|
||||
- Your masterkey should be generated, which you can use to unlock your 3DS in System Settings.
|
||||
Note: A windows exe is also provided in the release tab. You simply launch it and follow directions.
|
||||
|
||||
mkey
|
||||
====
|
||||
|
||||
+2
-2
@@ -2,7 +2,7 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en-US">
|
||||
<head>
|
||||
<title>eiphax | mkey generator</title>
|
||||
<title>nintendo homebrew | mkey generator</title>
|
||||
<link rel="stylesheet" href="static/main.css" type="text/css">
|
||||
<link rel="shortcut icon" href="//eiphax.tech/favicon.ico" type="image/x-icon">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
|
||||
@@ -151,7 +151,7 @@
|
||||
There is currently no known way to derive/obtain the device ID through trivial/visible methods.<br>
|
||||
|
||||
<h4>Source code?</h4>
|
||||
You can get the code for the backend <a href="https://github.com/eip618/mkey">here</a>.
|
||||
You can get the code for the backend <a href="https://github.com/nh-server/mkey">here</a>.
|
||||
|
||||
<!-- y'all ever just kang? -->
|
||||
</div>
|
||||
|
||||
+155
@@ -0,0 +1,155 @@
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "ctr.h"
|
||||
|
||||
void ctr_set_iv( ctr_aes_context* ctx,
|
||||
u8 iv[16] )
|
||||
{
|
||||
memcpy(ctx->iv, iv, 16);
|
||||
}
|
||||
|
||||
void ctr_add_counter( ctr_aes_context* ctx,
|
||||
u32 carry )
|
||||
{
|
||||
u32 counter[4];
|
||||
u32 sum;
|
||||
int i;
|
||||
|
||||
for(i=0; i<4; i++)
|
||||
counter[i] = (ctx->ctr[i*4+0]<<24) | (ctx->ctr[i*4+1]<<16) | (ctx->ctr[i*4+2]<<8) | (ctx->ctr[i*4+3]<<0);
|
||||
|
||||
for(i=3; i>=0; i--)
|
||||
{
|
||||
sum = counter[i] + carry;
|
||||
|
||||
if (sum < counter[i])
|
||||
carry = 1;
|
||||
else
|
||||
carry = 0;
|
||||
|
||||
counter[i] = sum;
|
||||
}
|
||||
|
||||
for(i=0; i<4; i++)
|
||||
{
|
||||
ctx->ctr[i*4+0] = counter[i]>>24;
|
||||
ctx->ctr[i*4+1] = counter[i]>>16;
|
||||
ctx->ctr[i*4+2] = counter[i]>>8;
|
||||
ctx->ctr[i*4+3] = counter[i]>>0;
|
||||
}
|
||||
}
|
||||
|
||||
void ctr_set_counter( ctr_aes_context* ctx,
|
||||
u8 ctr[16] )
|
||||
{
|
||||
memcpy(ctx->ctr, ctr, 16);
|
||||
}
|
||||
|
||||
|
||||
void ctr_init_counter( ctr_aes_context* ctx,
|
||||
u8 key[16],
|
||||
u8 ctr[16] )
|
||||
{
|
||||
aes_setkey_enc(&ctx->aes, key, 128);
|
||||
ctr_set_counter(ctx, ctr);
|
||||
}
|
||||
|
||||
|
||||
void ctr_crypt_counter_block( ctr_aes_context* ctx,
|
||||
u8 input[16],
|
||||
u8 output[16] )
|
||||
{
|
||||
int i;
|
||||
u8 stream[16];
|
||||
|
||||
|
||||
aes_crypt_ecb(&ctx->aes, AES_ENCRYPT, ctx->ctr, stream);
|
||||
|
||||
|
||||
if (input)
|
||||
{
|
||||
for(i=0; i<16; i++)
|
||||
{
|
||||
output[i] = stream[i] ^ input[i];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(i=0; i<16; i++)
|
||||
output[i] = stream[i];
|
||||
}
|
||||
|
||||
ctr_add_counter(ctx, 1);
|
||||
}
|
||||
|
||||
|
||||
void ctr_crypt_counter( ctr_aes_context* ctx,
|
||||
u8* input,
|
||||
u8* output,
|
||||
u32 size )
|
||||
{
|
||||
u8 stream[16];
|
||||
u32 i;
|
||||
|
||||
while(size >= 16)
|
||||
{
|
||||
ctr_crypt_counter_block(ctx, input, output);
|
||||
|
||||
if (input)
|
||||
input += 16;
|
||||
if (output)
|
||||
output += 16;
|
||||
|
||||
size -= 16;
|
||||
}
|
||||
|
||||
if (size)
|
||||
{
|
||||
memset(stream, 0, 16);
|
||||
ctr_crypt_counter_block(ctx, stream, stream);
|
||||
|
||||
if (input)
|
||||
{
|
||||
for(i=0; i<size; i++)
|
||||
output[i] = input[i] ^ stream[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
memcpy(output, stream, size);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ctr_init_cbc_encrypt( ctr_aes_context* ctx,
|
||||
u8 key[16],
|
||||
u8 iv[16] )
|
||||
{
|
||||
aes_setkey_enc(&ctx->aes, key, 128);
|
||||
ctr_set_iv(ctx, iv);
|
||||
}
|
||||
|
||||
void ctr_init_cbc_decrypt( ctr_aes_context* ctx,
|
||||
u8 key[16],
|
||||
u8 iv[16] )
|
||||
{
|
||||
aes_setkey_dec(&ctx->aes, key, 128);
|
||||
ctr_set_iv(ctx, iv);
|
||||
}
|
||||
|
||||
void ctr_encrypt_cbc( ctr_aes_context* ctx,
|
||||
u8* input,
|
||||
u8* output,
|
||||
u32 size )
|
||||
{
|
||||
aes_crypt_cbc(&ctx->aes, AES_ENCRYPT, size, ctx->iv, input, output);
|
||||
}
|
||||
|
||||
void ctr_decrypt_cbc( ctr_aes_context* ctx,
|
||||
u8* input,
|
||||
u8* output,
|
||||
u32 size )
|
||||
{
|
||||
aes_crypt_cbc(&ctx->aes, AES_DECRYPT, size, ctx->iv, input, output);
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
#ifndef _CTR_H
|
||||
#define _CTR_H
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
#include "polarssl/aes.h"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
u8 ctr[16];
|
||||
u8 iv[16];
|
||||
aes_context aes;
|
||||
} ctr_aes_context;
|
||||
|
||||
void ctr_set_iv( ctr_aes_context* ctx,
|
||||
u8 iv[16] );
|
||||
|
||||
void ctr_add_counter( ctr_aes_context* ctx,
|
||||
u32 carry );
|
||||
|
||||
void ctr_set_counter( ctr_aes_context* ctx,
|
||||
u8 ctr[16] );
|
||||
|
||||
|
||||
void ctr_init_counter( ctr_aes_context* ctx,
|
||||
u8 key[16],
|
||||
u8 ctr[16] );
|
||||
|
||||
|
||||
void ctr_crypt_counter_block( ctr_aes_context* ctx,
|
||||
u8 input[16],
|
||||
u8 output[16] );
|
||||
|
||||
|
||||
void ctr_crypt_counter( ctr_aes_context* ctx,
|
||||
u8* input,
|
||||
u8* output,
|
||||
u32 size );
|
||||
|
||||
|
||||
void ctr_init_cbc_encrypt( ctr_aes_context* ctx,
|
||||
u8 key[16],
|
||||
u8 iv[16] );
|
||||
|
||||
void ctr_init_cbc_decrypt( ctr_aes_context* ctx,
|
||||
u8 key[16],
|
||||
u8 iv[16] );
|
||||
|
||||
void ctr_encrypt_cbc( ctr_aes_context* ctx,
|
||||
u8* input,
|
||||
u8* output,
|
||||
u32 size );
|
||||
|
||||
void ctr_decrypt_cbc( ctr_aes_context* ctx,
|
||||
u8* input,
|
||||
u8* output,
|
||||
u32 size );
|
||||
|
||||
#endif
|
||||
+172
@@ -0,0 +1,172 @@
|
||||
/*
|
||||
* mkey - parental controls master key generator for certain video game consoles
|
||||
* Copyright (C) 2015-2019, Daz Jones (Dazzozo) <daz@dazzozo.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "mkey.h"
|
||||
#include "types.h"
|
||||
|
||||
#include <time.h>
|
||||
|
||||
#define MAIN_ARGS_REQUIRED (2)
|
||||
|
||||
const char* main_format_devices(void);
|
||||
void main_set_data_path(mkey_ctx* ctx);
|
||||
|
||||
int main(int argc, const char* argv[])
|
||||
{
|
||||
mkey_ctx ctx;
|
||||
mkey_init(&ctx, false, NULL);
|
||||
|
||||
main_set_data_path(&ctx);
|
||||
|
||||
const char* default_device = "CTR"; //teh bias
|
||||
mkey_set_default_device(&ctx, default_device);
|
||||
printf("mkey (c) 2015-2016, SALT\n\n");
|
||||
|
||||
if(argc < 0) { //ignore
|
||||
|
||||
printf("Usage: mkey <inquiry> [-p device] [-m month] [-d day] [-a aux] [-v]\n");
|
||||
printf("inquiry 8 or 10 digit inquiry number\n");
|
||||
printf("month month displayed on device (system time)\n");
|
||||
printf("day day displayed on device (system time)\n");
|
||||
printf("aux auxiliary data (e.g. device ID)\n\n");
|
||||
|
||||
const char* devices = main_format_devices();
|
||||
printf("device device type: %s - %s by default\n", devices, default_device);
|
||||
printf("-v, --verbose enable debugging output\n");
|
||||
|
||||
free((void*)devices);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Optional parameters.
|
||||
argc -= MAIN_ARGS_REQUIRED;
|
||||
argv += MAIN_ARGS_REQUIRED;
|
||||
|
||||
time_t t = time(NULL);
|
||||
struct tm tm = *localtime(&t);
|
||||
|
||||
u8 month = tm.tm_mon + 1;
|
||||
u8 day = tm.tm_mday;
|
||||
const char* aux = NULL;
|
||||
|
||||
const char* device = "CTR";
|
||||
bool debug = false;
|
||||
char master_key[10] = {0};
|
||||
|
||||
//for(int i = 0; i < argc; i++) {
|
||||
//if(!strcmp(argv[i], "-p") || !strcmp(argv[i], "--device")) device = argv[i + 1];
|
||||
//if(!strcmp(argv[i], "-v") || !strcmp(argv[i], "--verbose")) debug = true;
|
||||
/*
|
||||
if(!strcmp(argv[i], "-m") || !strcmp(argv[i], "--month"))
|
||||
{
|
||||
if(!isnumeric(argv[i + 1])) {
|
||||
printf("Error: input values must be numbers.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
month = strtoul(argv[i + 1], 0, 10);
|
||||
}
|
||||
if(!strcmp(argv[i], "-d") || !strcmp(argv[i], "--day"))
|
||||
{
|
||||
if(!isnumeric(argv[i + 1])) {
|
||||
printf("Error: input values must be numbers.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
day = strtoul(argv[i + 1], 0, 10);
|
||||
}
|
||||
if(!strcmp(argv[i], "-a") || !strcmp(argv[i], "--aux")) aux = argv[i + 1];
|
||||
*/
|
||||
char day_3ds[20]={0};
|
||||
char mon_3ds[20]={0};
|
||||
char inq_3ds[20]={0};
|
||||
|
||||
printf("Please enter inquiry number (8 or 10 digits)\n");
|
||||
printf(">>>");
|
||||
scanf("%10s",inq_3ds);
|
||||
|
||||
if(strlen(inq_3ds) != 10 && strlen(inq_3ds) != 8) {
|
||||
printf("Error: inquiry number must be 8 or 10 number digits.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf("Trying using your PC's system time: m=%d, d=%d...\n", month, day);
|
||||
mkey_generate(&ctx, inq_3ds, month, day, aux, device, master_key);
|
||||
printf("\n----------------------------\nMaster key is %s.\n----------------------------\n\n", master_key);
|
||||
memset(master_key, 0, 10);
|
||||
|
||||
printf("If the above masterkey failed on your 3DS, enter your 3DS's actual month and day (look at top of system settings):\n");
|
||||
printf("Please enter month number (1-12)\n");
|
||||
printf("1=Jan, 2=Feb, 3=Mar, 4=Apr, 5=May, 6=Jun, 7=Jul, 8=Aug, 9=Sep, 10=Oct, 11=Nov, 12=Dec\n");
|
||||
printf(">>>");
|
||||
scanf("%2s",mon_3ds);
|
||||
month = strtoul(mon_3ds, 0, 10);
|
||||
printf("month %d entered\n\n", month);
|
||||
|
||||
printf("Please enter day of month (1-31)\n");
|
||||
printf(">>>");
|
||||
scanf("%2s",day_3ds);
|
||||
day = strtoul(day_3ds, 0, 10);
|
||||
printf("day %d entered\n", day);
|
||||
|
||||
if(debug) mkey_set_debug(&ctx, true);
|
||||
|
||||
//char master_key[10] = {0};
|
||||
int result = mkey_generate(&ctx, inq_3ds, month, day, aux, device, master_key);
|
||||
if(result < 0) {
|
||||
if(!debug) {
|
||||
printf("An error occurred. Your input values may be incorrect, or you may be missing a key file.\n");
|
||||
//printf("Use --verbose for more details.\n");
|
||||
}
|
||||
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf("\n----------------------------\nMaster key is %s.\n----------------------------\n\n", master_key);
|
||||
|
||||
printf("Press enter to quit");
|
||||
getchar();
|
||||
getchar();
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char* main_format_devices(void)
|
||||
{
|
||||
size_t size = (mkey_num_devices() * (sizeof(*mkey_devices) + 2)) + 1;
|
||||
char* buffer = malloc(size);
|
||||
memset(buffer, 0, size);
|
||||
|
||||
strcat(buffer, "{");
|
||||
|
||||
for(int i = 0; i < mkey_num_devices(); i++) {
|
||||
strcat(buffer, mkey_devices[i]);
|
||||
if(i != mkey_num_devices() - 1) strcat(buffer, ", ");
|
||||
}
|
||||
|
||||
strcat(buffer, "}");
|
||||
return buffer;
|
||||
}
|
||||
|
||||
#if __linux__
|
||||
#include <unistd.h>
|
||||
#elif _WIN32
|
||||
#include <Windows.h>
|
||||
#endif
|
||||
|
||||
// :-(
|
||||
void main_set_data_path(mkey_ctx* ctx)
|
||||
{
|
||||
mkey_set_data_path(ctx,"data");
|
||||
}
|
||||
+735
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* mkey - parental controls master key generator for certain video game consoles
|
||||
* Copyright (C) 2015-2019, Daz Jones (Dazzozo) <daz@dazzozo.com>
|
||||
* Copyright (C) 2015-2019, SALT
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "types.h"
|
||||
#include "utils.h"
|
||||
|
||||
typedef struct {
|
||||
u8 region;
|
||||
u8 version;
|
||||
u8 reserved[0xE];
|
||||
u8 ctr[0x10];
|
||||
u8 hmac_key[0x20];
|
||||
} mkey_data;
|
||||
|
||||
typedef struct {
|
||||
bool dbg;
|
||||
char data_path[MAX_PATH];
|
||||
bool data_path_set;
|
||||
const char* default_device;
|
||||
} mkey_ctx;
|
||||
|
||||
extern const char mkey_devices[][4];
|
||||
int mkey_num_devices(void);
|
||||
|
||||
void mkey_set_debug(mkey_ctx* ctx, bool enable);
|
||||
void mkey_set_data_path(mkey_ctx* ctx, const char* data_path);
|
||||
void mkey_set_default_device(mkey_ctx* ctx, const char* device);
|
||||
|
||||
void mkey_init(mkey_ctx* ctx, bool debug, const char* data_path);
|
||||
int mkey_generate(mkey_ctx* ctx, const char* inquiry_number, u8 month, u8 day, const char* aux, const char* device, char* master_key);
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,139 @@
|
||||
/**
|
||||
* \file aes.h
|
||||
*
|
||||
* Copyright (C) 2006-2010, Brainspark B.V.
|
||||
*
|
||||
* This file is part of PolarSSL (http://www.polarssl.org)
|
||||
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef POLARSSL_AES_H
|
||||
#define POLARSSL_AES_H
|
||||
|
||||
#define AES_ENCRYPT 1
|
||||
#define AES_DECRYPT 0
|
||||
|
||||
#define POLARSSL_ERR_AES_INVALID_KEY_LENGTH -0x0800
|
||||
#define POLARSSL_ERR_AES_INVALID_INPUT_LENGTH -0x0810
|
||||
|
||||
/**
|
||||
* \brief AES context structure
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
int nr; /*!< number of rounds */
|
||||
unsigned long *rk; /*!< AES round keys */
|
||||
unsigned long buf[68]; /*!< unaligned data */
|
||||
}
|
||||
aes_context;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \brief AES key schedule (encryption)
|
||||
*
|
||||
* \param ctx AES context to be initialized
|
||||
* \param key encryption key
|
||||
* \param keysize must be 128, 192 or 256
|
||||
*
|
||||
* \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
|
||||
*/
|
||||
int aes_setkey_enc( aes_context *ctx, const unsigned char *key, int keysize );
|
||||
|
||||
/**
|
||||
* \brief AES key schedule (decryption)
|
||||
*
|
||||
* \param ctx AES context to be initialized
|
||||
* \param key decryption key
|
||||
* \param keysize must be 128, 192 or 256
|
||||
*
|
||||
* \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
|
||||
*/
|
||||
int aes_setkey_dec( aes_context *ctx, const unsigned char *key, int keysize );
|
||||
|
||||
/**
|
||||
* \brief AES-ECB block encryption/decryption
|
||||
*
|
||||
* \param ctx AES context
|
||||
* \param mode AES_ENCRYPT or AES_DECRYPT
|
||||
* \param input 16-byte input block
|
||||
* \param output 16-byte output block
|
||||
*
|
||||
* \return 0 if successful
|
||||
*/
|
||||
int aes_crypt_ecb( aes_context *ctx,
|
||||
int mode,
|
||||
const unsigned char input[16],
|
||||
unsigned char output[16] );
|
||||
|
||||
/**
|
||||
* \brief AES-CBC buffer encryption/decryption
|
||||
* Length should be a multiple of the block
|
||||
* size (16 bytes)
|
||||
*
|
||||
* \param ctx AES context
|
||||
* \param mode AES_ENCRYPT or AES_DECRYPT
|
||||
* \param length length of the input data
|
||||
* \param iv initialization vector (updated after use)
|
||||
* \param input buffer holding the input data
|
||||
* \param output buffer holding the output data
|
||||
*
|
||||
* \return 0 if successful, or POLARSSL_ERR_AES_INVALID_INPUT_LENGTH
|
||||
*/
|
||||
int aes_crypt_cbc( aes_context *ctx,
|
||||
int mode,
|
||||
int length,
|
||||
unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output );
|
||||
|
||||
/**
|
||||
* \brief AES-CFB128 buffer encryption/decryption.
|
||||
*
|
||||
* \param ctx AES context
|
||||
* \param mode AES_ENCRYPT or AES_DECRYPT
|
||||
* \param length length of the input data
|
||||
* \param iv_off offset in IV (updated after use)
|
||||
* \param iv initialization vector (updated after use)
|
||||
* \param input buffer holding the input data
|
||||
* \param output buffer holding the output data
|
||||
*
|
||||
* \return 0 if successful
|
||||
*/
|
||||
int aes_crypt_cfb128( aes_context *ctx,
|
||||
int mode,
|
||||
int length,
|
||||
int *iv_off,
|
||||
unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output );
|
||||
|
||||
/**
|
||||
* \brief Checkup routine
|
||||
*
|
||||
* \return 0 if successful, or 1 if the test failed
|
||||
*/
|
||||
int aes_self_test( int verbose );
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* aes.h */
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,336 @@
|
||||
/**
|
||||
* \file config.h
|
||||
*
|
||||
* Copyright (C) 2006-2010, Brainspark B.V.
|
||||
*
|
||||
* This file is part of PolarSSL (http://www.polarssl.org)
|
||||
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* This set of compile-time options may be used to enable
|
||||
* or disable features selectively, and reduce the global
|
||||
* memory footprint.
|
||||
*/
|
||||
#ifndef POLARSSL_CONFIG_H
|
||||
#define POLARSSL_CONFIG_H
|
||||
|
||||
#ifndef _CRT_SECURE_NO_DEPRECATE
|
||||
#define _CRT_SECURE_NO_DEPRECATE 1
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Uncomment if native integers are 8-bit wide.
|
||||
*
|
||||
#define POLARSSL_HAVE_INT8
|
||||
*/
|
||||
|
||||
/*
|
||||
* Uncomment if native integers are 16-bit wide.
|
||||
*
|
||||
#define POLARSSL_HAVE_INT16
|
||||
*/
|
||||
|
||||
/*
|
||||
* Uncomment if the compiler supports long long.
|
||||
*
|
||||
#define POLARSSL_HAVE_LONGLONG
|
||||
*/
|
||||
|
||||
/*
|
||||
* Uncomment to enable the use of assembly code.
|
||||
*
|
||||
* Requires support for asm() in compiler.
|
||||
*
|
||||
* Used in:
|
||||
* library/timing.c
|
||||
* library/padlock.c
|
||||
* include/polarssl/bn_mul.h
|
||||
*
|
||||
*/
|
||||
//#define POLARSSL_HAVE_ASM
|
||||
|
||||
/*
|
||||
* Uncomment if the CPU supports SSE2 (IA-32 specific).
|
||||
*
|
||||
#define POLARSSL_HAVE_SSE2
|
||||
*/
|
||||
|
||||
/*
|
||||
* Enable all SSL/TLS debugging messages.
|
||||
*/
|
||||
#define POLARSSL_DEBUG_MSG
|
||||
|
||||
/*
|
||||
* Enable the checkup functions (*_self_test).
|
||||
*/
|
||||
//#define POLARSSL_SELF_TEST
|
||||
|
||||
/*
|
||||
* Enable run-time version information functions
|
||||
*/
|
||||
#define POLARSSL_VERSION_C
|
||||
|
||||
/*
|
||||
* Enable the prime-number generation code.
|
||||
*/
|
||||
#define POLARSSL_GENPRIME
|
||||
|
||||
/*
|
||||
* Uncomment this macro to store the AES tables in ROM.
|
||||
*
|
||||
#define POLARSSL_AES_ROM_TABLES
|
||||
*/
|
||||
|
||||
/*
|
||||
* Module: library/aes.c
|
||||
* Caller: library/ssl_tls.c
|
||||
*
|
||||
* This module enables the following ciphersuites:
|
||||
* SSL_RSA_AES_128_SHA
|
||||
* SSL_RSA_AES_256_SHA
|
||||
* SSL_EDH_RSA_AES_256_SHA
|
||||
*/
|
||||
#define POLARSSL_AES_C
|
||||
|
||||
/*
|
||||
* Module: library/arc4.c
|
||||
* Caller: library/ssl_tls.c
|
||||
*
|
||||
* This module enables the following ciphersuites:
|
||||
* SSL_RSA_RC4_128_MD5
|
||||
* SSL_RSA_RC4_128_SHA
|
||||
*/
|
||||
#define POLARSSL_ARC4_C
|
||||
|
||||
/*
|
||||
* Module: library/base64.c
|
||||
* Caller: library/x509parse.c
|
||||
*
|
||||
* This module is required for X.509 support.
|
||||
*/
|
||||
#define POLARSSL_BASE64_C
|
||||
|
||||
/*
|
||||
* Module: library/bignum.c
|
||||
* Caller: library/dhm.c
|
||||
* library/rsa.c
|
||||
* library/ssl_tls.c
|
||||
* library/x509parse.c
|
||||
*
|
||||
* This module is required for RSA and DHM support.
|
||||
*/
|
||||
#define POLARSSL_BIGNUM_C
|
||||
|
||||
/*
|
||||
* Module: library/camellia.c
|
||||
* Caller: library/ssl_tls.c
|
||||
*
|
||||
* This module enabled the following cipher suites:
|
||||
* SSL_RSA_CAMELLIA_128_SHA
|
||||
* SSL_RSA_CAMELLIA_256_SHA
|
||||
* SSL_EDH_RSA_CAMELLIA_256_SHA
|
||||
*/
|
||||
#define POLARSSL_CAMELLIA_C
|
||||
|
||||
/*
|
||||
* Module: library/certs.c
|
||||
* Caller:
|
||||
*
|
||||
* This module is used for testing (ssl_client/server).
|
||||
*/
|
||||
#define POLARSSL_CERTS_C
|
||||
|
||||
/*
|
||||
* Module: library/debug.c
|
||||
* Caller: library/ssl_cli.c
|
||||
* library/ssl_srv.c
|
||||
* library/ssl_tls.c
|
||||
*
|
||||
* This module provides debugging functions.
|
||||
*/
|
||||
#define POLARSSL_DEBUG_C
|
||||
|
||||
/*
|
||||
* Module: library/des.c
|
||||
* Caller: library/ssl_tls.c
|
||||
*
|
||||
* This module enables the following ciphersuites:
|
||||
* SSL_RSA_DES_168_SHA
|
||||
* SSL_EDH_RSA_DES_168_SHA
|
||||
*/
|
||||
#define POLARSSL_DES_C
|
||||
|
||||
/*
|
||||
* Module: library/dhm.c
|
||||
* Caller: library/ssl_cli.c
|
||||
* library/ssl_srv.c
|
||||
*
|
||||
* This module enables the following ciphersuites:
|
||||
* SSL_EDH_RSA_DES_168_SHA
|
||||
* SSL_EDH_RSA_AES_256_SHA
|
||||
* SSL_EDH_RSA_CAMELLIA_256_SHA
|
||||
*/
|
||||
#define POLARSSL_DHM_C
|
||||
|
||||
/*
|
||||
* Module: library/havege.c
|
||||
* Caller:
|
||||
*
|
||||
* This module enables the HAVEGE random number generator.
|
||||
*/
|
||||
#define POLARSSL_HAVEGE_C
|
||||
|
||||
/*
|
||||
* Module: library/md2.c
|
||||
* Caller: library/x509parse.c
|
||||
*
|
||||
* Uncomment to enable support for (rare) MD2-signed X.509 certs.
|
||||
*
|
||||
#define POLARSSL_MD2_C
|
||||
*/
|
||||
|
||||
/*
|
||||
* Module: library/md4.c
|
||||
* Caller: library/x509parse.c
|
||||
*
|
||||
* Uncomment to enable support for (rare) MD4-signed X.509 certs.
|
||||
*
|
||||
#define POLARSSL_MD4_C
|
||||
*/
|
||||
|
||||
/*
|
||||
* Module: library/md5.c
|
||||
* Caller: library/ssl_tls.c
|
||||
* library/x509parse.c
|
||||
*
|
||||
* This module is required for SSL/TLS and X.509.
|
||||
*/
|
||||
#define POLARSSL_MD5_C
|
||||
|
||||
/*
|
||||
* Module: library/net.c
|
||||
* Caller:
|
||||
*
|
||||
* This module provides TCP/IP networking routines.
|
||||
*/
|
||||
#define POLARSSL_NET_C
|
||||
|
||||
/*
|
||||
* Module: library/padlock.c
|
||||
* Caller: library/aes.c
|
||||
*
|
||||
* This modules adds support for the VIA PadLock on x86.
|
||||
*/
|
||||
#define POLARSSL_PADLOCK_C
|
||||
|
||||
/*
|
||||
* Module: library/rsa.c
|
||||
* Caller: library/ssl_cli.c
|
||||
* library/ssl_srv.c
|
||||
* library/ssl_tls.c
|
||||
* library/x509.c
|
||||
*
|
||||
* This module is required for SSL/TLS and MD5-signed certificates.
|
||||
*/
|
||||
#define POLARSSL_RSA_C
|
||||
|
||||
/*
|
||||
* Module: library/sha1.c
|
||||
* Caller: library/ssl_cli.c
|
||||
* library/ssl_srv.c
|
||||
* library/ssl_tls.c
|
||||
* library/x509parse.c
|
||||
*
|
||||
* This module is required for SSL/TLS and SHA1-signed certificates.
|
||||
*/
|
||||
#define POLARSSL_SHA1_C
|
||||
|
||||
/*
|
||||
* Module: library/sha2.c
|
||||
* Caller:
|
||||
*
|
||||
* This module adds support for SHA-224 and SHA-256.
|
||||
*/
|
||||
#define POLARSSL_SHA2_C
|
||||
|
||||
/*
|
||||
* Module: library/sha4.c
|
||||
* Caller:
|
||||
*
|
||||
* This module adds support for SHA-384 and SHA-512.
|
||||
*/
|
||||
#define POLARSSL_SHA4_C
|
||||
|
||||
/*
|
||||
* Module: library/ssl_cli.c
|
||||
* Caller:
|
||||
*
|
||||
* This module is required for SSL/TLS client support.
|
||||
*/
|
||||
#define POLARSSL_SSL_CLI_C
|
||||
|
||||
/*
|
||||
* Module: library/ssl_srv.c
|
||||
* Caller:
|
||||
*
|
||||
* This module is required for SSL/TLS server support.
|
||||
*/
|
||||
#define POLARSSL_SSL_SRV_C
|
||||
|
||||
/*
|
||||
* Module: library/ssl_tls.c
|
||||
* Caller: library/ssl_cli.c
|
||||
* library/ssl_srv.c
|
||||
*
|
||||
* This module is required for SSL/TLS.
|
||||
*/
|
||||
#define POLARSSL_SSL_TLS_C
|
||||
|
||||
/*
|
||||
* Module: library/timing.c
|
||||
* Caller: library/havege.c
|
||||
*
|
||||
* This module is used by the HAVEGE random number generator.
|
||||
*/
|
||||
#define POLARSSL_TIMING_C
|
||||
|
||||
/*
|
||||
* Module: library/x509parse.c
|
||||
* Caller: library/ssl_cli.c
|
||||
* library/ssl_srv.c
|
||||
* library/ssl_tls.c
|
||||
*
|
||||
* This module is required for X.509 certificate parsing.
|
||||
*/
|
||||
#define POLARSSL_X509_PARSE_C
|
||||
|
||||
/*
|
||||
* Module: library/x509_write.c
|
||||
* Caller:
|
||||
*
|
||||
* This module is required for X.509 certificate writing.
|
||||
*/
|
||||
#define POLARSSL_X509_WRITE_C
|
||||
|
||||
/*
|
||||
* Module: library/xtea.c
|
||||
* Caller:
|
||||
*/
|
||||
#define POLARSSL_XTEA_C
|
||||
|
||||
#endif /* config.h */
|
||||
@@ -0,0 +1,98 @@
|
||||
/**
|
||||
* \file padlock.h
|
||||
*
|
||||
* Copyright (C) 2006-2010, Brainspark B.V.
|
||||
*
|
||||
* This file is part of PolarSSL (http://www.polarssl.org)
|
||||
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef POLARSSL_PADLOCK_H
|
||||
#define POLARSSL_PADLOCK_H
|
||||
|
||||
#include "polarssl/aes.h"
|
||||
|
||||
#if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && defined(__i386__)
|
||||
|
||||
#ifndef POLARSSL_HAVE_X86
|
||||
#define POLARSSL_HAVE_X86
|
||||
#endif
|
||||
|
||||
#define PADLOCK_RNG 0x000C
|
||||
#define PADLOCK_ACE 0x00C0
|
||||
#define PADLOCK_PHE 0x0C00
|
||||
#define PADLOCK_PMM 0x3000
|
||||
|
||||
#define PADLOCK_ALIGN16(x) (unsigned long *) (16 + ((long) x & ~15))
|
||||
|
||||
#define POLARSSL_ERR_PADLOCK_DATA_MISALIGNED -0x08E0
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \brief PadLock detection routine
|
||||
*
|
||||
* \param The feature to detect
|
||||
*
|
||||
* \return 1 if CPU has support for the feature, 0 otherwise
|
||||
*/
|
||||
int padlock_supports( int feature );
|
||||
|
||||
/**
|
||||
* \brief PadLock AES-ECB block en(de)cryption
|
||||
*
|
||||
* \param ctx AES context
|
||||
* \param mode AES_ENCRYPT or AES_DECRYPT
|
||||
* \param input 16-byte input block
|
||||
* \param output 16-byte output block
|
||||
*
|
||||
* \return 0 if success, 1 if operation failed
|
||||
*/
|
||||
int padlock_xcryptecb( aes_context *ctx,
|
||||
int mode,
|
||||
const unsigned char input[16],
|
||||
unsigned char output[16] );
|
||||
|
||||
/**
|
||||
* \brief PadLock AES-CBC buffer en(de)cryption
|
||||
*
|
||||
* \param ctx AES context
|
||||
* \param mode AES_ENCRYPT or AES_DECRYPT
|
||||
* \param length length of the input data
|
||||
* \param iv initialization vector (updated after use)
|
||||
* \param input buffer holding the input data
|
||||
* \param output buffer holding the output data
|
||||
*
|
||||
* \return 0 if success, 1 if operation failed
|
||||
*/
|
||||
int padlock_xcryptcbc( aes_context *ctx,
|
||||
int mode,
|
||||
int length,
|
||||
unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output );
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* HAVE_X86 */
|
||||
|
||||
#endif /* padlock.h */
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,353 @@
|
||||
/**
|
||||
* \file rsa.h
|
||||
*
|
||||
* Copyright (C) 2006-2010, Brainspark B.V.
|
||||
*
|
||||
* This file is part of PolarSSL (http://www.polarssl.org)
|
||||
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef POLARSSL_RSA_H
|
||||
#define POLARSSL_RSA_H
|
||||
|
||||
#include "polarssl/bignum.h"
|
||||
|
||||
/*
|
||||
* RSA Error codes
|
||||
*/
|
||||
#define POLARSSL_ERR_RSA_BAD_INPUT_DATA -0x0400
|
||||
#define POLARSSL_ERR_RSA_INVALID_PADDING -0x0410
|
||||
#define POLARSSL_ERR_RSA_KEY_GEN_FAILED -0x0420
|
||||
#define POLARSSL_ERR_RSA_KEY_CHECK_FAILED -0x0430
|
||||
#define POLARSSL_ERR_RSA_PUBLIC_FAILED -0x0440
|
||||
#define POLARSSL_ERR_RSA_PRIVATE_FAILED -0x0450
|
||||
#define POLARSSL_ERR_RSA_VERIFY_FAILED -0x0460
|
||||
#define POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE -0x0470
|
||||
#define POLARSSL_ERR_RSA_RNG_FAILED -0x0480
|
||||
|
||||
/*
|
||||
* PKCS#1 constants
|
||||
*/
|
||||
#define SIG_RSA_RAW 0
|
||||
#define SIG_RSA_MD2 2
|
||||
#define SIG_RSA_MD4 3
|
||||
#define SIG_RSA_MD5 4
|
||||
#define SIG_RSA_SHA1 5
|
||||
#define SIG_RSA_SHA224 14
|
||||
#define SIG_RSA_SHA256 11
|
||||
#define SIG_RSA_SHA384 12
|
||||
#define SIG_RSA_SHA512 13
|
||||
|
||||
#define RSA_PUBLIC 0
|
||||
#define RSA_PRIVATE 1
|
||||
|
||||
#define RSA_PKCS_V15 0
|
||||
#define RSA_PKCS_V21 1
|
||||
|
||||
#define RSA_SIGN 1
|
||||
#define RSA_CRYPT 2
|
||||
|
||||
#define ASN1_STR_CONSTRUCTED_SEQUENCE "\x30"
|
||||
#define ASN1_STR_NULL "\x05"
|
||||
#define ASN1_STR_OID "\x06"
|
||||
#define ASN1_STR_OCTET_STRING "\x04"
|
||||
|
||||
#define OID_DIGEST_ALG_MDX "\x2A\x86\x48\x86\xF7\x0D\x02\x00"
|
||||
#define OID_HASH_ALG_SHA1 "\x2b\x0e\x03\x02\x1a"
|
||||
#define OID_HASH_ALG_SHA2X "\x60\x86\x48\x01\x65\x03\x04\x02\x00"
|
||||
|
||||
#define OID_ISO_MEMBER_BODIES "\x2a"
|
||||
#define OID_ISO_IDENTIFIED_ORG "\x2b"
|
||||
|
||||
/*
|
||||
* ISO Member bodies OID parts
|
||||
*/
|
||||
#define OID_COUNTRY_US "\x86\x48"
|
||||
#define OID_RSA_DATA_SECURITY "\x86\xf7\x0d"
|
||||
|
||||
/*
|
||||
* ISO Identified organization OID parts
|
||||
*/
|
||||
#define OID_OIW_SECSIG_SHA1 "\x0e\x03\x02\x1a"
|
||||
|
||||
/*
|
||||
* DigestInfo ::= SEQUENCE {
|
||||
* digestAlgorithm DigestAlgorithmIdentifier,
|
||||
* digest Digest }
|
||||
*
|
||||
* DigestAlgorithmIdentifier ::= AlgorithmIdentifier
|
||||
*
|
||||
* Digest ::= OCTET STRING
|
||||
*/
|
||||
#define ASN1_HASH_MDX \
|
||||
( \
|
||||
ASN1_STR_CONSTRUCTED_SEQUENCE "\x20" \
|
||||
ASN1_STR_CONSTRUCTED_SEQUENCE "\x0C" \
|
||||
ASN1_STR_OID "\x08" \
|
||||
OID_DIGEST_ALG_MDX \
|
||||
ASN1_STR_NULL "\x00" \
|
||||
ASN1_STR_OCTET_STRING "\x10" \
|
||||
)
|
||||
|
||||
#define ASN1_HASH_SHA1 \
|
||||
ASN1_STR_CONSTRUCTED_SEQUENCE "\x21" \
|
||||
ASN1_STR_CONSTRUCTED_SEQUENCE "\x09" \
|
||||
ASN1_STR_OID "\x05" \
|
||||
OID_HASH_ALG_SHA1 \
|
||||
ASN1_STR_NULL "\x00" \
|
||||
ASN1_STR_OCTET_STRING "\x14"
|
||||
|
||||
#define ASN1_HASH_SHA2X \
|
||||
ASN1_STR_CONSTRUCTED_SEQUENCE "\x11" \
|
||||
ASN1_STR_CONSTRUCTED_SEQUENCE "\x0d" \
|
||||
ASN1_STR_OID "\x09" \
|
||||
OID_HASH_ALG_SHA2X \
|
||||
ASN1_STR_NULL "\x00" \
|
||||
ASN1_STR_OCTET_STRING "\x00"
|
||||
|
||||
/**
|
||||
* \brief RSA context structure
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
int ver; /*!< always 0 */
|
||||
int len; /*!< size(N) in chars */
|
||||
|
||||
mpi N; /*!< public modulus */
|
||||
mpi E; /*!< public exponent */
|
||||
|
||||
mpi D; /*!< private exponent */
|
||||
mpi P; /*!< 1st prime factor */
|
||||
mpi Q; /*!< 2nd prime factor */
|
||||
mpi DP; /*!< D % (P - 1) */
|
||||
mpi DQ; /*!< D % (Q - 1) */
|
||||
mpi QP; /*!< 1 / (Q % P) */
|
||||
|
||||
mpi RN; /*!< cached R^2 mod N */
|
||||
mpi RP; /*!< cached R^2 mod P */
|
||||
mpi RQ; /*!< cached R^2 mod Q */
|
||||
|
||||
int padding; /*!< 1.5 or OAEP/PSS */
|
||||
int hash_id; /*!< hash identifier */
|
||||
}
|
||||
rsa_context;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \brief Initialize an RSA context
|
||||
*
|
||||
* \param ctx RSA context to be initialized
|
||||
* \param padding RSA_PKCS_V15 or RSA_PKCS_V21
|
||||
* \param hash_id RSA_PKCS_V21 hash identifier
|
||||
*
|
||||
* \note The hash_id parameter is actually ignored
|
||||
* when using RSA_PKCS_V15 padding.
|
||||
*
|
||||
* \note Currently, RSA_PKCS_V21 padding
|
||||
* is not supported.
|
||||
*/
|
||||
void rsa_init( rsa_context *ctx,
|
||||
int padding,
|
||||
int hash_id);
|
||||
|
||||
/**
|
||||
* \brief Generate an RSA keypair
|
||||
*
|
||||
* \param ctx RSA context that will hold the key
|
||||
* \param f_rng RNG function
|
||||
* \param p_rng RNG parameter
|
||||
* \param nbits size of the public key in bits
|
||||
* \param exponent public exponent (e.g., 65537)
|
||||
*
|
||||
* \note rsa_init() must be called beforehand to setup
|
||||
* the RSA context.
|
||||
*
|
||||
* \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
|
||||
*/
|
||||
int rsa_gen_key( rsa_context *ctx,
|
||||
int (*f_rng)(void *),
|
||||
void *p_rng,
|
||||
int nbits, int exponent );
|
||||
|
||||
/**
|
||||
* \brief Check a public RSA key
|
||||
*
|
||||
* \param ctx RSA context to be checked
|
||||
*
|
||||
* \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
|
||||
*/
|
||||
int rsa_check_pubkey( const rsa_context *ctx );
|
||||
|
||||
/**
|
||||
* \brief Check a private RSA key
|
||||
*
|
||||
* \param ctx RSA context to be checked
|
||||
*
|
||||
* \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
|
||||
*/
|
||||
int rsa_check_privkey( const rsa_context *ctx );
|
||||
|
||||
/**
|
||||
* \brief Do an RSA public key operation
|
||||
*
|
||||
* \param ctx RSA context
|
||||
* \param input input buffer
|
||||
* \param output output buffer
|
||||
*
|
||||
* \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
|
||||
*
|
||||
* \note This function does NOT take care of message
|
||||
* padding. Also, be sure to set input[0] = 0 or assure that
|
||||
* input is smaller than N.
|
||||
*
|
||||
* \note The input and output buffers must be large
|
||||
* enough (eg. 128 bytes if RSA-1024 is used).
|
||||
*/
|
||||
int rsa_public( rsa_context *ctx,
|
||||
const unsigned char *input,
|
||||
unsigned char *output );
|
||||
|
||||
/**
|
||||
* \brief Do an RSA private key operation
|
||||
*
|
||||
* \param ctx RSA context
|
||||
* \param input input buffer
|
||||
* \param output output buffer
|
||||
*
|
||||
* \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
|
||||
*
|
||||
* \note The input and output buffers must be large
|
||||
* enough (eg. 128 bytes if RSA-1024 is used).
|
||||
*/
|
||||
int rsa_private( rsa_context *ctx,
|
||||
const unsigned char *input,
|
||||
unsigned char *output );
|
||||
|
||||
/**
|
||||
* \brief Add the message padding, then do an RSA operation
|
||||
*
|
||||
* \param ctx RSA context
|
||||
* \param f_rng RNG function
|
||||
* \param p_rng RNG parameter
|
||||
* \param mode RSA_PUBLIC or RSA_PRIVATE
|
||||
* \param ilen contains the plaintext length
|
||||
* \param input buffer holding the data to be encrypted
|
||||
* \param output buffer that will hold the ciphertext
|
||||
*
|
||||
* \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
|
||||
*
|
||||
* \note The output buffer must be as large as the size
|
||||
* of ctx->N (eg. 128 bytes if RSA-1024 is used).
|
||||
*/
|
||||
int rsa_pkcs1_encrypt( rsa_context *ctx,
|
||||
int (*f_rng)(void *),
|
||||
void *p_rng,
|
||||
int mode, int ilen,
|
||||
const unsigned char *input,
|
||||
unsigned char *output );
|
||||
|
||||
/**
|
||||
* \brief Do an RSA operation, then remove the message padding
|
||||
*
|
||||
* \param ctx RSA context
|
||||
* \param mode RSA_PUBLIC or RSA_PRIVATE
|
||||
* \param input buffer holding the encrypted data
|
||||
* \param output buffer that will hold the plaintext
|
||||
* \param olen will contain the plaintext length
|
||||
* \param output_max_len maximum length of the output buffer
|
||||
*
|
||||
* \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
|
||||
*
|
||||
* \note The output buffer must be as large as the size
|
||||
* of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
|
||||
* an error is thrown.
|
||||
*/
|
||||
int rsa_pkcs1_decrypt( rsa_context *ctx,
|
||||
int mode, int *olen,
|
||||
const unsigned char *input,
|
||||
unsigned char *output,
|
||||
int output_max_len );
|
||||
|
||||
/**
|
||||
* \brief Do a private RSA to sign a message digest
|
||||
*
|
||||
* \param ctx RSA context
|
||||
* \param mode RSA_PUBLIC or RSA_PRIVATE
|
||||
* \param hash_id SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512}
|
||||
* \param hashlen message digest length (for SIG_RSA_RAW only)
|
||||
* \param hash buffer holding the message digest
|
||||
* \param sig buffer that will hold the ciphertext
|
||||
*
|
||||
* \return 0 if the signing operation was successful,
|
||||
* or an POLARSSL_ERR_RSA_XXX error code
|
||||
*
|
||||
* \note The "sig" buffer must be as large as the size
|
||||
* of ctx->N (eg. 128 bytes if RSA-1024 is used).
|
||||
*/
|
||||
int rsa_pkcs1_sign( rsa_context *ctx,
|
||||
int mode,
|
||||
int hash_id,
|
||||
int hashlen,
|
||||
const unsigned char *hash,
|
||||
unsigned char *sig );
|
||||
|
||||
/**
|
||||
* \brief Do a public RSA and check the message digest
|
||||
*
|
||||
* \param ctx points to an RSA public key
|
||||
* \param mode RSA_PUBLIC or RSA_PRIVATE
|
||||
* \param hash_id SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512}
|
||||
* \param hashlen message digest length (for SIG_RSA_RAW only)
|
||||
* \param hash buffer holding the message digest
|
||||
* \param sig buffer holding the ciphertext
|
||||
*
|
||||
* \return 0 if the verify operation was successful,
|
||||
* or an POLARSSL_ERR_RSA_XXX error code
|
||||
*
|
||||
* \note The "sig" buffer must be as large as the size
|
||||
* of ctx->N (eg. 128 bytes if RSA-1024 is used).
|
||||
*/
|
||||
int rsa_pkcs1_verify( rsa_context *ctx,
|
||||
int mode,
|
||||
int hash_id,
|
||||
int hashlen,
|
||||
const unsigned char *hash,
|
||||
unsigned char *sig );
|
||||
|
||||
/**
|
||||
* \brief Free the components of an RSA key
|
||||
*
|
||||
* \param ctx RSA Context to free
|
||||
*/
|
||||
void rsa_free( rsa_context *ctx );
|
||||
|
||||
/**
|
||||
* \brief Checkup routine
|
||||
*
|
||||
* \return 0 if successful, or 1 if the test failed
|
||||
*/
|
||||
int rsa_self_test( int verbose );
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* rsa.h */
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,155 @@
|
||||
/**
|
||||
* \file sha2.h
|
||||
*
|
||||
* Copyright (C) 2006-2010, Brainspark B.V.
|
||||
*
|
||||
* This file is part of PolarSSL (http://www.polarssl.org)
|
||||
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef POLARSSL_SHA2_H
|
||||
#define POLARSSL_SHA2_H
|
||||
|
||||
/**
|
||||
* \brief SHA-256 context structure
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
unsigned long total[2]; /*!< number of bytes processed */
|
||||
unsigned long state[8]; /*!< intermediate digest state */
|
||||
unsigned char buffer[64]; /*!< data block being processed */
|
||||
|
||||
unsigned char ipad[64]; /*!< HMAC: inner padding */
|
||||
unsigned char opad[64]; /*!< HMAC: outer padding */
|
||||
int is224; /*!< 0 => SHA-256, else SHA-224 */
|
||||
}
|
||||
sha2_context;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \brief SHA-256 context setup
|
||||
*
|
||||
* \param ctx context to be initialized
|
||||
* \param is224 0 = use SHA256, 1 = use SHA224
|
||||
*/
|
||||
void sha2_starts( sha2_context *ctx, int is224 );
|
||||
|
||||
/**
|
||||
* \brief SHA-256 process buffer
|
||||
*
|
||||
* \param ctx SHA-256 context
|
||||
* \param input buffer holding the data
|
||||
* \param ilen length of the input data
|
||||
*/
|
||||
void sha2_update( sha2_context *ctx, const unsigned char *input, int ilen );
|
||||
|
||||
/**
|
||||
* \brief SHA-256 final digest
|
||||
*
|
||||
* \param ctx SHA-256 context
|
||||
* \param output SHA-224/256 checksum result
|
||||
*/
|
||||
void sha2_finish( sha2_context *ctx, unsigned char output[32] );
|
||||
|
||||
/**
|
||||
* \brief Output = SHA-256( input buffer )
|
||||
*
|
||||
* \param input buffer holding the data
|
||||
* \param ilen length of the input data
|
||||
* \param output SHA-224/256 checksum result
|
||||
* \param is224 0 = use SHA256, 1 = use SHA224
|
||||
*/
|
||||
void sha2( const unsigned char *input, int ilen,
|
||||
unsigned char output[32], int is224 );
|
||||
|
||||
/**
|
||||
* \brief Output = SHA-256( file contents )
|
||||
*
|
||||
* \param path input file name
|
||||
* \param output SHA-224/256 checksum result
|
||||
* \param is224 0 = use SHA256, 1 = use SHA224
|
||||
*
|
||||
* \return 0 if successful, 1 if fopen failed,
|
||||
* or 2 if fread failed
|
||||
*/
|
||||
int sha2_file( const char *path, unsigned char output[32], int is224 );
|
||||
|
||||
/**
|
||||
* \brief SHA-256 HMAC context setup
|
||||
*
|
||||
* \param ctx HMAC context to be initialized
|
||||
* \param key HMAC secret key
|
||||
* \param keylen length of the HMAC key
|
||||
* \param is224 0 = use SHA256, 1 = use SHA224
|
||||
*/
|
||||
void sha2_hmac_starts( sha2_context *ctx, const unsigned char *key, int keylen,
|
||||
int is224 );
|
||||
|
||||
/**
|
||||
* \brief SHA-256 HMAC process buffer
|
||||
*
|
||||
* \param ctx HMAC context
|
||||
* \param input buffer holding the data
|
||||
* \param ilen length of the input data
|
||||
*/
|
||||
void sha2_hmac_update( sha2_context *ctx, const unsigned char *input, int ilen );
|
||||
|
||||
/**
|
||||
* \brief SHA-256 HMAC final digest
|
||||
*
|
||||
* \param ctx HMAC context
|
||||
* \param output SHA-224/256 HMAC checksum result
|
||||
*/
|
||||
void sha2_hmac_finish( sha2_context *ctx, unsigned char output[32] );
|
||||
|
||||
/**
|
||||
* \brief SHA-256 HMAC context reset
|
||||
*
|
||||
* \param ctx HMAC context to be reset
|
||||
*/
|
||||
void sha2_hmac_reset( sha2_context *ctx );
|
||||
|
||||
/**
|
||||
* \brief Output = HMAC-SHA-256( hmac key, input buffer )
|
||||
*
|
||||
* \param key HMAC secret key
|
||||
* \param keylen length of the HMAC key
|
||||
* \param input buffer holding the data
|
||||
* \param ilen length of the input data
|
||||
* \param output HMAC-SHA-224/256 result
|
||||
* \param is224 0 = use SHA256, 1 = use SHA224
|
||||
*/
|
||||
void sha2_hmac( const unsigned char *key, int keylen,
|
||||
const unsigned char *input, int ilen,
|
||||
unsigned char output[32], int is224 );
|
||||
|
||||
/**
|
||||
* \brief Checkup routine
|
||||
*
|
||||
* \return 0 if successful, or 1 if the test failed
|
||||
*/
|
||||
int sha2_self_test( int verbose );
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* sha2.h */
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user