0

In this example code

http://botan.randombit.net/manual/fpe.html

there is a method that I am trying to use in a Visual C++ managed wrapper, but I keep getting compile error on the 'unlock' What is this? (could it be mutex::unlock) And how can i resolve the error?

std::vector<byte> sha1(const std::string& acct_name)
   {
   SHA_160 hash;
   hash.update(acct_name);
   return unlock(hash.final());
   }

Error 16 error C3861: 'unlock': identifier not found

EDIT: My Stdafx.h file now looks like this but it is still not compiling (even after including secmem.h)

#pragma once

#include <botan/botan.h>
#include <botan/fpe_fe1.h>
#include <botan/sha160.h>
#include <botan/secmem.h>
#include <stdexcept>
#include <vector>

EDIT: Additional information - version of Botan library I'm using is Version 1.10.9 (latest Stable). I compiled using the python script and did not exclude any modules (built it with everything) in debug mode.

erotavlas
  • 4,274
  • 4
  • 45
  • 104
  • 1
    Looks like [`unlock`](https://botan.randombit.net/doxygen/namespaceBotan.html#a07b99a8cd5ed8949c8645dc15c8b6659) converts from `secure_vector` to `std::vector`. The online documentation states the method is declared at `secman.h`. – wendelbsilva Jun 22 '15 at 18:44
  • @wendelbsilva still no luck after including the header you mentioned. – erotavlas Jun 22 '15 at 19:00
  • 1
    do you still get the same error after the include? If yes, would you mind adding the version of Botan library you are using. – wendelbsilva Jun 22 '15 at 21:07
  • 1
    BTW, I just noticed that the class `SHA_160` has another [`final`](http://botan.randombit.net/doxygen/classBotan_1_1Buffered__Computation.html#a71e7df2fab206504a5edb69d64d675f5) method where you can pass as a parameter a `vector` to get the output. With this method, you wouldnt need the `unlock` fuction. – wendelbsilva Jun 22 '15 at 21:12
  • 1
    Try to _rebuild_ your solution, sometimes changing a precompiled header and recompiling current source file doesn't pick up the changes – Mike Vine Jun 22 '15 at 22:50
  • @wendelbsilva added more info in my OP - current stable release 1.10.9 – erotavlas Jun 22 '15 at 22:54
  • @erotavlas I hope my answer helps you. – wendelbsilva Jun 23 '15 at 15:18

2 Answers2

1

I just checked and looks like Botan v. 1.10.9 doesn’t have unlock. You have two options.

The version 1.10.9 has another final method where you can pass a vector of byte as reference to get the return.

Something like:

byte out[hash.output_length()];
hash.final(out);

Another option is to convert from SecureVector to std::vector.

SecureVector<byte> temp = hash.final();
std::vector<byte> ret(temp.begin(), temp.end());

Depending of my application, I would chose one over the other.

.

Just in case someone come to this question and is using Botan 1.11.

The method unlock to convert from SecureVector to std::vector is in the header secman.h. In addition, the class SHA_160 has another final method where you can pass as a parameter a std::vector<byte> to get the output. With this method, you wouldnt need the unlock function.

wendelbsilva
  • 774
  • 6
  • 21
  • Thanks for this. Any ideas why in their example (original ink above to FPE example) they pass result of this sha1 method to SymmetricKey. The result is of type std::vector but I see no constructor for SymmetricKey that takes a parameter of that type. – erotavlas Jun 23 '15 at 15:54
  • The [`SymmetricKey`](http://fossies.org/dox/Botan-1.10.9/symkey_8h_source.html) constructor for 1.10.9 doesnt accept `std::vector`. Although, it accept a byte array (first option of the solution). Why they convert? I dont know but I would assume they wants to use `key` in a function that only accept `SymmetricKey` as parameter. That would be my guess. – wendelbsilva Jun 23 '15 at 16:03
  • Ok then since I want to pass the result to create a SymmetricKey then I shall use your first solution. However Intellisense underlines the hash on this line byte out[hash.output_length()]; because it says expression must have a constant value. – erotavlas Jun 23 '15 at 16:07
  • In c++, the variable in the declaration of the array need to be constant at the compilation time. That means `byte out[size]` will not work (sry). You can allocate manually the size you want using `new` (but that's BAD). Or, if you are using c++ 11 you can create a vector `std::vector out(hash.output_length())` and use it in the final function like: `hash.final(out.data);`. – wendelbsilva Jun 23 '15 at 16:15
  • This sha1 method is working now for me, but I have no clue how to pass the result to the SymmetricKey. The example they posted does not work. If you have any idea please let me know. I posted last week to the mailing list for Botan but no reply yet. – erotavlas Jun 23 '15 at 18:23
  • One of the `SymmetricKey` constructor receives an array of bytes as parameter `(const byte in[], size_t len)`. If you have a array of byte, try to see if you can use it. – wendelbsilva Jun 23 '15 at 18:50
0

The tutorial uses this

using namespace Botan;

Maybe it is a namespace problem. Either you also declare the Botan namespace as "using", or you use Botan::unlock instead of unlock.

I'd recommend the second one, even if it seems longer. It's worth it!

KABoissonneault
  • 2,359
  • 18
  • 17