Skip to content

Commit

Permalink
Add signature code
Browse files Browse the repository at this point in the history
Signature PQE algos added conmteroller to generate keys message and signature
  • Loading branch information
DaveJW committed Jan 22, 2021
1 parent 904a5f4 commit 0e0bf3c
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 3 deletions.
118 changes: 118 additions & 0 deletions IBCQC_NetCore/Controllers/PQESignController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using IBCQC_NetCore.Functions;
using IBCQC_NetCore.Models;
using IBCQC_NetCore.OqsdotNet;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace IBCQC_NetCore.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class PQESignController : ControllerBase
{
private static CallerInfo callerInfo;
private static CallerInfo participatingPartyInfo;
private static string certSerial;
private readonly ILogger<PQESignController> _logger;
private string algoRequested;

public PQESignController(ILogger<PQESignController> logger)
{
_logger = logger;
}

//in thbis call we get the serial of the person we invite to chat we get the clients form their certificate

[HttpGet("{algoname}")]
public IActionResult Get(string algoname)
{

_logger.LogInformation($"[{DateTime.UtcNow.ToLongTimeString()}] TestOQS called for algorithm: " + algoname);

//TODO : check if this is an algo name or an integer

bool isint = int.TryParse(algoname, out int algonumber);

if (isint)
{

//get the algoname

algoRequested = Enum.GetName(typeof(SupportedAlgorithmsEnum), algonumber);

//because enum has no hypen
algoRequested = algoRequested.Replace("_", "-");

}

else
{
try
{
//see if it is supported
var isConfiged = (SupportedAlgorithmsEnum)System.Enum.Parse(typeof(SupportedAlgorithmsEnum), algoname);
algoRequested = isConfiged.ToString();

//need to chexck for sign algos except falcon
algoRequested = algoRequested.Replace("_", "-");
}

catch
{
return StatusCode(500, "Algorithm requested is not supported or recognised : " + algoname);
}


}

if (string.IsNullOrEmpty(algoRequested))
{
return StatusCode(500, "Algorithm requested is not supported or recognised : " + algoname);

}


Sig signer = new Sig(algoRequested);
// The message to sign
byte[] message = new System.Text.UTF8Encoding().GetBytes("Message to sign for validation");

// Generate the signer's key pair
byte[] public_key;
byte[] secret_key;
signer.keypair(out public_key, out secret_key);

// The signer sends the public key to the verifier

// The signer signs the message
byte[] signature;
signer.sign(out signature, message, secret_key);



SplitKeyHandlerFunction.ByteToHexBitFiddle(message);

// The signer sends the signature to the verifier
//get nw instanmce to do verification

var verifier = new Sig(algoRequested);


// The verifier verifies the signature
if (verifier.verify(message, signature, public_key))
{
return StatusCode(200, "Algorithm requested is supported : " + algoname +":: Signature ::" + SplitKeyHandlerFunction.ByteToHexBitFiddle(signature) +":: Message::" + SplitKeyHandlerFunction.ByteToHexBitFiddle(message) + ":: Public Key::" + SplitKeyHandlerFunction.ByteToHexBitFiddle(public_key));
}


return StatusCode(200, "Failed to verify but Algorithm requested is supported : " + algoname);


}

}
}
4 changes: 2 additions & 2 deletions IBCQC_NetCore/Functions/SplitKeyHandlerFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ internal static ReturnKeyFormat SendKeyParts(short keyParts, byte[] secret_key,
return returnformattedSegment;
}

static string ByteToHexBitFiddle(byte[] bytes)
internal static string ByteToHexBitFiddle(byte[] bytes)
{
char[] c = new char[bytes.Length * 2];
int b;
Expand All @@ -163,7 +163,7 @@ static string ByteToHexBitFiddle(byte[] bytes)
return new string(c);
}

static string ByteToHexBitFiddle(byte[] bytes, int len)
internal static string ByteToHexBitFiddle(byte[] bytes, int len)
{
char[] c = new char[len * 2];
int b;
Expand Down
23 changes: 22 additions & 1 deletion IBCQC_NetCore/OqsdotNet/SupportedAlgorithmsEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,29 @@ public enum SupportedAlgorithmsEnum
/** Algorithm identifier for SIKE p751 KEM. */
SIKE_p751=264,
/** Algorithm identifier for SIKE p751 compressed KEM. */
SIKE_p751_compressed=265
SIKE_p751_compressed=265,


//signatures


DILITHIUM_2=400,
DILITHIUM_3=401,
DILITHIUM_4=402,
Falcon_512=403, //falcon-512
Falcon_1024=404,
picnic_L1_FS=405,
picnic_L1_UR=406,
picnic_L1_full=407,
picnic_L3_FS=408,
picnic_L3_UR=409,
picnic_L3_full=410,
picnic_L5_FS=411,
picnic_L5_UR=412,
picnic_L5_full=413,
picnic3_L1=414,
picnic3_L3=415,
picnic3_L5=416

}
}
4 changes: 4 additions & 0 deletions IBCQC_NetCore/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iis": {
"applicationUrl": "http://localhost/IBCQC_NetCore",
"sslPort": 0
},
"iisExpress": {
"applicationUrl": "http://localhost:58205",
"sslPort": 44317
Expand Down

0 comments on commit 0e0bf3c

Please sign in to comment.