- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在为 NTRUMLS C# 库编写一个 NTRUMLS C 包装器。我遇到了一个问题,我认为我没有通过映射到 C 库入口点的外部函数接口(interface)正确地编码参数数据。
这是我在 ffi.cs
中使用的特定外部函数接口(interface)。
using System;
using System.Runtime.InteropServices;
using NTRUMLS.Params;
namespace NTRUMLS.ffi {
public static class ffi {
[DllImport("ntrumls")]
public static extern int pq_gen_key(ParamSet p, out IntPtr privkey_blob_len, out byte[] privkey_blob, out IntPtr pubkey_blob_len, out byte[] pubkey_blob);
}
}
我在 param.cs
中传递的特定 ParamSet 结构,我认为这可能是导致问题的具体原因。
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace NTRUMLS.Params {
[SerializableAttribute]
[ComVisibleAttribute(true)]
public enum ParamSetId {
Xxx20140508401,
Xxx20140508439,
Xxx20140508593,
Xxx20140508743,
Xxx20151024401,
Xxx20151024443,
Xxx20151024563,
// Xxx20151024509,
Xxx20151024743,
Xxx20151024907,
}
[StructLayout(LayoutKind.Sequential)]
public struct ParamSet {
ParamSetId id;
IntPtr name;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
byte[] oid;
[MarshalAs(UnmanagedType.U1)]
byte n_bits;
[MarshalAs(UnmanagedType.U1)]
byte q_bits;
[MarshalAs(UnmanagedType.U2)]
ushort n;
[MarshalAs(UnmanagedType.I1)]
sbyte p;
[MarshalAs(UnmanagedType.I8)]
long q;
[MarshalAs(UnmanagedType.I8)]
long b_s;
[MarshalAs(UnmanagedType.I8)]
long b_t;
[MarshalAs(UnmanagedType.I8)]
long norm_bound_s;
[MarshalAs(UnmanagedType.I8)]
long norm_bound_t;
[MarshalAs(UnmanagedType.U1)]
byte d1;
[MarshalAs(UnmanagedType.U1)]
byte d2;
[MarshalAs(UnmanagedType.U1)]
byte d3;
[MarshalAs(UnmanagedType.U2)]
ushort padded_n;
public ushort get_n() {
return n;
}
public sbyte get_p() {
return p;
}
public byte get_d1() {
return d1;
}
public byte get_d2() {
return d2;
}
public byte get_d3() {
return d3;
}
public uint product_form_bytes() {
return (uint)(4 * (d1 + d2 + d3));
}
public uint polynomial_bytes() {
return (uint)(padded_n * 8);
}
public uint privkey_packed_bytes() {
return (uint)(5 + 2 * ((2 * ( d1 + d2 + d3) * n_bits + 7) / 8) + ((n +4) / 5));
}
public uint pubkey_packed_bytes() {
return (uint)(5 + (n * q_bits + 7) / 8 + 64);
}
public ParamSet (ParamSetId ID, IntPtr NAME, byte[] OID, byte N_BITS, byte Q_BITS, ushort N, sbyte P, long Q, long B_S, long B_T, long NORM_BOUND_S, long NORM_BOUND_T, byte D1, byte D2, byte D3, ushort PADDED_N) {
id = ID;
name = NAME;
oid = OID;
n_bits = N_BITS;
q_bits = Q_BITS;
n = N;
p = P;
q = Q;;
b_s = B_S;
b_t = B_T;
norm_bound_s = NORM_BOUND_S;
norm_bound_t = NORM_BOUND_T;
d1 = D1;
d2 = D2;
d3 = D3;
padded_n = PADDED_N;
}
}
public static class ParamSets {
/// <summary>
/// 256 bit security parameter
/// </summary>
public static readonly ParamSet Xxx20140508_743 = new ParamSet(
ParamSetId.Xxx20140508743,
Marshal.StringToHGlobalAuto("Xxx20140508743"),
new byte[] {0xff, 0xff, 0xfc},
10,
20,
743,
3,
1 << 20,
336,
112,
(1 << 19) - 336,
(1 << 19) - 112,
11,
11,
15,
768);
}
}
这里是包装器本身 NTRUMLSWrapper.cs
。
using NTRUMLS.ffi;
using NTRUMLS.Params;
using System;
using System.Runtime.InteropServices;
namespace NTRUMLS.Library {
public static class NTRUMLSWrapper {
public static KeyPair generate_keys(ParamSet param) {
int pub_len = 0;
int priv_len = 0;
Console.WriteLine("Param N: " + param.get_n());
Console.WriteLine("Param P: " + param.get_p());
Console.WriteLine("Param D1: " + param.get_d1());
Console.WriteLine("Param D2: " + param.get_d2());
Console.WriteLine("Param D3: " + param.get_d3());
GCHandle pub_len_handle = GCHandle.Alloc(pub_len);
GCHandle priv_len_handle = GCHandle.Alloc(priv_len);
IntPtr privkey_blob_len = (IntPtr)pub_len_handle;
IntPtr pubkey_blob_len = (IntPtr)priv_len_handle;
GCHandle handle = GCHandle.Alloc(param);
IntPtr paramater = (IntPtr)handle;
byte[] pv = new byte[priv_len];
byte[] pb = new byte[pub_len];
// GCHandle pv_handle = GCHandle.Alloc(pv);
// GCHandle pb_handle = GCHandle.Alloc(pb);
var result = ffi.ffi.pq_gen_key(param, out privkey_blob_len, out pv, out pubkey_blob_len, out pb);
Console.WriteLine("Result: " + result + " Private Key BLob Length: " + priv_len + " Public Key Blob Lengh: " + pub_len);
if (result != 0)
Console.WriteLine("We got problems");
byte[] privatekey_blob = new byte[privkey_blob_len.ToInt64()];
byte[] pubkey_blob = new byte[pubkey_blob_len.ToInt64()];
result = ffi.ffi.pq_gen_key(param, out privkey_blob_len, out privatekey_blob, out pubkey_blob_len, out pubkey_blob);
if (result != 0)
Console.WriteLine("We got problems");
Console.WriteLine("Result: " + result.ToString() + " Private Key BLob Length: " + privkey_blob_len + " Public Key Blob Lengh: " + pubkey_blob_len);
// byte[] privkeyBytes = new byte[priv_len.ToInt32()];
// byte[] pubkeyBytes = new byte[pub_len.ToInt32()];
return new KeyPair(new PrivateKey(pubkey_blob), new PublicKey(privatekey_blob));;
}
}
public struct PrivateKey {
byte[] ffi_key;
public PrivateKey (byte[] bytes) {
ffi_key = bytes;
}
public byte[] get_bytes() {
return ffi_key;
}
}
public struct PublicKey {
byte[] ffi_key;
public PublicKey (byte[] bytes) {
ffi_key = bytes;
}
public byte[] get_bytes() {
return ffi_key;
}
}
public struct KeyPair {
PublicKey publicKey;
PrivateKey privateKey;
public KeyPair(PrivateKey privKey, PublicKey pubkey)
{
publicKey = pubkey;
privateKey = privKey;
}
public PublicKey getPublic()
{
return publicKey;
}
public PrivateKey getPrivate()
{
return privateKey;
}
}
}
和 Program.cs
来运行 key 生成测试。
using System;
using NTRUMLS.Library;
using NTRUMLS.Params;
namespace NTRUMLS
{
public class Program
{
public static void Main(string[] args)
{
KeyPair keypair = NTRUMLSWrapper.generate_keys(ParamSets.Xxx20140508_743);
Console.WriteLine("Generated Keys!");
// TODO Sign, than Verify to confirm test
}
}
}
这是我正在编码的 C 函数的头函数
在 pqntrusign.h
int
pq_gen_key(
PQ_PARAM_SET *params,
size_t *privkey_blob_len,
unsigned char *privkey_blob,
size_t *pubkey_blob_len,
unsigned char *pubkey_blob);
在 params.h
和 params.c
中
enum _PQ_PARAM_SET_ID {
XXX_20140508_401,
XXX_20140508_439,
XXX_20140508_593,
XXX_20140508_743,
XXX_20151024_401,
XXX_20151024_443,
XXX_20151024_563,
//XXX_20151024_509,
XXX_20151024_743,
XXX_20151024_907,
};
struct _PQ_PARAM_SET {
PQ_PARAM_SET_ID id; /* parameter set id */
const char *name; /* human readable name */
const uint8_t OID[3]; /* OID */
uint8_t N_bits; /* ceil(log2(N)) */
uint8_t q_bits; /* ceil(log2(q)) */
uint16_t N; /* ring degree */
int8_t p; /* message space prime */
int64_t q; /* ring modulus */
int64_t B_s; /* max norm of f*a convolution */
int64_t B_t; /* max norm of g*a convolution */
int64_t norm_bound_s;/* q/2 - B_s */
int64_t norm_bound_t;/* q/2 - B_t */
uint8_t d1; /* Product form +1/-1 counts */
uint8_t d2;
uint8_t d3;
uint16_t padded_N; /* # Polynomial coefficients for Karatsuba */
};
我相信当前的问题是 ParamSet struct
,以及将 name 属性与 C struct const char*name
指针属性正确匹配。但是,我不完全确定情况是否如此。
我修改了 NTRUMLS C pqntrusign.c
源文件以打印到 log.txt 并记录我传递的变量。这是我在 int pq_gen_key
函数开头添加的片段。
FILE * fp;
fp = fopen ("log.txt", "a+");
fprintf(fp, "Logging made it here variables priv_blob_len_pointer %i pub_blob_len_pointer %i priv_blob_len %i pub_blob_len %i privkey %x pubkey %x \n", privkey_blob_len, pubkey_blob_len, *privkey_blob_len, *pubkey_blob_len, privkey_blob, pubkey_blob);
fprintf(fp, "Logging param ID: %i Oid %x : N: %i Q: %i P %i Padded N: %i D1: %i D2: %i D3: %i \n", P->id, P->OID, P->N, P->q, P->p, P->padded_N, P->d1, P->d2, P->d3);
if(!P || !privkey_blob_len || !pubkey_blob_len)
{
return PQNTRU_ERROR;
}
N = P->N;
padN = P->padded_N;
q = P->q;
p = P->p;
d1 = P->d1;
d2 = P->d2;
d3 = P->d3;
fprintf(fp, "Logging Local N: %i Q: %i P %i Padded N: %i D1: %i D2: %i D3: %i \n", N, q, p, padN, d1, d2, d3);
fclose(fp);
我使用的是 Fedora 23 x64,因此上传到 NTRUMLS-Sharp github 上的当前 libntrumls.so 是在添加了该片段的情况下编译的。
C# 控制台的输出
Param N: 743
Param P: 3
Param D1: 11
Param D2: 11
Param D3: 15
Result: -1 Private Key BLob Length: 0 Public Key Blob Lengh: 0
We got problems
We got problems
Result: -1 Private Key BLob Length: 3 Public Key Blob Lengh: 11
Generated Keys!
log.txt 的输出
Logging made it here variables priv_blob_len_pointer -923442424 pub_blob_len_pointer -923442432 priv_blob_len -994004096 pub_blob_len -994004064 privkey c8f56510 pubkey f0b0b
Logging param ID: 3 Oid c8f56528 : N: 0 Q: 42678032 P 19 Padded N: 0 D1: 11 D2: 0 D3: 0
Logging Local N: 0 Q: 42678032 P 19 Padded N: 0 D1: 11 D2: 0 D3: 0
Logging made it here variables priv_blob_len_pointer -923442424 pub_blob_len_pointer -923442432 priv_blob_len -994003392 pub_blob_len -994003352 privkey c8f56510 pubkey f0b0b
Logging param ID: 3 Oid c8f56528 : N: 0 Q: 42678032 P 19 Padded N: 0 D1: 11 D2: 0 D3: 0
Logging Local N: 0 Q: 42678032 P 19 Padded N: 0 D1: 11 D2: 0 D3: 0
编辑:
另一个需要注意的有趣的事情是记录为 3 的参数 ID 是正确的,因为枚举 Xxx20140508743
是 3,而 D1 是正确的 11。但是每次我运行该函数时,为 OID 记录的输出都是不同的,当它是我的 ParamSet.Xxx20140508_743
中的一组 3 字节数组时。这使我相信错误出在编码(marshal)处理 const char *name
中,这会丢弃其余部分。
下面是另一个用于比较的 log.txt 输出(这也是在 Marshal.StringToHGlobalAuto("Xxx20140508743\0")
中添加了“\0”
Logging made it here variables priv_blob_len_pointer 982750168 pub_blob_len_pointer 982750160 priv_blob_len -960449664 pub_blob_len -960449632 privkey 3a9395e0 pubkey f0b0b
Logging param ID: 3 Oid 3a9395f8 : N: 0 Q: 38160144 P 19 Padded N: 0 D1: 11 D2: 0 D3: 0
Logging Local N: 0 Q: 38160144 P 19 Padded N: 0 D1: 11 D2: 0 D3: 0
Logging made it here variables priv_blob_len_pointer 982750168 pub_blob_len_pointer 982750160 priv_blob_len -960448960 pub_blob_len -960448920 privkey 3a9395e0 pubkey f0b0b
Logging param ID: 3 Oid 3a9395f8 : N: 0 Q: 38160144 P 19 Padded N: 0 D1: 11 D2: 0 D3: 0
Logging Local N: 0 Q: 38160144 P 19 Padded N: 0 D1: 11 D2: 0 D3: 0
我认为 Windows dll 还不能正常工作。如果有人不能立即在我的代码或错误日志中看到问题,编译和测试所有内容的说明都在 NTRUMLS C# README 文件中。
感谢您花时间阅读这个问题!我知道它很长且具有描述性,但我不太确定如何在不提供所有信息的情况下缩小它的范围,因为我花了很多时间搜索论坛并修改我的代码才能做到这一点。
最好的问候
最佳答案
现在一切正常!
编码(marshal)参数结构看起来像这样
IntPtr parameter = Marshal.AllocHGlobal(Marshal.SizeOf(param));
Marshal.StructureToPtr(param, parameter, false);
下面是最终的函数。
public static KeyPair generate_keys(ParamSet param) {
uint pub_len = 0;
uint priv_len = 0;
IntPtr privkey_blob_len = new IntPtr(priv_len);
IntPtr pubkey_blob_len = new IntPtr(pub_len);
IntPtr parameter = Marshal.AllocHGlobal(Marshal.SizeOf(param));
Marshal.StructureToPtr(param, parameter, false);
IntPtr pv_ptr = IntPtr.Zero;
IntPtr pb_ptr = IntPtr.Zero;
var result = ffi.ffi.pq_gen_key(parameter, out privkey_blob_len, pv_ptr, out pubkey_blob_len, pb_ptr);
Console.WriteLine("Result: " + result + " Private Key BLob Length: " + privkey_blob_len.ToInt32() + " Public Key Blob Lengh: " + pubkey_blob_len);
if (result != 0)
Console.WriteLine("We got problems");
pv_ptr = Marshal.AllocHGlobal(privkey_blob_len.ToInt32());
pb_ptr = Marshal.AllocHGlobal(pubkey_blob_len.ToInt32());
result = ffi.ffi.pq_gen_key(parameter, out privkey_blob_len, pv_ptr, out pubkey_blob_len, pb_ptr);
if (result != 0)
Console.WriteLine("We got problems");
Console.WriteLine("Result: " + result.ToString() + " Private Key BLob Length: " + privkey_blob_len + " Public Key Blob Lengh: " + pubkey_blob_len);
byte[] privkeyBytes = new byte[privkey_blob_len.ToInt32()];
byte[] pubkeyBytes = new byte[pubkey_blob_len.ToInt32()];
Marshal.Copy(pv_ptr, privkeyBytes, 0, privkey_blob_len.ToInt32());
Marshal.Copy(pb_ptr, pubkeyBytes, 0, pubkey_blob_len.ToInt32());
Marshal.FreeHGlobal(pv_ptr);
Marshal.FreeHGlobal(pb_ptr);
Marshal.FreeHGlobal(parameter);
return new KeyPair(new PrivateKey(privkeyBytes), new PublicKey(pubkeyBytes));
}
还有我的 friend
public static class ffi {
[DllImport("ntrumls")]
public static extern int pq_gen_key(IntPtr p, out IntPtr privkey_blob_len, IntPtr privkey_blob, out IntPtr pubkey_blob_len, IntPtr pubkey_blob);
}
关于c# - 将 C# 结构编码到 native C 库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37714341/
我需要将文本放在 中在一个 Div 中,在另一个 Div 中,在另一个 Div 中。所以这是它的样子: #document Change PIN
奇怪的事情发生了。 我有一个基本的 html 代码。 html,头部, body 。(因为我收到了一些反对票,这里是完整的代码) 这是我的CSS: html { backgroun
我正在尝试将 Assets 中的一组图像加载到 UICollectionview 中存在的 ImageView 中,但每当我运行应用程序时它都会显示错误。而且也没有显示图像。 我在ViewDidLoa
我需要根据带参数的 perl 脚本的输出更改一些环境变量。在 tcsh 中,我可以使用别名命令来评估 perl 脚本的输出。 tcsh: alias setsdk 'eval `/localhome/
我使用 Windows 身份验证创建了一个新的 Blazor(服务器端)应用程序,并使用 IIS Express 运行它。它将显示一条消息“Hello Domain\User!”来自右上方的以下 Ra
这是我的方法 void login(Event event);我想知道 Kotlin 中应该如何 最佳答案 在 Kotlin 中通配符运算符是 * 。它指示编译器它是未知的,但一旦知道,就不会有其他类
看下面的代码 for story in book if story.title.length < 140 - var story
我正在尝试用 C 语言学习字符串处理。我写了一个程序,它存储了一些音乐轨道,并帮助用户检查他/她想到的歌曲是否存在于存储的轨道中。这是通过要求用户输入一串字符来完成的。然后程序使用 strstr()
我正在学习 sscanf 并遇到如下格式字符串: sscanf("%[^:]:%[^*=]%*[*=]%n",a,b,&c); 我理解 %[^:] 部分意味着扫描直到遇到 ':' 并将其分配给 a。:
def char_check(x,y): if (str(x) in y or x.find(y) > -1) or (str(y) in x or y.find(x) > -1):
我有一种情况,我想将文本文件中的现有行包含到一个新 block 中。 line 1 line 2 line in block line 3 line 4 应该变成 line 1 line 2 line
我有一个新项目,我正在尝试设置 Django 调试工具栏。首先,我尝试了快速设置,它只涉及将 'debug_toolbar' 添加到我的已安装应用程序列表中。有了这个,当我转到我的根 URL 时,调试
在 Matlab 中,如果我有一个函数 f,例如签名是 f(a,b,c),我可以创建一个只有一个变量 b 的函数,它将使用固定的 a=a1 和 c=c1 调用 f: g = @(b) f(a1, b,
我不明白为什么 ForEach 中的元素之间有多余的垂直间距在 VStack 里面在 ScrollView 里面使用 GeometryReader 时渲染自定义水平分隔线。 Scrol
我想知道,是否有关于何时使用 session 和 cookie 的指南或最佳实践? 什么应该和什么不应该存储在其中?谢谢! 最佳答案 这些文档很好地了解了 session cookie 的安全问题以及
我在 scipy/numpy 中有一个 Nx3 矩阵,我想用它制作一个 3 维条形图,其中 X 轴和 Y 轴由矩阵的第一列和第二列的值、高度确定每个条形的 是矩阵中的第三列,条形的数量由 N 确定。
假设我用两种不同的方式初始化信号量 sem_init(&randomsem,0,1) sem_init(&randomsem,0,0) 现在, sem_wait(&randomsem) 在这两种情况下
我怀疑该值如何存储在“WORD”中,因为 PStr 包含实际输出。? 既然Pstr中存储的是小写到大写的字母,那么在printf中如何将其给出为“WORD”。有人可以吗?解释一下? #include
我有一个 3x3 数组: var my_array = [[0,1,2], [3,4,5], [6,7,8]]; 并想获得它的第一个 2
我意识到您可以使用如下方式轻松检查焦点: var hasFocus = true; $(window).blur(function(){ hasFocus = false; }); $(win
我是一名优秀的程序员,十分优秀!